-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiomonitor.cpp
210 lines (173 loc) · 5.99 KB
/
audiomonitor.cpp
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
/*
babyphone - A baby monitor application for Maemo / MeeGo (Nokia N900, N950, N9).
Copyright (C) 2011 Roman Morawek <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "audiomonitor.h"
#include <stdlib.h>
#include <cmath>
#include <QDebug>
#include <QAudioDeviceInfo>
#include <QAudioInput>
#include <QTimer>
/*!
The constructor initializes the audio device and negotiates the audio stream
format. The audio sampling is started but immediately suspended. The
application has to resume it to receive audio data.
*/
AudioMonitor::AudioMonitor(const Settings *settings, QObject *parent)
:QIODevice(parent), itsSettings(settings)
{
// open IODevice
open(QIODevice::WriteOnly);
// determine suitable audio format
QAudioFormat itsAudioFormat;
// this is what we want
itsAudioFormat.setFrequency(8000);
itsAudioFormat.setChannels(1);
itsAudioFormat.setSampleSize(16);
itsAudioFormat.setSampleType(QAudioFormat::SignedInt);
itsAudioFormat.setByteOrder(QAudioFormat::LittleEndian);
itsAudioFormat.setCodec("audio/pcm");
// this is what we get
QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
if (!info.isFormatSupported(itsAudioFormat)) {
itsAudioFormat = info.nearestFormat(itsAudioFormat);
qWarning() << "Could not get desired audio format. Nearest available format has"
<< "frequency" << itsAudioFormat.frequency()
<< "sample size" << itsAudioFormat.sampleSize();
}
if (itsAudioFormat.sampleSize() != 16) {
qCritical() << "Audio device doesn't support needed format, exiting.";
exit(1);
return;
}
// create device
itsDevice = new QAudioInput(itsAudioFormat, this);
itsDevice->setNotifyInterval(itsSettings->AUDIO_SAMPLE_INTERVAL);
itsActive = false;
// reset counter
itsCounter = 0;
}
/*!
The destructor closes the audio device.
*/
AudioMonitor::~AudioMonitor()
{
// stop monitoring, if applicable
if (itsActive)
stop();
// close IODevice
close();
}
/*!
starts audio sampling and sets its state accordingly.
Returns true in case of success, otherwise false.
*/
bool AudioMonitor::start()
{
// start capturing
// check whether we are already active before
if (!itsActive) {
itsDevice->start(this);
// check for success
if (itsDevice->error() == false) {
itsActive = true;
}
else
return false;
}
else
qWarning() << "Tried to start audio, allthough already running.";
return true;
}
/*!
stops audio sampling and sets its state accordingly.
*/
void AudioMonitor::stop()
{
// stop capturing
itsActive = false;
itsDevice->stop();
}
/*!
readDate needs to be implemented from the abstract parent's class but has no
functionality since we have an unidirectional audio stream.
*/
qint64 AudioMonitor::readData(char *data, qint64 maxlen)
{
Q_UNUSED(data);
Q_UNUSED(maxlen);
return 0;
}
/*!
writeData implements the central audio analysis functionality. It processes
the audio queue in quantities of AUDIO_SAMPLE_SUBINTERVAL. For each such
buffer it determines the maximum audio sample value and adds this to a
cummulated energy variable. After processing the whole data buffer this energy
variable is rescaled and represents the amplitude value, which is signalled
to the outside then.
writeData also compares this audio amplitude with the given threshold of the
application settings and handles the time based audio counter based on this.
If the volume is above the threshold the configurable increment is added to
the counter, otherwise the counter is decremented. The counter is reported
together with the volume in the signal.
*/
qint64 AudioMonitor::writeData(const char *data, qint64 len)
{
quint32 curEnergy = 0;
qint16 max = 0;
qint16 samples = len/2; // change data type to keep efficient
// sample format is S16LE, only!
const qint16 *buffer = (qint16*)data;
// derive energy
for (int i = 0; i < samples; ++i) {
// store maximum
if (*buffer > max)
max = *buffer;
// subinterval expired
if ((i % itsSettings->AUDIO_SAMPLE_SUBINTERVAL) == 0) {
// add current maximum and reset it
curEnergy += max;
//qDebug() << "add" << max << "new value:" << curEnergy;
max = 0;
}
// process next sample
buffer++;
}
// scale volume
int volume = itsSettings->itsAudioAmplify *
log(curEnergy*itsSettings->AUDIO_SAMPLE_SUBINTERVAL/(float)samples);
// inhibit negative values from logarithm
if (volume < 0)
volume = 0;
// update timer counter
if (volume > itsSettings->THRESHOLD_VALUE) {
// increment counter
itsCounter += itsSettings->itsDurationInfluence;
// check for overflow
if (itsCounter / COUNTER_SCALE_FACTOR > itsSettings->VOLUME_COUNTER_MAX) {
// overflow, clip it
itsCounter = itsSettings->VOLUME_COUNTER_MAX * COUNTER_SCALE_FACTOR;
}
}
else {
// decrement counter
itsCounter -= itsSettings->VOLUME_COUNTER_DEC;
// check for underflow
if (itsCounter < 0)
itsCounter = 0;
}
// signal the resulting values
emit update(itsCounter / COUNTER_SCALE_FACTOR, volume);
return len;
}