Diffstat (limited to 'core/multimedia/opieplayer/audiodevice.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/audiodevice.cpp | 153 |
1 files changed, 69 insertions, 84 deletions
diff --git a/core/multimedia/opieplayer/audiodevice.cpp b/core/multimedia/opieplayer/audiodevice.cpp index 8861015..59136af 100644 --- a/core/multimedia/opieplayer/audiodevice.cpp +++ b/core/multimedia/opieplayer/audiodevice.cpp | |||
@@ -1,44 +1,48 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | // L.J.Potter added better error code Fri 02-15-2002 14:37:47 | ||
21 | |||
22 | |||
20 | #include <stdlib.h> | 23 | #include <stdlib.h> |
24 | #include <stdio.h> | ||
21 | #include <qpe/qpeapplication.h> | 25 | #include <qpe/qpeapplication.h> |
22 | #include <qpe/config.h> | 26 | #include <qpe/config.h> |
23 | #include "audiodevice.h" | 27 | #include "audiodevice.h" |
24 | 28 | ||
25 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) | 29 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) |
26 | #include "qpe/qcopenvelope_qws.h" | 30 | #include "qpe/qcopenvelope_qws.h" |
27 | #endif | 31 | #endif |
28 | 32 | ||
29 | #ifdef Q_WS_WIN | 33 | #ifdef Q_WS_WIN |
30 | #include <windows.h> | 34 | #include <windows.h> |
31 | #include <mmsystem.h> | 35 | #include <mmsystem.h> |
32 | #include <mmreg.h> | 36 | #include <mmreg.h> |
33 | #endif | 37 | #endif |
34 | 38 | ||
35 | #if defined(Q_WS_X11) || defined(Q_WS_QWS) | 39 | #if defined(Q_WS_X11) || defined(Q_WS_QWS) |
36 | #include <fcntl.h> | 40 | #include <fcntl.h> |
37 | #include <sys/ioctl.h> | 41 | #include <sys/ioctl.h> |
38 | #include <sys/soundcard.h> | 42 | #include <sys/soundcard.h> |
39 | #include <sys/stat.h> | 43 | #include <sys/stat.h> |
40 | #include <sys/time.h> | 44 | #include <sys/time.h> |
41 | #include <sys/types.h> | 45 | #include <sys/types.h> |
42 | #include <unistd.h> | 46 | #include <unistd.h> |
43 | #endif | 47 | #endif |
44 | 48 | ||
@@ -84,303 +88,284 @@ public: | |||
84 | #endif | 88 | #endif |
85 | 89 | ||
86 | 90 | ||
87 | int AudioDevicePrivate::dspFd = 0; | 91 | int AudioDevicePrivate::dspFd = 0; |
88 | bool AudioDevicePrivate::muted = FALSE; | 92 | bool AudioDevicePrivate::muted = FALSE; |
89 | unsigned int AudioDevicePrivate::leftVolume = 0; | 93 | unsigned int AudioDevicePrivate::leftVolume = 0; |
90 | unsigned int AudioDevicePrivate::rightVolume = 0; | 94 | unsigned int AudioDevicePrivate::rightVolume = 0; |
91 | 95 | ||
92 | 96 | ||
93 | void AudioDevice::getVolume( unsigned int& leftVolume, unsigned int& rightVolume, bool &muted ) { | 97 | void AudioDevice::getVolume( unsigned int& leftVolume, unsigned int& rightVolume, bool &muted ) { |
94 | muted = AudioDevicePrivate::muted; | 98 | muted = AudioDevicePrivate::muted; |
95 | unsigned int volume; | 99 | unsigned int volume; |
96 | #ifdef Q_OS_WIN32 | 100 | #ifdef Q_OS_WIN32 |
97 | HWAVEOUT handle; | 101 | HWAVEOUT handle; |
98 | WAVEFORMATEX formatData; | 102 | WAVEFORMATEX formatData; |
99 | formatData.cbSize = sizeof(WAVEFORMATEX); | 103 | formatData.cbSize = sizeof(WAVEFORMATEX); |
100 | formatData.wFormatTag = WAVE_FORMAT_PCM; | 104 | formatData.wFormatTag = WAVE_FORMAT_PCM; |
101 | formatData.nAvgBytesPerSec = 4 * 44000; | 105 | formatData.nAvgBytesPerSec = 4 * 44000; |
102 | formatData.nBlockAlign = 4; | 106 | formatData.nBlockAlign = 4; |
103 | formatData.nChannels = 2; | 107 | formatData.nChannels = 2; |
104 | formatData.nSamplesPerSec = 44000; | 108 | formatData.nSamplesPerSec = 44000; |
105 | formatData.wBitsPerSample = 16; | 109 | formatData.wBitsPerSample = 16; |
106 | waveOutOpen(&handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL); | 110 | waveOutOpen(&handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL); |
107 | if ( waveOutGetVolume( handle, (LPDWORD)&volume ) ) | 111 | if ( waveOutGetVolume( handle, (LPDWORD)&volume ) ) |
108 | qDebug( "get volume of audio device failed" ); | 112 | qDebug( "get volume of audio device failed" ); |
109 | waveOutClose( handle ); | 113 | waveOutClose( handle ); |
110 | leftVolume = volume & 0xFFFF; | 114 | leftVolume = volume & 0xFFFF; |
111 | rightVolume = volume >> 16; | 115 | rightVolume = volume >> 16; |
112 | #else | 116 | #else |
113 | int mixerHandle = open( "/dev/mixer", O_RDWR ); | 117 | int mixerHandle = open( "/dev/mixer", O_RDWR ); |
114 | if ( mixerHandle >= 0 ) { | 118 | if ( mixerHandle >= 0 ) { |
115 | ioctl( mixerHandle, MIXER_READ(0), &volume ); | 119 | if(ioctl( mixerHandle, MIXER_READ(0), &volume )==-1) |
120 | perror("ioctl(\"MIXER_READ\")"); | ||
116 | close( mixerHandle ); | 121 | close( mixerHandle ); |
117 | } else | 122 | } else |
118 | qDebug( "get volume of audio device failed" ); | 123 | perror("open(\"/dev/mixer\")"); |
119 | leftVolume = ((volume & 0x00FF) << 16) / 101; | 124 | leftVolume = ((volume & 0x00FF) << 16) / 101; |
120 | rightVolume = ((volume & 0xFF00) << 8) / 101; | 125 | rightVolume = ((volume & 0xFF00) << 8) / 101; |
121 | #endif | 126 | #endif |
122 | } | 127 | } |
123 | 128 | ||
124 | 129 | ||
125 | void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume, bool muted ) { | 130 | void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume, bool muted ) { |
126 | AudioDevicePrivate::muted = muted; | 131 | AudioDevicePrivate::muted = muted; |
127 | if ( muted ) { | 132 | if ( muted ) { |
128 | AudioDevicePrivate::leftVolume = leftVolume; | 133 | AudioDevicePrivate::leftVolume = leftVolume; |
129 | AudioDevicePrivate::rightVolume = rightVolume; | 134 | AudioDevicePrivate::rightVolume = rightVolume; |
130 | leftVolume = 0; | 135 | leftVolume = 0; |
131 | rightVolume = 0; | 136 | rightVolume = 0; |
132 | } else { | 137 | } else { |
133 | leftVolume = ( (int) leftVolume < 0 ) ? 0 : (( leftVolume > 0xFFFF ) ? 0xFFFF : leftVolume ); | 138 | leftVolume = ( (int) leftVolume < 0 ) ? 0 : (( leftVolume > 0xFFFF ) ? 0xFFFF : leftVolume ); |
134 | rightVolume = ( (int)rightVolume < 0 ) ? 0 : (( rightVolume > 0xFFFF ) ? 0xFFFF : rightVolume ); | 139 | rightVolume = ( (int)rightVolume < 0 ) ? 0 : (( rightVolume > 0xFFFF ) ? 0xFFFF : rightVolume ); |
135 | } | 140 | } |
136 | #ifdef Q_OS_WIN32 | 141 | #ifdef Q_OS_WIN32 |
137 | HWAVEOUT handle; | 142 | HWAVEOUT handle; |
138 | WAVEFORMATEX formatData; | 143 | WAVEFORMATEX formatData; |
139 | formatData.cbSize = sizeof(WAVEFORMATEX); | 144 | formatData.cbSize = sizeof(WAVEFORMATEX); |
140 | formatData.wFormatTag = WAVE_FORMAT_PCM; | 145 | formatData.wFormatTag = WAVE_FORMAT_PCM; |
141 | formatData.nAvgBytesPerSec = 4 * 44000; | 146 | formatData.nAvgBytesPerSec = 4 * 44000; |
142 | formatData.nBlockAlign = 4; | 147 | formatData.nBlockAlign = 4; |
143 | formatData.nChannels = 2; | 148 | formatData.nChannels = 2; |
144 | formatData.nSamplesPerSec = 44000; | 149 | formatData.nSamplesPerSec = 44000; |
145 | formatData.wBitsPerSample = 16; | 150 | formatData.wBitsPerSample = 16; |
146 | waveOutOpen(&handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL); | 151 | waveOutOpen(&handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL); |
147 | unsigned int volume = (rightVolume << 16) | leftVolume; | 152 | unsigned int volume = (rightVolume << 16) | leftVolume; |
148 | if ( waveOutSetVolume( handle, volume ) ) | 153 | if ( waveOutSetVolume( handle, volume ) ) |
149 | qDebug( "set volume of audio device failed" ); | 154 | qDebug( "set volume of audio device failed" ); |
150 | waveOutClose( handle ); | 155 | waveOutClose( handle ); |
151 | #else | 156 | #else |
152 | // Volume can be from 0 to 100 which is 101 distinct values | 157 | // Volume can be from 0 to 100 which is 101 distinct values |
153 | unsigned int rV = (rightVolume * 101) >> 16; | 158 | unsigned int rV = (rightVolume * 101) >> 16; |
154 | 159 | ||
155 | # if 0 | 160 | # if 0 |
156 | unsigned int lV = (leftVolume * 101) >> 16; | 161 | unsigned int lV = (leftVolume * 101) >> 16; |
157 | unsigned int volume = ((rV << 8) & 0xFF00) | (lV & 0x00FF); | 162 | unsigned int volume = ((rV << 8) & 0xFF00) | (lV & 0x00FF); |
158 | int mixerHandle = 0; | 163 | int mixerHandle = 0; |
159 | if ( ( mixerHandle = open( "/dev/mixer", O_RDWR ) ) >= 0 ) { | 164 | if ( ( mixerHandle = open( "/dev/mixer", O_RDWR ) ) >= 0 ) { |
160 | ioctl( mixerHandle, MIXER_WRITE(0), &volume ); | 165 | if(ioctl( mixerHandle, MIXER_WRITE(0), &volume ) ==-1) |
166 | perror("ioctl(\"MIXER_WRITE\")"); | ||
161 | close( mixerHandle ); | 167 | close( mixerHandle ); |
162 | } else | 168 | } else |
163 | qDebug( "set volume of audio device failed" ); | 169 | perror("open(\"/dev/mixer\")"); |
170 | |||
164 | # else | 171 | # else |
165 | // This is the way this has to be done now I guess, doesn't allow for | 172 | // This is the way this has to be done now I guess, doesn't allow for |
166 | // independant right and left channel setting, or setting for different outputs | 173 | // independant right and left channel setting, or setting for different outputs |
167 | Config cfg("Sound"); | 174 | Config cfg("Sound"); |
168 | cfg.setGroup("System"); | 175 | cfg.setGroup("System"); |
169 | cfg.writeEntry("Volume",(int)rV); | 176 | cfg.writeEntry("Volume",(int)rV); |
170 | # endif | 177 | # endif |
171 | 178 | ||
172 | #endif | 179 | #endif |
173 | // qDebug( "setting volume to: 0x%x", volume ); | 180 | // qDebug( "setting volume to: 0x%x", volume ); |
174 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) | 181 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) |
175 | // Send notification that the volume has changed | 182 | // Send notification that the volume has changed |
176 | QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << muted; | 183 | QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << muted; |
177 | #endif | 184 | #endif |
178 | } | 185 | } |
179 | 186 | ||
180 | 187 | ||
181 | 188 | ||
182 | 189 | ||
183 | AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) { | 190 | AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) { |
184 | d = new AudioDevicePrivate; | 191 | d = new AudioDevicePrivate; |
185 | d->frequency = f; | 192 | d->frequency = f; |
186 | d->channels = chs; | 193 | d->channels = chs; |
187 | d->bytesPerSample = bps; | 194 | d->bytesPerSample = bps; |
195 | qDebug("%d",bps); | ||
196 | int format=0; | ||
197 | if( bps == 8) format = AFMT_U8; | ||
198 | else if( bps <= 0) format = AFMT_S16_LE; | ||
199 | else format = AFMT_S16_LE; | ||
188 | 200 | ||
201 | qDebug("AD- freq %d, channels %d, b/sample %d, bitrate %d",f,chs,bps,format); | ||
189 | connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( volumeChanged(bool) ) ); | 202 | connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( volumeChanged(bool) ) ); |
190 | 203 | ||
191 | #ifdef Q_OS_WIN32 | ||
192 | UINT result; | ||
193 | WAVEFORMATEX formatData; | ||
194 | formatData.cbSize = sizeof(WAVEFORMATEX); | ||
195 | /* | ||
196 | // Other possible formats windows supports | ||
197 | formatData.wFormatTag = WAVE_FORMAT_MPEG; | ||
198 | formatData.wFormatTag = WAVE_FORMAT_MPEGLAYER3; | ||
199 | formatData.wFormatTag = WAVE_FORMAT_ADPCM; | ||
200 | */ | ||
201 | formatData.wFormatTag = WAVE_FORMAT_PCM; | ||
202 | formatData.nAvgBytesPerSec = bps * chs * f; | ||
203 | formatData.nBlockAlign = bps * chs; | ||
204 | formatData.nChannels = chs; | ||
205 | formatData.nSamplesPerSec = f; | ||
206 | formatData.wBitsPerSample = bps * 8; | ||
207 | // Open a waveform device for output | ||
208 | if (result = waveOutOpen((LPHWAVEOUT)&d->handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL)) { | ||
209 | QString errorMsg = "error opening audio device.\nReason: %i - "; | ||
210 | switch (result) { | ||
211 | case MMSYSERR_ALLOCATED:errorMsg += "Specified resource is already allocated."; break; | ||
212 | case MMSYSERR_BADDEVICEID:errorMsg += "Specified device identifier is out of range."; break; | ||
213 | case MMSYSERR_NODRIVER:errorMsg += "No device driver is present."; break; | ||
214 | case MMSYSERR_NOMEM:errorMsg += "Unable to allocate or lock memory."; break; | ||
215 | case WAVERR_BADFORMAT:errorMsg += "Attempted to open with an unsupported waveform-audio format."; break; | ||
216 | case WAVERR_SYNC: errorMsg += "The device is synchronous but waveOutOpen was called without using the WAVE_ALLOWSYNC flag."; break; | ||
217 | default: errorMsg += "Undefined error"; break; | ||
218 | } | ||
219 | qDebug( errorMsg, result ); | ||
220 | } | ||
221 | |||
222 | d->bufferSize = sound_fragment_bytes; | ||
223 | #else | ||
224 | 204 | ||
225 | int fragments = 0x10000 * 8 + sound_fragment_shift; | 205 | int fragments = 0x10000 * 8 + sound_fragment_shift; |
226 | int format = AFMT_S16_LE; | ||
227 | int capabilities = 0; | 206 | int capabilities = 0; |
228 | 207 | ||
229 | #ifdef KEEP_DEVICE_OPEN | 208 | #ifdef KEEP_DEVICE_OPEN |
230 | if ( AudioDevicePrivate::dspFd == 0 ) { | 209 | if ( AudioDevicePrivate::dspFd == 0 ) { |
231 | #endif | 210 | #endif |
232 | if ( ( d->handle = ::open( "/dev/dsp", O_WRONLY ) ) < 0 ) { | 211 | if ( ( d->handle = ::open( "/dev/dsp", O_WRONLY ) ) < 0 ) { |
233 | qDebug( "error opening audio device /dev/dsp, sending data to /dev/null instead" ); | 212 | perror("open(\"/dev/dsp\") sending to /dev/null instead"); |
234 | d->handle = ::open( "/dev/null", O_WRONLY ); | 213 | d->handle = ::open( "/dev/null", O_WRONLY ); |
235 | } | 214 | } |
236 | #ifdef KEEP_DEVICE_OPEN | 215 | #ifdef KEEP_DEVICE_OPEN |
237 | AudioDevicePrivate::dspFd = d->handle; | 216 | AudioDevicePrivate::dspFd = d->handle; |
238 | } else { | 217 | } else { |
239 | d->handle = AudioDevicePrivate::dspFd; | 218 | d->handle = AudioDevicePrivate::dspFd; |
240 | } | 219 | } |
241 | #endif | 220 | #endif |
242 | 221 | ||
243 | ioctl( d->handle, SNDCTL_DSP_GETCAPS, &capabilities ); | 222 | if(ioctl( d->handle, SNDCTL_DSP_GETCAPS, &capabilities )==-1) |
244 | ioctl( d->handle, SNDCTL_DSP_SETFRAGMENT, &fragments ); | 223 | perror("ioctl(\"SNDCTL_DSP_GETCAPS\")"); |
245 | ioctl( d->handle, SNDCTL_DSP_SETFMT, &format ); | 224 | if(ioctl( d->handle, SNDCTL_DSP_SETFRAGMENT, &fragments )==-1) |
246 | ioctl( d->handle, SNDCTL_DSP_SPEED, &d->frequency ); | 225 | perror("ioctl(\"SNDCTL_DSP_SETFRAGMENT\")"); |
226 | if(ioctl( d->handle, SNDCTL_DSP_SETFMT, & format )==-1) | ||
227 | perror("ioctl(\"SNDCTL_DSP_SETFMT\")"); | ||
228 | qDebug("freq %d", d->frequency); | ||
229 | if(ioctl( d->handle, SNDCTL_DSP_SPEED, &d->frequency )==-1) | ||
230 | perror("ioctl(\"SNDCTL_DSP_SPEED\")"); | ||
231 | qDebug("channels %d",d->channels); | ||
247 | if ( ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ) == -1 ) { | 232 | if ( ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ) == -1 ) { |
248 | d->channels = ( d->channels == 1 ) ? 2 : d->channels; | 233 | d->channels = ( d->channels == 1 ) ? 2 : d->channels; |
249 | ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ); | 234 | if(ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels )==-1) |
235 | perror("ioctl(\"SNDCTL_DSP_CHANNELS\")"); | ||
250 | } | 236 | } |
251 | 237 | ||
252 | d->bufferSize = sound_fragment_bytes; | 238 | d->bufferSize = sound_fragment_bytes; |
253 | d->unwrittenBuffer = new char[d->bufferSize]; | 239 | d->unwrittenBuffer = new char[d->bufferSize]; |
254 | d->unwritten = 0; | 240 | d->unwritten = 0; |
255 | d->can_GETOSPACE = TRUE; // until we find otherwise | 241 | d->can_GETOSPACE = TRUE; // until we find otherwise |
256 | 242 | ||
257 | //if ( chs != d->channels ) qDebug( "Wanted %d, got %d channels", chs, d->channels ); | 243 | //if ( chs != d->channels ) qDebug( "Wanted %d, got %d channels", chs, d->channels ); |
258 | //if ( f != d->frequency ) qDebug( "wanted %dHz, got %dHz", f, d->frequency ); | 244 | //if ( f != d->frequency ) qDebug( "wanted %dHz, got %dHz", f, d->frequency ); |
259 | //if ( capabilities & DSP_CAP_BATCH ) qDebug( "Sound card has local buffer" ); | 245 | //if ( capabilities & DSP_CAP_BATCH ) qDebug( "Sound card has local buffer" ); |
260 | //if ( capabilities & DSP_CAP_REALTIME )qDebug( "Sound card has realtime sync" ); | 246 | //if ( capabilities & DSP_CAP_REALTIME )qDebug( "Sound card has realtime sync" ); |
261 | //if ( capabilities & DSP_CAP_TRIGGER ) qDebug( "Sound card has precise trigger" ); | 247 | //if ( capabilities & DSP_CAP_TRIGGER ) qDebug( "Sound card has precise trigger" ); |
262 | //if ( capabilities & DSP_CAP_MMAP ) qDebug( "Sound card can mmap" ); | 248 | //if ( capabilities & DSP_CAP_MMAP ) qDebug( "Sound card can mmap" ); |
263 | #endif | ||
264 | } | 249 | } |
265 | 250 | ||
266 | 251 | ||
267 | AudioDevice::~AudioDevice() { | 252 | AudioDevice::~AudioDevice() { |
268 | #ifdef Q_OS_WIN32 | 253 | #ifdef Q_OS_WIN32 |
269 | waveOutClose( (HWAVEOUT)d->handle ); | 254 | waveOutClose( (HWAVEOUT)d->handle ); |
270 | #else | 255 | #else |
271 | # ifndef KEEP_DEVICE_OPEN | 256 | # ifndef KEEP_DEVICE_OPEN |
272 | close( d->handle ); // Now it should be safe to shut the handle | 257 | close( d->handle ); // Now it should be safe to shut the handle |
273 | # endif | 258 | # endif |
274 | delete d->unwrittenBuffer; | 259 | delete d->unwrittenBuffer; |
275 | delete d; | 260 | delete d; |
276 | #endif | 261 | #endif |
277 | } | 262 | } |
278 | 263 | ||
279 | 264 | ||
280 | void AudioDevice::volumeChanged( bool muted ) | 265 | void AudioDevice::volumeChanged( bool muted ) |
281 | { | 266 | { |
282 | AudioDevicePrivate::muted = muted; | 267 | AudioDevicePrivate::muted = muted; |
283 | } | 268 | } |
284 | 269 | ||
285 | 270 | ||
286 | void AudioDevice::write( char *buffer, unsigned int length ) | 271 | void AudioDevice::write( char *buffer, unsigned int length ) |
287 | { | 272 | { |
288 | #ifdef Q_OS_WIN32 | 273 | #ifdef Q_OS_WIN32 |
289 | // returns immediately and (to be implemented) emits completedIO() when finished writing | 274 | // returns immediately and (to be implemented) emits completedIO() when finished writing |
290 | WAVEHDR *lpWaveHdr = (WAVEHDR *)malloc( sizeof(WAVEHDR) ); | 275 | WAVEHDR *lpWaveHdr = (WAVEHDR *)malloc( sizeof(WAVEHDR) ); |
291 | // maybe the buffer should be copied so that this fool proof, but its a performance hit | 276 | // maybe the buffer should be copied so that this fool proof, but its a performance hit |
292 | lpWaveHdr->lpData = buffer; | 277 | lpWaveHdr->lpData = buffer; |
293 | lpWaveHdr->dwBufferLength = length; | 278 | lpWaveHdr->dwBufferLength = length; |
294 | lpWaveHdr->dwFlags = 0L; | 279 | lpWaveHdr->dwFlags = 0L; |
295 | lpWaveHdr->dwLoops = 0L; | 280 | lpWaveHdr->dwLoops = 0L; |
296 | waveOutPrepareHeader( (HWAVEOUT)d->handle, lpWaveHdr, sizeof(WAVEHDR) ); | 281 | waveOutPrepareHeader( (HWAVEOUT)d->handle, lpWaveHdr, sizeof(WAVEHDR) ); |
297 | // waveOutWrite returns immediately. the data is sent in the background. | 282 | // waveOutWrite returns immediately. the data is sent in the background. |
298 | if ( waveOutWrite( (HWAVEOUT)d->handle, lpWaveHdr, sizeof(WAVEHDR) ) ) | 283 | if ( waveOutWrite( (HWAVEOUT)d->handle, lpWaveHdr, sizeof(WAVEHDR) ) ) |
299 | qDebug( "failed to write block to audio device" ); | 284 | qDebug( "failed to write block to audio device" ); |
300 | // emit completedIO(); | 285 | // emit completedIO(); |
301 | #else | 286 | #else |
302 | int t = ::write( d->handle, buffer, length ); | 287 | int t = ::write( d->handle, buffer, length ); |
303 | if ( t<0 ) t = 0; | 288 | if ( t<0 ) t = 0; |
304 | if ( t != (int)length) { | 289 | if ( t != (int)length) { |
305 | qDebug("Ahhh!! memcpys 1"); | 290 | qDebug("Ahhh!! memcpys 1"); |
306 | memcpy(d->unwrittenBuffer,buffer+t,length-t); | 291 | memcpy(d->unwrittenBuffer,buffer+t,length-t); |
307 | d->unwritten = length-t; | 292 | d->unwritten = length-t; |
308 | } | 293 | } |
309 | #endif | 294 | #endif |
310 | } | 295 | } |
311 | 296 | ||
312 | 297 | ||
313 | unsigned int AudioDevice::channels() const | 298 | unsigned int AudioDevice::channels() const |
314 | { | 299 | { |
315 | return d->channels; | 300 | return d->channels; |
316 | } | 301 | } |
317 | 302 | ||
318 | 303 | ||
319 | unsigned int AudioDevice::frequency() const | 304 | unsigned int AudioDevice::frequency() const |
320 | { | 305 | { |
321 | return d->frequency; | 306 | return d->frequency; |
322 | } | 307 | } |
323 | 308 | ||
324 | 309 | ||
325 | unsigned int AudioDevice::bytesPerSample() const | 310 | unsigned int AudioDevice::bytesPerSample() const |
326 | { | 311 | { |
327 | return d->bytesPerSample; | 312 | return d->bytesPerSample; |
328 | } | 313 | } |
329 | 314 | ||
330 | 315 | ||
331 | unsigned int AudioDevice::bufferSize() const | 316 | unsigned int AudioDevice::bufferSize() const |
332 | { | 317 | { |
333 | return d->bufferSize; | 318 | return d->bufferSize; |
334 | } | 319 | } |
335 | 320 | ||
336 | unsigned int AudioDevice::canWrite() const | 321 | unsigned int AudioDevice::canWrite() const |
337 | { | 322 | { |
338 | #ifdef Q_OS_WIN32 | 323 | #ifdef Q_OS_WIN32 |
339 | return bufferSize(); // Any better? | 324 | return bufferSize(); // Any better? |
340 | #else | 325 | #else |
341 | audio_buf_info info; | 326 | audio_buf_info info; |
342 | if ( d->can_GETOSPACE && ioctl(d->handle,SNDCTL_DSP_GETOSPACE,&info) ) { | 327 | if ( d->can_GETOSPACE && ioctl(d->handle,SNDCTL_DSP_GETOSPACE,&info) ) { |
343 | d->can_GETOSPACE = FALSE; | 328 | d->can_GETOSPACE = FALSE; |
344 | fcntl( d->handle, F_SETFL, O_NONBLOCK ); | 329 | fcntl( d->handle, F_SETFL, O_NONBLOCK ); |
345 | } | 330 | } |
346 | if ( d->can_GETOSPACE ) { | 331 | if ( d->can_GETOSPACE ) { |
347 | int t = info.fragments * sound_fragment_bytes; | 332 | int t = info.fragments * sound_fragment_bytes; |
348 | return QMIN(t,(int)bufferSize()); | 333 | return QMIN(t,(int)bufferSize()); |
349 | } else { | 334 | } else { |
350 | if ( d->unwritten ) { | 335 | if ( d->unwritten ) { |
351 | int t = ::write( d->handle, d->unwrittenBuffer, d->unwritten ); | 336 | int t = ::write( d->handle, d->unwrittenBuffer, d->unwritten ); |
352 | if ( t<0 ) t = 0; | 337 | if ( t<0 ) t = 0; |
353 | if ( (unsigned)t!=d->unwritten ) { | 338 | if ( (unsigned)t!=d->unwritten ) { |
354 | memcpy(d->unwrittenBuffer,d->unwrittenBuffer+t,d->unwritten-t); | 339 | memcpy(d->unwrittenBuffer,d->unwrittenBuffer+t,d->unwritten-t); |
355 | d->unwritten -= t; | 340 | d->unwritten -= t; |
356 | } else { | 341 | } else { |
357 | d->unwritten = 0; | 342 | d->unwritten = 0; |
358 | } | 343 | } |
359 | } | 344 | } |
360 | if ( d->unwritten ) | 345 | if ( d->unwritten ) |
361 | return 0; | 346 | return 0; |
362 | else | 347 | else |
363 | return d->bufferSize; | 348 | return d->bufferSize; |
364 | } | 349 | } |
365 | #endif | 350 | #endif |
366 | } | 351 | } |
367 | 352 | ||
368 | 353 | ||
369 | int AudioDevice::bytesWritten() { | 354 | int AudioDevice::bytesWritten() { |
370 | #ifdef Q_OS_WIN32 | 355 | #ifdef Q_OS_WIN32 |
371 | MMTIME pmmt = { TIME_BYTES, 0 }; | 356 | MMTIME pmmt = { TIME_BYTES, 0 }; |
372 | if ( ( waveOutGetPosition( (HWAVEOUT)d->handle, &pmmt, sizeof(MMTIME) ) != MMSYSERR_NOERROR ) || ( pmmt.wType != TIME_BYTES ) ) { | 357 | if ( ( waveOutGetPosition( (HWAVEOUT)d->handle, &pmmt, sizeof(MMTIME) ) != MMSYSERR_NOERROR ) || ( pmmt.wType != TIME_BYTES ) ) { |
373 | qDebug( "failed to get audio device position" ); | 358 | qDebug( "failed to get audio device position" ); |
374 | return -1; | 359 | return -1; |
375 | } | 360 | } |
376 | return pmmt.u.cb; | 361 | return pmmt.u.cb; |
377 | #else | 362 | #else |
378 | int buffered = 0; | 363 | int buffered = 0; |
379 | if ( ioctl( d->handle, SNDCTL_DSP_GETODELAY, &buffered ) ) { | 364 | if ( ioctl( d->handle, SNDCTL_DSP_GETODELAY, &buffered ) ) { |
380 | qDebug( "failed to get audio device position" ); | 365 | qDebug( "failed to get audio device position" ); |
381 | return -1; | 366 | return -1; |
382 | } | 367 | } |
383 | return buffered; | 368 | return buffered; |
384 | #endif | 369 | #endif |
385 | } | 370 | } |
386 | 371 | ||