From cc32c6b12f59dfd1d669e16666769fa17e69e811 Mon Sep 17 00:00:00 2001 From: spacy51 Date: Thu, 28 Aug 2008 22:06:48 +0000 Subject: [PATCH] ADDED volume control to GB core [blargg] CLEANUP gbSound.h [blargg] git-svn-id: https://svn.code.sf.net/p/vbam/code/trunk@658 a31d4220-a93d-0410-bf67-fe4944624d44 --- src/Sound.cpp | 37 +++++++++---------- src/Sound.h | 23 +++++++----- src/dmg/gbSound.cpp | 59 +++++++++++++++--------------- src/dmg/gbSound.h | 89 ++++++++++++++++++++++----------------------- 4 files changed, 104 insertions(+), 104 deletions(-) diff --git a/src/Sound.cpp b/src/Sound.cpp index 9e144b66..8c59f617 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -2,8 +2,9 @@ // Copyright (C) 1999-2003 Forgotten // Copyright (C) 2004 Forgotten and the VBA development team // Copyright (C) 2004-2006 VBA development team -// Copyright (C) 2007 Shay Green (blargg) - +// Copyright (C) 2007-2008 VBA-M development team +// Copyright (C) 2007-2008 Shay Green (blargg) +// // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2, or(at your option) @@ -18,7 +19,7 @@ // along with this program; if not, write to the Free Software Foundation, // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#include +#include #include "Sound.h" @@ -54,24 +55,22 @@ extern bool stopState; // TODO: silence sound when true -int soundQuality = 2; -int soundInterpolation = 0; -float soundFiltering = 1; -static float soundFiltering_; -float soundVolume = 1; -static float soundVolume_ = -1; -bool soundEcho = false; +int const SOUND_CLOCK_TICKS_ = 167772; // 1/100 second -static int soundEnableFlag = 0x3ff; // emulator channels enabled +u16 soundFinalWave [1470]; +int soundBufferLen = sizeof soundFinalWave; +int soundQuality = 2; +int soundInterpolation = 0; +bool soundPaused = true; +float soundFiltering = 1; +float soundVolume = 1; +bool soundEcho = false; +int SOUND_CLOCK_TICKS = SOUND_CLOCK_TICKS_; +int soundTicks = SOUND_CLOCK_TICKS_; -int const SOUND_CLOCK_TICKS_ = 167772; -int SOUND_CLOCK_TICKS = SOUND_CLOCK_TICKS_; -int soundTicks = SOUND_CLOCK_TICKS_; - -u16 soundFinalWave [1470]; -int soundBufferLen = sizeof soundFinalWave; - -bool soundPaused = true; +static int soundEnableFlag = 0x3ff; // emulator channels enabled +static float soundFiltering_ = -1; +static float soundVolume_ = -1; void interp_rate() { /* empty for now */ } diff --git a/src/Sound.h b/src/Sound.h index 6526fbd5..469fd383 100644 --- a/src/Sound.h +++ b/src/Sound.h @@ -1,20 +1,16 @@ -// GBA sound emulation +// Sound emulation setup/options and GBA sound emulation #ifndef VBA_SOUND_H #define VBA_SOUND_H #include "System.h" -//// Setup/options +//// Setup/options (these affect GBA and GB sound) // Initializes sound and returns true if successful. Sets sound quality to // current value in soundQuality global. bool soundInit(); -// Sets sample rate to 44100 / quality -void soundSetQuality( int quality ); -extern int soundQuality; // current sound quality - // Manages sound volume, where 1.0 is normal void soundSetVolume( float ); float soundGetVolume(); @@ -24,8 +20,8 @@ float soundGetVolume(); // 0x002 Pulse 2 // 0x004 Wave // 0x008 Noise -// 0x100 GBA left -// 0x200 GBA right +// 0x100 PCM 1 +// 0x200 PCM 2 void soundSetEnable( int mask ); int soundGetEnable(); @@ -41,14 +37,21 @@ void soundShutdown(); extern int soundBufferLen; // size of sound buffer in BYTES extern u16 soundFinalWave[1470];// 16-bit SIGNED stereo sample buffer + +//// GBA sound options + +// Sets sample rate to 44100 / quality +void soundSetQuality( int quality ); +extern int soundQuality; // current sound quality + // Sound settings extern int soundInterpolation; // 1 if PCM should have low-pass filtering extern float soundFiltering; // 0.0 = none, 1.0 = max (only if soundInterpolation is 1) -//// Emulation +//// GBA sound emulation -// GBA register addresses +// GBA sound registers #define SGCNT0_H 0x82 #define FIFOA_L 0xa0 #define FIFOA_H 0xa2 diff --git a/src/dmg/gbSound.cpp b/src/dmg/gbSound.cpp index 70ff4bbb..a965297d 100644 --- a/src/dmg/gbSound.cpp +++ b/src/dmg/gbSound.cpp @@ -2,6 +2,8 @@ // Copyright (C) 1999-2003 Forgotten // Copyright (C) 2005-2006 Forgotten and the VBA development team // Copyright (C) 2007-2008 VBA-M development team +// Copyright (C) 2007-2008 Shay Green (blargg) +// // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2, or(at your option) @@ -16,9 +18,9 @@ // along with this program; if not, write to the Free Software Foundation, // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#include +#include -#include "../System.h" +#include "../Sound.h" #include "../Util.h" #include "gbGlobals.h" #include "gbSound.h" @@ -26,23 +28,18 @@ #include "gb_apu/Gb_Apu.h" #include "gb_apu/Effects_Buffer.h" -extern int soundGetEnable(); - -static Gb_Apu* gb_apu; -static Simple_Effects_Buffer* stereo_buffer; - -extern u16 soundFinalWave[1470]; - extern int gbHardware; -extern void soundResume(); +gb_effects_config_t gb_effects_config = { false, 0.20f, 0.15f, false }; -extern int soundBufferLen; -extern int soundQuality; -extern bool soundPaused; -extern int soundTicks; -extern int SOUND_CLOCK_TICKS; +static gb_effects_config_t gb_effects_config_current; +static Simple_Effects_Buffer* stereo_buffer; +static Gb_Apu* gb_apu; +static float soundVolume_ = -1; +static int prevSoundEnable = -1; + +int const chan_count = 4; int const ticks_to_time = 2 * GB_APU_OVERCLOCK; static inline blip_time_t blip_time() @@ -90,13 +87,6 @@ static void flush_samples() } } -int const chan_count = 4; - -gb_effects_config_t gb_effects_config = { false, 0.2f, 0.15f, false }; -static gb_effects_config_t gb_effects_config_current; - -static int prevSoundEnable = -1; - static void apply_effects() { prevSoundEnable = soundGetEnable(); @@ -122,6 +112,14 @@ void gbSoundConfigEffects( gb_effects_config_t const& c ) gb_effects_config = c; } +static void apply_volume() +{ + soundVolume_ = soundGetVolume(); + + if ( gb_apu ) + gb_apu->volume( soundVolume_ ); +} + void gbSoundTick() { if ( gb_apu && stereo_buffer ) @@ -135,6 +133,9 @@ void gbSoundTick() if ( memcmp( &gb_effects_config_current, &gb_effects_config, sizeof gb_effects_config ) || soundGetEnable() != prevSoundEnable ) apply_effects(); + + if ( soundVolume_ != soundGetVolume() ) + apply_volume(); } } @@ -160,7 +161,7 @@ static void remake_stereo_buffer() stereo_buffer = 0; stereo_buffer = new Simple_Effects_Buffer; // TODO: handle out of memory - stereo_buffer->set_sample_rate( 44100 / soundQuality ); // TODO: handle out of memory + if ( stereo_buffer->set_sample_rate( 44100 / soundQuality ) ) { } // TODO: handle out of memory stereo_buffer->clock_rate( gb_apu->clock_rate ); // APU @@ -168,20 +169,21 @@ static void remake_stereo_buffer() Multi_Buffer::wave_type+1, Multi_Buffer::wave_type+2, Multi_Buffer::wave_type+3, Multi_Buffer::mixed_type+1 }; - stereo_buffer->set_channel_count( chan_count, chan_types ); + if ( stereo_buffer->set_channel_count( chan_count, chan_types ) ) { } // TODO: handle errors if ( !gb_apu ) { - gb_apu = new Gb_Apu; + gb_apu = new Gb_Apu; // TODO: handle errors reset_apu(); } apply_effects(); + apply_volume(); } void gbSoundReset() { - SOUND_CLOCK_TICKS = 20000; + SOUND_CLOCK_TICKS = 20000; // 1/100 second remake_stereo_buffer(); reset_apu(); @@ -221,9 +223,6 @@ void gbSoundReset() } } -extern bool soundInit(); -extern void soundShutdown(); - void gbSoundSetQuality(int quality) { if ( soundQuality != quality ) @@ -364,7 +363,7 @@ static void gbSoundReadGameOld(int version,gzFile gzFile) // Load state utilReadData( gzFile, gbsound_format ); - if ( version >= 11 ) + if ( version >= 11 ) // TODO: never executed; remove? utilReadData( gzFile, gbsound_format2 ); utilReadData( gzFile, gbsound_format3 ); diff --git a/src/dmg/gbSound.h b/src/dmg/gbSound.h index 62402aa4..4bab347f 100644 --- a/src/dmg/gbSound.h +++ b/src/dmg/gbSound.h @@ -1,22 +1,34 @@ -// -*- C++ -*- -// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. -// Copyright (C) 1999-2003 Forgotten -// Copyright (C) 2004 Forgotten and the VBA development team +// GB sound emulation -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2, or(at your option) -// any later version. -// -// This program 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 General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software Foundation, -// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#ifndef VBA_GBSOUND_H +#define VBA_GBSOUND_H +// See Sound.h for sound setup/options + +//// GB sound options + +// Sets sample rate to 44100 / quality +void gbSoundSetQuality( int quality ); +extern int soundQuality; // current sound quality + +// Effects configuration +struct gb_effects_config_t +{ + bool enabled; // false = disable all effects + + float echo; // 0.0 = none, 1.0 = lots + float stereo; // 0.0 = channels in center, 1.0 = channels on left/right + bool surround; // true = put some channels in back +}; + +// Changes effects configuration +void gbSoundConfigEffects( gb_effects_config_t const& ); +extern gb_effects_config_t gb_effects_config; // current configuration + + +//// GB sound emulation + +// GB sound registers #define NR10 0xff10 #define NR11 0xff11 #define NR12 0xff12 @@ -39,36 +51,23 @@ #define NR51 0xff25 #define NR52 0xff26 -#define SOUND_EVENT(address,value) \ - gbSoundEvent(address,value) +// Resets emulated sound hardware +void gbSoundReset(); -extern void gbSoundTick(); -extern void gbSoundPause(); -extern void gbSoundResume(); -extern void gbSoundEnable(int); -extern void gbSoundDisable(int); -extern int gbSoundGetEnable(); -extern void gbSoundReset(); -extern void gbSoundSaveGame(gzFile); -extern void gbSoundReadGame(int,gzFile); -extern void gbSoundEvent(register u16, register int); -extern void gbSoundSetQuality(int); +// Emulates write to sound hardware +void gbSoundEvent( u16 address, int data ); +#define SOUND_EVENT gbSoundEvent -extern u8 gbSoundRead(u16 address); +// Emulates read from sound hardware +u8 gbSoundRead( u16 address ); -extern int soundTicks; -extern int soundQuality; -extern int SOUND_CLOCK_TICKS; +// Notifies emulator that SOUND_CLOCK_TICKS clocks have passed +void gbSoundTick(); +extern int SOUND_CLOCK_TICKS; // Number of 16.8 MHz clocks between calls to gbSoundTick() +extern int soundTicks; // Number of 16.8 MHz clocks until gbSoundTick() will be called -struct gb_effects_config_t -{ - bool enabled; // false = disable all effects +// Saves/loads emulator state +void gbSoundSaveGame( gzFile out ); +void gbSoundReadGame( int version, gzFile in ); - float echo; // 0.0 = none, 1.0 = lots - float stereo; // 0.0 = channels in center, 1.0 = channels on left/right - bool surround; // true = put some channels in back -}; - -// Changes effects configuration -void gbSoundConfigEffects( gb_effects_config_t const& ); -extern gb_effects_config_t gb_effects_config; // current configuration +#endif