CubicSDR/src/audio/AudioThread.h

105 lines
2.5 KiB
C
Raw Normal View History

#pragma once
#include <queue>
#include <vector>
#include <map>
2014-11-21 21:50:14 -05:00
#include <string>
#include <atomic>
#include "AudioThread.h"
2014-11-21 21:50:14 -05:00
#include "ThreadQueue.h"
#include "RtAudio.h"
2014-12-11 19:07:21 -05:00
#include "DemodDefs.h"
2014-12-24 01:28:33 -05:00
class AudioThreadInput: public ReferenceCounter {
public:
long long frequency;
int inputRate;
int sampleRate;
int channels;
float peak;
int type;
std::vector<float> data;
2015-05-27 23:22:19 -04:00
std::mutex busy_update;
2014-12-24 01:28:33 -05:00
AudioThreadInput() :
frequency(0), sampleRate(0), channels(0), peak(0) {
}
~AudioThreadInput() {
std::lock_guard < std::mutex > lock(m_mutex);
}
};
class AudioThreadCommand {
public:
enum AudioThreadCommandEnum {
AUDIO_THREAD_CMD_NULL, AUDIO_THREAD_CMD_SET_DEVICE, AUDIO_THREAD_CMD_SET_SAMPLE_RATE
};
AudioThreadCommand() :
cmd(AUDIO_THREAD_CMD_NULL), int_value(0) {
}
AudioThreadCommandEnum cmd;
int int_value;
};
2014-12-23 01:59:03 -05:00
typedef ThreadQueue<AudioThreadInput *> AudioThreadInputQueue;
typedef ThreadQueue<AudioThreadCommand> AudioThreadCommandQueue;
2015-07-29 20:57:02 -04:00
class AudioThread : public IOThread {
public:
2014-12-23 01:59:03 -05:00
AudioThreadInput *currentInput;
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;
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);
void setupDevice(int deviceId);
void setInitOutputDevice(int deviceId, int sampleRate=-1);
int getOutputDevice();
void setSampleRate(int sampleRate);
int getSampleRate();
2015-07-29 20:57:02 -04:00
void run();
void terminate();
bool isActive();
void setActive(bool state);
2015-01-10 20:33:30 -05:00
void setGain(float gain_in);
float getGain();
AudioThreadCommandQueue *getCommandQueue();
private:
RtAudio dac;
unsigned int nBufferFrames;
RtAudio::StreamOptions opts;
RtAudio::StreamParameters parameters;
AudioThreadCommandQueue cmdQueue;
2014-12-11 19:07:21 -05:00
DemodulatorThreadCommandQueue* threadQueueNotify;
int sampleRate;
public:
2014-12-18 20:32:05 -05:00
void bindThread(AudioThread *other);
void removeThread(AudioThread *other);
static std::map<int,AudioThread *> deviceController;
static std::map<int,int> deviceSampleRate;
static std::map<int,std::thread *> deviceThread;
static void deviceCleanup();
static void setDeviceSampleRate(int deviceId, int sampleRate);
std::atomic<std::vector<AudioThread *> *> boundThreads;
2014-11-21 21:50:14 -05:00
};