5 // (C) 2005 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 12/02/2005 Fixed a problem with sound callback thread signaling the
13 // JLH 12/03/2005 Fixed sound callback dropping samples when the sample buffer
14 // is shorter than the callback sample buffer
19 // - Figure out why it's losing samples (Bard's Tale) [DONE]
20 // - Figure out why it's playing too fast [DONE]
25 #include <string.h> // For memset, memcpy
32 //#define WRITE_OUT_WAVE
34 //#define SAMPLE_RATE (44100.0)
35 #define SAMPLE_RATE (48000.0)
36 #define SAMPLES_PER_FRAME (SAMPLE_RATE / 60.0)
37 #define CYCLES_PER_SAMPLE (1024000.0 / SAMPLE_RATE)
38 //#define SOUND_BUFFER_SIZE (8192)
39 // 32K ought to be enough for anybody
40 #define SOUND_BUFFER_SIZE (32768)
47 static SDL_AudioSpec desired, obtained;
48 static SDL_AudioDeviceID device;
49 static bool soundInitialized = false;
50 static bool speakerState = false;
51 static int16_t soundBuffer[SOUND_BUFFER_SIZE];
52 static uint32_t soundBufferPos;
53 static uint64_t lastToggleCycles;
54 static SDL_cond * conditional = NULL;
55 static SDL_mutex * mutex = NULL;
56 static SDL_mutex * mutex2 = NULL;
57 static int16_t sample;
58 static uint8_t ampPtr = 12; // Start with -2047 - +2047
59 static int16_t amplitude[17] = { 0, 1, 2, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047,
60 4095, 8191, 16383, 32767 };
62 static FILE * fp = NULL;
65 // Private function prototypes
67 static void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
71 // Initialize the SDL sound system
76 // To weed out problems for now...
80 desired.freq = SAMPLE_RATE; // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
81 desired.format = AUDIO_S16SYS; // This uses the native endian (for portability)...
83 desired.samples = 512; // Let's try a 1/2K buffer (can always go lower)
84 desired.callback = SDLSoundCallback;
86 device = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);
90 WriteLog("Sound: Failed to initialize SDL sound.\n");
94 conditional = SDL_CreateCond();
95 mutex = SDL_CreateMutex();
96 mutex2 = SDL_CreateMutex();// Let's try real signalling...
99 sample = desired.silence; // ? wilwok ? yes
101 SDL_PauseAudioDevice(device, 0); // Start playback!
102 soundInitialized = true;
103 WriteLog("Sound: Successfully initialized.\n");
105 #ifdef WRITE_OUT_WAVE
106 fp = fopen("./apple2.wav", "wb");
112 // Close down the SDL sound subsystem
116 if (soundInitialized)
118 SDL_PauseAudioDevice(device, 1);
119 SDL_CloseAudioDevice(device);
120 SDL_DestroyCond(conditional);
121 SDL_DestroyMutex(mutex);
122 SDL_DestroyMutex(mutex2);
123 WriteLog("Sound: Done.\n");
125 #ifdef WRITE_OUT_WAVE
132 void SoundPause(void)
134 if (soundInitialized)
135 SDL_PauseAudioDevice(device, 1);
139 void SoundResume(void)
141 if (soundInitialized)
142 SDL_PauseAudioDevice(device, 0);
147 // Sound card callback handler
149 static void SDLSoundCallback(void * /*userdata*/, Uint8 * buffer8, int length8)
151 //WriteLog("SDLSoundCallback(): begin (soundBufferPos=%i)\n", soundBufferPos);
152 // The sound buffer should only starve when starting which will cause it to
153 // lag behind the emulation at most by around 1 frame...
154 // (Actually, this should never happen since we fill the buffer beforehand.)
155 // (But, then again, if the sound hasn't been toggled for a while, then this
156 // makes perfect sense as the buffer won't have been filled at all!)
157 // (Should NOT starve now, now that we properly handle frame edges...)
159 // Let's try using a mutex for shared resource consumption...
160 //Actually, I think Lock/UnlockAudio() does this already...
161 //WriteLog("SDLSoundCallback: soundBufferPos = %i\n", soundBufferPos);
164 // Recast this as a 16-bit type...
165 int16_t * buffer = (int16_t *)buffer8;
166 uint32_t length = (uint32_t)length8 / 2;
168 //WriteLog("SDLSoundCallback(): filling buffer...\n");
169 if (soundBufferPos < length)
171 // The sound buffer is starved...
172 for(uint32_t i=0; i<soundBufferPos; i++)
173 buffer[i] = soundBuffer[i];
175 // Fill buffer with last value
176 for(uint32_t i=soundBufferPos; i<length; i++)
179 // Reset soundBufferPos to start of buffer...
184 // Fill sound buffer with frame buffered sound
185 for(uint32_t i=0; i<length; i++)
186 buffer[i] = soundBuffer[i];
188 soundBufferPos -= length;
190 // Move current buffer down to start
191 for(uint32_t i=0; i<soundBufferPos; i++)
192 soundBuffer[i] = soundBuffer[length + i];
196 //WriteLog("SDLSoundCallback(): SDL_mutexV(mutex2)\n");
198 // Wake up any threads waiting for the buffer to drain...
199 SDL_CondSignal(conditional);
200 //WriteLog("SDLSoundCallback(): end\n");
204 // This is called by the main CPU thread every ~21.333 cycles.
205 void WriteSampleToBuffer(void)
207 //WriteLog("WriteSampleToBuffer(): SDL_mutexP(mutex2)\n");
210 // This should almost never happen, but, if it does...
211 while (soundBufferPos >= (SOUND_BUFFER_SIZE - 1))
213 //WriteLog("WriteSampleToBuffer(): Waiting for sound thread. soundBufferPos=%i, SOUNDBUFFERSIZE-1=%i\n", soundBufferPos, SOUND_BUFFER_SIZE-1);
214 SDL_mutexV(mutex2); // Release it so sound thread can get it,
215 SDL_mutexP(mutex); // Must lock the mutex for the cond to work properly...
216 SDL_CondWait(conditional, mutex); // Sleep/wait for the sound thread
217 SDL_mutexV(mutex); // Must unlock the mutex for the cond to work properly...
218 SDL_mutexP(mutex2); // Re-lock it until we're done with it...
221 soundBuffer[soundBufferPos++] = sample;
222 //WriteLog("WriteSampleToBuffer(): SDL_mutexV(mutex2)\n");
227 void ToggleSpeaker(void)
229 if (!soundInitialized)
232 speakerState = !speakerState;
233 sample = (speakerState ? amplitude[ampPtr] : -amplitude[ampPtr]);
239 // Currently set for 16-bit samples
245 void VolumeDown(void)
252 uint8_t GetVolume(void)