]> Shamusworld >> Repos - virtualjaguar/blobdiff - src/dac.cpp
Initial fixes for audio subsystem. Fragile; may break in unexpected ways.
[virtualjaguar] / src / dac.cpp
index dbb1b5842caeef2469dce21d0c46589391a4f259..617918a13f725f248f9403a5bf6741706a77e4a8 100644 (file)
@@ -3,10 +3,10 @@
 //
 // Originally by David Raingeard
 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
-// Rewritten by James L. Hammons
+// Rewritten by James Hammons
 // (C) 2010 Underground Software
 //
-// JLH = James L. Hammons <jlhamm@acm.org>
+// JLH = James Hammons <jlhamm@acm.org>
 //
 // Who  When        What
 // ---  ----------  -------------------------------------------------------------
@@ -69,7 +69,6 @@ static uint8 SCLKFrequencyDivider = 19;                               // Default is roughly 22 KHz (20774 H
 // Private function prototypes
 
 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
-int GetCalculatedFrequency(void);
 
 //
 // Initialize the SDL sound system
@@ -201,6 +200,44 @@ int GetCalculatedFrequency(void)
        return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
 }
 
+static int oldFreq = 0;
+
+void DACSetNewFrequency(int freq)
+{
+       if (freq == oldFreq)
+               return;
+
+       oldFreq = freq;
+
+       // Should do some sanity checking on the frequency...
+
+       if (SDLSoundInitialized)
+               SDL_CloseAudio();
+
+       desired.freq = freq;// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
+       WriteLog("DAC: Changing sample rate to %u Hz!\n", desired.freq);
+
+       if (SDLSoundInitialized)
+       {
+               if (SDL_OpenAudio(&desired, NULL) < 0)  // NULL means SDL guarantees what we want
+               {
+// This is bad, Bad, BAD !!! DON'T ABORT BECAUSE WE DIDN'T GET OUR FREQ! !!! FIX !!!
+#warning !!! FIX !!! Aborting because of SDL audio problem is bad!
+                       WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
+//                                             LogDone();
+//                                             exit(1);
+#warning "Reimplement GUICrashGracefully!"
+//                                             GUICrashGracefully("Failed to initialize SDL sound!");
+                       return;
+               }
+       }
+
+       DACReset();
+
+       if (SDLSoundInitialized)
+               SDL_PauseAudio(false);                  // Start playback!
+}
+
 //
 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
 //