summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/audiodevice.cpp
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/audiodevice.cpp') (more/less context) (show whitespace changes)
-rw-r--r--core/multimedia/opieplayer/audiodevice.cpp75
1 files changed, 30 insertions, 45 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
@@ -8,25 +8,29 @@
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>
@@ -103,28 +107,29 @@ void AudioDevice::getVolume( unsigned int& leftVolume, unsigned int& rightVolume
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
125void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume, bool muted ) { 130void 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;
@@ -148,128 +153,108 @@ void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume,
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
183AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) { 190AudioDevice::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
267AudioDevice::~AudioDevice() { 252AudioDevice::~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;