|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 // |
|
43 // W A R N I N G |
|
44 // ------------- |
|
45 // |
|
46 // This file is not part of the Qt API. It exists for the convenience |
|
47 // of other Qt classes. This header file may change from version to |
|
48 // version without notice, or even be removed. |
|
49 // |
|
50 // We mean it. |
|
51 // |
|
52 |
|
53 #include <QtCore/qcoreapplication.h> |
|
54 #include "qaudioinput_alsa_p.h" |
|
55 #include "qaudiodeviceinfo_alsa_p.h" |
|
56 |
|
57 QT_BEGIN_NAMESPACE |
|
58 |
|
59 //#define DEBUG_AUDIO 1 |
|
60 |
|
61 QAudioInputPrivate::QAudioInputPrivate(const QByteArray &device) |
|
62 { |
|
63 bytesAvailable = 0; |
|
64 handle = 0; |
|
65 ahandler = 0; |
|
66 access = SND_PCM_ACCESS_RW_INTERLEAVED; |
|
67 pcmformat = SND_PCM_FORMAT_S16; |
|
68 buffer_size = 0; |
|
69 period_size = 0; |
|
70 buffer_time = 100000; |
|
71 period_time = 20000; |
|
72 totalTimeValue = 0; |
|
73 intervalTime = 1000; |
|
74 audioBuffer = 0; |
|
75 errorState = QAudio::NoError; |
|
76 deviceState = QAudio::StoppedState; |
|
77 audioSource = 0; |
|
78 pullMode = true; |
|
79 resuming = false; |
|
80 |
|
81 m_device = device; |
|
82 |
|
83 timer = new QTimer(this); |
|
84 connect(timer,SIGNAL(timeout()),SLOT(userFeed())); |
|
85 } |
|
86 |
|
87 QAudioInputPrivate::~QAudioInputPrivate() |
|
88 { |
|
89 close(); |
|
90 disconnect(timer, SIGNAL(timeout())); |
|
91 QCoreApplication::processEvents(); |
|
92 delete timer; |
|
93 } |
|
94 |
|
95 QAudio::Error QAudioInputPrivate::error() const |
|
96 { |
|
97 return errorState; |
|
98 } |
|
99 |
|
100 QAudio::State QAudioInputPrivate::state() const |
|
101 { |
|
102 return deviceState; |
|
103 } |
|
104 |
|
105 void QAudioInputPrivate::setFormat(const QAudioFormat& fmt) |
|
106 { |
|
107 if (deviceState == QAudio::StoppedState) |
|
108 settings = fmt; |
|
109 } |
|
110 |
|
111 QAudioFormat QAudioInputPrivate::format() const |
|
112 { |
|
113 return settings; |
|
114 } |
|
115 |
|
116 int QAudioInputPrivate::xrun_recovery(int err) |
|
117 { |
|
118 int count = 0; |
|
119 bool reset = false; |
|
120 |
|
121 if(err == -EPIPE) { |
|
122 errorState = QAudio::UnderrunError; |
|
123 err = snd_pcm_prepare(handle); |
|
124 if(err < 0) |
|
125 reset = true; |
|
126 else { |
|
127 bytesAvailable = checkBytesReady(); |
|
128 if (bytesAvailable <= 0) |
|
129 reset = true; |
|
130 } |
|
131 |
|
132 } else if((err == -ESTRPIPE)||(err == -EIO)) { |
|
133 errorState = QAudio::IOError; |
|
134 while((err = snd_pcm_resume(handle)) == -EAGAIN){ |
|
135 usleep(100); |
|
136 count++; |
|
137 if(count > 5) { |
|
138 reset = true; |
|
139 break; |
|
140 } |
|
141 } |
|
142 if(err < 0) { |
|
143 err = snd_pcm_prepare(handle); |
|
144 if(err < 0) |
|
145 reset = true; |
|
146 } |
|
147 } |
|
148 if(reset) { |
|
149 close(); |
|
150 open(); |
|
151 snd_pcm_prepare(handle); |
|
152 return 0; |
|
153 } |
|
154 return err; |
|
155 } |
|
156 |
|
157 int QAudioInputPrivate::setFormat() |
|
158 { |
|
159 snd_pcm_format_t format = SND_PCM_FORMAT_S16; |
|
160 |
|
161 if(settings.sampleSize() == 8) { |
|
162 format = SND_PCM_FORMAT_U8; |
|
163 } else if(settings.sampleSize() == 16) { |
|
164 if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
165 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
166 format = SND_PCM_FORMAT_S16_LE; |
|
167 else |
|
168 format = SND_PCM_FORMAT_S16_BE; |
|
169 } else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
170 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
171 format = SND_PCM_FORMAT_U16_LE; |
|
172 else |
|
173 format = SND_PCM_FORMAT_U16_BE; |
|
174 } |
|
175 } else if(settings.sampleSize() == 24) { |
|
176 if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
177 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
178 format = SND_PCM_FORMAT_S24_LE; |
|
179 else |
|
180 format = SND_PCM_FORMAT_S24_BE; |
|
181 } else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
182 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
183 format = SND_PCM_FORMAT_U24_LE; |
|
184 else |
|
185 format = SND_PCM_FORMAT_U24_BE; |
|
186 } |
|
187 } else if(settings.sampleSize() == 32) { |
|
188 if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
189 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
190 format = SND_PCM_FORMAT_S32_LE; |
|
191 else |
|
192 format = SND_PCM_FORMAT_S32_BE; |
|
193 } else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
194 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
195 format = SND_PCM_FORMAT_U32_LE; |
|
196 else |
|
197 format = SND_PCM_FORMAT_U32_BE; |
|
198 } else if(settings.sampleType() == QAudioFormat::Float) { |
|
199 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
200 format = SND_PCM_FORMAT_FLOAT_LE; |
|
201 else |
|
202 format = SND_PCM_FORMAT_FLOAT_BE; |
|
203 } |
|
204 } else if(settings.sampleSize() == 64) { |
|
205 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
206 format = SND_PCM_FORMAT_FLOAT64_LE; |
|
207 else |
|
208 format = SND_PCM_FORMAT_FLOAT64_BE; |
|
209 } |
|
210 |
|
211 return snd_pcm_hw_params_set_format( handle, hwparams, format); |
|
212 } |
|
213 |
|
214 void QAudioInputPrivate::start(QIODevice* device) |
|
215 { |
|
216 if(deviceState != QAudio::StoppedState) |
|
217 close(); |
|
218 |
|
219 if(!pullMode && audioSource) |
|
220 delete audioSource; |
|
221 |
|
222 pullMode = true; |
|
223 audioSource = device; |
|
224 |
|
225 deviceState = QAudio::ActiveState; |
|
226 |
|
227 if( !open() ) |
|
228 return; |
|
229 |
|
230 emit stateChanged(deviceState); |
|
231 } |
|
232 |
|
233 QIODevice* QAudioInputPrivate::start() |
|
234 { |
|
235 if(deviceState != QAudio::StoppedState) |
|
236 close(); |
|
237 |
|
238 if(!pullMode && audioSource) |
|
239 delete audioSource; |
|
240 |
|
241 pullMode = false; |
|
242 audioSource = new InputPrivate(this); |
|
243 audioSource->open(QIODevice::ReadOnly | QIODevice::Unbuffered); |
|
244 |
|
245 deviceState = QAudio::IdleState; |
|
246 |
|
247 if( !open() ) |
|
248 return 0; |
|
249 |
|
250 emit stateChanged(deviceState); |
|
251 |
|
252 return audioSource; |
|
253 } |
|
254 |
|
255 void QAudioInputPrivate::stop() |
|
256 { |
|
257 if(deviceState == QAudio::StoppedState) |
|
258 return; |
|
259 |
|
260 deviceState = QAudio::StoppedState; |
|
261 |
|
262 close(); |
|
263 emit stateChanged(deviceState); |
|
264 } |
|
265 |
|
266 bool QAudioInputPrivate::open() |
|
267 { |
|
268 #ifdef DEBUG_AUDIO |
|
269 QTime now(QTime::currentTime()); |
|
270 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; |
|
271 #endif |
|
272 clockStamp.restart(); |
|
273 timeStamp.restart(); |
|
274 elapsedTimeOffset = 0; |
|
275 |
|
276 int dir; |
|
277 int err=-1; |
|
278 int count=0; |
|
279 unsigned int freakuency=settings.frequency(); |
|
280 |
|
281 QString dev = QString(QLatin1String(m_device.constData())); |
|
282 QList<QByteArray> devices = QAudioDeviceInfoInternal::availableDevices(QAudio::AudioInput); |
|
283 if(dev.compare(QLatin1String("default")) == 0) { |
|
284 #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
|
285 if (devices.size() > 0) |
|
286 dev = QLatin1String(devices.first()); |
|
287 else |
|
288 return false; |
|
289 #else |
|
290 dev = QLatin1String("hw:0,0"); |
|
291 #endif |
|
292 } else { |
|
293 #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
|
294 dev = QLatin1String(m_device); |
|
295 #else |
|
296 int idx = 0; |
|
297 char *name; |
|
298 |
|
299 QString shortName = QLatin1String(m_device.mid(m_device.indexOf('=',0)+1).constData()); |
|
300 |
|
301 while(snd_card_get_name(idx,&name) == 0) { |
|
302 if(qstrncmp(shortName.toLocal8Bit().constData(),name,shortName.length()) == 0) |
|
303 break; |
|
304 idx++; |
|
305 } |
|
306 dev = QString(QLatin1String("hw:%1,0")).arg(idx); |
|
307 #endif |
|
308 } |
|
309 |
|
310 // Step 1: try and open the device |
|
311 while((count < 5) && (err < 0)) { |
|
312 err=snd_pcm_open(&handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_CAPTURE,0); |
|
313 if(err < 0) |
|
314 count++; |
|
315 } |
|
316 if (( err < 0)||(handle == 0)) { |
|
317 errorState = QAudio::OpenError; |
|
318 deviceState = QAudio::StoppedState; |
|
319 emit stateChanged(deviceState); |
|
320 return false; |
|
321 } |
|
322 snd_pcm_nonblock( handle, 0 ); |
|
323 |
|
324 // Step 2: Set the desired HW parameters. |
|
325 snd_pcm_hw_params_alloca( &hwparams ); |
|
326 |
|
327 bool fatal = false; |
|
328 QString errMessage; |
|
329 unsigned int chunks = 8; |
|
330 |
|
331 err = snd_pcm_hw_params_any( handle, hwparams ); |
|
332 if ( err < 0 ) { |
|
333 fatal = true; |
|
334 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_any: err = %1").arg(err); |
|
335 } |
|
336 if ( !fatal ) { |
|
337 err = snd_pcm_hw_params_set_rate_resample( handle, hwparams, 1 ); |
|
338 if ( err < 0 ) { |
|
339 fatal = true; |
|
340 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_rate_resample: err = %1").arg(err); |
|
341 } |
|
342 } |
|
343 if ( !fatal ) { |
|
344 err = snd_pcm_hw_params_set_access( handle, hwparams, access ); |
|
345 if ( err < 0 ) { |
|
346 fatal = true; |
|
347 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_access: err = %1").arg(err); |
|
348 } |
|
349 } |
|
350 if ( !fatal ) { |
|
351 err = setFormat(); |
|
352 if ( err < 0 ) { |
|
353 fatal = true; |
|
354 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_format: err = %1").arg(err); |
|
355 } |
|
356 } |
|
357 if ( !fatal ) { |
|
358 err = snd_pcm_hw_params_set_channels( handle, hwparams, (unsigned int)settings.channels() ); |
|
359 if ( err < 0 ) { |
|
360 fatal = true; |
|
361 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_channels: err = %1").arg(err); |
|
362 } |
|
363 } |
|
364 if ( !fatal ) { |
|
365 err = snd_pcm_hw_params_set_rate_near( handle, hwparams, &freakuency, 0 ); |
|
366 if ( err < 0 ) { |
|
367 fatal = true; |
|
368 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_rate_near: err = %1").arg(err); |
|
369 } |
|
370 } |
|
371 if ( !fatal ) { |
|
372 err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, &dir); |
|
373 if ( err < 0 ) { |
|
374 fatal = true; |
|
375 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_buffer_time_near: err = %1").arg(err); |
|
376 } |
|
377 } |
|
378 if ( !fatal ) { |
|
379 err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir); |
|
380 if ( err < 0 ) { |
|
381 fatal = true; |
|
382 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_period_time_near: err = %1").arg(err); |
|
383 } |
|
384 } |
|
385 if ( !fatal ) { |
|
386 err = snd_pcm_hw_params_set_periods_near(handle, hwparams, &chunks, &dir); |
|
387 if ( err < 0 ) { |
|
388 fatal = true; |
|
389 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_periods_near: err = %1").arg(err); |
|
390 } |
|
391 } |
|
392 if ( !fatal ) { |
|
393 err = snd_pcm_hw_params(handle, hwparams); |
|
394 if ( err < 0 ) { |
|
395 fatal = true; |
|
396 errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params: err = %1").arg(err); |
|
397 } |
|
398 } |
|
399 if( err < 0) { |
|
400 qWarning()<<errMessage; |
|
401 errorState = QAudio::OpenError; |
|
402 deviceState = QAudio::StoppedState; |
|
403 emit stateChanged(deviceState); |
|
404 return false; |
|
405 } |
|
406 snd_pcm_hw_params_get_buffer_size(hwparams,&buffer_frames); |
|
407 buffer_size = snd_pcm_frames_to_bytes(handle,buffer_frames); |
|
408 snd_pcm_hw_params_get_period_size(hwparams,&period_frames, &dir); |
|
409 period_size = snd_pcm_frames_to_bytes(handle,period_frames); |
|
410 snd_pcm_hw_params_get_buffer_time(hwparams,&buffer_time, &dir); |
|
411 snd_pcm_hw_params_get_period_time(hwparams,&period_time, &dir); |
|
412 |
|
413 // Step 3: Set the desired SW parameters. |
|
414 snd_pcm_sw_params_t *swparams; |
|
415 snd_pcm_sw_params_alloca(&swparams); |
|
416 snd_pcm_sw_params_current(handle, swparams); |
|
417 snd_pcm_sw_params_set_start_threshold(handle,swparams,period_frames); |
|
418 snd_pcm_sw_params_set_stop_threshold(handle,swparams,buffer_frames); |
|
419 snd_pcm_sw_params_set_avail_min(handle, swparams,period_frames); |
|
420 snd_pcm_sw_params(handle, swparams); |
|
421 |
|
422 // Step 4: Prepare audio |
|
423 if(audioBuffer == 0) |
|
424 audioBuffer = new char[buffer_size]; |
|
425 snd_pcm_prepare( handle ); |
|
426 snd_pcm_start(handle); |
|
427 |
|
428 // Step 5: Setup timer |
|
429 bytesAvailable = checkBytesReady(); |
|
430 |
|
431 if(pullMode) |
|
432 connect(audioSource,SIGNAL(readyRead()),this,SLOT(userFeed())); |
|
433 |
|
434 // Step 6: Start audio processing |
|
435 chunks = buffer_size/period_size; |
|
436 timer->start(period_time*chunks/2000); |
|
437 |
|
438 errorState = QAudio::NoError; |
|
439 |
|
440 totalTimeValue = 0; |
|
441 |
|
442 return true; |
|
443 } |
|
444 |
|
445 void QAudioInputPrivate::close() |
|
446 { |
|
447 timer->stop(); |
|
448 |
|
449 if ( handle ) { |
|
450 snd_pcm_drop( handle ); |
|
451 snd_pcm_close( handle ); |
|
452 handle = 0; |
|
453 delete [] audioBuffer; |
|
454 audioBuffer=0; |
|
455 } |
|
456 } |
|
457 |
|
458 int QAudioInputPrivate::checkBytesReady() |
|
459 { |
|
460 if(resuming) |
|
461 bytesAvailable = period_size; |
|
462 else if(deviceState != QAudio::ActiveState |
|
463 && deviceState != QAudio::IdleState) |
|
464 bytesAvailable = 0; |
|
465 else { |
|
466 int frames = snd_pcm_avail_update(handle); |
|
467 if (frames < 0) { |
|
468 bytesAvailable = frames; |
|
469 } else { |
|
470 if((int)frames > (int)buffer_frames) |
|
471 frames = buffer_frames; |
|
472 bytesAvailable = snd_pcm_frames_to_bytes(handle, frames); |
|
473 } |
|
474 } |
|
475 return bytesAvailable; |
|
476 } |
|
477 |
|
478 int QAudioInputPrivate::bytesReady() const |
|
479 { |
|
480 return qMax(bytesAvailable, 0); |
|
481 } |
|
482 |
|
483 qint64 QAudioInputPrivate::read(char* data, qint64 len) |
|
484 { |
|
485 Q_UNUSED(len) |
|
486 |
|
487 // Read in some audio data and write it to QIODevice, pull mode |
|
488 if ( !handle ) |
|
489 return 0; |
|
490 |
|
491 bytesAvailable = checkBytesReady(); |
|
492 |
|
493 if (bytesAvailable < 0) { |
|
494 // bytesAvailable as negative is error code, try to recover from it. |
|
495 xrun_recovery(bytesAvailable); |
|
496 bytesAvailable = checkBytesReady(); |
|
497 if (bytesAvailable < 0) { |
|
498 // recovery failed must stop and set error. |
|
499 close(); |
|
500 errorState = QAudio::IOError; |
|
501 deviceState = QAudio::StoppedState; |
|
502 emit stateChanged(deviceState); |
|
503 return 0; |
|
504 } |
|
505 } |
|
506 |
|
507 int count=0, err = 0; |
|
508 while(count < 5) { |
|
509 int chunks = bytesAvailable/period_size; |
|
510 int frames = chunks*period_frames; |
|
511 if(frames > (int)buffer_frames) |
|
512 frames = buffer_frames; |
|
513 int readFrames = snd_pcm_readi(handle, audioBuffer, frames); |
|
514 if (readFrames >= 0) { |
|
515 err = snd_pcm_frames_to_bytes(handle, readFrames); |
|
516 #ifdef DEBUG_AUDIO |
|
517 qDebug()<<QString::fromLatin1("read in bytes = %1 (frames=%2)").arg(err).arg(readFrames).toLatin1().constData(); |
|
518 #endif |
|
519 break; |
|
520 } else if((readFrames == -EAGAIN) || (readFrames == -EINTR)) { |
|
521 errorState = QAudio::IOError; |
|
522 err = 0; |
|
523 break; |
|
524 } else { |
|
525 if(readFrames == -EPIPE) { |
|
526 errorState = QAudio::UnderrunError; |
|
527 err = snd_pcm_prepare(handle); |
|
528 } else if(readFrames == -ESTRPIPE) { |
|
529 err = snd_pcm_prepare(handle); |
|
530 } |
|
531 if(err != 0) break; |
|
532 } |
|
533 count++; |
|
534 } |
|
535 if(err > 0) { |
|
536 // got some send it onward |
|
537 #ifdef DEBUG_AUDIO |
|
538 qDebug()<<"frames to write to QIODevice = "<< |
|
539 snd_pcm_bytes_to_frames( handle, (int)err )<<" ("<<err<<") bytes"; |
|
540 #endif |
|
541 if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState) |
|
542 return 0; |
|
543 if (pullMode) { |
|
544 qint64 l = audioSource->write(audioBuffer,err); |
|
545 if(l < 0) { |
|
546 close(); |
|
547 errorState = QAudio::IOError; |
|
548 deviceState = QAudio::StoppedState; |
|
549 emit stateChanged(deviceState); |
|
550 } else if(l == 0) { |
|
551 if (deviceState != QAudio::IdleState) { |
|
552 errorState = QAudio::NoError; |
|
553 deviceState = QAudio::IdleState; |
|
554 emit stateChanged(deviceState); |
|
555 } |
|
556 } else { |
|
557 totalTimeValue += err; |
|
558 resuming = false; |
|
559 if (deviceState != QAudio::ActiveState) { |
|
560 errorState = QAudio::NoError; |
|
561 deviceState = QAudio::ActiveState; |
|
562 emit stateChanged(deviceState); |
|
563 } |
|
564 } |
|
565 return l; |
|
566 |
|
567 } else { |
|
568 memcpy(data,audioBuffer,err); |
|
569 totalTimeValue += err; |
|
570 resuming = false; |
|
571 if (deviceState != QAudio::ActiveState) { |
|
572 errorState = QAudio::NoError; |
|
573 deviceState = QAudio::ActiveState; |
|
574 emit stateChanged(deviceState); |
|
575 } |
|
576 return err; |
|
577 } |
|
578 } |
|
579 return 0; |
|
580 } |
|
581 |
|
582 void QAudioInputPrivate::resume() |
|
583 { |
|
584 if(deviceState == QAudio::SuspendedState) { |
|
585 int err = 0; |
|
586 |
|
587 if(handle) { |
|
588 err = snd_pcm_prepare( handle ); |
|
589 if(err < 0) |
|
590 xrun_recovery(err); |
|
591 |
|
592 err = snd_pcm_start(handle); |
|
593 if(err < 0) |
|
594 xrun_recovery(err); |
|
595 |
|
596 bytesAvailable = buffer_size; |
|
597 } |
|
598 resuming = true; |
|
599 deviceState = QAudio::ActiveState; |
|
600 int chunks = buffer_size/period_size; |
|
601 timer->start(period_time*chunks/2000); |
|
602 emit stateChanged(deviceState); |
|
603 } |
|
604 } |
|
605 |
|
606 void QAudioInputPrivate::setBufferSize(int value) |
|
607 { |
|
608 buffer_size = value; |
|
609 } |
|
610 |
|
611 int QAudioInputPrivate::bufferSize() const |
|
612 { |
|
613 return buffer_size; |
|
614 } |
|
615 |
|
616 int QAudioInputPrivate::periodSize() const |
|
617 { |
|
618 return period_size; |
|
619 } |
|
620 |
|
621 void QAudioInputPrivate::setNotifyInterval(int ms) |
|
622 { |
|
623 intervalTime = qMax(0, ms); |
|
624 } |
|
625 |
|
626 int QAudioInputPrivate::notifyInterval() const |
|
627 { |
|
628 return intervalTime; |
|
629 } |
|
630 |
|
631 qint64 QAudioInputPrivate::processedUSecs() const |
|
632 { |
|
633 qint64 result = qint64(1000000) * totalTimeValue / |
|
634 (settings.channels()*(settings.sampleSize()/8)) / |
|
635 settings.frequency(); |
|
636 |
|
637 return result; |
|
638 } |
|
639 |
|
640 void QAudioInputPrivate::suspend() |
|
641 { |
|
642 if(deviceState == QAudio::ActiveState||resuming) { |
|
643 timer->stop(); |
|
644 deviceState = QAudio::SuspendedState; |
|
645 emit stateChanged(deviceState); |
|
646 } |
|
647 } |
|
648 |
|
649 void QAudioInputPrivate::userFeed() |
|
650 { |
|
651 if(deviceState == QAudio::StoppedState || deviceState == QAudio::SuspendedState) |
|
652 return; |
|
653 #ifdef DEBUG_AUDIO |
|
654 QTime now(QTime::currentTime()); |
|
655 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :userFeed() IN"; |
|
656 #endif |
|
657 deviceReady(); |
|
658 } |
|
659 |
|
660 bool QAudioInputPrivate::deviceReady() |
|
661 { |
|
662 if(pullMode) { |
|
663 // reads some audio data and writes it to QIODevice |
|
664 read(0,0); |
|
665 } else { |
|
666 // emits readyRead() so user will call read() on QIODevice to get some audio data |
|
667 InputPrivate* a = qobject_cast<InputPrivate*>(audioSource); |
|
668 a->trigger(); |
|
669 } |
|
670 bytesAvailable = checkBytesReady(); |
|
671 |
|
672 if(deviceState != QAudio::ActiveState) |
|
673 return true; |
|
674 |
|
675 if (bytesAvailable < 0) { |
|
676 // bytesAvailable as negative is error code, try to recover from it. |
|
677 xrun_recovery(bytesAvailable); |
|
678 bytesAvailable = checkBytesReady(); |
|
679 if (bytesAvailable < 0) { |
|
680 // recovery failed must stop and set error. |
|
681 close(); |
|
682 errorState = QAudio::IOError; |
|
683 deviceState = QAudio::StoppedState; |
|
684 emit stateChanged(deviceState); |
|
685 return 0; |
|
686 } |
|
687 } |
|
688 |
|
689 if(intervalTime && (timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { |
|
690 emit notify(); |
|
691 elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; |
|
692 timeStamp.restart(); |
|
693 } |
|
694 return true; |
|
695 } |
|
696 |
|
697 qint64 QAudioInputPrivate::elapsedUSecs() const |
|
698 { |
|
699 if (deviceState == QAudio::StoppedState) |
|
700 return 0; |
|
701 |
|
702 return clockStamp.elapsed()*1000; |
|
703 } |
|
704 |
|
705 void QAudioInputPrivate::reset() |
|
706 { |
|
707 if(handle) |
|
708 snd_pcm_reset(handle); |
|
709 } |
|
710 |
|
711 void QAudioInputPrivate::drain() |
|
712 { |
|
713 if(handle) |
|
714 snd_pcm_drain(handle); |
|
715 } |
|
716 |
|
717 InputPrivate::InputPrivate(QAudioInputPrivate* audio) |
|
718 { |
|
719 audioDevice = qobject_cast<QAudioInputPrivate*>(audio); |
|
720 } |
|
721 |
|
722 InputPrivate::~InputPrivate() |
|
723 { |
|
724 } |
|
725 |
|
726 qint64 InputPrivate::readData( char* data, qint64 len) |
|
727 { |
|
728 return audioDevice->read(data,len); |
|
729 } |
|
730 |
|
731 qint64 InputPrivate::writeData(const char* data, qint64 len) |
|
732 { |
|
733 Q_UNUSED(data) |
|
734 Q_UNUSED(len) |
|
735 return 0; |
|
736 } |
|
737 |
|
738 void InputPrivate::trigger() |
|
739 { |
|
740 emit readyRead(); |
|
741 } |
|
742 |
|
743 QT_END_NAMESPACE |