From a53df3596e2061c202845f9f1c183177ca8a7eda Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 26 Oct 2010 15:15:14 +0100 Subject: Do not unnecessarily reset state of spectrum demo This patch ensures that the state of the application is not reset when: - Any of the 'Play generated tone', 'Play file' or settings dialogs are opened - Any of the 'Play generated tone', 'Play file' or settings dialogs are dismissed by pressing the Cancel button - A new input or output device is selected via the settings dialog, and that new device supports the data format which is currently being used within the application - The window function is changed via the settings dialog Note that the application is still reset if a new input or output device is selected via the settings dialog, and this device does not support the current data format. Task-number: QTBUG-12935 Task-number: QTBUG-14810 --- demos/spectrum/app/engine.cpp | 84 +++++++++++++++++++++------------------ demos/spectrum/app/mainwidget.cpp | 21 ++++++---- demos/spectrum/app/mainwidget.h | 1 + 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/demos/spectrum/app/engine.cpp b/demos/spectrum/app/engine.cpp index 119a0e3..0495fe3 100644 --- a/demos/spectrum/app/engine.cpp +++ b/demos/spectrum/app/engine.cpp @@ -453,44 +453,50 @@ bool Engine::initialize() { bool result = false; - reset(); + QAudioFormat format = m_format; if (selectFormat()) { - const qint64 bufferLength = audioLength(m_format, BufferDurationUs); - m_buffer.resize(bufferLength); - m_buffer.fill(0); - emit bufferDurationChanged(BufferDurationUs); - - if (m_generateTone) { - if (0 == m_tone.endFreq) { - const qreal nyquist = nyquistFrequency(m_format); - m_tone.endFreq = qMin(qreal(SpectrumHighFreq), nyquist); - } + if (m_format != format) { + format = m_format; + reset(); + m_format = format; + + const qint64 bufferLength = audioLength(m_format, BufferDurationUs); + m_buffer.resize(bufferLength); + m_buffer.fill(0); + emit bufferDurationChanged(BufferDurationUs); + + if (m_generateTone) { + if (0 == m_tone.endFreq) { + const qreal nyquist = nyquistFrequency(m_format); + m_tone.endFreq = qMin(qreal(SpectrumHighFreq), nyquist); + } - // Call function defined in utils.h, at global scope - ::generateTone(m_tone, m_format, m_buffer); - m_dataLength = m_buffer.size(); - emit dataDurationChanged(bufferDuration()); - setRecordPosition(bufferDuration()); - result = true; - } else if (m_file) { - const qint64 length = m_wavFile.readData(*m_file, m_buffer, m_format); - if (length) { - m_dataLength = length; - emit dataDurationChanged(dataDuration()); - setRecordPosition(dataDuration()); + // Call function defined in utils.h, at global scope + ::generateTone(m_tone, m_format, m_buffer); + m_dataLength = m_buffer.size(); + emit dataDurationChanged(bufferDuration()); + setRecordPosition(bufferDuration()); + result = true; + } else if (m_file) { + const qint64 length = m_wavFile.readData(*m_file, m_buffer, m_format); + if (length) { + m_dataLength = length; + emit dataDurationChanged(dataDuration()); + setRecordPosition(dataDuration()); + result = true; + } + } else { + m_audioInput = new QAudioInput(m_audioInputDevice, m_format, this); + m_audioInput->setNotifyInterval(NotifyIntervalMs); result = true; } - } else { - m_audioInput = new QAudioInput(m_audioInputDevice, m_format, this); - m_audioInput->setNotifyInterval(NotifyIntervalMs); - result = true; - } - m_audioOutput = new QAudioOutput(m_audioOutputDevice, m_format, this); - m_audioOutput->setNotifyInterval(NotifyIntervalMs); - m_spectrumLengthBytes = SpectrumLengthSamples * - (m_format.sampleSize() / 8) * m_format.channels(); + m_audioOutput = new QAudioOutput(m_audioOutputDevice, m_format, this); + m_audioOutput->setNotifyInterval(NotifyIntervalMs); + m_spectrumLengthBytes = SpectrumLengthSamples * + (m_format.sampleSize() / 8) * m_format.channels(); + } } else { if (m_file) emit errorMessage(tr("Audio format not supported"), @@ -510,12 +516,14 @@ bool Engine::selectFormat() { bool foundSupportedFormat = false; - if (m_file) { - // Header is read from the WAV file; just need to check whether - // it is supported by the audio output device - QAudioFormat format = m_wavFile.format(); - if (m_audioOutputDevice.isFormatSupported(m_wavFile.format())) { - setFormat(m_wavFile.format()); + if (m_file || QAudioFormat() != m_format) { + QAudioFormat format = m_format; + if (m_file) + // Header is read from the WAV file; just need to check whether + // it is supported by the audio output device + format = m_wavFile.format(); + if (m_audioOutputDevice.isFormatSupported(format)) { + setFormat(format); foundSupportedFormat = true; } else { // Try flipping mono <-> stereo diff --git a/demos/spectrum/app/mainwidget.cpp b/demos/spectrum/app/mainwidget.cpp index dd51a91..fba28c6 100644 --- a/demos/spectrum/app/mainwidget.cpp +++ b/demos/spectrum/app/mainwidget.cpp @@ -200,19 +200,20 @@ void MainWidget::dataDurationChanged(qint64 duration) void MainWidget::showFileDialog() { - reset(); const QString dir; const QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open WAV file"), dir, "*.wav"); if (fileNames.count()) { + reset(); setMode(LoadFileMode); m_engine->loadFile(fileNames.front()); updateButtonStates(); + } else { + updateModeMenu(); } } void MainWidget::showSettingsDialog() { - reset(); m_settingsDialog->exec(); if (m_settingsDialog->result() == QDialog::Accepted) { m_engine->setAudioInputDevice(m_settingsDialog->inputDevice()); @@ -223,9 +224,9 @@ void MainWidget::showSettingsDialog() void MainWidget::showToneGeneratorDialog() { - reset(); m_toneGeneratorDialog->exec(); if (m_toneGeneratorDialog->result() == QDialog::Accepted) { + reset(); setMode(GenerateToneMode); const qreal amplitude = m_toneGeneratorDialog->amplitude(); if (m_toneGeneratorDialog->isFrequencySweepEnabled()) { @@ -236,6 +237,8 @@ void MainWidget::showToneGeneratorDialog() m_engine->generateTone(tone); updateButtonStates(); } + } else { + updateModeMenu(); } } @@ -445,10 +448,14 @@ void MainWidget::reset() void MainWidget::setMode(Mode mode) { - m_mode = mode; - m_loadFileAction->setChecked(LoadFileMode == mode); - m_generateToneAction->setChecked(GenerateToneMode == mode); - m_recordAction->setChecked(RecordMode == mode); + updateModeMenu(); +} + +void MainWidget::updateModeMenu() +{ + m_loadFileAction->setChecked(LoadFileMode == m_mode); + m_generateToneAction->setChecked(GenerateToneMode == m_mode); + m_recordAction->setChecked(RecordMode == m_mode); } diff --git a/demos/spectrum/app/mainwidget.h b/demos/spectrum/app/mainwidget.h index ddab8b7..cc1f9ed 100644 --- a/demos/spectrum/app/mainwidget.h +++ b/demos/spectrum/app/mainwidget.h @@ -89,6 +89,7 @@ private slots: void showToneGeneratorDialog(); void initializeRecord(); void dataDurationChanged(qint64 duration); + void updateModeMenu(); private: void createUi(); -- cgit v0.12