aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ioemu/iodev/soundwin.cc
blob: b8b386cae4ea3e10574899cda34c786a0eabba7e (plain)
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
/////////////////////////////////////////////////////////////////////////
// $Id: soundwin.cc,v 1.13 2003/04/05 08:26:49 vruppert Exp $
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2001  MandrakeSoft S.A.
//
//    MandrakeSoft S.A.
//    43, rue d'Aboukir
//    75002 Paris - France
//    http://www.linux-mandrake.com/
//    http://www.mandrakesoft.com/
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

// This file (SOUNDWIN.CC) written and donated by Josef Drexler

// Define BX_PLUGGABLE in files that can be compiled into plugins.  For
// platforms that require a special tag on exported symbols, BX_PLUGGABLE 
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE

#include "bochs.h"
#if defined(WIN32) && BX_SUPPORT_SB16

#define LOG_THIS bx_devices.pluginSB16Device->

bx_sound_windows_c::bx_sound_windows_c(bx_sb16_c *sb16)
  :bx_sound_output_c(sb16)
{
  this->sb16 = sb16;

  MidiOpen = 0;
  WaveOpen = 0;

  ismidiready = 1;
  iswaveready = 1;

  // size is the total size of the midi header and buffer and the
  // BX_SOUND_WINDOWS_NBUF wave header and buffers, all aligned
  // on a 16-byte boundary

#define ALIGN(size) ( (size + 15) & ~15 )

#define size   ALIGN(sizeof(MIDIHDR)) \
	     + ALIGN(sizeof(WAVEHDR)) \
	     + ALIGN(BX_SOUND_WINDOWS_MAXSYSEXLEN) * BX_SOUND_WINDOWS_NBUF \
	     + ALIGN(BX_SOUND_OUTPUT_WAVEPACKETSIZE) * BX_SOUND_WINDOWS_NBUF

  DataHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
  DataPointer = (Bit8u*) GlobalLock(DataHandle);

  if (DataPointer == NULL)
    BX_PANIC(("GlobalLock returned NULL-pointer"));

#define NEWBUFFER(size) &(DataPointer[offset]); offset += ALIGN(size)

  int offset = 0;
  MidiHeader = (LPMIDIHDR) NEWBUFFER(sizeof(MIDIHDR));
  MidiData = (LPSTR) NEWBUFFER(BX_SOUND_WINDOWS_MAXSYSEXLEN);

  for (int bufnum=0; bufnum<BX_SOUND_WINDOWS_NBUF; bufnum++)
    {
      WaveHeader[bufnum] = (LPWAVEHDR) NEWBUFFER(sizeof(WAVEHDR));
      WaveData[bufnum] = (LPSTR) NEWBUFFER(BX_SOUND_OUTPUT_WAVEPACKETSIZE);
    }

  if (offset > size)
    BX_PANIC(("Allocated memory was too small!"));

#undef size
#undef ALIGN
#undef NEWBUFFER
}

bx_sound_windows_c::~bx_sound_windows_c()
{
  GlobalUnlock(DataHandle);
  GlobalFree(DataHandle);
}

int bx_sound_windows_c::waveready()
{
  if (iswaveready == 0)
    checkwaveready();

  if (iswaveready == 1)
    return BX_SOUND_OUTPUT_OK;
  else
    return BX_SOUND_OUTPUT_ERR;
}
int bx_sound_windows_c::midiready()
{
  if (ismidiready == 0)
    checkmidiready();

  if (ismidiready == 1)
    return BX_SOUND_OUTPUT_OK;
  else
    return BX_SOUND_OUTPUT_ERR;
}

int bx_sound_windows_c::openmidioutput(char *device)
{
  // could make the output device selectable,
  // but currently only the midi mapper is supported
  UNUSED(device);

  UINT deviceid = (UINT) MIDIMAPPER;

  MidiOpen = 0;

  UINT ret = midiOutOpen( &MidiOut, deviceid, 0, 0, CALLBACK_NULL);
  if (ret == 0)
    MidiOpen = 1;

  WRITELOG( MIDILOG(4), "midiOutOpen() = %d, MidiOpen: %d", ret, MidiOpen);

  return (MidiOpen == 1) ? BX_SOUND_OUTPUT_OK : BX_SOUND_OUTPUT_ERR;
}

int bx_sound_windows_c::sendmidicommand(int delta, int command, int length, Bit8u data[])
{
  UINT ret;

  if (MidiOpen != 1)
    return BX_SOUND_OUTPUT_ERR;

  if ( (command == 0xf0) || (command == 0xf7) || (length > 3) )
    {
      WRITELOG( WAVELOG(5), "SYSEX started, length %d", length);
      ismidiready = 0;   // until the buffer is done
      memcpy(MidiData, data, length);
      MidiHeader->lpData = MidiData;
      MidiHeader->dwBufferLength = BX_SOUND_WINDOWS_MAXSYSEXLEN;
      MidiHeader->dwBytesRecorded = 0;
      MidiHeader->dwUser = 0;
      MidiHeader->dwFlags = 0;
      ret = midiOutPrepareHeader(MidiOut, MidiHeader, sizeof(*MidiHeader));
      if (ret != 0)
	WRITELOG( MIDILOG(2), "midiOutPrepareHeader() = %d", ret);
      ret = midiOutLongMsg(MidiOut, MidiHeader, sizeof(*MidiHeader));
      if (ret != 0)
	WRITELOG( MIDILOG(2), "midiOutLongMsg() = %d", ret);
    }
  else
    {
      DWORD msg = command;

      for (int i = 0; i<length; i++)
	msg |= (data[i] << (8 * (i + 1) ) );

      ret = midiOutShortMsg(MidiOut, msg);
      WRITELOG( MIDILOG(4), "midiOutShortMsg(%x) = %d", msg, ret);
    }

  return (ret == 0) ? BX_SOUND_OUTPUT_OK : BX_SOUND_OUTPUT_ERR;
}

int bx_sound_windows_c::closemidioutput()
{
  UINT ret;

  if (MidiOpen != 1)
    return BX_SOUND_OUTPUT_ERR;

  ret = midiOutReset(MidiOut);
  if (ismidiready == 0)
    checkmidiready();   // to clear any pending SYSEX

  ret = midiOutClose(MidiOut);
  WRITELOG( MIDILOG(4), "midiOutClose() = %d", ret);
  MidiOpen = 0;

  return (ret == 0) ? BX_SOUND_OUTPUT_OK : BX_SOUND_OUTPUT_ERR;
}

int bx_sound_windows_c::openwaveoutput(char *device)
{
  // could make the output device selectable,
  // but currently only the midi mapper is supported
  UNUSED(device);

  WRITELOG( WAVELOG(4), "openwaveoutput(%s)", device);

#ifdef usewaveOut
  WaveDevice = (UINT) WAVEMAPPER;

  for (int i=0; i<BX_SOUND_WINDOWS_NBUF; i++)
    WaveHeader[i]->dwFlags = WHDR_DONE;

  head = 0;
  tailfull = 0;
  tailplay = 0;
  needreopen = 0;
#endif

  return BX_SOUND_OUTPUT_OK;
}

int bx_sound_windows_c::playnextbuffer()
{
  UINT ret;
  PCMWAVEFORMAT waveformat;
  int bufnum;

  // if the format is different, we have to reopen the device,
  // so reset it first
  if (needreopen != 0)
    if (WaveOpen != 0)
      ret = waveOutReset( WaveOut );

  // clean up the buffers and mark if output is ready
  checkwaveready();

  // do we have to play anything?
  if (tailplay == head)
    return BX_SOUND_OUTPUT_OK;

  // if the format is different, we have to close and reopen the device
  // or, just open the device if it's not open yet
  if ( (needreopen != 0) || (WaveOpen == 0) )
    {
      if (WaveOpen != 0)
	{
	  ret = waveOutClose( WaveOut );
	  WaveOpen = 0;
	}

      // try three times to find a suitable format
      for (int tries = 0; tries < 3; tries++)
	{
	  int frequency = WaveInfo.frequency;
	  int stereo = WaveInfo.stereo;
	  int bits = WaveInfo.bits;
	  int format = WaveInfo.format;
	  int bps = (bits / 8) * (stereo + 1);

	  waveformat.wf.wFormatTag = WAVE_FORMAT_PCM;
	  waveformat.wf.nChannels = stereo + 1;
	  waveformat.wf.nSamplesPerSec = frequency;
	  waveformat.wf.nAvgBytesPerSec = frequency * bps;
	  waveformat.wf.nBlockAlign = bps;
	  waveformat.wBitsPerSample = bits;

	  ret = waveOutOpen( &(WaveOut), WaveDevice, (LPWAVEFORMATEX)&(waveformat.wf), 0, 0, CALLBACK_NULL);
	  if (ret != 0)
	    {
	      char errormsg[4*MAXERRORLENGTH+1];
	      waveOutGetErrorTextA(ret, errormsg, 4*MAXERRORLENGTH+1);
	      WRITELOG( WAVELOG(5), "waveOutOpen: %s", errormsg);
	      switch (tries)
		{
		case 0:	// maybe try a different frequency
		  if (frequency < 15600)
		    frequency = 11025;
		  else if (frequency < 31200)
		    frequency = 22050;
		  else
		    frequency = 44100;

		  WRITELOG( WAVELOG(4), "Couldn't open wave device (error %d), trying frequency %d", ret, frequency);

		  break;
		case 1:	// or something else
		  frequency = 11025;
		  stereo = 0;
		  bits = 8;
		  bps = 1;

		  WRITELOG( WAVELOG(4), "Couldn't open wave device again (error %d), trying 11KHz, mono, 8bit", ret);

		  break;
		case 2:	// nope, doesn't work

		  WRITELOG( WAVELOG(2), "Couldn't open wave device (error %d)!", ret);

		  return BX_SOUND_OUTPUT_ERR;
		}
	      WRITELOG( WAVELOG(5), "The format was: wFormatTag=%d, nChannels=%d, nSamplesPerSec=%d,",
			waveformat.wf.wFormatTag, waveformat.wf.nChannels, waveformat.wf.nSamplesPerSec);
	      WRITELOG( WAVELOG(5), "                nAvgBytesPerSec=%d, nBlockAlign=%d, wBitsPerSample=%d",
			waveformat.wf.nAvgBytesPerSec, waveformat.wf.nBlockAlign, waveformat.wBitsPerSample);

	    }
	  else
	    {
	      WaveOpen = 1;
	      needreopen = 0;
	      break;
	    }
	}
    }

  for (bufnum=tailplay; bufnum != head;
       bufnum++, bufnum &= BX_SOUND_WINDOWS_NMASK, tailplay=bufnum)
    {
      WRITELOG( WAVELOG(5), "Playing buffer %d", bufnum);

      // prepare the wave header
      WaveHeader[bufnum]->lpData = WaveData[bufnum];
      WaveHeader[bufnum]->dwBufferLength = length[bufnum];
      WaveHeader[bufnum]->dwBytesRecorded = length[bufnum];
      WaveHeader[bufnum]->dwUser = 0;
      WaveHeader[bufnum]->dwFlags = 0;
      WaveHeader[bufnum]->dwLoops = 1;

      ret = waveOutPrepareHeader(WaveOut, WaveHeader[bufnum], sizeof(*WaveHeader[bufnum]));
      if (ret != 0)
	{
	  WRITELOG( WAVELOG(2), "waveOutPrepareHeader = %d", ret);
	  return BX_SOUND_OUTPUT_ERR;
	}

      ret = waveOutWrite(WaveOut, WaveHeader[bufnum], sizeof(*WaveHeader[bufnum]));
      if (ret != 0)
	{
	  char errormsg[4*MAXERRORLENGTH+1];
	  waveOutGetErrorTextA(ret, errormsg, 4*MAXERRORLENGTH+1);
	  WRITELOG( WAVELOG(5), "waveOutWrite: %s", errormsg);
	}
    }
    return BX_SOUND_OUTPUT_OK;
}

int bx_sound_windows_c::startwaveplayback(int frequency, int bits, int stereo, int format)
{
  // UINT ret;

  WRITELOG( WAVELOG(4), "startwaveplayback(%d, %d, %d, %x)", frequency, bits, stereo, format);

#ifdef usewaveOut
  // check if any of the properties have changed
  if ( (WaveInfo.frequency != frequency) ||
       (WaveInfo.bits != bits) ||
       (WaveInfo.stereo != stereo) ||
       (WaveInfo.format != format) )
    {
      needreopen = 1;

      // store the current settings to be used by sendwavepacket()
      WaveInfo.frequency = frequency;
      WaveInfo.bits = bits;
      WaveInfo.stereo = stereo;
      WaveInfo.format = format;
    }
#endif

#ifdef usesndPlaySnd
  int bps = (bits / 8) * (stereo + 1);
  LPWAVEFILEHEADER header = (LPWAVEFILEHEADER) WaveData;

  memcpy(header->RIFF, "RIFF", 4);
  memcpy(header->TYPE, "WAVE", 4);
  memcpy(header->chnk, "fmt ", 4);
  header->chnklen = 16;
  header->waveformat.wf.wFormatTag = WAVE_FORMAT_PCM;
  header->waveformat.wf.nChannels = stereo + 1;
  header->waveformat.wf.nSamplesPerSec = frequency;
  header->waveformat.wf.nAvgBytesPerSec = frequency * bps;
  header->waveformat.wf.nBlockAlign = bps;
  header->waveformat.wBitsPerSample = bits;
  memcpy(header->chnk2, "data", 4);
#endif

  return BX_SOUND_OUTPUT_OK;
}

int bx_sound_windows_c::sendwavepacket(int length, Bit8u data[])
{
//  UINT ret;
  int bufnum;

  WRITELOG( WAVELOG(4), "sendwavepacket(%d, %p)", length, data);

#ifdef usewaveOut
  bufnum = head;

  memcpy(WaveData[bufnum], data, length);
  this->length[bufnum] = length;

  // select next buffer to write to
  bufnum++;
  bufnum &= BX_SOUND_WINDOWS_NMASK;

  if ( ( (bufnum + 1) & BX_SOUND_WINDOWS_NMASK) == tailfull )
    {   // this should not actually happen!
      WRITELOG( WAVELOG(2), "Output buffer overflow! Not played. Iswaveready was %d", iswaveready);
      iswaveready = 0;          // stop the output for a while
      return BX_SOUND_OUTPUT_ERR;
    }

  head = bufnum;

  // check if more buffers are available, otherwise stall the emulator
  if ( ( (bufnum + 2) & BX_SOUND_WINDOWS_NMASK) == tailfull )
    {
      WRITELOG( WAVELOG(5), "Buffer status: Head %d, TailFull %d, TailPlay %d. Stall.",
		head, tailfull, tailplay);
      iswaveready = 0;
    }

  playnextbuffer();

#endif

#ifdef usesndPlaySnd
  LPWAVEFILEHEADER header = (LPWAVEFILEHEADER) WaveData;

  header->length = length + 36;
  header->chnk2len = length;

  memcpy( &(header->data), data, length);

  FILE *test = fopen("test", "a");
  fwrite(WaveData, 1, length + 44, test);
  fclose(test);

  ret = sndPlaySoundA( (LPCSTR) header, SND_SYNC | SND_MEMORY );
  if (ret != 0)
    {
      WRITELOG( WAVELOG(3), "sndPlaySoundA: %d", ret);
    }
#endif

  return BX_SOUND_OUTPUT_OK;
}

int bx_sound_windows_c::stopwaveplayback()
{
  WRITELOG( WAVELOG(4), "stopwaveplayback()");

#ifdef usewaveOut
  // this is handled by checkwaveready() when closing
#endif

#ifdef usesndPlaySnd
  sndPlaySoundA( NULL, SND_ASYNC | SND_MEMORY );

  WaveOpen = 0;
#endif

  return BX_SOUND_OUTPUT_OK;
}

int bx_sound_windows_c::closewaveoutput()
{
//  int bufnum;

  WRITELOG( WAVELOG(4), "closewaveoutput");

#ifdef usewaveOut
  if (WaveOpen == 1)
    {
      waveOutReset(WaveOut);

      // let checkwaveready() clean up the buffers
      checkwaveready();

      waveOutClose(WaveOut);

      head = 0;
      tailfull = 0;
      tailplay = 0;
      needreopen = 0;
    }
#endif

  return BX_SOUND_OUTPUT_OK;
}

void bx_sound_windows_c::checkmidiready()
{
  UINT ret;

  if ( (MidiHeader->dwFlags & WHDR_DONE) != 0)
    {
      WRITELOG( MIDILOG(5), "SYSEX message done, midi ready again.");
      ret = midiOutUnprepareHeader( MidiOut, MidiHeader, sizeof(*MidiHeader));
      ismidiready = 1;
    }
}
void bx_sound_windows_c::checkwaveready()
{
  int bufnum;
  UINT ret;

  // clean up all finished buffers and mark them as available
  for (bufnum=tailfull;
       (bufnum != tailplay) &&
	  ( (WaveHeader[bufnum]->dwFlags & WHDR_DONE) != 0);
       bufnum++, bufnum &= BX_SOUND_WINDOWS_NMASK)
    {
      WRITELOG( WAVELOG(5), "Buffer %d done.", bufnum);

      ret = waveOutUnprepareHeader(WaveOut, WaveHeader[bufnum], sizeof(*WaveHeader[bufnum]));
    }

  tailfull = bufnum;

  // enable gathering data if a buffer is available
  if ( ( (head + 2) & BX_SOUND_WINDOWS_NMASK) != tailfull )
    {
      WRITELOG( WAVELOG(5), "Buffer status: Head %d, TailFull %d, TailPlay %d. Ready.",
		head, tailfull, tailplay);
      iswaveready = 1;
    }
}

#endif // defined(WIN32)