author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
child 13 | c0432d11811c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
74 |
deviceState = QAudio::StoppedState; |
0 | 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 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
WAVEHDR* blocks = blockArray; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
int count = buffer_size/period_size; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
for(int i = 0; i < count; i++) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
waveInUnprepareHeader(hWaveIn,&blocks[i], sizeof(WAVEHDR)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
blocks+=sizeof(WAVEHDR); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
} |
0 | 164 |
HeapFree(GetProcessHeap(), 0, blockArray); |
165 |
} |
|
166 |
||
167 |
QAudio::Error QAudioInputPrivate::error() const |
|
168 |
{ |
|
169 |
return errorState; |
|
170 |
} |
|
171 |
||
172 |
QAudio::State QAudioInputPrivate::state() const |
|
173 |
{ |
|
174 |
return deviceState; |
|
175 |
} |
|
176 |
||
177 |
QAudioFormat QAudioInputPrivate::format() const |
|
178 |
{ |
|
179 |
return settings; |
|
180 |
} |
|
181 |
||
182 |
QIODevice* QAudioInputPrivate::start(QIODevice* device) |
|
183 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
184 |
if(deviceState != QAudio::StoppedState) |
0 | 185 |
close(); |
186 |
||
187 |
if(!pullMode && audioSource) { |
|
188 |
delete audioSource; |
|
189 |
} |
|
190 |
||
191 |
if(device) { |
|
192 |
//set to pull mode |
|
193 |
pullMode = true; |
|
194 |
audioSource = device; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
195 |
deviceState = QAudio::ActiveState; |
0 | 196 |
} else { |
197 |
//set to push mode |
|
198 |
pullMode = false; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
199 |
deviceState = QAudio::IdleState; |
0 | 200 |
audioSource = new InputPrivate(this); |
201 |
audioSource->open(QIODevice::ReadOnly | QIODevice::Unbuffered); |
|
202 |
} |
|
203 |
||
204 |
if( !open() ) |
|
205 |
return 0; |
|
206 |
||
207 |
emit stateChanged(deviceState); |
|
208 |
||
209 |
return audioSource; |
|
210 |
} |
|
211 |
||
212 |
void QAudioInputPrivate::stop() |
|
213 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
if(deviceState == QAudio::StoppedState) |
0 | 215 |
return; |
216 |
||
217 |
close(); |
|
218 |
emit stateChanged(deviceState); |
|
219 |
} |
|
220 |
||
221 |
bool QAudioInputPrivate::open() |
|
222 |
{ |
|
223 |
#ifdef DEBUG_AUDIO |
|
224 |
QTime now(QTime::currentTime()); |
|
225 |
qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; |
|
226 |
#endif |
|
227 |
header = 0; |
|
228 |
if(buffer_size == 0) { |
|
229 |
// Default buffer size, 100ms, default period size is 20ms |
|
230 |
buffer_size = settings.frequency()*settings.channels()*(settings.sampleSize()/8)*0.1; |
|
231 |
period_size = buffer_size/5; |
|
232 |
} else { |
|
233 |
period_size = buffer_size/5; |
|
234 |
} |
|
235 |
timeStamp.restart(); |
|
236 |
elapsedTimeOffset = 0; |
|
237 |
wfx.nSamplesPerSec = settings.frequency(); |
|
238 |
wfx.wBitsPerSample = settings.sampleSize(); |
|
239 |
wfx.nChannels = settings.channels(); |
|
240 |
wfx.cbSize = 0; |
|
241 |
||
242 |
wfx.wFormatTag = WAVE_FORMAT_PCM; |
|
243 |
wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels; |
|
244 |
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec; |
|
245 |
||
246 |
UINT_PTR devId = WAVE_MAPPER; |
|
247 |
||
248 |
WAVEINCAPS wic; |
|
249 |
unsigned long iNumDevs,ii; |
|
250 |
iNumDevs = waveInGetNumDevs(); |
|
251 |
for(ii=0;ii<iNumDevs;ii++) { |
|
252 |
if(waveInGetDevCaps(ii, &wic, sizeof(WAVEINCAPS)) |
|
253 |
== MMSYSERR_NOERROR) { |
|
254 |
QString tmp; |
|
255 |
tmp = QString::fromUtf16((const unsigned short*)wic.szPname); |
|
256 |
if(tmp.compare(QLatin1String(m_device)) == 0) { |
|
257 |
devId = ii; |
|
258 |
break; |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
if(waveInOpen(&hWaveIn, devId, &wfx, |
|
264 |
(DWORD_PTR)&waveInProc, |
|
265 |
(DWORD_PTR) this, |
|
266 |
CALLBACK_FUNCTION) != MMSYSERR_NOERROR) { |
|
267 |
errorState = QAudio::OpenError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
268 |
deviceState = QAudio::StoppedState; |
0 | 269 |
emit stateChanged(deviceState); |
270 |
qWarning("QAudioInput: failed to open audio device"); |
|
271 |
return false; |
|
272 |
} |
|
273 |
waveBlocks = allocateBlocks(period_size, buffer_size/period_size); |
|
274 |
||
275 |
if(waveBlocks == 0) { |
|
276 |
errorState = QAudio::OpenError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
277 |
deviceState = QAudio::StoppedState; |
0 | 278 |
emit stateChanged(deviceState); |
279 |
qWarning("QAudioInput: failed to allocate blocks. open failed"); |
|
280 |
return false; |
|
281 |
} |
|
282 |
||
283 |
EnterCriticalSection(&waveInCriticalSection); |
|
284 |
waveFreeBlockCount = buffer_size/period_size; |
|
285 |
LeaveCriticalSection(&waveInCriticalSection); |
|
286 |
||
287 |
waveCurrentBlock = 0; |
|
288 |
||
289 |
for(int i=0; i<buffer_size/period_size; i++) { |
|
290 |
result = waveInAddBuffer(hWaveIn, &waveBlocks[i], sizeof(WAVEHDR)); |
|
291 |
if(result != MMSYSERR_NOERROR) { |
|
292 |
qWarning("QAudioInput: failed to setup block %d,err=%d",i,result); |
|
293 |
errorState = QAudio::OpenError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
294 |
deviceState = QAudio::StoppedState; |
0 | 295 |
emit stateChanged(deviceState); |
296 |
return false; |
|
297 |
} |
|
298 |
} |
|
299 |
result = waveInStart(hWaveIn); |
|
300 |
if(result) { |
|
301 |
qWarning("QAudioInput: failed to start audio input"); |
|
302 |
errorState = QAudio::OpenError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
303 |
deviceState = QAudio::StoppedState; |
0 | 304 |
emit stateChanged(deviceState); |
305 |
return false; |
|
306 |
} |
|
307 |
timeStampOpened.restart(); |
|
308 |
elapsedTimeOffset = 0; |
|
309 |
totalTimeValue = 0; |
|
310 |
errorState = QAudio::NoError; |
|
311 |
return true; |
|
312 |
} |
|
313 |
||
314 |
void QAudioInputPrivate::close() |
|
315 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
316 |
if(deviceState == QAudio::StoppedState) |
0 | 317 |
return; |
318 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
319 |
deviceState = QAudio::StoppedState; |
0 | 320 |
waveInReset(hWaveIn); |
321 |
waveInClose(hWaveIn); |
|
322 |
||
323 |
int count = 0; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
324 |
while(!finished && count < 500) { |
0 | 325 |
count++; |
326 |
Sleep(10); |
|
327 |
} |
|
328 |
||
329 |
EnterCriticalSection(&waveInCriticalSection); |
|
330 |
for(int i=0; i<waveFreeBlockCount; i++) { |
|
331 |
if(waveBlocks[i].dwFlags & WHDR_PREPARED) |
|
332 |
waveInUnprepareHeader(hWaveIn,&waveBlocks[i],sizeof(WAVEHDR)); |
|
333 |
} |
|
334 |
LeaveCriticalSection(&waveInCriticalSection); |
|
335 |
freeBlocks(waveBlocks); |
|
336 |
} |
|
337 |
||
338 |
int QAudioInputPrivate::bytesReady() const |
|
339 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
340 |
if(period_size == 0 || buffer_size == 0) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
341 |
return 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
342 |
|
0 | 343 |
int buf = ((buffer_size/period_size)-waveFreeBlockCount)*period_size; |
344 |
if(buf < 0) |
|
345 |
buf = 0; |
|
346 |
return buf; |
|
347 |
} |
|
348 |
||
349 |
qint64 QAudioInputPrivate::read(char* data, qint64 len) |
|
350 |
{ |
|
351 |
bool done = false; |
|
352 |
||
353 |
char* p = data; |
|
354 |
qint64 l = 0; |
|
355 |
qint64 written = 0; |
|
356 |
while(!done) { |
|
357 |
// Read in some audio data |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
358 |
if(waveBlocks[header].dwBytesRecorded > 0 && waveBlocks[header].dwFlags & WHDR_DONE) { |
0 | 359 |
if(pullMode) { |
360 |
l = audioSource->write(waveBlocks[header].lpData, |
|
361 |
waveBlocks[header].dwBytesRecorded); |
|
362 |
#ifdef DEBUG_AUDIO |
|
363 |
qDebug()<<"IN: "<<waveBlocks[header].dwBytesRecorded<<", OUT: "<<l; |
|
364 |
#endif |
|
365 |
if(l < 0) { |
|
366 |
// error |
|
367 |
qWarning("QAudioInput: IOError"); |
|
368 |
errorState = QAudio::IOError; |
|
369 |
||
370 |
} else if(l == 0) { |
|
371 |
// cant write to IODevice |
|
372 |
qWarning("QAudioInput: IOError, can't write to QIODevice"); |
|
373 |
errorState = QAudio::IOError; |
|
374 |
||
375 |
} else { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
376 |
totalTimeValue += waveBlocks[header].dwBytesRecorded; |
0 | 377 |
errorState = QAudio::NoError; |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
378 |
if (deviceState != QAudio::ActiveState) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
379 |
deviceState = QAudio::ActiveState; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
380 |
emit stateChanged(deviceState); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
381 |
} |
0 | 382 |
resuming = false; |
383 |
} |
|
384 |
} else { |
|
385 |
// push mode |
|
386 |
memcpy(p,waveBlocks[header].lpData,waveBlocks[header].dwBytesRecorded); |
|
387 |
l = waveBlocks[header].dwBytesRecorded; |
|
388 |
#ifdef DEBUG_AUDIO |
|
389 |
qDebug()<<"IN: "<<waveBlocks[header].dwBytesRecorded<<", OUT: "<<l; |
|
390 |
#endif |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
391 |
totalTimeValue += waveBlocks[header].dwBytesRecorded; |
0 | 392 |
errorState = QAudio::NoError; |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
393 |
if (deviceState != QAudio::ActiveState) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
394 |
deviceState = QAudio::ActiveState; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
395 |
emit stateChanged(deviceState); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
396 |
} |
0 | 397 |
resuming = false; |
398 |
} |
|
399 |
} else { |
|
400 |
//no data, not ready yet, next time |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
401 |
break; |
0 | 402 |
} |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
403 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
404 |
waveInUnprepareHeader(hWaveIn,&waveBlocks[header], sizeof(WAVEHDR)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
405 |
|
0 | 406 |
EnterCriticalSection(&waveInCriticalSection); |
407 |
waveFreeBlockCount++; |
|
408 |
LeaveCriticalSection(&waveInCriticalSection); |
|
409 |
waveBlocks[header].dwBytesRecorded=0; |
|
410 |
waveBlocks[header].dwFlags = 0L; |
|
411 |
result = waveInPrepareHeader(hWaveIn,&waveBlocks[header], sizeof(WAVEHDR)); |
|
412 |
if(result != MMSYSERR_NOERROR) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
413 |
result = waveInPrepareHeader(hWaveIn,&waveBlocks[header], sizeof(WAVEHDR)); |
0 | 414 |
qWarning("QAudioInput: failed to prepare block %d,err=%d",header,result); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
415 |
errorState = QAudio::IOError; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
416 |
EnterCriticalSection(&waveInCriticalSection); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
417 |
waveFreeBlockCount--; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
418 |
LeaveCriticalSection(&waveInCriticalSection); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
419 |
return 0; |
0 | 420 |
} |
421 |
result = waveInAddBuffer(hWaveIn, &waveBlocks[header], sizeof(WAVEHDR)); |
|
422 |
if(result != MMSYSERR_NOERROR) { |
|
423 |
qWarning("QAudioInput: failed to setup block %d,err=%d",header,result); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
424 |
errorState = QAudio::IOError; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
425 |
EnterCriticalSection(&waveInCriticalSection); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
426 |
waveFreeBlockCount--; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
427 |
LeaveCriticalSection(&waveInCriticalSection); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
428 |
return 0; |
0 | 429 |
} |
430 |
header++; |
|
431 |
if(header >= buffer_size/period_size) |
|
432 |
header = 0; |
|
433 |
p+=l; |
|
434 |
||
435 |
EnterCriticalSection(&waveInCriticalSection); |
|
436 |
if(!pullMode) { |
|
437 |
if(l+period_size > len && waveFreeBlockCount == buffer_size/period_size) |
|
438 |
done = true; |
|
439 |
} else { |
|
440 |
if(waveFreeBlockCount == buffer_size/period_size) |
|
441 |
done = true; |
|
442 |
} |
|
443 |
LeaveCriticalSection(&waveInCriticalSection); |
|
444 |
||
445 |
written+=l; |
|
446 |
} |
|
447 |
#ifdef DEBUG_AUDIO |
|
448 |
qDebug()<<"read in len="<<written; |
|
449 |
#endif |
|
450 |
return written; |
|
451 |
} |
|
452 |
||
453 |
void QAudioInputPrivate::resume() |
|
454 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
455 |
if(deviceState == QAudio::SuspendedState) { |
0 | 456 |
deviceState = QAudio::ActiveState; |
457 |
for(int i=0; i<buffer_size/period_size; i++) { |
|
458 |
result = waveInAddBuffer(hWaveIn, &waveBlocks[i], sizeof(WAVEHDR)); |
|
459 |
if(result != MMSYSERR_NOERROR) { |
|
460 |
qWarning("QAudioInput: failed to setup block %d,err=%d",i,result); |
|
461 |
errorState = QAudio::OpenError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
462 |
deviceState = QAudio::StoppedState; |
0 | 463 |
emit stateChanged(deviceState); |
464 |
return; |
|
465 |
} |
|
466 |
} |
|
467 |
EnterCriticalSection(&waveInCriticalSection); |
|
468 |
waveFreeBlockCount = buffer_size/period_size; |
|
469 |
LeaveCriticalSection(&waveInCriticalSection); |
|
470 |
||
471 |
waveCurrentBlock = 0; |
|
472 |
header = 0; |
|
473 |
resuming = true; |
|
474 |
waveInStart(hWaveIn); |
|
475 |
QTimer::singleShot(20,this,SLOT(feedback())); |
|
476 |
emit stateChanged(deviceState); |
|
477 |
} |
|
478 |
} |
|
479 |
||
480 |
void QAudioInputPrivate::setBufferSize(int value) |
|
481 |
{ |
|
482 |
buffer_size = value; |
|
483 |
} |
|
484 |
||
485 |
int QAudioInputPrivate::bufferSize() const |
|
486 |
{ |
|
487 |
return buffer_size; |
|
488 |
} |
|
489 |
||
490 |
int QAudioInputPrivate::periodSize() const |
|
491 |
{ |
|
492 |
return period_size; |
|
493 |
} |
|
494 |
||
495 |
void QAudioInputPrivate::setNotifyInterval(int ms) |
|
496 |
{ |
|
497 |
if(ms >= minimumIntervalTime) |
|
498 |
intervalTime = ms; |
|
499 |
else |
|
500 |
intervalTime = minimumIntervalTime; |
|
501 |
} |
|
502 |
||
503 |
int QAudioInputPrivate::notifyInterval() const |
|
504 |
{ |
|
505 |
return intervalTime; |
|
506 |
} |
|
507 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
qint64 QAudioInputPrivate::processedUSecs() const |
0 | 509 |
{ |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
510 |
if (deviceState == QAudio::StoppedState) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
511 |
return 0; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
512 |
qint64 result = qint64(1000000) * totalTimeValue / |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
513 |
(settings.channels()*(settings.sampleSize()/8)) / |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
514 |
settings.frequency(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
515 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
516 |
return result; |
0 | 517 |
} |
518 |
||
519 |
void QAudioInputPrivate::suspend() |
|
520 |
{ |
|
521 |
if(deviceState == QAudio::ActiveState) { |
|
522 |
waveInReset(hWaveIn); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
523 |
deviceState = QAudio::SuspendedState; |
0 | 524 |
emit stateChanged(deviceState); |
525 |
} |
|
526 |
} |
|
527 |
||
528 |
void QAudioInputPrivate::feedback() |
|
529 |
{ |
|
530 |
#ifdef DEBUG_AUDIO |
|
531 |
QTime now(QTime::currentTime()); |
|
532 |
qDebug()<<now.second()<<"s "<<now.msec()<<"ms :feedback() INPUT "<<this; |
|
533 |
#endif |
|
534 |
bytesAvailable = bytesReady(); |
|
535 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
536 |
if(!(deviceState==QAudio::StoppedState||deviceState==QAudio::SuspendedState)) |
0 | 537 |
emit processMore(); |
538 |
} |
|
539 |
||
540 |
bool QAudioInputPrivate::deviceReady() |
|
541 |
{ |
|
542 |
#ifdef DEBUG_AUDIO |
|
543 |
QTime now(QTime::currentTime()); |
|
544 |
qDebug()<<now.second()<<"s "<<now.msec()<<"ms :deviceReady() INPUT"; |
|
545 |
#endif |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
546 |
if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
547 |
return true; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
548 |
|
0 | 549 |
if(pullMode) { |
550 |
// reads some audio data and writes it to QIODevice |
|
551 |
read(0,0); |
|
552 |
} else { |
|
553 |
// emits readyRead() so user will call read() on QIODevice to get some audio data |
|
554 |
InputPrivate* a = qobject_cast<InputPrivate*>(audioSource); |
|
555 |
a->trigger(); |
|
556 |
} |
|
557 |
||
558 |
if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { |
|
559 |
emit notify(); |
|
560 |
elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; |
|
561 |
timeStamp.restart(); |
|
562 |
} |
|
563 |
return true; |
|
564 |
} |
|
565 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
566 |
qint64 QAudioInputPrivate::elapsedUSecs() const |
0 | 567 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
568 |
if (deviceState == QAudio::StoppedState) |
0 | 569 |
return 0; |
570 |
||
571 |
return timeStampOpened.elapsed()*1000; |
|
572 |
} |
|
573 |
||
574 |
void QAudioInputPrivate::reset() |
|
575 |
{ |
|
576 |
close(); |
|
577 |
} |
|
578 |
||
579 |
InputPrivate::InputPrivate(QAudioInputPrivate* audio) |
|
580 |
{ |
|
581 |
audioDevice = qobject_cast<QAudioInputPrivate*>(audio); |
|
582 |
} |
|
583 |
||
584 |
InputPrivate::~InputPrivate() {} |
|
585 |
||
586 |
qint64 InputPrivate::readData( char* data, qint64 len) |
|
587 |
{ |
|
588 |
// push mode, user read() called |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
589 |
if(audioDevice->deviceState != QAudio::ActiveState && |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
590 |
audioDevice->deviceState != QAudio::IdleState) |
0 | 591 |
return 0; |
592 |
// Read in some audio data |
|
593 |
return audioDevice->read(data,len); |
|
594 |
} |
|
595 |
||
596 |
qint64 InputPrivate::writeData(const char* data, qint64 len) |
|
597 |
{ |
|
598 |
Q_UNUSED(data) |
|
599 |
Q_UNUSED(len) |
|
600 |
||
601 |
emit readyRead(); |
|
602 |
return 0; |
|
603 |
} |
|
604 |
||
605 |
void InputPrivate::trigger() |
|
606 |
{ |
|
607 |
emit readyRead(); |
|
608 |
} |
|
609 |
||
610 |
QT_END_NAMESPACE |