Multiple audio support: use device names for sample rates and volumes maps

This commit is contained in:
f4exb 2018-03-25 01:47:22 +01:00
parent a4b6edbf07
commit c500b1853b
4 changed files with 104 additions and 21 deletions

View File

@ -18,8 +18,9 @@
#include <audio/audiodevicemanager.h> #include <audio/audiodevicemanager.h>
#include "util/simpleserializer.h" #include "util/simpleserializer.h"
AudioDeviceManager::AudioDeviceManager(unsigned int defaultAudioSampleRate) : const float AudioDeviceManager::m_defaultAudioInputVolume = 0.15f;
m_defaultAudioSampleRate(defaultAudioSampleRate)
AudioDeviceManager::AudioDeviceManager()
{ {
m_inputDevicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); m_inputDevicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
m_outputDevicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput); m_outputDevicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
@ -34,6 +35,48 @@ AudioDeviceManager::~AudioDeviceManager()
} }
} }
bool AudioDeviceManager::getOutputDeviceName(int outputDeviceIndex, QString &deviceName) const
{
if (outputDeviceIndex < 0)
{
deviceName = "System default device";
return true;
}
else
{
if (outputDeviceIndex < m_outputDevicesInfo.size())
{
deviceName = m_outputDevicesInfo[outputDeviceIndex].deviceName();
return true;
}
else
{
return false;
}
}
}
bool AudioDeviceManager::getInputDeviceName(int inputDeviceIndex, QString &deviceName) const
{
if (inputDeviceIndex < 0)
{
deviceName = "System default device";
return true;
}
else
{
if (inputDeviceIndex < m_inputDevicesInfo.size())
{
deviceName = m_inputDevicesInfo[inputDeviceIndex].deviceName();
return true;
}
else
{
return false;
}
}
}
void AudioDeviceManager::resetToDefaults() void AudioDeviceManager::resetToDefaults()
{ {
} }
@ -68,10 +111,8 @@ void AudioDeviceManager::addAudioSink(AudioFifo* audioFifo, int outputDeviceInde
{ {
qDebug("AudioDeviceManager::addAudioSink: %d: %p", outputDeviceIndex, audioFifo); qDebug("AudioDeviceManager::addAudioSink: %d: %p", outputDeviceIndex, audioFifo);
if (m_audioOutputs.find(outputDeviceIndex) == m_audioOutputs.end()) if (m_audioOutputs.find(outputDeviceIndex) == m_audioOutputs.end()) {
{
m_audioOutputs[outputDeviceIndex] = new AudioOutput(); m_audioOutputs[outputDeviceIndex] = new AudioOutput();
m_audioOutputSampleRates[outputDeviceIndex] = m_defaultAudioSampleRate;
} }
if (m_audioOutputs[outputDeviceIndex]->getNbFifos() == 0) { if (m_audioOutputs[outputDeviceIndex]->getNbFifos() == 0) {
@ -120,10 +161,8 @@ void AudioDeviceManager::addAudioSource(AudioFifo* audioFifo, int inputDeviceInd
{ {
qDebug("AudioDeviceManager::addAudioSource: %d: %p", inputDeviceIndex, audioFifo); qDebug("AudioDeviceManager::addAudioSource: %d: %p", inputDeviceIndex, audioFifo);
if (m_audioInputs.find(inputDeviceIndex) == m_audioInputs.end()) if (m_audioInputs.find(inputDeviceIndex) == m_audioInputs.end()) {
{
m_audioInputs[inputDeviceIndex] = new AudioInput(); m_audioInputs[inputDeviceIndex] = new AudioInput();
m_audioInputSampleRates[inputDeviceIndex] = m_defaultAudioSampleRate;
} }
if (m_audioInputs[inputDeviceIndex]->getNbFifos() == 0) { if (m_audioInputs[inputDeviceIndex]->getNbFifos() == 0) {
@ -170,8 +209,24 @@ void AudioDeviceManager::removeAudioSource(AudioFifo* audioFifo)
void AudioDeviceManager::startAudioOutput(int outputDeviceIndex) void AudioDeviceManager::startAudioOutput(int outputDeviceIndex)
{ {
m_audioOutputs[outputDeviceIndex]->start(outputDeviceIndex, m_audioOutputSampleRates[outputDeviceIndex]); unsigned int sampleRate;
m_audioOutputSampleRates[outputDeviceIndex] = m_audioOutputs[outputDeviceIndex]->getRate(); // update with actual rate QString deviceName;
if (getOutputDeviceName(outputDeviceIndex, deviceName))
{
if (m_audioOutputSampleRates.find(deviceName) == m_audioOutputSampleRates.end()) {
sampleRate = m_defaultAudioSampleRate;
} else {
sampleRate = m_audioOutputSampleRates[deviceName];
}
m_audioOutputs[outputDeviceIndex]->start(outputDeviceIndex, sampleRate);
m_audioOutputSampleRates[deviceName] = m_audioOutputs[outputDeviceIndex]->getRate(); // update with actual rate
}
else
{
qWarning("AudioDeviceManager::startAudioOutput: unknown device index %d", outputDeviceIndex);
}
} }
void AudioDeviceManager::stopAudioOutput(int outputDeviceIndex) void AudioDeviceManager::stopAudioOutput(int outputDeviceIndex)
@ -181,8 +236,32 @@ void AudioDeviceManager::stopAudioOutput(int outputDeviceIndex)
void AudioDeviceManager::startAudioInput(int inputDeviceIndex) void AudioDeviceManager::startAudioInput(int inputDeviceIndex)
{ {
m_audioInputs[inputDeviceIndex]->start(inputDeviceIndex, m_audioInputSampleRates[inputDeviceIndex]); unsigned int sampleRate;
m_audioInputSampleRates[inputDeviceIndex] = m_audioInputs[inputDeviceIndex]->getRate(); // update with actual rate float volume;
QString deviceName;
if (getInputDeviceName(inputDeviceIndex, deviceName))
{
if (m_audioInputSampleRates.find(deviceName) == m_audioInputSampleRates.end()) {
sampleRate = m_defaultAudioSampleRate;
} else {
sampleRate = m_audioInputSampleRates[deviceName];
}
if (m_audioInputVolumes.find(deviceName) == m_audioInputVolumes.end()) {
volume = m_defaultAudioInputVolume;
} else {
volume = m_audioInputVolumes[deviceName];
}
m_audioInputs[inputDeviceIndex]->start(inputDeviceIndex, sampleRate);
m_audioInputSampleRates[deviceName] = m_audioInputs[inputDeviceIndex]->getRate(); // update with actual rate
m_audioInputs[inputDeviceIndex]->setVolume(volume);
}
else
{
qWarning("AudioDeviceManager::startAudioInput: unknown device index %d", inputDeviceIndex);
}
} }
void AudioDeviceManager::stopAudioInput(int inputDeviceIndex) void AudioDeviceManager::stopAudioInput(int inputDeviceIndex)

View File

@ -31,31 +31,37 @@ class AudioFifo;
class SDRBASE_API AudioDeviceManager { class SDRBASE_API AudioDeviceManager {
public: public:
AudioDeviceManager(unsigned int defaultAudioSampleRate); AudioDeviceManager();
~AudioDeviceManager(); ~AudioDeviceManager();
const QList<QAudioDeviceInfo>& getInputDevices() const { return m_inputDevicesInfo; } const QList<QAudioDeviceInfo>& getInputDevices() const { return m_inputDevicesInfo; }
const QList<QAudioDeviceInfo>& getOutputDevices() const { return m_outputDevicesInfo; } const QList<QAudioDeviceInfo>& getOutputDevices() const { return m_outputDevicesInfo; }
bool getOutputDeviceName(int outputDeviceIndex, QString &deviceName) const;
bool getInputDeviceName(int outputDeviceIndex, QString &deviceName) const;
void addAudioSink(AudioFifo* audioFifo, int outputDeviceIndex = -1); //!< Add the audio sink void addAudioSink(AudioFifo* audioFifo, int outputDeviceIndex = -1); //!< Add the audio sink
void removeAudioSink(AudioFifo* audioFifo); //!< Remove the audio sink void removeAudioSink(AudioFifo* audioFifo); //!< Remove the audio sink
void addAudioSource(AudioFifo* audioFifo, int inputDeviceIndex = -1); //!< Add an audio source void addAudioSource(AudioFifo* audioFifo, int inputDeviceIndex = -1); //!< Add an audio source
void removeAudioSource(AudioFifo* audioFifo); //!< Remove an audio source void removeAudioSource(AudioFifo* audioFifo); //!< Remove an audio source
static const unsigned int m_defaultAudioSampleRate = 48000;
static const float m_defaultAudioInputVolume;
private: private:
unsigned int m_defaultAudioSampleRate;
QList<QAudioDeviceInfo> m_inputDevicesInfo; QList<QAudioDeviceInfo> m_inputDevicesInfo;
QList<QAudioDeviceInfo> m_outputDevicesInfo; QList<QAudioDeviceInfo> m_outputDevicesInfo;
QMap<AudioFifo*, int> m_audioSinkFifos; //< Audio sink FIFO to audio output device index-1 map QMap<AudioFifo*, int> m_audioSinkFifos; //< Audio sink FIFO to audio output device index-1 map
QMap<int, AudioOutput*> m_audioOutputs; //!< audio device index-1 to audio output map (index -1 is default device) QMap<int, AudioOutput*> m_audioOutputs; //!< audio device index to audio output map (index -1 is default device)
QMap<int, unsigned int> m_audioOutputSampleRates; //!< audio device index-1 to audio sample rate QMap<QString, unsigned int> m_audioOutputSampleRates; //!< audio device name to audio sample rate
QMap<AudioFifo*, int> m_audioSourceFifos; //< Audio source FIFO to audio input device index-1 map QMap<AudioFifo*, int> m_audioSourceFifos; //< Audio source FIFO to audio input device index-1 map
QMap<int, AudioInput*> m_audioInputs; //!< audio device index-1 to audio input map (index -1 is default device) QMap<int, AudioInput*> m_audioInputs; //!< audio device index to audio input map (index -1 is default device)
QMap<int, unsigned int> m_audioInputSampleRates; //!< audio device index-1 to audio sample rate QMap<QString, unsigned int> m_audioInputSampleRates; //!< audio device name to audio sample rate
QMap<QString, float> m_audioInputVolumes; //!< audio device name to input volume
void resetToDefaults(); void resetToDefaults();
QByteArray serialize() const; QByteArray serialize() const;

View File

@ -26,7 +26,6 @@
DSPEngine::DSPEngine() : DSPEngine::DSPEngine() :
m_deviceSourceEnginesUIDSequence(0), m_deviceSourceEnginesUIDSequence(0),
m_deviceSinkEnginesUIDSequence(0), m_deviceSinkEnginesUIDSequence(0),
m_audioDeviceManager(m_defaultAudioSampleRate),
m_audioInputDeviceIndex(-1), // default device m_audioInputDeviceIndex(-1), // default device
m_audioOutputDeviceIndex(-1) // default device m_audioOutputDeviceIndex(-1) // default device
{ {

View File

@ -41,7 +41,7 @@ public:
static DSPEngine *instance(); static DSPEngine *instance();
unsigned int getDefaultAudioSampleRate() const { return m_defaultAudioSampleRate; } unsigned int getDefaultAudioSampleRate() const { return AudioDeviceManager::m_defaultAudioSampleRate; }
DSPDeviceSourceEngine *addDeviceSourceEngine(); DSPDeviceSourceEngine *addDeviceSourceEngine();
void removeLastDeviceSourceEngine(); void removeLastDeviceSourceEngine();
@ -82,7 +82,6 @@ private:
int m_audioOutputDeviceIndex; int m_audioOutputDeviceIndex;
QTimer m_masterTimer; QTimer m_masterTimer;
bool m_dvSerialSupport; bool m_dvSerialSupport;
static const unsigned int m_defaultAudioSampleRate = 48000;
#ifdef DSD_USE_SERIALDV #ifdef DSD_USE_SERIALDV
DVSerialEngine m_dvSerialEngine; DVSerialEngine m_dvSerialEngine;
#endif #endif