From d5981b3b6ed8103b9d4c0c0eb6a91d0ca2cf7aa0 Mon Sep 17 00:00:00 2001 From: stephena Date: Fri, 6 Jan 2006 00:31:56 +0000 Subject: [PATCH] Some sound code fixes, adding the following commandline arguments (which can also be set in the Settings class): -freq : sets the sound sample output frequency, defaults to 31400 -tiafreq : sets the sound sample generation frequency, defaults to 31400 -clipvol : boolean which toggles volume clipping, defaults to true Some fixes to the 'joystick button is an axis' code, so that it actually works in GUI mode. git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@938 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba --- stella/src/common/SoundSDL.cxx | 50 ++++++++++++++++------------ stella/src/emucore/Settings.cxx | 14 +++++++- stella/src/emucore/TIASnd.cxx | 52 +++++++++++++++++------------- stella/src/emucore/TIASnd.hxx | 24 ++++++++++---- stella/src/gui/DialogContainer.cxx | 33 +++++++++---------- 5 files changed, 103 insertions(+), 70 deletions(-) diff --git a/stella/src/common/SoundSDL.cxx b/stella/src/common/SoundSDL.cxx index ba7b8b033..e7bb025c5 100644 --- a/stella/src/common/SoundSDL.cxx +++ b/stella/src/common/SoundSDL.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: SoundSDL.cxx,v 1.27 2005-12-17 01:23:07 stephena Exp $ +// $Id: SoundSDL.cxx,v 1.28 2006-01-06 00:31:55 stephena Exp $ //============================================================================ #ifdef SOUND_SUPPORT @@ -30,21 +30,20 @@ #include "Settings.hxx" #include "System.hxx" #include "OSystem.hxx" -#include "TIASnd.hxx" #include "SoundSDL.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SoundSDL::SoundSDL(OSystem* osystem) - : Sound(osystem), - myIsEnabled(osystem->settings().getBool("sound")), - myIsInitializedFlag(false), - myLastRegisterSetCycle(0), - myDisplayFrameRate(60), - myNumChannels(1), - myFragmentSizeLogBase2(0), - myIsMuted(false), - myVolume(100) + : Sound(osystem), + myIsEnabled(osystem->settings().getBool("sound")), + myIsInitializedFlag(false), + myLastRegisterSetCycle(0), + myDisplayFrameRate(60), + myNumChannels(1), + myFragmentSizeLogBase2(0), + myIsMuted(false), + myVolume(100) { } @@ -93,10 +92,12 @@ void SoundSDL::initialize() else { uInt32 fragsize = myOSystem->settings().getInt("fragsize"); + Int32 frequency = myOSystem->settings().getInt("freq"); + Int32 tiafreq = myOSystem->settings().getInt("tiafreq"); SDL_AudioSpec desired; #ifndef PSP - desired.freq = 31400; + desired.freq = frequency; desired.format = AUDIO_U8; #else desired.freq = 44100; @@ -141,18 +142,25 @@ void SoundSDL::initialize() // Now initialize the TIASound object which will actually generate sound myTIASound.outputFrequency(myHardwareSpec.freq); + myTIASound.tiaFrequency(tiafreq); myTIASound.channels(myHardwareSpec.channels); + bool clipvol = myOSystem->settings().getBool("clipvol"); + myTIASound.clipVolume(clipvol); + // Adjust volume to that defined in settings myVolume = myOSystem->settings().getInt("volume"); setVolume(myVolume); // Show some info if(myOSystem->settings().getBool("showinfo")) - cout << "Sound enabled:" << endl - << " Volume : " << myVolume << endl - << " Frag size: " << fragsize << endl - << " Channels : " << myNumChannels << endl << endl; + cout << "Sound enabled:" << endl + << " Volume : " << myVolume << endl + << " Frag size : " << fragsize << endl + << " Frequency : " << myHardwareSpec.freq << endl + << " TIA Freq. : " << tiafreq << endl + << " Channels : " << myNumChannels << endl + << " Clip volume: " << (int)clipvol << endl << endl; } } @@ -497,11 +505,11 @@ bool SoundSDL::save(Serializer& out) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SoundSDL::RegWriteQueue::RegWriteQueue(uInt32 capacity) - : myCapacity(capacity), - myBuffer(0), - mySize(0), - myHead(0), - myTail(0) + : myCapacity(capacity), + myBuffer(0), + mySize(0), + myHead(0), + myTail(0) { myBuffer = new RegWrite[myCapacity]; } diff --git a/stella/src/emucore/Settings.cxx b/stella/src/emucore/Settings.cxx index 3ad00d8d8..edd79927f 100644 --- a/stella/src/emucore/Settings.cxx +++ b/stella/src/emucore/Settings.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Settings.cxx,v 1.69 2005-12-24 22:50:52 stephena Exp $ +// $Id: Settings.cxx,v 1.70 2006-01-06 00:31:56 stephena Exp $ //============================================================================ #include @@ -54,7 +54,10 @@ Settings::Settings(OSystem* osystem) set("sound", "true"); set("fragsize", "512"); + set("freq", "31400"); + set("tiafreq", "31400"); set("volume", "100"); + set("clipvol", "true"); set("keymap", ""); set("joymap", ""); @@ -228,6 +231,12 @@ void Settings::validate() i = getInt("volume"); if(i < 0 || i > 100) set("volume", "100"); + i = getInt("freq"); + if(i < 0 || i > 48000) + set("freq", "31400"); + i = getInt("tiafreq"); + if(i < 0 || i > 48000) + set("tiafreq", "31400"); #endif i = getInt("zoom"); @@ -284,7 +293,10 @@ void Settings::usage() << " -sound <1|0> Enable sound generation\n" << " -channels <1|2> Enable mono or stereo sound\n" << " -fragsize The size of sound fragments (must be a power of two)\n" + << " -freq Set sound sample output frequency (0 - 48000)\n" + << " -tiafreq Set sound sample generation frequency (0 - 48000)\n" << " -volume Set the volume (0 - 100)\n" + << " -clipvol <1|0> Enable volume clipping (eliminates popping)\n" << endl #endif << " -paddle <0|1|2|3> Indicates which paddle the mouse should emulate\n" diff --git a/stella/src/emucore/TIASnd.cxx b/stella/src/emucore/TIASnd.cxx index 9aedf9185..529bf9943 100644 --- a/stella/src/emucore/TIASnd.cxx +++ b/stella/src/emucore/TIASnd.cxx @@ -13,18 +13,20 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: TIASnd.cxx,v 1.3 2005-10-09 17:31:47 stephena Exp $ +// $Id: TIASnd.cxx,v 1.4 2006-01-06 00:31:56 stephena Exp $ //============================================================================ #include "System.hxx" #include "TIASnd.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -TIASound::TIASound(Int32 outputFrequency, uInt32 channels) - : myOutputFrequency(outputFrequency), - myChannels(channels), - myOutputCounter(0), - myVolumePercentage(100) +TIASound::TIASound(Int32 outputFrequency, Int32 tiaFrequency, uInt32 channels) + : myOutputFrequency(outputFrequency), + myTIAFrequency(tiaFrequency), + myChannels(channels), + myOutputCounter(0), + myVolumePercentage(100), + myVolumeClip(128) { reset(); } @@ -45,18 +47,27 @@ void TIASound::reset() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void TIASound::outputFrequency(uInt32 freq) +void TIASound::outputFrequency(Int32 freq) { myOutputFrequency = freq; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void TIASound::tiaFrequency(Int32 freq) +{ + myTIAFrequency = freq; +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void TIASound::channels(uInt32 number) { - if(number == 2) - myChannels = 2; - else - myChannels = 1; + myChannels = number == 2 ? 2 : 1; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void TIASound::clipVolume(bool clip) +{ + myVolumeClip = clip ? 128 : 0; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -127,9 +138,7 @@ uInt8 TIASound::get(uInt16 address) void TIASound::volume(uInt32 percent) { if((percent >= 0) && (percent <= 100)) - { myVolumePercentage = percent; - } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -350,27 +359,24 @@ void TIASound::process(uInt8* buffer, uInt32 samples) if(myChannels == 1) { // Handle mono sample generation - while((samples > 0) && (myOutputCounter >= TIASoundFrequency)) + while((samples > 0) && (myOutputCounter >= myTIAFrequency)) { *(buffer++) = (((myP4[0] & 8) ? v0 : 0) + - ((myP4[1] & 8) ? v1 : 0)) + 128; - myOutputCounter -= TIASoundFrequency; + ((myP4[1] & 8) ? v1 : 0)) + myVolumeClip; + myOutputCounter -= myTIAFrequency; samples--; } } else { // Handle stereo sample generation - while((samples > 0) && (myOutputCounter >= TIASoundFrequency)) + while((samples > 0) && (myOutputCounter >= myTIAFrequency)) { - *(buffer++) = ((myP4[0] & 8) ? v0 : 0) + 128; - *(buffer++) = ((myP4[1] & 8) ? v1 : 0) + 128; - myOutputCounter -= TIASoundFrequency; + *(buffer++) = ((myP4[0] & 8) ? v0 : 0) + myVolumeClip; + *(buffer++) = ((myP4[1] & 8) ? v1 : 0) + myVolumeClip; + myOutputCounter -= myTIAFrequency; samples--; } } } } - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -const int TIASound::TIASoundFrequency = 31400; diff --git a/stella/src/emucore/TIASnd.hxx b/stella/src/emucore/TIASnd.hxx index ceeb7e002..c5498f7d3 100644 --- a/stella/src/emucore/TIASnd.hxx +++ b/stella/src/emucore/TIASnd.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: TIASnd.hxx,v 1.3 2005-10-09 17:31:47 stephena Exp $ +// $Id: TIASnd.hxx,v 1.4 2006-01-06 00:31:56 stephena Exp $ //============================================================================ #ifndef TIASOUND_HXX @@ -26,18 +26,16 @@ hardware. @author Bradford W. Mott - @version $Id: TIASnd.hxx,v 1.3 2005-10-09 17:31:47 stephena Exp $ + @version $Id: TIASnd.hxx,v 1.4 2006-01-06 00:31:56 stephena Exp $ */ class TIASound { - public: - static const int TIASoundFrequency; - public: /** Create a new TIA Sound object using the specified output frequency */ - TIASound(Int32 outputFrequency = TIASoundFrequency, uInt32 channels = 1); + TIASound(Int32 outputFrequency = 31400, Int32 tiaFrequency = 31400, + uInt32 channels = 1); /** Destructor @@ -53,13 +51,23 @@ class TIASound /** Set the frequency output samples should be generated at */ - void outputFrequency(uInt32 freq); + void outputFrequency(Int32 freq); + + /** + Set the frequency the of the TIA device + */ + void tiaFrequency(Int32 freq); /** Selects the number of audio channels per sample (1 = mono, 2 = stereo) */ void channels(uInt32 number); + /** + Set volume clipping (decrease volume range by half to eliminate popping) + */ + void clipVolume(bool clip); + public: /** Sets the specified sound register to the given value @@ -134,9 +142,11 @@ class TIASound uInt8 myP5[2]; // 5-bit register LFSR (lower 5 bits used) Int32 myOutputFrequency; + Int32 myTIAFrequency; uInt32 myChannels; Int32 myOutputCounter; uInt32 myVolumePercentage; + uInt8 myVolumeClip; }; #endif diff --git a/stella/src/gui/DialogContainer.cxx b/stella/src/gui/DialogContainer.cxx index 5548c1111..78f69f7fd 100644 --- a/stella/src/gui/DialogContainer.cxx +++ b/stella/src/gui/DialogContainer.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: DialogContainer.cxx,v 1.24 2006-01-05 18:53:23 stephena Exp $ +// $Id: DialogContainer.cxx,v 1.25 2006-01-06 00:31:56 stephena Exp $ //============================================================================ #include "OSystem.hxx" @@ -274,7 +274,20 @@ void DialogContainer::handleJoyEvent(int stick, int button, uInt8 state) // Send the event to the dialog box on the top of the stack Dialog* activeDialog = myDialogStack.top(); - if(activeDialog->wantsEvents()) + // Some buttons act as directions. In those cases, translate them + // to axis events instead of mouse button events + int up, down, left, right = -1; + int value = state > 0 ? 32767 : 0; + myOSystem->getJoyButtonDirections(up, down, left, right); + if(button == up) + handleJoyAxisEvent(stick, 1, -value); // axis 1, -value ==> UP + else if(button == down) + handleJoyAxisEvent(stick, 1, value); // axis 1, +value ==> DOWN + else if(button == left) + handleJoyAxisEvent(stick, 0, -value); // axis 0, -value ==> LEFT + else if(button == right) + handleJoyAxisEvent(stick, 0, value); // axis 0, +value ==> RIGHT + else if(activeDialog->wantsEvents()) { if(state == 1) activeDialog->handleJoyDown(stick, button); @@ -282,24 +295,8 @@ void DialogContainer::handleJoyEvent(int stick, int button, uInt8 state) activeDialog->handleJoyUp(stick, button); } else - { - // Some buttons act as directions. In those cases, translate them - // to axis events instead of mouse button events - int up, down, left, right = -1; - int value = state > 0 ? 32767 : 0; - myOSystem->getJoyButtonDirections(up, down, left, right); - if(button == up) - handleJoyAxisEvent(stick, 1, value); // axis 1, +value ==> UP - else if(button == down) - handleJoyAxisEvent(stick, 1, -value); // axis 1, -value ==> DOWN - else if(button == left) - handleJoyAxisEvent(stick, 0, value); // axis 0, +value ==> LEFT - else if(button == right) - handleJoyAxisEvent(stick, 0, -value); // axis 0, -value ==> RIGHT - else myOSystem->eventHandler().createMouseButtonEvent( ourJoyMouse.x, ourJoyMouse.y, state); - } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -