-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportaudiorec.cpp
executable file
·172 lines (137 loc) · 4.15 KB
/
portaudiorec.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
#include "portaudiorec.h"
#include <QTextStream>
#include "PointerRingBuffer.h"
PortaudioRec::PortaudioRec(PointerRingBuffer *rb)
{
this->rb = rb;
PaError err = Pa_Initialize();
if( err != paNoError )
{
this->error = err;
}
}
PortaudioRec::~PortaudioRec()
{
if (Pa_IsStreamActive(this->stream))
{
Pa_StopStream(this->stream);
}
/*PaError err = */ Pa_Terminate();
}
QStringList PortaudioRec::getHostApiList()
{
QStringList hostInfoList;
for (int i = 0; i < Pa_GetHostApiCount(); ++i)
hostInfoList << QString(Pa_GetHostApiInfo(i)->name);
return hostInfoList;
}
int PortaudioRec::getInputDeviceIndex(QString name)
{
QStringList hostInfoList = getHostApiList();
for (int i = 0; i < Pa_GetDeviceCount(); ++i)
{
const PaDeviceInfo* deviceInfo = Pa_GetDeviceInfo(i);
if ( deviceInfo->maxInputChannels > 1 && QString(deviceInfo->name) + "[" + hostInfoList.value(deviceInfo->hostApi) + "]" == name )
{
return i;
}
}
return -1;
}
QStringList PortaudioRec::getInputDeviceList()
{
QStringList ret;
QStringList hostInfoList = getHostApiList();
for (int i = 0; i < Pa_GetDeviceCount(); ++i)
{
const PaDeviceInfo* deviceInfo = Pa_GetDeviceInfo(i);
if (deviceInfo->maxInputChannels > 1)
{
ret << QString(deviceInfo->name) + "[" + hostInfoList.value(deviceInfo->hostApi) + "]";
}
}
return ret;
}
int PortaudioRec::getStandardInputDeviceIndex()
{
return Pa_GetDefaultInputDevice();
}
QString PortaudioRec::getDeviceName(int index)
{
const PaDeviceInfo* deviceInfo = Pa_GetDeviceInfo(index);
return QString(deviceInfo->name) + "[" + QString(Pa_GetHostApiInfo(deviceInfo->hostApi)->name) + "]";
}
int PortaudioRec::myMemberCallback(const void *input, void *, unsigned long frameCount, const PaStreamCallbackTimeInfo *, PaStreamCallbackFlags)
{
if (this->rb != NULL)
this->rb->put((char*)input, frameCount * 8);
//this->rb->Put((samplePtr)input, int24Sample, frameCount * 2);
// Do what you need (having access to every part of MyPortaudioClass)
return paContinue;
}
void PortaudioRec::printDeviceList()
{
QStringList hostApiList = getHostApiList();
QTextStream out(stdout);
for (int i = 0; i < Pa_GetDeviceCount(); ++i)
{
const PaDeviceInfo* deviceInfo = Pa_GetDeviceInfo(i);
if (deviceInfo->maxInputChannels > 1)
{
out << i << " = " << QString(deviceInfo->name) << ":" << hostApiList.at(deviceInfo->hostApi) << ":" << deviceInfo->maxInputChannels << "\n";
}
}
}
void PortaudioRec::run()
{
PaError err;
const PaDeviceInfo* info = Pa_GetDeviceInfo(this->deviceIndex);
/* Open an audio I/O stream. */
PaStreamParameters parameters;
parameters.device = this->deviceIndex;
parameters.channelCount = 2;
parameters.sampleFormat = paInt32;
parameters.hostApiSpecificStreamInfo = NULL;
parameters.suggestedLatency = info->defaultHighInputLatency;
err = Pa_OpenStream( &this->stream, ¶meters, NULL, SAMPLE_RATE, paFramesPerBufferUnspecified, paNoFlag, NULL, this);
if (err != paNoError)
{
this->lastError = QString(Pa_GetErrorText(err));
return;
}
err = Pa_StartStream(stream);
if (err != paNoError)
{
this->lastError = QString(Pa_GetErrorText(err));
return;
}
int buffer[4096];
while (this != NULL && this->rb != NULL)
{
long len = Pa_GetStreamReadAvailable(stream);
if (len < 0)
{
this->lastError = QString(Pa_GetErrorText(len));
}
if (len > 512)
{
len = 512;
}
err = Pa_ReadStream(this->stream, &buffer[0], len);
if (err != paNoError)
{
this->lastError = QString(Pa_GetErrorText(err));
}
this->rb->put((char*)&buffer[0], len * 8);
}
}
int PortaudioRec::startRec(int deviceIndex)
{
if (deviceIndex < 0 || deviceIndex >= Pa_GetDeviceCount())
{
return 0;
}
this->deviceIndex = deviceIndex;
this->start(QThread::TimeCriticalPriority);
return -1;
}