CubicSDR/src/audio/AudioThread.h

152 lines
3.9 KiB
C
Raw Permalink Normal View History

// Copyright (c) Charles J. Cliffe
// SPDX-License-Identifier: GPL-2.0+
#pragma once
#include <queue>
#include <vector>
#include <map>
2014-11-21 21:50:14 -05:00
#include <string>
#include <atomic>
#include <memory>
#include "ThreadBlockingQueue.h"
#include "RtAudio.h"
2014-12-11 19:07:21 -05:00
#include "DemodDefs.h"
class AudioThreadInput {
public:
2021-04-04 22:13:31 -04:00
long long frequency{};
int inputRate{};
int sampleRate{};
int channels{};
float peak{};
int type{};
bool is_squelch_active{};
std::vector<float> data;
2014-12-24 01:28:33 -05:00
AudioThreadInput() :
frequency(0), inputRate(0), sampleRate(0), channels(0), peak(0), type(0), is_squelch_active(false) {
}
2021-04-04 22:13:31 -04:00
explicit AudioThreadInput(AudioThreadInput *copyFrom) {
copy(copyFrom);
}
void copy(AudioThreadInput *copyFrom) {
frequency = copyFrom->frequency;
inputRate = copyFrom->inputRate;
sampleRate = copyFrom->sampleRate;
channels = copyFrom->channels;
peak = copyFrom->peak;
type = copyFrom->type;
is_squelch_active = copyFrom->is_squelch_active;
data.assign(copyFrom->data.begin(), copyFrom->data.end());
}
2021-04-04 22:13:31 -04:00
virtual ~AudioThreadInput() = default;
};
typedef std::shared_ptr<AudioThreadInput> AudioThreadInputPtr;
2017-08-27 05:11:30 -04:00
typedef ThreadBlockingQueue<AudioThreadInputPtr> DemodulatorThreadOutputQueue;
typedef std::shared_ptr<DemodulatorThreadOutputQueue> DemodulatorThreadOutputQueuePtr;
class AudioThreadCommand {
public:
2021-04-22 00:34:45 -04:00
enum class Type {
AUDIO_THREAD_CMD_NULL, AUDIO_THREAD_CMD_SET_DEVICE, AUDIO_THREAD_CMD_SET_SAMPLE_RATE
};
AudioThreadCommand() :
2021-04-22 00:34:45 -04:00
cmdType(AudioThreadCommand::Type::AUDIO_THREAD_CMD_NULL), int_value(0) {
}
2021-04-22 00:34:45 -04:00
AudioThreadCommand::Type cmdType;
int int_value;
};
typedef ThreadBlockingQueue<AudioThreadInputPtr> AudioThreadInputQueue;
typedef ThreadBlockingQueue<AudioThreadCommand> AudioThreadCommandQueue;
typedef std::shared_ptr<AudioThreadInputQueue> AudioThreadInputQueuePtr;
typedef std::shared_ptr<AudioThreadCommandQueue> AudioThreadCommandQueuePtr;
2015-07-29 20:57:02 -04:00
class AudioThread : public IOThread {
2017-09-01 00:18:35 -04:00
public:
AudioThread();
2021-04-04 22:13:31 -04:00
~AudioThread() override;
2014-11-21 21:50:14 -05:00
2014-12-31 19:45:01 -05:00
static void enumerateDevices(std::vector<RtAudio::DeviceInfo> &devs);
2021-04-04 22:13:31 -04:00
void setInitOutputDevice(int deviceId, int sampleRate_in = -1);
int getOutputDevice();
int getSampleRate();
2017-09-01 00:18:35 -04:00
2021-04-04 22:13:31 -04:00
void run() override;
void terminate() override;
bool isActive();
void setActive(bool state);
2015-01-10 20:33:30 -05:00
void setGain(float gain_in);
2017-09-01 00:18:35 -04:00
static std::map<int, int> deviceSampleRate;
2015-01-10 20:33:30 -05:00
AudioThreadCommandQueue *getCommandQueue();
2017-09-01 00:18:35 -04:00
//give access to the this AudioThread lock
std::recursive_mutex& getMutex();
static void deviceCleanup();
static void setDeviceSampleRate(int deviceId, int sampleRate);
//
void attachControllerThread(std::thread* controllerThread);
2017-09-01 00:18:35 -04:00
//fields below, only to be used by other AudioThreads !
size_t underflowCount;
2017-09-01 00:18:35 -04:00
//protected by m_mutex
std::vector<AudioThread *> boundThreads;
AudioThreadInputQueuePtr inputQueue;
AudioThreadInputPtr currentInput;
size_t audioQueuePtr;
float gain;
2017-09-01 00:18:35 -04:00
private:
2017-09-01 00:18:35 -04:00
std::atomic_bool active;
std::atomic_int outputDevice;
RtAudio dac;
unsigned int nBufferFrames;
RtAudio::StreamOptions opts;
RtAudio::StreamParameters parameters;
AudioThreadCommandQueue cmdQueue;
int sampleRate;
//if != nullptr, it mean AudioThread is a controller thread.
std::thread* controllerThread;
//The own m_mutex protecting this AudioThread, in particular boundThreads
std::recursive_mutex m_mutex;
2017-09-01 00:18:35 -04:00
void setupDevice(int deviceId);
2021-04-04 22:13:31 -04:00
void setSampleRate(int sampleRate_in);
2014-12-18 20:32:05 -05:00
void bindThread(AudioThread *other);
void removeThread(AudioThread *other);
static std::map<int, AudioThread* > deviceController;
//The mutex protecting static deviceController, deviceThread and deviceSampleRate access.
static std::recursive_mutex m_device_mutex;
2014-11-21 21:50:14 -05:00
};