Repository navigation
Expand file tree
/
Copy pathpulsethread.cpp
More file actions
98 lines (81 loc) · 2.58 KB
/
Copy pathpulsethread.cpp
File metadata and controls
98 lines (81 loc) · 2.58 KB
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
#include "pulsethread.h"
#include <QCoreApplication>
#include <QDebug>
#define RAW_BUFFERSIZE 128
PulseThread::PulseThread(const QString &_pulseDevice,
unsigned int _sampleRate,
unsigned int _bufferSize) {
pulseDevice = _pulseDevice;
sampleRate = _sampleRate;
bufferSize = _bufferSize;
bufferLeft = new float[bufferSize];
bufferRight = new float[bufferSize];
copyBufferLeft = new float[bufferSize];
copyBufferRight = new float[bufferSize];
stopped = true;
paSampleSpec.format = PA_SAMPLE_FLOAT32;
paSampleSpec.rate = sampleRate;
paSampleSpec.channels = 2;
bufferIndex = 0;
}
PulseThread::~PulseThread() {
delete [] bufferLeft;
delete [] bufferRight;
delete [] copyBufferLeft;
delete [] copyBufferRight;
bufferLeft = 0;
bufferRight = 0;
copyBufferLeft = 0;
copyBufferRight = 0;
}
void
PulseThread::run() {
stopped = false;
int error;
float rawBuffer[RAW_BUFFERSIZE];
paSimple = pa_simple_new(NULL,
"alsaspecview",
PA_STREAM_RECORD,
qPrintable(pulseDevice),
"record",
&paSampleSpec,
NULL,
NULL,
&error);
if (!paSimple) {
qErrnoWarning(error, pa_strerror(error));
QCoreApplication::quit();
}
for (;;) {
int numRead, error;
numRead = pa_simple_read(paSimple, rawBuffer, RAW_BUFFERSIZE, &error);
if (numRead < 0) {
qErrnoWarning(error, pa_strerror(error));
QCoreApplication::quit();
}
if (numRead % 2 != 0) {
qWarning("Non-even number of samples read!");
} else {
for (int indRead = 0; indRead < RAW_BUFFERSIZE / 8; indRead++) {
bufferLeft[bufferIndex] = rawBuffer[indRead*2];
bufferRight[bufferIndex] = rawBuffer[indRead*2 + 1];
bufferIndex++;
if (bufferIndex == bufferSize) {
for (int unsigned ind = 0; ind < bufferSize; ind++) {
copyBufferLeft[ind] = bufferLeft[ind];
copyBufferRight[ind] = bufferRight[ind];
}
emit bufferFilled(copyBufferLeft, bufferSize);
bufferIndex = 0;
}
}
}
}
}
void
PulseThread::stop() {
if (paSimple) {
pa_simple_free(paSimple);
}
stopped = true;
}