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; // 16 bit audio
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 = Settings.SoundBufferSize;
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;
48 fprintf(stderr, "Cannot output 8 bit sound\n");
51 fprintf(stderr, "Invalid audio format\n");
54 Settings.Stereo = spec.channels == 2 ? TRUE : FALSE;
56 printf("Audio: %d Hz, %d %s, %s, %u samples in buffer\n", spec.freq,
57 spec.channels, Settings.Stereo ? "channels" : "channel",
58 Settings.SixteenBitSound ? "16 bits" : "8 bits",
64 SDL_QuitSubSystem(SDL_INIT_AUDIO);
67 Config.enableAudio = false;
68 Settings.APUEnabled = FALSE;
69 printf("Audio: no audio\n");
74 void S9xDeinitAudioOutput()
76 if (!Config.enableAudio) return;
78 SDL_QuitSubSystem(SDL_INIT_AUDIO);
81 void S9xAudioOutputEnable(bool enable)
83 if (!Config.enableAudio) return;
85 CPU.APU_APUExecuting = Settings.APUEnabled = TRUE;
86 so.stereo = Settings.Stereo;
87 so.playback_rate = Settings.SoundPlaybackRate;
88 S9xSetPlaybackRate(so.playback_rate);
89 S9xSetSoundMute(FALSE);
90 SDL_PauseAudio(FALSE);
92 S9xSetSoundMute(TRUE);