engine.h Example File
multimedia/spectrum/app/engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include "spectrum.h"
#include "spectrumanalyser.h"
#include "wavfile.h"
#include <QAudioDeviceInfo>
#include <QAudioFormat>
#include <QBuffer>
#include <QByteArray>
#include <QDir>
#include <QObject>
#include <QVector>
#ifdef DUMP_CAPTURED_AUDIO
#define DUMP_DATA
#endif
#ifdef DUMP_SPECTRUM
#define DUMP_DATA
#endif
class FrequencySpectrum;
class QAudioInput;
class QAudioOutput;
class Engine : public QObject
{
Q_OBJECT
public:
explicit Engine(QObject *parent = 0);
~Engine();
const QList<QAudioDeviceInfo> &availableAudioInputDevices() const
{ return m_availableAudioInputDevices; }
const QList<QAudioDeviceInfo> &availableAudioOutputDevices() const
{ return m_availableAudioOutputDevices; }
QAudio::Mode mode() const { return m_mode; }
QAudio::State state() const { return m_state; }
const QAudioFormat& format() const { return m_format; }
void reset();
bool loadFile(const QString &fileName);
bool generateTone(const Tone &tone);
bool generateSweptTone(qreal amplitude);
bool initializeRecord();
qint64 recordPosition() const { return m_recordPosition; }
qreal rmsLevel() const { return m_rmsLevel; }
qreal peakLevel() const { return m_peakLevel; }
qint64 playPosition() const { return m_playPosition; }
qint64 bufferLength() const;
qint64 dataLength() const { return m_dataLength; }
void setWindowFunction(WindowFunction type);
public slots:
void startRecording();
void startPlayback();
void suspend();
void setAudioInputDevice(const QAudioDeviceInfo &device);
void setAudioOutputDevice(const QAudioDeviceInfo &device);
signals:
void stateChanged(QAudio::Mode mode, QAudio::State state);
void infoMessage(const QString &message, int durationMs);
void errorMessage(const QString &heading, const QString &detail);
void formatChanged(const QAudioFormat &format);
void bufferLengthChanged(qint64 duration);
void dataLengthChanged(qint64 duration);
void recordPositionChanged(qint64 position);
void playPositionChanged(qint64 position);
void levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples);
void spectrumChanged(qint64 position, qint64 length, const FrequencySpectrum &spectrum);
void bufferChanged(qint64 position, qint64 length, const QByteArray &buffer);
private slots:
void audioNotify();
void audioStateChanged(QAudio::State state);
void audioDataReady();
void spectrumChanged(const FrequencySpectrum &spectrum);
private:
void resetAudioDevices();
bool initialize();
bool selectFormat();
void stopRecording();
void stopPlayback();
void setState(QAudio::State state);
void setState(QAudio::Mode mode, QAudio::State state);
void setFormat(const QAudioFormat &format);
void setRecordPosition(qint64 position, bool forceEmit = false);
void setPlayPosition(qint64 position, bool forceEmit = false);
void calculateLevel(qint64 position, qint64 length);
void calculateSpectrum(qint64 position);
void setLevel(qreal rmsLevel, qreal peakLevel, int numSamples);
#ifdef DUMP_DATA
void createOutputDir();
QString outputPath() const { return m_outputDir.path(); }
#endif
#ifdef DUMP_CAPTURED_AUDIO
void dumpData();
#endif
private:
QAudio::Mode m_mode;
QAudio::State m_state;
bool m_generateTone;
SweptTone m_tone;
WavFile* m_file;
WavFile* m_analysisFile;
QAudioFormat m_format;
const QList<QAudioDeviceInfo> m_availableAudioInputDevices;
QAudioDeviceInfo m_audioInputDevice;
QAudioInput* m_audioInput;
QIODevice* m_audioInputIODevice;
qint64 m_recordPosition;
const QList<QAudioDeviceInfo> m_availableAudioOutputDevices;
QAudioDeviceInfo m_audioOutputDevice;
QAudioOutput* m_audioOutput;
QString m_audioOutputCategory;
qint64 m_playPosition;
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_spectrumBufferLength;
QByteArray m_spectrumBuffer;
SpectrumAnalyser m_spectrumAnalyser;
qint64 m_spectrumPosition;
int m_count;
#ifdef DUMP_DATA
QDir m_outputDir;
#endif
};
#endif // ENGINE_H