Skip to content

Commit

Permalink
Merge pull request #2038 from mxi-box/audio
Browse files Browse the repository at this point in the history
Audio optimize & default input/output bugfix
  • Loading branch information
f4exb authored Mar 30, 2024
2 parents 8f33bad + 556cc8a commit aa95a5b
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 81 deletions.
3 changes: 1 addition & 2 deletions plugins/samplesource/audioinput/audioinputgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ void AudioInputGui::updateSampleRateAndFrequency()
void AudioInputGui::refreshDeviceList()
{
ui->device->blockSignals(true);
AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager();
const QList<AudioDeviceInfo>& audioList = audioDeviceManager->getInputDevices();
const QList<AudioDeviceInfo>& audioList = AudioDeviceInfo::availableInputDevices();

ui->device->clear();
for (const auto &itAudio : audioList)
Expand Down
2 changes: 1 addition & 1 deletion plugins/samplesource/fcdpro/fcdproinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void FCDProInput::closeDevice()
bool FCDProInput::openFCDAudio(const char* cardname)
{
AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager();
const QList<AudioDeviceInfo>& audioList = audioDeviceManager->getInputDevices();
const QList<AudioDeviceInfo>& audioList = AudioDeviceInfo::availableInputDevices();

for (const auto &itAudio : audioList)
{
Expand Down
2 changes: 1 addition & 1 deletion plugins/samplesource/fcdproplus/fcdproplusinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void FCDProPlusInput::closeDevice()
bool FCDProPlusInput::openFCDAudio(const char* cardname)
{
AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager();
const QList<AudioDeviceInfo>& audioList = audioDeviceManager->getInputDevices();
const QList<AudioDeviceInfo>& audioList = AudioDeviceInfo::availableInputDevices();

for (const auto &itAudio : audioList)
{
Expand Down
84 changes: 50 additions & 34 deletions sdrbase/audio/audiodeviceinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

#include "audiodeviceinfo.h"

bool inputDevicesEnumerated = false, outputDevicesEnumerated = false;
QList<AudioDeviceInfo> inputDevices, outputDevices;
AudioDeviceInfo defaultInputDevice_, defaultOutputDevice_;

QString AudioDeviceInfo::deviceName() const
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
Expand Down Expand Up @@ -67,69 +71,81 @@ QString AudioDeviceInfo::realm() const
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QList<AudioDeviceInfo> AudioDeviceInfo::availableInputDevices()
const QList<AudioDeviceInfo> &AudioDeviceInfo::availableInputDevices()
{
QList<QAudioDevice> devInfos = QMediaDevices::audioInputs();
QList<AudioDeviceInfo> list;

for (auto devInfo : devInfos) {
list.append(AudioDeviceInfo(devInfo));
if (!inputDevicesEnumerated) {
QList<QAudioDevice> devInfos = QMediaDevices::audioInputs();
for (auto devInfo : devInfos) {
inputDevices.append(AudioDeviceInfo(devInfo));
}
inputDevicesEnumerated = true;
}

return list;
return inputDevices;
}

QList<AudioDeviceInfo> AudioDeviceInfo::availableOutputDevices()
const QList<AudioDeviceInfo> &AudioDeviceInfo::availableOutputDevices()
{
QList<QAudioDevice> devInfos = QMediaDevices::audioOutputs();
QList<AudioDeviceInfo> list;

for (auto devInfo : devInfos) {
list.append(AudioDeviceInfo(devInfo));
if (!outputDevicesEnumerated) {
QList<QAudioDevice> devInfos = QMediaDevices::audioOutputs();
for (auto devInfo : devInfos) {
outputDevices.append(AudioDeviceInfo(devInfo));
}
outputDevicesEnumerated = true;
}

return list;
return outputDevices;
}
#else
QList<AudioDeviceInfo> AudioDeviceInfo::availableInputDevices()
const QList<AudioDeviceInfo> &AudioDeviceInfo::availableInputDevices()
{
QList<QAudioDeviceInfo> devInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
QList<AudioDeviceInfo> list;

for (auto devInfo : devInfos) {
list.append(AudioDeviceInfo(devInfo));
if (!inputDevicesEnumerated) {
QList<QAudioDeviceInfo> devInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
for (auto devInfo : devInfos) {
inputDevices.append(AudioDeviceInfo(devInfo));
}
inputDevicesEnumerated = true;
}

return list;
return inputDevices;
}

QList<AudioDeviceInfo> AudioDeviceInfo::availableOutputDevices()
const QList<AudioDeviceInfo> &AudioDeviceInfo::availableOutputDevices()
{
QList<QAudioDeviceInfo> devInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
QList<AudioDeviceInfo> list;

for (auto devInfo : devInfos) {
list.append(AudioDeviceInfo(devInfo));
if (!outputDevicesEnumerated) {
QList<QAudioDeviceInfo> devInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
for (auto devInfo : devInfos) {
outputDevices.append(AudioDeviceInfo(devInfo));
}
outputDevicesEnumerated = true;
}

return list;
return outputDevices;
}
#endif

AudioDeviceInfo AudioDeviceInfo::defaultOutputDevice()
const AudioDeviceInfo &AudioDeviceInfo::defaultOutputDevice()
{
if (defaultOutputDevice_.m_deviceInfo.isNull())
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return AudioDeviceInfo(QMediaDevices::defaultAudioOutput());
defaultOutputDevice_ = AudioDeviceInfo(QMediaDevices::defaultAudioOutput());
#else
return AudioDeviceInfo(QAudioDeviceInfo::defaultOutputDevice());
defaultOutputDevice_ = AudioDeviceInfo(QAudioDeviceInfo::defaultOutputDevice());
#endif
}
return defaultOutputDevice_;
}

AudioDeviceInfo AudioDeviceInfo::defaultInputDevice()
const AudioDeviceInfo &AudioDeviceInfo::defaultInputDevice()
{
if (defaultInputDevice_.m_deviceInfo.isNull())
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return AudioDeviceInfo(QMediaDevices::defaultAudioInput());
defaultInputDevice_ = AudioDeviceInfo(QMediaDevices::defaultAudioInput());
#else
return AudioDeviceInfo(QAudioDeviceInfo::defaultInputDevice());
defaultInputDevice_ = AudioDeviceInfo(QAudioDeviceInfo::defaultInputDevice());
#endif
}
return defaultInputDevice_;
}
8 changes: 4 additions & 4 deletions sdrbase/audio/audiodeviceinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class SDRBASE_API AudioDeviceInfo {
bool isFormatSupported(const QAudioFormat &settings) const;
QList<int> supportedSampleRates() const;

static QList<AudioDeviceInfo> availableInputDevices();
static QList<AudioDeviceInfo> availableOutputDevices();
static AudioDeviceInfo defaultInputDevice();
static AudioDeviceInfo defaultOutputDevice();
static const QList<AudioDeviceInfo> &availableInputDevices();
static const QList<AudioDeviceInfo> &availableOutputDevices();
static const AudioDeviceInfo &defaultInputDevice();
static const AudioDeviceInfo &defaultOutputDevice();

private:

Expand Down
54 changes: 28 additions & 26 deletions sdrbase/audio/audiodevicemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,22 @@ QDataStream& operator>>(QDataStream& ds, AudioDeviceManager::OutputDeviceInfo& i
AudioDeviceManager::AudioDeviceManager()
{
qDebug("AudioDeviceManager::AudioDeviceManager: scan input devices");
m_inputDevicesInfo = AudioDeviceInfo::availableInputDevices();
{
auto &devicesInfo = AudioDeviceInfo::availableInputDevices();

for (int i = 0; i < m_inputDevicesInfo.size(); i++) {
qDebug("AudioDeviceManager::AudioDeviceManager: input device #%d: %s", i, qPrintable(m_inputDevicesInfo[i].deviceName()));
for (int i = 0; i < devicesInfo.size(); i++) {
qDebug("AudioDeviceManager::AudioDeviceManager: input device #%d: %s", i, qPrintable(devicesInfo[i].deviceName()));
}
}

qDebug("AudioDeviceManager::AudioDeviceManager: scan output devices");

m_outputDevicesInfo = AudioDeviceInfo::availableOutputDevices();
{
auto &devicesInfo = AudioDeviceInfo::availableOutputDevices();

for (int i = 0; i < m_outputDevicesInfo.size(); i++) {
qDebug("AudioDeviceManager::AudioDeviceManager: output device #%d: %s", i, qPrintable(m_outputDevicesInfo[i].deviceName()));
for (int i = 0; i < devicesInfo.size(); i++) {
qDebug("AudioDeviceManager::AudioDeviceManager: output device #%d: %s", i, qPrintable(devicesInfo[i].deviceName()));
}
}

m_defaultInputStarted = false;
m_defaultOutputStarted = false;

Expand Down Expand Up @@ -142,9 +144,9 @@ bool AudioDeviceManager::getOutputDeviceName(int outputDeviceIndex, QString &dev
}
else
{
if (outputDeviceIndex < m_outputDevicesInfo.size())
if (outputDeviceIndex < AudioDeviceInfo::availableOutputDevices().size())
{
deviceName = m_outputDevicesInfo[outputDeviceIndex].deviceName();
deviceName = AudioDeviceInfo::availableOutputDevices()[outputDeviceIndex].deviceName();
return true;
}
else
Expand All @@ -163,9 +165,9 @@ bool AudioDeviceManager::getInputDeviceName(int inputDeviceIndex, QString &devic
}
else
{
if (inputDeviceIndex < m_inputDevicesInfo.size())
if (inputDeviceIndex < AudioDeviceInfo::availableInputDevices().size())
{
deviceName = m_inputDevicesInfo[inputDeviceIndex].deviceName();
deviceName = AudioDeviceInfo::availableInputDevices()[inputDeviceIndex].deviceName();
return true;
}
else
Expand All @@ -177,10 +179,10 @@ bool AudioDeviceManager::getInputDeviceName(int inputDeviceIndex, QString &devic

int AudioDeviceManager::getOutputDeviceIndex(const QString &deviceName) const
{
for (int i = 0; i < m_outputDevicesInfo.size(); i++)
for (int i = 0; i < AudioDeviceInfo::availableOutputDevices().size(); i++)
{
//qDebug("AudioDeviceManager::getOutputDeviceIndex: %d: %s|%s", i, qPrintable(deviceName), qPrintable(m_outputDevicesInfo[i].deviceName()));
if (deviceName == m_outputDevicesInfo[i].deviceName()) {
//qDebug("AudioDeviceManager::getOutputDeviceIndex: %d: %s|%s", i, qPrintable(deviceName), qPrintable(AudioDeviceInfo::availableOutputDevices()[i].deviceName()));
if (deviceName == AudioDeviceInfo::availableOutputDevices()[i].deviceName()) {
return i;
}
}
Expand All @@ -190,10 +192,10 @@ int AudioDeviceManager::getOutputDeviceIndex(const QString &deviceName) const

int AudioDeviceManager::getInputDeviceIndex(const QString &deviceName) const
{
for (int i = 0; i < m_inputDevicesInfo.size(); i++)
for (int i = 0; i < AudioDeviceInfo::availableInputDevices().size(); i++)
{
//qDebug("AudioDeviceManager::getInputDeviceIndex: %d: %s|%s", i, qPrintable(deviceName), qPrintable(m_inputDevicesInfo[i].deviceName()));
if (deviceName == m_inputDevicesInfo[i].deviceName()) {
//qDebug("AudioDeviceManager::getInputDeviceIndex: %d: %s|%s", i, qPrintable(deviceName), qPrintable(AudioDeviceInfo::availableInputDevices()[i].deviceName()));
if (deviceName == AudioDeviceInfo::availableInputDevices()[i].deviceName()) {
return i;
}
}
Expand Down Expand Up @@ -295,7 +297,7 @@ void AudioDeviceManager::addAudioSink(AudioFifo* audioFifo, MessageQueue *sample
if (outputDeviceIndex < 0) {
audioOutputDevice->setDeviceName("System default");
} else {
audioOutputDevice->setDeviceName(m_outputDevicesInfo[outputDeviceIndex].deviceName());
audioOutputDevice->setDeviceName(AudioDeviceInfo::availableOutputDevices()[outputDeviceIndex].deviceName());
}

qDebug("AudioDeviceManager::addAudioSink: new AudioOutputDevice on thread: %p", thread);
Expand Down Expand Up @@ -382,7 +384,7 @@ void AudioDeviceManager::addAudioSource(AudioFifo* audioFifo, MessageQueue *samp
if (inputDeviceIndex < 0) {
audioInputDevice->setDeviceName("System default");
} else {
audioInputDevice->setDeviceName(m_outputDevicesInfo[inputDeviceIndex].deviceName());
audioInputDevice->setDeviceName(AudioDeviceInfo::availableOutputDevices()[inputDeviceIndex].deviceName());
}

qDebug("AudioDeviceManager::addAudioSource: new AudioInputDevice on thread: %p", thread);
Expand Down Expand Up @@ -500,7 +502,7 @@ void AudioDeviceManager::startAudioOutput(int outputDeviceIndex)
m_audioOutputInfos[deviceName].udpChannelMode = udpChannelMode;
m_audioOutputInfos[deviceName].udpChannelCodec = udpChannelCodec;
m_audioOutputInfos[deviceName].udpDecimationFactor = decimationFactor;
m_defaultOutputStarted = (outputDeviceIndex == -1);
m_defaultOutputStarted |= (outputDeviceIndex == -1);
}
else
{
Expand Down Expand Up @@ -538,7 +540,7 @@ void AudioDeviceManager::startAudioInput(int inputDeviceIndex)

m_audioInputs[inputDeviceIndex]->setVolume(volume);
m_audioInputInfos[deviceName].volume = volume;
m_defaultInputStarted = (inputDeviceIndex == -1);
m_defaultInputStarted |= (inputDeviceIndex == -1);
}
else
{
Expand Down Expand Up @@ -789,9 +791,9 @@ void AudioDeviceManager::inputInfosCleanup()
{
QSet<QString> deviceNames;
deviceNames.insert(m_defaultDeviceName);
QList<AudioDeviceInfo>::const_iterator itd = m_inputDevicesInfo.begin();
QList<AudioDeviceInfo>::const_iterator itd = AudioDeviceInfo::availableInputDevices().begin();

for (; itd != m_inputDevicesInfo.end(); ++itd)
for (; itd != AudioDeviceInfo::availableInputDevices().end(); ++itd)
{
qDebug("AudioDeviceManager::inputInfosCleanup: device: %s", qPrintable(itd->deviceName()));
deviceNames.insert(itd->deviceName());
Expand All @@ -817,9 +819,9 @@ void AudioDeviceManager::outputInfosCleanup()
{
QSet<QString> deviceNames;
deviceNames.insert(m_defaultDeviceName);
QList<AudioDeviceInfo>::const_iterator itd = m_outputDevicesInfo.begin();
QList<AudioDeviceInfo>::const_iterator itd = AudioDeviceInfo::availableOutputDevices().begin();

for (; itd != m_outputDevicesInfo.end(); ++itd)
for (; itd != AudioDeviceInfo::availableOutputDevices().end(); ++itd)
{
qDebug("AudioDeviceManager::outputInfosCleanup: device: %s", qPrintable(itd->deviceName()));
deviceNames.insert(itd->deviceName());
Expand Down
6 changes: 0 additions & 6 deletions sdrbase/audio/audiodevicemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ class SDRBASE_API AudioDeviceManager : public QObject {
AudioDeviceManager();
~AudioDeviceManager();

const QList<AudioDeviceInfo>& getInputDevices() const { return m_inputDevicesInfo; }
const QList<AudioDeviceInfo>& getOutputDevices() const { return m_outputDevicesInfo; }

bool getOutputDeviceName(int outputDeviceIndex, QString &deviceName) const;
bool getInputDeviceName(int inputDeviceIndex, QString &deviceName) const;
int getOutputDeviceIndex(const QString &deviceName) const;
Expand Down Expand Up @@ -136,9 +133,6 @@ class SDRBASE_API AudioDeviceManager : public QObject {
static const QString m_defaultDeviceName;

private:
QList<AudioDeviceInfo> m_inputDevicesInfo;
QList<AudioDeviceInfo> m_outputDevicesInfo;

QMap<AudioFifo*, int> m_audioSinkFifos; //< audio sink FIFO to audio output device index-1 map
QMap<AudioFifo*, MessageQueue*> m_audioFifoToSinkMessageQueues; //!< audio sink FIFO to attached sink message queue
QMap<int, QList<MessageQueue*> > m_outputDeviceSinkMessageQueues; //!< sink message queues attached to device
Expand Down
2 changes: 1 addition & 1 deletion sdrbase/audio/audiooutputdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool AudioOutputDevice::start(int deviceIndex, int sampleRate)
}
else
{
QList<AudioDeviceInfo> devicesInfo = AudioDeviceInfo::availableOutputDevices();
auto &devicesInfo = AudioDeviceInfo::availableOutputDevices();

if (deviceIndex < devicesInfo.size())
{
Expand Down
4 changes: 2 additions & 2 deletions sdrbase/webapi/webapiadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ int WebAPIAdapter::instanceAudioGet(
{
(void) error;
DSPEngine *dspEngine = DSPEngine::instance();
const QList<AudioDeviceInfo>& audioInputDevices = dspEngine->getAudioDeviceManager()->getInputDevices();
const QList<AudioDeviceInfo>& audioOutputDevices = dspEngine->getAudioDeviceManager()->getOutputDevices();
const QList<AudioDeviceInfo>& audioInputDevices = AudioDeviceInfo::availableInputDevices();
const QList<AudioDeviceInfo>& audioOutputDevices = AudioDeviceInfo::availableOutputDevices();
int nbInputDevices = audioInputDevices.size();
int nbOutputDevices = audioOutputDevices.size();

Expand Down
6 changes: 3 additions & 3 deletions sdrgui/gui/audiodialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ AudioDialogX::AudioDialogX(AudioDeviceManager* audioDeviceManager, QWidget* pare
// out panel

AudioDeviceManager::OutputDeviceInfo outDeviceInfo;
AudioDeviceInfo defaultOutputDeviceInfo = AudioDeviceInfo::defaultOutputDevice();
const AudioDeviceInfo &defaultOutputDeviceInfo = AudioDeviceInfo::defaultOutputDevice();
treeItem = new QTreeWidgetItem(ui->audioOutTree);
treeItem->setText(1, AudioDeviceManager::m_defaultDeviceName);
bool found = m_audioDeviceManager->getOutputDeviceInfo(AudioDeviceManager::m_defaultDeviceName, outDeviceInfo);
treeItem->setText(0, found ? "__" : "_D");
ui->audioOutTree->setCurrentItem(treeItem);

const QList<AudioDeviceInfo>& outputDevices = m_audioDeviceManager->getOutputDevices();
const QList<AudioDeviceInfo>& outputDevices = AudioDeviceInfo::availableOutputDevices();

for(QList<AudioDeviceInfo>::const_iterator it = outputDevices.begin(); it != outputDevices.end(); ++it)
{
Expand Down Expand Up @@ -75,7 +75,7 @@ AudioDialogX::AudioDialogX(AudioDeviceManager* audioDeviceManager, QWidget* pare
treeItem->setText(0, found ? "__" : "_D");
ui->audioInTree->setCurrentItem(treeItem);

const QList<AudioDeviceInfo>& inputDevices = m_audioDeviceManager->getInputDevices();
const QList<AudioDeviceInfo>& inputDevices = AudioDeviceInfo::availableInputDevices();

for(QList<AudioDeviceInfo>::const_iterator it = inputDevices.begin(); it != inputDevices.end(); ++it)
{
Expand Down
2 changes: 1 addition & 1 deletion sdrgui/gui/audioselectdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ AudioSelectDialog::AudioSelectDialog(const AudioDeviceManager* audioDeviceManage
defaultItem->setText(2, tr("%1").arg(sampleRate));
defaultItem->setTextAlignment(2, Qt::AlignRight);

QList<AudioDeviceInfo> devices = input ? m_audioDeviceManager->getInputDevices() : m_audioDeviceManager->getOutputDevices();
const QList<AudioDeviceInfo> &devices = input ? AudioDeviceInfo::availableInputDevices() : AudioDeviceInfo::availableOutputDevices();

for(QList<AudioDeviceInfo>::const_iterator it = devices.begin(); it != devices.end(); ++it)
{
Expand Down

0 comments on commit aa95a5b

Please sign in to comment.