]> Shamusworld >> Repos - stargem2/blobdiff - src/sound.cpp
Added config file key bindings, general code cleanup
[stargem2] / src / sound.cpp
index 2f7522047072082c2b3c32fba6a6e04be0f340d4..4191928b2c52584c87680ab6c8701ca7990fe0d6 100755 (executable)
@@ -12,6 +12,7 @@
 // JLH  06/15/2006  Scrubbed all BYTE, WORD & DWORD references from the code
 // JLH  02/10/2009  Fixed sound IRQ callback so that CPU samples are taken at
 //                  cycle exact boundaries
+// JLH  07/21/2009  Moved numeric constants into macros for sanity's sake
 //
 // Notes:
 //   The sound CPU (6808) runs at 894,750 (3,579,000 / 4) Hz.
 
 //using namespace std;
 
-#define AUDIO_SAMPLE_RATE      44100
+//#define AUDIO_SAMPLE_RATE                            44100.0
+#define AUDIO_SAMPLE_RATE                              48000.0
+#define CYCLES_TO_EXECUTE                              (M6808_CLOCK_SPEED_IN_HZ / AUDIO_SAMPLE_RATE)
 
 // Local variables
 
-SDL_AudioSpec desired;
-bool soundInitialized = false;
+static SDL_AudioSpec desired;
+static bool soundInitialized = false;
+static uint32 cyclesToExecuteWholePart;
+static double cyclesToExecuteFractionalPart;
 
 // Private function prototypes
 
@@ -58,10 +63,16 @@ void SoundInit(void)
        if (SDL_OpenAudio(&desired, NULL) < 0)                  // NULL means SDL guarantees what we want
        {
                WriteLog("Sound: Failed to initialize SDL sound.\n");
-//             exit(1);
                return;
        }
 
+       // Setup clock cycles & etc.
+       cyclesToExecuteWholePart = (uint32)CYCLES_TO_EXECUTE;
+       cyclesToExecuteFractionalPart = CYCLES_TO_EXECUTE - (double)cyclesToExecuteWholePart;
+#if 0
+printf("Cycles to execute: %lf; cycles W: %u; cycles F: %lf\n", CYCLES_TO_EXECUTE, cyclesToExecuteWholePart, cyclesToExecuteFractionalPart);
+#endif
+
        SDL_PauseAudio(false);                                                  // Start playback!
        soundInitialized = true;
        WriteLog("Sound: Successfully initialized.\n");
@@ -80,15 +91,6 @@ void SoundDone(void)
        }
 }
 
-/*
-The way to determine the # of cycles for each sample is to take the CPU clock frequency
-and divide by the sample rate.
-
-Like so:
-double cycles = M6808_CLOCK_SPEED_IN_HZ / AUDIO_SAMPLE_RATE;
-Then we need to separate out the fractional part from the integer part.
-*/
-
 //
 // Sound card callback handler
 //
@@ -98,54 +100,21 @@ void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
        extern uint8 sram[];
        int cnt = 0;
 
-#define CYCLE_EXACT_SOUND
-#ifdef CYCLE_EXACT_SOUND
        static float overflow = 0.0;
-       static uint32 time = 20;
-#endif
+       static uint32 time = cyclesToExecuteWholePart;
 
        while (cnt != length)
        {
-               // This is close, but not cycle exact (exact would be 20.289115646...)
-
-//Need to figure out how to get that fraction to execute... !!! FIX !!! [DONE]
-/*
-One way would be to use a fractional accumulator, then subtract 1 every
-time it overflows. Like so:
-
-double overflow = 0;
-uint32 time = 20;
-while (!done)
-{
-       Execute6808(&soundCPU, time);
-       overflow += 0.289115646;
-       if (overflow > 1.0)
-       {
-               overflow -= 1.0;
-               time = 21;
-       }
-       else
-               time = 20;
-}
-*/
-#ifdef CYCLE_EXACT_SOUND
                Execute6808(&soundCPU, time);
                soundCPU.clock -= time;
-#else
-               Execute6808(&soundCPU, 20);
-               soundCPU.clock -= 20;
-#endif
                buffer[cnt++] = sram[0x0400];                           // Fill the buffer with the PIA output value
-#ifdef CYCLE_EXACT_SOUND
-               overflow += 0.289115646;                                        // Fractional part of CPU cycles...
+               time = cyclesToExecuteWholePart;
+               overflow += cyclesToExecuteFractionalPart;
 
                if (overflow > 1.0)
                {
                        overflow -= 1.0;
-                       time = 21;
+                       time++;
                }
-               else
-                       time = 20;
-#endif
        }
 }