Moved speaker beep generator to the speaker code.

This commit is contained in:
Volker Ruppert 2015-02-16 08:48:11 +00:00
parent 3ac8362523
commit f19e91e2f5
5 changed files with 89 additions and 99 deletions

View File

@ -37,9 +37,6 @@
#include "soundsdl.h"
#include "soundalsa.h"
#ifndef WIN32
#include <pthread.h>
#endif
#if BX_WITH_SDL || BX_WITH_SDL2
#include <SDL.h>
#endif
@ -48,10 +45,6 @@
bx_soundmod_ctl_c* theSoundModCtl = NULL;
BX_MUTEX(beep_mutex);
Bit32u beep_callback(void *dev, Bit16u rate, Bit8u *buffer, Bit32u len);
int CDECL libsoundmod_LTX_plugin_init(plugin_t *plugin, plugintype_t type, int argc, char *argv[])
{
if (type == PLUGTYPE_CORE) {
@ -77,12 +70,6 @@ bx_soundmod_ctl_c::bx_soundmod_ctl_c()
bx_soundmod_ctl_c::~bx_soundmod_ctl_c()
{
beep_active = 0;
if (waveout != NULL) {
if (beep_callback_id >= 0) {
waveout->unregister_wave_callback(beep_callback_id);
}
}
delete soundmod;
}
@ -132,11 +119,6 @@ void bx_soundmod_ctl_c::init()
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);
}
} else {
BX_PANIC(("no waveout support in sound driver '%s'", driver));
@ -148,67 +130,6 @@ void* bx_soundmod_ctl_c::get_module()
return soundmod;
}
Bit32u bx_soundmod_ctl_c::beep_generator(Bit16u rate, Bit8u *buffer, Bit32u len)
{
Bit32u j = 0;
Bit16u beep_samples;
static Bit8u beep_level = 0x40;
static Bit16u beep_pos = 0;
BX_LOCK(beep_mutex);
if (!beep_active) {
BX_UNLOCK(beep_mutex);
return 0;
}
beep_samples = (Bit32u)((float)rate / beep_cur_freq / 2);
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;
beep_samples = (Bit32u)((float)rate / beep_cur_freq / 2);
}
} while (j < len);
BX_UNLOCK(beep_mutex);
return len;
}
Bit32u beep_callback(void *dev, Bit16u rate, Bit8u *buffer, Bit32u len)
{
return ((bx_soundmod_ctl_c*)dev)->beep_generator(rate, buffer, len);
}
bx_bool bx_soundmod_ctl_c::beep_on(float frequency)
{
if (soundmod != NULL) {
BX_DEBUG(("Beep ON (frequency=%.2f)",frequency));
if (frequency != beep_cur_freq) {
BX_LOCK(beep_mutex);
beep_cur_freq = frequency;
beep_active = 1;
BX_UNLOCK(beep_mutex);
}
return 1;
}
return 0;
}
bx_bool bx_soundmod_ctl_c::beep_off()
{
if (soundmod != NULL) {
BX_DEBUG(("Beep OFF"));
BX_LOCK(beep_mutex);
beep_active = 0;
beep_cur_freq = 0.0;
BX_UNLOCK(beep_mutex);
return 1;
}
return 0;
}
/* Handlers for the VOC file output */
// Write the header of the VOC file.

View File

@ -30,16 +30,10 @@ public:
virtual ~bx_soundmod_ctl_c();
virtual void init(void);
virtual void* get_module();
virtual bx_bool beep_on(float frequency);
virtual bx_bool beep_off();
virtual void VOC_init_file(FILE *stream);
virtual void VOC_write_block(FILE *stream, int block, Bit32u headerlen,
Bit8u header[], Bit32u datalen, Bit8u data[]);
Bit32u beep_generator(Bit16u rate, Bit8u *buffer, Bit32u len);
private:
bx_sound_lowlevel_c *soundmod;
bx_soundlow_waveout_c *waveout;
int beep_callback_id;
bx_bool beep_active;
float beep_cur_freq;
};

View File

@ -3,7 +3,7 @@
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003 David N. Welton <davidw@dedasys.com>.
// Copyright (C) 2003-2014 The Bochs Project
// Copyright (C) 2003-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
@ -25,6 +25,10 @@
#include "speaker.h"
#if BX_SUPPORT_SOUNDLOW
#ifndef WIN32
#include <pthread.h>
#endif
#include "sound/soundlow.h"
#include "sound/soundmod.h"
#endif
@ -48,6 +52,12 @@ bx_speaker_c *theSpeaker= NULL;
#define BX_SPK_MODE_SYSTEM 2
#define BX_SPK_MODE_GUI 3
#if BX_SUPPORT_SOUNDLOW
BX_MUTEX(beep_mutex);
Bit32u beep_callback(void *dev, Bit16u rate, Bit8u *buffer, Bit32u len);
#endif
// builtin configuration handling functions
void speaker_init_options(void)
@ -129,6 +139,9 @@ bx_speaker_c::bx_speaker_c()
#ifdef __linux__
consolefd = -1;
#endif
#if BX_SUPPORT_SOUNDLOW
waveout = NULL;
#endif
}
bx_speaker_c::~bx_speaker_c()
@ -138,6 +151,14 @@ bx_speaker_c::~bx_speaker_c()
if (consolefd >= 0) {
close(consolefd);
}
#endif
#if BX_SUPPORT_SOUNDLOW
beep_active = 0;
if (waveout != NULL) {
if (beep_callback_id >= 0) {
waveout->unregister_wave_callback(beep_callback_id);
}
}
#endif
BX_DEBUG(("Exit"));
}
@ -156,11 +177,17 @@ void bx_speaker_c::init(void)
const char *mode = SIM->get_param_enum("mode", base)->get_selected();
if (!strcmp(mode, "sound")) {
output_mode = BX_SPK_MODE_SOUND;
outputinit = 0;
#if BX_SUPPORT_SOUNDLOW
if (DEV_soundmod_beep_off()) {
bx_sound_lowlevel_c *soundmod = DEV_sound_get_module();
if (soundmod == NULL) {
BX_PANIC(("Couldn't initialize lowlevel driver"));
}
waveout = soundmod->get_waveout();
if (waveout != NULL) {
beep_active = 0;
BX_INIT_MUTEX(beep_mutex);
beep_callback_id = waveout->register_wave_callback(theSpeaker, beep_callback);
BX_INFO(("Using lowlevel sound support for output"));
outputinit = 1;
} else {
BX_ERROR(("Failed to use lowlevel sound support for output"));
}
@ -191,6 +218,41 @@ void bx_speaker_c::reset(unsigned type)
beep_off();
}
#if BX_SUPPORT_SOUNDLOW
Bit32u bx_speaker_c::beep_generator(Bit16u rate, Bit8u *buffer, Bit32u len)
{
Bit32u j = 0;
Bit16u beep_samples;
static Bit8u beep_level = 0x40;
static Bit16u beep_pos = 0;
BX_LOCK(beep_mutex);
if (!beep_active) {
BX_UNLOCK(beep_mutex);
return 0;
}
beep_samples = (Bit32u)((float)rate / beep_frequency / 2);
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;
beep_samples = (Bit32u)((float)rate / beep_frequency / 2);
}
} while (j < len);
BX_UNLOCK(beep_mutex);
return len;
}
Bit32u beep_callback(void *dev, Bit16u rate, Bit8u *buffer, Bit32u len)
{
return ((bx_speaker_c*)dev)->beep_generator(rate, buffer, len);
}
#endif
void bx_speaker_c::beep_on(float frequency)
{
#if defined(WIN32)
@ -198,12 +260,14 @@ void bx_speaker_c::beep_on(float frequency)
beep_off();
}
#endif
beep_frequency = frequency;
if (output_mode == BX_SPK_MODE_SOUND) {
#if BX_SUPPORT_SOUNDLOW
if (outputinit) {
DEV_soundmod_beep_on(frequency);
if ((waveout != NULL) && (frequency != beep_frequency)) {
BX_LOCK(beep_mutex);
beep_frequency = frequency;
beep_active = 1;
BX_UNLOCK(beep_mutex);
}
#endif
} else if (output_mode == BX_SPK_MODE_SYSTEM) {
@ -219,6 +283,7 @@ void bx_speaker_c::beep_on(float frequency)
// give the gui a chance to signal beep on
bx_gui->beep_on(frequency);
}
beep_frequency = frequency;
}
#if defined(WIN32)
@ -245,8 +310,11 @@ void bx_speaker_c::beep_off()
{
if (output_mode == BX_SPK_MODE_SOUND) {
#if BX_SUPPORT_SOUNDLOW
if (outputinit) {
DEV_soundmod_beep_off();
if (waveout != NULL) {
BX_LOCK(beep_mutex);
beep_active = 0;
beep_frequency = 0.0;
BX_UNLOCK(beep_mutex);
}
#endif
} else if (output_mode == BX_SPK_MODE_SYSTEM) {
@ -267,6 +335,5 @@ void bx_speaker_c::beep_off()
// give the gui a chance to signal beep off
bx_gui->beep_off();
}
beep_frequency = 0.0;
}

View File

@ -2,7 +2,8 @@
// $Id$
/////////////////////////////////////////////////////////////////////////
//
// Copyright 2003 by David N. Welton <davidw@dedasys.com>.
// Copyright (C) 2003 David N. Welton <davidw@dedasys.com>.
// Copyright (C) 2003-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
@ -28,6 +29,8 @@
#ifndef BX_PC_SPEAKER_H
#define BX_PC_SPEAKER_H
class bx_soundlow_waveout_c;
class bx_speaker_c : public bx_speaker_stub_c {
public:
bx_speaker_c();
@ -38,9 +41,11 @@ public:
void beep_on(float frequency);
void beep_off();
#if BX_SUPPORT_SOUNDLOW
Bit32u beep_generator(Bit16u rate, Bit8u *buffer, Bit32u len);
#endif
private:
float beep_frequency; // 0 : beep is off
bx_bool outputinit;
unsigned output_mode;
#ifdef __linux__
/* Do we have access? If not, just skip everything else. */
@ -49,6 +54,11 @@ private:
#elif defined(WIN32)
Bit64u usec_start;
#endif
#if BX_SUPPORT_SOUNDLOW
bx_soundlow_waveout_c *waveout;
int beep_callback_id;
bx_bool beep_active;
#endif
};
#endif

View File

@ -257,8 +257,6 @@ extern "C" {
///////// Sound module macros
#define DEV_sound_get_module() \
((bx_sound_lowlevel_c*)bx_devices.pluginSoundModCtl->get_module())
#define DEV_soundmod_beep_on(a) bx_devices.pluginSoundModCtl->beep_on(a)
#define DEV_soundmod_beep_off() bx_devices.pluginSoundModCtl->beep_off()
#define DEV_soundmod_VOC_init_file(a) \
(bx_devices.pluginSoundModCtl->VOC_init_file(a))
#define DEV_soundmod_VOC_write_block(a,b,c,d,e,f) \