From 5e03d45bad1d16b5b1bdab463ba1951433d6dc59 Mon Sep 17 00:00:00 2001 From: stephena Date: Thu, 15 Aug 2002 00:29:40 +0000 Subject: [PATCH] Added native sound support for the SDL version. The SDL version no longer requires the external stella-sound program. Please test extensively. git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@108 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba --- stella/src/build/makefile | 14 ++-- stella/src/ui/sdl/SndSDL.cxx | 148 +++++++++++++++++++++++++++++++++ stella/src/ui/sdl/SndSDL.hxx | 77 +++++++++++++++++ stella/src/ui/sdl/mainSDL.cxx | 7 +- stella/src/ui/sound/TIASound.c | 8 +- 5 files changed, 242 insertions(+), 12 deletions(-) create mode 100644 stella/src/ui/sdl/SndSDL.cxx create mode 100644 stella/src/ui/sdl/SndSDL.hxx diff --git a/stella/src/build/makefile b/stella/src/build/makefile index 9e9a2abe0..96dc68385 100644 --- a/stella/src/build/makefile +++ b/stella/src/build/makefile @@ -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: makefile,v 1.20 2002-08-14 02:30:52 bwmott Exp $ +## $Id: makefile,v 1.21 2002-08-15 00:29:39 stephena Exp $ ##============================================================================ ##============================================================================ @@ -23,16 +23,11 @@ ## Comment a line out to disable that option, remove comment to enable it. ##============================================================================ -### this should work with all compilers -# OPTIMIZATIONS = -### for normal optimization, full warnings -# OPTIMIZATIONS = -O -Wall -### for common optimizations, full warnings except unused vars OPTIMIZATIONS = -O2 -Wall -Wno-unused ### to get full optimization under gcc/x Intel based OS's.. # OPTIMIZATIONS = -O3 -mcpu=pentiumpro -march=pentiumpro -Wall -Wno-unused \ # -funroll-loops -fstrength-reduce -fomit-frame-pointer -ffast-math \ -# -malign-functions=2 -malign-jumps=2 -malign-loops=2 +# -falign-functions=2 -falign-jumps=2 -falign-loops=2 ### to get full optimization under gcc/x Athlon based OS's.. # OPTIMIZATIONS = -O3 -mcpu=athlon -march=athlon -Wall -Wno-unused \ # -funroll-loops -fstrength-reduce -fomit-frame-pointer -ffast-math \ @@ -192,7 +187,7 @@ linux-sdl: LDFLAGS+="$(CFLAGS.SDL)" \ LDLIBS="-lX11 -lXext" \ LDLIBS+="$(LIBS.SDL)" \ - OBJS="mainSDL.o SndUnix.o RectList.o" \ + OBJS="mainSDL.o SndSDL.o RectList.o TIASound.o" \ OBJS+="$(OBJS.SDL)" bsdi-x: make stella.x11 \ @@ -412,6 +407,9 @@ mainX11.o: $(UI)/x11/mainX11.cxx mainSDL.o: $(UI)/sdl/mainSDL.cxx $(CXX) -c $(CXXFLAGS) $(OPTIONS) $(LDFLAGS) $(UI)/sdl/mainSDL.cxx +SndSDL.o: $(UI)/sdl/SndSDL.cxx $(UI)/sdl/SndSDL.hxx + $(CXX) -c $(CXXFLAGS) $(OPTIONS) $(LDFLAGS) $(UI)/sdl/SndSDL.cxx + RectList.o: $(UI)/sdl/RectList.cxx $(UI)/sdl/RectList.hxx $(CXX) -c $(CXXFLAGS) $(OPTIONS) $(LDFLAGS) $(UI)/sdl/RectList.cxx diff --git a/stella/src/ui/sdl/SndSDL.cxx b/stella/src/ui/sdl/SndSDL.cxx new file mode 100644 index 000000000..c954d06e5 --- /dev/null +++ b/stella/src/ui/sdl/SndSDL.cxx @@ -0,0 +1,148 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-1998 by Bradford W. Mott +// +// See the file "license" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +// +// $Id: SndSDL.cxx,v 1.1 2002-08-15 00:29:40 stephena Exp $ +//============================================================================ + +#include + +#include "TIASound.h" +#include "SndSDL.hxx" + +#define FRAGSIZE 1 << 10 + +static int currentVolume = 0; + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +static void fill_audio(void* udata, Uint8* stream, int len) +{ + Uint8 buffer[FRAGSIZE]; + + if(len > FRAGSIZE) + len = FRAGSIZE; + + Tia_process(buffer, len); + SDL_MixAudio(stream, buffer, len, currentVolume); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +SoundSDL::SoundSDL(int volume, bool activate) +{ + myEnabled = activate; + + if(!myEnabled) + return; + + if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) + { + cerr << "Couldn't init SDL audio system: " << SDL_GetError() << endl; + myEnabled = false; + return; + } + + SDL_AudioSpec desired, obtained; + + desired.freq = 31400; + desired.samples = FRAGSIZE; + desired.format = AUDIO_U8; + desired.callback = fill_audio; + desired.userdata = NULL; + desired.channels = 1; + + if(SDL_OpenAudio(&desired, &obtained) < 0) + { + cerr << "Couldn't open SDL audio: " << SDL_GetError() << endl; + myEnabled = false; + return; + } + + /* Initialize the TIA Sound Library */ + Tia_sound_init(31400, obtained.freq); + + isMuted = false; + setVolume(volume); + + SDL_PauseAudio(0); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +SoundSDL::~SoundSDL() +{ + if(myEnabled) + SDL_CloseAudio(); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void SoundSDL::set(Sound::Register reg, uInt8 value) +{ + if(!myEnabled) + return; + + switch(reg) + { + case AUDC0: + Update_tia_sound(0x15, value); + break; + + case AUDC1: + Update_tia_sound(0x16, value); + break; + + case AUDF0: + Update_tia_sound(0x17, value); + break; + + case AUDF1: + Update_tia_sound(0x18, value); + break; + + case AUDV0: + Update_tia_sound(0x19, value); + break; + + case AUDV1: + Update_tia_sound(0x1A, value); + break; + + default: + return; + break; + } +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void SoundSDL::mute(bool state) +{ + if(!myEnabled) + return; + + // Ignore multiple calls to do the same thing + if(isMuted == state) + return; + + isMuted = state; + + SDL_PauseAudio(isMuted ? 1 : 0); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void SoundSDL::setVolume(int percent) +{ + if(!myEnabled) + return; + + if((percent >= 0) && (percent <= 100)) + currentVolume = (int) (((float) percent / 100.0) * (float) SDL_MIX_MAXVOLUME); +} diff --git a/stella/src/ui/sdl/SndSDL.hxx b/stella/src/ui/sdl/SndSDL.hxx new file mode 100644 index 000000000..6b1e893eb --- /dev/null +++ b/stella/src/ui/sdl/SndSDL.hxx @@ -0,0 +1,77 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-1998 by Bradford W. Mott +// +// See the file "license" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +// +// $Id: SndSDL.hxx,v 1.1 2002-08-15 00:29:40 stephena Exp $ +//============================================================================ + +#ifndef SOUNDSDL_HXX +#define SOUNDSDL_HXX + +#include + +#include "bspf.hxx" +#include "Sound.hxx" + +/** + This class implements the sound API for SDL. + + @author Stephen Anthony + @version $Id: SndSDL.hxx,v 1.1 2002-08-15 00:29:40 stephena Exp $ +*/ +class SoundSDL : public Sound +{ + public: + /** + Create a new sound object + */ + SoundSDL(int volume, bool activate = true); + + /** + Destructor + */ + virtual ~SoundSDL(); + + public: + /** + Set the value of the specified sound register + + @param reg The sound register to set + @param val The new value for the sound registers + */ + virtual void set(Sound::Register reg, uInt8 val); + + /** + Set the mute state of the sound object + + @param state Mutes sound iff true + */ + virtual void mute(bool state); + + /** + Set the current volume according to the given percentage + + @param percent Scaled value (0-100) indicating the desired volume + */ + void setVolume(int percent); + + + private: + // Indicates if the sound system was initialized + bool myEnabled; + + // Indicates if the sound is currently muted + bool isMuted; +}; +#endif diff --git a/stella/src/ui/sdl/mainSDL.cxx b/stella/src/ui/sdl/mainSDL.cxx index 11f3c4112..e7b23b6f7 100644 --- a/stella/src/ui/sdl/mainSDL.cxx +++ b/stella/src/ui/sdl/mainSDL.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: mainSDL.cxx,v 1.25 2002-08-04 00:28:18 stephena Exp $ +// $Id: mainSDL.cxx,v 1.26 2002-08-15 00:29:40 stephena Exp $ //============================================================================ #include @@ -39,6 +39,7 @@ #include "SndUnix.hxx" #include "RectList.hxx" #include "Settings.hxx" +#include "SndSDL.hxx" #ifdef HAVE_PNG #include "Snapshot.hxx" @@ -213,7 +214,7 @@ static uInt32 currentState = 0; */ bool setupDisplay() { - Uint32 initflags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK; + Uint32 initflags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_TIMER; if(SDL_Init(initflags) < 0) return false; @@ -1550,7 +1551,7 @@ int main(int argc, char* argv[]) } // Create a sound object for use with the console - SoundUnix sound(settings->theDesiredVolume); + SoundSDL sound(settings->theDesiredVolume); // Get just the filename of the file containing the ROM image const char* filename = (!strrchr(file, '/')) ? file : strrchr(file, '/') + 1; diff --git a/stella/src/ui/sound/TIASound.c b/stella/src/ui/sound/TIASound.c index 726f1d6aa..b2c896432 100644 --- a/stella/src/ui/sound/TIASound.c +++ b/stella/src/ui/sound/TIASound.c @@ -32,6 +32,10 @@ /* */ /*****************************************************************************/ +#ifdef __cplusplus +extern "C" { +#endif + #include #include #include @@ -609,4 +613,6 @@ void Tia_process (register unsigned char *buffer, register uint16 n) } - +#ifdef __cplusplus +} +#endif