summaryrefslogtreecommitdiffstats
path: root/examples/multimedia/spectrum/mainwidget.cpp
blob: 214d317d9753b7aaffc405baf446d73f93b953ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "mainwidget.h"
#include "engine.h"
#include "levelmeter.h"
#include "progressbar.h"
#include "settingsdialog.h"
#include "spectrograph.h"
#include "tonegeneratordialog.h"
#include "utils.h"
#include "waveform.h"

#include <QFileDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QStyle>
#include <QTimerEvent>
#include <QVBoxLayout>

const int NullTimerId = -1;

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent),
      m_mode(NoMode),
      m_engine(new Engine(this))
#ifndef DISABLE_WAVEFORM
      ,
      m_waveform(new Waveform(this))
#endif
      ,
      m_progressBar(new ProgressBar(this)),
      m_spectrograph(new Spectrograph(this)),
      m_levelMeter(new LevelMeter(this)),
      m_modeButton(new QPushButton(this)),
      m_recordButton(new QPushButton(this)),
      m_pauseButton(new QPushButton(this)),
      m_playButton(new QPushButton(this)),
      m_settingsButton(new QPushButton(this)),
      m_infoMessage(new QLabel(tr("Select a mode to begin"), this)),
      m_infoMessageTimerId(NullTimerId),
      m_settingsDialog(new SettingsDialog(m_engine->availableAudioInputDevices(),
                                          m_engine->availableAudioOutputDevices(), this)),
      m_toneGeneratorDialog(new ToneGeneratorDialog(this)),
      m_modeMenu(new QMenu(this)),
      m_loadFileAction(nullptr),
      m_generateToneAction(nullptr),
      m_recordAction(nullptr),
      m_errorOccurred(false)
{
    m_spectrograph->setParams(SpectrumNumBands, SpectrumLowFreq, SpectrumHighFreq);

    createUi();
    connectUi();
}

MainWidget::~MainWidget() = default;

//-----------------------------------------------------------------------------
// Public slots
//-----------------------------------------------------------------------------

void MainWidget::stateChanged(QAudioDevice::Mode mode, QAudio::State state)
{
    Q_UNUSED(mode);

    updateButtonStates();

    if (QAudio::ActiveState != state && QAudio::SuspendedState != state) {
        m_levelMeter->reset();
        m_spectrograph->reset();
    }
}

void MainWidget::formatChanged(const QAudioFormat &format)
{
    infoMessage(formatToString(format), NullMessageTimeout);

#ifndef DISABLE_WAVEFORM
    if (QAudioFormat() != format) {
        m_waveform->initialize(format, WaveformTileLength, WaveformWindowDuration);
    }
#endif
}

void MainWidget::spectrumChanged(qint64 position, qint64 length, const FrequencySpectrum &spectrum)
{
    m_progressBar->windowChanged(position, length);
    m_spectrograph->spectrumChanged(spectrum);
}

void MainWidget::infoMessage(const QString &message, int timeoutMs)
{
    m_infoMessage->setText(message);

    if (NullTimerId != m_infoMessageTimerId) {
        killTimer(m_infoMessageTimerId);
        m_infoMessageTimerId = NullTimerId;
    }

    if (NullMessageTimeout != timeoutMs)
        m_infoMessageTimerId = startTimer(timeoutMs);
}

void MainWidget::errorMessage(const QString &heading, const QString &detail)
{
    QMessageBox::warning(this, heading, detail, QMessageBox::Close);
    m_errorOccurred = true;
    reset();
}

void MainWidget::timerEvent(QTimerEvent *event)
{
    Q_ASSERT(event->timerId() == m_infoMessageTimerId);
    Q_UNUSED(event); // suppress warnings in release builds
    killTimer(m_infoMessageTimerId);
    m_infoMessageTimerId = NullTimerId;
    m_infoMessage->setText("");
}

void MainWidget::audioPositionChanged(qint64 position)
{
#ifndef DISABLE_WAVEFORM
    m_waveform->audioPositionChanged(position);
#else
    Q_UNUSED(position);
#endif
}

void MainWidget::bufferLengthChanged(qint64 length)
{
    m_progressBar->bufferLengthChanged(length);
}

//-----------------------------------------------------------------------------
// Private slots
//-----------------------------------------------------------------------------

void MainWidget::showFileDialog()
{
    m_errorOccurred = false;
    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()
{
    m_settingsDialog->exec();
    if (m_settingsDialog->result() == QDialog::Accepted) {
        m_engine->setAudioInputDevice(m_settingsDialog->inputDevice());
        m_engine->setAudioOutputDevice(m_settingsDialog->outputDevice());
        m_engine->setWindowFunction(m_settingsDialog->windowFunction());
    }
}

void MainWidget::showToneGeneratorDialog()
{
    m_errorOccurred = false;
    m_toneGeneratorDialog->exec();
    if (m_toneGeneratorDialog->result() == QDialog::Accepted) {
        reset();
        setMode(GenerateToneMode);
        const qreal amplitude = m_toneGeneratorDialog->amplitude();
        if (m_toneGeneratorDialog->isFrequencySweepEnabled()) {
            m_engine->generateSweptTone(amplitude);
        } else {
            const qreal frequency = m_toneGeneratorDialog->frequency();
            const Tone tone(frequency, amplitude);
            m_engine->generateTone(tone);
            updateButtonStates();
        }
    } else {
        setMode(NoMode);
        updateModeMenu();
    }
}

void MainWidget::initializeRecord()
{
    m_errorOccurred = false;
    reset();
    setMode(RecordMode);
    if (m_engine->initializeRecord())
        updateButtonStates();
}

//-----------------------------------------------------------------------------
// Private functions
//-----------------------------------------------------------------------------

void MainWidget::createUi()
{
    createMenus();

    setWindowTitle(tr("Spectrum Analyser"));

    QVBoxLayout *windowLayout = new QVBoxLayout(this);

    m_infoMessage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_infoMessage->setAlignment(Qt::AlignHCenter);
    windowLayout->addWidget(m_infoMessage);

#ifdef SUPERIMPOSE_PROGRESS_ON_WAVEFORM
    std::unique_ptr<QHBoxLayout> waveformLayout(new QHBoxLayout);
    waveformLayout->addWidget(m_progressBar);
    m_progressBar->setMinimumHeight(m_waveform->minimumHeight());
    waveformLayout->setContentsMargins(0, 0, 0, 0);
    m_waveform->setLayout(waveformLayout.release());
    windowLayout->addWidget(m_waveform);
#else
#    ifndef DISABLE_WAVEFORM
    windowLayout->addWidget(m_waveform);
#    endif // DISABLE_WAVEFORM
    windowLayout->addWidget(m_progressBar);
#endif // SUPERIMPOSE_PROGRESS_ON_WAVEFORM

    // Spectrograph and level meter

    std::unique_ptr<QHBoxLayout> analysisLayout(new QHBoxLayout);
    analysisLayout->addWidget(m_spectrograph);
    analysisLayout->addWidget(m_levelMeter);
    windowLayout->addLayout(analysisLayout.release());

    // Button panel

    const QSize buttonSize(30, 30);

    m_modeButton->setText(tr("Mode"));

    m_recordIcon = QIcon(":/images/record.png");
    m_recordButton->setIcon(m_recordIcon);
    m_recordButton->setEnabled(false);
    m_recordButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_recordButton->setMinimumSize(buttonSize);

    m_pauseIcon = style()->standardIcon(QStyle::SP_MediaPause);
    m_pauseButton->setIcon(m_pauseIcon);
    m_pauseButton->setEnabled(false);
    m_pauseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_pauseButton->setMinimumSize(buttonSize);

    m_playIcon = style()->standardIcon(QStyle::SP_MediaPlay);
    m_playButton->setIcon(m_playIcon);
    m_playButton->setEnabled(false);
    m_playButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_playButton->setMinimumSize(buttonSize);

    m_settingsIcon = QIcon(":/images/settings.png");
    m_settingsButton->setIcon(m_settingsIcon);
    m_settingsButton->setEnabled(true);
    m_settingsButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_settingsButton->setMinimumSize(buttonSize);

    std::unique_ptr<QHBoxLayout> buttonPanelLayout(new QHBoxLayout);
    buttonPanelLayout->addStretch();
    buttonPanelLayout->addWidget(m_modeButton);
    buttonPanelLayout->addWidget(m_recordButton);
    buttonPanelLayout->addWidget(m_pauseButton);
    buttonPanelLayout->addWidget(m_playButton);
    buttonPanelLayout->addWidget(m_settingsButton);

    QWidget *buttonPanel = new QWidget(this);
    buttonPanel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    buttonPanel->setLayout(buttonPanelLayout.release());

    std::unique_ptr<QHBoxLayout> bottomPaneLayout(new QHBoxLayout);
    bottomPaneLayout->addWidget(buttonPanel);
    windowLayout->addLayout(bottomPaneLayout.release());

    // Apply layout

    setLayout(windowLayout);
}

void MainWidget::connectUi()
{
    connect(m_recordButton, &QPushButton::clicked, m_engine, &Engine::startRecording);

    connect(m_pauseButton, &QPushButton::clicked, m_engine, &Engine::suspend);

    connect(m_playButton, &QPushButton::clicked, m_engine, &Engine::startPlayback);

    connect(m_settingsButton, &QPushButton::clicked, this, &MainWidget::showSettingsDialog);

    connect(m_engine, &Engine::stateChanged, this, &MainWidget::stateChanged);

    connect(m_engine, &Engine::formatChanged, this, &MainWidget::formatChanged);

    m_progressBar->bufferLengthChanged(m_engine->bufferLength());

    connect(m_engine, &Engine::bufferLengthChanged, this, &MainWidget::bufferLengthChanged);

    connect(m_engine, &Engine::dataLengthChanged, this, &MainWidget::updateButtonStates);

    connect(m_engine, &Engine::recordPositionChanged, m_progressBar,
            &ProgressBar::recordPositionChanged);

    connect(m_engine, &Engine::playPositionChanged, m_progressBar,
            &ProgressBar::playPositionChanged);

    connect(m_engine, &Engine::recordPositionChanged, this, &MainWidget::audioPositionChanged);

    connect(m_engine, &Engine::playPositionChanged, this, &MainWidget::audioPositionChanged);

    connect(m_engine, &Engine::levelChanged, m_levelMeter, &LevelMeter::levelChanged);

    connect(m_engine,
            QOverload<qint64, qint64, const FrequencySpectrum &>::of(&Engine::spectrumChanged),
            this,
            QOverload<qint64, qint64, const FrequencySpectrum &>::of(&MainWidget::spectrumChanged));

    connect(m_engine, &Engine::infoMessage, this, &MainWidget::infoMessage);

    connect(m_engine, &Engine::errorMessage, this, &MainWidget::errorMessage);

    connect(m_spectrograph, &Spectrograph::infoMessage, this, &MainWidget::infoMessage);

#ifndef DISABLE_WAVEFORM
    connect(m_engine, &Engine::bufferChanged, m_waveform, &Waveform::bufferChanged);
#endif
}

void MainWidget::createMenus()
{
    m_modeButton->setMenu(m_modeMenu);

    m_generateToneAction = m_modeMenu->addAction(tr("Play generated tone"));
    m_recordAction = m_modeMenu->addAction(tr("Record and play back"));
    m_loadFileAction = m_modeMenu->addAction(tr("Play file"));

    m_loadFileAction->setCheckable(true);
    m_generateToneAction->setCheckable(true);
    m_recordAction->setCheckable(true);

    connect(m_loadFileAction, &QAction::triggered, this, &MainWidget::showFileDialog);
    connect(m_generateToneAction, &QAction::triggered, this, &MainWidget::showToneGeneratorDialog);
    connect(m_recordAction, &QAction::triggered, this, &MainWidget::initializeRecord);
}

void MainWidget::updateButtonStates()
{
    const bool recordEnabled = ((QAudioDevice::Output == m_engine->mode()
                                 || (QAudio::ActiveState != m_engine->state()
                                     && QAudio::IdleState != m_engine->state()))
                                && RecordMode == m_mode);
    m_recordButton->setEnabled(m_errorOccurred ? false : recordEnabled);

    const bool pauseEnabled =
            (QAudio::ActiveState == m_engine->state() || QAudio::IdleState == m_engine->state());
    m_pauseButton->setEnabled(m_errorOccurred ? false : pauseEnabled);

    const bool playEnabled = (/*m_engine->dataLength() &&*/
                              (QAudioDevice::Output != m_engine->mode()
                               || (QAudio::ActiveState != m_engine->state()
                                   && QAudio::IdleState != m_engine->state())));

    m_playButton->setEnabled(m_errorOccurred ? false : playEnabled);
}

void MainWidget::reset()
{
#ifndef DISABLE_WAVEFORM
    m_waveform->reset();
#endif
    m_engine->reset();
    m_levelMeter->reset();
    m_spectrograph->reset();
    m_progressBar->reset();
    if (m_errorOccurred) {
        setMode(Mode::NoMode);
        updateButtonStates();
    }
}

void MainWidget::setMode(Mode mode)
{
    m_mode = mode;
    updateModeMenu();
}

void MainWidget::updateModeMenu()
{
    m_loadFileAction->setChecked(LoadFileMode == m_mode);
    m_generateToneAction->setChecked(GenerateToneMode == m_mode);
    m_recordAction->setChecked(RecordMode == m_mode);
}

#include "moc_mainwidget.cpp"