1 /////////////////////////////////////////////////////////////////////////
2 // $Id: soundwin.cc,v 1.22 2008/08/24 17:28:42 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2001 MandrakeSoft S.A.
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /////////////////////////////////////////////////////////////////////////
28 // This file (SOUNDWIN.CC) written and donated by Josef Drexler
30 // Define BX_PLUGGABLE in files that can be compiled into plugins. For
31 // platforms that require a special tag on exported symbols, BX_PLUGGABLE
32 // is used to know when we are exporting symbols and when we are importing.
35 #define NO_DEVICE_INCLUDES
40 #if defined(WIN32) && BX_SUPPORT_SB16
44 #define LOG_THIS sb16->
46 bx_sound_windows_c::bx_sound_windows_c(bx_sb16_c
*sb16
)
47 :bx_sound_output_c(sb16
)
57 // size is the total size of the midi header and buffer and the
58 // BX_SOUND_WINDOWS_NBUF wave header and buffers, all aligned
59 // on a 16-byte boundary
61 #define ALIGN(size) ((size + 15) & ~15)
63 #define size ALIGN(sizeof(MIDIHDR)) \
64 + ALIGN(sizeof(WAVEHDR)) \
65 + ALIGN(BX_SOUND_WINDOWS_MAXSYSEXLEN) * BX_SOUND_WINDOWS_NBUF \
66 + ALIGN(BX_SOUND_OUTPUT_WAVEPACKETSIZE) * BX_SOUND_WINDOWS_NBUF
68 DataHandle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
69 DataPointer
= (Bit8u
*) GlobalLock(DataHandle
);
71 if (DataPointer
== NULL
)
72 BX_PANIC(("GlobalLock returned NULL-pointer"));
74 #define NEWBUFFER(size) &(DataPointer[offset]); offset += ALIGN(size)
77 MidiHeader
= (LPMIDIHDR
) NEWBUFFER(sizeof(MIDIHDR
));
78 MidiData
= (LPSTR
) NEWBUFFER(BX_SOUND_WINDOWS_MAXSYSEXLEN
);
80 for (int bufnum
=0; bufnum
<BX_SOUND_WINDOWS_NBUF
; bufnum
++)
82 WaveHeader
[bufnum
] = (LPWAVEHDR
) NEWBUFFER(sizeof(WAVEHDR
));
83 WaveData
[bufnum
] = (LPSTR
) NEWBUFFER(BX_SOUND_OUTPUT_WAVEPACKETSIZE
+64);
87 BX_PANIC(("Allocated memory was too small!"));
94 bx_sound_windows_c::~bx_sound_windows_c()
96 GlobalUnlock(DataHandle
);
97 GlobalFree(DataHandle
);
100 int bx_sound_windows_c::waveready()
102 if (iswaveready
== 0)
105 if (iswaveready
== 1)
106 return BX_SOUND_OUTPUT_OK
;
108 return BX_SOUND_OUTPUT_ERR
;
110 int bx_sound_windows_c::midiready()
112 if (ismidiready
== 0)
115 if (ismidiready
== 1)
116 return BX_SOUND_OUTPUT_OK
;
118 return BX_SOUND_OUTPUT_ERR
;
121 int bx_sound_windows_c::openmidioutput(char *device
)
123 // could make the output device selectable,
124 // but currently only the midi mapper is supported
127 UINT deviceid
= (UINT
) MIDIMAPPER
;
131 UINT ret
= midiOutOpen(&MidiOut
, deviceid
, 0, 0, CALLBACK_NULL
);
135 WRITELOG(MIDILOG(4), "midiOutOpen() = %d, MidiOpen: %d", ret
, MidiOpen
);
137 return (MidiOpen
== 1) ? BX_SOUND_OUTPUT_OK
: BX_SOUND_OUTPUT_ERR
;
140 int bx_sound_windows_c::sendmidicommand(int delta
, int command
, int length
, Bit8u data
[])
145 return BX_SOUND_OUTPUT_ERR
;
147 if ((command
== 0xf0) || (command
== 0xf7) || (length
> 3))
149 WRITELOG(WAVELOG(5), "SYSEX started, length %d", length
);
150 ismidiready
= 0; // until the buffer is done
151 memcpy(MidiData
, data
, length
);
152 MidiHeader
->lpData
= MidiData
;
153 MidiHeader
->dwBufferLength
= BX_SOUND_WINDOWS_MAXSYSEXLEN
;
154 MidiHeader
->dwBytesRecorded
= 0;
155 MidiHeader
->dwUser
= 0;
156 MidiHeader
->dwFlags
= 0;
157 ret
= midiOutPrepareHeader(MidiOut
, MidiHeader
, sizeof(*MidiHeader
));
159 WRITELOG(MIDILOG(2), "midiOutPrepareHeader() = %d", ret
);
160 ret
= midiOutLongMsg(MidiOut
, MidiHeader
, sizeof(*MidiHeader
));
162 WRITELOG(MIDILOG(2), "midiOutLongMsg() = %d", ret
);
168 for (int i
= 0; i
<length
; i
++)
169 msg
|= (data
[i
] << (8 * (i
+ 1)));
171 ret
= midiOutShortMsg(MidiOut
, msg
);
172 WRITELOG(MIDILOG(4), "midiOutShortMsg(%x) = %d", msg
, ret
);
175 return (ret
== 0) ? BX_SOUND_OUTPUT_OK
: BX_SOUND_OUTPUT_ERR
;
178 int bx_sound_windows_c::closemidioutput()
183 return BX_SOUND_OUTPUT_ERR
;
185 ret
= midiOutReset(MidiOut
);
186 if (ismidiready
== 0)
187 checkmidiready(); // to clear any pending SYSEX
189 ret
= midiOutClose(MidiOut
);
190 WRITELOG(MIDILOG(4), "midiOutClose() = %d", ret
);
193 return (ret
== 0) ? BX_SOUND_OUTPUT_OK
: BX_SOUND_OUTPUT_ERR
;
196 int bx_sound_windows_c::openwaveoutput(char *device
)
198 // could make the output device selectable,
199 // but currently only the midi mapper is supported
202 WRITELOG(WAVELOG(4), "openwaveoutput(%s)", device
);
205 WaveDevice
= (UINT
) WAVEMAPPER
;
207 for (int i
=0; i
<BX_SOUND_WINDOWS_NBUF
; i
++)
208 WaveHeader
[i
]->dwFlags
= WHDR_DONE
;
216 return BX_SOUND_OUTPUT_OK
;
219 int bx_sound_windows_c::playnextbuffer()
222 PCMWAVEFORMAT waveformat
;
225 // if the format is different, we have to reopen the device,
229 ret
= waveOutReset(WaveOut
);
231 // clean up the buffers and mark if output is ready
234 // do we have to play anything?
235 if (tailplay
== head
)
236 return BX_SOUND_OUTPUT_OK
;
238 // if the format is different, we have to close and reopen the device
239 // or, just open the device if it's not open yet
240 if ((needreopen
!= 0) || (WaveOpen
== 0))
244 ret
= waveOutClose(WaveOut
);
248 // try three times to find a suitable format
249 for (int tries
= 0; tries
< 3; tries
++)
251 int frequency
= WaveInfo
.frequency
;
252 int stereo
= WaveInfo
.stereo
;
253 int bits
= WaveInfo
.bits
;
254 // int format = WaveInfo.format;
255 int bps
= (bits
/ 8) * (stereo
+ 1);
257 waveformat
.wf
.wFormatTag
= WAVE_FORMAT_PCM
;
258 waveformat
.wf
.nChannels
= stereo
+ 1;
259 waveformat
.wf
.nSamplesPerSec
= frequency
;
260 waveformat
.wf
.nAvgBytesPerSec
= frequency
* bps
;
261 waveformat
.wf
.nBlockAlign
= bps
;
262 waveformat
.wBitsPerSample
= bits
;
264 ret
= waveOutOpen(&(WaveOut
), WaveDevice
, (LPWAVEFORMATEX
)&(waveformat
.wf
), 0, 0, CALLBACK_NULL
);
267 char errormsg
[4*MAXERRORLENGTH
+1];
268 waveOutGetErrorTextA(ret
, errormsg
, 4*MAXERRORLENGTH
+1);
269 WRITELOG(WAVELOG(5), "waveOutOpen: %s", errormsg
);
271 case 0: // maybe try a different frequency
272 if (frequency
< 15600)
274 else if (frequency
< 31200)
279 WRITELOG(WAVELOG(4), "Couldn't open wave device (error %d), trying frequency %d", ret
, frequency
);
282 case 1: // or something else
288 WRITELOG(WAVELOG(4), "Couldn't open wave device again (error %d), trying 11KHz, mono, 8bit", ret
);
291 case 2: // nope, doesn't work
292 WRITELOG(WAVELOG(2), "Couldn't open wave device (error %d)!", ret
);
293 return BX_SOUND_OUTPUT_ERR
;
296 WRITELOG(WAVELOG(5), "The format was: wFormatTag=%d, nChannels=%d, nSamplesPerSec=%d,",
297 waveformat
.wf
.wFormatTag
, waveformat
.wf
.nChannels
, waveformat
.wf
.nSamplesPerSec
);
298 WRITELOG(WAVELOG(5), " nAvgBytesPerSec=%d, nBlockAlign=%d, wBitsPerSample=%d",
299 waveformat
.wf
.nAvgBytesPerSec
, waveformat
.wf
.nBlockAlign
, waveformat
.wBitsPerSample
);
310 for (bufnum
=tailplay
; bufnum
!= head
;
311 bufnum
++, bufnum
&= BX_SOUND_WINDOWS_NMASK
, tailplay
=bufnum
)
313 WRITELOG(WAVELOG(5), "Playing buffer %d", bufnum
);
315 // prepare the wave header
316 WaveHeader
[bufnum
]->lpData
= WaveData
[bufnum
];
317 WaveHeader
[bufnum
]->dwBufferLength
= length
[bufnum
];
318 WaveHeader
[bufnum
]->dwBytesRecorded
= length
[bufnum
];
319 WaveHeader
[bufnum
]->dwUser
= 0;
320 WaveHeader
[bufnum
]->dwFlags
= 0;
321 WaveHeader
[bufnum
]->dwLoops
= 1;
323 ret
= waveOutPrepareHeader(WaveOut
, WaveHeader
[bufnum
], sizeof(*WaveHeader
[bufnum
]));
326 WRITELOG(WAVELOG(2), "waveOutPrepareHeader = %d", ret
);
327 return BX_SOUND_OUTPUT_ERR
;
330 ret
= waveOutWrite(WaveOut
, WaveHeader
[bufnum
], sizeof(*WaveHeader
[bufnum
]));
333 char errormsg
[4*MAXERRORLENGTH
+1];
334 waveOutGetErrorTextA(ret
, errormsg
, 4*MAXERRORLENGTH
+1);
335 WRITELOG(WAVELOG(5), "waveOutWrite: %s", errormsg
);
339 return BX_SOUND_OUTPUT_OK
;
342 int bx_sound_windows_c::startwaveplayback(int frequency
, int bits
, int stereo
, int format
)
344 WRITELOG(WAVELOG(4), "startwaveplayback(%d, %d, %d, %x)", frequency
, bits
, stereo
, format
);
347 // check if any of the properties have changed
348 if ((WaveInfo
.frequency
!= frequency
) ||
349 (WaveInfo
.bits
!= bits
) ||
350 (WaveInfo
.stereo
!= stereo
) ||
351 (WaveInfo
.format
!= format
))
355 // store the current settings to be used by sendwavepacket()
356 WaveInfo
.frequency
= frequency
;
357 WaveInfo
.bits
= bits
;
358 WaveInfo
.stereo
= stereo
;
359 WaveInfo
.format
= format
;
364 int bps
= (bits
/ 8) * (stereo
+ 1);
365 LPWAVEFILEHEADER header
= (LPWAVEFILEHEADER
) WaveData
[0];
367 memcpy(header
->RIFF
, "RIFF", 4);
368 memcpy(header
->TYPE
, "WAVE", 4);
369 memcpy(header
->chnk
, "fmt ", 4);
370 header
->chnklen
= 16;
371 header
->waveformat
.wf
.wFormatTag
= WAVE_FORMAT_PCM
;
372 header
->waveformat
.wf
.nChannels
= stereo
+ 1;
373 header
->waveformat
.wf
.nSamplesPerSec
= frequency
;
374 header
->waveformat
.wf
.nAvgBytesPerSec
= frequency
* bps
;
375 header
->waveformat
.wf
.nBlockAlign
= bps
;
376 header
->waveformat
.wBitsPerSample
= bits
;
377 memcpy(header
->chnk2
, "data", 4);
380 return BX_SOUND_OUTPUT_OK
;
383 int bx_sound_windows_c::sendwavepacket(int length
, Bit8u data
[])
392 WRITELOG(WAVELOG(4), "sendwavepacket(%d, %p)", length
, data
);
397 memcpy(WaveData
[bufnum
], data
, length
);
398 this->length
[bufnum
] = length
;
400 // select next buffer to write to
402 bufnum
&= BX_SOUND_WINDOWS_NMASK
;
404 if (((bufnum
+ 1) & BX_SOUND_WINDOWS_NMASK
) == tailfull
)
405 { // this should not actually happen!
406 WRITELOG(WAVELOG(2), "Output buffer overflow! Not played. Iswaveready was %d", iswaveready
);
407 iswaveready
= 0; // stop the output for a while
408 return BX_SOUND_OUTPUT_ERR
;
413 // check if more buffers are available, otherwise stall the emulator
414 if (((bufnum
+ 2) & BX_SOUND_WINDOWS_NMASK
) == tailfull
)
416 WRITELOG(WAVELOG(5), "Buffer status: Head %d, TailFull %d, TailPlay %d. Stall.",
417 head
, tailfull
, tailplay
);
425 LPWAVEFILEHEADER header
= (LPWAVEFILEHEADER
) WaveData
[0];
427 header
->length
= length
+ 36;
428 header
->chnk2len
= length
;
430 memcpy(&(header
->data
), data
, length
);
432 ret
= sndPlaySoundA((LPCSTR
) header
, SND_SYNC
| SND_MEMORY
);
435 WRITELOG(WAVELOG(3), "sndPlaySoundA: %d", ret
);
439 return BX_SOUND_OUTPUT_OK
;
442 int bx_sound_windows_c::stopwaveplayback()
444 WRITELOG(WAVELOG(4), "stopwaveplayback()");
447 // this is handled by checkwaveready() when closing
451 sndPlaySoundA(NULL
, SND_ASYNC
| SND_MEMORY
);
456 return BX_SOUND_OUTPUT_OK
;
459 int bx_sound_windows_c::closewaveoutput()
461 WRITELOG(WAVELOG(4), "closewaveoutput");
466 waveOutReset(WaveOut
);
468 // let checkwaveready() clean up the buffers
471 waveOutClose(WaveOut
);
480 return BX_SOUND_OUTPUT_OK
;
483 void bx_sound_windows_c::checkmidiready()
487 if ((MidiHeader
->dwFlags
& WHDR_DONE
) != 0)
489 WRITELOG(MIDILOG(5), "SYSEX message done, midi ready again.");
490 ret
= midiOutUnprepareHeader(MidiOut
, MidiHeader
, sizeof(*MidiHeader
));
494 void bx_sound_windows_c::checkwaveready()
499 // clean up all finished buffers and mark them as available
500 for (bufnum
=tailfull
; (bufnum
!= tailplay
) &&
501 ((WaveHeader
[bufnum
]->dwFlags
& WHDR_DONE
) != 0);
502 bufnum
++, bufnum
&= BX_SOUND_WINDOWS_NMASK
)
504 WRITELOG(WAVELOG(5), "Buffer %d done.", bufnum
);
505 ret
= waveOutUnprepareHeader(WaveOut
, WaveHeader
[bufnum
], sizeof(*WaveHeader
[bufnum
]));
510 // enable gathering data if a buffer is available
511 if (((head
+ 2) & BX_SOUND_WINDOWS_NMASK
) != tailfull
)
513 WRITELOG(WAVELOG(5), "Buffer status: Head %d, TailFull %d, TailPlay %d. Ready.",
514 head
, tailfull
, tailplay
);
519 #endif // defined(WIN32)