7 #define DIE(format, ...) do { \
8 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
9 fprintf(stderr, format "\n", ## __VA_ARGS__); \
14 static SDL_AudioSpec spec;
16 static void audioCallback(void *userdata, Uint8 *stream, int len)
18 int samplecount = len / 2;
19 S9xMixSamples((short*)stream, samplecount);
22 void S9xInitAudioOutput()
24 if (!Config.enableAudio) goto no_audio;
26 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
27 DIE("SDL_InitSubSystem(AUDIO): %s", SDL_GetError());
29 SDL_AudioSpec desired;
30 desired.freq = Settings.SoundPlaybackRate;
31 desired.format = AUDIO_S16LSB;
32 desired.channels = Settings.Stereo ? 2 : 1;
33 desired.samples = MAX_BUFFER_SIZE;
34 desired.callback = audioCallback;
37 if (SDL_OpenAudio(&desired, &spec) < 0) {
38 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
42 Settings.APUEnabled = TRUE;
43 Settings.SoundPlaybackRate = spec.freq;
44 if (spec.format == AUDIO_S16LSB)
45 Settings.SixteenBitSound = TRUE;
46 else if (spec.format == AUDIO_S8)
47 Settings.SixteenBitSound = FALSE;
49 fprintf(stderr, "Invalid audio format\n");
52 Settings.Stereo = spec.channels == 2 ? TRUE : FALSE;
54 printf("Audio: %d Hz, %d %s, %s\n", spec.freq,
55 spec.channels, Settings.Stereo ? "channels" : "channel",
56 Settings.SixteenBitSound ? "16 bits" : "8 bits");
61 SDL_QuitSubSystem(SDL_INIT_AUDIO);
64 Config.enableAudio = false;
65 Settings.APUEnabled = FALSE;
66 printf("Audio: no audio\n");
71 void S9xDeinitAudioOutput()
73 if (!Config.enableAudio) return;
75 SDL_QuitSubSystem(SDL_INIT_AUDIO);
78 void S9xAudioOutputEnable(bool enable)
80 if (!Config.enableAudio) return;
82 CPU.APU_APUExecuting = Settings.APUEnabled = TRUE;
83 so.stereo = Settings.Stereo;
84 so.playback_rate = Settings.SoundPlaybackRate;
85 S9xSetPlaybackRate(so.playback_rate);
86 S9xSetSoundMute(FALSE);
87 SDL_PauseAudio(FALSE);
89 S9xSetSoundMute(TRUE);