#ifndef AUDIOHANDLER_H #define AUDIOHANDLER_H #include #include #include #include #include "rtaudio/RtAudio.h" typedef signed short MY_TYPE; #define FORMAT RTAUDIO_SINT16 #define SCALE 32767.0 #include #include #include #include #include "resampler/speex_resampler.h" #include //#define BUFFER_SIZE (32*1024) #define INTERNAL_SAMPLE_RATE 48000 #define MULAW_BIAS 33 #define MULAW_MAX 0x1fff struct audioPacket { quint32 seq; QTime time; quint16 sent; QByteArray datain; QByteArray dataout; }; class audioHandler : public QObject { Q_OBJECT public: audioHandler(QObject* parent = 0); ~audioHandler(); void getLatency(); bool setDevice(QAudioDeviceInfo deviceInfo); void start(); void flush(); void stop(); qint64 bytesAvailable() const; bool isSequential() const; void getNextAudioChunk(QByteArray &data); bool isChunkAvailable(); private slots: bool init(const quint8 bits, const quint8 channels, const quint16 samplerate, const quint16 latency, const bool isulaw, const bool isinput, int port, quint8 resampleQuality); void incomingAudio(const audioPacket data); void changeLatency(const quint16 newSize); void notified(); void stateChanged(QAudio::State state); void setVolume(unsigned char volume); signals: void audioMessage(QString message); void sendLatency(quint16 newSize); void haveAudioData(const QByteArray& data); private: int readData(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status); static int staticRead(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void* userData) { return static_cast(userData)->readData(outputBuffer, inputBuffer, nFrames, streamTime, status); } int writeData(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status); static int staticWrite(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void* userData) { return static_cast(userData)->writeData(outputBuffer, inputBuffer, nFrames, streamTime, status); } void reinit(); bool isInitialized; RtAudio audio; int audioDevice = 0; RtAudio::StreamParameters aParams; RtAudio::DeviceInfo info; SpeexResamplerState* resampler = Q_NULLPTR; bool isUlaw; quint16 audioLatency; bool isInput; // Used to determine whether input or output audio unsigned int chunkSize; bool chunkAvailable; quint32 lastSeq; quint16 radioSampleRate; quint8 radioSampleBits; quint8 radioChannels; QMapaudioBuffer; unsigned int ratioNum; unsigned int ratioDen; QMutex mutex; volatile bool lock=false; }; #endif // AUDIOHANDLER_H