diff options
author | Gareth Stockwell <ext-gareth.stockwell@nokia.com> | 2010-10-26 16:15:29 (GMT) |
---|---|---|
committer | Gareth Stockwell <ext-gareth.stockwell@nokia.com> | 2010-10-29 12:46:34 (GMT) |
commit | 3cf35876400cb008fb25c8a3191c996af6059264 (patch) | |
tree | d7f38aa848daf1f70c2f3fd878f4eddee067661f /demos/spectrum/app/engine.h | |
parent | a53df3596e2061c202845f9f1c183177ca8a7eda (diff) | |
download | Qt-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/engine.h')
-rw-r--r-- | demos/spectrum/app/engine.h | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/demos/spectrum/app/engine.h b/demos/spectrum/app/engine.h index e14ac83..c97083e 100644 --- a/demos/spectrum/app/engine.h +++ b/demos/spectrum/app/engine.h @@ -91,12 +91,6 @@ public: QAudio::State state() const { return m_state; } /** - * \return Reference to internal audio buffer - * \note This reference is valid for the lifetime of the Engine - */ - const QByteArray& buffer() const { return m_buffer; } - - /** * \return Current audio format * \note May be QAudioFormat() if engine is not initialized */ @@ -129,7 +123,7 @@ public: /** * Position of the audio input device. - * \return Position in microseconds. + * \return Position in bytes. */ qint64 recordPosition() const { return m_recordPosition; } @@ -147,27 +141,21 @@ public: /** * Position of the audio output device. - * \return Position in microseconds. + * \return Position in bytes. */ qint64 playPosition() const { return m_playPosition; } /** * Length of the internal engine buffer. - * \return Buffer length in microseconds. + * \return Buffer length in bytes. */ - qint64 bufferDuration() const; + qint64 bufferLength() const; /** * Amount of data held in the buffer. - * \return Data duration in microseconds. + * \return Data length in bytes. */ - qint64 dataDuration() const; - - /** - * Returns the size of the underlying audio buffer in bytes. - * This should be an approximation of the capture latency. - */ - qint64 audioBufferLength() const; + qint64 dataLength() const { return m_dataLength; } /** * Set window function applied to audio data before spectral analysis. @@ -203,23 +191,23 @@ signals: * Length of buffer has changed. * \param duration Duration in microseconds */ - void bufferDurationChanged(qint64 duration); + void bufferLengthChanged(qint64 duration); /** * Amount of data in buffer has changed. - * \param duration Duration of data in microseconds + * \param Length of data in bytes */ - void dataDurationChanged(qint64 duration); + void dataLengthChanged(qint64 duration); /** * Position of the audio input device has changed. - * \param position Position in microseconds + * \param position Position in bytes */ void recordPositionChanged(qint64 position); /** * Position of the audio output device has changed. - * \param position Position in microseconds + * \param position Position in bytes */ void playPositionChanged(qint64 position); @@ -233,12 +221,19 @@ signals: /** * Spectrum has changed. - * \param position Position of start of window in microseconds - * \param length Length of window in microseconds + * \param position Position of start of window in bytes + * \param length Length of window in bytes * \param spectrum Resulting frequency spectrum */ void spectrumChanged(qint64 position, qint64 length, const FrequencySpectrum &spectrum); + /** + * Buffer containing audio data has changed. + * \param position Position of start of buffer in bytes + * \param buffer Buffer + */ + void bufferChanged(qint64 position, qint64 length, const QByteArray &buffer); + private slots: void audioNotify(); void audioStateChanged(QAudio::State state); @@ -246,6 +241,7 @@ private slots: void spectrumChanged(const FrequencySpectrum &spectrum); private: + void resetAudioDevices(); bool initialize(); bool selectFormat(); void stopRecording(); @@ -275,8 +271,10 @@ private: bool m_generateTone; SweptTone m_tone; - QFile* m_file; - WavFile m_wavFile; + WavFile* m_file; + // We need a second file handle via which to read data into m_buffer + // for analysis + WavFile* m_analysisFile; QAudioFormat m_format; @@ -293,12 +291,15 @@ private: QBuffer m_audioOutputIODevice; QByteArray m_buffer; + qint64 m_bufferPosition; + qint64 m_bufferLength; qint64 m_dataLength; + int m_levelBufferLength; qreal m_rmsLevel; qreal m_peakLevel; - int m_spectrumLengthBytes; + int m_spectrumBufferLength; QByteArray m_spectrumBuffer; SpectrumAnalyser m_spectrumAnalyser; qint64 m_spectrumPosition; |