|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 QtMultimedia module of the Qt Toolkit. |
|
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 |
|
54 #include "qaudioinput_win32_p.h" |
|
55 |
|
56 QT_BEGIN_NAMESPACE |
|
57 |
|
58 //#define DEBUG_AUDIO 1 |
|
59 |
|
60 static CRITICAL_SECTION waveInCriticalSection; |
|
61 |
|
62 static const int minimumIntervalTime = 50; |
|
63 |
|
64 QAudioInputPrivate::QAudioInputPrivate(const QByteArray &device, const QAudioFormat& audioFormat): |
|
65 settings(audioFormat) |
|
66 { |
|
67 bytesAvailable = 0; |
|
68 buffer_size = 0; |
|
69 period_size = 0; |
|
70 m_device = device; |
|
71 totalTimeValue = 0; |
|
72 intervalTime = 1000; |
|
73 errorState = QAudio::NoError; |
|
74 deviceState = QAudio::StopState; |
|
75 audioSource = 0; |
|
76 pullMode = true; |
|
77 resuming = false; |
|
78 finished = false; |
|
79 |
|
80 connect(this,SIGNAL(processMore()),SLOT(deviceReady())); |
|
81 |
|
82 InitializeCriticalSection(&waveInCriticalSection); |
|
83 } |
|
84 |
|
85 QAudioInputPrivate::~QAudioInputPrivate() |
|
86 { |
|
87 stop(); |
|
88 DeleteCriticalSection(&waveInCriticalSection); |
|
89 } |
|
90 |
|
91 void CALLBACK QAudioInputPrivate::waveInProc( HWAVEIN hWaveIn, UINT uMsg, |
|
92 DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 ) |
|
93 { |
|
94 Q_UNUSED(dwParam1) |
|
95 Q_UNUSED(dwParam2) |
|
96 Q_UNUSED(hWaveIn) |
|
97 |
|
98 QAudioInputPrivate* qAudio; |
|
99 qAudio = (QAudioInputPrivate*)(dwInstance); |
|
100 if(!qAudio) |
|
101 return; |
|
102 |
|
103 switch(uMsg) { |
|
104 case WIM_OPEN: |
|
105 break; |
|
106 case WIM_DATA: |
|
107 EnterCriticalSection(&waveInCriticalSection); |
|
108 if(qAudio->waveFreeBlockCount > 0) |
|
109 qAudio->waveFreeBlockCount--; |
|
110 qAudio->feedback(); |
|
111 LeaveCriticalSection(&waveInCriticalSection); |
|
112 break; |
|
113 case WIM_CLOSE: |
|
114 EnterCriticalSection(&waveInCriticalSection); |
|
115 qAudio->finished = true; |
|
116 LeaveCriticalSection(&waveInCriticalSection); |
|
117 break; |
|
118 default: |
|
119 return; |
|
120 } |
|
121 } |
|
122 |
|
123 WAVEHDR* QAudioInputPrivate::allocateBlocks(int size, int count) |
|
124 { |
|
125 int i; |
|
126 unsigned char* buffer; |
|
127 WAVEHDR* blocks; |
|
128 DWORD totalBufferSize = (size + sizeof(WAVEHDR))*count; |
|
129 |
|
130 if((buffer=(unsigned char*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, |
|
131 totalBufferSize)) == 0) { |
|
132 qWarning("QAudioInput: Memory allocation error"); |
|
133 return 0; |
|
134 } |
|
135 blocks = (WAVEHDR*)buffer; |
|
136 buffer += sizeof(WAVEHDR)*count; |
|
137 for(i = 0; i < count; i++) { |
|
138 blocks[i].dwBufferLength = size; |
|
139 blocks[i].lpData = (LPSTR)buffer; |
|
140 blocks[i].dwBytesRecorded=0; |
|
141 blocks[i].dwUser = 0L; |
|
142 blocks[i].dwFlags = 0L; |
|
143 blocks[i].dwLoops = 0L; |
|
144 result = waveInPrepareHeader(hWaveIn,&blocks[i], sizeof(WAVEHDR)); |
|
145 if(result != MMSYSERR_NOERROR) { |
|
146 qWarning("QAudioInput: Can't prepare block %d",i); |
|
147 return 0; |
|
148 } |
|
149 buffer += size; |
|
150 } |
|
151 return blocks; |
|
152 } |
|
153 |
|
154 void QAudioInputPrivate::freeBlocks(WAVEHDR* blockArray) |
|
155 { |
|
156 HeapFree(GetProcessHeap(), 0, blockArray); |
|
157 } |
|
158 |
|
159 QAudio::Error QAudioInputPrivate::error() const |
|
160 { |
|
161 return errorState; |
|
162 } |
|
163 |
|
164 QAudio::State QAudioInputPrivate::state() const |
|
165 { |
|
166 return deviceState; |
|
167 } |
|
168 |
|
169 QAudioFormat QAudioInputPrivate::format() const |
|
170 { |
|
171 return settings; |
|
172 } |
|
173 |
|
174 QIODevice* QAudioInputPrivate::start(QIODevice* device) |
|
175 { |
|
176 if(deviceState != QAudio::StopState) |
|
177 close(); |
|
178 |
|
179 if(!pullMode && audioSource) { |
|
180 delete audioSource; |
|
181 } |
|
182 |
|
183 if(device) { |
|
184 //set to pull mode |
|
185 pullMode = true; |
|
186 audioSource = device; |
|
187 } else { |
|
188 //set to push mode |
|
189 pullMode = false; |
|
190 audioSource = new InputPrivate(this); |
|
191 audioSource->open(QIODevice::ReadOnly | QIODevice::Unbuffered); |
|
192 } |
|
193 |
|
194 if( !open() ) |
|
195 return 0; |
|
196 |
|
197 emit stateChanged(deviceState); |
|
198 |
|
199 return audioSource; |
|
200 } |
|
201 |
|
202 void QAudioInputPrivate::stop() |
|
203 { |
|
204 if(deviceState == QAudio::StopState) |
|
205 return; |
|
206 |
|
207 close(); |
|
208 emit stateChanged(deviceState); |
|
209 } |
|
210 |
|
211 bool QAudioInputPrivate::open() |
|
212 { |
|
213 #ifdef DEBUG_AUDIO |
|
214 QTime now(QTime::currentTime()); |
|
215 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; |
|
216 #endif |
|
217 header = 0; |
|
218 if(buffer_size == 0) { |
|
219 // Default buffer size, 100ms, default period size is 20ms |
|
220 buffer_size = settings.frequency()*settings.channels()*(settings.sampleSize()/8)*0.1; |
|
221 period_size = buffer_size/5; |
|
222 } else { |
|
223 period_size = buffer_size/5; |
|
224 } |
|
225 #ifdef Q_OS_WINCE |
|
226 // For wince reduce size to 40ms for buffer size and 20ms period |
|
227 buffer_size = settings.frequency()*settings.channels()*(settings.sampleSize()/8)*0.04; |
|
228 period_size = buffer_size/2; |
|
229 #endif |
|
230 timeStamp.restart(); |
|
231 elapsedTimeOffset = 0; |
|
232 wfx.nSamplesPerSec = settings.frequency(); |
|
233 wfx.wBitsPerSample = settings.sampleSize(); |
|
234 wfx.nChannels = settings.channels(); |
|
235 wfx.cbSize = 0; |
|
236 |
|
237 wfx.wFormatTag = WAVE_FORMAT_PCM; |
|
238 wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels; |
|
239 wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec; |
|
240 |
|
241 UINT_PTR devId = WAVE_MAPPER; |
|
242 |
|
243 WAVEINCAPS wic; |
|
244 unsigned long iNumDevs,ii; |
|
245 iNumDevs = waveInGetNumDevs(); |
|
246 for(ii=0;ii<iNumDevs;ii++) { |
|
247 if(waveInGetDevCaps(ii, &wic, sizeof(WAVEINCAPS)) |
|
248 == MMSYSERR_NOERROR) { |
|
249 QString tmp; |
|
250 tmp = QString::fromUtf16((const unsigned short*)wic.szPname); |
|
251 if(tmp.compare(QLatin1String(m_device)) == 0) { |
|
252 devId = ii; |
|
253 break; |
|
254 } |
|
255 } |
|
256 } |
|
257 |
|
258 if(waveInOpen(&hWaveIn, devId, &wfx, |
|
259 (DWORD_PTR)&waveInProc, |
|
260 (DWORD_PTR) this, |
|
261 CALLBACK_FUNCTION) != MMSYSERR_NOERROR) { |
|
262 errorState = QAudio::OpenError; |
|
263 deviceState = QAudio::StopState; |
|
264 emit stateChanged(deviceState); |
|
265 qWarning("QAudioInput: failed to open audio device"); |
|
266 return false; |
|
267 } |
|
268 waveBlocks = allocateBlocks(period_size, buffer_size/period_size); |
|
269 |
|
270 if(waveBlocks == 0) { |
|
271 errorState = QAudio::OpenError; |
|
272 deviceState = QAudio::StopState; |
|
273 emit stateChanged(deviceState); |
|
274 qWarning("QAudioInput: failed to allocate blocks. open failed"); |
|
275 return false; |
|
276 } |
|
277 |
|
278 EnterCriticalSection(&waveInCriticalSection); |
|
279 waveFreeBlockCount = buffer_size/period_size; |
|
280 LeaveCriticalSection(&waveInCriticalSection); |
|
281 |
|
282 waveCurrentBlock = 0; |
|
283 |
|
284 for(int i=0; i<buffer_size/period_size; i++) { |
|
285 result = waveInAddBuffer(hWaveIn, &waveBlocks[i], sizeof(WAVEHDR)); |
|
286 if(result != MMSYSERR_NOERROR) { |
|
287 qWarning("QAudioInput: failed to setup block %d,err=%d",i,result); |
|
288 errorState = QAudio::OpenError; |
|
289 deviceState = QAudio::StopState; |
|
290 emit stateChanged(deviceState); |
|
291 return false; |
|
292 } |
|
293 } |
|
294 result = waveInStart(hWaveIn); |
|
295 if(result) { |
|
296 qWarning("QAudioInput: failed to start audio input"); |
|
297 errorState = QAudio::OpenError; |
|
298 deviceState = QAudio::StopState; |
|
299 emit stateChanged(deviceState); |
|
300 return false; |
|
301 } |
|
302 timeStampOpened.restart(); |
|
303 elapsedTimeOffset = 0; |
|
304 totalTimeValue = 0; |
|
305 errorState = QAudio::NoError; |
|
306 deviceState = QAudio::ActiveState; |
|
307 return true; |
|
308 } |
|
309 |
|
310 void QAudioInputPrivate::close() |
|
311 { |
|
312 if(deviceState == QAudio::StopState) |
|
313 return; |
|
314 |
|
315 waveInReset(hWaveIn); |
|
316 waveInClose(hWaveIn); |
|
317 deviceState = QAudio::StopState; |
|
318 |
|
319 int count = 0; |
|
320 while(!finished && count < 100) { |
|
321 count++; |
|
322 Sleep(10); |
|
323 } |
|
324 |
|
325 EnterCriticalSection(&waveInCriticalSection); |
|
326 for(int i=0; i<waveFreeBlockCount; i++) { |
|
327 if(waveBlocks[i].dwFlags & WHDR_PREPARED) |
|
328 waveInUnprepareHeader(hWaveIn,&waveBlocks[i],sizeof(WAVEHDR)); |
|
329 } |
|
330 LeaveCriticalSection(&waveInCriticalSection); |
|
331 freeBlocks(waveBlocks); |
|
332 } |
|
333 |
|
334 int QAudioInputPrivate::bytesReady() const |
|
335 { |
|
336 int buf = ((buffer_size/period_size)-waveFreeBlockCount)*period_size; |
|
337 if(buf < 0) |
|
338 buf = 0; |
|
339 return buf; |
|
340 } |
|
341 |
|
342 qint64 QAudioInputPrivate::read(char* data, qint64 len) |
|
343 { |
|
344 bool done = false; |
|
345 |
|
346 char* p = data; |
|
347 qint64 l = 0; |
|
348 qint64 written = 0; |
|
349 while(!done) { |
|
350 // Read in some audio data |
|
351 if(waveBlocks[header].dwBytesRecorded > 0) { |
|
352 if(pullMode) { |
|
353 l = audioSource->write(waveBlocks[header].lpData, |
|
354 waveBlocks[header].dwBytesRecorded); |
|
355 #ifdef DEBUG_AUDIO |
|
356 qDebug()<<"IN: "<<waveBlocks[header].dwBytesRecorded<<", OUT: "<<l; |
|
357 #endif |
|
358 if(l < 0) { |
|
359 // error |
|
360 qWarning("QAudioInput: IOError"); |
|
361 errorState = QAudio::IOError; |
|
362 |
|
363 } else if(l == 0) { |
|
364 // cant write to IODevice |
|
365 qWarning("QAudioInput: IOError, can't write to QIODevice"); |
|
366 errorState = QAudio::IOError; |
|
367 |
|
368 } else { |
|
369 totalTimeValue += waveBlocks[header].dwBytesRecorded |
|
370 /((settings.channels()*settings.sampleSize()/8)) |
|
371 *10000/settings.frequency()*100; |
|
372 errorState = QAudio::NoError; |
|
373 deviceState = QAudio::ActiveState; |
|
374 resuming = false; |
|
375 } |
|
376 } else { |
|
377 // push mode |
|
378 memcpy(p,waveBlocks[header].lpData,waveBlocks[header].dwBytesRecorded); |
|
379 l = waveBlocks[header].dwBytesRecorded; |
|
380 #ifdef DEBUG_AUDIO |
|
381 qDebug()<<"IN: "<<waveBlocks[header].dwBytesRecorded<<", OUT: "<<l; |
|
382 #endif |
|
383 totalTimeValue += waveBlocks[header].dwBytesRecorded |
|
384 /((settings.channels()*settings.sampleSize()/8)) |
|
385 *10000/settings.frequency()*100; |
|
386 errorState = QAudio::NoError; |
|
387 deviceState = QAudio::ActiveState; |
|
388 resuming = false; |
|
389 } |
|
390 } else { |
|
391 //no data, not ready yet, next time |
|
392 return 0; |
|
393 } |
|
394 EnterCriticalSection(&waveInCriticalSection); |
|
395 waveFreeBlockCount++; |
|
396 LeaveCriticalSection(&waveInCriticalSection); |
|
397 waveBlocks[header].dwBytesRecorded=0; |
|
398 waveBlocks[header].dwFlags = 0L; |
|
399 result = waveInPrepareHeader(hWaveIn,&waveBlocks[header], sizeof(WAVEHDR)); |
|
400 if(result != MMSYSERR_NOERROR) { |
|
401 qWarning("QAudioInput: failed to prepare block %d,err=%d",header,result); |
|
402 errorState = QAudio::OpenError; |
|
403 deviceState = QAudio::StopState; |
|
404 emit stateChanged(deviceState); |
|
405 } |
|
406 result = waveInAddBuffer(hWaveIn, &waveBlocks[header], sizeof(WAVEHDR)); |
|
407 if(result != MMSYSERR_NOERROR) { |
|
408 qWarning("QAudioInput: failed to setup block %d,err=%d",header,result); |
|
409 errorState = QAudio::OpenError; |
|
410 deviceState = QAudio::StopState; |
|
411 emit stateChanged(deviceState); |
|
412 } |
|
413 header++; |
|
414 if(header >= buffer_size/period_size) |
|
415 header = 0; |
|
416 p+=l; |
|
417 |
|
418 EnterCriticalSection(&waveInCriticalSection); |
|
419 if(!pullMode) { |
|
420 if(l+period_size > len && waveFreeBlockCount == buffer_size/period_size) |
|
421 done = true; |
|
422 } else { |
|
423 if(waveFreeBlockCount == buffer_size/period_size) |
|
424 done = true; |
|
425 } |
|
426 LeaveCriticalSection(&waveInCriticalSection); |
|
427 |
|
428 written+=l; |
|
429 } |
|
430 #ifdef DEBUG_AUDIO |
|
431 qDebug()<<"read in len="<<written; |
|
432 #endif |
|
433 return written; |
|
434 } |
|
435 |
|
436 void QAudioInputPrivate::resume() |
|
437 { |
|
438 if(deviceState == QAudio::SuspendState) { |
|
439 deviceState = QAudio::ActiveState; |
|
440 for(int i=0; i<buffer_size/period_size; i++) { |
|
441 result = waveInAddBuffer(hWaveIn, &waveBlocks[i], sizeof(WAVEHDR)); |
|
442 if(result != MMSYSERR_NOERROR) { |
|
443 qWarning("QAudioInput: failed to setup block %d,err=%d",i,result); |
|
444 errorState = QAudio::OpenError; |
|
445 deviceState = QAudio::StopState; |
|
446 emit stateChanged(deviceState); |
|
447 return; |
|
448 } |
|
449 } |
|
450 EnterCriticalSection(&waveInCriticalSection); |
|
451 waveFreeBlockCount = buffer_size/period_size; |
|
452 LeaveCriticalSection(&waveInCriticalSection); |
|
453 |
|
454 waveCurrentBlock = 0; |
|
455 header = 0; |
|
456 resuming = true; |
|
457 waveInStart(hWaveIn); |
|
458 QTimer::singleShot(20,this,SLOT(feedback())); |
|
459 emit stateChanged(deviceState); |
|
460 } |
|
461 } |
|
462 |
|
463 void QAudioInputPrivate::setBufferSize(int value) |
|
464 { |
|
465 buffer_size = value; |
|
466 } |
|
467 |
|
468 int QAudioInputPrivate::bufferSize() const |
|
469 { |
|
470 return buffer_size; |
|
471 } |
|
472 |
|
473 int QAudioInputPrivate::periodSize() const |
|
474 { |
|
475 return period_size; |
|
476 } |
|
477 |
|
478 void QAudioInputPrivate::setNotifyInterval(int ms) |
|
479 { |
|
480 if(ms >= minimumIntervalTime) |
|
481 intervalTime = ms; |
|
482 else |
|
483 intervalTime = minimumIntervalTime; |
|
484 } |
|
485 |
|
486 int QAudioInputPrivate::notifyInterval() const |
|
487 { |
|
488 return intervalTime; |
|
489 } |
|
490 |
|
491 qint64 QAudioInputPrivate::totalTime() const |
|
492 { |
|
493 return totalTimeValue; |
|
494 } |
|
495 |
|
496 void QAudioInputPrivate::suspend() |
|
497 { |
|
498 if(deviceState == QAudio::ActiveState) { |
|
499 waveInReset(hWaveIn); |
|
500 deviceState = QAudio::SuspendState; |
|
501 emit stateChanged(deviceState); |
|
502 } |
|
503 } |
|
504 |
|
505 void QAudioInputPrivate::feedback() |
|
506 { |
|
507 #ifdef DEBUG_AUDIO |
|
508 QTime now(QTime::currentTime()); |
|
509 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :feedback() INPUT "<<this; |
|
510 #endif |
|
511 bytesAvailable = bytesReady(); |
|
512 |
|
513 if(!(deviceState==QAudio::StopState||deviceState==QAudio::SuspendState)) |
|
514 emit processMore(); |
|
515 } |
|
516 |
|
517 bool QAudioInputPrivate::deviceReady() |
|
518 { |
|
519 #ifdef DEBUG_AUDIO |
|
520 QTime now(QTime::currentTime()); |
|
521 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :deviceReady() INPUT"; |
|
522 #endif |
|
523 if(pullMode) { |
|
524 // reads some audio data and writes it to QIODevice |
|
525 read(0,0); |
|
526 } else { |
|
527 // emits readyRead() so user will call read() on QIODevice to get some audio data |
|
528 InputPrivate* a = qobject_cast<InputPrivate*>(audioSource); |
|
529 a->trigger(); |
|
530 } |
|
531 if(deviceState != QAudio::ActiveState) |
|
532 return true; |
|
533 |
|
534 if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { |
|
535 emit notify(); |
|
536 elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; |
|
537 timeStamp.restart(); |
|
538 } |
|
539 return true; |
|
540 } |
|
541 |
|
542 qint64 QAudioInputPrivate::clock() const |
|
543 { |
|
544 if (deviceState == QAudio::StopState) |
|
545 return 0; |
|
546 |
|
547 return timeStampOpened.elapsed()*1000; |
|
548 } |
|
549 |
|
550 void QAudioInputPrivate::reset() |
|
551 { |
|
552 close(); |
|
553 } |
|
554 |
|
555 InputPrivate::InputPrivate(QAudioInputPrivate* audio) |
|
556 { |
|
557 audioDevice = qobject_cast<QAudioInputPrivate*>(audio); |
|
558 } |
|
559 |
|
560 InputPrivate::~InputPrivate() {} |
|
561 |
|
562 qint64 InputPrivate::readData( char* data, qint64 len) |
|
563 { |
|
564 // push mode, user read() called |
|
565 if(audioDevice->deviceState != QAudio::ActiveState) |
|
566 return 0; |
|
567 // Read in some audio data |
|
568 return audioDevice->read(data,len); |
|
569 } |
|
570 |
|
571 qint64 InputPrivate::writeData(const char* data, qint64 len) |
|
572 { |
|
573 Q_UNUSED(data) |
|
574 Q_UNUSED(len) |
|
575 |
|
576 emit readyRead(); |
|
577 return 0; |
|
578 } |
|
579 |
|
580 void InputPrivate::trigger() |
|
581 { |
|
582 emit readyRead(); |
|
583 } |
|
584 |
|
585 QT_END_NAMESPACE |