2 // Sound Interface v2.0
5 // (c) 2006 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 06/15/2006 Added changelog ;-)
12 // JLH 06/15/2006 Scrubbed all BYTE, WORD & DWORD references from the code
15 // The sound CPU (6808) runs at 894,750 (3,579,000 / 4) Hz.
16 // At 44.1 KHz, this works out to 894750 / 44100 = 20.289115646... cycles per sample.
19 // Still need to add volume control...
28 //using namespace std;
32 SDL_AudioSpec desired;
33 bool soundInitialized = false;
35 // Private function prototypes
37 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
40 // Initialize the SDL sound system
44 // memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
46 desired.freq = 44100; // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
47 desired.format = AUDIO_U8; // This uses the native endian (for portability)...
49 // desired.samples = 4096; // Let's try a 4K buffer (can always go lower)
50 desired.samples = 2048; // Let's try a 2K buffer (can always go lower)
51 desired.callback = SDLSoundCallback;
53 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
55 WriteLog("Sound: Failed to initialize SDL sound.\n");
60 SDL_PauseAudio(false); // Start playback!
61 soundInitialized = true;
62 WriteLog("Sound: Successfully initialized.\n");
66 // Close down the SDL sound subsystem
74 WriteLog("Sound: Done.\n");
79 // Sound card callback handler
81 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
83 extern V6808REGS soundCPU;
84 // extern uint8 * sram;
88 #define CYCLE_EXACT_SOUND
89 #ifdef CYCLE_EXACT_SOUND
96 // This is close, but not cycle exact (exact would be 20.289115646...)
98 //Need to figure out how to get that fraction to execute... !!! FIX !!!
100 One way would be to use a fractional accumulator, then subtract 1 every
101 time it overflows. Like so:
107 Execute6808(&soundCPU, time);
108 overflow += 0.289115646;
118 #ifdef CYCLE_EXACT_SOUND
119 Execute6808(&soundCPU, time);
120 soundCPU.clock -= time;
122 Execute6808(&soundCPU, 20);
123 soundCPU.clock -= 20;
125 buffer[cnt++] = sram[0x0400]; // Fill the buffer with the PIA output value
126 #ifdef CYCLE_EXACT_SOUND
127 overflow += 0.289115646;