Added initial version of a sound "driver" that writes to file(s).

- New code is based on the dummy sound driver and file output code from SB16.
- The midiout feature supports the MIDI file format.
- The waveout feature currently only supports the VOC format.
TODO:
- Add support for a more detailed sound configuration (waveout to device,
  midiout to a file.
- The waveout feature should support WAV and RAW file formats.
- Move VOC file handling from soundmod.cc to soundfile.cc.
- Finally remove file handling code from sound devices.
This commit is contained in:
Volker Ruppert 2015-02-16 15:47:17 +00:00
parent f19e91e2f5
commit de08a6ba55
4 changed files with 317 additions and 5 deletions

View File

@ -49,7 +49,7 @@ top_builddir = ../..
LIBTOOL=@LIBTOOL@
WIN32_DLL_IMPORT_LIBRARY=../../@WIN32_DLL_IMPORT_LIB@
SOUNDLOW_OBJS = soundlow.o @SOUNDLOW_OBJS@
SOUNDLOW_OBJS = soundlow.o soundfile.o @SOUNDLOW_OBJS@
SOUNDMOD_LINK_OPTS = -lwinmm
SOUNDMOD_LINK_OPTS_VCPP = winmm.lib
@ -148,13 +148,19 @@ sb16.o: sb16.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h ../../osdep.h
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
../../memory/memory.h ../../pc_system.h ../../gui/gui.h \
../../instrument/stubs/instrument.h ../../plugin.h ../../extplugin.h \
../../param_names.h sb16.h opl.h soundlow.h
../../param_names.h soundlow.h sb16.h opl.h
soundalsa.o: soundalsa.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../osdep.h ../../bx_debug/debug.h ../../config.h ../../osdep.h \
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
../../memory/memory.h ../../pc_system.h ../../gui/gui.h \
../../instrument/stubs/instrument.h ../../plugin.h ../../extplugin.h \
../../param_names.h soundlow.h soundalsa.h
soundfile.o: soundfile.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../osdep.h ../../bx_debug/debug.h ../../config.h ../../osdep.h \
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
../../memory/memory.h ../../pc_system.h ../../gui/gui.h \
../../instrument/stubs/instrument.h ../../plugin.h ../../extplugin.h \
../../param_names.h soundlow.h soundfile.h
soundlnx.o: soundlnx.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../osdep.h ../../bx_debug/debug.h ../../config.h ../../osdep.h \
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
@ -173,7 +179,7 @@ soundmod.o: soundmod.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../memory/memory.h ../../pc_system.h ../../gui/gui.h \
../../instrument/stubs/instrument.h ../../plugin.h ../../extplugin.h \
../../param_names.h soundmod.h soundlow.h soundlnx.h soundosx.h \
soundwin.h soundsdl.h soundalsa.h
soundwin.h soundsdl.h soundalsa.h soundfile.h
soundosx.o: soundosx.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../osdep.h ../../bx_debug/debug.h ../../config.h ../../osdep.h \
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
@ -209,13 +215,19 @@ sb16.lo: sb16.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h ../../osdep.h
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
../../memory/memory.h ../../pc_system.h ../../gui/gui.h \
../../instrument/stubs/instrument.h ../../plugin.h ../../extplugin.h \
../../param_names.h sb16.h opl.h soundlow.h
../../param_names.h soundlow.h sb16.h opl.h
soundalsa.lo: soundalsa.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../osdep.h ../../bx_debug/debug.h ../../config.h ../../osdep.h \
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
../../memory/memory.h ../../pc_system.h ../../gui/gui.h \
../../instrument/stubs/instrument.h ../../plugin.h ../../extplugin.h \
../../param_names.h soundlow.h soundalsa.h
soundfile.lo: soundfile.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../osdep.h ../../bx_debug/debug.h ../../config.h ../../osdep.h \
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
../../memory/memory.h ../../pc_system.h ../../gui/gui.h \
../../instrument/stubs/instrument.h ../../plugin.h ../../extplugin.h \
../../param_names.h soundlow.h soundfile.h
soundlnx.lo: soundlnx.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../osdep.h ../../bx_debug/debug.h ../../config.h ../../osdep.h \
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \
@ -234,7 +246,7 @@ soundmod.lo: soundmod.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../memory/memory.h ../../pc_system.h ../../gui/gui.h \
../../instrument/stubs/instrument.h ../../plugin.h ../../extplugin.h \
../../param_names.h soundmod.h soundlow.h soundlnx.h soundosx.h \
soundwin.h soundsdl.h soundalsa.h
soundwin.h soundsdl.h soundalsa.h soundfile.h
soundosx.lo: soundosx.@CPP_SUFFIX@ ../iodev.h ../../bochs.h ../../config.h \
../../osdep.h ../../bx_debug/debug.h ../../config.h ../../osdep.h \
../../gui/siminterface.h ../../cpudb.h ../../gui/paramtree.h \

View File

@ -0,0 +1,238 @@
/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2001-2015 The Bochs Project
//
// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/////////////////////////////////////////////////////////////////////////
// Support for sound output to files (based on SB16 code)
#include "iodev.h"
#include "soundlow.h"
#include "soundfile.h"
#if BX_SUPPORT_SOUNDLOW
#ifndef WIN32
#include <pthread.h>
#endif
#define LOG_THIS
// bx_soundlow_waveout_file_c class implemenzation
bx_soundlow_waveout_file_c::bx_soundlow_waveout_file_c()
:bx_soundlow_waveout_c()
{
wavefile = NULL;
}
bx_soundlow_waveout_file_c::~bx_soundlow_waveout_file_c()
{
if (wavefile != NULL) {
fputc(0, wavefile);
fclose(wavefile);
wavefile = NULL;
}
}
int bx_soundlow_waveout_file_c::openwaveoutput(const char *wavedev)
{
if ((wavefile == NULL) && (strlen(wavedev) > 0)) {
wavefile = fopen(wavedev, "wb");
if (wavefile == NULL) {
BX_ERROR(("Failed to open WAVE output file %s.", wavedev));
} else {
DEV_soundmod_VOC_init_file(wavefile);
}
}
set_pcm_params(&real_pcm_param);
pcm_callback_id = register_wave_callback(this, pcm_callback);
BX_INIT_MUTEX(mixer_mutex);
start_mixer_thread();
return BX_SOUNDLOW_OK;
}
int bx_soundlow_waveout_file_c::set_pcm_params(bx_pcm_param_t *param)
{
return BX_SOUNDLOW_OK;
}
int bx_soundlow_waveout_file_c::output(int length, Bit8u data[])
{
Bit8u temparray[12] =
{ (Bit8u)(real_pcm_param.samplerate & 0xff), (Bit8u)(real_pcm_param.samplerate >> 8), 0, 0,
(Bit8u)real_pcm_param.bits, real_pcm_param.channels, 0, 0, 0, 0, 0, 0 };
if (wavefile != NULL) {
switch ((real_pcm_param.format >> 1) & 7) {
case 2:
temparray[6] = 3;
break;
case 3:
temparray[6] = 2;
break;
case 4:
temparray[6] = 1;
break;
}
if (real_pcm_param.bits == 16)
temparray[6] = 4;
DEV_soundmod_VOC_write_block(wavefile, 9, 12, temparray, length, data);
BX_MSLEEP(100);
}
return BX_SOUNDLOW_OK;
}
// bx_soundlow_midiout_file_c class implemenzation
bx_soundlow_midiout_file_c::bx_soundlow_midiout_file_c()
:bx_soundlow_midiout_c()
{
midifile = NULL;
}
bx_soundlow_midiout_file_c::~bx_soundlow_midiout_file_c()
{
struct {
Bit8u delta, statusbyte, metaevent, length;
} metatrackend = { 0, 0xff, 0x2f, 0 };
if (midifile != NULL) {
// Meta event track end (0xff 0x2f 0x00) plus leading delta time
fwrite(&metatrackend, 1, sizeof(metatrackend), midifile);
Bit32u tracklen = ftell(midifile);
if (tracklen < 0)
BX_PANIC (("ftell failed in finishmidifile"));
if (tracklen < 22)
BX_PANIC (("MIDI track length too short"));
tracklen -= 22; // subtract the midi file and track header
fseek(midifile, 22 - 4, SEEK_SET);
// value has to be in big endian
#ifdef BX_LITTLE_ENDIAN
tracklen = bx_bswap32(tracklen);
#endif
fwrite(&tracklen, 4, 1, midifile);
fclose(midifile);
}
}
int bx_soundlow_midiout_file_c::openmidioutput(const char *mididev)
{
struct {
Bit8u chunk[4];
Bit32u chunklen; // all values in BIG Endian!
Bit16u smftype;
Bit16u tracknum;
Bit16u timecode; // 0x80 + deltatimesperquarter << 8
} midiheader =
#ifdef BX_LITTLE_ENDIAN
{ "MTh", 0x06000000, 0, 0x0100, 0x8001 };
#else
{ "MTh", 6, 0, 1, 0x180 };
#endif
midiheader.chunk[3] = 'd';
struct {
Bit8u chunk[4];
Bit32u chunklen;
Bit8u data[15];
} trackheader =
#ifdef BX_LITTLE_ENDIAN
{ "MTr", 0xffffff7f,
#else
{ "MTr", 0x7fffffff,
#endif
{ 0x00,0xff,0x51,3,0x07,0xa1,0x20, // set tempo 120 (0x7a120 us per quarter)
0x00,0xff,0x58,4,4,2,0x18,0x08 }}; // time sig 4/4
trackheader.chunk[3] = 'k';
if ((midifile == NULL) && (strlen(mididev) > 0)) {
midifile = fopen(mididev, "wb");
if (midifile == NULL) {
BX_ERROR(("Failed to open MIDI output file %s.", mididev));
return BX_SOUNDLOW_ERR;
} else {
fwrite(&midiheader, 1, 14, midifile);
fwrite(&trackheader, 1, 23, midifile);
}
}
return BX_SOUNDLOW_OK;
}
int bx_soundlow_midiout_file_c::sendmidicommand(int delta, int command, int length, Bit8u data[])
{
if (midifile != NULL) {
writedeltatime(delta);
fputc(command, midifile);
if ((command == 0xf0) ||
(command == 0xf7)) // write event length for sysex/meta events
writedeltatime(length);
fwrite(data, 1, length, midifile);
}
return BX_SOUNDLOW_OK;
}
void bx_soundlow_midiout_file_c::writedeltatime(Bit32u deltatime)
{
int i, count = 0;
Bit8u outbytes[4], value[4];
if (deltatime == 0) {
count = 1;
value[0] = 0;
} else {
while ((deltatime > 0) && (count < 4)) { // split into parts of seven bits
outbytes[count++] = deltatime & 0x7f;
deltatime >>= 7;
}
for (i=0; i<count; i++) // reverse order and
value[i] = outbytes[count - i - 1] | 0x80; // set eighth bit on
value[count - 1] &= 0x7f; // all but last byte
}
for (int i=0; i<count; i++)
fputc(value[i], midifile);
}
// bx_sound_oss_c class implemenzation
bx_sound_file_c::bx_sound_file_c()
:bx_sound_lowlevel_c()
{
BX_INFO(("Sound lowlevel module 'file' initialized"));
}
bx_soundlow_waveout_c* bx_sound_file_c::get_waveout()
{
if (waveout == NULL) {
waveout = new bx_soundlow_waveout_file_c();
}
return waveout;
}
bx_soundlow_midiout_c* bx_sound_file_c::get_midiout()
{
if (midiout == NULL) {
midiout = new bx_soundlow_midiout_file_c();
}
return midiout;
}
#endif

View File

@ -0,0 +1,59 @@
/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2015 The Bochs Project
//
// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/////////////////////////////////////////////////////////////////////////
// Support for sound output to files (based on SB16 code)
class bx_soundlow_waveout_file_c : public bx_soundlow_waveout_c {
public:
bx_soundlow_waveout_file_c();
virtual ~bx_soundlow_waveout_file_c();
virtual int openwaveoutput(const char *wavedev);
virtual int set_pcm_params(bx_pcm_param_t *param);
virtual int output(int length, Bit8u data[]);
private:
FILE *wavefile;
};
class bx_soundlow_midiout_file_c : public bx_soundlow_midiout_c {
public:
bx_soundlow_midiout_file_c();
virtual ~bx_soundlow_midiout_file_c();
virtual int openmidioutput(const char *mididev);
virtual int sendmidicommand(int delta, int command, int length, Bit8u data[]);
private:
void writedeltatime(Bit32u deltatime);
FILE *midifile;
};
class bx_sound_file_c : public bx_sound_lowlevel_c {
public:
bx_sound_file_c();
virtual ~bx_sound_file_c() {}
virtual bx_soundlow_waveout_c* get_waveout();
virtual bx_soundlow_midiout_c* get_midiout();
};

View File

@ -36,6 +36,7 @@
#include "soundwin.h"
#include "soundsdl.h"
#include "soundalsa.h"
#include "soundfile.h"
#if BX_WITH_SDL || BX_WITH_SDL2
#include <SDL.h>
@ -87,6 +88,8 @@ void bx_soundmod_ctl_c::init()
if (strcmp(driver, "dummy") == 0) {
soundmod = new bx_sound_lowlevel_c();
} else if (!strcmp(driver, "file")) {
soundmod = new bx_sound_file_c();
#if BX_HAVE_ALSASOUND
} else if (!strcmp(driver, "alsa")) {
soundmod = new bx_sound_alsa_c();