Fixed compilation error
[bochs-mirror.git] / iodev / soundwin.cc
blob28dbe1f206dfdc9a7ca218263fdaab10d77e0f47
1 /////////////////////////////////////////////////////////////////////////
2 // $Id: soundwin.cc,v 1.22 2008/08/24 17:28:42 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (C) 2001 MandrakeSoft S.A.
6 //
7 // MandrakeSoft S.A.
8 // 43, rue d'Aboukir
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.
33 #define BX_PLUGGABLE
35 #define NO_DEVICE_INCLUDES
36 #include "iodev.h"
37 #define BX_SOUNDLOW
38 #include "sb16.h"
40 #if defined(WIN32) && BX_SUPPORT_SB16
42 #include "soundwin.h"
44 #define LOG_THIS sb16->
46 bx_sound_windows_c::bx_sound_windows_c(bx_sb16_c *sb16)
47 :bx_sound_output_c(sb16)
49 this->sb16 = sb16;
51 MidiOpen = 0;
52 WaveOpen = 0;
54 ismidiready = 1;
55 iswaveready = 1;
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)
76 unsigned offset = 0;
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);
86 if (offset > size)
87 BX_PANIC(("Allocated memory was too small!"));
89 #undef size
90 #undef ALIGN
91 #undef NEWBUFFER
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)
103 checkwaveready();
105 if (iswaveready == 1)
106 return BX_SOUND_OUTPUT_OK;
107 else
108 return BX_SOUND_OUTPUT_ERR;
110 int bx_sound_windows_c::midiready()
112 if (ismidiready == 0)
113 checkmidiready();
115 if (ismidiready == 1)
116 return BX_SOUND_OUTPUT_OK;
117 else
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
125 UNUSED(device);
127 UINT deviceid = (UINT) MIDIMAPPER;
129 MidiOpen = 0;
131 UINT ret = midiOutOpen(&MidiOut, deviceid, 0, 0, CALLBACK_NULL);
132 if (ret == 0)
133 MidiOpen = 1;
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[])
142 UINT ret;
144 if (MidiOpen != 1)
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));
158 if (ret != 0)
159 WRITELOG(MIDILOG(2), "midiOutPrepareHeader() = %d", ret);
160 ret = midiOutLongMsg(MidiOut, MidiHeader, sizeof(*MidiHeader));
161 if (ret != 0)
162 WRITELOG(MIDILOG(2), "midiOutLongMsg() = %d", ret);
164 else
166 DWORD msg = command;
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()
180 UINT ret;
182 if (MidiOpen != 1)
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);
191 MidiOpen = 0;
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
200 UNUSED(device);
202 WRITELOG(WAVELOG(4), "openwaveoutput(%s)", device);
204 #ifdef usewaveOut
205 WaveDevice = (UINT) WAVEMAPPER;
207 for (int i=0; i<BX_SOUND_WINDOWS_NBUF; i++)
208 WaveHeader[i]->dwFlags = WHDR_DONE;
210 head = 0;
211 tailfull = 0;
212 tailplay = 0;
213 needreopen = 0;
214 #endif
216 return BX_SOUND_OUTPUT_OK;
219 int bx_sound_windows_c::playnextbuffer()
221 UINT ret;
222 PCMWAVEFORMAT waveformat;
223 int bufnum;
225 // if the format is different, we have to reopen the device,
226 // so reset it first
227 if (needreopen != 0)
228 if (WaveOpen != 0)
229 ret = waveOutReset(WaveOut);
231 // clean up the buffers and mark if output is ready
232 checkwaveready();
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))
242 if (WaveOpen != 0)
244 ret = waveOutClose(WaveOut);
245 WaveOpen = 0;
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);
265 if (ret != 0)
267 char errormsg[4*MAXERRORLENGTH+1];
268 waveOutGetErrorTextA(ret, errormsg, 4*MAXERRORLENGTH+1);
269 WRITELOG(WAVELOG(5), "waveOutOpen: %s", errormsg);
270 switch (tries) {
271 case 0: // maybe try a different frequency
272 if (frequency < 15600)
273 frequency = 11025;
274 else if (frequency < 31200)
275 frequency = 22050;
276 else
277 frequency = 44100;
279 WRITELOG(WAVELOG(4), "Couldn't open wave device (error %d), trying frequency %d", ret, frequency);
280 break;
282 case 1: // or something else
283 frequency = 11025;
284 stereo = 0;
285 bits = 8;
286 bps = 1;
288 WRITELOG(WAVELOG(4), "Couldn't open wave device again (error %d), trying 11KHz, mono, 8bit", ret);
289 break;
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);
301 else
303 WaveOpen = 1;
304 needreopen = 0;
305 break;
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]));
324 if (ret != 0)
326 WRITELOG(WAVELOG(2), "waveOutPrepareHeader = %d", ret);
327 return BX_SOUND_OUTPUT_ERR;
330 ret = waveOutWrite(WaveOut, WaveHeader[bufnum], sizeof(*WaveHeader[bufnum]));
331 if (ret != 0)
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);
346 #ifdef usewaveOut
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))
353 needreopen = 1;
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;
361 #endif
363 #ifdef usesndPlaySnd
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);
378 #endif
380 return BX_SOUND_OUTPUT_OK;
383 int bx_sound_windows_c::sendwavepacket(int length, Bit8u data[])
385 #ifdef usewaveOut
386 int bufnum;
387 #endif
388 #ifdef usesndPlaySnd
389 UINT ret;
390 #endif
392 WRITELOG(WAVELOG(4), "sendwavepacket(%d, %p)", length, data);
394 #ifdef usewaveOut
395 bufnum = head;
397 memcpy(WaveData[bufnum], data, length);
398 this->length[bufnum] = length;
400 // select next buffer to write to
401 bufnum++;
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;
411 head = bufnum;
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);
418 iswaveready = 0;
421 playnextbuffer();
422 #endif
424 #ifdef usesndPlaySnd
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);
433 if (ret != 0)
435 WRITELOG(WAVELOG(3), "sndPlaySoundA: %d", ret);
437 #endif
439 return BX_SOUND_OUTPUT_OK;
442 int bx_sound_windows_c::stopwaveplayback()
444 WRITELOG(WAVELOG(4), "stopwaveplayback()");
446 #ifdef usewaveOut
447 // this is handled by checkwaveready() when closing
448 #endif
450 #ifdef usesndPlaySnd
451 sndPlaySoundA(NULL, SND_ASYNC | SND_MEMORY);
453 WaveOpen = 0;
454 #endif
456 return BX_SOUND_OUTPUT_OK;
459 int bx_sound_windows_c::closewaveoutput()
461 WRITELOG(WAVELOG(4), "closewaveoutput");
463 #ifdef usewaveOut
464 if (WaveOpen == 1)
466 waveOutReset(WaveOut);
468 // let checkwaveready() clean up the buffers
469 checkwaveready();
471 waveOutClose(WaveOut);
473 head = 0;
474 tailfull = 0;
475 tailplay = 0;
476 needreopen = 0;
478 #endif
480 return BX_SOUND_OUTPUT_OK;
483 void bx_sound_windows_c::checkmidiready()
485 UINT ret;
487 if ((MidiHeader->dwFlags & WHDR_DONE) != 0)
489 WRITELOG(MIDILOG(5), "SYSEX message done, midi ready again.");
490 ret = midiOutUnprepareHeader(MidiOut, MidiHeader, sizeof(*MidiHeader));
491 ismidiready = 1;
494 void bx_sound_windows_c::checkwaveready()
496 int bufnum;
497 UINT ret;
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]));
508 tailfull = 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);
515 iswaveready = 1;
519 #endif // defined(WIN32)