demos/spectrum/app/mainwidget.cpp
changeset 25 e24348a560a6
child 29 b72c6db6890b
equal deleted inserted replaced
23:89e065397ea6 25:e24348a560a6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "engine.h"
       
    43 #include "levelmeter.h"
       
    44 #include "mainwidget.h"
       
    45 #include "waveform.h"
       
    46 #include "progressbar.h"
       
    47 #include "settingsdialog.h"
       
    48 #include "spectrograph.h"
       
    49 #include "tonegeneratordialog.h"
       
    50 #include "utils.h"
       
    51 
       
    52 #include <QLabel>
       
    53 #include <QPushButton>
       
    54 #include <QHBoxLayout>
       
    55 #include <QVBoxLayout>
       
    56 #include <QStyle>
       
    57 #include <QMenu>
       
    58 #include <QFileDialog>
       
    59 #include <QTimerEvent>
       
    60 #include <QMessageBox>
       
    61 
       
    62 const int NullTimerId = -1;
       
    63 
       
    64 MainWidget::MainWidget(QWidget *parent)
       
    65     :   QWidget(parent)
       
    66     ,   m_mode(NoMode)
       
    67     ,   m_engine(new Engine(this))
       
    68 #ifndef DISABLE_WAVEFORM
       
    69     ,   m_waveform(new Waveform(m_engine->buffer(), this))
       
    70 #endif
       
    71     ,   m_progressBar(new ProgressBar(this))
       
    72     ,   m_spectrograph(new Spectrograph(this))
       
    73     ,   m_levelMeter(new LevelMeter(this))
       
    74     ,   m_modeButton(new QPushButton(this))
       
    75     ,   m_recordButton(new QPushButton(this))
       
    76     ,   m_pauseButton(new QPushButton(this))
       
    77     ,   m_playButton(new QPushButton(this))
       
    78     ,   m_settingsButton(new QPushButton(this))
       
    79     ,   m_infoMessage(new QLabel(tr("Select a mode to begin"), this))
       
    80     ,   m_infoMessageTimerId(NullTimerId)
       
    81     ,   m_settingsDialog(new SettingsDialog(
       
    82             m_engine->availableAudioInputDevices(),
       
    83             m_engine->availableAudioOutputDevices(),
       
    84             this))
       
    85     ,   m_toneGeneratorDialog(new ToneGeneratorDialog(this))
       
    86     ,   m_modeMenu(new QMenu(this))
       
    87     ,   m_loadFileAction(0)
       
    88     ,   m_generateToneAction(0)
       
    89     ,   m_recordAction(0)
       
    90 {
       
    91     m_spectrograph->setParams(SpectrumNumBands, SpectrumLowFreq, SpectrumHighFreq);
       
    92 
       
    93     createUi();
       
    94     connectUi();
       
    95 }
       
    96 
       
    97 MainWidget::~MainWidget()
       
    98 {
       
    99 
       
   100 }
       
   101 
       
   102 
       
   103 //-----------------------------------------------------------------------------
       
   104 // Public slots
       
   105 //-----------------------------------------------------------------------------
       
   106 
       
   107 void MainWidget::stateChanged(QAudio::Mode mode, QAudio::State state)
       
   108 {
       
   109     Q_UNUSED(mode);
       
   110 
       
   111     updateButtonStates();
       
   112 
       
   113     if (QAudio::ActiveState != state && QAudio::SuspendedState != state) {
       
   114         m_levelMeter->reset();
       
   115         m_spectrograph->reset();
       
   116     }
       
   117 }
       
   118 
       
   119 void MainWidget::formatChanged(const QAudioFormat &format)
       
   120 {
       
   121    infoMessage(formatToString(format), NullMessageTimeout);
       
   122 
       
   123 #ifndef DISABLE_WAVEFORM
       
   124     if (QAudioFormat() != format) {
       
   125         m_waveform->initialize(format, WaveformTileLength,
       
   126                                WaveformWindowDuration);
       
   127     }
       
   128 #endif
       
   129 }
       
   130 
       
   131 void MainWidget::spectrumChanged(qint64 position, qint64 length,
       
   132                                  const FrequencySpectrum &spectrum)
       
   133 {
       
   134     m_progressBar->windowChanged(position, length);
       
   135     m_spectrograph->spectrumChanged(spectrum);
       
   136 }
       
   137 
       
   138 void MainWidget::infoMessage(const QString &message, int timeoutMs)
       
   139 {
       
   140     m_infoMessage->setText(message);
       
   141 
       
   142     if (NullTimerId != m_infoMessageTimerId) {
       
   143         killTimer(m_infoMessageTimerId);
       
   144         m_infoMessageTimerId = NullTimerId;
       
   145     }
       
   146 
       
   147     if (NullMessageTimeout != timeoutMs)
       
   148         m_infoMessageTimerId = startTimer(timeoutMs);
       
   149 }
       
   150 
       
   151 void MainWidget::errorMessage(const QString &heading, const QString &detail)
       
   152 {
       
   153 #ifdef Q_OS_SYMBIAN
       
   154     const QString message = heading + "\n" + detail;
       
   155     QMessageBox::warning(this, "", message, QMessageBox::Close);
       
   156 #else
       
   157     QMessageBox::warning(this, heading, detail, QMessageBox::Close);
       
   158 #endif
       
   159 }
       
   160 
       
   161 void MainWidget::timerEvent(QTimerEvent *event)
       
   162 {
       
   163     Q_ASSERT(event->timerId() == m_infoMessageTimerId);
       
   164     Q_UNUSED(event) // suppress warnings in release builds
       
   165     killTimer(m_infoMessageTimerId);
       
   166     m_infoMessageTimerId = NullTimerId;
       
   167     m_infoMessage->setText("");
       
   168 }
       
   169 
       
   170 void MainWidget::positionChanged(qint64 positionUs)
       
   171 {
       
   172 #ifndef DISABLE_WAVEFORM
       
   173     qint64 positionBytes = audioLength(m_engine->format(), positionUs);
       
   174     m_waveform->positionChanged(positionBytes);
       
   175 #else
       
   176     Q_UNUSED(positionUs)
       
   177 #endif
       
   178 }
       
   179 
       
   180 void MainWidget::bufferDurationChanged(qint64 durationUs)
       
   181 {
       
   182     m_progressBar->bufferDurationChanged(durationUs);
       
   183 }
       
   184 
       
   185 
       
   186 //-----------------------------------------------------------------------------
       
   187 // Private slots
       
   188 //-----------------------------------------------------------------------------
       
   189 
       
   190 void MainWidget::dataDurationChanged(qint64 duration)
       
   191 {
       
   192 #ifndef DISABLE_WAVEFORM
       
   193     const qint64 dataLength = audioLength(m_engine->format(), duration);
       
   194     m_waveform->dataLengthChanged(dataLength);
       
   195 #else
       
   196     Q_UNUSED(duration)
       
   197 #endif
       
   198 
       
   199     updateButtonStates();
       
   200 }
       
   201 
       
   202 void MainWidget::showFileDialog()
       
   203 {
       
   204     reset();
       
   205     const QString dir;
       
   206     const QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open WAV file"), dir, "*.wav");
       
   207     if (fileNames.count()) {
       
   208         setMode(LoadFileMode);
       
   209         m_engine->loadFile(fileNames.front());
       
   210         updateButtonStates();
       
   211     }
       
   212 }
       
   213 
       
   214 void MainWidget::showSettingsDialog()
       
   215 {
       
   216     reset();
       
   217     m_settingsDialog->exec();
       
   218     if (m_settingsDialog->result() == QDialog::Accepted) {
       
   219         m_engine->setAudioInputDevice(m_settingsDialog->inputDevice());
       
   220         m_engine->setAudioOutputDevice(m_settingsDialog->outputDevice());
       
   221         m_engine->setWindowFunction(m_settingsDialog->windowFunction());
       
   222     }
       
   223 }
       
   224 
       
   225 void MainWidget::showToneGeneratorDialog()
       
   226 {
       
   227     reset();
       
   228     m_toneGeneratorDialog->exec();
       
   229     if (m_toneGeneratorDialog->result() == QDialog::Accepted) {
       
   230         setMode(GenerateToneMode);
       
   231         const qreal amplitude = m_toneGeneratorDialog->amplitude();
       
   232         if (m_toneGeneratorDialog->isFrequencySweepEnabled()) {
       
   233             m_engine->generateSweptTone(amplitude);
       
   234         } else {
       
   235             const qreal frequency = m_toneGeneratorDialog->frequency();
       
   236             const Tone tone(frequency, amplitude);
       
   237             m_engine->generateTone(tone);
       
   238             updateButtonStates();
       
   239         }
       
   240     }
       
   241 }
       
   242 
       
   243 void MainWidget::initializeRecord()
       
   244 {
       
   245     reset();
       
   246     setMode(RecordMode);
       
   247     if (m_engine->initializeRecord())
       
   248         updateButtonStates();
       
   249 }
       
   250 
       
   251 
       
   252 //-----------------------------------------------------------------------------
       
   253 // Private functions
       
   254 //-----------------------------------------------------------------------------
       
   255 
       
   256 void MainWidget::createUi()
       
   257 {
       
   258     createMenus();
       
   259 
       
   260     setWindowTitle(tr("Spectrum Analyser"));
       
   261 
       
   262     QVBoxLayout *windowLayout = new QVBoxLayout(this);
       
   263 
       
   264     m_infoMessage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
       
   265     m_infoMessage->setAlignment(Qt::AlignHCenter);
       
   266     windowLayout->addWidget(m_infoMessage);
       
   267 
       
   268 #ifdef SUPERIMPOSE_PROGRESS_ON_WAVEFORM
       
   269     QScopedPointer<QHBoxLayout> waveformLayout(new QHBoxLayout);
       
   270     waveformLayout->addWidget(m_progressBar);
       
   271     m_progressBar->setMinimumHeight(m_waveform->minimumHeight());
       
   272     waveformLayout->setMargin(0);
       
   273     m_waveform->setLayout(waveformLayout.data());
       
   274     waveformLayout.take();
       
   275     windowLayout->addWidget(m_waveform);
       
   276 #else
       
   277 #ifndef DISABLE_WAVEFORM
       
   278     windowLayout->addWidget(m_waveform);
       
   279 #endif // DISABLE_WAVEFORM
       
   280     windowLayout->addWidget(m_progressBar);
       
   281 #endif // SUPERIMPOSE_PROGRESS_ON_WAVEFORM
       
   282 
       
   283     // Spectrograph and level meter
       
   284 
       
   285     QScopedPointer<QHBoxLayout> analysisLayout(new QHBoxLayout);
       
   286     analysisLayout->addWidget(m_spectrograph);
       
   287     analysisLayout->addWidget(m_levelMeter);
       
   288     windowLayout->addLayout(analysisLayout.data());
       
   289     analysisLayout.take();
       
   290 
       
   291     // Button panel
       
   292 
       
   293     const QSize buttonSize(30, 30);
       
   294 
       
   295     m_modeButton->setText(tr("Mode"));
       
   296 
       
   297     m_recordIcon = QIcon(":/images/record.png");
       
   298     m_recordButton->setIcon(m_recordIcon);
       
   299     m_recordButton->setEnabled(false);
       
   300     m_recordButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   301     m_recordButton->setMinimumSize(buttonSize);
       
   302 
       
   303     m_pauseIcon = style()->standardIcon(QStyle::SP_MediaPause);
       
   304     m_pauseButton->setIcon(m_pauseIcon);
       
   305     m_pauseButton->setEnabled(false);
       
   306     m_pauseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   307     m_pauseButton->setMinimumSize(buttonSize);
       
   308 
       
   309     m_playIcon = style()->standardIcon(QStyle::SP_MediaPlay);
       
   310     m_playButton->setIcon(m_playIcon);
       
   311     m_playButton->setEnabled(false);
       
   312     m_playButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   313     m_playButton->setMinimumSize(buttonSize);
       
   314 
       
   315     m_settingsIcon = QIcon(":/images/settings.png");
       
   316     m_settingsButton->setIcon(m_settingsIcon);
       
   317     m_settingsButton->setEnabled(true);
       
   318     m_settingsButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   319     m_settingsButton->setMinimumSize(buttonSize);
       
   320 
       
   321     QScopedPointer<QHBoxLayout> buttonPanelLayout(new QHBoxLayout);
       
   322     buttonPanelLayout->addStretch();
       
   323     buttonPanelLayout->addWidget(m_modeButton);
       
   324     buttonPanelLayout->addWidget(m_recordButton);
       
   325     buttonPanelLayout->addWidget(m_pauseButton);
       
   326     buttonPanelLayout->addWidget(m_playButton);
       
   327     buttonPanelLayout->addWidget(m_settingsButton);
       
   328 
       
   329     QWidget *buttonPanel = new QWidget(this);
       
   330     buttonPanel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   331     buttonPanel->setLayout(buttonPanelLayout.data());
       
   332     buttonPanelLayout.take(); // ownership transferred to buttonPanel
       
   333 
       
   334     QScopedPointer<QHBoxLayout> bottomPaneLayout(new QHBoxLayout);
       
   335     bottomPaneLayout->addWidget(buttonPanel);
       
   336     windowLayout->addLayout(bottomPaneLayout.data());
       
   337     bottomPaneLayout.take(); // ownership transferred to windowLayout
       
   338 
       
   339     // Apply layout
       
   340 
       
   341     setLayout(windowLayout);
       
   342 }
       
   343 
       
   344 void MainWidget::connectUi()
       
   345 {
       
   346     CHECKED_CONNECT(m_recordButton, SIGNAL(clicked()),
       
   347             m_engine, SLOT(startRecording()));
       
   348 
       
   349     CHECKED_CONNECT(m_pauseButton, SIGNAL(clicked()),
       
   350             m_engine, SLOT(suspend()));
       
   351 
       
   352     CHECKED_CONNECT(m_playButton, SIGNAL(clicked()),
       
   353             m_engine, SLOT(startPlayback()));
       
   354 
       
   355     CHECKED_CONNECT(m_settingsButton, SIGNAL(clicked()),
       
   356             this, SLOT(showSettingsDialog()));
       
   357 
       
   358     CHECKED_CONNECT(m_engine, SIGNAL(stateChanged(QAudio::Mode,QAudio::State)),
       
   359             this, SLOT(stateChanged(QAudio::Mode,QAudio::State)));
       
   360 
       
   361     CHECKED_CONNECT(m_engine, SIGNAL(formatChanged(const QAudioFormat &)),
       
   362             this, SLOT(formatChanged(const QAudioFormat &)));
       
   363 
       
   364     m_progressBar->bufferDurationChanged(m_engine->bufferDuration());
       
   365 
       
   366     CHECKED_CONNECT(m_engine, SIGNAL(bufferDurationChanged(qint64)),
       
   367             this, SLOT(bufferDurationChanged(qint64)));
       
   368 
       
   369     CHECKED_CONNECT(m_engine, SIGNAL(dataDurationChanged(qint64)),
       
   370             this, SLOT(dataDurationChanged(qint64)));
       
   371 
       
   372     CHECKED_CONNECT(m_engine, SIGNAL(recordPositionChanged(qint64)),
       
   373             m_progressBar, SLOT(recordPositionChanged(qint64)));
       
   374 
       
   375     CHECKED_CONNECT(m_engine, SIGNAL(playPositionChanged(qint64)),
       
   376             m_progressBar, SLOT(playPositionChanged(qint64)));
       
   377 
       
   378     CHECKED_CONNECT(m_engine, SIGNAL(recordPositionChanged(qint64)),
       
   379             this, SLOT(positionChanged(qint64)));
       
   380 
       
   381     CHECKED_CONNECT(m_engine, SIGNAL(playPositionChanged(qint64)),
       
   382             this, SLOT(positionChanged(qint64)));
       
   383 
       
   384     CHECKED_CONNECT(m_engine, SIGNAL(levelChanged(qreal, qreal, int)),
       
   385             m_levelMeter, SLOT(levelChanged(qreal, qreal, int)));
       
   386 
       
   387     CHECKED_CONNECT(m_engine, SIGNAL(spectrumChanged(qint64, qint64, const FrequencySpectrum &)),
       
   388             this, SLOT(spectrumChanged(qint64, qint64, const FrequencySpectrum &)));
       
   389 
       
   390     CHECKED_CONNECT(m_engine, SIGNAL(infoMessage(QString, int)),
       
   391             this, SLOT(infoMessage(QString, int)));
       
   392 
       
   393     CHECKED_CONNECT(m_engine, SIGNAL(errorMessage(QString, QString)),
       
   394             this, SLOT(errorMessage(QString, QString)));
       
   395 
       
   396     CHECKED_CONNECT(m_spectrograph, SIGNAL(infoMessage(QString, int)),
       
   397             this, SLOT(infoMessage(QString, int)));
       
   398 }
       
   399 
       
   400 void MainWidget::createMenus()
       
   401 {
       
   402     m_modeButton->setMenu(m_modeMenu);
       
   403 
       
   404     m_generateToneAction = m_modeMenu->addAction(tr("Play generated tone"));
       
   405     m_recordAction = m_modeMenu->addAction(tr("Record and play back"));
       
   406     m_loadFileAction = m_modeMenu->addAction(tr("Play file"));
       
   407 
       
   408     m_loadFileAction->setCheckable(true);
       
   409     m_generateToneAction->setCheckable(true);
       
   410     m_recordAction->setCheckable(true);
       
   411 
       
   412     connect(m_loadFileAction, SIGNAL(triggered(bool)), this, SLOT(showFileDialog()));
       
   413     connect(m_generateToneAction, SIGNAL(triggered(bool)), this, SLOT(showToneGeneratorDialog()));
       
   414     connect(m_recordAction, SIGNAL(triggered(bool)), this, SLOT(initializeRecord()));
       
   415 }
       
   416 
       
   417 void MainWidget::updateButtonStates()
       
   418 {
       
   419     const bool recordEnabled = ((QAudio::AudioOutput == m_engine->mode() ||
       
   420                                 (QAudio::ActiveState != m_engine->state() &&
       
   421                                  QAudio::IdleState != m_engine->state())) &&
       
   422                                 RecordMode == m_mode);
       
   423     m_recordButton->setEnabled(recordEnabled);
       
   424 
       
   425     const bool pauseEnabled = (QAudio::ActiveState == m_engine->state() ||
       
   426                                QAudio::IdleState == m_engine->state());
       
   427     m_pauseButton->setEnabled(pauseEnabled);
       
   428 
       
   429     const bool playEnabled = (m_engine->dataDuration() &&
       
   430                               (QAudio::AudioOutput != m_engine->mode() ||
       
   431                                (QAudio::ActiveState != m_engine->state() &&
       
   432                                 QAudio::IdleState != m_engine->state())));
       
   433     m_playButton->setEnabled(playEnabled);
       
   434 }
       
   435 
       
   436 void MainWidget::reset()
       
   437 {
       
   438 #ifndef DISABLE_WAVEFORM
       
   439     m_waveform->reset();
       
   440 #endif
       
   441     m_engine->reset();
       
   442     m_levelMeter->reset();
       
   443     m_spectrograph->reset();
       
   444     m_progressBar->reset();
       
   445 }
       
   446 
       
   447 void MainWidget::setMode(Mode mode)
       
   448 {
       
   449 
       
   450     m_mode = mode;
       
   451     m_loadFileAction->setChecked(LoadFileMode == mode);
       
   452     m_generateToneAction->setChecked(GenerateToneMode == mode);
       
   453     m_recordAction->setChecked(RecordMode == mode);
       
   454 }
       
   455