summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/audiodevice.cpp
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/audiodevice.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/multimedia/opieplayer/audiodevice.cpp155
1 files changed, 69 insertions, 86 deletions
diff --git a/core/multimedia/opieplayer/audiodevice.cpp b/core/multimedia/opieplayer/audiodevice.cpp
index 7b3700a..9a10eb4 100644
--- a/core/multimedia/opieplayer/audiodevice.cpp
+++ b/core/multimedia/opieplayer/audiodevice.cpp
@@ -1,5 +1,5 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of Qtopia Environment.
5** 5**
@@ -19,4 +19,6 @@
19**********************************************************************/ 19**********************************************************************/
20// L.J.Potter added better error code Fri 02-15-2002 14:37:47
20 21
21#include <stdlib.h> 22#include <stdlib.h>
23#include <stdio.h>
22#include <qpe/qpeapplication.h> 24#include <qpe/qpeapplication.h>
@@ -108,3 +110,3 @@ void AudioDevice::getVolume( unsigned int& leftVolume, unsigned int& rightVolume
108 if ( waveOutGetVolume( handle, (LPDWORD)&volume ) ) 110 if ( waveOutGetVolume( handle, (LPDWORD)&volume ) )
109 qDebug( "get volume of audio device failed" ); 111// qDebug( "get volume of audio device failed" );
110 waveOutClose( handle ); 112 waveOutClose( handle );
@@ -115,6 +117,7 @@ void AudioDevice::getVolume( unsigned int& leftVolume, unsigned int& rightVolume
115 if ( mixerHandle >= 0 ) { 117 if ( mixerHandle >= 0 ) {
116 ioctl( mixerHandle, MIXER_READ(0), &volume ); 118 if(ioctl( mixerHandle, MIXER_READ(0), &volume )==-1)
119 perror("ioctl(\"MIXER_READ\")");
117 close( mixerHandle ); 120 close( mixerHandle );
118 } else 121 } else
119 qDebug( "get volume of audio device failed" ); 122 perror("open(\"/dev/mixer\")");
120 leftVolume = ((volume & 0x00FF) << 16) / 101; 123 leftVolume = ((volume & 0x00FF) << 16) / 101;
@@ -128,9 +131,9 @@ void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume,
128 if ( muted ) { 131 if ( muted ) {
129 AudioDevicePrivate::leftVolume = leftVolume; 132 AudioDevicePrivate::leftVolume = leftVolume;
130 AudioDevicePrivate::rightVolume = rightVolume; 133 AudioDevicePrivate::rightVolume = rightVolume;
131 leftVolume = 0; 134 leftVolume = 0;
132 rightVolume = 0; 135 rightVolume = 0;
133 } else { 136 } else {
134 leftVolume = ( (int) leftVolume < 0 ) ? 0 : (( leftVolume > 0xFFFF ) ? 0xFFFF : leftVolume ); 137 leftVolume = ( (int) leftVolume < 0 ) ? 0 : (( leftVolume > 0xFFFF ) ? 0xFFFF : leftVolume );
135 rightVolume = ( (int)rightVolume < 0 ) ? 0 : (( rightVolume > 0xFFFF ) ? 0xFFFF : rightVolume ); 138 rightVolume = ( (int)rightVolume < 0 ) ? 0 : (( rightVolume > 0xFFFF ) ? 0xFFFF : rightVolume );
136 } 139 }
@@ -149,3 +152,3 @@ void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume,
149 if ( waveOutSetVolume( handle, volume ) ) 152 if ( waveOutSetVolume( handle, volume ) )
150 qDebug( "set volume of audio device failed" ); 153// qDebug( "set volume of audio device failed" );
151 waveOutClose( handle ); 154 waveOutClose( handle );
@@ -160,6 +163,8 @@ void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume,
160 if ( ( mixerHandle = open( "/dev/mixer", O_RDWR ) ) >= 0 ) { 163 if ( ( mixerHandle = open( "/dev/mixer", O_RDWR ) ) >= 0 ) {
161 ioctl( mixerHandle, MIXER_WRITE(0), &volume ); 164 if(ioctl( mixerHandle, MIXER_WRITE(0), &volume ) ==-1)
165 perror("ioctl(\"MIXER_WRITE\")");
162 close( mixerHandle ); 166 close( mixerHandle );
163 } else 167 } else
164 qDebug( "set volume of audio device failed" ); 168 perror("open(\"/dev/mixer\")");
169
165# else 170# else
@@ -188,41 +193,13 @@ AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) {
188 d->bytesPerSample = bps; 193 d->bytesPerSample = bps;
194// qDebug("%d",bps);
195 int format=0;
196 if( bps == 8) format = AFMT_U8;
197 else if( bps <= 0) format = AFMT_S16_LE;
198 else format = AFMT_S16_LE;
189 199
200// qDebug("AD- freq %d, channels %d, b/sample %d, bitrate %d",f,chs,bps,format);
190 connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( volumeChanged(bool) ) ); 201 connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( volumeChanged(bool) ) );
191 202
192#ifdef Q_OS_WIN32
193 UINT result;
194 WAVEFORMATEX formatData;
195 formatData.cbSize = sizeof(WAVEFORMATEX);
196/*
197 // Other possible formats windows supports
198 formatData.wFormatTag = WAVE_FORMAT_MPEG;
199 formatData.wFormatTag = WAVE_FORMAT_MPEGLAYER3;
200 formatData.wFormatTag = WAVE_FORMAT_ADPCM;
201*/
202 formatData.wFormatTag = WAVE_FORMAT_PCM;
203 formatData.nAvgBytesPerSec = bps * chs * f;
204 formatData.nBlockAlign = bps * chs;
205 formatData.nChannels = chs;
206 formatData.nSamplesPerSec = f;
207 formatData.wBitsPerSample = bps * 8;
208 // Open a waveform device for output
209 if (result = waveOutOpen((LPHWAVEOUT)&d->handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL)) {
210 QString errorMsg = "error opening audio device.\nReason: %i - ";
211 switch (result) {
212 case MMSYSERR_ALLOCATED:errorMsg += "Specified resource is already allocated."; break;
213 case MMSYSERR_BADDEVICEID:errorMsg += "Specified device identifier is out of range."; break;
214 case MMSYSERR_NODRIVER:errorMsg += "No device driver is present."; break;
215 case MMSYSERR_NOMEM:errorMsg += "Unable to allocate or lock memory."; break;
216 case WAVERR_BADFORMAT:errorMsg += "Attempted to open with an unsupported waveform-audio format."; break;
217 case WAVERR_SYNC: errorMsg += "The device is synchronous but waveOutOpen was called without using the WAVE_ALLOWSYNC flag."; break;
218 default: errorMsg += "Undefined error"; break;
219 }
220 qDebug( errorMsg, result );
221 }
222
223 d->bufferSize = sound_fragment_bytes;
224#else
225 203
226 int fragments = 0x10000 * 8 + sound_fragment_shift; 204 int fragments = 0x10000 * 8 + sound_fragment_shift;
227 int format = AFMT_S16_LE;
228 int capabilities = 0; 205 int capabilities = 0;
@@ -233,7 +210,7 @@ AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) {
233 if ( ( d->handle = ::open( "/dev/dsp", O_WRONLY ) ) < 0 ) { 210 if ( ( d->handle = ::open( "/dev/dsp", O_WRONLY ) ) < 0 ) {
234 qDebug( "error opening audio device /dev/dsp, sending data to /dev/null instead" ); 211 perror("open(\"/dev/dsp\") sending to /dev/null instead");
235 d->handle = ::open( "/dev/null", O_WRONLY ); 212 d->handle = ::open( "/dev/null", O_WRONLY );
236 } 213 }
237#ifdef KEEP_DEVICE_OPEN 214#ifdef KEEP_DEVICE_OPEN
238 AudioDevicePrivate::dspFd = d->handle; 215 AudioDevicePrivate::dspFd = d->handle;
239 } else { 216 } else {
@@ -243,9 +220,16 @@ AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) {
243 220
244 ioctl( d->handle, SNDCTL_DSP_GETCAPS, &capabilities ); 221 if(ioctl( d->handle, SNDCTL_DSP_GETCAPS, &capabilities )==-1)
245 ioctl( d->handle, SNDCTL_DSP_SETFRAGMENT, &fragments ); 222 perror("ioctl(\"SNDCTL_DSP_GETCAPS\")");
246 ioctl( d->handle, SNDCTL_DSP_SETFMT, &format ); 223 if(ioctl( d->handle, SNDCTL_DSP_SETFRAGMENT, &fragments )==-1)
247 ioctl( d->handle, SNDCTL_DSP_SPEED, &d->frequency ); 224 perror("ioctl(\"SNDCTL_DSP_SETFRAGMENT\")");
225 if(ioctl( d->handle, SNDCTL_DSP_SETFMT, & format )==-1)
226 perror("ioctl(\"SNDCTL_DSP_SETFMT\")");
227 qDebug("freq %d", d->frequency);
228 if(ioctl( d->handle, SNDCTL_DSP_SPEED, &d->frequency )==-1)
229 perror("ioctl(\"SNDCTL_DSP_SPEED\")");
230 qDebug("channels %d",d->channels);
248 if ( ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ) == -1 ) { 231 if ( ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ) == -1 ) {
249 d->channels = ( d->channels == 1 ) ? 2 : d->channels; 232 d->channels = ( d->channels == 1 ) ? 2 : d->channels;
250 ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ); 233 if(ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels )==-1)
234 perror("ioctl(\"SNDCTL_DSP_CHANNELS\")");
251 } 235 }
@@ -257,4 +241,4 @@ AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) {
257 241
258 //if ( chs != d->channels ) qDebug( "Wanted %d, got %d channels", chs, d->channels ); 242 //if ( chs != d->channels ) qDebug( "Wanted %d, got %d channels", chs, d->channels );
259 //if ( f != d->frequency ) qDebug( "wanted %dHz, got %dHz", f, d->frequency ); 243 //if ( f != d->frequency ) qDebug( "wanted %dHz, got %dHz", f, d->frequency );
260 //if ( capabilities & DSP_CAP_BATCH ) qDebug( "Sound card has local buffer" ); 244 //if ( capabilities & DSP_CAP_BATCH ) qDebug( "Sound card has local buffer" );
@@ -263,3 +247,2 @@ AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) {
263 //if ( capabilities & DSP_CAP_MMAP ) qDebug( "Sound card can mmap" ); 247 //if ( capabilities & DSP_CAP_MMAP ) qDebug( "Sound card can mmap" );
264#endif
265} 248}
@@ -272,3 +255,3 @@ AudioDevice::~AudioDevice() {
272# ifndef KEEP_DEVICE_OPEN 255# ifndef KEEP_DEVICE_OPEN
273 close( d->handle ); // Now it should be safe to shut the handle 256 close( d->handle ); // Now it should be safe to shut the handle
274# endif 257# endif
@@ -299,3 +282,3 @@ void AudioDevice::write( char *buffer, unsigned int length )
299 if ( waveOutWrite( (HWAVEOUT)d->handle, lpWaveHdr, sizeof(WAVEHDR) ) ) 282 if ( waveOutWrite( (HWAVEOUT)d->handle, lpWaveHdr, sizeof(WAVEHDR) ) )
300 qDebug( "failed to write block to audio device" ); 283 qDebug( "failed to write block to audio device" );
301 // emit completedIO(); 284 // emit completedIO();
@@ -305,5 +288,5 @@ void AudioDevice::write( char *buffer, unsigned int length )
305 if ( t != (int)length) { 288 if ( t != (int)length) {
306 qDebug("Ahhh!! memcpys 1"); 289 qDebug("Ahhh!! memcpys 1");
307 memcpy(d->unwrittenBuffer,buffer+t,length-t); 290 memcpy(d->unwrittenBuffer,buffer+t,length-t);
308 d->unwritten = length-t; 291 d->unwritten = length-t;
309 } 292 }
@@ -343,23 +326,23 @@ unsigned int AudioDevice::canWrite() const
343 if ( d->can_GETOSPACE && ioctl(d->handle,SNDCTL_DSP_GETOSPACE,&info) ) { 326 if ( d->can_GETOSPACE && ioctl(d->handle,SNDCTL_DSP_GETOSPACE,&info) ) {
344 d->can_GETOSPACE = FALSE; 327 d->can_GETOSPACE = FALSE;
345 fcntl( d->handle, F_SETFL, O_NONBLOCK ); 328 fcntl( d->handle, F_SETFL, O_NONBLOCK );
346 } 329 }
347 if ( d->can_GETOSPACE ) { 330 if ( d->can_GETOSPACE ) {
348 int t = info.fragments * sound_fragment_bytes; 331 int t = info.fragments * sound_fragment_bytes;
349 return QMIN(t,(int)bufferSize()); 332 return QMIN(t,(int)bufferSize());
350 } else { 333 } else {
351 if ( d->unwritten ) { 334 if ( d->unwritten ) {
352 int t = ::write( d->handle, d->unwrittenBuffer, d->unwritten ); 335 int t = ::write( d->handle, d->unwrittenBuffer, d->unwritten );
353 if ( t<0 ) t = 0; 336 if ( t<0 ) t = 0;
354 if ( (unsigned)t!=d->unwritten ) { 337 if ( (unsigned)t!=d->unwritten ) {
355 memcpy(d->unwrittenBuffer,d->unwrittenBuffer+t,d->unwritten-t); 338 memcpy(d->unwrittenBuffer,d->unwrittenBuffer+t,d->unwritten-t);
356 d->unwritten -= t; 339 d->unwritten -= t;
357 } else { 340 } else {
358 d->unwritten = 0; 341 d->unwritten = 0;
359 } 342 }
360 } 343 }
361 if ( d->unwritten ) 344 if ( d->unwritten )
362 return 0; 345 return 0;
363 else 346 else
364 return d->bufferSize; 347 return d->bufferSize;
365 } 348 }
@@ -373,4 +356,4 @@ int AudioDevice::bytesWritten() {
373 if ( ( waveOutGetPosition( (HWAVEOUT)d->handle, &pmmt, sizeof(MMTIME) ) != MMSYSERR_NOERROR ) || ( pmmt.wType != TIME_BYTES ) ) { 356 if ( ( waveOutGetPosition( (HWAVEOUT)d->handle, &pmmt, sizeof(MMTIME) ) != MMSYSERR_NOERROR ) || ( pmmt.wType != TIME_BYTES ) ) {
374 qDebug( "failed to get audio device position" ); 357 qDebug( "failed to get audio device position" );
375 return -1; 358 return -1;
376 } 359 }
@@ -380,4 +363,4 @@ int AudioDevice::bytesWritten() {
380 if ( ioctl( d->handle, SNDCTL_DSP_GETODELAY, &buffered ) ) { 363 if ( ioctl( d->handle, SNDCTL_DSP_GETODELAY, &buffered ) ) {
381 qDebug( "failed to get audio device position" ); 364 qDebug( "failed to get audio device position" );
382 return -1; 365 return -1;
383 } 366 }