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
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2015-01-11 23:13:50 +03:00
|
|
|
// Copyright (C) 2001-2015 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"
|
2014-11-24 21:25:14 +03:00
|
|
|
#include "soundlow.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;
|
|
|
|
|
|
|
|
// size is the total size of the midi header and buffer and the
|
2015-02-08 13:33:12 +03:00
|
|
|
// wave header and buffer, all aligned on a 16-byte boundary
|
2001-04-10 05:04:59 +04:00
|
|
|
|
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)) \
|
2015-02-08 13:33:12 +03:00
|
|
|
+ ALIGN(sizeof(WAVEHDR)) * 2 \
|
2011-05-14 01:08:33 +04:00
|
|
|
+ ALIGN(BX_SOUND_WINDOWS_MAXSYSEXLEN) \
|
2015-02-08 13:33:12 +03:00
|
|
|
+ ALIGN(BX_SOUNDLOW_WAVEPACKETSIZE + 64)
|
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);
|
|
|
|
|
2015-02-06 18:57:26 +03:00
|
|
|
WaveOutHdr = (LPWAVEHDR) NEWBUFFER(sizeof(WAVEHDR));
|
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::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
|
|
|
{
|
2013-11-12 00:44:38 +04:00
|
|
|
UINT deviceid;
|
|
|
|
|
|
|
|
if (strlen(mididev) == 0) {
|
|
|
|
deviceid = (UINT) MIDIMAPPER;
|
|
|
|
} else {
|
|
|
|
deviceid = atoi(mididev);
|
|
|
|
if (((deviceid < 0) || (deviceid >= midiOutGetNumDevs())) &&
|
|
|
|
(deviceid != (UINT) MIDIMAPPER)) {
|
|
|
|
BX_ERROR(("MIDI device ID out of range - using default MIDI mapper"));
|
|
|
|
deviceid = (UINT) MIDIMAPPER;
|
|
|
|
}
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
WaveDevice = (UINT) WAVEMAPPER;
|
|
|
|
|
2015-02-01 15:33:21 +03:00
|
|
|
set_pcm_params(real_pcm_param);
|
2015-02-06 18:57:26 +03:00
|
|
|
pcm_callback_id = register_wave_callback(this, pcm_callback);
|
|
|
|
BX_INIT_MUTEX(mixer_mutex);
|
|
|
|
start_mixer_thread();
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-06 18:57:26 +03:00
|
|
|
int bx_sound_windows_c::set_pcm_params(bx_pcm_param_t param)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
UINT ret;
|
|
|
|
PCMWAVEFORMAT waveformat;
|
|
|
|
|
2015-02-06 18:57:26 +03:00
|
|
|
BX_DEBUG(("set_pcm_params(): %u, %u, %u, %02x", param.samplerate, param.bits,
|
|
|
|
param.channels, param.format));
|
|
|
|
if (WaveOutOpen != 0) {
|
|
|
|
ret = waveOutReset(hWaveOut);
|
|
|
|
ret = waveOutClose(hWaveOut);
|
|
|
|
WaveOutOpen = 0;
|
|
|
|
}
|
2008-02-16 01:05:43 +03:00
|
|
|
|
2015-02-06 18:57:26 +03:00
|
|
|
// try three times to find a suitable format
|
|
|
|
for (int tries = 0; tries < 3; tries++) {
|
|
|
|
int frequency = real_pcm_param.samplerate;
|
|
|
|
bx_bool stereo = real_pcm_param.channels == 2;
|
|
|
|
int bits = real_pcm_param.bits;
|
|
|
|
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(&(hWaveOut), WaveDevice, (LPWAVEFORMATEX)&(waveformat.wf), 0, 0, CALLBACK_NULL);
|
|
|
|
if (ret != 0) {
|
|
|
|
char errormsg[4*MAXERRORLENGTH+1];
|
|
|
|
waveOutGetErrorTextA(ret, errormsg, 4*MAXERRORLENGTH+1);
|
|
|
|
BX_DEBUG(("waveOutOpen: %s", errormsg));
|
|
|
|
switch (tries) {
|
2008-02-16 01:05:43 +03:00
|
|
|
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
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2015-02-06 18:57:26 +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));
|
|
|
|
} else {
|
|
|
|
WaveOutOpen = 1;
|
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
2015-02-06 18:57:26 +03:00
|
|
|
int bx_sound_windows_c::waveout(int length, Bit8u data[])
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2006-02-05 20:13:54 +03:00
|
|
|
UINT ret;
|
2015-01-11 23:13:50 +03:00
|
|
|
|
2015-02-06 18:57:26 +03:00
|
|
|
// prepare the wave header
|
|
|
|
WaveOutHdr->lpData = (LPSTR)data;
|
|
|
|
WaveOutHdr->dwBufferLength = length;
|
|
|
|
WaveOutHdr->dwBytesRecorded = length;
|
|
|
|
WaveOutHdr->dwUser = 0;
|
|
|
|
WaveOutHdr->dwFlags = 0;
|
|
|
|
WaveOutHdr->dwLoops = 1;
|
|
|
|
|
|
|
|
ret = waveOutPrepareHeader(hWaveOut, WaveOutHdr, sizeof(*WaveOutHdr));
|
|
|
|
if (ret != 0) {
|
|
|
|
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
|
|
|
|
2015-02-06 18:57:26 +03:00
|
|
|
ret = waveOutWrite(hWaveOut, WaveOutHdr, sizeof(*WaveOutHdr));
|
|
|
|
if (ret != 0) {
|
|
|
|
char errormsg[4*MAXERRORLENGTH+1];
|
|
|
|
waveOutGetErrorTextA(ret, errormsg, 4*MAXERRORLENGTH+1);
|
|
|
|
BX_ERROR(("waveOutWrite(): %s", errormsg));
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2015-02-06 18:57:26 +03:00
|
|
|
Sleep(100);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
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
|
|
|
|
2015-02-06 18:57:26 +03:00
|
|
|
if (WaveOutOpen == 1) {
|
2011-05-14 01:08:33 +04:00
|
|
|
waveOutReset(hWaveOut);
|
|
|
|
waveOutClose(hWaveOut);
|
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 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
|
|
|
|
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;
|
2015-02-08 11:53:20 +03:00
|
|
|
wavein_param.samplerate = 0;
|
2011-05-14 01:08:33 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 11:53:20 +03:00
|
|
|
int bx_sound_windows_c::startwaverecord(bx_pcm_param_t *param)
|
2011-05-14 01:08:33 +04:00
|
|
|
{
|
|
|
|
Bit64u timer_val;
|
|
|
|
Bit8u shift = 0;
|
|
|
|
MMRESULT result;
|
|
|
|
|
2011-05-24 20:47:07 +04:00
|
|
|
if (record_timer_index != BX_NULL_TIMER_HANDLE) {
|
2015-02-08 11:53:20 +03:00
|
|
|
if (param->bits == 16) shift++;
|
|
|
|
if (param->channels == 2) shift++;
|
|
|
|
record_packet_size = (param->samplerate / 10) << shift; // 0.1 sec
|
2011-05-24 20:47:07 +04:00
|
|
|
if (record_packet_size > BX_SOUNDLOW_WAVEPACKETSIZE) {
|
|
|
|
record_packet_size = BX_SOUNDLOW_WAVEPACKETSIZE;
|
|
|
|
}
|
2015-02-08 11:53:20 +03:00
|
|
|
timer_val = (Bit64u)record_packet_size * 1000000 / (param->samplerate << shift);
|
2011-05-24 20:47:07 +04:00
|
|
|
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
|
2015-02-08 11:53:20 +03:00
|
|
|
if (memcmp(param, &wavein_param, sizeof(bx_pcm_param_t)) != 0) {
|
|
|
|
wavein_param = *param;
|
2011-05-14 01:08:33 +04:00
|
|
|
|
|
|
|
if (WaveInOpen) {
|
|
|
|
waveInClose(hWaveIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specify recording parameters
|
|
|
|
WAVEFORMATEX pFormat;
|
|
|
|
pFormat.wFormatTag = WAVE_FORMAT_PCM;
|
2015-02-08 11:53:20 +03:00
|
|
|
pFormat.nChannels = param->channels;
|
|
|
|
pFormat.nSamplesPerSec = param->samplerate;
|
|
|
|
pFormat.nAvgBytesPerSec = param->samplerate << shift;
|
2011-05-14 01:08:33 +04:00
|
|
|
pFormat.nBlockAlign = 1 << shift;
|
2015-02-08 11:53:20 +03:00
|
|
|
pFormat.wBitsPerSample = param->bits;
|
2011-05-14 01:08:33 +04:00
|
|
|
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)
|