From 7141f3197f6894616dd7fb0a965281395c3ae09a Mon Sep 17 00:00:00 2001 From: spacy51 Date: Tue, 26 Aug 2008 23:45:34 +0000 Subject: [PATCH] Sound code clean up git-svn-id: https://svn.code.sf.net/p/vbam/code/trunk@642 a31d4220-a93d-0410-bf67-fe4944624d44 --- src/Sound.cpp | 68 +++++++++------------- src/Sound.h | 108 ++++++++++++++++++++--------------- src/dmg/gbSound.cpp | 23 ++++---- src/dmg/gbSound.h | 5 -- src/win32/DirectSound.cpp | 7 +-- src/win32/MainWndOptions.cpp | 27 +++++---- src/win32/OpenAL.cpp | 1 - src/win32/VBA.cpp | 10 +--- src/win32/XAudio2.cpp | 1 - 9 files changed, 123 insertions(+), 127 deletions(-) diff --git a/src/Sound.cpp b/src/Sound.cpp index 1f5b14ff..9e144b66 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -58,9 +58,11 @@ int soundQuality = 2; int soundInterpolation = 0; float soundFiltering = 1; static float soundFiltering_; -int soundVolume = 0; -static int soundVolume_; -int soundEnableFlag = 0x3ff; // emulator channels enabled +float soundVolume = 1; +static float soundVolume_ = -1; +bool soundEcho = false; + +static int soundEnableFlag = 0x3ff; // emulator channels enabled int const SOUND_CLOCK_TICKS_ = 167772; int SOUND_CLOCK_TICKS = SOUND_CLOCK_TICKS_; @@ -69,7 +71,6 @@ int soundTicks = SOUND_CLOCK_TICKS_; u16 soundFinalWave [1470]; int soundBufferLen = sizeof soundFinalWave; -int soundDebug = 0; bool soundPaused = true; void interp_rate() { /* empty for now */ } @@ -297,20 +298,16 @@ static void apply_volume( bool apu_only = false ) if ( !apu_only ) soundVolume_ = soundVolume; - // Emulator volume - static float const vols [6] = { 1, 2, 3, 4, 0.25, 0.5 }; - double const volume = vols [soundVolume_]; - if ( gb_apu ) { static float const apu_vols [4] = { 0.25, 0.5, 1, 0.25 }; - gb_apu->volume( volume * apu_vols [ioMem [SGCNT0_H] & 3] ); + gb_apu->volume( soundVolume_ * apu_vols [ioMem [SGCNT0_H] & 3] ); } if ( !apu_only ) { for ( int i = 0; i < 3; i++ ) - pcm_synth [i].volume( 0.66 / 256 * volume ); + pcm_synth [i].volume( 0.66 / 256 * soundVolume_ ); } } @@ -381,13 +378,10 @@ static void flush_samples() while ( stereo_buffer->samples_avail() >= out_buf_size ) { stereo_buffer->read_samples( (blip_sample_t*) soundFinalWave, out_buf_size ); - if(systemSoundOn) - { - if(soundPaused) - soundResume(); + if(soundPaused) + soundResume(); - systemWriteDataToSoundBuffer(); - } + systemWriteDataToSoundBuffer(); } } @@ -407,9 +401,9 @@ static void apply_filtering() } } -static void soundTick() +void psoundTickfn() { - if ( systemSoundOn && gb_apu && stereo_buffer ) + if ( gb_apu && stereo_buffer ) { // Run sound hardware to present end_frame( SOUND_CLOCK_TICKS ); @@ -424,8 +418,6 @@ static void soundTick() } } -void (*psoundTickfn)() = soundTick; - static void apply_muting() { if ( !stereo_buffer || !ioMem ) @@ -492,16 +484,6 @@ static void remake_stereo_buffer() apply_volume(); } -void setsystemSoundOn(bool value) -{ - systemSoundOn = value; -} - -void setsoundPaused(bool value) -{ - soundPaused = value; -} - void soundShutdown() { systemSoundShutdown(); @@ -509,28 +491,32 @@ void soundShutdown() void soundPause() { + soundPaused = true; systemSoundPause(); - setsoundPaused(true); } void soundResume() { + soundPaused = false; systemSoundResume(); - setsoundPaused(false); } -void soundEnable(int channels) +void soundSetVolume( float volume ) +{ + soundVolume = volume; +} + +float soundGetVolume() +{ + return soundVolume; +} + +void soundSetEnable(int channels) { soundEnableFlag = channels; apply_muting(); } -void soundDisable(int channels) -{ - soundEnableFlag &= ~channels; - apply_muting(); -} - int soundGetEnable() { return (soundEnableFlag & 0x30f); @@ -543,7 +529,7 @@ void soundReset() remake_stereo_buffer(); reset_apu(); - setsoundPaused(true); + soundPaused = true; SOUND_CLOCK_TICKS = SOUND_CLOCK_TICKS_; soundTicks = SOUND_CLOCK_TICKS_; @@ -789,7 +775,7 @@ static void soundReadGameOld( gzFile in, int version ) pcm [0].dac = state.soundDSAValue; pcm [1].dac = state.soundDSBValue; - int quality = utilReadInt( in ); // ignore this crap + (void) utilReadInt( in ); // ignore quality } #include diff --git a/src/Sound.h b/src/Sound.h index cc53913e..6526fbd5 100644 --- a/src/Sound.h +++ b/src/Sound.h @@ -1,64 +1,80 @@ -// -*- C++ -*- -// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. -// Copyright (C) 1999-2003 Forgotten -// Copyright (C) 2004 Forgotten and the VBA development team -// Copyright (C) 2004-2006 VBA development team -// Copyright (C) 2007-2008 VBA-M development team -// 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. +// GBA sound emulation #ifndef VBA_SOUND_H #define VBA_SOUND_H #include "System.h" +//// Setup/options + +// 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(); + +// Manages muting bitmask. The bits control the following channels: +// 0x001 Pulse 1 +// 0x002 Pulse 2 +// 0x004 Wave +// 0x008 Noise +// 0x100 GBA left +// 0x200 GBA right +void soundSetEnable( int mask ); +int soundGetEnable(); + +// Pauses/resumes system sound output +void soundPause(); +void soundResume(); +extern bool soundPaused; // current paused state + +// Cleans up sound. Afterwards, soundInit() can be called again. +void soundShutdown(); + +// Sound buffering +extern int soundBufferLen; // size of sound buffer in BYTES +extern u16 soundFinalWave[1470];// 16-bit SIGNED stereo sample buffer + +// 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 register addresses #define SGCNT0_H 0x82 #define FIFOA_L 0xa0 #define FIFOA_H 0xa2 #define FIFOB_L 0xa4 #define FIFOB_H 0xa6 -extern void (*psoundTickfn)(); -extern void soundShutdown(); -extern bool soundInit(); -extern void soundPause(); -extern void soundResume(); -extern void soundEnable(int); -extern void soundDisable(int); -extern int soundGetEnable(); -extern void soundReset(); -extern void soundSaveGame(gzFile); -extern void soundReadGame(gzFile, int); -extern void soundEvent(u32, u8); -extern void soundEvent(u32, u16); -extern void soundTimerOverflow(int); -extern void soundSetQuality(int); -extern void setsystemSoundOn(bool value); -extern void setsoundPaused(bool value); -extern void interp_rate(); +// Resets emulated sound hardware +void soundReset(); +// Emulates write to sound hardware +void soundEvent( u32 addr, u8 data ); +void soundEvent( u32 addr, u16 data ); // TODO: error-prone to overload like this + +// Notifies emulator that a timer has overflowed +void soundTimerOverflow( int which ); + +// Notifies emulator that PCM rate may have changed +void interp_rate(); + +// Notifies emulator that SOUND_CLOCK_TICKS clocks have passed +void psoundTickfn(); extern int SOUND_CLOCK_TICKS; // Number of 16.8 MHz clocks between calls to soundTick() extern int soundTicks; // Number of 16.8 MHz clocks until soundTick() will be called -extern int soundQuality; // sample rate = 44100 / soundQuality -extern int soundBufferLen; // size of sound buffer in BYTES -extern u16 soundFinalWave[1470];// 16-bit SIGNED stereo sample buffer -extern int soundVolume; // emulator volume code (not linear) -extern int soundInterpolation; // 1 if PCM should have low-pass filtering -extern float soundFiltering; // 0.0 = none, 1.0 = max (only if soundInterpolation!=0) - -// Unknown purpose -extern bool soundPaused; +// Saves/loads emulator state +void soundSaveGame( gzFile ); +void soundReadGame( gzFile, int version ); #endif // VBA_SOUND_H diff --git a/src/dmg/gbSound.cpp b/src/dmg/gbSound.cpp index fa45581c..9a92efe5 100644 --- a/src/dmg/gbSound.cpp +++ b/src/dmg/gbSound.cpp @@ -26,11 +26,12 @@ #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 soundVolume; extern int gbHardware; @@ -82,13 +83,10 @@ static void flush_samples() while ( stereo_buffer->samples_avail() >= out_buf_size ) { stereo_buffer->read_samples( (blip_sample_t*) soundFinalWave, out_buf_size ); - if(systemSoundOn) - { - if(soundPaused) - soundResume(); + if(soundPaused) + soundResume(); - systemWriteDataToSoundBuffer(); - } + systemWriteDataToSoundBuffer(); } } @@ -97,8 +95,11 @@ int const chan_count = 4; gb_effects_config_t gb_effects_config; static gb_effects_config_t gb_effects_config_current; +static int prevSoundEnable = -1; + static void apply_effects() { + prevSoundEnable = soundGetEnable(); gb_effects_config_current = gb_effects_config; stereo_buffer->config().enabled = gb_effects_config_current.enabled; @@ -109,7 +110,9 @@ static void apply_effects() for ( int i = 0; i < chan_count; i++ ) { - Multi_Buffer::channel_t ch = stereo_buffer->channel( i ); + Multi_Buffer::channel_t ch = { 0, 0, 0 }; + if ( prevSoundEnable >> i & 1 ) + ch = stereo_buffer->channel( i ); gb_apu->set_output( ch.center, ch.left, ch.right, i ); } } @@ -121,7 +124,7 @@ void gbSoundConfigEffects( gb_effects_config_t const& c ) void gbSoundTick() { - if ( systemSoundOn && gb_apu && stereo_buffer ) + if ( gb_apu && stereo_buffer ) { // Run sound hardware to present end_frame( SOUND_CLOCK_TICKS * ticks_to_time ); @@ -130,7 +133,7 @@ void gbSoundTick() // Update effects config if it was changed if ( memcmp( &gb_effects_config_current, &gb_effects_config, - sizeof gb_effects_config ) ) + sizeof gb_effects_config ) || soundGetEnable() != prevSoundEnable ) apply_effects(); } } diff --git a/src/dmg/gbSound.h b/src/dmg/gbSound.h index d37f4935..62402aa4 100644 --- a/src/dmg/gbSound.h +++ b/src/dmg/gbSound.h @@ -17,9 +17,6 @@ // along with this program; if not, write to the Free Software Foundation, // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#ifndef GBSOUND_H -#define GBSOUND_H - #define NR10 0xff10 #define NR11 0xff11 #define NR12 0xff12 @@ -75,5 +72,3 @@ struct gb_effects_config_t // Changes effects configuration void gbSoundConfigEffects( gb_effects_config_t const& ); extern gb_effects_config_t gb_effects_config; // current configuration - -#endif diff --git a/src/win32/DirectSound.cpp b/src/win32/DirectSound.cpp index 9be2dfad..d4efbae1 100644 --- a/src/win32/DirectSound.cpp +++ b/src/win32/DirectSound.cpp @@ -35,7 +35,6 @@ #include extern bool soundBufferLow; -extern void setsystemSoundOn(bool value); class DirectSound : public ISound { @@ -206,7 +205,6 @@ bool DirectSound::init() return false; } - setsystemSoundOn(true); return true; } @@ -279,9 +277,10 @@ void DirectSound::write() } } } - } else { + }/* else { + // TODO: remove? setsoundPaused(true); - } + }*/ } diff --git a/src/win32/MainWndOptions.cpp b/src/win32/MainWndOptions.cpp index 46f7e321..424f8d07 100644 --- a/src/win32/MainWndOptions.cpp +++ b/src/win32/MainWndOptions.cpp @@ -802,13 +802,16 @@ void MainWnd::OnUpdateOptionsEmulatorBmpformat(CCmdUI* pCmdUI) void MainWnd::OnOptionsSoundMute() { - soundDisable(0x30f); + if( 0 == soundGetEnable() ) { + soundSetEnable( 0x30f ); + } else { + soundSetEnable( 0x000 ); + } } void MainWnd::OnUpdateOptionsSoundMute(CCmdUI* pCmdUI) { - int active = soundGetEnable() & 0x30f; - pCmdUI->SetCheck(active == 0); + pCmdUI->SetCheck( 0 == soundGetEnable() ); } void MainWnd::OnAudioEffects() @@ -876,34 +879,35 @@ void MainWnd::OnUpdateOptionsSound44khz(CCmdUI* pCmdUI) BOOL MainWnd::OnOptionsSoundVolume(UINT nID) { - soundVolume = nID - ID_OPTIONS_SOUND_VOLUME_1X; - return TRUE; + UINT soundVolume = nID - ID_OPTIONS_SOUND_VOLUME_1X; + soundSetVolume( (float)soundVolume ); + return TRUE; } void MainWnd::OnUpdateOptionsSoundVolume(CCmdUI *pCmdUI) { - pCmdUI->SetCheck(soundVolume == (int)(pCmdUI->m_nID - ID_OPTIONS_SOUND_VOLUME_1X)); + pCmdUI->SetCheck( soundGetVolume() == (float)(pCmdUI->m_nID - ID_OPTIONS_SOUND_VOLUME_1X) ); } void MainWnd::OnOptionsSoundVolume25x() { - soundVolume = 4; + soundSetVolume( 0.25f ); } void MainWnd::OnUpdateOptionsSoundVolume25x(CCmdUI* pCmdUI) { - pCmdUI->SetCheck(soundVolume == 4); + pCmdUI->SetCheck( 0.25f == soundGetVolume() ); } void MainWnd::OnOptionsSoundVolume5x() { - soundVolume = 5; + soundSetVolume( 0.5f ); } void MainWnd::OnUpdateOptionsSoundVolume5x(CCmdUI* pCmdUI) { - pCmdUI->SetCheck(soundVolume == 5); + pCmdUI->SetCheck( 0.5f == soundGetVolume() ); } void MainWnd::updateSoundChannels(UINT id) @@ -935,8 +939,7 @@ void MainWnd::updateSoundChannels(UINT id) else active |= flag; - soundEnable(active); - soundDisable((~active)&0x30f); + soundSetEnable(active); } void MainWnd::OnOptionsSoundChannel1() diff --git a/src/win32/OpenAL.cpp b/src/win32/OpenAL.cpp index beeb35e4..b46cd27d 100644 --- a/src/win32/OpenAL.cpp +++ b/src/win32/OpenAL.cpp @@ -197,7 +197,6 @@ bool OpenAL::init() soundBufferLen = ( freq / 60 ) * 4; - setsystemSoundOn( true ); initialized = true; return true; } diff --git a/src/win32/VBA.cpp b/src/win32/VBA.cpp index cd2a54bf..a86f5bf9 100644 --- a/src/win32/VBA.cpp +++ b/src/win32/VBA.cpp @@ -1585,15 +1585,11 @@ void VBA::loadSettings() } int res = regQueryDwordValue("soundEnable", 0x30f); - - soundEnable(res); - soundDisable(~res); + soundSetEnable(res); soundQuality = regQueryDwordValue("soundQuality", 1); - soundVolume = regQueryDwordValue("soundVolume", 0); - if(soundVolume < 0 || soundVolume > 5) - soundVolume = 0; + soundSetVolume( (float)(regQueryDwordValue("soundVolume", 100)) / 100.0f ); soundInterpolation = regQueryDwordValue("soundInterpolation", 0); if(soundInterpolation < 0 || soundInterpolation > 1) @@ -2585,7 +2581,7 @@ void VBA::saveSettings() regSetDwordValue("soundQuality", soundQuality); - regSetDwordValue("soundVolume", soundVolume); + regSetDwordValue("soundVolume", (DWORD)(soundGetVolume() * 100.0f)); regSetDwordValue("soundInterpolation", soundInterpolation); regSetDwordValue("tripleBuffering", tripleBuffering); diff --git a/src/win32/XAudio2.cpp b/src/win32/XAudio2.cpp index 96787c76..3213a7b9 100644 --- a/src/win32/XAudio2.cpp +++ b/src/win32/XAudio2.cpp @@ -289,7 +289,6 @@ bool XAudio2_Output::init() playing = true; - setsystemSoundOn( true ); initialized = true; return true; }