From 1e52f0cbe0cfe1e3de51c3c8a7a2aea2c3a14e72 Mon Sep 17 00:00:00 2001 From: Volker Ruppert Date: Mon, 2 Feb 2015 21:11:31 +0000 Subject: [PATCH] Implemented simple mixing support (addition and clipping). Fixed minimum value of signed data types. TODO: Use the mixer thread support in the sound modules (alsa, oss, osx, win). --- bochs/config.h.in | 8 ++++---- bochs/iodev/sound/soundlow.cc | 30 +++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/bochs/config.h.in b/bochs/config.h.in index 97ab94f67..a99d819ac 100644 --- a/bochs/config.h.in +++ b/bochs/config.h.in @@ -493,19 +493,19 @@ typedef Bit32u bx_phy_address; #define BX_MAX_BIT64U ( (Bit64u) -1 ) #define BX_MIN_BIT64U ( 0 ) #define BX_MAX_BIT64S ( ((Bit64u) -1) >> 1 ) -#define BX_MIN_BIT64S ( (Bit64s)-(((Bit64u) -1) >> 1) ) +#define BX_MIN_BIT64S ( (Bit64s)-(((Bit64u) -1) >> 1) - 1) #define BX_MAX_BIT32U ( (Bit32u) -1 ) #define BX_MIN_BIT32U ( 0 ) #define BX_MAX_BIT32S ( ((Bit32u) -1) >> 1 ) -#define BX_MIN_BIT32S ( (Bit32s)-(((Bit32u) -1) >> 1) ) +#define BX_MIN_BIT32S ( (Bit32s)-(((Bit32u) -1) >> 1) - 1) #define BX_MAX_BIT16U ( (Bit16u) -1 ) #define BX_MIN_BIT16U ( 0 ) #define BX_MAX_BIT16S ( ((Bit16u) -1) >> 1 ) -#define BX_MIN_BIT16S ( (Bit16s)-(((Bit16u) -1) >> 1) ) +#define BX_MIN_BIT16S ( (Bit16s)-(((Bit16u) -1) >> 1) - 1) #define BX_MAX_BIT8U ( (Bit8u) -1 ) #define BX_MIN_BIT8U ( 0 ) #define BX_MAX_BIT8S ( ((Bit8u) -1) >> 1 ) -#define BX_MIN_BIT8S ( (Bit8s)-(((Bit8u) -1) >> 1) ) +#define BX_MIN_BIT8S ( (Bit8s)-(((Bit8u) -1) >> 1) - 1) // create an unsigned integer type that is the same size as a pointer. diff --git a/bochs/iodev/sound/soundlow.cc b/bochs/iodev/sound/soundlow.cc index b93f4f33c..243d5bc2f 100644 --- a/bochs/iodev/sound/soundlow.cc +++ b/bochs/iodev/sound/soundlow.cc @@ -116,6 +116,7 @@ BX_THREAD_FUNC(mixer_thread, indata) mixer_control = 1; while (mixer_control > 0) { len = soundmod->get_waveout_packetsize(); + memset(mixbuffer, 0, len); if (soundmod->mixer_common(mixbuffer, len)) { soundmod->waveout(len, mixbuffer); } else { @@ -161,7 +162,10 @@ void bx_sound_lowlevel_c::start_mixer_thread() bx_bool bx_sound_lowlevel_c::mixer_common(Bit8u *buffer, int len) { - Bit32u len2 = 0; + Bit32u count, len2 = 0, len3 = 0; + Bit16s src1, src2, dst_val; + Bit32s tmp_val; + Bit8u *src, *dst; Bit8u *tmpbuffer = new Bit8u[len]; BX_LOCK(mixer_mutex); @@ -170,15 +174,31 @@ bx_bool bx_sound_lowlevel_c::mixer_common(Bit8u *buffer, int len) memset(tmpbuffer, 0, len); len2 = get_wave[i].cb(get_wave[i].device, real_pcm_param.samplerate, tmpbuffer, len); if (len2 > 0) { - // TODO: mix wave data - memcpy(buffer, tmpbuffer, len2); - break; + src = tmpbuffer; + dst = buffer; + count = len / 2; + while (count--) { + src1 = (src[0] | (src[1] << 8)); + src2 = (dst[0] | (dst[1] << 8)); + tmp_val = (Bit32s)src1 + (Bit32s)src2; + if (tmp_val > BX_MAX_BIT16S) { + tmp_val = BX_MAX_BIT16S; + } else if (tmp_val < BX_MIN_BIT16S) { + tmp_val = BX_MIN_BIT16S; + } + dst_val = (Bit16s)tmp_val; + dst[0] = dst_val & 0xff; + dst[1] = (Bit8u)(dst_val >> 8); + src += 2; + dst += 2; + } + if (len3 < len2) len3 = len2; } } } BX_UNLOCK(mixer_mutex); delete [] tmpbuffer; - return (len2 > 0); + return (len3 > 0); } int bx_sound_lowlevel_c::waveready()