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