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
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-02-05 11:33:03 +03:00
|
|
|
// Copyright (C) 2001-2017 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"
|
2017-02-20 21:21:19 +03:00
|
|
|
#include "soundmod.h"
|
2004-09-05 14:30:19 +04:00
|
|
|
#include "soundwin.h"
|
|
|
|
|
2015-02-24 00:32:34 +03:00
|
|
|
#if BX_HAVE_SOUND_WIN && BX_SUPPORT_SOUNDLOW
|
2011-08-18 11:05:09 +04:00
|
|
|
|
2013-06-24 23:19:12 +04:00
|
|
|
#define LOG_THIS
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2015-03-17 23:55:51 +03:00
|
|
|
#define SOUNDWIN_PACKETS_PER_SEC 20
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
// size is the total size of the midi header and buffer and the
|
|
|
|
// 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
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
// some data for the wave buffers
|
|
|
|
HANDLE DataHandle; // returned by GlobalAlloc()
|
|
|
|
Bit8u *DataPointer; // returned by GlobalLock()
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2017-02-20 21:21:19 +03:00
|
|
|
// sound driver plugin entry points
|
|
|
|
|
|
|
|
int CDECL libwin_sound_plugin_init(plugin_t *plugin, plugintype_t type)
|
|
|
|
{
|
2017-03-14 21:21:05 +03:00
|
|
|
// Nothing here yet
|
2017-02-20 21:21:19 +03:00
|
|
|
return 0; // Success
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDECL libwin_sound_plugin_fini(void)
|
|
|
|
{
|
2017-03-14 21:21:05 +03:00
|
|
|
// Nothing here yet
|
2017-02-20 21:21:19 +03:00
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
// helper function
|
|
|
|
Bit8u* newbuffer(unsigned blksize)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2015-02-13 14:30:46 +03:00
|
|
|
static unsigned offset = 0;
|
|
|
|
Bit8u *ptr;
|
2013-11-12 00:44:38 +04:00
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
ptr = &(DataPointer[offset]);
|
|
|
|
if ((offset + ALIGN(blksize)) > size) {
|
|
|
|
return NULL;
|
2013-11-12 00:44:38 +04:00
|
|
|
} else {
|
2015-02-13 14:30:46 +03:00
|
|
|
offset += ALIGN(blksize);
|
|
|
|
return ptr;
|
2013-11-12 00:44:38 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2017-03-14 21:21:05 +03:00
|
|
|
// bx_soundlow_waveout_win_c class implementation
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
bx_soundlow_waveout_win_c::bx_soundlow_waveout_win_c()
|
|
|
|
:bx_soundlow_waveout_c()
|
|
|
|
{
|
|
|
|
WaveOutOpen = 0;
|
|
|
|
WaveOutHdr = (LPWAVEHDR) newbuffer(sizeof(WAVEHDR));
|
|
|
|
if (WaveOutHdr == NULL)
|
|
|
|
BX_PANIC(("Allocated memory was too small!"));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
bx_soundlow_waveout_win_c::~bx_soundlow_waveout_win_c()
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2015-02-13 14:30:46 +03:00
|
|
|
if (WaveOutOpen == 1) {
|
|
|
|
waveOutReset(hWaveOut);
|
|
|
|
waveOutClose(hWaveOut);
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
int bx_soundlow_waveout_win_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-13 14:30:46 +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);
|
2017-02-05 11:33:03 +03:00
|
|
|
start_resampler_thread();
|
2015-02-06 18:57:26 +03:00
|
|
|
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-13 14:30:46 +03:00
|
|
|
int bx_soundlow_waveout_win_c::set_pcm_params(bx_pcm_param_t *param)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
UINT ret;
|
|
|
|
PCMWAVEFORMAT waveformat;
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
BX_DEBUG(("set_pcm_params(): %u, %u, %u, %02x", param->samplerate, param->bits,
|
|
|
|
param->channels, param->format));
|
2015-02-06 18:57:26 +03:00
|
|
|
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-03-17 23:55:51 +03:00
|
|
|
int bx_soundlow_waveout_win_c::get_packetsize()
|
|
|
|
{
|
|
|
|
return (real_pcm_param.samplerate * 4 / SOUNDWIN_PACKETS_PER_SEC);
|
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
int bx_soundlow_waveout_win_c::output(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-03-17 23:55:51 +03:00
|
|
|
Sleep(1000 / SOUNDWIN_PACKETS_PER_SEC);
|
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
|
|
|
}
|
|
|
|
|
2017-03-14 21:21:05 +03:00
|
|
|
// bx_soundlow_wavein_win_c class implementation
|
2015-02-14 20:25:39 +03:00
|
|
|
|
|
|
|
bx_soundlow_wavein_win_c::bx_soundlow_wavein_win_c()
|
|
|
|
:bx_soundlow_wavein_c()
|
|
|
|
{
|
|
|
|
WaveInOpen = 0;
|
|
|
|
WaveInHdr = (LPWAVEHDR) newbuffer(sizeof(WAVEHDR));
|
|
|
|
WaveInData = (LPSTR) newbuffer(BX_SOUNDLOW_WAVEPACKETSIZE+64);
|
|
|
|
if (WaveInData == NULL)
|
|
|
|
BX_PANIC(("Allocated memory was too small!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_soundlow_wavein_win_c::~bx_soundlow_wavein_win_c()
|
|
|
|
{
|
|
|
|
if (WaveInOpen == 1) {
|
|
|
|
waveInClose(hWaveIn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_soundlow_wavein_win_c::openwaveinput(const char *wavedev, sound_record_handler_t rh)
|
|
|
|
{
|
|
|
|
UNUSED(wavedev);
|
|
|
|
record_handler = rh;
|
|
|
|
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
|
|
|
|
}
|
|
|
|
recording = 0;
|
|
|
|
wavein_param.samplerate = 0;
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_soundlow_wavein_win_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) {
|
|
|
|
BX_ERROR(("Couldn't add buffer for recording (error = %d)", result));
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
} else {
|
|
|
|
result = waveInStart(hWaveIn);
|
|
|
|
if (result) {
|
|
|
|
BX_ERROR(("Couldn't start recording (error = %d)", result));
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
} else {
|
|
|
|
recording = 1;
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_soundlow_wavein_win_c::startwaverecord(bx_pcm_param_t *param)
|
|
|
|
{
|
|
|
|
Bit64u timer_val;
|
|
|
|
Bit8u shift = 0;
|
|
|
|
MMRESULT result;
|
|
|
|
|
|
|
|
if (record_timer_index != BX_NULL_TIMER_HANDLE) {
|
|
|
|
if (param->bits == 16) shift++;
|
|
|
|
if (param->channels == 2) shift++;
|
|
|
|
record_packet_size = (param->samplerate / 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 / (param->samplerate << shift);
|
|
|
|
bx_pc_system.activate_timer(record_timer_index, (Bit32u)timer_val, 1);
|
|
|
|
}
|
|
|
|
// check if any of the properties have changed
|
|
|
|
if (memcmp(param, &wavein_param, sizeof(bx_pcm_param_t)) != 0) {
|
|
|
|
wavein_param = *param;
|
|
|
|
|
|
|
|
if (WaveInOpen) {
|
|
|
|
waveInClose(hWaveIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specify recording parameters
|
|
|
|
WAVEFORMATEX pFormat;
|
|
|
|
pFormat.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
pFormat.nChannels = param->channels;
|
|
|
|
pFormat.nSamplesPerSec = param->samplerate;
|
|
|
|
pFormat.nAvgBytesPerSec = param->samplerate << shift;
|
|
|
|
pFormat.nBlockAlign = 1 << shift;
|
|
|
|
pFormat.wBitsPerSample = param->bits;
|
|
|
|
pFormat.cbSize = 0;
|
|
|
|
result = waveInOpen(&hWaveIn, WAVEMAPPER, &pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);
|
|
|
|
if (result) {
|
|
|
|
BX_ERROR(("Couldn't open wave device for recording (error = %d)", result));
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
} else {
|
|
|
|
WaveInOpen = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return recordnextpacket();
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_soundlow_wavein_win_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_soundlow_wavein_win_c::stopwaverecord()
|
|
|
|
{
|
|
|
|
if (record_timer_index != BX_NULL_TIMER_HANDLE) {
|
|
|
|
bx_pc_system.deactivate_timer(record_timer_index);
|
|
|
|
}
|
|
|
|
if (WaveInOpen && recording) {
|
|
|
|
do {} while (waveInUnprepareHeader(hWaveIn, WaveInHdr, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
|
|
|
|
recording = 0;
|
|
|
|
}
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_soundlow_wavein_win_c::record_timer_handler(void *this_ptr)
|
|
|
|
{
|
|
|
|
bx_soundlow_wavein_win_c *class_ptr = (bx_soundlow_wavein_win_c *) this_ptr;
|
|
|
|
|
|
|
|
class_ptr->record_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_soundlow_wavein_win_c::record_timer(void)
|
|
|
|
{
|
|
|
|
record_handler(this, record_packet_size);
|
|
|
|
}
|
|
|
|
|
2017-03-14 21:21:05 +03:00
|
|
|
// bx_soundlow_midiout_win_c class implementation
|
2015-02-13 14:30:46 +03:00
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
bx_soundlow_midiout_win_c::bx_soundlow_midiout_win_c()
|
|
|
|
:bx_soundlow_midiout_c()
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2015-02-13 14:30:46 +03:00
|
|
|
MidiOpen = 0;
|
|
|
|
ismidiready = 1;
|
|
|
|
MidiHeader = (LPMIDIHDR) newbuffer(sizeof(MIDIHDR));
|
|
|
|
MidiData = (LPSTR) newbuffer(BX_SOUND_WINDOWS_MAXSYSEXLEN);
|
2015-02-14 20:25:39 +03:00
|
|
|
if (MidiData == NULL)
|
2015-02-13 14:30:46 +03:00
|
|
|
BX_PANIC(("Allocated memory was too small!"));
|
|
|
|
}
|
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
bx_soundlow_midiout_win_c::~bx_soundlow_midiout_win_c()
|
2015-02-14 20:25:39 +03:00
|
|
|
{
|
2015-02-17 21:28:25 +03:00
|
|
|
closemidioutput();
|
2015-02-13 14:30:46 +03:00
|
|
|
}
|
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
int bx_soundlow_midiout_win_c::openmidioutput(const char *mididev)
|
2015-02-13 14:30:46 +03: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MidiOpen = 0;
|
|
|
|
|
|
|
|
UINT ret = midiOutOpen(&MidiOut, deviceid, 0, 0, CALLBACK_NULL);
|
|
|
|
if (ret == 0)
|
|
|
|
MidiOpen = 1;
|
|
|
|
|
|
|
|
BX_DEBUG(("midiOutOpen() = %d, MidiOpen: %d", ret, MidiOpen));
|
|
|
|
|
|
|
|
return (MidiOpen == 1) ? BX_SOUNDLOW_OK : BX_SOUNDLOW_ERR;
|
|
|
|
}
|
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
int bx_soundlow_midiout_win_c::midiready()
|
2015-02-13 14:30:46 +03:00
|
|
|
{
|
|
|
|
if (ismidiready == 0)
|
|
|
|
checkmidiready();
|
|
|
|
|
|
|
|
if (ismidiready == 1)
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
else
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
}
|
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
int bx_soundlow_midiout_win_c::sendmidicommand(int delta, int command, int length, Bit8u data[])
|
2015-02-13 14:30:46 +03:00
|
|
|
{
|
|
|
|
UINT ret;
|
|
|
|
|
|
|
|
if (MidiOpen != 1)
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
|
|
|
|
if ((command == 0xf0) || (command == 0xf7) || (length > 3))
|
|
|
|
{
|
|
|
|
BX_DEBUG(("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)
|
|
|
|
BX_ERROR(("midiOutPrepareHeader(): error = %d", ret));
|
|
|
|
ret = midiOutLongMsg(MidiOut, MidiHeader, sizeof(*MidiHeader));
|
|
|
|
if (ret != 0)
|
|
|
|
BX_ERROR(("midiOutLongMsg(): error = %d", ret));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD msg = command;
|
|
|
|
|
|
|
|
for (int i = 0; i<length; i++)
|
|
|
|
msg |= (data[i] << (8 * (i + 1)));
|
|
|
|
|
|
|
|
ret = midiOutShortMsg(MidiOut, msg);
|
|
|
|
BX_DEBUG(("midiOutShortMsg(%x) = %d", msg, ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret == 0) ? BX_SOUNDLOW_OK : BX_SOUNDLOW_ERR;
|
|
|
|
}
|
|
|
|
|
2015-02-17 21:28:25 +03:00
|
|
|
int bx_soundlow_midiout_win_c::closemidioutput()
|
|
|
|
{
|
|
|
|
UINT ret;
|
|
|
|
|
|
|
|
if (MidiOpen == 1) {
|
|
|
|
ret = midiOutReset(MidiOut);
|
|
|
|
if (ismidiready == 0)
|
|
|
|
checkmidiready(); // to clear any pending SYSEX
|
|
|
|
ret = midiOutClose(MidiOut);
|
|
|
|
BX_DEBUG(("midiOutClose() = %d", ret));
|
|
|
|
}
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
void bx_soundlow_midiout_win_c::checkmidiready()
|
2015-02-13 14:30:46 +03:00
|
|
|
{
|
2015-02-15 21:32:36 +03:00
|
|
|
if ((MidiHeader->dwFlags & MHDR_DONE) != 0) {
|
|
|
|
BX_DEBUG(("SYSEX message done, midi ready again"));
|
|
|
|
midiOutUnprepareHeader(MidiOut, MidiHeader, sizeof(*MidiHeader));
|
|
|
|
ismidiready = 1;
|
|
|
|
}
|
|
|
|
}
|
2015-02-13 14:30:46 +03:00
|
|
|
|
2017-03-14 21:21:05 +03:00
|
|
|
// bx_sound_windows_c class implementation
|
2015-02-13 14:30:46 +03:00
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
bx_sound_windows_c::bx_sound_windows_c()
|
2017-03-14 21:21:05 +03:00
|
|
|
:bx_sound_lowlevel_c("win")
|
2015-02-15 21:32:36 +03:00
|
|
|
{
|
|
|
|
DataHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
|
|
|
|
DataPointer = (Bit8u*) GlobalLock(DataHandle);
|
2015-02-13 14:30:46 +03:00
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
if (DataPointer == NULL)
|
|
|
|
BX_PANIC(("GlobalLock returned NULL-pointer"));
|
2015-02-13 14:30:46 +03:00
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
#undef size
|
|
|
|
#undef ALIGN
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
bx_sound_windows_c::~bx_sound_windows_c()
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2015-02-15 21:32:36 +03:00
|
|
|
GlobalUnlock(DataHandle);
|
|
|
|
GlobalFree(DataHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_soundlow_waveout_c* bx_sound_windows_c::get_waveout()
|
|
|
|
{
|
|
|
|
if (waveout == NULL) {
|
|
|
|
waveout = new bx_soundlow_waveout_win_c();
|
|
|
|
}
|
|
|
|
return waveout;
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_soundlow_wavein_c* bx_sound_windows_c::get_wavein()
|
|
|
|
{
|
|
|
|
if (wavein == NULL) {
|
|
|
|
wavein = new bx_soundlow_wavein_win_c();
|
|
|
|
}
|
|
|
|
return wavein;
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_soundlow_midiout_c* bx_sound_windows_c::get_midiout()
|
|
|
|
{
|
|
|
|
if (midiout == NULL) {
|
|
|
|
midiout = new bx_soundlow_midiout_win_c();
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2015-02-15 21:32:36 +03:00
|
|
|
return midiout;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2012-04-23 22:52:11 +04:00
|
|
|
|
2015-02-24 00:32:34 +03:00
|
|
|
#endif // BX_HAVE_SOUND_WIN
|