Debian Codegraph @

src/CaptureThreadImplPortAudio.cpp

raw
1 // Copyright 2004 "Gilles Degottex"
2
3 // This file is part of "Music"
4
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // "Music" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
20 #include "CaptureThread.h"
21
22 #include <cassert>
23 #include <time.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <iostream>
28 #include <vector>
29 using namespace std;
30
31 // ------------------------------ PortAudio implementation ----------------------------
32 #ifdef CAPTURE_PORTAUDIO
33 CaptureThreadImplPortAudio::CaptureThreadImplPortAudio(CaptureThread* capture_thread)
34 : CaptureThreadImpl(capture_thread, "PortAudio", QString("Portable cross-platform Audio API (lib:")+Pa_GetVersionText()+"["+QString::number(Pa_GetVersion())+"])")
35 {
36 m_stream = NULL;
37
38 m_source = "default";
39 }
40
41 bool CaptureThreadImplPortAudio::is_available()
42 {
43 if(!m_stream)
44 {
45 try
46 {
47 m_err = Pa_Initialize();
48 if(m_err != paNoError) throw QString("PortAudio: is_available:Pa_Initialize ")+Pa_GetErrorText(m_err);
49
50 int numDevices;
51
52 numDevices = Pa_GetDeviceCount();
53 if(numDevices < 0)
54 throw QString("PortAudio: is_available:Pa_GetDeviceCount ")+Pa_GetErrorText(numDevices);
55 else if(numDevices == 0)
56 throw QString("PortAudio: is_available:Pa_GetDeviceCount no devices available")+Pa_GetErrorText(numDevices);
57
58 /* const PaDeviceInfo *deviceInfo;
59
60 for(int i=0; i<numDevices; i++ )
61 {
62 deviceInfo = Pa_GetDeviceInfo( i );
63 cout << deviceInfo->name << endl;
64 cout << deviceInfo->defaultSampleRate << endl;
65 }*/
66 }
67 catch(QString error)
68 {
69 Pa_Terminate();
70 m_stream = NULL;
71 m_status = "N/A";
72 return false;
73 }
74 capture_finished();
75 }
76
77 m_status = "OK";
78
79 return true;
80 }
81
82 void CaptureThreadImplPortAudio::setSamplingRate(int value)
83 {
84 // cout << "CaptureThreadImplPortAudio::setSamplingRate " << value << endl;
85
86 assert(value>0 || value==CaptureThread::SAMPLING_RATE_MAX);
87
88 if(m_sampling_rate!=value || value==CaptureThread::SAMPLING_RATE_MAX)
89 {
90 bool was_running = m_capture_thread->isCapturing();
91 if(was_running) m_capture_thread->stopCapture();
92
93 m_sampling_rate = value;
94
95 if(m_sampling_rate==CaptureThread::SAMPLING_RATE_MAX)
96 {
97 try
98 {
99 set_params(true);
100 }
101 catch(QString error)
102 {
103 cout << "CaptureThread: ERROR: " << error.toStdString() << endl;
104 m_capture_thread->emitError(error);
105 }
106
107 try{
108 // it was just for testing
109 capture_finished();
110 }
111 catch(QString error)
112 {
113 cout << "CaptureThread: ERROR: " << error.toStdString() << endl;
114 m_capture_thread->emitError(error);
115 }
116 }
117 else
118 m_capture_thread->emitSamplingRateChanged();
119
120 if(was_running) m_capture_thread->startCapture();
121 }
122
123 // cout << "~CaptureThreadImplPortAudio::setSamplingRate" << endl;
124 }
125
126 int CaptureThreadImplPortAudio::PortAudioCallback( const void *inputBuffer, void *outputBuffer,
127 unsigned long framesPerBuffer,
128 const PaStreamCallbackTimeInfo* timeInfo,
129 PaStreamCallbackFlags statusFlags,
130 void *userData )
131 {
132 (void)outputBuffer;
133 return ((CaptureThreadImplPortAudio*)userData)->portAudioCallback(inputBuffer, framesPerBuffer, timeInfo, statusFlags);
134 }
135 int CaptureThreadImplPortAudio::portAudioCallback(const void *inputBuffer,
136 unsigned long framesPerBuffer,
137 const PaStreamCallbackTimeInfo* timeInfo,
138 PaStreamCallbackFlags statusFlags)
139 {
140 (void)timeInfo;
141 (void)statusFlags;
142 if(m_capture_thread->m_pause)
143 return 0;
144
145 m_capture_thread->m_lock.lock();
146
147 float *in = (float*)inputBuffer;
148
149 for(unsigned long i=0; i<framesPerBuffer; i++)
150 m_capture_thread->m_values.push_front(*in++);
151 // addValue(*in++, i, m_channel_count); // TODO
152
153 m_capture_thread->m_packet_size = framesPerBuffer;
154 if(m_capture_thread->m_ext_lock)
155 {
156 m_capture_thread->m_packet_size_sll = 0;
157 m_capture_thread->m_ext_lock = false;
158 }
159 m_capture_thread->m_packet_size_sll += framesPerBuffer;
160
161 m_capture_thread->m_lock.unlock();
162
163 return 0;
164 }
165
166 void CaptureThreadImplPortAudio::set_params(bool test)
167 {
168 m_err = Pa_Initialize();
169 if(m_err != paNoError) throw QString("PortAudio: set_params:Pa_Initialize ")+Pa_GetErrorText(m_err);
170
171 PaStreamParameters params;
172 params.device = paNoDevice;
173 params.channelCount = 1;
174 params.sampleFormat = paFloat32;
175 params.suggestedLatency = 0;
176 params.hostApiSpecificStreamInfo = NULL;
177
178 if(getASCIISource()!="default")
179 {
180 int numDevices = Pa_GetDeviceCount();
181 const PaDeviceInfo* deviceInfo;
182 for(int i=0; params.device==paNoDevice && i<numDevices; i++)
183 {
184 deviceInfo = Pa_GetDeviceInfo(i);
185 if(QString(deviceInfo->name)==getASCIISource())
186 params.device = i;
187 }
188
189 if(params.device==paNoDevice)
190 cout << "CaptureThread: INFO: PortAudio: cannot determine selected source \"" << getASCIISource().toStdString() << "\"" << endl;
191 }
192
193 if(!test)
194 {
195 if(params.device==paNoDevice)
196 cout << "CaptureThread: INFO: PortAudio: using default device" << endl;
197 else
198 cout << "CaptureThread: INFO: PortAudio: using \"" << getASCIISource().toStdString() << "\"" << endl;
199
200 setFormatDescrsAndFns(4, true, true, 1); // TODO do something if nbchannel=1 not supported
201 }
202
203 if(m_sampling_rate==CaptureThread::SAMPLING_RATE_MAX || m_sampling_rate==CaptureThread::SAMPLING_RATE_UNKNOWN)
204 {
205 int old_sampling_rate = m_sampling_rate;
206
207 cout << "CaptureThread: INFO: PortAudio: sampling rate set to max or undefined, try to determinate it." << endl;
208
209 list<int> sampling_rates;
210 sampling_rates.push_front(8000); sampling_rates.push_front(11025); sampling_rates.push_front(16000);
211 sampling_rates.push_front(22050); sampling_rates.push_front(24000); sampling_rates.push_front(32000);
212 sampling_rates.push_front(44100);
213
214 m_err = -1;
215 while(m_err!=paNoError)
216 {
217 if(sampling_rates.empty())
218 throw QString("PortAudio: cannot set any sample rate (")+Pa_GetErrorText(m_err)+")";
219
220 m_err = Pa_Initialize();
221 if(m_err != paNoError) throw QString("PortAudio: set_params:Pa_Initialize ")+Pa_GetErrorText(m_err);
222
223 m_sampling_rate = sampling_rates.front();
224 cout << "CaptureThread: INFO: PortAudio: try sampling rate " << m_sampling_rate << " ..." << flush;
225
226 // cout << "nbc1 " << params.channelCount << endl;
227
228 if(params.device==paNoDevice)
229 m_err = Pa_OpenDefaultStream(&m_stream, 1, 0, paFloat32, m_sampling_rate, 0, PortAudioCallback, this);
230 else
231 m_err = Pa_OpenStream(&m_stream, &params, NULL, m_sampling_rate, 0, paNoFlag, PortAudioCallback, this);
232
233 if(m_err != paNoError) cout << " failed" << endl;
234 else cout << " success" << endl;
235
236 sampling_rates.pop_front();
237 }
238
239 if(old_sampling_rate!=m_sampling_rate)
240 m_capture_thread->emitSamplingRateChanged();
241 }
242 else
243 {
244 // cout << "nbc2 " << params.channelCount << endl;
245 // cout << "dev2 " << params.device << "/" << paNoDevice << endl;
246
247 if(params.device==paNoDevice)
248 {
249 m_err = Pa_OpenDefaultStream(&m_stream, 1, 0, paFloat32, m_sampling_rate, 0, PortAudioCallback, this);
250 if(m_err != paNoError)
251 throw QString("PortAudio: set_params:Pa_OpenDefaultStream ")+Pa_GetErrorText(m_err);
252 }
253 else
254 {
255 m_err = Pa_OpenStream(&m_stream, &params, NULL, m_sampling_rate, 0, paNoFlag, PortAudioCallback, this);
256 if(m_err != paNoError)
257 throw QString("PortAudio: set_params:Pa_OpenStream ")+Pa_GetErrorText(m_err);
258 }
259 }
260 }
261
262 void CaptureThreadImplPortAudio::capture_init()
263 {
264 set_params(false);
265
266 m_err = Pa_StartStream(m_stream);
267 if(m_err != paNoError)
268 throw QString("PortAudio: capture_init:Pa_StartStream ")+Pa_GetErrorText(m_err);
269
270 m_capture_thread->m_capturing = true;
271 m_capture_thread->emitCaptureStarted();
272 m_capture_thread->emitCaptureToggled(true);
273 }
274 void CaptureThreadImplPortAudio::capture_finished()
275 {
276 if(m_stream)
277 {
278 if(!Pa_IsStreamStopped(m_stream))
279 {
280 m_err = Pa_StopStream(m_stream);
281 if(m_err != paNoError) throw QString("PortAudio: capture_finished: ")+Pa_GetErrorText(m_err);
282 }
283
284 if(m_stream)
285 {
286 m_err = Pa_CloseStream(m_stream);
287 if(m_err != paNoError) throw QString("PortAudio: capture_finished: ")+Pa_GetErrorText(m_err);
288 }
289
290 m_stream = NULL;
291
292 m_capture_thread->m_capturing = false;
293 m_capture_thread->emitCaptureStoped();
294 m_capture_thread->emitCaptureToggled(false);
295 }
296
297 m_err = Pa_Terminate();
298 // if(m_err != paNoError) throw QString("PortAudio: capture_finished: ")+Pa_GetErrorText(m_err);
299 }
300 void CaptureThreadImplPortAudio::startCapture()
301 {
302 try
303 {
304 capture_init();
305 }
306 catch(QString error)
307 {
308 capture_finished();
309 cout << "CaptureThread: ERROR: " << error.toStdString() << endl;
310 m_capture_thread->emitError(error);
311 }
312 }
313 void CaptureThreadImplPortAudio::stopCapture()
314 {
315 try
316 {
317 capture_finished();
318 }
319 catch(QString error)
320 {
321 cout << "CaptureThread: ERROR: " << error.toStdString() << endl;
322 m_capture_thread->emitError(error);
323 }
324 }
325 CaptureThreadImplPortAudio::~CaptureThreadImplPortAudio()
326 {
327 stopCapture();
328 }
329
330 #endif