diff --git a/plugins/samplesource/audioinput/audioinputgui.cpp b/plugins/samplesource/audioinput/audioinputgui.cpp index ce6141c66..c6b321de5 100644 --- a/plugins/samplesource/audioinput/audioinputgui.cpp +++ b/plugins/samplesource/audioinput/audioinputgui.cpp @@ -186,8 +186,7 @@ void AudioInputGui::updateSampleRateAndFrequency() void AudioInputGui::refreshDeviceList() { ui->device->blockSignals(true); - AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager(); - const QList& audioList = audioDeviceManager->getInputDevices(); + const QList& audioList = AudioDeviceInfo::availableInputDevices(); ui->device->clear(); for (const auto &itAudio : audioList) diff --git a/sdrbase/audio/audiodeviceinfo.cpp b/sdrbase/audio/audiodeviceinfo.cpp index c40c7f0c7..e766898eb 100644 --- a/sdrbase/audio/audiodeviceinfo.cpp +++ b/sdrbase/audio/audiodeviceinfo.cpp @@ -20,6 +20,10 @@ #include "audiodeviceinfo.h" +bool inputDevicesEnumerated = false, outputDevicesEnumerated = false; +QList inputDevices, outputDevices; +AudioDeviceInfo defaultInputDevice_, defaultOutputDevice_; + QString AudioDeviceInfo::deviceName() const { #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) @@ -67,69 +71,81 @@ QString AudioDeviceInfo::realm() const } #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) -QList AudioDeviceInfo::availableInputDevices() +const QList &AudioDeviceInfo::availableInputDevices() { - QList devInfos = QMediaDevices::audioInputs(); - QList list; - - for (auto devInfo : devInfos) { - list.append(AudioDeviceInfo(devInfo)); + if (!m_inputDevicesEnumerated) { + QList devInfos = QMediaDevices::audioInputs(); + for (auto devInfo : devInfos) { + m_inputDevices.append(AudioDeviceInfo(devInfo)); + } + m_inputDevicesEnumerated = true; } - return list; + return m_inputDevices; } -QList AudioDeviceInfo::availableOutputDevices() +const QList &AudioDeviceInfo::availableOutputDevices() { - QList devInfos = QMediaDevices::audioOutputs(); - QList list; - - for (auto devInfo : devInfos) { - list.append(AudioDeviceInfo(devInfo)); + if (!m_outputDevicesEnumerated) { + QList devInfos = QMediaDevices::audioOutputs(); + for (auto devInfo : devInfos) { + m_outputDevices.append(AudioDeviceInfo(devInfo)); + } + m_outputDevicesEnumerated = true; } - return list; + return m_outputDevices; } #else -QList AudioDeviceInfo::availableInputDevices() +const QList &AudioDeviceInfo::availableInputDevices() { - QList devInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); - QList list; - - for (auto devInfo : devInfos) { - list.append(AudioDeviceInfo(devInfo)); + if (!inputDevicesEnumerated) { + QList devInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); + for (auto devInfo : devInfos) { + inputDevices.append(AudioDeviceInfo(devInfo)); + } + inputDevicesEnumerated = true; } - return list; + return inputDevices; } -QList AudioDeviceInfo::availableOutputDevices() +const QList &AudioDeviceInfo::availableOutputDevices() { - QList devInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput); - QList list; - - for (auto devInfo : devInfos) { - list.append(AudioDeviceInfo(devInfo)); + if (!outputDevicesEnumerated) { + QList 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_; } diff --git a/sdrbase/audio/audiodeviceinfo.h b/sdrbase/audio/audiodeviceinfo.h index 27a377925..5e3608801 100644 --- a/sdrbase/audio/audiodeviceinfo.h +++ b/sdrbase/audio/audiodeviceinfo.h @@ -67,10 +67,10 @@ public: bool isFormatSupported(const QAudioFormat &settings) const; QList supportedSampleRates() const; - static QList availableInputDevices(); - static QList availableOutputDevices(); - static AudioDeviceInfo defaultInputDevice(); - static AudioDeviceInfo defaultOutputDevice(); + static const QList &availableInputDevices(); + static const QList &availableOutputDevices(); + static const AudioDeviceInfo &defaultInputDevice(); + static const AudioDeviceInfo &defaultOutputDevice(); private: diff --git a/sdrbase/audio/audiodevicemanager.cpp b/sdrbase/audio/audiodevicemanager.cpp index bb4ca35ba..949945910 100644 --- a/sdrbase/audio/audiodevicemanager.cpp +++ b/sdrbase/audio/audiodevicemanager.cpp @@ -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; @@ -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 @@ -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 @@ -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; } } @@ -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; } } @@ -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); @@ -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); @@ -789,9 +791,9 @@ void AudioDeviceManager::inputInfosCleanup() { QSet deviceNames; deviceNames.insert(m_defaultDeviceName); - QList::const_iterator itd = m_inputDevicesInfo.begin(); + QList::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()); @@ -817,9 +819,9 @@ void AudioDeviceManager::outputInfosCleanup() { QSet deviceNames; deviceNames.insert(m_defaultDeviceName); - QList::const_iterator itd = m_outputDevicesInfo.begin(); + QList::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()); diff --git a/sdrbase/audio/audiodevicemanager.h b/sdrbase/audio/audiodevicemanager.h index 0cbbb9e5e..ca78191eb 100644 --- a/sdrbase/audio/audiodevicemanager.h +++ b/sdrbase/audio/audiodevicemanager.h @@ -102,9 +102,6 @@ public: AudioDeviceManager(); ~AudioDeviceManager(); - const QList& getInputDevices() const { return m_inputDevicesInfo; } - const QList& 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; @@ -136,9 +133,6 @@ public: static const QString m_defaultDeviceName; private: - QList m_inputDevicesInfo; - QList m_outputDevicesInfo; - QMap m_audioSinkFifos; //< audio sink FIFO to audio output device index-1 map QMap m_audioFifoToSinkMessageQueues; //!< audio sink FIFO to attached sink message queue QMap > m_outputDeviceSinkMessageQueues; //!< sink message queues attached to device diff --git a/sdrbase/audio/audiooutputdevice.cpp b/sdrbase/audio/audiooutputdevice.cpp index 4a9b98001..b886165fb 100644 --- a/sdrbase/audio/audiooutputdevice.cpp +++ b/sdrbase/audio/audiooutputdevice.cpp @@ -91,7 +91,7 @@ bool AudioOutputDevice::start(int deviceIndex, int sampleRate) } else { - QList devicesInfo = AudioDeviceInfo::availableOutputDevices(); + auto &devicesInfo = AudioDeviceInfo::availableOutputDevices(); if (deviceIndex < devicesInfo.size()) { diff --git a/sdrgui/gui/audiodialog.cpp b/sdrgui/gui/audiodialog.cpp index 7f7a7d378..5183b1bc6 100644 --- a/sdrgui/gui/audiodialog.cpp +++ b/sdrgui/gui/audiodialog.cpp @@ -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& outputDevices = m_audioDeviceManager->getOutputDevices(); + const QList& outputDevices = AudioDeviceInfo::availableOutputDevices(); for(QList::const_iterator it = outputDevices.begin(); it != outputDevices.end(); ++it) { @@ -75,7 +75,7 @@ AudioDialogX::AudioDialogX(AudioDeviceManager* audioDeviceManager, QWidget* pare treeItem->setText(0, found ? "__" : "_D"); ui->audioInTree->setCurrentItem(treeItem); - const QList& inputDevices = m_audioDeviceManager->getInputDevices(); + const QList& inputDevices = AudioDeviceInfo::availableInputDevices(); for(QList::const_iterator it = inputDevices.begin(); it != inputDevices.end(); ++it) { diff --git a/sdrgui/gui/audioselectdialog.cpp b/sdrgui/gui/audioselectdialog.cpp index 4ce95fdb1..79fbdab43 100644 --- a/sdrgui/gui/audioselectdialog.cpp +++ b/sdrgui/gui/audioselectdialog.cpp @@ -40,7 +40,7 @@ AudioSelectDialog::AudioSelectDialog(const AudioDeviceManager* audioDeviceManage defaultItem->setText(2, tr("%1").arg(sampleRate)); defaultItem->setTextAlignment(2, Qt::AlignRight); - QList devices = input ? m_audioDeviceManager->getInputDevices() : m_audioDeviceManager->getOutputDevices(); + const QList &devices = input ? AudioDeviceInfo::availableInputDevices() : AudioDeviceInfo::availableOutputDevices(); for(QList::const_iterator it = devices.begin(); it != devices.end(); ++it) {