41 static SDL_AudioDeviceID audio = 0;
42 static int stereo_separation_orig = 100;
43 static int stereo_separation_other = 0;
44 static int system_sound_mix_freq;
45 static int system_sid_cycles_per_sec;
46 static double dma_audio_mixing_value;
49 #if NUMBER_OF_SIDS != 4
50 # error "Currently NUMBER_OF_SIDS macro must be set to 4!"
56 #define DEBUG_AUDIO_LOCKS(...)
60 static SDL_SpinLock opl3_lock;
64 SDL_AtomicLock(&opl3_lock);
67 static XEMU_INLINE void UNLOCK_OPL (
const char *m )
69 SDL_AtomicUnlock(&opl3_lock);
74 # define UNLOCK_OPL(m)
75 # warning "Disabled LOCK for OPL3, you may experience Xemu crashes and/or sound anomalies!"
80 static XEMU_INLINE void LOCK_SID (
const char *m,
const int i )
86 static XEMU_INLINE void UNLOCK_SID (
const char *m,
const int i )
92 # define LOCK_SID(m,i)
93 # define UNLOCK_SID(m,i)
94 # warning "Disabled LOCK for SID, you may experience Xemu crashes and/or sound anomalies!"
116 UNLOCK_OPL(
"WRITER");
123 LOCK_SID(
"INCER", i);
125 UNLOCK_SID(
"INCER", i);
130 #ifdef AUDIO_EMULATION
131 static inline void render_dma_audio (
int channel, Sint16 *buffer,
int len )
133 static Sint16 sample[4];
134 static double rate_counter[4] = {0,0,0,0};
135 Uint8 *chio =
D7XX + 0x20 + channel * 0x10;
136 unsigned int addr = chio[0xA] + (chio[0xB] << 8) + (chio[0xC] << 16);
137 const Uint16 limit = chio[0x7] + (chio[0x8] << 8);
138 const double rate_step =
139 (double)(chio[4] + (chio[5] << 8) + (chio[6] << 16))
141 dma_audio_mixing_value;
145 const Uint16 sample_signedness_conversion = (chio[0] & 0x20) << 10;
147 if (sample_signedness_conversion)
150 for (
unsigned int i = 0; i < len; i++) {
151 if (!(chio[0] & 0x80) || (chio[0] & 0x08)) {
154 rate_counter[channel] = 0;
158 rate_counter[channel] += rate_step;
162 while (rate_counter[channel] >= 0x1000000) {
163 rate_counter[channel] -= 0x1000000;
166 addr += ((chio[0] & 3) == 3) ? 2 : 1;
169 switch (chio[0] & 3) {
190 const Sint16 signed_sample = (
Uint16)(unsigned_read ^ sample_signedness_conversion);
192 sample[channel] = ((int)signed_sample * chio[9]) / 0xFF;
196 if ((chio[0] & 0x40)) {
197 addr = chio[1] + (chio[2] << 8) + (chio[3] << 16);
201 rate_counter[channel] = 0;
207 buffer[i] = sample[channel];
211 chio[0xA] =
addr & 0xFF;
212 chio[0xB] = (
addr >> 8) & 0xFF;
213 chio[0xC] = (
addr >> 16) & 0xFF;
238 sep = (sep + 100) / 2;
240 stereo_separation_orig = (sep * vol) / 100;
241 stereo_separation_other = ((100 - sep) * vol) / 100;
247 #define MIXED_CHANNELS 10
249 #ifdef CORRUPTION_DEBUG
250 # define EXTRA_STREAM_CHANNELS 99
252 # define EXTRA_STREAM_CHANNELS 0
255 #define STREAMS_SIZE_ALL (((MIXED_CHANNELS) + (EXTRA_STREAM_CHANNELS)) * (AUDIO_BUFFER_SAMPLES_MAX))
256 #define STREAMS(n) (streams + ((n) * (AUDIO_BUFFER_SAMPLES_MAX)))
257 #define STREAMS_SAMPLE(n,d) ((int)(STREAMS(n)[d]))
260 static void audio_callback (
void *userdata,
Uint8 *stereo_out_stream,
int len )
263 static int nosound_previous = -1;
271 memset(stereo_out_stream, 0, len);
282 for (
int i = 0; i < 4; i++)
283 render_dma_audio(i,
STREAMS(i), len);
287 memset(
STREAMS(4 + i), 0, len *
sizeof(Sint16));
290 LOCK_SID(
"RENDER", i);
292 UNLOCK_SID(
"RENDER", i);
298 UNLOCK_OPL(
"RENDER");
300 memset(
STREAMS(8), 0, len *
sizeof(Sint16));
301 memset(
STREAMS(9), 0, len *
sizeof(Sint16));
304 for (
int i = 0, j = 0; i < len; i++) {
311 int left = ((orig_left * stereo_separation_orig) / 100) + ((orig_right * stereo_separation_other) / 100);
312 int right = ((orig_right * stereo_separation_orig) / 100) + ((orig_left * stereo_separation_other) / 100);
314 int left = orig_left;
315 int right = orig_right;
318 if (left > 0x7FFF) left = 0x7FFF;
319 else if (left < -0x8000) left = -0x8000;
320 if (right > 0x7FFF) right = 0x7FFF;
321 else if (right < -0x8000) right = -0x8000;
323 ((Sint16*)stereo_out_stream)[j++] = left;
324 ((Sint16*)stereo_out_stream)[j++] = right;
326 #ifdef CORRUPTION_DEBUG
327 # warning "You have CORRUPTION_DEBUG enabled"
341 for (
int i = 0; i < 0x100; i++)
345 memset(
D7XX + 0x20, 0, 0x40);
346 DEBUGPRINT(
"AUDIO: clearing audio related registers." NL);
355 LOCK_SID(
"RESET", i);
356 sid_init(&
sid[i], system_sid_cycles_per_sec, system_sound_mix_freq);
357 UNLOCK_SID(
"RESET", i);
362 DEBUGPRINT(
"AUDIO: reset for %d SIDs (%d cycles per sec) and 1 OPL3 chip for %dHz sampling rate." NL,
NUMBER_OF_SIDS, system_sid_cycles_per_sec, system_sound_mix_freq);
369 static volatile int started = 0;
371 ERROR_WINDOW(
"Trying to restart audio??\nRefuseing to do so!!");
378 SDL_PauseAudioDevice(audio, 0);
382 void audio65_init (
int sid_cycles_per_sec,
int sound_mix_freq,
int volume,
int separation,
unsigned int buffer_size )
384 static volatile int initialized = 0;
386 ERROR_WINDOW(
"Trying to reinitialize audio??\nRefusing to do so!!");
391 UNLOCK_SID(
"INIT", i);
393 system_sound_mix_freq = sound_mix_freq;
394 system_sid_cycles_per_sec = sid_cycles_per_sec;
396 #ifdef AUDIO_EMULATION
397 dma_audio_mixing_value = (double)40500000.0 / (
double)sound_mix_freq;
398 SDL_AudioSpec audio_want, audio_got;
399 SDL_memset(&audio_want, 0,
sizeof(audio_want));
400 audio_want.freq = sound_mix_freq;
401 audio_want.format = AUDIO_S16SYS;
402 audio_want.channels = 2;
403 audio_want.samples = buffer_size;
404 audio_want.callback = audio_callback;
405 audio_want.userdata = NULL;
407 ERROR_WINDOW(
"audio was not zero before calling SDL_OpenAudioDevice!");
408 audio = SDL_OpenAudioDevice(NULL, 0, &audio_want, &audio_got, 0);
410 for (
int i = 0; i < SDL_GetNumAudioDevices(0); i++)
411 DEBUG(
"AUDIO: audio device is #%d: %s" NL, i, SDL_GetAudioDeviceName(i, 0));
413 if (audio_want.freq != audio_got.freq || audio_want.format != audio_got.format || audio_want.channels != audio_got.channels || audio_got.samples >
AUDIO_BUFFER_SAMPLES_MAX) {
414 SDL_CloseAudioDevice(audio);
418 DEBUGPRINT(
"AUDIO: initialized (#%d), %d Hz, %d channels, %d buffer sample size." NL, audio, audio_got.freq, audio_got.channels, audio_got.samples);
426 DEBUGPRINT(
"AUDIO: has been disabled at compilation time." NL);