demos/spectrum/app/engine.h
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:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are met:
       
    14 ** - Redistributions of source code must retain the above copyright notice,
       
    15 **   this list of conditions and the following disclaimer.
       
    16 ** - Redistributions in binary form must reproduce the above copyright notice,
       
    17 **   this list of conditions and the following disclaimer in the documentation
       
    18 **   and/or other materials provided with the distribution.
       
    19 ** - Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
       
    20 **   names of its contributors may be used to endorse or promote products
       
    21 **   derived from this software without specific prior written permission.
       
    22 **
       
    23 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    24 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    25 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    26 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
       
    27 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    28 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    29 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    30 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    31 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    32 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    33 ** POSSIBILITY OF SUCH DAMAGE.
       
    34 ** $QT_END_LICENSE$
       
    35 **
       
    36 *****************************************************************************/
       
    37 
       
    38 #ifndef ENGINE_H
       
    39 #define ENGINE_H
       
    40 
       
    41 #include "spectrum.h"
       
    42 #include "spectrumanalyser.h"
       
    43 #include "wavfile.h"
       
    44 
       
    45 #include <QObject>
       
    46 #include <QByteArray>
       
    47 #include <QBuffer>
       
    48 #include <QVector>
       
    49 #include <QtMultimedia/QAudioDeviceInfo>
       
    50 #include <QtMultimedia/QAudioFormat>
       
    51 
       
    52 #ifdef DUMP_CAPTURED_AUDIO
       
    53 #define DUMP_DATA
       
    54 #endif
       
    55 
       
    56 #ifdef DUMP_SPECTRUM
       
    57 #define DUMP_DATA
       
    58 #endif
       
    59 
       
    60 #ifdef DUMP_DATA
       
    61 #include <QDir>
       
    62 #endif
       
    63 
       
    64 class QAudioInput;
       
    65 class QAudioOutput;
       
    66 class FrequencySpectrum;
       
    67 class QFile;
       
    68 
       
    69 /**
       
    70  * This class interfaces with the QtMultimedia audio classes, and also with
       
    71  * the SpectrumAnalyser class.  Its role is to manage the capture and playback
       
    72  * of audio data, meanwhile performing real-time analysis of the audio level
       
    73  * and frequency spectrum.
       
    74  */
       
    75 class Engine : public QObject {
       
    76     Q_OBJECT
       
    77 public:
       
    78     Engine(QObject *parent = 0);
       
    79     ~Engine();
       
    80 
       
    81     const QList<QAudioDeviceInfo>& availableAudioInputDevices() const
       
    82                                     { return m_availableAudioInputDevices; }
       
    83 
       
    84     const QList<QAudioDeviceInfo>& availableAudioOutputDevices() const
       
    85                                     { return m_availableAudioOutputDevices; }
       
    86 
       
    87     QAudio::Mode mode() const       { return m_mode; }
       
    88     QAudio::State state() const     { return m_state; }
       
    89 
       
    90     /**
       
    91      * \return Reference to internal audio buffer
       
    92      * \note This reference is valid for the lifetime of the Engine
       
    93      */
       
    94     const QByteArray& buffer() const    { return m_buffer; }
       
    95 
       
    96     /**
       
    97      * \return Current audio format
       
    98      * \note May be QAudioFormat() if engine is not initialized
       
    99      */
       
   100     const QAudioFormat& format() const  { return m_format; }
       
   101 
       
   102     /**
       
   103      * Stop any ongoing recording or playback, and reset to ground state.
       
   104      */
       
   105     void reset();
       
   106 
       
   107     /**
       
   108      * Load data from WAV file
       
   109      */
       
   110     bool loadFile(const QString &fileName);
       
   111 
       
   112     /**
       
   113      * Generate tone
       
   114      */
       
   115     bool generateTone(const Tone &tone);
       
   116 
       
   117     /**
       
   118      * Generate tone
       
   119      */
       
   120     bool generateSweptTone(qreal amplitude);
       
   121 
       
   122     /**
       
   123      * Initialize for recording
       
   124      */
       
   125     bool initializeRecord();
       
   126 
       
   127     /**
       
   128      * Position of the audio input device.
       
   129      * \return Position in microseconds.
       
   130      */
       
   131     qint64 recordPosition() const   { return m_recordPosition; }
       
   132 
       
   133     /**
       
   134      * RMS level of the most recently processed set of audio samples.
       
   135      * \return Level in range (0.0, 1.0)
       
   136      */
       
   137     qreal rmsLevel() const          { return m_rmsLevel; }
       
   138 
       
   139     /**
       
   140      * Peak level of the most recently processed set of audio samples.
       
   141      * \return Level in range (0.0, 1.0)
       
   142      */
       
   143     qreal peakLevel() const         { return m_peakLevel; }
       
   144 
       
   145     /**
       
   146      * Position of the audio output device.
       
   147      * \return Position in microseconds.
       
   148      */
       
   149     qint64 playPosition() const     { return m_playPosition; }
       
   150 
       
   151     /**
       
   152      * Length of the internal engine buffer.
       
   153      * \return Buffer length in microseconds.
       
   154      */
       
   155     qint64 bufferDuration() const;
       
   156 
       
   157     /**
       
   158      * Amount of data held in the buffer.
       
   159      * \return Data duration in microseconds.
       
   160      */
       
   161     qint64 dataDuration() const;
       
   162 
       
   163     /**
       
   164      * Returns the size of the underlying audio buffer in bytes.
       
   165      * This should be an approximation of the capture latency.
       
   166      */
       
   167     qint64 audioBufferLength() const;
       
   168 
       
   169     /**
       
   170      * Set window function applied to audio data before spectral analysis.
       
   171      */
       
   172     void setWindowFunction(WindowFunction type);
       
   173 
       
   174 public slots:
       
   175     void startRecording();
       
   176     void startPlayback();
       
   177     void suspend();
       
   178     void setAudioInputDevice(const QAudioDeviceInfo &device);
       
   179     void setAudioOutputDevice(const QAudioDeviceInfo &device);
       
   180 
       
   181 signals:
       
   182     void stateChanged(QAudio::Mode mode, QAudio::State state);
       
   183 
       
   184     /**
       
   185      * Informational message for non-modal display
       
   186      */
       
   187     void infoMessage(const QString &message, int durationMs);
       
   188 
       
   189     /**
       
   190      * Error message for modal display
       
   191      */
       
   192     void errorMessage(const QString &heading, const QString &detail);
       
   193 
       
   194     /**
       
   195      * Format of audio data has changed
       
   196      */
       
   197     void formatChanged(const QAudioFormat &format);
       
   198 
       
   199     /**
       
   200      * Length of buffer has changed.
       
   201      * \param duration Duration in microseconds
       
   202      */
       
   203     void bufferDurationChanged(qint64 duration);
       
   204 
       
   205     /**
       
   206      * Amount of data in buffer has changed.
       
   207      * \param duration Duration of data in microseconds
       
   208      */
       
   209     void dataDurationChanged(qint64 duration);
       
   210 
       
   211     /**
       
   212      * Position of the audio input device has changed.
       
   213      * \param position Position in microseconds
       
   214      */
       
   215     void recordPositionChanged(qint64 position);
       
   216 
       
   217     /**
       
   218      * Position of the audio output device has changed.
       
   219      * \param position Position in microseconds
       
   220      */
       
   221     void playPositionChanged(qint64 position);
       
   222 
       
   223     /**
       
   224      * Level changed
       
   225      * \param rmsLevel RMS level in range 0.0 - 1.0
       
   226      * \param peakLevel Peak level in range 0.0 - 1.0
       
   227      * \param numSamples Number of audio samples analysed
       
   228      */
       
   229     void levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples);
       
   230 
       
   231     /**
       
   232      * Spectrum has changed.
       
   233      * \param position Position of start of window in microseconds
       
   234      * \param length   Length of window in microseconds
       
   235      * \param spectrum Resulting frequency spectrum
       
   236      */
       
   237     void spectrumChanged(qint64 position, qint64 length, const FrequencySpectrum &spectrum);
       
   238 
       
   239 private slots:
       
   240     void audioNotify();
       
   241     void audioStateChanged(QAudio::State state);
       
   242     void audioDataReady();
       
   243     void spectrumChanged(const FrequencySpectrum &spectrum);
       
   244 
       
   245 private:
       
   246     bool initialize();
       
   247     bool selectFormat();
       
   248     void stopRecording();
       
   249     void stopPlayback();
       
   250     void setState(QAudio::State state);
       
   251     void setState(QAudio::Mode mode, QAudio::State state);
       
   252     void setFormat(const QAudioFormat &format);
       
   253     void setRecordPosition(qint64 position, bool forceEmit = false);
       
   254     void setPlayPosition(qint64 position, bool forceEmit = false);
       
   255     void calculateLevel(qint64 position, qint64 length);
       
   256     void calculateSpectrum(qint64 position);
       
   257     void setLevel(qreal rmsLevel, qreal peakLevel, int numSamples);
       
   258 
       
   259 #ifdef DUMP_DATA
       
   260     void createOutputDir();
       
   261     QString outputPath() const { return m_outputDir.path(); }
       
   262 #endif
       
   263 
       
   264 #ifdef DUMP_CAPTURED_AUDIO
       
   265     void dumpData();
       
   266 #endif
       
   267 
       
   268 private:
       
   269     QAudio::Mode        m_mode;
       
   270     QAudio::State       m_state;
       
   271 
       
   272     bool                m_generateTone;
       
   273     SweptTone           m_tone;
       
   274 
       
   275     QFile*              m_file;
       
   276     WavFile             m_wavFile;
       
   277 
       
   278     QAudioFormat        m_format;
       
   279 
       
   280     const QList<QAudioDeviceInfo> m_availableAudioInputDevices;
       
   281     QAudioDeviceInfo    m_audioInputDevice;
       
   282     QAudioInput*        m_audioInput;
       
   283     QIODevice*          m_audioInputIODevice;
       
   284     qint64              m_recordPosition;
       
   285 
       
   286     const QList<QAudioDeviceInfo> m_availableAudioOutputDevices;
       
   287     QAudioDeviceInfo    m_audioOutputDevice;
       
   288     QAudioOutput*       m_audioOutput;
       
   289     qint64              m_playPosition;
       
   290     QBuffer             m_audioOutputIODevice;
       
   291 
       
   292     QByteArray          m_buffer;
       
   293     qint64              m_dataLength;
       
   294 
       
   295     qreal               m_rmsLevel;
       
   296     qreal               m_peakLevel;
       
   297 
       
   298     int                 m_spectrumLengthBytes;
       
   299     QByteArray          m_spectrumBuffer;
       
   300     SpectrumAnalyser    m_spectrumAnalyser;
       
   301     qint64              m_spectrumPosition;
       
   302 
       
   303     int                 m_count;
       
   304 
       
   305 #ifdef DUMP_DATA
       
   306     QDir                m_outputDir;
       
   307 #endif
       
   308 
       
   309 };
       
   310 
       
   311 #endif // ENGINE_H