5 // (C) 2014 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
23 #include <string.h> // For memset, memcpy
30 //#define WRITE_OUT_WAVE
32 //#define SAMPLE_RATE (44100.0)
33 #define SAMPLE_RATE (48000.0)
34 #define SAMPLES_PER_FRAME (SAMPLE_RATE / 60.0)
35 #define CYCLES_PER_SAMPLE (1024000.0 / SAMPLE_RATE)
36 //#define SOUND_BUFFER_SIZE (8192)
37 #define SOUND_BUFFER_SIZE (32768)
44 static SDL_AudioSpec desired, obtained;
45 static SDL_AudioDeviceID device;
46 static bool soundInitialized = false;
47 static bool speakerState = false;
48 static int16_t soundBuffer[SOUND_BUFFER_SIZE];
49 static uint32_t soundBufferPos;
50 static uint64_t lastToggleCycles;
51 static SDL_cond * conditional = NULL;
52 static SDL_mutex * mutex = NULL;
53 static SDL_mutex * mutex2 = NULL;
54 static int16_t sample;
55 static uint8_t ampPtr = 12; // Start with -2047 - +2047
56 static int16_t amplitude[17] = { 0, 1, 2, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047,
57 4095, 8191, 16383, 32767 };
59 static FILE * fp = NULL;
62 // Private function prototypes
64 static void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
68 // Initialize the SDL sound system
73 // To weed out problems for now...
77 desired.freq = SAMPLE_RATE; // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
78 desired.format = AUDIO_S16SYS; // This uses the native endian (for portability)...
80 desired.samples = 512; // Let's try a 1/2K buffer (can always go lower)
81 desired.callback = SDLSoundCallback;
83 device = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);
87 WriteLog("Sound: Failed to initialize SDL sound.\n");
91 conditional = SDL_CreateCond();
92 mutex = SDL_CreateMutex();
93 mutex2 = SDL_CreateMutex();// Let's try real signalling...
96 sample = desired.silence; // ? wilwok ? yes
98 SDL_PauseAudioDevice(device, 0); // Start playback!
99 soundInitialized = true;
100 WriteLog("Sound: Successfully initialized.\n");
102 #ifdef WRITE_OUT_WAVE
103 fp = fopen("./apple2.wav", "wb");
109 // Close down the SDL sound subsystem
113 if (soundInitialized)
115 SDL_PauseAudioDevice(device, 1);
116 SDL_CloseAudioDevice(device);
117 SDL_DestroyCond(conditional);
118 SDL_DestroyMutex(mutex);
119 SDL_DestroyMutex(mutex2);
120 WriteLog("Sound: Done.\n");
122 #ifdef WRITE_OUT_WAVE
129 void SoundPause(void)
131 if (soundInitialized)
132 SDL_PauseAudioDevice(device, 1);
136 void SoundResume(void)
138 if (soundInitialized)
139 SDL_PauseAudioDevice(device, 0);
144 // Sound card callback handler
146 static void SDLSoundCallback(void * /*userdata*/, Uint8 * buffer8, int length8)
148 //WriteLog("SDLSoundCallback(): begin (soundBufferPos=%i)\n", soundBufferPos);
149 // The sound buffer should only starve when starting which will cause it to
150 // lag behind the emulation at most by around 1 frame...
151 // (Actually, this should never happen since we fill the buffer beforehand.)
152 // (But, then again, if the sound hasn't been toggled for a while, then this
153 // makes perfect sense as the buffer won't have been filled at all!)
154 // (Should NOT starve now, now that we properly handle frame edges...)
156 // Let's try using a mutex for shared resource consumption...
157 //Actually, I think Lock/UnlockAudio() does this already...
158 //WriteLog("SDLSoundCallback: soundBufferPos = %i\n", soundBufferPos);
161 // Recast this as a 16-bit type...
162 int16_t * buffer = (int16_t *)buffer8;
163 uint32_t length = (uint32_t)length8 / 2;
165 //WriteLog("SDLSoundCallback(): filling buffer...\n");
166 if (soundBufferPos < length)
168 // The sound buffer is starved...
169 for(uint32_t i=0; i<soundBufferPos; i++)
170 buffer[i] = soundBuffer[i];
172 // Fill buffer with last value
173 for(uint32_t i=soundBufferPos; i<length; i++)
176 // Reset soundBufferPos to start of buffer...
181 // Fill sound buffer with frame buffered sound
182 for(uint32_t i=0; i<length; i++)
183 buffer[i] = soundBuffer[i];
185 soundBufferPos -= length;
187 // Move current buffer down to start
188 for(uint32_t i=0; i<soundBufferPos; i++)
189 soundBuffer[i] = soundBuffer[length + i];
193 //WriteLog("SDLSoundCallback(): SDL_mutexV(mutex2)\n");
195 // Wake up any threads waiting for the buffer to drain...
196 SDL_CondSignal(conditional);
197 //WriteLog("SDLSoundCallback(): end\n");
201 // This is called by the main CPU thread every ~21.333 cycles.
202 void WriteSampleToBuffer(void)
204 //WriteLog("WriteSampleToBuffer(): SDL_mutexP(mutex2)\n");
207 // This should almost never happen, but, if it does...
208 while (soundBufferPos >= (SOUND_BUFFER_SIZE - 1))
210 //WriteLog("WriteSampleToBuffer(): Waiting for sound thread. soundBufferPos=%i, SOUNDBUFFERSIZE-1=%i\n", soundBufferPos, SOUND_BUFFER_SIZE-1);
211 SDL_mutexV(mutex2); // Release it so sound thread can get it,
212 SDL_mutexP(mutex); // Must lock the mutex for the cond to work properly...
213 SDL_CondWait(conditional, mutex); // Sleep/wait for the sound thread
214 SDL_mutexV(mutex); // Must unlock the mutex for the cond to work properly...
215 SDL_mutexP(mutex2); // Re-lock it until we're done with it...
218 soundBuffer[soundBufferPos++] = sample;
219 //WriteLog("WriteSampleToBuffer(): SDL_mutexV(mutex2)\n");
224 void ToggleSpeaker(void)
226 if (!soundInitialized)
229 speakerState = !speakerState;
230 sample = (speakerState ? amplitude[ampPtr] : -amplitude[ampPtr]);
236 // Currently set for 16-bit samples
242 void VolumeDown(void)
249 uint8_t GetVolume(void)