-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
221 lines (170 loc) · 6.01 KB
/
mainwindow.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
211
212
213
214
215
216
217
218
219
220
221
#include<iostream>
#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
for (int i = 0; i < 10000; ++i) {
displayList.push_back(0);
displayListIdx.push_back((i / sampleRate) * 1000);
}
arrangePlots();
arrangeFFTParams();
connect(this, SIGNAL(fftFilled()), this, SLOT(updateFFTPlot()));
connect(this, SIGNAL(stftFilled()), this, SLOT(updateSTFTPlot()));
QAudioProbe *probe;
QAudioRecorder * audioRecorder = new QAudioRecorder(this);
probe = new QAudioProbe;
connect(probe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(processBuffer(QAudioBuffer)));
probe->setSource(audioRecorder);
audioRecorder->setOutputLocation(QUrl::fromLocalFile("tmp"));
audioRecorder->record();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::arrangePlots()
{
ui->powerPlot->enableAxis(0, true);
ui->powerPlot->enableAxis(2, true);
ui->powerPlot->setAxisScale(ui->powerPlot->yLeft, -120, 0);
ui->powerPlot->setAxisScale(ui->powerPlot->xBottom, 0, 100);
ui->powerPlot->setTitle("Power (dB)");
ui->powerPlot->setAxisTitle(ui->powerPlot->xBottom, "Time (ms)");
curvePower->setPen( Qt::red, 1 ),
curvePower->setRenderHint( QwtPlotItem::RenderAntialiased, true );
ui->fftPlot->enableAxis(0, true);
ui->fftPlot->enableAxis(2, true);
ui->fftPlot->setAxisScale(ui->fftPlot->yLeft, 0, 1);
ui->fftPlot->setAxisScale(ui->fftPlot->xBottom, 0, sampleRate / 2);
ui->fftPlot->setTitle("FFT Magnitude");
ui->fftPlot->setAxisTitle(ui->fftPlot->xBottom, "Frequency (Hz)");
curveFFT->setPen( Qt::red, 1 ),
curveFFT->setRenderHint( QwtPlotItem::RenderAntialiased, true );
spec = new QwtPlotSpectrogram();
ui->spectrogram->enableAxis(0, true);
ui->spectrogram->enableAxis(2, true);
ui->spectrogram->setAxisScale(ui->spectrogram->xBottom, 0, 1000 * (16 * fftLen) / sampleRate);
ui->spectrogram->setTitle("STFT");
ui->spectrogram->setAxisTitle(ui->spectrogram->yLeft, "Frequency (Hz)");
ui->spectrogram->setAxisTitle(ui->spectrogram->xBottom, "Time (ms)");
colorMap = new QwtLinearColorMap(Qt::darkCyan, Qt::red);
colorMap->addColorStop(0.1, Qt::cyan);
colorMap->addColorStop(0.6, Qt::green);
colorMap->addColorStop(0.95, Qt::yellow);
spec->setColorMap(colorMap);
dataSpec->setInterval(Qt::XAxis, QwtInterval( 0, 1000 * (16 * fftLen) / sampleRate));
dataSpec->setInterval(Qt::YAxis, QwtInterval( 0, (sampleRate / 2) ));
dataSpec->setInterval(Qt::ZAxis, QwtInterval( 0, 2 ));
}
void MainWindow::on_optionsButton_clicked()
{
OptionsMenu optionsMenu;
optionsMenu.setModal(true);
optionsMenu.exec();
fftLen = optionsMenu.getFftLen();
windowType = optionsMenu.getWindowType();
arrangeFFTParams();
}
void MainWindow::convertDB(float &data)
{
data = 20 * log10(data * data);
}
void MainWindow::processBuffer(const QAudioBuffer& buffer)
{
const float *soundData = buffer.constData<float>();
float data;
for (int i = 0; i < buffer.frameCount(); i++){
data = soundData[i];
fftList.push_back(data);
fftList.pop_front();
fftCount++;
if (fftCount == fftLen){
emit fftFilled();
fftCount = 0;
}
stftList.push_back(data);
stftCount++;
if (stftCount == 8 * fftLen){
emit stftFilled();
stftCount = 0;
}
convertDB(data);
displayList.push_back(data);
displayList.pop_front();
}
dataPower = new QwtPointArrayData(displayListIdx, displayList);
curvePower->setSamples(dataPower);
curvePower->attach(ui->powerPlot);
ui->powerPlot->replot();
ui->powerPlot->show();
}
void MainWindow::arrangeFFTParams(){
fftList.clear();
fftListIdx.clear();
fftMag.clear();
stftMag.clear();
for (int i = 0; i < fftLen; ++i) {
fftList.push_back(0);
if (i < fftLen / 2) fftListIdx.push_back((i * sampleRate) / fftLen);
fftMag.push_back(0);
}
for (int i = 0; i < (16 * fftLen); i++)
stftMag.push_back(0);
dataSpec->setValueMatrix(stftMag, 32);
}
void MainWindow::updateFFTPlot(){
fftw_plan p;
fftw_complex *out;
double res;
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * fftLen);
p = fftw_plan_dft_r2c_1d(fftLen, fftList.data(), out, FFTW_ESTIMATE);
fftw_execute(p);
for (int i = 0; i < fftLen / 2; i++){
res = sqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]);
fftMag.push_back(res);
fftMag.pop_front();
}
dataFFT = new QwtPointArrayData(fftListIdx, fftMag);
curveFFT->setSamples(dataFFT);
curveFFT->attach(ui->fftPlot);
ui->fftPlot->replot();
ui->fftPlot->show();
fftw_destroy_plan(p);
}
void MainWindow::updateSTFTPlot(){
fftw_plan p;
fftw_complex *out;
double res;
int count = 0;
double stftBlock[fftLen];
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * fftLen);
for (int j = 0; j < stftList.length(); j = j + (fftLen / 2 )){
for (int k = 0; k < fftLen; k++){
stftBlock[k] = stftList[k + j];
if (windowType == "Hann"){
stftBlock[k] = stftBlock[k] * 0.5 * (1 - cos(2 * M_PI * k / fftLen));
}
}
p = fftw_plan_dft_r2c_1d(fftLen, stftBlock, out, FFTW_ESTIMATE);
fftw_execute(p);
for (int i = 0; i < fftLen / 2; i++){
res = sqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]);
dataSpec->setValue(i, (count / (fftLen / 2)), res);
count++;
if (count >= (32 * fftLen)){
spec->setData(dataSpec);
spec->attach(ui->spectrogram);
ui->spectrogram->replot();
ui->spectrogram->show();
count = 0;
stftList.clear();
}
}
}
fftw_destroy_plan(p);
}