2014-11-16 16:50:37 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
2014-12-18 20:11:25 -05:00
|
|
|
#include <map>
|
2014-11-21 21:50:14 -05:00
|
|
|
#include <string>
|
2014-11-23 19:39:27 -05:00
|
|
|
#include <atomic>
|
2014-11-16 16:50:37 -05:00
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
#include "AudioThread.h"
|
2014-11-21 21:50:14 -05:00
|
|
|
#include "ThreadQueue.h"
|
2014-12-04 19:03:02 -05:00
|
|
|
#include "RtAudio.h"
|
2014-12-11 19:07:21 -05:00
|
|
|
#include "DemodDefs.h"
|
2014-11-21 00:49:41 -05:00
|
|
|
|
2014-12-24 01:28:33 -05:00
|
|
|
class AudioThreadInput: public ReferenceCounter {
|
2014-11-22 20:57:06 -05:00
|
|
|
public:
|
2015-01-04 17:11:20 -05:00
|
|
|
long long frequency;
|
2015-08-24 01:31:37 -04:00
|
|
|
int inputRate;
|
2014-11-22 20:57:06 -05:00
|
|
|
int sampleRate;
|
2014-12-21 17:37:41 -05:00
|
|
|
int channels;
|
2015-02-08 18:59:08 -05:00
|
|
|
float peak;
|
2015-11-29 13:35:12 -05:00
|
|
|
int type;
|
2014-12-24 00:11:41 -05:00
|
|
|
std::vector<float> data;
|
2015-05-27 23:22:19 -04:00
|
|
|
std::mutex busy_update;
|
2014-12-21 17:37:41 -05:00
|
|
|
|
2014-12-24 01:28:33 -05:00
|
|
|
AudioThreadInput() :
|
2015-02-08 18:59:08 -05:00
|
|
|
frequency(0), sampleRate(0), channels(0), peak(0) {
|
2014-12-21 17:37:41 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~AudioThreadInput() {
|
2014-12-27 15:04:43 -05:00
|
|
|
std::lock_guard < std::mutex > lock(m_mutex);
|
2014-12-21 17:37:41 -05:00
|
|
|
}
|
2014-11-22 20:57:06 -05:00
|
|
|
};
|
|
|
|
|
2014-12-15 20:47:46 -05:00
|
|
|
class AudioThreadCommand {
|
|
|
|
public:
|
|
|
|
enum AudioThreadCommandEnum {
|
2015-03-13 22:25:07 -04:00
|
|
|
AUDIO_THREAD_CMD_NULL, AUDIO_THREAD_CMD_SET_DEVICE, AUDIO_THREAD_CMD_SET_SAMPLE_RATE
|
2014-12-15 20:47:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
AudioThreadCommand() :
|
2014-12-18 20:11:25 -05:00
|
|
|
cmd(AUDIO_THREAD_CMD_NULL), int_value(0) {
|
2014-12-15 20:47:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioThreadCommandEnum cmd;
|
|
|
|
int int_value;
|
|
|
|
};
|
|
|
|
|
2014-12-23 01:59:03 -05:00
|
|
|
typedef ThreadQueue<AudioThreadInput *> AudioThreadInputQueue;
|
2014-12-15 20:47:46 -05:00
|
|
|
typedef ThreadQueue<AudioThreadCommand> AudioThreadCommandQueue;
|
2014-11-22 20:57:06 -05:00
|
|
|
|
2015-07-29 20:57:02 -04:00
|
|
|
class AudioThread : public IOThread {
|
2014-11-16 16:50:37 -05:00
|
|
|
public:
|
2014-12-23 01:59:03 -05:00
|
|
|
AudioThreadInput *currentInput;
|
2014-12-15 20:47:46 -05:00
|
|
|
AudioThreadInputQueue *inputQueue;
|
2015-07-19 23:19:10 -04:00
|
|
|
std::atomic_uint audioQueuePtr;
|
|
|
|
std::atomic_uint underflowCount;
|
|
|
|
std::atomic_bool initialized;
|
|
|
|
std::atomic_bool active;
|
|
|
|
std::atomic_int outputDevice;
|
2015-01-10 20:33:30 -05:00
|
|
|
std::atomic<float> gain;
|
2014-12-04 19:44:49 -05:00
|
|
|
|
2015-07-30 19:30:46 -04:00
|
|
|
AudioThread();
|
2014-11-22 22:33:32 -05:00
|
|
|
~AudioThread();
|
2014-11-21 21:50:14 -05:00
|
|
|
|
2014-12-31 19:45:01 -05:00
|
|
|
static void enumerateDevices(std::vector<RtAudio::DeviceInfo> &devs);
|
2014-12-18 20:11:25 -05:00
|
|
|
|
2014-12-31 21:31:37 -05:00
|
|
|
void setupDevice(int deviceId);
|
2015-03-13 22:25:07 -04:00
|
|
|
void setInitOutputDevice(int deviceId, int sampleRate=-1);
|
2014-12-31 21:31:37 -05:00
|
|
|
int getOutputDevice();
|
2015-03-13 22:25:07 -04:00
|
|
|
void setSampleRate(int sampleRate);
|
2015-03-15 21:02:26 -04:00
|
|
|
int getSampleRate();
|
2015-07-29 20:57:02 -04:00
|
|
|
void run();
|
2014-11-23 19:39:27 -05:00
|
|
|
void terminate();
|
2014-11-22 20:57:06 -05:00
|
|
|
|
2014-12-18 20:11:25 -05:00
|
|
|
bool isActive();
|
|
|
|
void setActive(bool state);
|
|
|
|
|
2015-01-10 20:33:30 -05:00
|
|
|
void setGain(float gain_in);
|
|
|
|
float getGain();
|
|
|
|
|
2014-12-31 21:31:37 -05:00
|
|
|
AudioThreadCommandQueue *getCommandQueue();
|
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
private:
|
2014-12-04 19:44:49 -05:00
|
|
|
RtAudio dac;
|
2015-03-13 22:25:07 -04:00
|
|
|
unsigned int nBufferFrames;
|
|
|
|
RtAudio::StreamOptions opts;
|
2014-12-18 20:11:25 -05:00
|
|
|
RtAudio::StreamParameters parameters;
|
2014-12-15 20:47:46 -05:00
|
|
|
AudioThreadCommandQueue cmdQueue;
|
2014-12-11 19:07:21 -05:00
|
|
|
DemodulatorThreadCommandQueue* threadQueueNotify;
|
2015-03-13 22:25:07 -04:00
|
|
|
int sampleRate;
|
2014-12-18 20:11:25 -05:00
|
|
|
|
|
|
|
public:
|
2014-12-18 20:32:05 -05:00
|
|
|
void bindThread(AudioThread *other);
|
|
|
|
void removeThread(AudioThread *other);
|
|
|
|
|
2014-12-18 20:11:25 -05:00
|
|
|
static std::map<int,AudioThread *> deviceController;
|
2015-03-13 22:25:07 -04:00
|
|
|
static std::map<int,int> deviceSampleRate;
|
2014-12-18 20:11:25 -05:00
|
|
|
static std::map<int,std::thread *> deviceThread;
|
|
|
|
static void deviceCleanup();
|
2015-03-13 22:25:07 -04:00
|
|
|
static void setDeviceSampleRate(int deviceId, int sampleRate);
|
2014-12-18 20:11:25 -05:00
|
|
|
std::atomic<std::vector<AudioThread *> *> boundThreads;
|
2014-11-21 21:50:14 -05:00
|
|
|
};
|
2014-11-22 20:57:06 -05:00
|
|
|
|