author | llornkcor <llornkcor> | 2002-02-25 05:10:32 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2002-02-25 05:10:32 (UTC) |
commit | 1bebd27631a6c9d1ab967cbb32dd4e796b3b0402 (patch) (side-by-side diff) | |
tree | 8ce49800cba26f6f2c33de74a71d4efe0338843f /core/multimedia/opieplayer/audiodevice.cpp | |
parent | d948361c1b475fab4660b28fbf5ec21fd34d923f (diff) | |
download | opie-1bebd27631a6c9d1ab967cbb32dd4e796b3b0402.zip opie-1bebd27631a6c9d1ab967cbb32dd4e796b3b0402.tar.gz opie-1bebd27631a6c9d1ab967cbb32dd4e796b3b0402.tar.bz2 |
fixed 2
Diffstat (limited to 'core/multimedia/opieplayer/audiodevice.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/audiodevice.cpp | 155 |
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,7 +1,7 @@ /********************************************************************** -** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** Copyright (C) 2000 Trolltech AS. All rights reserved. ** -** This file is part of the Qtopia Environment. +** This file is part of Qtopia Environment. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software @@ -17,8 +17,10 @@ ** not clear to you. ** **********************************************************************/ +// L.J.Potter added better error code Fri 02-15-2002 14:37:47 #include <stdlib.h> +#include <stdio.h> #include <qpe/qpeapplication.h> #include <qpe/config.h> #include "audiodevice.h" @@ -106,17 +108,18 @@ void AudioDevice::getVolume( unsigned int& leftVolume, unsigned int& rightVolume formatData.wBitsPerSample = 16; waveOutOpen(&handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL); if ( waveOutGetVolume( handle, (LPDWORD)&volume ) ) - qDebug( "get volume of audio device failed" ); +// qDebug( "get volume of audio device failed" ); waveOutClose( handle ); leftVolume = volume & 0xFFFF; rightVolume = volume >> 16; #else int mixerHandle = open( "/dev/mixer", O_RDWR ); if ( mixerHandle >= 0 ) { - ioctl( mixerHandle, MIXER_READ(0), &volume ); + if(ioctl( mixerHandle, MIXER_READ(0), &volume )==-1) + perror("ioctl(\"MIXER_READ\")"); close( mixerHandle ); } else - qDebug( "get volume of audio device failed" ); + perror("open(\"/dev/mixer\")"); leftVolume = ((volume & 0x00FF) << 16) / 101; rightVolume = ((volume & 0xFF00) << 8) / 101; #endif @@ -126,13 +129,13 @@ void AudioDevice::getVolume( unsigned int& leftVolume, unsigned int& rightVolume void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume, bool muted ) { AudioDevicePrivate::muted = muted; if ( muted ) { - AudioDevicePrivate::leftVolume = leftVolume; - AudioDevicePrivate::rightVolume = rightVolume; - leftVolume = 0; - rightVolume = 0; + AudioDevicePrivate::leftVolume = leftVolume; + AudioDevicePrivate::rightVolume = rightVolume; + leftVolume = 0; + rightVolume = 0; } else { leftVolume = ( (int) leftVolume < 0 ) ? 0 : (( leftVolume > 0xFFFF ) ? 0xFFFF : leftVolume ); - rightVolume = ( (int)rightVolume < 0 ) ? 0 : (( rightVolume > 0xFFFF ) ? 0xFFFF : rightVolume ); + rightVolume = ( (int)rightVolume < 0 ) ? 0 : (( rightVolume > 0xFFFF ) ? 0xFFFF : rightVolume ); } #ifdef Q_OS_WIN32 HWAVEOUT handle; @@ -147,7 +150,7 @@ void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume, waveOutOpen(&handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL); unsigned int volume = (rightVolume << 16) | leftVolume; if ( waveOutSetVolume( handle, volume ) ) - qDebug( "set volume of audio device failed" ); +// qDebug( "set volume of audio device failed" ); waveOutClose( handle ); #else // Volume can be from 0 to 100 which is 101 distinct values @@ -158,10 +161,12 @@ void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume, unsigned int volume = ((rV << 8) & 0xFF00) | (lV & 0x00FF); int mixerHandle = 0; if ( ( mixerHandle = open( "/dev/mixer", O_RDWR ) ) >= 0 ) { - ioctl( mixerHandle, MIXER_WRITE(0), &volume ); + if(ioctl( mixerHandle, MIXER_WRITE(0), &volume ) ==-1) + perror("ioctl(\"MIXER_WRITE\")"); close( mixerHandle ); } else - qDebug( "set volume of audio device failed" ); + perror("open(\"/dev/mixer\")"); + # else // This is the way this has to be done now I guess, doesn't allow for // independant right and left channel setting, or setting for different outputs @@ -186,68 +191,47 @@ AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) { d->frequency = f; d->channels = chs; d->bytesPerSample = bps; +// qDebug("%d",bps); + int format=0; + if( bps == 8) format = AFMT_U8; + else if( bps <= 0) format = AFMT_S16_LE; + else format = AFMT_S16_LE; +// qDebug("AD- freq %d, channels %d, b/sample %d, bitrate %d",f,chs,bps,format); connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( volumeChanged(bool) ) ); -#ifdef Q_OS_WIN32 - UINT result; - WAVEFORMATEX formatData; - formatData.cbSize = sizeof(WAVEFORMATEX); -/* - // Other possible formats windows supports - formatData.wFormatTag = WAVE_FORMAT_MPEG; - formatData.wFormatTag = WAVE_FORMAT_MPEGLAYER3; - formatData.wFormatTag = WAVE_FORMAT_ADPCM; -*/ - formatData.wFormatTag = WAVE_FORMAT_PCM; - formatData.nAvgBytesPerSec = bps * chs * f; - formatData.nBlockAlign = bps * chs; - formatData.nChannels = chs; - formatData.nSamplesPerSec = f; - formatData.wBitsPerSample = bps * 8; - // Open a waveform device for output - if (result = waveOutOpen((LPHWAVEOUT)&d->handle, WAVE_MAPPER, &formatData, 0L, 0L, CALLBACK_NULL)) { - QString errorMsg = "error opening audio device.\nReason: %i - "; - switch (result) { - case MMSYSERR_ALLOCATED: errorMsg += "Specified resource is already allocated."; break; - case MMSYSERR_BADDEVICEID: errorMsg += "Specified device identifier is out of range."; break; - case MMSYSERR_NODRIVER: errorMsg += "No device driver is present."; break; - case MMSYSERR_NOMEM: errorMsg += "Unable to allocate or lock memory."; break; - case WAVERR_BADFORMAT: errorMsg += "Attempted to open with an unsupported waveform-audio format."; break; - case WAVERR_SYNC: errorMsg += "The device is synchronous but waveOutOpen was called without using the WAVE_ALLOWSYNC flag."; break; - default: errorMsg += "Undefined error"; break; - } - qDebug( errorMsg, result ); - } - - d->bufferSize = sound_fragment_bytes; -#else int fragments = 0x10000 * 8 + sound_fragment_shift; - int format = AFMT_S16_LE; int capabilities = 0; #ifdef KEEP_DEVICE_OPEN if ( AudioDevicePrivate::dspFd == 0 ) { #endif if ( ( d->handle = ::open( "/dev/dsp", O_WRONLY ) ) < 0 ) { - qDebug( "error opening audio device /dev/dsp, sending data to /dev/null instead" ); - d->handle = ::open( "/dev/null", O_WRONLY ); + perror("open(\"/dev/dsp\") sending to /dev/null instead"); + d->handle = ::open( "/dev/null", O_WRONLY ); } #ifdef KEEP_DEVICE_OPEN - AudioDevicePrivate::dspFd = d->handle; + AudioDevicePrivate::dspFd = d->handle; } else { d->handle = AudioDevicePrivate::dspFd; } #endif - ioctl( d->handle, SNDCTL_DSP_GETCAPS, &capabilities ); - ioctl( d->handle, SNDCTL_DSP_SETFRAGMENT, &fragments ); - ioctl( d->handle, SNDCTL_DSP_SETFMT, &format ); - ioctl( d->handle, SNDCTL_DSP_SPEED, &d->frequency ); + if(ioctl( d->handle, SNDCTL_DSP_GETCAPS, &capabilities )==-1) + perror("ioctl(\"SNDCTL_DSP_GETCAPS\")"); + if(ioctl( d->handle, SNDCTL_DSP_SETFRAGMENT, &fragments )==-1) + perror("ioctl(\"SNDCTL_DSP_SETFRAGMENT\")"); + if(ioctl( d->handle, SNDCTL_DSP_SETFMT, & format )==-1) + perror("ioctl(\"SNDCTL_DSP_SETFMT\")"); + qDebug("freq %d", d->frequency); + if(ioctl( d->handle, SNDCTL_DSP_SPEED, &d->frequency )==-1) + perror("ioctl(\"SNDCTL_DSP_SPEED\")"); + qDebug("channels %d",d->channels); if ( ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ) == -1 ) { - d->channels = ( d->channels == 1 ) ? 2 : d->channels; - ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ); + d->channels = ( d->channels == 1 ) ? 2 : d->channels; + if(ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels )==-1) + perror("ioctl(\"SNDCTL_DSP_CHANNELS\")"); } d->bufferSize = sound_fragment_bytes; @@ -255,13 +239,12 @@ AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) { d->unwritten = 0; d->can_GETOSPACE = TRUE; // until we find otherwise - //if ( chs != d->channels ) qDebug( "Wanted %d, got %d channels", chs, d->channels ); - //if ( f != d->frequency ) qDebug( "wanted %dHz, got %dHz", f, d->frequency ); + //if ( chs != d->channels ) qDebug( "Wanted %d, got %d channels", chs, d->channels ); + //if ( f != d->frequency ) qDebug( "wanted %dHz, got %dHz", f, d->frequency ); //if ( capabilities & DSP_CAP_BATCH ) qDebug( "Sound card has local buffer" ); //if ( capabilities & DSP_CAP_REALTIME )qDebug( "Sound card has realtime sync" ); //if ( capabilities & DSP_CAP_TRIGGER ) qDebug( "Sound card has precise trigger" ); //if ( capabilities & DSP_CAP_MMAP ) qDebug( "Sound card can mmap" ); -#endif } @@ -270,7 +253,7 @@ AudioDevice::~AudioDevice() { waveOutClose( (HWAVEOUT)d->handle ); #else # ifndef KEEP_DEVICE_OPEN - close( d->handle ); // Now it should be safe to shut the handle + close( d->handle ); // Now it should be safe to shut the handle # endif delete d->unwrittenBuffer; delete d; @@ -297,15 +280,15 @@ void AudioDevice::write( char *buffer, unsigned int length ) waveOutPrepareHeader( (HWAVEOUT)d->handle, lpWaveHdr, sizeof(WAVEHDR) ); // waveOutWrite returns immediately. the data is sent in the background. if ( waveOutWrite( (HWAVEOUT)d->handle, lpWaveHdr, sizeof(WAVEHDR) ) ) - qDebug( "failed to write block to audio device" ); + qDebug( "failed to write block to audio device" ); // emit completedIO(); #else int t = ::write( d->handle, buffer, length ); if ( t<0 ) t = 0; if ( t != (int)length) { - qDebug("Ahhh!! memcpys 1"); - memcpy(d->unwrittenBuffer,buffer+t,length-t); - d->unwritten = length-t; + qDebug("Ahhh!! memcpys 1"); + memcpy(d->unwrittenBuffer,buffer+t,length-t); + d->unwritten = length-t; } #endif } @@ -341,27 +324,27 @@ unsigned int AudioDevice::canWrite() const #else audio_buf_info info; if ( d->can_GETOSPACE && ioctl(d->handle,SNDCTL_DSP_GETOSPACE,&info) ) { - d->can_GETOSPACE = FALSE; - fcntl( d->handle, F_SETFL, O_NONBLOCK ); + d->can_GETOSPACE = FALSE; + fcntl( d->handle, F_SETFL, O_NONBLOCK ); } if ( d->can_GETOSPACE ) { - int t = info.fragments * sound_fragment_bytes; - return QMIN(t,(int)bufferSize()); + int t = info.fragments * sound_fragment_bytes; + return QMIN(t,(int)bufferSize()); } else { - if ( d->unwritten ) { - int t = ::write( d->handle, d->unwrittenBuffer, d->unwritten ); - if ( t<0 ) t = 0; - if ( (unsigned)t!=d->unwritten ) { - memcpy(d->unwrittenBuffer,d->unwrittenBuffer+t,d->unwritten-t); - d->unwritten -= t; - } else { - d->unwritten = 0; - } - } - if ( d->unwritten ) - return 0; - else - return d->bufferSize; + if ( d->unwritten ) { + int t = ::write( d->handle, d->unwrittenBuffer, d->unwritten ); + if ( t<0 ) t = 0; + if ( (unsigned)t!=d->unwritten ) { + memcpy(d->unwrittenBuffer,d->unwrittenBuffer+t,d->unwritten-t); + d->unwritten -= t; + } else { + d->unwritten = 0; + } + } + if ( d->unwritten ) + return 0; + else + return d->bufferSize; } #endif } @@ -371,15 +354,15 @@ int AudioDevice::bytesWritten() { #ifdef Q_OS_WIN32 MMTIME pmmt = { TIME_BYTES, 0 }; if ( ( waveOutGetPosition( (HWAVEOUT)d->handle, &pmmt, sizeof(MMTIME) ) != MMSYSERR_NOERROR ) || ( pmmt.wType != TIME_BYTES ) ) { - qDebug( "failed to get audio device position" ); - return -1; + qDebug( "failed to get audio device position" ); + return -1; } return pmmt.u.cb; #else int buffered = 0; if ( ioctl( d->handle, SNDCTL_DSP_GETODELAY, &buffered ) ) { - qDebug( "failed to get audio device position" ); - return -1; + qDebug( "failed to get audio device position" ); + return -1; } return buffered; #endif |