1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
/* fhandler_dev_dsp: code to emulate OSS sound model /dev/dsp
Copyright 2001, 2002, 2003 Red Hat, Inc
Written by Andy Younger (andy@snoogie.demon.co.uk)
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <stdio.h>
#include <windows.h>
#include <sys/soundcard.h>
#include <mmsystem.h>
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
//------------------------------------------------------------------------
// Simple encapsulation of the win32 audio device.
//
static void CALLBACK wave_callback (HWAVE hWave, UINT msg, DWORD instance,
DWORD param1, DWORD param2);
class Audio
{
public:
enum
{
MAX_BLOCKS = 12,
BLOCK_SIZE = 16384,
TOT_BLOCK_SIZE = BLOCK_SIZE + sizeof (WAVEHDR)
};
Audio ();
~Audio ();
bool open (int rate, int bits, int channels, bool bCallback = false);
void close ();
int getvolume ();
void setvolume (int newVolume);
bool write (const void *pSampleData, int nBytes);
int blocks ();
void callback_sampledone (void *pData);
void setformat (int format) {formattype_ = format;}
int numbytesoutput ();
void *operator new (size_t, void *p) {return p;}
private:
char *initialisebuffer ();
void waitforcallback ();
bool flush ();
HWAVEOUT dev_;
volatile int nBlocksInQue_;
int nBytesWritten_;
char *buffer_;
int bufferIndex_;
CRITICAL_SECTION lock_;
char *freeblocks_[MAX_BLOCKS];
int formattype_;
char bigwavebuffer_[MAX_BLOCKS * TOT_BLOCK_SIZE];
};
static char audio_buf[sizeof (class Audio)];
Audio::Audio ()
{
InitializeCriticalSection (&lock_);
memset (bigwavebuffer_, 0, sizeof (bigwavebuffer_));
for (int i = 0; i < MAX_BLOCKS; i++)
freeblocks_[i] = &bigwavebuffer_[i * TOT_BLOCK_SIZE];
}
Audio::~Audio ()
{
if (dev_)
close ();
DeleteCriticalSection (&lock_);
}
bool
Audio::open (int rate, int bits, int channels, bool bCallback)
{
WAVEFORMATEX format;
int nDevices = waveOutGetNumDevs ();
nBytesWritten_ = 0L;
bufferIndex_ = 0;
buffer_ = 0L;
debug_printf ("number devices %d", nDevices);
if (nDevices <= 0)
return false;
debug_printf ("trying to map device freq %d, bits %d, "
"channels %d, callback %d", rate, bits, channels,
bCallback);
int bytesperSample = bits / 8;
memset (&format, 0, sizeof (format));
format.wFormatTag = WAVE_FORMAT_PCM;
format.wBitsPerSample = bits;
format.nChannels = channels;
format.nSamplesPerSec = rate;
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nChannels *
bytesperSample;
format.nBlockAlign = format.nChannels * bytesperSample;
nBlocksInQue_ = 0;
HRESULT res = waveOutOpen (&dev_, WAVE_MAPPER, &format, (DWORD) wave_callback,
(DWORD) this, bCallback ? CALLBACK_FUNCTION : 0);
if (res == S_OK)
{
debug_printf ("Sucessfully opened!");
return true;
}
else
{
debug_printf ("failed to open");
return false;
}
}
void
Audio::close ()
{
if (dev_)
{
flush (); // force out last block whatever size..
while (blocks ()) // block till finished..
waitforcallback ();
waveOutReset (dev_);
waveOutClose (dev_);
dev_ = 0L;
}
nBytesWritten_ = 0L;
}
int
Audio::numbytesoutput ()
{
return nBytesWritten_;
}
int
Audio::getvolume ()
{
DWORD volume;
waveOutGetVolume (dev_, &volume);
return ((volume >> 16) + (volume & 0xffff)) >> 1;
}
void
Audio::setvolume (int newVolume)
{
waveOutSetVolume (dev_, (newVolume << 16) | newVolume);
}
char *
Audio::initialisebuffer ()
{
EnterCriticalSection (&lock_);
WAVEHDR *pHeader = 0L;
for (int i = 0; i < MAX_BLOCKS; i++)
{
char *pData = freeblocks_[i];
if (pData)
{
pHeader = (WAVEHDR *) pData;
if (pHeader->dwFlags & WHDR_DONE)
{
waveOutUnprepareHeader (dev_, pHeader, sizeof (WAVEHDR));
}
freeblocks_[i] = 0L;
break;
}
}
LeaveCriticalSection (&lock_);
if (pHeader)
{
memset (pHeader, 0, sizeof (WAVEHDR));
pHeader->dwBufferLength = BLOCK_SIZE;
pHeader->lpData = (LPSTR) (&pHeader[1]);
return (char *) pHeader->lpData;
}
return 0L;
}
bool
Audio::write (const void *pSampleData, int nBytes)
{
// split up big blocks into smaller BLOCK_SIZE chunks
while (nBytes > BLOCK_SIZE)
{
write (pSampleData, BLOCK_SIZE);
nBytes -= BLOCK_SIZE;
pSampleData = (void *) ((char *) pSampleData + BLOCK_SIZE);
}
// Block till next sound is flushed
if (blocks () == MAX_BLOCKS)
waitforcallback ();
// Allocate new wave buffer if necessary
if (buffer_ == 0L)
{
buffer_ = initialisebuffer ();
if (buffer_ == 0L)
return false;
}
// Handle gathering blocks into larger buffer
int sizeleft = BLOCK_SIZE - bufferIndex_;
if (nBytes < sizeleft)
{
memcpy (&buffer_[bufferIndex_], pSampleData, nBytes);
bufferIndex_ += nBytes;
nBytesWritten_ += nBytes;
return true;
}
// flushing when we reach our limit of BLOCK_SIZE
memcpy (&buffer_[bufferIndex_], pSampleData, sizeleft);
bufferIndex_ += sizeleft;
nBytesWritten_ += sizeleft;
flush ();
// change pointer to rest of sample, and size accordingly
pSampleData = (void *) ((char *) pSampleData + sizeleft);
nBytes -= sizeleft;
// if we still have some sample left over write it out
if (nBytes)
return write (pSampleData, nBytes);
return true;
}
// return number of blocks back.
int
Audio::blocks ()
{
EnterCriticalSection (&lock_);
int ret = nBlocksInQue_;
LeaveCriticalSection (&lock_);
return ret;
}
// This is called on an interupt so use locking.. Note nBlocksInQue_ is
// modified by it so we should wrap all references to it in locks.
void
Audio::callback_sampledone (void *pData)
{
EnterCriticalSection (&lock_);
nBlocksInQue_--;
for (int i = 0; i < MAX_BLOCKS; i++)
if (!freeblocks_[i])
{
freeblocks_[i] = (char *) pData;
break;
}
LeaveCriticalSection (&lock_);
}
void
Audio::waitforcallback ()
{
int n = blocks ();
if (!n)
return;
do
{
Sleep (250);
}
while (n == blocks ());
}
bool
Audio::flush ()
{
if (!buffer_)
return false;
// Send internal buffer out to the soundcard
WAVEHDR *pHeader = ((WAVEHDR *) buffer_) - 1;
pHeader->dwBufferLength = bufferIndex_;
// Quick bit of sample buffer conversion
if (formattype_ == AFMT_S8)
{
unsigned char *p = ((unsigned char *) buffer_);
for (int i = 0; i < bufferIndex_; i++)
{
p[i] -= 0x7f;
}
}
if (waveOutPrepareHeader (dev_, pHeader, sizeof (WAVEHDR)) == S_OK &&
waveOutWrite (dev_, pHeader, sizeof (WAVEHDR)) == S_OK)
{
EnterCriticalSection (&lock_);
nBlocksInQue_++;
LeaveCriticalSection (&lock_);
bufferIndex_ = 0;
buffer_ = 0L;
return true;
}
else
{
EnterCriticalSection (&lock_);
for (int i = 0; i < MAX_BLOCKS; i++)
if (!freeblocks_[i])
{
freeblocks_[i] = (char *) pHeader;
break;
}
LeaveCriticalSection (&lock_);
}
return false;
}
//------------------------------------------------------------------------
// Call back routine
static void CALLBACK
wave_callback (HWAVE hWave, UINT msg, DWORD instance, DWORD param1,
DWORD param2)
{
if (msg == WOM_DONE)
{
Audio *ptr = (Audio *) instance;
ptr->callback_sampledone ((void *) param1);
}
}
//------------------------------------------------------------------------
// /dev/dsp handler
static Audio *s_audio; // static instance of the Audio handler
//------------------------------------------------------------------------
// wav file detection..
#pragma pack(1)
struct wavchunk
{
char id[4];
unsigned int len;
};
struct wavformat
{
unsigned short wFormatTag;
unsigned short wChannels;
unsigned int dwSamplesPerSec;
unsigned int dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
};
#pragma pack()
bool
fhandler_dev_dsp::setupwav (const char *pData, int nBytes)
{
int len;
const char *end = pData + nBytes;
if (!(pData[0] == 'R' && pData[1] == 'I' &&
pData[2] == 'F' && pData[3] == 'F'))
return false;
if (!(pData[8] == 'W' && pData[9] == 'A' &&
pData[10] == 'V' && pData[11] == 'E'))
return false;
len = *(int *) &pData[4];
pData += 12;
while (len && pData < end)
{
wavchunk * pChunk = (wavchunk *) pData;
int blklen = pChunk-> len;
if (pChunk->id[0] == 'f' && pChunk->id[1] == 'm' &&
pChunk->id[2] == 't' && pChunk->id[3] == ' ')
{
wavformat *format = (wavformat *) (pChunk + 1);
if ((char *) (format + 1) > end)
return false;
// Open up audio device with correct frequency for wav file
//
// FIXME: should through away all the header & not output
// it to the soundcard.
s_audio->close ();
if (s_audio->open (format->dwSamplesPerSec, format->wBitsPerSample,
format->wChannels) == false)
{
s_audio->open (audiofreq_, audiobits_, audiochannels_);
}
else
{
audiofreq_ = format->dwSamplesPerSec;
audiobits_ = format->wBitsPerSample;
audiochannels_ = format->wChannels;
}
return true;
}
pData += blklen + sizeof (wavchunk);
}
return false;
}
//------------------------------------------------------------------------
fhandler_dev_dsp::fhandler_dev_dsp ():
fhandler_base ()
{
}
fhandler_dev_dsp::~fhandler_dev_dsp ()
{
}
int
fhandler_dev_dsp::open (int flags, mode_t mode)
{
// currently we only support writing
if ((flags & (O_WRONLY | O_RDONLY | O_RDWR)) != O_WRONLY)
{
set_errno (EACCES);
return 0;
}
set_flags ((flags & ~O_TEXT) | O_BINARY);
if (!s_audio)
s_audio = new (audio_buf) Audio;
// Work out initial sample format & frequency
// dev/dsp defaults
audioformat_ = AFMT_S8;
audiofreq_ = 8000;
audiobits_ = 8;
audiochannels_ = 1;
int res;
if (!s_audio->open (audiofreq_, audiobits_, audiochannels_))
res = 0;
else
{
set_open_status ();
res = 1;
}
debug_printf ("returns %d", res);
return res;
}
int
fhandler_dev_dsp::write (const void *ptr, size_t len)
{
if (s_audio->numbytesoutput () == 0)
{
// check for wave file & setup frequencys properly if possible.
setupwav ((const char *) ptr, len);
// Open audio device properly with callbacks.
s_audio->close ();
if (!s_audio->open (audiofreq_, audiobits_, audiochannels_, true))
return 0;
}
s_audio->write (ptr, len);
return len;
}
void __stdcall
fhandler_dev_dsp::read (void *ptr, size_t& len)
{
return;
}
_off64_t
fhandler_dev_dsp::lseek (_off64_t offset, int whence)
{
return 0;
}
int
fhandler_dev_dsp::close (void)
{
s_audio->close ();
return 0;
}
int
fhandler_dev_dsp::dup (fhandler_base * child)
{
fhandler_dev_dsp *fhc = (fhandler_dev_dsp *) child;
fhc->set_flags (get_flags ());
fhc->audiochannels_ = audiochannels_;
fhc->audiobits_ = audiobits_;
fhc->audiofreq_ = audiofreq_;
fhc->audioformat_ = audioformat_;
return 0;
}
int
fhandler_dev_dsp::ioctl (unsigned int cmd, void *ptr)
{
int *intptr = (int *) ptr;
switch (cmd)
{
#define CASE(a) case a : debug_printf("/dev/dsp: ioctl %s", #a);
CASE (SNDCTL_DSP_RESET)
audioformat_ = AFMT_S8;
audiofreq_ = 8000;
audiobits_ = 8;
audiochannels_ = 1;
return 0;
CASE (SNDCTL_DSP_GETBLKSIZE)
*intptr = Audio::BLOCK_SIZE;
return 0;
CASE (SNDCTL_DSP_SETFMT)
{
int nBits = 0;
if (*intptr == AFMT_S16_LE)
nBits = 16;
else if (*intptr == AFMT_U8)
nBits = 8;
else if (*intptr == AFMT_S8)
nBits = 8;
if (nBits)
{
s_audio->setformat (*intptr);
s_audio->close ();
if (s_audio->open (audiofreq_, nBits, audiochannels_) == true)
{
audiobits_ = nBits;
return 0;
}
else
{
s_audio->open (audiofreq_, audiobits_, audiochannels_);
return -1;
}
}
}
break;
CASE (SNDCTL_DSP_SPEED)
s_audio->close ();
if (s_audio->open (*intptr, audiobits_, audiochannels_) == true)
{
audiofreq_ = *intptr;
return 0;
}
else
{
s_audio->open (audiofreq_, audiobits_, audiochannels_);
return -1;
}
break;
CASE (SNDCTL_DSP_STEREO)
{
int nChannels = *intptr + 1;
s_audio->close ();
if (s_audio->open (audiofreq_, audiobits_, nChannels) == true)
{
audiochannels_ = nChannels;
return 0;
}
else
{
s_audio->open (audiofreq_, audiobits_, audiochannels_);
return -1;
}
}
break;
CASE (SNDCTL_DSP_GETOSPACE)
{
audio_buf_info *p = (audio_buf_info *) ptr;
int nBlocks = s_audio->blocks ();
int leftblocks = ((Audio::MAX_BLOCKS - nBlocks) - 1);
if (leftblocks < 0)
leftblocks = 0;
if (leftblocks > 1)
leftblocks = 1;
int left = leftblocks * Audio::BLOCK_SIZE;
p->fragments = leftblocks;
p->fragstotal = Audio::MAX_BLOCKS;
p->fragsize = Audio::BLOCK_SIZE;
p->bytes = left;
debug_printf ("ptr %p nblocks %d leftblocks %d left bytes %d ",
ptr, nBlocks, leftblocks, left);
return 0;
}
break;
CASE (SNDCTL_DSP_SETFRAGMENT)
{
// Fake!! esound & mikmod require this on non PowerPC platforms.
//
return 0;
}
break;
CASE (SNDCTL_DSP_GETFMTS)
{
*intptr = AFMT_S16_LE | AFMT_U8 | AFMT_S8; // more?
return 0;
}
break;
default:
debug_printf ("/dev/dsp: ioctl not handled yet! FIXME:");
break;
#undef CASE
};
return -1;
}
void
fhandler_dev_dsp::dump ()
{
paranoid_printf ("here, fhandler_dev_dsp");
}
void
fhandler_dev_dsp::fixup_after_exec ()
{
/* FIXME: Is there a better way to do this? */
s_audio = new (audio_buf) Audio;
}
|