1 /* fhandler_dev_dsp: code to emulate OSS sound model /dev/dsp
3 Copyright 2001 Red Hat, Inc
5 Written by Andy Younger (andy@snoogie.demon.co.uk)
7 This file is part of Cygwin.
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
19 #include <sys/soundcard.h>
20 #include <sys/fcntl.h>
23 //------------------------------------------------------------------------
24 // Simple encapsulation of the win32 audio device.
26 static void CALLBACK wave_callback (HWAVE hWave, UINT msg, DWORD instance,
27 DWORD param1, DWORD param2);
31 enum { MAX_BLOCKS = 12, BLOCK_SIZE = 16384 };
36 bool open (int rate, int bits, int channels, bool bCallback = false);
39 void setvolume (int newVolume);
40 bool write (const void *pSampleData, int nBytes);
42 void callback_sampledone (void *pData);
43 void setformat (int format) {formattype_ = format;}
44 int numbytesoutput ();
47 char *initialisebuffer ();
48 void waitforcallback ();
52 volatile int nBlocksInQue_;
56 CRITICAL_SECTION lock_;
57 char *freeblocks_[MAX_BLOCKS];
60 char bigwavebuffer_[MAX_BLOCKS * BLOCK_SIZE];
65 int size = BLOCK_SIZE + sizeof (WAVEHDR);
67 InitializeCriticalSection (&lock_);
68 memset (freeblocks_, 0, sizeof (freeblocks_));
69 for (int i = 0; i < MAX_BLOCKS; i++)
71 char *pBuffer = &bigwavebuffer_[i * size];
72 memset (pBuffer, 0, size);
73 freeblocks_[i] = pBuffer;
81 DeleteCriticalSection (&lock_);
85 Audio::open (int rate, int bits, int channels, bool bCallback = false)
88 int nDevices = waveOutGetNumDevs ();
93 debug_printf ("number devices %d\n", nDevices);
97 debug_printf ("trying to map device freq %d, bits %d, "
98 "channels %d, callback %d\n", rate, bits, channels,
101 int bytesperSample = bits / 8;
103 memset (&format, 0, sizeof (format));
104 format.wFormatTag = WAVE_FORMAT_PCM;
105 format.wBitsPerSample = bits;
106 format.nChannels = channels;
107 format.nSamplesPerSec = rate;
108 format.nAvgBytesPerSec = format.nSamplesPerSec * format.nChannels *
110 format.nBlockAlign = format.nChannels * bytesperSample;
113 HRESULT res = waveOutOpen (&dev_, WAVE_MAPPER, &format, (DWORD) wave_callback,
114 (DWORD) this, bCallback ? CALLBACK_FUNCTION : 0);
117 debug_printf ("Sucessfully opened!");
122 debug_printf ("failed to open");
132 flush (); // force out last block whatever size..
134 while (blocks ()) // block till finished..
145 Audio::numbytesoutput ()
147 return nBytesWritten_;
155 waveOutGetVolume (dev_, &volume);
156 return ((volume >> 16) + (volume & 0xffff)) >> 1;
160 Audio::setvolume (int newVolume)
162 waveOutSetVolume (dev_, (newVolume << 16) | newVolume);
166 Audio::initialisebuffer ()
168 EnterCriticalSection (&lock_);
169 WAVEHDR *pHeader = 0L;
170 for (int i = 0; i < MAX_BLOCKS; i++)
172 char *pData = freeblocks_[i];
175 pHeader = (WAVEHDR *) pData;
176 if (pHeader->dwFlags & WHDR_DONE)
178 waveOutUnprepareHeader (dev_, pHeader, sizeof (WAVEHDR));
184 LeaveCriticalSection (&lock_);
188 memset (pHeader, 0, sizeof (WAVEHDR));
189 pHeader->dwBufferLength = BLOCK_SIZE;
190 pHeader->lpData = (LPSTR) (&pHeader[1]);
191 return (char *) pHeader->lpData;
197 Audio::write (const void *pSampleData, int nBytes)
199 // split up big blocks into smaller BLOCK_SIZE chunks
200 while (nBytes > BLOCK_SIZE)
202 write (pSampleData, BLOCK_SIZE);
203 nBytes -= BLOCK_SIZE;
204 pSampleData = (void *) ((char *) pSampleData + BLOCK_SIZE);
207 // Block till next sound is flushed
208 if (blocks () == MAX_BLOCKS)
211 // Allocate new wave buffer if necessary
214 buffer_ = initialisebuffer ();
220 // Handle gathering blocks into larger buffer
221 int sizeleft = BLOCK_SIZE - bufferIndex_;
222 if (nBytes < sizeleft)
224 memcpy (&buffer_[bufferIndex_], pSampleData, nBytes);
225 bufferIndex_ += nBytes;
226 nBytesWritten_ += nBytes;
230 // flushing when we reach our limit of BLOCK_SIZE
231 memcpy (&buffer_[bufferIndex_], pSampleData, sizeleft);
232 bufferIndex_ += sizeleft;
233 nBytesWritten_ += sizeleft;
236 // change pointer to rest of sample, and size accordingly
237 pSampleData = (void *) ((char *) pSampleData + sizeleft);
240 // if we still have some sample left over write it out
242 return write (pSampleData, nBytes);
247 // return number of blocks back.
251 EnterCriticalSection (&lock_);
252 int ret = nBlocksInQue_;
253 LeaveCriticalSection (&lock_);
257 // This is called on an interupt so use locking.. Note nBlocksInQue_ is
258 // modified by it so we should wrap all references to it in locks.
260 Audio::callback_sampledone (void *pData)
262 EnterCriticalSection (&lock_);
265 for (int i = 0; i < MAX_BLOCKS; i++)
268 freeblocks_[i] = (char *) pData;
272 LeaveCriticalSection (&lock_);
276 Audio::waitforcallback ()
285 while (n == blocks ());
294 // Send internal buffer out to the soundcard
295 WAVEHDR *pHeader = ((WAVEHDR *) buffer_) - 1;
296 pHeader->dwBufferLength = bufferIndex_;
298 // Quick bit of sample buffer conversion
299 if (formattype_ == AFMT_S8)
301 unsigned char *p = ((unsigned char *) buffer_);
302 for (int i = 0; i < bufferIndex_; i++)
308 if (waveOutPrepareHeader (dev_, pHeader, sizeof (WAVEHDR)) == S_OK &&
309 waveOutWrite (dev_, pHeader, sizeof (WAVEHDR)) == S_OK)
311 EnterCriticalSection (&lock_);
313 LeaveCriticalSection (&lock_);
320 EnterCriticalSection (&lock_);
321 for (int i = 0; i < MAX_BLOCKS; i++)
324 freeblocks_[i] = (char *) pHeader;
327 LeaveCriticalSection (&lock_);
332 //------------------------------------------------------------------------
335 wave_callback (HWAVE hWave, UINT msg, DWORD instance, DWORD param1,
340 Audio *ptr = (Audio *) instance;
341 ptr->callback_sampledone ((void *) param1);
345 //------------------------------------------------------------------------
347 static Audio *s_audio; // static instance of the Audio handler
349 //------------------------------------------------------------------------
350 // wav file detection..
359 unsigned short wFormatTag;
360 unsigned short wChannels;
361 unsigned int dwSamplesPerSec;
362 unsigned int dwAvgBytesPerSec;
363 unsigned short wBlockAlign;
364 unsigned short wBitsPerSample;
369 fhandler_dev_dsp::setupwav (const char *pData, int nBytes)
372 const char *end = pData + nBytes;
374 if (!(pData[0] == 'R' && pData[1] == 'I' &&
375 pData[2] == 'F' && pData[3] == 'F'))
377 if (!(pData[8] == 'W' && pData[9] == 'A' &&
378 pData[10] == 'V' && pData[11] == 'E'))
381 len = *(int *) &pData[4];
383 while (len && pData < end)
385 wavchunk * pChunk = (wavchunk *) pData;
386 int blklen = pChunk-> len;
387 if (pChunk->id[0] == 'f' && pChunk->id[1] == 'm' &&
388 pChunk->id[2] == 't' && pChunk->id[3] == ' ')
390 wavformat *format = (wavformat *) (pChunk + 1);
391 if ((char *) (format + 1) > end)
394 // Open up audio device with correct frequency for wav file
396 // FIXME: should through away all the header & not output
397 // it to the soundcard.
399 if (s_audio->open (format->dwSamplesPerSec, format->wBitsPerSample,
400 format->wChannels) == false)
402 s_audio->open (audiofreq_, audiobits_, audiochannels_);
406 audiofreq_ = format->dwSamplesPerSec;
407 audiobits_ = format->wBitsPerSample;
408 audiochannels_ = format->wChannels;
413 pData += blklen + sizeof (wavchunk);
418 //------------------------------------------------------------------------
419 fhandler_dev_dsp::fhandler_dev_dsp (const char *name):
420 fhandler_base (FH_OSS_DSP, name)
422 set_cb (sizeof *this);
425 fhandler_dev_dsp::~fhandler_dev_dsp ()
430 fhandler_dev_dsp::open (const char *path, int flags, mode_t mode = 0)
432 // currently we only support writing
433 if ((flags & (O_WRONLY | O_RDONLY | O_RDWR)) != O_WRONLY)
441 // Work out initial sample format & frequency
442 if (strcmp (path, "/dev/dsp") == 0L)
445 audioformat_ = AFMT_S8;
451 if (!s_audio->open (audiofreq_, audiobits_, audiochannels_))
452 debug_printf ("/dev/dsp: failed to open\n");
456 debug_printf ("/dev/dsp: successfully opened\n");
462 fhandler_dev_dsp::write (const void *ptr, size_t len)
464 if (s_audio->numbytesoutput () == 0)
466 // check for wave file & setup frequencys properly if possible.
467 setupwav ((const char *) ptr, len);
469 // Open audio device properly with callbacks.
471 if (!s_audio->open (audiofreq_, audiobits_, audiochannels_, true))
475 s_audio->write (ptr, len);
480 fhandler_dev_dsp::read (void *ptr, size_t len)
486 fhandler_dev_dsp::lseek (off_t offset, int whence)
492 fhandler_dev_dsp::close (void)
499 fhandler_dev_dsp::dup (fhandler_base * child)
501 fhandler_dev_dsp *fhc = (fhandler_dev_dsp *) child;
503 fhc->set_flags (get_flags ());
504 fhc->audiochannels_ = audiochannels_;
505 fhc->audiobits_ = audiobits_;
506 fhc->audiofreq_ = audiofreq_;
507 fhc->audioformat_ = audioformat_;
512 fhandler_dev_dsp::ioctl (unsigned int cmd, void *ptr)
514 int *intptr = (int *) ptr;
517 #define CASE(a) case a : debug_printf("/dev/dsp: ioctl %s\n", #a);
519 CASE (SNDCTL_DSP_RESET)
520 audioformat_ = AFMT_S8;
526 CASE (SNDCTL_DSP_GETBLKSIZE)
527 *intptr = Audio::BLOCK_SIZE;
530 CASE (SNDCTL_DSP_SETFMT)
533 if (*intptr == AFMT_S16_LE)
535 else if (*intptr == AFMT_U8)
537 else if (*intptr == AFMT_S8)
541 s_audio->setformat (*intptr);
543 if (s_audio->open (audiofreq_, nBits, audiochannels_) == true)
550 s_audio->open (audiofreq_, audiobits_, audiochannels_);
557 CASE (SNDCTL_DSP_SPEED)
559 if (s_audio->open (*intptr, audiobits_, audiochannels_) == true)
561 audiofreq_ = *intptr;
566 s_audio->open (audiofreq_, audiobits_, audiochannels_);
571 CASE (SNDCTL_DSP_STEREO)
573 int nChannels = *intptr + 1;
576 if (s_audio->open (audiofreq_, audiobits_, nChannels) == true)
578 audiochannels_ = nChannels;
583 s_audio->open (audiofreq_, audiobits_, audiochannels_);
589 CASE (SNDCTL_DSP_GETOSPACE)
591 audio_buf_info *p = (audio_buf_info *) ptr;
593 int nBlocks = s_audio->blocks ();
594 int leftblocks = ((Audio::MAX_BLOCKS - nBlocks) - 1);
599 int left = leftblocks * Audio::BLOCK_SIZE;
601 p->fragments = leftblocks;
602 p->fragstotal = Audio::MAX_BLOCKS;
603 p->fragsize = Audio::BLOCK_SIZE;
606 debug_printf ("ptr %p nblocks %d leftblocks %d left bytes %d ",
607 ptr, nBlocks, leftblocks, left);
613 CASE (SNDCTL_DSP_SETFRAGMENT)
615 // Fake!! esound & mikmod require this on non PowerPC platforms.
622 debug_printf ("/dev/dsp: ioctl not handled yet! FIXME:\n");
631 fhandler_dev_dsp::dump ()
633 paranoid_printf ("here, fhandler_dev_dsp");