summaryrefslogtreecommitdiffstats
path: root/demos/spectrum/app/mainwidget.cpp
diff options
context:
space:
mode:
authorGareth Stockwell <ext-gareth.stockwell@nokia.com>2010-10-26 16:15:29 (GMT)
committerGareth Stockwell <ext-gareth.stockwell@nokia.com>2010-10-29 12:46:34 (GMT)
commit3cf35876400cb008fb25c8a3191c996af6059264 (patch)
treed7f38aa848daf1f70c2f3fd878f4eddee067661f /demos/spectrum/app/mainwidget.cpp
parenta53df3596e2061c202845f9f1c183177ca8a7eda (diff)
downloadQt-3cf35876400cb008fb25c8a3191c996af6059264.zip
Qt-3cf35876400cb008fb25c8a3191c996af6059264.tar.gz
Qt-3cf35876400cb008fb25c8a3191c996af6059264.tar.bz2
Play whole file in spectrum analyzer demo
This change required a significant refactoring of the application, including: * Re-write WavFile class so that it inherits from QFile rather than taking a QIODevice as an argument to its method calls. * Modified Engine class so that its internal QByteArray buffer (m_buffer) need not correspond to the entire clip. This was done by introducing the m_bufferPosition variable, which indicates the offset from the start of the clip to the start of the region of the clip which is currently held in memory. For tone generation and record/playback modes, the buffer does still map directly to the whole clip, so m_bufferPosition is always zero. For file playback, the WavFile instance is the QIODevice which is passed to QAudioOutput; m_buffer is just the part of the file which is currently in memory for spectrum analysis, level calculation and waveform rendering. * For file playback, introduced a second WavFile instance as a member of the Engine class. This is because QFile::seek() is called in order to read the part of the file currently required for analysis. If the QAudioOutput implementation passes its QIODevice across a thread boundary, this seeking causes playback to jump around within the file rather than progressing smoothly forward. * Modified the audioLength utility function so that its return value is always a multiple of the sample size. In the process of making the above changes, a few other minor modifications were made: * Modify all internal APIs concerned with buffer offsets and lengths to deal in bytes. Previously, some calls passed values in microseconds and others in bytes, which was confusing. * Remove write functionality from WavFile class, since it is not used in this application. Task-number: QTBUG-12936
Diffstat (limited to 'demos/spectrum/app/mainwidget.cpp')
-rw-r--r--demos/spectrum/app/mainwidget.cpp46
1 files changed, 19 insertions, 27 deletions
diff --git a/demos/spectrum/app/mainwidget.cpp b/demos/spectrum/app/mainwidget.cpp
index fba28c6..4b53bbe 100644
--- a/demos/spectrum/app/mainwidget.cpp
+++ b/demos/spectrum/app/mainwidget.cpp
@@ -65,7 +65,7 @@ MainWidget::MainWidget(QWidget *parent)
, m_mode(NoMode)
, m_engine(new Engine(this))
#ifndef DISABLE_WAVEFORM
- , m_waveform(new Waveform(m_engine->buffer(), this))
+ , m_waveform(new Waveform(this))
#endif
, m_progressBar(new ProgressBar(this))
, m_spectrograph(new Spectrograph(this))
@@ -166,19 +166,18 @@ void MainWidget::timerEvent(QTimerEvent *event)
m_infoMessage->setText("");
}
-void MainWidget::positionChanged(qint64 positionUs)
+void MainWidget::audioPositionChanged(qint64 position)
{
#ifndef DISABLE_WAVEFORM
- qint64 positionBytes = audioLength(m_engine->format(), positionUs);
- m_waveform->positionChanged(positionBytes);
+ m_waveform->audioPositionChanged(position);
#else
- Q_UNUSED(positionUs)
+ Q_UNUSED(position)
#endif
}
-void MainWidget::bufferDurationChanged(qint64 durationUs)
+void MainWidget::bufferLengthChanged(qint64 length)
{
- m_progressBar->bufferDurationChanged(durationUs);
+ m_progressBar->bufferLengthChanged(length);
}
@@ -186,18 +185,6 @@ void MainWidget::bufferDurationChanged(qint64 durationUs)
// Private slots
//-----------------------------------------------------------------------------
-void MainWidget::dataDurationChanged(qint64 duration)
-{
-#ifndef DISABLE_WAVEFORM
- const qint64 dataLength = audioLength(m_engine->format(), duration);
- m_waveform->dataLengthChanged(dataLength);
-#else
- Q_UNUSED(duration)
-#endif
-
- updateButtonStates();
-}
-
void MainWidget::showFileDialog()
{
const QString dir;
@@ -363,13 +350,13 @@ void MainWidget::connectUi()
CHECKED_CONNECT(m_engine, SIGNAL(formatChanged(const QAudioFormat &)),
this, SLOT(formatChanged(const QAudioFormat &)));
- m_progressBar->bufferDurationChanged(m_engine->bufferDuration());
+ m_progressBar->bufferLengthChanged(m_engine->bufferLength());
- CHECKED_CONNECT(m_engine, SIGNAL(bufferDurationChanged(qint64)),
- this, SLOT(bufferDurationChanged(qint64)));
+ CHECKED_CONNECT(m_engine, SIGNAL(bufferLengthChanged(qint64)),
+ this, SLOT(bufferLengthChanged(qint64)));
- CHECKED_CONNECT(m_engine, SIGNAL(dataDurationChanged(qint64)),
- this, SLOT(dataDurationChanged(qint64)));
+ CHECKED_CONNECT(m_engine, SIGNAL(dataLengthChanged(qint64)),
+ this, SLOT(updateButtonStates()));
CHECKED_CONNECT(m_engine, SIGNAL(recordPositionChanged(qint64)),
m_progressBar, SLOT(recordPositionChanged(qint64)));
@@ -378,10 +365,10 @@ void MainWidget::connectUi()
m_progressBar, SLOT(playPositionChanged(qint64)));
CHECKED_CONNECT(m_engine, SIGNAL(recordPositionChanged(qint64)),
- this, SLOT(positionChanged(qint64)));
+ this, SLOT(audioPositionChanged(qint64)));
CHECKED_CONNECT(m_engine, SIGNAL(playPositionChanged(qint64)),
- this, SLOT(positionChanged(qint64)));
+ this, SLOT(audioPositionChanged(qint64)));
CHECKED_CONNECT(m_engine, SIGNAL(levelChanged(qreal, qreal, int)),
m_levelMeter, SLOT(levelChanged(qreal, qreal, int)));
@@ -397,6 +384,11 @@ void MainWidget::connectUi()
CHECKED_CONNECT(m_spectrograph, SIGNAL(infoMessage(QString, int)),
this, SLOT(infoMessage(QString, int)));
+
+#ifndef DISABLE_WAVEFORM
+ CHECKED_CONNECT(m_engine, SIGNAL(bufferChanged(qint64, qint64, const QByteArray &)),
+ m_waveform, SLOT(bufferChanged(qint64, qint64, const QByteArray &)));
+#endif
}
void MainWidget::createMenus()
@@ -428,7 +420,7 @@ void MainWidget::updateButtonStates()
QAudio::IdleState == m_engine->state());
m_pauseButton->setEnabled(pauseEnabled);
- const bool playEnabled = (m_engine->dataDuration() &&
+ const bool playEnabled = (/*m_engine->dataLength() &&*/
(QAudio::AudioOutput != m_engine->mode() ||
(QAudio::ActiveState != m_engine->state() &&
QAudio::IdleState != m_engine->state())));