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
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2012-06-09 14:12:05 +04:00
|
|
|
// Copyright (C) 2011-2012 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"
|
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"
|
2011-02-15 00:14:20 +03:00
|
|
|
|
2012-05-20 21:22:50 +04:00
|
|
|
#ifndef WIN32
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2012-06-09 00:49:39 +04:00
|
|
|
#if BX_WITH_SDL
|
|
|
|
#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
|
|
|
|
2011-02-15 00:14:20 +03:00
|
|
|
int libsoundmod_LTX_plugin_init(plugin_t *plugin, plugintype_t type, int argc, char *argv[])
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void libsoundmod_LTX_plugin_fini(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2012-05-20 18:19:49 +04:00
|
|
|
put("sound", "SOUND");
|
|
|
|
soundmod = NULL;
|
|
|
|
}
|
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
|
|
|
{
|
2013-06-24 23:19:12 +04:00
|
|
|
if (soundmod != NULL) {
|
|
|
|
soundmod->closewaveoutput();
|
|
|
|
}
|
|
|
|
delete soundmod;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_soundmod_ctl_c::init()
|
|
|
|
{
|
|
|
|
const char *driver = SIM->get_param_string(BXPN_SOUND_DRIVER)->getptr();
|
|
|
|
const char *wavedev = SIM->get_param_string(BXPN_SOUND_WAVEDEV)->getptr();
|
|
|
|
if (!strcmp(driver, "default")) {
|
|
|
|
soundmod = new BX_SOUND_LOWLEVEL_C();
|
|
|
|
#if BX_HAVE_ALSASOUND
|
|
|
|
} else if (!strcmp(driver, "alsa")) {
|
|
|
|
soundmod = new bx_sound_linux_c();
|
|
|
|
wavedev = "alsa";
|
|
|
|
#endif
|
2012-06-09 14:12:05 +04:00
|
|
|
#if BX_WITH_SDL
|
2013-06-24 23:19:12 +04:00
|
|
|
} else if (!strcmp(driver, "sdl")) {
|
|
|
|
soundmod = new bx_sound_sdl_c();
|
2012-06-09 00:49:39 +04:00
|
|
|
#endif
|
2013-06-24 23:19:12 +04:00
|
|
|
} else if (!strcmp(driver, "dummy")) {
|
|
|
|
soundmod = new bx_sound_lowlevel_c();
|
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-06-24 23:19:12 +04:00
|
|
|
int ret = soundmod->openwaveoutput(wavedev);
|
|
|
|
if (ret != BX_SOUNDLOW_OK) {
|
|
|
|
BX_PANIC(("Could not open wave output device"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-05-20 21:22:50 +04:00
|
|
|
#ifndef WIN32
|
|
|
|
pthread_t thread;
|
|
|
|
#endif
|
|
|
|
Bit8u *beep_buffer;
|
|
|
|
int beep_bytes, beep_bufsize;
|
|
|
|
bx_bool beep_active = 0;
|
|
|
|
|
2012-05-21 22:45:09 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
DWORD WINAPI beep_thread(LPVOID indata)
|
|
|
|
#else
|
2012-05-20 21:22:50 +04:00
|
|
|
void beep_thread(void *indata)
|
2012-05-21 22:45:09 +04:00
|
|
|
#endif
|
2012-05-20 21:22:50 +04:00
|
|
|
{
|
|
|
|
Bit8u level;
|
2012-05-21 22:45:09 +04:00
|
|
|
int i, j;
|
2012-05-20 21:22:50 +04:00
|
|
|
|
|
|
|
beep_active = 1;
|
|
|
|
bx_sound_lowlevel_c *soundmod = (bx_sound_lowlevel_c*)indata;
|
|
|
|
level = 0x40;
|
|
|
|
i = 0;
|
|
|
|
while (beep_bytes > 0) {
|
2012-05-21 22:45:09 +04:00
|
|
|
j = 0;
|
|
|
|
do {
|
|
|
|
beep_buffer[j++] = level;
|
|
|
|
if ((++i % beep_bytes) == 0) level ^= 0x40;
|
|
|
|
} while (j < beep_bufsize);
|
2012-05-20 21:22:50 +04:00
|
|
|
soundmod->sendwavepacket(beep_bufsize, beep_buffer);
|
2012-06-09 00:49:39 +04:00
|
|
|
if (soundmod->get_type() == BX_SOUNDLOW_WIN) {
|
2012-05-21 22:45:09 +04:00
|
|
|
#ifdef WIN32
|
2012-06-09 00:49:39 +04:00
|
|
|
Sleep(100);
|
2012-05-21 22:45:09 +04:00
|
|
|
#endif
|
2012-06-09 00:49:39 +04:00
|
|
|
} else if (soundmod->get_type() == BX_SOUNDLOW_SDL) {
|
|
|
|
#if BX_WITH_SDL
|
|
|
|
SDL_Delay(100);
|
|
|
|
#endif
|
|
|
|
}
|
2012-05-20 21:22:50 +04:00
|
|
|
}
|
|
|
|
soundmod->stopwaveplayback();
|
|
|
|
free(beep_buffer);
|
|
|
|
beep_active = 0;
|
2012-05-21 22:45:09 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
return 0;
|
|
|
|
#else
|
2012-05-20 21:22:50 +04:00
|
|
|
pthread_exit(NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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));
|
2013-06-12 01:04:07 +04:00
|
|
|
if (beep_active) {
|
|
|
|
beep_off();
|
|
|
|
}
|
|
|
|
soundmod->startwaveplayback(44100, 8, 0, 0);
|
|
|
|
beep_bytes = (int)(44100.0 / frequency / 2);
|
|
|
|
beep_bufsize = 4410;
|
|
|
|
beep_buffer = (Bit8u*)malloc(beep_bufsize);
|
2012-05-21 22:45:09 +04:00
|
|
|
#ifdef WIN32
|
2013-06-12 01:04:07 +04:00
|
|
|
DWORD threadID;
|
|
|
|
CreateThread(NULL, 0, beep_thread, soundmod, 0, &threadID);
|
2012-05-21 22:45:09 +04:00
|
|
|
#else
|
2013-06-12 01:04:07 +04:00
|
|
|
pthread_create(&thread, NULL, (void *(*)(void *))&beep_thread, soundmod);
|
2012-05-21 22:45:09 +04:00
|
|
|
#endif
|
2012-05-20 18:19:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
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"));
|
|
|
|
if (beep_active) {
|
|
|
|
beep_bytes = 0;
|
2012-05-21 22:45:09 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
while (beep_active) Sleep(1);
|
|
|
|
#else
|
2012-05-20 21:22:50 +04:00
|
|
|
pthread_join(thread, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
2012-05-20 18:19:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
// The dummy sound lowlevel functions. They don't do anything.
|
2013-06-24 23:19:12 +04:00
|
|
|
bx_sound_lowlevel_c::bx_sound_lowlevel_c()
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
2013-06-24 23:19:12 +04:00
|
|
|
put("sound", "SOUND");
|
2011-05-24 20:47:07 +04:00
|
|
|
record_timer_index = BX_NULL_TIMER_HANDLE;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
bx_sound_lowlevel_c::~bx_sound_lowlevel_c()
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::waveready()
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::midiready()
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::openmidioutput(const char *mididev)
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
|
|
|
UNUSED(mididev);
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::sendmidicommand(int delta, int command, int length, Bit8u data[])
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
|
|
|
UNUSED(delta);
|
|
|
|
UNUSED(command);
|
|
|
|
UNUSED(length);
|
|
|
|
UNUSED(data);
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::closemidioutput()
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::openwaveoutput(const char *wavedev)
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
|
|
|
UNUSED(wavedev);
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
int bx_sound_lowlevel_c::startwaveplayback(int frequency, int bits, bx_bool stereo, int format)
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
|
|
|
UNUSED(frequency);
|
|
|
|
UNUSED(bits);
|
|
|
|
UNUSED(stereo);
|
|
|
|
UNUSED(format);
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::sendwavepacket(int length, Bit8u data[])
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
|
|
|
UNUSED(length);
|
|
|
|
UNUSED(data);
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::stopwaveplayback()
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
int bx_sound_lowlevel_c::closewaveoutput()
|
2011-02-13 20:26:52 +03:00
|
|
|
{
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2011-02-13 20:26:52 +03:00
|
|
|
}
|
2011-02-15 00:14:20 +03:00
|
|
|
|
2011-04-04 20:07:24 +04:00
|
|
|
int bx_sound_lowlevel_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, "soundmod");
|
|
|
|
// record timer: inactive, continuous, frequency variable
|
|
|
|
}
|
2011-04-04 20:07:24 +04:00
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_lowlevel_c::startwaverecord(int frequency, int bits, bx_bool stereo, int format)
|
|
|
|
{
|
|
|
|
Bit64u timer_val;
|
|
|
|
Bit8u shift = 0;
|
|
|
|
|
|
|
|
UNUSED(format);
|
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-04-04 20:07:24 +04:00
|
|
|
}
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_lowlevel_c::getwavepacket(int length, Bit8u data[])
|
|
|
|
{
|
|
|
|
memset(data, 0, length);
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_lowlevel_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-04-04 20:07:24 +04:00
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_lowlevel_c::closewaveinput()
|
|
|
|
{
|
|
|
|
stopwaverecord();
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_sound_lowlevel_c::record_timer_handler(void *this_ptr)
|
|
|
|
{
|
|
|
|
bx_sound_lowlevel_c *class_ptr = (bx_sound_lowlevel_c *) this_ptr;
|
|
|
|
|
|
|
|
class_ptr->record_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_sound_lowlevel_c::record_timer(void)
|
|
|
|
{
|
2013-06-24 23:19:12 +04:00
|
|
|
record_handler(this, record_packet_size);
|
2011-04-04 20:07:24 +04:00
|
|
|
}
|
|
|
|
|
2011-02-15 00:14:20 +03:00
|
|
|
#endif
|