1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-13 20:01:46 -05:00

Audio Input: Make settings assignments atomic. Part of #1329

This commit is contained in:
f4exb 2022-10-22 12:28:24 +02:00
parent 2eeadfe758
commit a1161657ad
6 changed files with 138 additions and 48 deletions

View File

@ -113,7 +113,7 @@ bool AudioInput::openAudioDevice(QString deviceName, qint32 sampleRate)
void AudioInput::init() void AudioInput::init()
{ {
applySettings(m_settings, true); applySettings(m_settings, QList<QString>(), true);
} }
bool AudioInput::start() bool AudioInput::start()
@ -131,7 +131,7 @@ bool AudioInput::start()
} }
qDebug() << "AudioInput::start"; qDebug() << "AudioInput::start";
applySettings(m_settings, true, true); applySettings(m_settings, QList<QString>(), true, true);
m_workerThread = new QThread(); m_workerThread = new QThread();
m_worker = new AudioInputWorker(&m_sampleFifo, &m_fifo); m_worker = new AudioInputWorker(&m_sampleFifo, &m_fifo);
@ -194,12 +194,12 @@ bool AudioInput::deserialize(const QByteArray& data)
success = false; success = false;
} }
MsgConfigureAudioInput* message = MsgConfigureAudioInput::create(m_settings, true); MsgConfigureAudioInput* message = MsgConfigureAudioInput::create(m_settings, QList<QString>(), true);
m_inputMessageQueue.push(message); m_inputMessageQueue.push(message);
if (m_guiMessageQueue) if (m_guiMessageQueue)
{ {
MsgConfigureAudioInput* messageToGUI = MsgConfigureAudioInput::create(m_settings, true); MsgConfigureAudioInput* messageToGUI = MsgConfigureAudioInput::create(m_settings, QList<QString>(), true);
m_guiMessageQueue->push(messageToGUI); m_guiMessageQueue->push(messageToGUI);
} }
@ -223,11 +223,11 @@ quint64 AudioInput::getCenterFrequency() const
bool AudioInput::handleMessage(const Message& message) bool AudioInput::handleMessage(const Message& message)
{ {
if(MsgConfigureAudioInput::match(message)) if (MsgConfigureAudioInput::match(message))
{ {
qDebug() << "AudioInput::handleMessage: MsgConfigureAudioInput"; qDebug() << "AudioInput::handleMessage: MsgConfigureAudioInput";
MsgConfigureAudioInput& conf = (MsgConfigureAudioInput&) message; MsgConfigureAudioInput& conf = (MsgConfigureAudioInput&) message;
applySettings(conf.getSettings(), conf.getForce()); applySettings(conf.getSettings(), conf.getSettingsKeys(), conf.getForce());
return true; return true;
} }
else if (MsgStartStop::match(message)) else if (MsgStartStop::match(message))
@ -259,13 +259,16 @@ bool AudioInput::handleMessage(const Message& message)
} }
} }
void AudioInput::applySettings(const AudioInputSettings& settings, bool force, bool starting) void AudioInput::applySettings(const AudioInputSettings& settings, QList<QString> settingsKeys, bool force, bool starting)
{ {
bool forwardChange = false; bool forwardChange = false;
QList<QString> reverseAPIKeys;
if ((m_settings.m_deviceName != settings.m_deviceName) qDebug() << "AudioInput::applySettings: "
|| (m_settings.m_sampleRate != settings.m_sampleRate) || force) << " force:" << force
<< settings.getDebugString(settingsKeys, force);
if (settingsKeys.contains("deviceName")
|| settingsKeys.contains("sampleRate") || force)
{ {
// Don't call openAudioDevice if called from start(), otherwise ::AudioInput // Don't call openAudioDevice if called from start(), otherwise ::AudioInput
// will be created on wrong thread and we'll crash after ::AudioInput::stop calls delete // will be created on wrong thread and we'll crash after ::AudioInput::stop calls delete
@ -279,27 +282,18 @@ void AudioInput::applySettings(const AudioInputSettings& settings, bool force, b
} }
} }
if ((m_settings.m_deviceName != settings.m_deviceName) || force) if (settingsKeys.contains("sampleRate") || force) {
{
reverseAPIKeys.append("device");
}
if ((m_settings.m_sampleRate != settings.m_sampleRate) || force)
{
reverseAPIKeys.append("sampleRate");
forwardChange = true; forwardChange = true;
} }
if ((m_settings.m_volume != settings.m_volume) || force) if (settingsKeys.contains("volume") || force)
{ {
reverseAPIKeys.append("volume");
m_audioInput.setVolume(settings.m_volume); m_audioInput.setVolume(settings.m_volume);
qDebug() << "AudioInput::applySettings: set volume to " << settings.m_volume; qDebug() << "AudioInput::applySettings: set volume to " << settings.m_volume;
} }
if ((m_settings.m_log2Decim != settings.m_log2Decim) || force) if (settingsKeys.contains("log2Decim") || force)
{ {
reverseAPIKeys.append("log2Decim");
forwardChange = true; forwardChange = true;
if (m_running) if (m_running)
@ -309,9 +303,8 @@ void AudioInput::applySettings(const AudioInputSettings& settings, bool force, b
} }
} }
if ((m_settings.m_iqMapping != settings.m_iqMapping) || force) if (settingsKeys.contains("iqMapping") || force)
{ {
reverseAPIKeys.append("iqMapping");
forwardChange = true; forwardChange = true;
if (m_running) { if (m_running) {
@ -319,16 +312,20 @@ void AudioInput::applySettings(const AudioInputSettings& settings, bool force, b
} }
} }
if (settings.m_useReverseAPI) if (settingsKeys.contains("useReverseAPI"))
{ {
bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) || bool fullUpdate = (settingsKeys.contains("useReverseAPI") && settings.m_useReverseAPI) ||
(m_settings.m_reverseAPIAddress != settings.m_reverseAPIAddress) || settingsKeys.contains("reverseAPIAddress") ||
(m_settings.m_reverseAPIPort != settings.m_reverseAPIPort) || settingsKeys.contains("reverseAPIPort") ||
(m_settings.m_reverseAPIDeviceIndex != settings.m_reverseAPIDeviceIndex); settingsKeys.contains("reverseAPIDeviceIndex");
webapiReverseSendSettings(reverseAPIKeys, settings, fullUpdate || force); webapiReverseSendSettings(settingsKeys, settings, fullUpdate || force);
} }
m_settings = settings; if (force) {
m_settings = settings;
} else {
m_settings.applySettings(settingsKeys, settings);
}
if (forwardChange) if (forwardChange)
{ {
@ -393,12 +390,12 @@ int AudioInput::webapiSettingsPutPatch(
AudioInputSettings settings = m_settings; AudioInputSettings settings = m_settings;
webapiUpdateDeviceSettings(settings, deviceSettingsKeys, response); webapiUpdateDeviceSettings(settings, deviceSettingsKeys, response);
MsgConfigureAudioInput *msg = MsgConfigureAudioInput::create(settings, force); MsgConfigureAudioInput *msg = MsgConfigureAudioInput::create(settings, deviceSettingsKeys, force);
m_inputMessageQueue.push(msg); m_inputMessageQueue.push(msg);
if (m_guiMessageQueue) // forward to GUI if any if (m_guiMessageQueue) // forward to GUI if any
{ {
MsgConfigureAudioInput *msgToGUI = MsgConfigureAudioInput::create(settings, force); MsgConfigureAudioInput *msgToGUI = MsgConfigureAudioInput::create(settings, deviceSettingsKeys, force);
m_guiMessageQueue->push(msgToGUI); m_guiMessageQueue->push(msgToGUI);
} }
@ -460,7 +457,7 @@ void AudioInput::webapiFormatDeviceSettings(SWGSDRangel::SWGDeviceSettings& resp
response.getAudioInputSettings()->setReverseApiDeviceIndex(settings.m_reverseAPIDeviceIndex); response.getAudioInputSettings()->setReverseApiDeviceIndex(settings.m_reverseAPIDeviceIndex);
} }
void AudioInput::webapiReverseSendSettings(QList<QString>& deviceSettingsKeys, const AudioInputSettings& settings, bool force) void AudioInput::webapiReverseSendSettings(const QList<QString>& deviceSettingsKeys, const AudioInputSettings& settings, bool force)
{ {
SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings(); SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings();
swgDeviceSettings->setDirection(0); // single Rx swgDeviceSettings->setDirection(0); // single Rx

View File

@ -46,20 +46,23 @@ public:
public: public:
const AudioInputSettings& getSettings() const { return m_settings; } const AudioInputSettings& getSettings() const { return m_settings; }
const QList<QString>& getSettingsKeys() const { return m_settingsKeys; }
bool getForce() const { return m_force; } bool getForce() const { return m_force; }
static MsgConfigureAudioInput* create(const AudioInputSettings& settings, bool force) static MsgConfigureAudioInput* create(const AudioInputSettings& settings, const QList<QString>& settingsKeys, bool force)
{ {
return new MsgConfigureAudioInput(settings, force); return new MsgConfigureAudioInput(settings, settingsKeys, force);
} }
private: private:
AudioInputSettings m_settings; AudioInputSettings m_settings;
QList<QString> m_settingsKeys;
bool m_force; bool m_force;
MsgConfigureAudioInput(const AudioInputSettings& settings, bool force) : MsgConfigureAudioInput(const AudioInputSettings& settings, const QList<QString>& settingsKeys, bool force) :
Message(), Message(),
m_settings(settings), m_settings(settings),
m_settingsKeys(settingsKeys),
m_force(force) m_force(force)
{ } { }
}; };
@ -149,9 +152,9 @@ private:
bool openDevice(); bool openDevice();
void closeDevice(); void closeDevice();
bool openAudioDevice(QString deviceName, int sampleRate); bool openAudioDevice(QString deviceName, int sampleRate);
void applySettings(const AudioInputSettings& settings, bool force, bool starting=false); void applySettings(const AudioInputSettings& settings, QList<QString> settingsKeys, bool force, bool starting=false);
void webapiReverseSendSettings(QList<QString>& deviceSettingsKeys, const AudioInputSettings& settings, bool force); void webapiReverseSendSettings(const QList<QString>& deviceSettingsKeys, const AudioInputSettings& settings, bool force);
void webapiReverseSendStartStop(bool start); void webapiReverseSendStartStop(bool start);
private slots: private slots:

View File

@ -73,6 +73,7 @@ void AudioInputGui::resetToDefaults()
{ {
m_settings.resetToDefaults(); m_settings.resetToDefaults();
displaySettings(); displaySettings();
m_forceSettings = true;
sendSettings(); sendSettings();
} }
@ -108,7 +109,13 @@ bool AudioInputGui::handleMessage(const Message& message)
if (AudioInput::MsgConfigureAudioInput::match(message)) if (AudioInput::MsgConfigureAudioInput::match(message))
{ {
const AudioInput::MsgConfigureAudioInput& cfg = (AudioInput::MsgConfigureAudioInput&) message; const AudioInput::MsgConfigureAudioInput& cfg = (AudioInput::MsgConfigureAudioInput&) message;
m_settings = cfg.getSettings();
if (cfg.getForce()) {
m_settings = cfg.getSettings();
} else {
m_settings.applySettings(cfg.getSettingsKeys(), cfg.getSettings());
}
blockApplySettings(true); blockApplySettings(true);
displaySettings(); displaySettings();
blockApplySettings(false); blockApplySettings(false);
@ -203,8 +210,8 @@ void AudioInputGui::refreshSampleRates(QString deviceName)
if (deviceName == AudioInputSettings::getFullDeviceName(deviceInfo)) if (deviceName == AudioInputSettings::getFullDeviceName(deviceInfo))
{ {
QList<int> sampleRates = deviceInfo.supportedSampleRates(); QList<int> sampleRates = deviceInfo.supportedSampleRates();
for(int i = 0; i < sampleRates.size(); ++i)
{ for (int i = 0; i < sampleRates.size(); ++i) {
ui->sampleRate->addItem(QString("%1").arg(sampleRates[i])); ui->sampleRate->addItem(QString("%1").arg(sampleRates[i]));
} }
} }
@ -212,20 +219,25 @@ void AudioInputGui::refreshSampleRates(QString deviceName)
ui->sampleRate->blockSignals(false); ui->sampleRate->blockSignals(false);
int index = ui->sampleRate->findText(QString("%1").arg(m_settings.m_sampleRate)); int index = ui->sampleRate->findText(QString("%1").arg(m_settings.m_sampleRate));
if (index >= 0)
if (index >= 0) {
ui->sampleRate->setCurrentIndex(index); ui->sampleRate->setCurrentIndex(index);
else } else {
ui->sampleRate->setCurrentIndex(0); ui->sampleRate->setCurrentIndex(0);
}
} }
void AudioInputGui::displaySettings() void AudioInputGui::displaySettings()
{ {
refreshDeviceList(); refreshDeviceList();
int index = ui->device->findText(m_settings.m_deviceName); int index = ui->device->findText(m_settings.m_deviceName);
if (index >= 0)
if (index >= 0) {
ui->device->setCurrentIndex(index); ui->device->setCurrentIndex(index);
else } else {
ui->device->setCurrentIndex(0); ui->device->setCurrentIndex(0);
}
ui->decim->setCurrentIndex(m_settings.m_log2Decim); ui->decim->setCurrentIndex(m_settings.m_log2Decim);
ui->volume->setValue((int)(m_settings.m_volume*10.0f)); ui->volume->setValue((int)(m_settings.m_volume*10.0f));
ui->volumeText->setText(QString("%1").arg(m_settings.m_volume, 3, 'f', 1)); ui->volumeText->setText(QString("%1").arg(m_settings.m_volume, 3, 'f', 1));
@ -238,6 +250,7 @@ void AudioInputGui::on_device_currentIndexChanged(int index)
(void) index; (void) index;
m_settings.m_deviceName = ui->device->currentText(); m_settings.m_deviceName = ui->device->currentText();
refreshSampleRates(m_settings.m_deviceName); refreshSampleRates(m_settings.m_deviceName);
m_settingsKeys.append("deviceName");
sendSettings(); sendSettings();
} }
@ -245,6 +258,7 @@ void AudioInputGui::on_sampleRate_currentIndexChanged(int index)
{ {
(void) index; (void) index;
m_settings.m_sampleRate = ui->sampleRate->currentText().toInt(); m_settings.m_sampleRate = ui->sampleRate->currentText().toInt();
m_settingsKeys.append("sampleRate");
sendSettings(); sendSettings();
} }
@ -255,6 +269,7 @@ void AudioInputGui::on_decim_currentIndexChanged(int index)
} }
m_settings.m_log2Decim = index; m_settings.m_log2Decim = index;
m_settingsKeys.append("log2Decim");
sendSettings(); sendSettings();
} }
@ -262,6 +277,7 @@ void AudioInputGui::on_volume_valueChanged(int value)
{ {
m_settings.m_volume = value/10.0f; m_settings.m_volume = value/10.0f;
ui->volumeText->setText(QString("%1").arg(m_settings.m_volume, 3, 'f', 1)); ui->volumeText->setText(QString("%1").arg(m_settings.m_volume, 3, 'f', 1));
m_settingsKeys.append("volume");
sendSettings(); sendSettings();
} }
@ -269,6 +285,7 @@ void AudioInputGui::on_channels_currentIndexChanged(int index)
{ {
m_settings.m_iqMapping = (AudioInputSettings::IQMapping)index; m_settings.m_iqMapping = (AudioInputSettings::IQMapping)index;
updateSampleRateAndFrequency(); updateSampleRateAndFrequency();
m_settingsKeys.append("iqMapping");
sendSettings(); sendSettings();
} }
@ -291,8 +308,9 @@ void AudioInputGui::updateHardware()
{ {
if (m_doApplySettings) if (m_doApplySettings)
{ {
AudioInput::MsgConfigureAudioInput* message = AudioInput::MsgConfigureAudioInput::create(m_settings, m_forceSettings); AudioInput::MsgConfigureAudioInput* message = AudioInput::MsgConfigureAudioInput::create(m_settings, m_settingsKeys, m_forceSettings);
m_sampleSource->getInputMessageQueue()->push(message); m_sampleSource->getInputMessageQueue()->push(message);
m_settingsKeys.clear();
m_forceSettings = false; m_forceSettings = false;
m_updateTimer.stop(); m_updateTimer.stop();
} }
@ -315,6 +333,10 @@ void AudioInputGui::openDeviceSettingsDialog(const QPoint& p)
m_settings.m_reverseAPIAddress = dialog.getReverseAPIAddress(); m_settings.m_reverseAPIAddress = dialog.getReverseAPIAddress();
m_settings.m_reverseAPIPort = dialog.getReverseAPIPort(); m_settings.m_reverseAPIPort = dialog.getReverseAPIPort();
m_settings.m_reverseAPIDeviceIndex = dialog.getReverseAPIDeviceIndex(); m_settings.m_reverseAPIDeviceIndex = dialog.getReverseAPIDeviceIndex();
m_settingsKeys.append("useReverseAPI");
m_settingsKeys.append("reverseAPIAddress");
m_settingsKeys.append("reverseAPIPort");
m_settingsKeys.append("reverseAPIDeviceIndex");
sendSettings(); sendSettings();
} }

View File

@ -56,6 +56,7 @@ private:
bool m_doApplySettings; bool m_doApplySettings;
bool m_forceSettings; bool m_forceSettings;
AudioInputSettings m_settings; AudioInputSettings m_settings;
QList<QString> m_settingsKeys;
QTimer m_updateTimer; QTimer m_updateTimer;
DeviceSampleSource* m_sampleSource; DeviceSampleSource* m_sampleSource;
int m_sampleRate; int m_sampleRate;

View File

@ -97,3 +97,69 @@ bool AudioInputSettings::deserialize(const QByteArray& data)
return false; return false;
} }
} }
void AudioInputSettings::applySettings(const QStringList& settingsKeys, const AudioInputSettings& settings)
{
if (settingsKeys.contains("deviceName")) {
m_deviceName = settings.m_deviceName;
}
if (settingsKeys.contains("sampleRate")) {
m_sampleRate = settings.m_sampleRate;
}
if (settingsKeys.contains("volume")) {
m_volume = settings.m_volume;
}
if (settingsKeys.contains("log2Decim")) {
m_log2Decim = settings.m_log2Decim;
}
if (settingsKeys.contains("iqMapping")) {
m_iqMapping = settings.m_iqMapping;
}
if (settingsKeys.contains("useReverseAPI")) {
m_useReverseAPI = settings.m_useReverseAPI;
}
if (settingsKeys.contains("reverseAPIAddress")) {
m_reverseAPIAddress = settings.m_reverseAPIAddress;
}
if (settingsKeys.contains("reverseAPIPort")) {
m_reverseAPIPort = settings.m_reverseAPIPort;
}
if (settingsKeys.contains("reverseAPIDeviceIndex")) {
m_reverseAPIDeviceIndex = settings.m_reverseAPIDeviceIndex;
}
}
QString AudioInputSettings::getDebugString(const QStringList& settingsKeys, bool force) const
{
std::ostringstream ostr;
if (settingsKeys.contains("deviceName") || force) {
ostr << " m_deviceName: " << m_deviceName.toStdString();
}
if (settingsKeys.contains("sampleRate") || force) {
ostr << " m_sampleRate: " << m_sampleRate;
}
if (settingsKeys.contains("volume") || force) {
ostr << " m_volume: " << m_volume;
}
if (settingsKeys.contains("log2Decim") || force) {
ostr << " m_log2Decim: " << m_log2Decim;
}
if (settingsKeys.contains("iqMapping") || force) {
ostr << " m_iqMapping: " << m_iqMapping;
}
if (settingsKeys.contains("useReverseAPI") || force) {
ostr << " m_useReverseAPI: " << m_useReverseAPI;
}
if (settingsKeys.contains("reverseAPIAddress") || force) {
ostr << " m_reverseAPIAddress: " << m_reverseAPIAddress.toStdString();
}
if (settingsKeys.contains("reverseAPIPort") || force) {
ostr << " m_reverseAPIPort: " << m_reverseAPIPort;
}
if (settingsKeys.contains("reverseAPIDeviceIndex") || force) {
ostr << " m_reverseAPIDeviceIndex: " << m_reverseAPIDeviceIndex;
}
return QString(ostr.str().c_str());
}

View File

@ -58,7 +58,8 @@ struct AudioInputSettings {
return deviceInfo.deviceName(); return deviceInfo.deviceName();
#endif #endif
} }
void applySettings(const QStringList& settingsKeys, const AudioInputSettings& settings);
QString getDebugString(const QStringList& settingsKeys, bool force=false) const;
}; };
#endif /* _AUDIOINPUT_AUDIOINPUTSETTINGS_H_ */ #endif /* _AUDIOINPUT_AUDIOINPUTSETTINGS_H_ */