2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2013-07-25 22:47:11 +04:00
|
|
|
// Copyright (C) 2001-2013 The Bochs Project
|
2001-04-10 05:04:59 +04:00
|
|
|
//
|
|
|
|
// 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
|
2009-02-08 12:05:52 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-02-16 01:05:43 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// This file (SOUNDWIN.CC) written and donated by Josef Drexler
|
|
|
|
|
2002-11-16 13:29:57 +03:00
|
|
|
// Define BX_PLUGGABLE in files that can be compiled into plugins. For
|
2008-01-27 01:24:03 +03:00
|
|
|
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
|
2002-11-16 13:29:57 +03:00
|
|
|
// is used to know when we are exporting symbols and when we are importing.
|
|
|
|
#define BX_PLUGGABLE
|
|
|
|
|
2011-02-15 00:14:20 +03:00
|
|
|
#include "iodev.h"
|
2011-02-11 01:58:22 +03:00
|
|
|
#include "soundmod.h"
|
2004-09-05 14:30:19 +04:00
|
|
|
#include "soundwin.h"
|
|
|
|
|
2011-08-18 11:05:09 +04:00
|
|
|
#if defined(WIN32) && BX_SUPPORT_SOUNDLOW
|
|
|
|
|
2013-06-24 23:19:12 +04:00
|
|
|
#define LOG_THIS
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2013-06-24 23:19:12 +04:00
|
|
|
bx_sound_windows_c::bx_sound_windows_c()
|
|
|
|
:bx_sound_lowlevel_c()
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
MidiOpen = 0;
|
2011-05-14 01:08:33 +04:00
|
|
|
WaveOutOpen = 0;
|
|
|
|
WaveInOpen = 0;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
#define ALIGN(size) ((size + 15) & ~15)
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#define size ALIGN(sizeof(MIDIHDR)) \
|
2011-05-14 01:08:33 +04:00
|
|
|
+ ALIGN(sizeof(WAVEHDR)) * (BX_SOUND_WINDOWS_NBUF + 1) \
|
|
|
|
+ ALIGN(BX_SOUND_WINDOWS_MAXSYSEXLEN) \
|
|
|
|
+ ALIGN(BX_SOUNDLOW_WAVEPACKETSIZE + 64) * (BX_SOUND_WINDOWS_NBUF + 1)
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
DataHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
|
|
|
|
DataPointer = (Bit8u*) GlobalLock(DataHandle);
|
|
|
|
|
|
|
|
if (DataPointer == NULL)
|
2001-05-20 07:18:10 +04:00
|
|
|
BX_PANIC(("GlobalLock returned NULL-pointer"));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#define NEWBUFFER(size) &(DataPointer[offset]); offset += ALIGN(size)
|
|
|
|
|
2008-08-24 21:28:42 +04:00
|
|
|
unsigned offset = 0;
|
2001-04-10 05:04:59 +04:00
|
|
|
MidiHeader = (LPMIDIHDR) NEWBUFFER(sizeof(MIDIHDR));
|
|
|
|
MidiData = (LPSTR) NEWBUFFER(BX_SOUND_WINDOWS_MAXSYSEXLEN);
|
|
|
|
|
|
|
|
for (int bufnum=0; bufnum<BX_SOUND_WINDOWS_NBUF; bufnum++)
|
2008-08-24 21:28:42 +04:00
|
|
|
{
|
2001-04-10 05:04:59 +04:00
|
|
|
WaveHeader[bufnum] = (LPWAVEHDR) NEWBUFFER(sizeof(WAVEHDR));
|
2011-03-20 21:02:12 +03:00
|
|
|
WaveData[bufnum] = (LPSTR) NEWBUFFER(BX_SOUNDLOW_WAVEPACKETSIZE+64);
|
2008-08-24 21:28:42 +04:00
|
|
|
}
|
2011-05-14 01:08:33 +04:00
|
|
|
WaveInHdr = (LPWAVEHDR) NEWBUFFER(sizeof(WAVEHDR));
|
|
|
|
WaveInData = (LPSTR) NEWBUFFER(BX_SOUNDLOW_WAVEPACKETSIZE+64);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
if (offset > size)
|
2001-05-20 07:18:10 +04:00
|
|
|
BX_PANIC(("Allocated memory was too small!"));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#undef size
|
|
|
|
#undef ALIGN
|
|
|
|
#undef NEWBUFFER
|
2011-01-26 02:29:08 +03:00
|
|
|
|
2013-07-25 22:47:11 +04:00
|
|
|
BX_INFO(("Sound lowlevel module 'win' initialized"));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
else
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
int bx_sound_windows_c::midiready()
|
|
|
|
{
|
|
|
|
if (ismidiready == 0)
|
|
|
|
checkmidiready();
|
|
|
|
|
|
|
|
if (ismidiready == 1)
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
else
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2011-02-13 20:26:52 +03:00
|
|
|
int bx_sound_windows_c::openmidioutput(const char *mididev)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
// could make the output device selectable,
|
|
|
|
// but currently only the midi mapper is supported
|
2011-01-26 02:29:08 +03:00
|
|
|
UNUSED(mididev);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
UINT deviceid = (UINT) MIDIMAPPER;
|
|
|
|
|
|
|
|
MidiOpen = 0;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
UINT ret = midiOutOpen(&MidiOut, deviceid, 0, 0, CALLBACK_NULL);
|
2001-04-10 05:04:59 +04:00
|
|
|
if (ret == 0)
|
|
|
|
MidiOpen = 1;
|
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("midiOutOpen() = %d, MidiOpen: %d", ret, MidiOpen));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return (MidiOpen == 1) ? BX_SOUNDLOW_OK : BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::sendmidicommand(int delta, int command, int length, Bit8u data[])
|
|
|
|
{
|
|
|
|
UINT ret;
|
|
|
|
|
|
|
|
if (MidiOpen != 1)
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
if ((command == 0xf0) || (command == 0xf7) || (length > 3))
|
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("SYSEX started, length %d", length));
|
2008-02-16 01:05:43 +03:00
|
|
|
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)
|
2011-05-20 21:39:32 +04:00
|
|
|
BX_ERROR(("midiOutPrepareHeader(): error = %d", ret));
|
2008-02-16 01:05:43 +03:00
|
|
|
ret = midiOutLongMsg(MidiOut, MidiHeader, sizeof(*MidiHeader));
|
|
|
|
if (ret != 0)
|
2011-05-20 21:39:32 +04:00
|
|
|
BX_ERROR(("midiOutLongMsg(): error = %d", ret));
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
else
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
|
|
|
DWORD msg = command;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
for (int i = 0; i<length; i++)
|
|
|
|
msg |= (data[i] << (8 * (i + 1)));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
ret = midiOutShortMsg(MidiOut, msg);
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("midiOutShortMsg(%x) = %d", msg, ret));
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return (ret == 0) ? BX_SOUNDLOW_OK : BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::closemidioutput()
|
|
|
|
{
|
|
|
|
UINT ret;
|
|
|
|
|
|
|
|
if (MidiOpen != 1)
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
ret = midiOutReset(MidiOut);
|
|
|
|
if (ismidiready == 0)
|
|
|
|
checkmidiready(); // to clear any pending SYSEX
|
|
|
|
|
|
|
|
ret = midiOutClose(MidiOut);
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("midiOutClose() = %d", ret));
|
2001-04-10 05:04:59 +04:00
|
|
|
MidiOpen = 0;
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return (ret == 0) ? BX_SOUNDLOW_OK : BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2011-02-13 20:26:52 +03:00
|
|
|
int bx_sound_windows_c::openwaveoutput(const char *wavedev)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
// could make the output device selectable,
|
2011-01-26 02:29:08 +03:00
|
|
|
// but currently only the wave mapper is supported
|
|
|
|
UNUSED(wavedev);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("openwaveoutput(%s)", wavedev));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2011-05-14 01:08:33 +04:00
|
|
|
if (WaveOutOpen != 0)
|
|
|
|
ret = waveOutReset(hWaveOut);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// clean up the buffers and mark if output is ready
|
|
|
|
checkwaveready();
|
|
|
|
|
|
|
|
// do we have to play anything?
|
|
|
|
if (tailplay == head)
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// if the format is different, we have to close and reopen the device
|
|
|
|
// or, just open the device if it's not open yet
|
2011-05-14 01:08:33 +04:00
|
|
|
if ((needreopen != 0) || (WaveOutOpen == 0))
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
2011-05-14 01:08:33 +04:00
|
|
|
if (WaveOutOpen != 0)
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
2011-05-14 01:08:33 +04:00
|
|
|
ret = waveOutClose(hWaveOut);
|
|
|
|
WaveOutOpen = 0;
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// try three times to find a suitable format
|
|
|
|
for (int tries = 0; tries < 3; tries++)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2011-05-14 01:08:33 +04:00
|
|
|
int frequency = WaveInfo[0].frequency;
|
|
|
|
bx_bool stereo = WaveInfo[0].stereo;
|
|
|
|
int bits = WaveInfo[0].bits;
|
|
|
|
// int format = WaveInfo[0].format;
|
2008-02-16 01:05:43 +03:00
|
|
|
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;
|
|
|
|
|
2011-05-14 01:08:33 +04:00
|
|
|
ret = waveOutOpen(&(hWaveOut), WaveDevice, (LPWAVEFORMATEX)&(waveformat.wf), 0, 0, CALLBACK_NULL);
|
2008-02-16 01:05:43 +03:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
char errormsg[4*MAXERRORLENGTH+1];
|
|
|
|
waveOutGetErrorTextA(ret, errormsg, 4*MAXERRORLENGTH+1);
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("waveOutOpen: %s", errormsg));
|
2008-02-16 01:05:43 +03:00
|
|
|
switch (tries) {
|
|
|
|
case 0: // maybe try a different frequency
|
|
|
|
if (frequency < 15600)
|
|
|
|
frequency = 11025;
|
|
|
|
else if (frequency < 31200)
|
|
|
|
frequency = 22050;
|
|
|
|
else
|
|
|
|
frequency = 44100;
|
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("Couldn't open wave device (error %d), trying frequency %d", ret, frequency));
|
2008-02-16 01:05:43 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // or something else
|
|
|
|
frequency = 11025;
|
|
|
|
stereo = 0;
|
|
|
|
bits = 8;
|
|
|
|
bps = 1;
|
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("Couldn't open wave device again (error %d), trying 11KHz, mono, 8bit", ret));
|
2008-02-16 01:05:43 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // nope, doesn't work
|
2011-05-20 21:39:32 +04:00
|
|
|
BX_ERROR(("Couldn't open wave output device (error = %d)!", ret));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("The format was: wFormatTag=%d, nChannels=%d, nSamplesPerSec=%d,",
|
|
|
|
waveformat.wf.wFormatTag, waveformat.wf.nChannels, waveformat.wf.nSamplesPerSec));
|
|
|
|
BX_DEBUG((" nAvgBytesPerSec=%d, nBlockAlign=%d, wBitsPerSample=%d",
|
|
|
|
waveformat.wf.nAvgBytesPerSec, waveformat.wf.nBlockAlign, waveformat.wBitsPerSample));
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-14 01:08:33 +04:00
|
|
|
WaveOutOpen = 1;
|
2008-02-16 01:05:43 +03:00
|
|
|
needreopen = 0;
|
|
|
|
break;
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
for (bufnum=tailplay; bufnum != head;
|
|
|
|
bufnum++, bufnum &= BX_SOUND_WINDOWS_NMASK, tailplay=bufnum)
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("Playing buffer %d", bufnum));
|
2008-02-16 01:05:43 +03:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2011-05-14 01:08:33 +04:00
|
|
|
ret = waveOutPrepareHeader(hWaveOut, WaveHeader[bufnum], sizeof(*WaveHeader[bufnum]));
|
2008-02-16 01:05:43 +03:00
|
|
|
if (ret != 0)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2011-05-20 21:39:32 +04:00
|
|
|
BX_ERROR(("waveOutPrepareHeader(): error = %d", ret));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-05-14 01:08:33 +04:00
|
|
|
ret = waveOutWrite(hWaveOut, WaveHeader[bufnum], sizeof(*WaveHeader[bufnum]));
|
2008-02-16 01:05:43 +03:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
char errormsg[4*MAXERRORLENGTH+1];
|
|
|
|
waveOutGetErrorTextA(ret, errormsg, 4*MAXERRORLENGTH+1);
|
2011-05-20 21:39:32 +04:00
|
|
|
BX_ERROR(("waveOutWrite(): %s", errormsg));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
int bx_sound_windows_c::startwaveplayback(int frequency, int bits, bx_bool stereo, int format)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("startwaveplayback(%d, %d, %d, %x)", frequency, bits, stereo, format));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#ifdef usewaveOut
|
|
|
|
// check if any of the properties have changed
|
2011-05-14 01:08:33 +04:00
|
|
|
if ((WaveInfo[0].frequency != frequency) ||
|
|
|
|
(WaveInfo[0].bits != bits) ||
|
|
|
|
(WaveInfo[0].stereo != stereo) ||
|
|
|
|
(WaveInfo[0].format != format))
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
|
|
|
needreopen = 1;
|
|
|
|
|
|
|
|
// store the current settings to be used by sendwavepacket()
|
2011-05-14 01:08:33 +04:00
|
|
|
WaveInfo[0].frequency = frequency;
|
|
|
|
WaveInfo[0].bits = bits;
|
|
|
|
WaveInfo[0].stereo = stereo;
|
|
|
|
WaveInfo[0].format = format;
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef usesndPlaySnd
|
|
|
|
int bps = (bits / 8) * (stereo + 1);
|
2006-02-05 20:13:54 +03:00
|
|
|
LPWAVEFILEHEADER header = (LPWAVEFILEHEADER) WaveData[0];
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::sendwavepacket(int length, Bit8u data[])
|
|
|
|
{
|
2006-02-05 20:13:54 +03:00
|
|
|
#ifdef usewaveOut
|
2001-04-10 05:04:59 +04:00
|
|
|
int bufnum;
|
2006-02-05 20:13:54 +03:00
|
|
|
#endif
|
|
|
|
#ifdef usesndPlaySnd
|
|
|
|
UINT ret;
|
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("sendwavepacket(%d, %p)", length, data));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
if (((bufnum + 1) & BX_SOUND_WINDOWS_NMASK) == tailfull)
|
|
|
|
{ // this should not actually happen!
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("Output buffer overflow! Not played. Iswaveready was %d", iswaveready));
|
2008-02-16 01:05:43 +03:00
|
|
|
iswaveready = 0; // stop the output for a while
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
head = bufnum;
|
|
|
|
|
|
|
|
// check if more buffers are available, otherwise stall the emulator
|
2008-02-16 01:05:43 +03:00
|
|
|
if (((bufnum + 2) & BX_SOUND_WINDOWS_NMASK) == tailfull)
|
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("Buffer status: Head %d, TailFull %d, TailPlay %d. Stall.",
|
|
|
|
head, tailfull, tailplay));
|
2008-02-16 01:05:43 +03:00
|
|
|
iswaveready = 0;
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
playnextbuffer();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef usesndPlaySnd
|
2006-02-05 20:13:54 +03:00
|
|
|
LPWAVEFILEHEADER header = (LPWAVEFILEHEADER) WaveData[0];
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
header->length = length + 36;
|
|
|
|
header->chnk2len = length;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
memcpy(&(header->data), data, length);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
ret = sndPlaySoundA((LPCSTR) header, SND_SYNC | SND_MEMORY);
|
2001-04-10 05:04:59 +04:00
|
|
|
if (ret != 0)
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("sndPlaySoundA: %d", ret));
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
#endif
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::stopwaveplayback()
|
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("stopwaveplayback()"));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#ifdef usewaveOut
|
|
|
|
// this is handled by checkwaveready() when closing
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef usesndPlaySnd
|
2008-02-16 01:05:43 +03:00
|
|
|
sndPlaySoundA(NULL, SND_ASYNC | SND_MEMORY);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
WaveOpen = 0;
|
|
|
|
#endif
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::closewaveoutput()
|
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("closewaveoutput"));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#ifdef usewaveOut
|
2011-05-14 01:08:33 +04:00
|
|
|
if (WaveOutOpen == 1)
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
2011-05-14 01:08:33 +04:00
|
|
|
waveOutReset(hWaveOut);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
// let checkwaveready() clean up the buffers
|
|
|
|
checkwaveready();
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-05-14 01:08:33 +04:00
|
|
|
waveOutClose(hWaveOut);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
head = 0;
|
|
|
|
tailfull = 0;
|
|
|
|
tailplay = 0;
|
|
|
|
needreopen = 0;
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
#endif
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void bx_sound_windows_c::checkmidiready()
|
|
|
|
{
|
2011-01-26 02:29:08 +03:00
|
|
|
if ((MidiHeader->dwFlags & MHDR_DONE) != 0)
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("SYSEX message done, midi ready again"));
|
2012-04-23 22:52:11 +04:00
|
|
|
midiOutUnprepareHeader(MidiOut, MidiHeader, sizeof(*MidiHeader));
|
2008-02-16 01:05:43 +03:00
|
|
|
ismidiready = 1;
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2012-04-23 22:52:11 +04:00
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
void bx_sound_windows_c::checkwaveready()
|
|
|
|
{
|
|
|
|
int bufnum;
|
|
|
|
|
|
|
|
// clean up all finished buffers and mark them as available
|
2008-02-16 01:05:43 +03:00
|
|
|
for (bufnum=tailfull; (bufnum != tailplay) &&
|
|
|
|
((WaveHeader[bufnum]->dwFlags & WHDR_DONE) != 0);
|
|
|
|
bufnum++, bufnum &= BX_SOUND_WINDOWS_NMASK)
|
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("Buffer %d done.", bufnum));
|
2012-04-23 22:52:11 +04:00
|
|
|
waveOutUnprepareHeader(hWaveOut, WaveHeader[bufnum], sizeof(*WaveHeader[bufnum]));
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
tailfull = bufnum;
|
|
|
|
|
|
|
|
// enable gathering data if a buffer is available
|
2008-02-16 01:05:43 +03:00
|
|
|
if (((head + 2) & BX_SOUND_WINDOWS_NMASK) != tailfull)
|
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("Buffer status: Head %d, TailFull %d, TailPlay %d. Ready.",
|
|
|
|
head, tailfull, tailplay));
|
2008-02-16 01:05:43 +03:00
|
|
|
iswaveready = 1;
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2011-05-14 01:08:33 +04:00
|
|
|
int bx_sound_windows_c::openwaveinput(const char *wavedev, sound_record_handler_t rh)
|
|
|
|
{
|
|
|
|
UNUSED(wavedev);
|
|
|
|
record_handler = rh;
|
2011-05-24 20:47:07 +04:00
|
|
|
if (rh != NULL) {
|
|
|
|
record_timer_index = bx_pc_system.register_timer(this, record_timer_handler, 1, 1, 0, "soundwin");
|
|
|
|
// record timer: inactive, continuous, frequency variable
|
|
|
|
}
|
2011-05-14 01:08:33 +04:00
|
|
|
recording = 0;
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::recordnextpacket()
|
|
|
|
{
|
|
|
|
MMRESULT result;
|
|
|
|
|
|
|
|
WaveInHdr->lpData = (LPSTR)WaveInData;
|
|
|
|
WaveInHdr->dwBufferLength = record_packet_size;
|
|
|
|
WaveInHdr->dwBytesRecorded = 0;
|
|
|
|
WaveInHdr->dwUser = 0L;
|
|
|
|
WaveInHdr->dwFlags = 0L;
|
|
|
|
WaveInHdr->dwLoops = 0L;
|
|
|
|
waveInPrepareHeader(hWaveIn, WaveInHdr, sizeof(WAVEHDR));
|
|
|
|
result = waveInAddBuffer(hWaveIn, WaveInHdr, sizeof(WAVEHDR));
|
|
|
|
if (result) {
|
2011-05-20 21:39:32 +04:00
|
|
|
BX_ERROR(("Couldn't add buffer for recording (error = %d)", result));
|
2011-05-14 01:08:33 +04:00
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
} else {
|
|
|
|
result = waveInStart(hWaveIn);
|
|
|
|
if (result) {
|
2011-05-20 21:39:32 +04:00
|
|
|
BX_ERROR(("Couldn't start recording (error = %d)", result));
|
2011-05-14 01:08:33 +04:00
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
} else {
|
|
|
|
recording = 1;
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::startwaverecord(int frequency, int bits, bx_bool stereo, int format)
|
|
|
|
{
|
|
|
|
Bit64u timer_val;
|
|
|
|
Bit8u shift = 0;
|
|
|
|
MMRESULT result;
|
|
|
|
|
2011-05-24 20:47:07 +04:00
|
|
|
if (record_timer_index != BX_NULL_TIMER_HANDLE) {
|
|
|
|
if (bits == 16) shift++;
|
|
|
|
if (stereo) shift++;
|
|
|
|
record_packet_size = (frequency / 10) << shift; // 0.1 sec
|
|
|
|
if (record_packet_size > BX_SOUNDLOW_WAVEPACKETSIZE) {
|
|
|
|
record_packet_size = BX_SOUNDLOW_WAVEPACKETSIZE;
|
|
|
|
}
|
|
|
|
timer_val = (Bit64u)record_packet_size * 1000000 / (frequency << shift);
|
|
|
|
bx_pc_system.activate_timer(record_timer_index, (Bit32u)timer_val, 1);
|
2011-05-14 01:08:33 +04:00
|
|
|
}
|
|
|
|
// check if any of the properties have changed
|
|
|
|
if ((WaveInfo[1].frequency != frequency) ||
|
|
|
|
(WaveInfo[1].bits != bits) ||
|
|
|
|
(WaveInfo[1].stereo != stereo) ||
|
|
|
|
(WaveInfo[1].format != format))
|
|
|
|
{
|
|
|
|
WaveInfo[1].frequency = frequency;
|
|
|
|
WaveInfo[1].bits = bits;
|
|
|
|
WaveInfo[1].stereo = stereo;
|
|
|
|
WaveInfo[1].format = format;
|
|
|
|
|
|
|
|
if (WaveInOpen) {
|
|
|
|
waveInClose(hWaveIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specify recording parameters
|
|
|
|
WAVEFORMATEX pFormat;
|
|
|
|
pFormat.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
pFormat.nChannels = 1 << stereo;
|
|
|
|
pFormat.nSamplesPerSec = frequency;
|
|
|
|
pFormat.nAvgBytesPerSec = frequency << shift;
|
|
|
|
pFormat.nBlockAlign = 1 << shift;
|
|
|
|
pFormat.wBitsPerSample = bits;
|
|
|
|
pFormat.cbSize = 0;
|
|
|
|
result = waveInOpen(&hWaveIn, WAVEMAPPER, &pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);
|
|
|
|
if (result) {
|
2011-05-20 21:39:32 +04:00
|
|
|
BX_ERROR(("Couldn't open wave device for recording (error = %d)", result));
|
2011-05-14 01:08:33 +04:00
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
} else {
|
|
|
|
WaveInOpen = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return recordnextpacket();
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::getwavepacket(int length, Bit8u data[])
|
|
|
|
{
|
|
|
|
if (WaveInOpen && recording) {
|
|
|
|
do {} while (waveInUnprepareHeader(hWaveIn, WaveInHdr, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
|
|
|
|
memcpy(data, WaveInData, length);
|
|
|
|
return recordnextpacket();
|
|
|
|
} else {
|
|
|
|
memset(data, 0, length);
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::stopwaverecord()
|
|
|
|
{
|
2011-05-24 20:47:07 +04:00
|
|
|
if (record_timer_index != BX_NULL_TIMER_HANDLE) {
|
|
|
|
bx_pc_system.deactivate_timer(record_timer_index);
|
|
|
|
}
|
2011-05-14 01:08:33 +04:00
|
|
|
if (WaveInOpen && recording) {
|
|
|
|
do {} while (waveInUnprepareHeader(hWaveIn, WaveInHdr, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
|
|
|
|
recording = 0;
|
|
|
|
}
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_windows_c::closewaveinput()
|
|
|
|
{
|
|
|
|
stopwaverecord();
|
|
|
|
if (WaveInOpen) {
|
|
|
|
waveInClose(hWaveIn);
|
|
|
|
}
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_sound_windows_c::record_timer_handler(void *this_ptr)
|
|
|
|
{
|
|
|
|
bx_sound_windows_c *class_ptr = (bx_sound_windows_c *) this_ptr;
|
|
|
|
|
|
|
|
class_ptr->record_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_sound_windows_c::record_timer(void)
|
|
|
|
{
|
2013-06-24 23:19:12 +04:00
|
|
|
record_handler(this, record_packet_size);
|
2011-05-14 01:08:33 +04:00
|
|
|
}
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
#endif // defined(WIN32)
|