author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 16 Apr 2010 15:50:13 +0300 | |
changeset 18 | 2f34d5167611 |
parent 3 | 41300fa6a67c |
child 19 | fcece45ef507 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
#include "qaudiooutput_win32_p.h" |
|
54 |
||
55 |
//#define DEBUG_AUDIO 1 |
|
56 |
||
57 |
QT_BEGIN_NAMESPACE |
|
58 |
||
59 |
static CRITICAL_SECTION waveOutCriticalSection; |
|
60 |
||
61 |
static const int minimumIntervalTime = 50; |
|
62 |
||
63 |
QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray &device, const QAudioFormat& audioFormat): |
|
64 |
settings(audioFormat) |
|
65 |
{ |
|
66 |
bytesAvailable = 0; |
|
67 |
buffer_size = 0; |
|
68 |
period_size = 0; |
|
69 |
m_device = device; |
|
70 |
totalTimeValue = 0; |
|
71 |
intervalTime = 1000; |
|
72 |
audioBuffer = 0; |
|
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 |
finished = false; |
|
78 |
InitializeCriticalSection(&waveOutCriticalSection); |
|
79 |
} |
|
80 |
||
81 |
QAudioOutputPrivate::~QAudioOutputPrivate() |
|
82 |
{ |
|
83 |
EnterCriticalSection(&waveOutCriticalSection); |
|
84 |
finished = true; |
|
85 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
86 |
||
87 |
close(); |
|
88 |
DeleteCriticalSection(&waveOutCriticalSection); |
|
89 |
} |
|
90 |
||
91 |
void CALLBACK QAudioOutputPrivate::waveOutProc( HWAVEOUT hWaveOut, UINT uMsg, |
|
92 |
DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 ) |
|
93 |
{ |
|
94 |
Q_UNUSED(dwParam1) |
|
95 |
Q_UNUSED(dwParam2) |
|
96 |
Q_UNUSED(hWaveOut) |
|
97 |
||
98 |
QAudioOutputPrivate* qAudio; |
|
99 |
qAudio = (QAudioOutputPrivate*)(dwInstance); |
|
100 |
if(!qAudio) |
|
101 |
return; |
|
102 |
||
103 |
switch(uMsg) { |
|
104 |
case WOM_OPEN: |
|
105 |
qAudio->feedback(); |
|
106 |
break; |
|
107 |
case WOM_CLOSE: |
|
108 |
return; |
|
109 |
case WOM_DONE: |
|
110 |
EnterCriticalSection(&waveOutCriticalSection); |
|
111 |
if(qAudio->finished || qAudio->buffer_size == 0 || qAudio->period_size == 0) { |
|
112 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
113 |
return; |
|
114 |
} |
|
115 |
qAudio->waveFreeBlockCount++; |
|
116 |
if(qAudio->waveFreeBlockCount >= qAudio->buffer_size/qAudio->period_size) |
|
117 |
qAudio->waveFreeBlockCount = qAudio->buffer_size/qAudio->period_size; |
|
118 |
qAudio->feedback(); |
|
119 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
120 |
break; |
|
121 |
default: |
|
122 |
return; |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
WAVEHDR* QAudioOutputPrivate::allocateBlocks(int size, int count) |
|
127 |
{ |
|
128 |
int i; |
|
129 |
unsigned char* buffer; |
|
130 |
WAVEHDR* blocks; |
|
131 |
DWORD totalBufferSize = (size + sizeof(WAVEHDR))*count; |
|
132 |
||
133 |
if((buffer=(unsigned char*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, |
|
134 |
totalBufferSize)) == 0) { |
|
135 |
qWarning("QAudioOutput: Memory allocation error"); |
|
136 |
return 0; |
|
137 |
} |
|
138 |
blocks = (WAVEHDR*)buffer; |
|
139 |
buffer += sizeof(WAVEHDR)*count; |
|
140 |
for(i = 0; i < count; i++) { |
|
141 |
blocks[i].dwBufferLength = size; |
|
142 |
blocks[i].lpData = (LPSTR)buffer; |
|
143 |
buffer += size; |
|
144 |
} |
|
145 |
return blocks; |
|
146 |
} |
|
147 |
||
148 |
void QAudioOutputPrivate::freeBlocks(WAVEHDR* blockArray) |
|
149 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
WAVEHDR* blocks = blockArray; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
int count = buffer_size/period_size; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
for(int i = 0; i < count; i++) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
waveOutUnprepareHeader(hWaveOut,&blocks[i], sizeof(WAVEHDR)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
blocks+=sizeof(WAVEHDR); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
} |
0 | 158 |
HeapFree(GetProcessHeap(), 0, blockArray); |
159 |
} |
|
160 |
||
161 |
QAudioFormat QAudioOutputPrivate::format() const |
|
162 |
{ |
|
163 |
return settings; |
|
164 |
} |
|
165 |
||
166 |
QIODevice* QAudioOutputPrivate::start(QIODevice* device) |
|
167 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
if(deviceState != QAudio::StoppedState) |
0 | 169 |
close(); |
170 |
||
171 |
if(!pullMode && audioSource) { |
|
172 |
delete audioSource; |
|
173 |
} |
|
174 |
||
175 |
if(device) { |
|
176 |
//set to pull mode |
|
177 |
pullMode = true; |
|
178 |
audioSource = device; |
|
179 |
deviceState = QAudio::ActiveState; |
|
180 |
} else { |
|
181 |
//set to push mode |
|
182 |
pullMode = false; |
|
183 |
audioSource = new OutputPrivate(this); |
|
184 |
audioSource->open(QIODevice::WriteOnly|QIODevice::Unbuffered); |
|
185 |
deviceState = QAudio::IdleState; |
|
186 |
} |
|
187 |
||
188 |
if( !open() ) |
|
189 |
return 0; |
|
190 |
||
191 |
emit stateChanged(deviceState); |
|
192 |
||
193 |
return audioSource; |
|
194 |
} |
|
195 |
||
196 |
void QAudioOutputPrivate::stop() |
|
197 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
198 |
if(deviceState == QAudio::StoppedState) |
0 | 199 |
return; |
200 |
close(); |
|
201 |
if(!pullMode && audioSource) { |
|
202 |
delete audioSource; |
|
203 |
audioSource = 0; |
|
204 |
} |
|
205 |
emit stateChanged(deviceState); |
|
206 |
} |
|
207 |
||
208 |
bool QAudioOutputPrivate::open() |
|
209 |
{ |
|
210 |
#ifdef DEBUG_AUDIO |
|
211 |
QTime now(QTime::currentTime()); |
|
212 |
qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; |
|
213 |
#endif |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
214 |
if (!(settings.frequency() >= 8000 && settings.frequency() <= 48000)) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
215 |
errorState = QAudio::OpenError; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
216 |
deviceState = QAudio::StoppedState; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
217 |
emit stateChanged(deviceState); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
218 |
qWarning("QAudioOutput: open error, frequency out of range."); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
219 |
return false; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
220 |
} |
0 | 221 |
if(buffer_size == 0) { |
222 |
// Default buffer size, 200ms, default period size is 40ms |
|
223 |
buffer_size = settings.frequency()*settings.channels()*(settings.sampleSize()/8)*0.2; |
|
224 |
period_size = buffer_size/5; |
|
225 |
} else { |
|
226 |
period_size = buffer_size/5; |
|
227 |
} |
|
228 |
waveBlocks = allocateBlocks(period_size, buffer_size/period_size); |
|
229 |
||
230 |
EnterCriticalSection(&waveOutCriticalSection); |
|
231 |
waveFreeBlockCount = buffer_size/period_size; |
|
232 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
233 |
||
234 |
waveCurrentBlock = 0; |
|
235 |
||
236 |
if(audioBuffer == 0) |
|
237 |
audioBuffer = new char[buffer_size]; |
|
238 |
||
239 |
timeStamp.restart(); |
|
240 |
elapsedTimeOffset = 0; |
|
241 |
||
242 |
wfx.nSamplesPerSec = settings.frequency(); |
|
243 |
wfx.wBitsPerSample = settings.sampleSize(); |
|
244 |
wfx.nChannels = settings.channels(); |
|
245 |
wfx.cbSize = 0; |
|
246 |
||
247 |
wfx.wFormatTag = WAVE_FORMAT_PCM; |
|
248 |
wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels; |
|
249 |
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec; |
|
250 |
||
251 |
UINT_PTR devId = WAVE_MAPPER; |
|
252 |
||
253 |
WAVEOUTCAPS woc; |
|
254 |
unsigned long iNumDevs,ii; |
|
255 |
iNumDevs = waveOutGetNumDevs(); |
|
256 |
for(ii=0;ii<iNumDevs;ii++) { |
|
257 |
if(waveOutGetDevCaps(ii, &woc, sizeof(WAVEOUTCAPS)) |
|
258 |
== MMSYSERR_NOERROR) { |
|
259 |
QString tmp; |
|
260 |
tmp = QString::fromUtf16((const unsigned short*)woc.szPname); |
|
261 |
if(tmp.compare(QLatin1String(m_device)) == 0) { |
|
262 |
devId = ii; |
|
263 |
break; |
|
264 |
} |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
if(waveOutOpen(&hWaveOut, devId, &wfx, |
|
269 |
(DWORD_PTR)&waveOutProc, |
|
270 |
(DWORD_PTR) this, |
|
271 |
CALLBACK_FUNCTION) != MMSYSERR_NOERROR) { |
|
272 |
errorState = QAudio::OpenError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
273 |
deviceState = QAudio::StoppedState; |
0 | 274 |
emit stateChanged(deviceState); |
275 |
qWarning("QAudioOutput: open error"); |
|
276 |
return false; |
|
277 |
} |
|
278 |
||
279 |
totalTimeValue = 0; |
|
280 |
timeStampOpened.restart(); |
|
281 |
elapsedTimeOffset = 0; |
|
282 |
||
283 |
errorState = QAudio::NoError; |
|
284 |
if(pullMode) { |
|
285 |
deviceState = QAudio::ActiveState; |
|
286 |
QTimer::singleShot(10, this, SLOT(feedback())); |
|
287 |
} else |
|
288 |
deviceState = QAudio::IdleState; |
|
289 |
||
290 |
return true; |
|
291 |
} |
|
292 |
||
293 |
void QAudioOutputPrivate::close() |
|
294 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
295 |
if(deviceState == QAudio::StoppedState) |
0 | 296 |
return; |
297 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
298 |
deviceState = QAudio::StoppedState; |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
299 |
errorState = QAudio::NoError; |
0 | 300 |
int delay = (buffer_size-bytesFree())*1000/(settings.frequency() |
301 |
*settings.channels()*(settings.sampleSize()/8)); |
|
302 |
waveOutReset(hWaveOut); |
|
303 |
Sleep(delay+10); |
|
304 |
||
305 |
freeBlocks(waveBlocks); |
|
306 |
waveOutClose(hWaveOut); |
|
307 |
delete [] audioBuffer; |
|
308 |
audioBuffer = 0; |
|
309 |
buffer_size = 0; |
|
310 |
} |
|
311 |
||
312 |
int QAudioOutputPrivate::bytesFree() const |
|
313 |
{ |
|
314 |
int buf; |
|
315 |
buf = waveFreeBlockCount*period_size; |
|
316 |
||
317 |
return buf; |
|
318 |
} |
|
319 |
||
320 |
int QAudioOutputPrivate::periodSize() const |
|
321 |
{ |
|
322 |
return period_size; |
|
323 |
} |
|
324 |
||
325 |
void QAudioOutputPrivate::setBufferSize(int value) |
|
326 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
327 |
if(deviceState == QAudio::StoppedState) |
0 | 328 |
buffer_size = value; |
329 |
} |
|
330 |
||
331 |
int QAudioOutputPrivate::bufferSize() const |
|
332 |
{ |
|
333 |
return buffer_size; |
|
334 |
} |
|
335 |
||
336 |
void QAudioOutputPrivate::setNotifyInterval(int ms) |
|
337 |
{ |
|
338 |
if(ms >= minimumIntervalTime) |
|
339 |
intervalTime = ms; |
|
340 |
else |
|
341 |
intervalTime = minimumIntervalTime; |
|
342 |
} |
|
343 |
||
344 |
int QAudioOutputPrivate::notifyInterval() const |
|
345 |
{ |
|
346 |
return intervalTime; |
|
347 |
} |
|
348 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
349 |
qint64 QAudioOutputPrivate::processedUSecs() const |
0 | 350 |
{ |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
351 |
if (deviceState == QAudio::StoppedState) |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
352 |
return 0; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
353 |
qint64 result = qint64(1000000) * totalTimeValue / |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
354 |
(settings.channels()*(settings.sampleSize()/8)) / |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
355 |
settings.frequency(); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
356 |
|
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
357 |
return result; |
0 | 358 |
} |
359 |
||
360 |
qint64 QAudioOutputPrivate::write( const char *data, qint64 len ) |
|
361 |
{ |
|
362 |
// Write out some audio data |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
363 |
if (deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState) |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
364 |
return 0; |
0 | 365 |
|
366 |
char* p = (char*)data; |
|
367 |
int l = (int)len; |
|
368 |
||
369 |
WAVEHDR* current; |
|
370 |
int remain; |
|
371 |
current = &waveBlocks[waveCurrentBlock]; |
|
372 |
while(l > 0) { |
|
373 |
EnterCriticalSection(&waveOutCriticalSection); |
|
374 |
if(waveFreeBlockCount==0) { |
|
375 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
376 |
break; |
|
377 |
} |
|
378 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
379 |
||
380 |
if(current->dwFlags & WHDR_PREPARED) |
|
381 |
waveOutUnprepareHeader(hWaveOut, current, sizeof(WAVEHDR)); |
|
382 |
||
383 |
if(l < period_size) |
|
384 |
remain = l; |
|
385 |
else |
|
386 |
remain = period_size; |
|
387 |
memcpy(current->lpData, p, remain); |
|
388 |
||
389 |
l -= remain; |
|
390 |
p += remain; |
|
391 |
current->dwBufferLength = remain; |
|
392 |
waveOutPrepareHeader(hWaveOut, current, sizeof(WAVEHDR)); |
|
393 |
waveOutWrite(hWaveOut, current, sizeof(WAVEHDR)); |
|
394 |
||
395 |
EnterCriticalSection(&waveOutCriticalSection); |
|
396 |
waveFreeBlockCount--; |
|
397 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
398 |
#ifdef DEBUG_AUDIO |
|
399 |
EnterCriticalSection(&waveOutCriticalSection); |
|
400 |
qDebug("write out l=%d, waveFreeBlockCount=%d", |
|
401 |
current->dwBufferLength,waveFreeBlockCount); |
|
402 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
403 |
#endif |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
404 |
totalTimeValue += current->dwBufferLength; |
0 | 405 |
waveCurrentBlock++; |
406 |
waveCurrentBlock %= buffer_size/period_size; |
|
407 |
current = &waveBlocks[waveCurrentBlock]; |
|
408 |
current->dwUser = 0; |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
409 |
errorState = QAudio::NoError; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
410 |
if (deviceState != QAudio::ActiveState) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
411 |
deviceState = QAudio::ActiveState; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
412 |
emit stateChanged(deviceState); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
413 |
} |
0 | 414 |
} |
415 |
return (len-l); |
|
416 |
} |
|
417 |
||
418 |
void QAudioOutputPrivate::resume() |
|
419 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
420 |
if(deviceState == QAudio::SuspendedState) { |
0 | 421 |
deviceState = QAudio::ActiveState; |
422 |
errorState = QAudio::NoError; |
|
423 |
waveOutRestart(hWaveOut); |
|
424 |
QTimer::singleShot(10, this, SLOT(feedback())); |
|
425 |
emit stateChanged(deviceState); |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
void QAudioOutputPrivate::suspend() |
|
430 |
{ |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
431 |
if(deviceState == QAudio::ActiveState || deviceState == QAudio::IdleState) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
432 |
int delay = (buffer_size-bytesFree())*1000/(settings.frequency() |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
433 |
*settings.channels()*(settings.sampleSize()/8)); |
0 | 434 |
waveOutPause(hWaveOut); |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
435 |
Sleep(delay+10); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
436 |
deviceState = QAudio::SuspendedState; |
0 | 437 |
errorState = QAudio::NoError; |
438 |
emit stateChanged(deviceState); |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
void QAudioOutputPrivate::feedback() |
|
443 |
{ |
|
444 |
#ifdef DEBUG_AUDIO |
|
445 |
QTime now(QTime::currentTime()); |
|
446 |
qDebug()<<now.second()<<"s "<<now.msec()<<"ms :feedback()"; |
|
447 |
#endif |
|
448 |
bytesAvailable = bytesFree(); |
|
449 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
450 |
if(!(deviceState==QAudio::StoppedState||deviceState==QAudio::SuspendedState)) { |
0 | 451 |
if(bytesAvailable >= period_size) |
452 |
QMetaObject::invokeMethod(this, "deviceReady", Qt::QueuedConnection); |
|
453 |
} |
|
454 |
} |
|
455 |
||
456 |
bool QAudioOutputPrivate::deviceReady() |
|
457 |
{ |
|
458 |
if(pullMode) { |
|
459 |
int chunks = bytesAvailable/period_size; |
|
460 |
#ifdef DEBUG_AUDIO |
|
461 |
qDebug()<<"deviceReady() avail="<<bytesAvailable<<" bytes, period size="<<period_size<<" bytes"; |
|
462 |
qDebug()<<"deviceReady() no. of chunks that can fit ="<<chunks<<", chunks in bytes ="<<chunks*period_size; |
|
463 |
#endif |
|
464 |
bool startup = false; |
|
465 |
if(totalTimeValue == 0) |
|
466 |
startup = true; |
|
467 |
||
468 |
bool full=false; |
|
469 |
EnterCriticalSection(&waveOutCriticalSection); |
|
470 |
if(waveFreeBlockCount==0) full = true; |
|
471 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
472 |
if (full){ |
|
473 |
#ifdef DEBUG_AUDIO |
|
474 |
qDebug() << "Skipping data as unable to write"; |
|
475 |
#endif |
|
476 |
if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime ) { |
|
477 |
emit notify(); |
|
478 |
elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; |
|
479 |
timeStamp.restart(); |
|
480 |
} |
|
481 |
return true; |
|
482 |
} |
|
483 |
||
484 |
if(startup) |
|
485 |
waveOutPause(hWaveOut); |
|
486 |
int input = period_size*chunks; |
|
487 |
int l = audioSource->read(audioBuffer,input); |
|
488 |
if(l > 0) { |
|
489 |
int out= write(audioBuffer,l); |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
490 |
if(out > 0) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
491 |
if (deviceState != QAudio::ActiveState) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
492 |
deviceState = QAudio::ActiveState; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
493 |
emit stateChanged(deviceState); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
494 |
} |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
495 |
} |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
496 |
if ( out < l) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
497 |
// Didnt write all data |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
498 |
audioSource->seek(audioSource->pos()-(l-out)); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
499 |
} |
0 | 500 |
if(startup) |
501 |
waveOutRestart(hWaveOut); |
|
502 |
} else if(l == 0) { |
|
503 |
bytesAvailable = bytesFree(); |
|
504 |
||
505 |
int check = 0; |
|
506 |
EnterCriticalSection(&waveOutCriticalSection); |
|
507 |
check = waveFreeBlockCount; |
|
508 |
LeaveCriticalSection(&waveOutCriticalSection); |
|
509 |
if(check == buffer_size/period_size) { |
|
510 |
errorState = QAudio::UnderrunError; |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
511 |
if (deviceState != QAudio::IdleState) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
512 |
deviceState = QAudio::IdleState; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
513 |
emit stateChanged(deviceState); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
514 |
} |
0 | 515 |
} |
516 |
||
517 |
} else if(l < 0) { |
|
518 |
bytesAvailable = bytesFree(); |
|
519 |
errorState = QAudio::IOError; |
|
520 |
} |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
521 |
} else { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
522 |
int buffered; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
523 |
EnterCriticalSection(&waveOutCriticalSection); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
524 |
buffered = waveFreeBlockCount; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
525 |
LeaveCriticalSection(&waveOutCriticalSection); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
526 |
errorState = QAudio::UnderrunError; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
527 |
if (buffered >= buffer_size/period_size && deviceState == QAudio::ActiveState) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
528 |
deviceState = QAudio::IdleState; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
529 |
emit stateChanged(deviceState); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
530 |
} |
0 | 531 |
} |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
532 |
if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState) |
0 | 533 |
return true; |
534 |
||
535 |
if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { |
|
536 |
emit notify(); |
|
537 |
elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; |
|
538 |
timeStamp.restart(); |
|
539 |
} |
|
540 |
||
541 |
return true; |
|
542 |
} |
|
543 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
544 |
qint64 QAudioOutputPrivate::elapsedUSecs() const |
0 | 545 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
546 |
if (deviceState == QAudio::StoppedState) |
0 | 547 |
return 0; |
548 |
||
549 |
return timeStampOpened.elapsed()*1000; |
|
550 |
} |
|
551 |
||
552 |
QAudio::Error QAudioOutputPrivate::error() const |
|
553 |
{ |
|
554 |
return errorState; |
|
555 |
} |
|
556 |
||
557 |
QAudio::State QAudioOutputPrivate::state() const |
|
558 |
{ |
|
559 |
return deviceState; |
|
560 |
} |
|
561 |
||
562 |
void QAudioOutputPrivate::reset() |
|
563 |
{ |
|
564 |
close(); |
|
565 |
} |
|
566 |
||
567 |
OutputPrivate::OutputPrivate(QAudioOutputPrivate* audio) |
|
568 |
{ |
|
569 |
audioDevice = qobject_cast<QAudioOutputPrivate*>(audio); |
|
570 |
} |
|
571 |
||
572 |
OutputPrivate::~OutputPrivate() {} |
|
573 |
||
574 |
qint64 OutputPrivate::readData( char* data, qint64 len) |
|
575 |
{ |
|
576 |
Q_UNUSED(data) |
|
577 |
Q_UNUSED(len) |
|
578 |
||
579 |
return 0; |
|
580 |
} |
|
581 |
||
582 |
qint64 OutputPrivate::writeData(const char* data, qint64 len) |
|
583 |
{ |
|
584 |
int retry = 0; |
|
585 |
qint64 written = 0; |
|
586 |
||
587 |
if((audioDevice->deviceState == QAudio::ActiveState) |
|
588 |
||(audioDevice->deviceState == QAudio::IdleState)) { |
|
589 |
qint64 l = len; |
|
590 |
while(written < l) { |
|
591 |
int chunk = audioDevice->write(data+written,(l-written)); |
|
592 |
if(chunk <= 0) |
|
593 |
retry++; |
|
594 |
else |
|
595 |
written+=chunk; |
|
596 |
||
597 |
if(retry > 10) |
|
598 |
return written; |
|
599 |
} |
|
600 |
audioDevice->deviceState = QAudio::ActiveState; |
|
601 |
} |
|
602 |
return written; |
|
603 |
} |
|
604 |
||
605 |
QT_END_NAMESPACE |