2011-02-13 20:26:52 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2011-02-13 20:26:52 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2015-01-05 00:12:41 +03:00
|
|
|
// Copyright (C) 2011-2015 The Bochs Project
|
2011-02-13 20:26:52 +03: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
|
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
// Common sound module code and dummy sound lowlevel functions
|
2011-02-13 20:26:52 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2011-02-15 00:14:20 +03:00
|
|
|
#include "iodev.h"
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
#if BX_SUPPORT_SOUNDLOW
|
2011-02-15 00:14:20 +03:00
|
|
|
|
2011-02-13 20:26:52 +03:00
|
|
|
#include "soundmod.h"
|
2014-11-24 21:25:14 +03:00
|
|
|
#include "soundlow.h"
|
2011-02-15 00:14:20 +03:00
|
|
|
#include "soundlnx.h"
|
|
|
|
#include "soundosx.h"
|
|
|
|
#include "soundwin.h"
|
2012-06-09 14:12:05 +04:00
|
|
|
#include "soundsdl.h"
|
2013-07-14 19:14:53 +04:00
|
|
|
#include "soundalsa.h"
|
2011-02-15 00:14:20 +03:00
|
|
|
|
2012-05-20 21:22:50 +04:00
|
|
|
#ifndef WIN32
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2014-11-14 21:25:37 +03:00
|
|
|
#if BX_WITH_SDL || BX_WITH_SDL2
|
2012-06-09 00:49:39 +04:00
|
|
|
#include <SDL.h>
|
|
|
|
#endif
|
2012-05-20 21:22:50 +04:00
|
|
|
|
2012-05-20 18:19:49 +04:00
|
|
|
#define LOG_THIS theSoundModCtl->
|
2011-02-15 00:14:20 +03:00
|
|
|
|
|
|
|
bx_soundmod_ctl_c* theSoundModCtl = NULL;
|
2011-02-13 20:26:52 +03:00
|
|
|
|
2015-01-17 22:53:03 +03:00
|
|
|
BX_MUTEX(beep_mutex);
|
2015-01-05 00:12:41 +03:00
|
|
|
|
|
|
|
Bit32u beep_callback(void *dev, Bit16u rate, Bit8u *buffer, Bit32u len);
|
|
|
|
|
2014-06-08 12:40:08 +04:00
|
|
|
int CDECL libsoundmod_LTX_plugin_init(plugin_t *plugin, plugintype_t type, int argc, char *argv[])
|
2011-02-15 00:14:20 +03:00
|
|
|
{
|
2011-12-25 12:52:34 +04:00
|
|
|
if (type == PLUGTYPE_CORE) {
|
|
|
|
theSoundModCtl = new bx_soundmod_ctl_c;
|
|
|
|
bx_devices.pluginSoundModCtl = theSoundModCtl;
|
|
|
|
return 0; // Success
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-15 00:14:20 +03:00
|
|
|
}
|
|
|
|
|
2014-06-08 12:40:08 +04:00
|
|
|
void CDECL libsoundmod_LTX_plugin_fini(void)
|
2011-02-15 00:14:20 +03:00
|
|
|
{
|
|
|
|
delete theSoundModCtl;
|
|
|
|
}
|
|
|
|
|
2012-05-20 18:19:49 +04:00
|
|
|
bx_soundmod_ctl_c::bx_soundmod_ctl_c()
|
2011-02-15 00:14:20 +03:00
|
|
|
{
|
2013-12-29 16:56:52 +04:00
|
|
|
put("soundctl", "SNDCTL");
|
2012-05-20 18:19:49 +04:00
|
|
|
soundmod = NULL;
|
2015-02-13 14:30:46 +03:00
|
|
|
waveout = NULL;
|
2012-05-20 18:19:49 +04:00
|
|
|
}
|
2011-02-15 00:14:20 +03:00
|
|
|
|
2013-06-24 23:19:12 +04:00
|
|
|
bx_soundmod_ctl_c::~bx_soundmod_ctl_c()
|
2012-05-20 18:19:49 +04:00
|
|
|
{
|
2015-01-17 22:53:03 +03:00
|
|
|
beep_active = 0;
|
2015-02-13 14:30:46 +03:00
|
|
|
if (waveout != NULL) {
|
|
|
|
if (beep_callback_id >= 0) {
|
|
|
|
waveout->unregister_wave_callback(beep_callback_id);
|
|
|
|
}
|
2013-06-24 23:19:12 +04:00
|
|
|
}
|
|
|
|
delete soundmod;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_soundmod_ctl_c::init()
|
|
|
|
{
|
2015-02-13 14:30:46 +03:00
|
|
|
int ret;
|
2014-11-22 14:43:40 +03:00
|
|
|
static const char default_name[] = BX_SOUND_LOWLEVEL_NAME;
|
2013-06-24 23:19:12 +04:00
|
|
|
const char *driver = SIM->get_param_string(BXPN_SOUND_DRIVER)->getptr();
|
2015-02-13 14:30:46 +03:00
|
|
|
const char *pwaveout = SIM->get_param_string(BXPN_SOUND_WAVEOUT)->getptr();
|
2013-07-06 19:12:04 +04:00
|
|
|
const char *wavein = SIM->get_param_string(BXPN_SOUND_WAVEIN)->getptr();
|
2014-11-22 14:43:40 +03:00
|
|
|
|
|
|
|
if (strcmp(driver, "default") == 0) {
|
|
|
|
driver = default_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(driver, "dummy") == 0) {
|
|
|
|
soundmod = new bx_sound_lowlevel_c();
|
2013-06-24 23:19:12 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
|
|
|
} else if (!strcmp(driver, "alsa")) {
|
2013-07-14 19:14:53 +04:00
|
|
|
soundmod = new bx_sound_alsa_c();
|
2013-06-24 23:19:12 +04:00
|
|
|
#endif
|
2014-11-14 21:25:37 +03:00
|
|
|
#if BX_WITH_SDL || BX_WITH_SDL2
|
2013-06-24 23:19:12 +04:00
|
|
|
} else if (!strcmp(driver, "sdl")) {
|
|
|
|
soundmod = new bx_sound_sdl_c();
|
2013-07-25 22:47:11 +04:00
|
|
|
#endif
|
|
|
|
#if (defined(linux) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
|
|
|
|
} else if (!strcmp(driver, "oss")) {
|
2015-02-07 00:31:30 +03:00
|
|
|
soundmod = new bx_sound_oss_c();
|
2013-07-25 22:47:11 +04:00
|
|
|
#endif
|
|
|
|
#if defined(macintosh)
|
|
|
|
} else if (!strcmp(driver, "osx")) {
|
|
|
|
soundmod = new bx_sound_osx_c();
|
|
|
|
#endif
|
|
|
|
#if defined(WIN32)
|
|
|
|
} else if (!strcmp(driver, "win")) {
|
|
|
|
soundmod = new bx_sound_windows_c();
|
2012-06-09 00:49:39 +04:00
|
|
|
#endif
|
2011-02-15 00:14:20 +03:00
|
|
|
} else {
|
2013-06-24 23:19:12 +04:00
|
|
|
BX_PANIC(("unknown lowlevel sound driver '%s'", driver));
|
|
|
|
return;
|
2011-02-15 00:14:20 +03:00
|
|
|
}
|
2013-07-06 19:12:04 +04:00
|
|
|
if (!strlen(wavein)) {
|
2015-02-13 14:30:46 +03:00
|
|
|
SIM->get_param_string(BXPN_SOUND_WAVEIN)->set(pwaveout);
|
2013-07-06 19:12:04 +04:00
|
|
|
}
|
2015-02-13 14:30:46 +03:00
|
|
|
waveout = soundmod->get_waveout();
|
|
|
|
if (waveout != NULL) {
|
|
|
|
ret = waveout->openwaveoutput(pwaveout);
|
|
|
|
if (ret != BX_SOUNDLOW_OK) {
|
|
|
|
BX_PANIC(("Could not open wave output device"));
|
|
|
|
} else {
|
|
|
|
beep_active = 0;
|
|
|
|
beep_cur_freq = 0.0;
|
|
|
|
BX_INIT_MUTEX(beep_mutex);
|
|
|
|
beep_callback_id = waveout->register_wave_callback(theSoundModCtl, beep_callback);
|
|
|
|
}
|
2015-01-05 00:12:41 +03:00
|
|
|
} else {
|
2015-02-13 14:30:46 +03:00
|
|
|
BX_PANIC(("no waveout support in sound driver '%s'", driver));
|
2013-06-24 23:19:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* bx_soundmod_ctl_c::get_module()
|
|
|
|
{
|
2011-08-18 11:05:09 +04:00
|
|
|
return soundmod;
|
2011-02-15 00:14:20 +03:00
|
|
|
}
|
|
|
|
|
2015-01-05 00:12:41 +03:00
|
|
|
Bit32u bx_soundmod_ctl_c::beep_generator(Bit16u rate, Bit8u *buffer, Bit32u len)
|
|
|
|
{
|
|
|
|
Bit32u j = 0;
|
2015-01-06 01:07:03 +03:00
|
|
|
Bit16u beep_samples;
|
2015-01-08 22:12:01 +03:00
|
|
|
static Bit8u beep_level = 0x40;
|
|
|
|
static Bit16u beep_pos = 0;
|
2015-01-05 00:12:41 +03:00
|
|
|
|
2015-01-08 22:12:01 +03:00
|
|
|
BX_LOCK(beep_mutex);
|
2015-01-05 00:12:41 +03:00
|
|
|
if (!beep_active) {
|
2015-01-08 22:12:01 +03:00
|
|
|
BX_UNLOCK(beep_mutex);
|
2015-01-05 00:12:41 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-06 01:07:03 +03:00
|
|
|
beep_samples = (Bit32u)((float)rate / beep_cur_freq / 2);
|
2015-01-05 00:12:41 +03:00
|
|
|
do {
|
|
|
|
buffer[j++] = 0;
|
|
|
|
buffer[j++] = beep_level;
|
|
|
|
buffer[j++] = 0;
|
|
|
|
buffer[j++] = beep_level;
|
|
|
|
if ((++beep_pos % beep_samples) == 0) {
|
|
|
|
beep_level ^= 0x80;
|
|
|
|
beep_pos = 0;
|
2015-01-06 01:07:03 +03:00
|
|
|
beep_samples = (Bit32u)((float)rate / beep_cur_freq / 2);
|
2015-01-05 00:12:41 +03:00
|
|
|
}
|
|
|
|
} while (j < len);
|
2015-01-08 22:12:01 +03:00
|
|
|
BX_UNLOCK(beep_mutex);
|
2015-01-05 00:12:41 +03:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bit32u beep_callback(void *dev, Bit16u rate, Bit8u *buffer, Bit32u len)
|
|
|
|
{
|
|
|
|
return ((bx_soundmod_ctl_c*)dev)->beep_generator(rate, buffer, len);
|
|
|
|
}
|
|
|
|
|
2012-05-20 18:19:49 +04:00
|
|
|
bx_bool bx_soundmod_ctl_c::beep_on(float frequency)
|
|
|
|
{
|
|
|
|
if (soundmod != NULL) {
|
2012-05-20 21:22:50 +04:00
|
|
|
BX_DEBUG(("Beep ON (frequency=%.2f)",frequency));
|
2015-01-05 00:12:41 +03:00
|
|
|
if (frequency != beep_cur_freq) {
|
|
|
|
BX_LOCK(beep_mutex);
|
|
|
|
beep_cur_freq = frequency;
|
|
|
|
beep_active = 1;
|
|
|
|
BX_UNLOCK(beep_mutex);
|
2014-05-02 10:38:49 +04:00
|
|
|
}
|
2015-01-08 22:12:01 +03:00
|
|
|
return 1;
|
2012-05-20 18:19:49 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_bool bx_soundmod_ctl_c::beep_off()
|
|
|
|
{
|
|
|
|
if (soundmod != NULL) {
|
2012-05-20 21:22:50 +04:00
|
|
|
BX_DEBUG(("Beep OFF"));
|
2015-01-08 22:12:01 +03:00
|
|
|
BX_LOCK(beep_mutex);
|
2015-01-05 00:12:41 +03:00
|
|
|
beep_active = 0;
|
2015-01-06 01:07:03 +03:00
|
|
|
beep_cur_freq = 0.0;
|
2015-01-08 22:12:01 +03:00
|
|
|
BX_UNLOCK(beep_mutex);
|
|
|
|
return 1;
|
2012-05-20 18:19:49 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-13 21:37:48 +04:00
|
|
|
/* Handlers for the VOC file output */
|
|
|
|
|
|
|
|
// Write the header of the VOC file.
|
|
|
|
|
|
|
|
void bx_soundmod_ctl_c::VOC_init_file(FILE *stream)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
char id[20];
|
|
|
|
Bit16u headerlen; // All in LITTLE Endian!
|
|
|
|
Bit16u version;
|
|
|
|
Bit16u chksum;
|
|
|
|
} vocheader =
|
|
|
|
{ "Creative Voice File",
|
|
|
|
#ifdef BX_LITTLE_ENDIAN
|
|
|
|
0x1a, 0x0114, 0x111f };
|
|
|
|
#else
|
|
|
|
0x1a00, 0x1401, 0x1f11 };
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vocheader.id[19] = 26; // Replace string end with 26
|
|
|
|
|
|
|
|
fwrite(&vocheader, 1, sizeof vocheader, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write one block to the VOC file
|
|
|
|
void bx_soundmod_ctl_c::VOC_write_block(FILE *stream, int block,
|
|
|
|
Bit32u headerlen, Bit8u header[],
|
|
|
|
Bit32u datalen, Bit8u data[])
|
|
|
|
{
|
|
|
|
Bit32u i;
|
|
|
|
|
|
|
|
if (block > 9) {
|
|
|
|
BX_ERROR(("VOC Block %d not recognized, ignored.", block));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc(block, stream);
|
|
|
|
|
|
|
|
i = headerlen + datalen;
|
|
|
|
#ifdef BX_LITTLE_ENDIAN
|
|
|
|
fwrite(&i, 1, 3, stream); // write the length in 24-bit little endian
|
|
|
|
#else
|
|
|
|
Bit8u lengthbytes[3];
|
|
|
|
lengthbytes[0] = i & 0xff; i >>= 8;
|
|
|
|
lengthbytes[1] = i & 0xff; i >>= 8;
|
|
|
|
lengthbytes[2] = i & 0xff;
|
|
|
|
fwrite(lengthbytes, 1, 3, stream);
|
|
|
|
#endif
|
|
|
|
BX_DEBUG(("Voc block %d; Headerlen %d; Datalen %d",
|
|
|
|
block, headerlen, datalen));
|
|
|
|
if (headerlen > 0)
|
|
|
|
fwrite(header, 1, headerlen, stream);
|
|
|
|
if (datalen > 0)
|
|
|
|
fwrite(data, 1, datalen, stream);
|
|
|
|
}
|
|
|
|
|
2011-02-15 00:14:20 +03:00
|
|
|
#endif
|