1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
42 #include "levelmeter.h"
43 #include "mainwidget.h"
45 #include "progressbar.h"
46 #include "settingsdialog.h"
47 #include "spectrograph.h"
48 #include "tonegeneratordialog.h"
52 #include <QPushButton>
53 #include <QHBoxLayout>
54 #include <QVBoxLayout>
57 #include <QFileDialog>
58 #include <QTimerEvent>
59 #include <QMessageBox>
61 const int NullTimerId = -1;
63 MainWidget::MainWidget(QWidget *parent)
66 , m_engine(new Engine(this))
67 #ifndef DISABLE_WAVEFORM
68 , m_waveform(new Waveform(this))
70 , m_progressBar(new ProgressBar(this))
71 , m_spectrograph(new Spectrograph(this))
72 , m_levelMeter(new LevelMeter(this))
73 , m_modeButton(new QPushButton(this))
74 , m_recordButton(new QPushButton(this))
75 , m_pauseButton(new QPushButton(this))
76 , m_playButton(new QPushButton(this))
77 , m_settingsButton(new QPushButton(this))
78 , m_infoMessage(new QLabel(tr("Select a mode to begin"), this))
79 , m_infoMessageTimerId(NullTimerId)
80 , m_settingsDialog(new SettingsDialog(
81 m_engine->availableAudioInputDevices(),
82 m_engine->availableAudioOutputDevices(),
84 , m_toneGeneratorDialog(new ToneGeneratorDialog(this))
85 , m_modeMenu(new QMenu(this))
87 , m_generateToneAction(0)
90 m_spectrograph->setParams(SpectrumNumBands, SpectrumLowFreq, SpectrumHighFreq);
96 MainWidget::~MainWidget()
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 void MainWidget::stateChanged(QAudio::Mode mode, QAudio::State state)
110 updateButtonStates();
112 if (QAudio::ActiveState != state && QAudio::SuspendedState != state) {
113 m_levelMeter->reset();
114 m_spectrograph->reset();
118 void MainWidget::formatChanged(const QAudioFormat &format)
120 infoMessage(formatToString(format), NullMessageTimeout);
122 #ifndef DISABLE_WAVEFORM
123 if (QAudioFormat() != format) {
124 m_waveform->initialize(format, WaveformTileLength,
125 WaveformWindowDuration);
130 void MainWidget::spectrumChanged(qint64 position, qint64 length,
131 const FrequencySpectrum &spectrum)
133 m_progressBar->windowChanged(position, length);
134 m_spectrograph->spectrumChanged(spectrum);
137 void MainWidget::infoMessage(const QString &message, int timeoutMs)
139 m_infoMessage->setText(message);
141 if (NullTimerId != m_infoMessageTimerId) {
142 killTimer(m_infoMessageTimerId);
143 m_infoMessageTimerId = NullTimerId;
146 if (NullMessageTimeout != timeoutMs)
147 m_infoMessageTimerId = startTimer(timeoutMs);
150 void MainWidget::errorMessage(const QString &heading, const QString &detail)
153 const QString message = heading + "\n" + detail;
154 QMessageBox::warning(this, "", message, QMessageBox::Close);
156 QMessageBox::warning(this, heading, detail, QMessageBox::Close);
160 void MainWidget::timerEvent(QTimerEvent *event)
162 Q_ASSERT(event->timerId() == m_infoMessageTimerId);
163 Q_UNUSED(event) // suppress warnings in release builds
164 killTimer(m_infoMessageTimerId);
165 m_infoMessageTimerId = NullTimerId;
166 m_infoMessage->setText("");
169 void MainWidget::audioPositionChanged(qint64 position)
171 #ifndef DISABLE_WAVEFORM
172 m_waveform->audioPositionChanged(position);
178 void MainWidget::bufferLengthChanged(qint64 length)
180 m_progressBar->bufferLengthChanged(length);
184 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
188 void MainWidget::showFileDialog()
191 const QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open WAV file"), dir, "*.wav");
192 if (fileNames.count()) {
194 setMode(LoadFileMode);
195 m_engine->loadFile(fileNames.front());
196 updateButtonStates();
202 void MainWidget::showSettingsDialog()
204 m_settingsDialog->exec();
205 if (m_settingsDialog->result() == QDialog::Accepted) {
206 m_engine->setAudioInputDevice(m_settingsDialog->inputDevice());
207 m_engine->setAudioOutputDevice(m_settingsDialog->outputDevice());
208 m_engine->setWindowFunction(m_settingsDialog->windowFunction());
212 void MainWidget::showToneGeneratorDialog()
214 m_toneGeneratorDialog->exec();
215 if (m_toneGeneratorDialog->result() == QDialog::Accepted) {
217 setMode(GenerateToneMode);
218 const qreal amplitude = m_toneGeneratorDialog->amplitude();
219 if (m_toneGeneratorDialog->isFrequencySweepEnabled()) {
220 m_engine->generateSweptTone(amplitude);
222 const qreal frequency = m_toneGeneratorDialog->frequency();
223 const Tone tone(frequency, amplitude);
224 m_engine->generateTone(tone);
225 updateButtonStates();
232 void MainWidget::initializeRecord()
236 if (m_engine->initializeRecord())
237 updateButtonStates();
241 //-----------------------------------------------------------------------------
243 //-----------------------------------------------------------------------------
245 void MainWidget::createUi()
249 setWindowTitle(tr("Spectrum Analyser"));
251 QVBoxLayout *windowLayout = new QVBoxLayout(this);
253 m_infoMessage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
254 m_infoMessage->setAlignment(Qt::AlignHCenter);
255 windowLayout->addWidget(m_infoMessage);
257 #ifdef SUPERIMPOSE_PROGRESS_ON_WAVEFORM
258 QScopedPointer<QHBoxLayout> waveformLayout(new QHBoxLayout);
259 waveformLayout->addWidget(m_progressBar);
260 m_progressBar->setMinimumHeight(m_waveform->minimumHeight());
261 waveformLayout->setMargin(0);
262 m_waveform->setLayout(waveformLayout.data());
263 waveformLayout.take();
264 windowLayout->addWidget(m_waveform);
266 #ifndef DISABLE_WAVEFORM
267 windowLayout->addWidget(m_waveform);
268 #endif // DISABLE_WAVEFORM
269 windowLayout->addWidget(m_progressBar);
270 #endif // SUPERIMPOSE_PROGRESS_ON_WAVEFORM
272 // Spectrograph and level meter
274 QScopedPointer<QHBoxLayout> analysisLayout(new QHBoxLayout);
275 analysisLayout->addWidget(m_spectrograph);
276 analysisLayout->addWidget(m_levelMeter);
277 windowLayout->addLayout(analysisLayout.data());
278 analysisLayout.take();
282 const QSize buttonSize(30, 30);
284 m_modeButton->setText(tr("Mode"));
286 m_recordIcon = QIcon(":/images/record.png");
287 m_recordButton->setIcon(m_recordIcon);
288 m_recordButton->setEnabled(false);
289 m_recordButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
290 m_recordButton->setMinimumSize(buttonSize);
292 m_pauseIcon = style()->standardIcon(QStyle::SP_MediaPause);
293 m_pauseButton->setIcon(m_pauseIcon);
294 m_pauseButton->setEnabled(false);
295 m_pauseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
296 m_pauseButton->setMinimumSize(buttonSize);
298 m_playIcon = style()->standardIcon(QStyle::SP_MediaPlay);
299 m_playButton->setIcon(m_playIcon);
300 m_playButton->setEnabled(false);
301 m_playButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
302 m_playButton->setMinimumSize(buttonSize);
304 m_settingsIcon = QIcon(":/images/settings.png");
305 m_settingsButton->setIcon(m_settingsIcon);
306 m_settingsButton->setEnabled(true);
307 m_settingsButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
308 m_settingsButton->setMinimumSize(buttonSize);
310 QScopedPointer<QHBoxLayout> buttonPanelLayout(new QHBoxLayout);
311 buttonPanelLayout->addStretch();
312 buttonPanelLayout->addWidget(m_modeButton);
313 buttonPanelLayout->addWidget(m_recordButton);
314 buttonPanelLayout->addWidget(m_pauseButton);
315 buttonPanelLayout->addWidget(m_playButton);
316 buttonPanelLayout->addWidget(m_settingsButton);
318 QWidget *buttonPanel = new QWidget(this);
319 buttonPanel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
320 buttonPanel->setLayout(buttonPanelLayout.data());
321 buttonPanelLayout.take(); // ownership transferred to buttonPanel
323 QScopedPointer<QHBoxLayout> bottomPaneLayout(new QHBoxLayout);
324 bottomPaneLayout->addWidget(buttonPanel);
325 windowLayout->addLayout(bottomPaneLayout.data());
326 bottomPaneLayout.take(); // ownership transferred to windowLayout
330 setLayout(windowLayout);
333 void MainWidget::connectUi()
335 CHECKED_CONNECT(m_recordButton, SIGNAL(clicked()),
336 m_engine, SLOT(startRecording()));
338 CHECKED_CONNECT(m_pauseButton, SIGNAL(clicked()),
339 m_engine, SLOT(suspend()));
341 CHECKED_CONNECT(m_playButton, SIGNAL(clicked()),
342 m_engine, SLOT(startPlayback()));
344 CHECKED_CONNECT(m_settingsButton, SIGNAL(clicked()),
345 this, SLOT(showSettingsDialog()));
347 CHECKED_CONNECT(m_engine, SIGNAL(stateChanged(QAudio::Mode,QAudio::State)),
348 this, SLOT(stateChanged(QAudio::Mode,QAudio::State)));
350 CHECKED_CONNECT(m_engine, SIGNAL(formatChanged(const QAudioFormat &)),
351 this, SLOT(formatChanged(const QAudioFormat &)));
353 m_progressBar->bufferLengthChanged(m_engine->bufferLength());
355 CHECKED_CONNECT(m_engine, SIGNAL(bufferLengthChanged(qint64)),
356 this, SLOT(bufferLengthChanged(qint64)));
358 CHECKED_CONNECT(m_engine, SIGNAL(dataLengthChanged(qint64)),
359 this, SLOT(updateButtonStates()));
361 CHECKED_CONNECT(m_engine, SIGNAL(recordPositionChanged(qint64)),
362 m_progressBar, SLOT(recordPositionChanged(qint64)));
364 CHECKED_CONNECT(m_engine, SIGNAL(playPositionChanged(qint64)),
365 m_progressBar, SLOT(playPositionChanged(qint64)));
367 CHECKED_CONNECT(m_engine, SIGNAL(recordPositionChanged(qint64)),
368 this, SLOT(audioPositionChanged(qint64)));
370 CHECKED_CONNECT(m_engine, SIGNAL(playPositionChanged(qint64)),
371 this, SLOT(audioPositionChanged(qint64)));
373 CHECKED_CONNECT(m_engine, SIGNAL(levelChanged(qreal, qreal, int)),
374 m_levelMeter, SLOT(levelChanged(qreal, qreal, int)));
376 CHECKED_CONNECT(m_engine, SIGNAL(spectrumChanged(qint64, qint64, const FrequencySpectrum &)),
377 this, SLOT(spectrumChanged(qint64, qint64, const FrequencySpectrum &)));
379 CHECKED_CONNECT(m_engine, SIGNAL(infoMessage(QString, int)),
380 this, SLOT(infoMessage(QString, int)));
382 CHECKED_CONNECT(m_engine, SIGNAL(errorMessage(QString, QString)),
383 this, SLOT(errorMessage(QString, QString)));
385 CHECKED_CONNECT(m_spectrograph, SIGNAL(infoMessage(QString, int)),
386 this, SLOT(infoMessage(QString, int)));
388 #ifndef DISABLE_WAVEFORM
389 CHECKED_CONNECT(m_engine, SIGNAL(bufferChanged(qint64, qint64, const QByteArray &)),
390 m_waveform, SLOT(bufferChanged(qint64, qint64, const QByteArray &)));
394 void MainWidget::createMenus()
396 m_modeButton->setMenu(m_modeMenu);
398 m_generateToneAction = m_modeMenu->addAction(tr("Play generated tone"));
399 m_recordAction = m_modeMenu->addAction(tr("Record and play back"));
400 m_loadFileAction = m_modeMenu->addAction(tr("Play file"));
402 m_loadFileAction->setCheckable(true);
403 m_generateToneAction->setCheckable(true);
404 m_recordAction->setCheckable(true);
406 connect(m_loadFileAction, SIGNAL(triggered(bool)), this, SLOT(showFileDialog()));
407 connect(m_generateToneAction, SIGNAL(triggered(bool)), this, SLOT(showToneGeneratorDialog()));
408 connect(m_recordAction, SIGNAL(triggered(bool)), this, SLOT(initializeRecord()));
411 void MainWidget::updateButtonStates()
413 const bool recordEnabled = ((QAudio::AudioOutput == m_engine->mode() ||
414 (QAudio::ActiveState != m_engine->state() &&
415 QAudio::IdleState != m_engine->state())) &&
416 RecordMode == m_mode);
417 m_recordButton->setEnabled(recordEnabled);
419 const bool pauseEnabled = (QAudio::ActiveState == m_engine->state() ||
420 QAudio::IdleState == m_engine->state());
421 m_pauseButton->setEnabled(pauseEnabled);
423 const bool playEnabled = (/*m_engine->dataLength() &&*/
424 (QAudio::AudioOutput != m_engine->mode() ||
425 (QAudio::ActiveState != m_engine->state() &&
426 QAudio::IdleState != m_engine->state())));
427 m_playButton->setEnabled(playEnabled);
430 void MainWidget::reset()
432 #ifndef DISABLE_WAVEFORM
436 m_levelMeter->reset();
437 m_spectrograph->reset();
438 m_progressBar->reset();
441 void MainWidget::setMode(Mode mode)
447 void MainWidget::updateModeMenu()
449 m_loadFileAction->setChecked(LoadFileMode == m_mode);
450 m_generateToneAction->setChecked(GenerateToneMode == m_mode);
451 m_recordAction->setChecked(RecordMode == m_mode);