1 #ifndef SOUNDBASE_H
2 #define SOUNDBASE_H
3 
4 #include "appglobal.h"
5 #include "wavio.h"
6 #include "buffermanag.h"
7 #include "downsamplefilter.h"
8 
9 
10 #include <QThread>
11 #include <QMutex>
12 
13 #define BYTESPOWER 18
14 
15 
16 //WWV WWVH             2500.0, 5000.0, 10000.0
17 
18 //·      GBR                           60.0 kHz
19 
20 //·      RWM                          4996.0, 9996.0, 14996.0
21 
22 //·      CHU                            7335.0
23 
24 
25 
26 #define PERIODSIZE (DOWNSAMPLESIZE)
27 #define BUFFERSIZE (8*DOWNSAMPLESIZE)
28 #define CALIBRATIONSIZE (PERIODSIZE)
29 #define CALIBRATIONLEADIN 80
30 
31 
32 class soundBase : public QThread
33 {
34   Q_OBJECT
35 
36 public:
37   enum edataSrc{SNDINCARD,SNDINFROMFILE,SNDINCARDTOFILE};
38   enum edataDst{SNDOUTCARD,SNDOUTTOFILE};
39   enum eplaybackState{PBINIT,PBSTARTING,PBRUNNING,PBCALIBRATESTART,PBCALIBRATEWAIT,PBCALIBRATE,PBEND};
40   enum ecaptureState{CPINIT,CPSTARTING,CPRUNNING,CPCALIBRATESTART,CPCALIBRATEWAIT,CPCALIBRATE,CPEND};
41 
42   explicit soundBase(QObject *parent = 0);
43   ~soundBase();
44   virtual bool init(int samplerate)=0;
45   void run();
46   void idleRX();
47   void idleTX();
48   void stopSoundThread();
getCardList()49   virtual void getCardList() {;}
50 
51   bool startCapture();
52   bool startPlayback();
53   buffer<FILTERPARAMTYPE,BYTESPOWER> rxBuffer;
54   buffer<FILTERPARAMTYPE,BYTESPOWER> rxVolumeBuffer;
55   buffer<SOUNDFRAME,16> txBuffer;
getVolumeDb()56   double getVolumeDb(){return volume;}
getVolumePtr()57   FILTERPARAMTYPE *getVolumePtr() {return downsampleFilterPtr->getVolumePtr();}
getLastError()58   const QString getLastError() { return lastErrorStr;}
isPlaying()59   bool isPlaying() {return playbackState!=PBINIT;}
isCapturing()60   bool isCapturing() {return captureState!=CPINIT;}
61 
62   bool calibrate(bool isCapture);
63   bool calibrationCount(unsigned int &frames, double &elapsedTime);
64   int countAvailable;
65 signals:
66 
67 public slots:
68 
69 protected:
70   bool soundDriverOK;
71   bool isStereo;
72   int capture();
73   int play();
74 
75   virtual int read(int &countAvailable)=0;
76   virtual int write(uint numFrames)=0;
77   virtual void flushCapture()=0;
78   virtual void flushPlayback()=0;
prepareCapture()79   virtual void prepareCapture() {;}
preparePlayback()80   virtual void preparePlayback() {;}
81   virtual void closeDevices()=0;
82   virtual void waitPlaybackEnd()=0;
83 
84 
85   int sampleRate;
86   qint16 tempRXBuffer[DOWNSAMPLESIZE*2*2]; // in some cases the hardware interface is stereo (can be S16_LE or S32_LE)
87   quint32 tempTXBuffer[DOWNSAMPLESIZE*2];
88   bool stopThread;
89   eplaybackState playbackState;
90   ecaptureState  captureState;
91 
92 
93   wavIO waveIn;
94   wavIO waveOut;
95   void errorHandler(QString title,QString info);
96   void switchCaptureState(ecaptureState cs);
97   void switchPlaybackState(eplaybackState ps);
98 
99 
100 
101 private:
102   downsampleFilter *downsampleFilterPtr;
103   double volume;
104 //  uint intVolume;
105   int captureCalibration(bool leadIn);
106   int playbackCalibration(bool leadIn);
107   QMutex mutex;
108   QElapsedTimer stopwatch;
109   unsigned int calibrationFrames;
110   unsigned int leadInCounter;
111   int calibrationTime;
112   double ucalibrationTime;
113   double ustartcalibrationTime;
114   struct timespec ts;
115   QString lastErrorStr;
116   quint64 storedFrames;
117   bool prebuf;
118   unsigned int prevFrames;
119 
120 
121 };
122 
123 #endif // SOUNDBASE_H
124