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
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef ENGINE_H
#define ENGINE_H
#include "spectrum.h"
#include "spectrumanalyser.h"
#include "wavfile.h"
#include <QObject>
#include <QByteArray>
#include <QBuffer>
#include <QVector>
#include <QtMultimedia/QAudioDeviceInfo>
#include <QtMultimedia/QAudioFormat>
#ifdef DUMP_CAPTURED_AUDIO
#define DUMP_DATA
#endif
#ifdef DUMP_SPECTRUM
#define DUMP_DATA
#endif
#ifdef DUMP_DATA
#include <QDir>
#endif
class FrequencySpectrum;
QT_FORWARD_DECLARE_CLASS(QAudioInput)
QT_FORWARD_DECLARE_CLASS(QAudioOutput)
QT_FORWARD_DECLARE_CLASS(QFile)
/**
* This class interfaces with the QtMultimedia audio classes, and also with
* the SpectrumAnalyser class. Its role is to manage the capture and playback
* of audio data, meanwhile performing real-time analysis of the audio level
* and frequency spectrum.
*/
class Engine : public QObject {
Q_OBJECT
public:
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; }
/**
* \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
*/
const QAudioFormat& format() const { return m_format; }
/**
* Stop any ongoing recording or playback, and reset to ground state.
*/
void reset();
/**
* Load data from WAV file
*/
bool loadFile(const QString &fileName);
/**
* Generate tone
*/
bool generateTone(const Tone &tone);
/**
* Generate tone
*/
bool generateSweptTone(qreal amplitude);
/**
* Initialize for recording
*/
bool initializeRecord();
/**
* Position of the audio input device.
* \return Position in microseconds.
*/
qint64 recordPosition() const { return m_recordPosition; }
/**
* RMS level of the most recently processed set of audio samples.
* \return Level in range (0.0, 1.0)
*/
qreal rmsLevel() const { return m_rmsLevel; }
/**
* Peak level of the most recently processed set of audio samples.
* \return Level in range (0.0, 1.0)
*/
qreal peakLevel() const { return m_peakLevel; }
/**
* Position of the audio output device.
* \return Position in microseconds.
*/
qint64 playPosition() const { return m_playPosition; }
/**
* Length of the internal engine buffer.
* \return Buffer length in microseconds.
*/
qint64 bufferDuration() const;
/**
* Amount of data held in the buffer.
* \return Data duration in microseconds.
*/
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;
/**
* Set window function applied to audio data before spectral analysis.
*/
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);
/**
* Informational message for non-modal display
*/
void infoMessage(const QString &message, int durationMs);
/**
* Error message for modal display
*/
void errorMessage(const QString &heading, const QString &detail);
/**
* Format of audio data has changed
*/
void formatChanged(const QAudioFormat &format);
/**
* Length of buffer has changed.
* \param duration Duration in microseconds
*/
void bufferDurationChanged(qint64 duration);
/**
* Amount of data in buffer has changed.
* \param duration Duration of data in microseconds
*/
void dataDurationChanged(qint64 duration);
/**
* Position of the audio input device has changed.
* \param position Position in microseconds
*/
void recordPositionChanged(qint64 position);
/**
* Position of the audio output device has changed.
* \param position Position in microseconds
*/
void playPositionChanged(qint64 position);
/**
* Level changed
* \param rmsLevel RMS level in range 0.0 - 1.0
* \param peakLevel Peak level in range 0.0 - 1.0
* \param numSamples Number of audio samples analyzed
*/
void levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples);
/**
* Spectrum has changed.
* \param position Position of start of window in microseconds
* \param length Length of window in microseconds
* \param spectrum Resulting frequency spectrum
*/
void spectrumChanged(qint64 position, qint64 length, const FrequencySpectrum &spectrum);
private slots:
void audioNotify();
void audioStateChanged(QAudio::State state);
void audioDataReady();
void spectrumChanged(const FrequencySpectrum &spectrum);
private:
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;
QFile* m_file;
WavFile m_wavFile;
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;
qint64 m_playPosition;
QBuffer m_audioOutputIODevice;
QByteArray m_buffer;
qint64 m_dataLength;
qreal m_rmsLevel;
qreal m_peakLevel;
int m_spectrumLengthBytes;
QByteArray m_spectrumBuffer;
SpectrumAnalyser m_spectrumAnalyser;
qint64 m_spectrumPosition;
int m_count;
#ifdef DUMP_DATA
QDir m_outputDir;
#endif
};
#endif // ENGINE_H
|