-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLsPaConnection.cpp
executable file
·156 lines (135 loc) · 4.06 KB
/
LsPaConnection.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
#include "LsPaConnection.h"
#include <portaudio.h>
#include <pa_ringbuffer.h>
#include <QStringList>
#include <QTextStream>
LsPaConnection::LsPaConnection(PaUtilRingBuffer *buffer, QObject *parent) :
QThread(parent)
{
this->rb = buffer;
this->deviceIndex = -1;
}
LsPaConnection::~LsPaConnection()
{
Pa_Terminate();
}
void LsPaConnection::printDeviceList()
{
if (Pa_Initialize() == pa_
int count = Pa_GetDeviceCount();
QTextStream out(stdout);
for (int i = 0; i < count; ++i)
{
const PaDeviceInfo* devInfo = Pa_GetDeviceInfo(deviceIndex);
if (devInfo != NULL && devInfo->maxInputChannels > 1)
{
const PaHostApiInfo* apiInfo = Pa_GetHostApiInfo(devInfo->hostApi);
if (apiInfo != NULL)
{
out << QString(devInfo->name) + "[" + QString(apiInfo->name) + "]\n";
}
}
}
}
void LsPaConnection::startRec(int deviceIndex)
{
this->deviceIndex = deviceIndex;
QThread::start(QThread::TimeCriticalPriority);
}
QString LsPaConnection::getDeviceName(int deviceIndex)
{
const PaDeviceInfo* devInfo = Pa_GetDeviceInfo(deviceIndex);
if (devInfo == NULL)
{
return "";
}
const PaHostApiInfo* apiInfo = Pa_GetHostApiInfo(devInfo->hostApi);
if (apiInfo == NULL)
{
return "";
}
QString ret = QString(devInfo->name) + "[" + QString(apiInfo->name) + "]";
return ret;
}
int LsPaConnection::getInputDeviceIndex(QString name)
{
int count = Pa_GetDeviceCount();
for (int i = 0; i < count; ++i)
{
if (this->getDeviceName(i) == name)
{
return i;
}
}
return -1;
}
QStringList LsPaConnection::getInputDeviceList()
{
int count = Pa_GetDeviceCount();
QStringList ret;
for (int i = 0; i < count; ++i)
{
const PaDeviceInfo* devInfo = Pa_GetDeviceInfo(deviceIndex);
if (devInfo != NULL && devInfo->maxInputChannels > 1)
{
const PaHostApiInfo* apiInfo = Pa_GetHostApiInfo(devInfo->hostApi);
if (apiInfo != NULL)
{
ret << QString(devInfo->name) + "[" + QString(apiInfo->name) + "]";
}
}
}
return ret;
}
void LsPaConnection::run()
{
PaStreamParameters inputParameters;
PaStream *stream;
PaError err;
err = Pa_Initialize();
if( err != paNoError ) return;
if (this->deviceIndex == -1)
{
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
}
else
{
inputParameters.device = this->deviceIndex;
}
if (inputParameters.device == paNoDevice)
{
return;
}
inputParameters.channelCount = 2;
inputParameters.sampleFormat = paInt32;
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
/* Record some audio. -------------------------------------------- */
err = Pa_OpenStream(
&stream,
&inputParameters,
NULL, /* &outputParameters, */
44100,
1024,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
NULL, /* no callback, use blocking API */
NULL ); /* no callback, so no callback userData */
if( err != paNoError ) this->rb = NULL;
err = Pa_StartStream( stream );
if( err != paNoError ) this->rb = NULL;
while (this != NULL && this->rb != NULL)
{
long len = Pa_GetStreamReadAvailable( stream );
if (len > this->rb->bufferSize )
{
len = this->rb->bufferSize;
}
if (this->rb != NULL)
{
err = Pa_ReadStream( stream, this->rb->buffer, len );
if( err != paNoError ) this->rb = NULL;
}
}
err = Pa_CloseStream( stream );
Pa_Terminate();
}