bit of cleaning
[drnoksnes] / platform / sdla.cpp
1 #include <SDL.h>
2
3 #include "platform.h"
4 #include "snes9x.h"
5 #include "soundux.h"
6
7 #define DIE(format, ...) do { \
8                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
9                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
10                 abort(); \
11         } while (0);
12
13
14 static SDL_AudioSpec spec;
15
16 static void audioCallback(void *userdata, Uint8 *stream, int len)
17 {
18         int samplecount = len / 2; // 16 bit audio
19         S9xMixSamples((short*)stream, samplecount);
20 }
21
22 void S9xInitAudioOutput()
23 {
24         if (!Config.enableAudio) goto no_audio;
25
26         if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) 
27                 DIE("SDL_InitSubSystem(AUDIO): %s", SDL_GetError());
28
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;
35         desired.userdata = 0;
36
37         if (SDL_OpenAudio(&desired, &spec) < 0) {
38                 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
39                 goto no_audio_free;
40         }
41
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");
49                 goto no_audio_free;
50         } else {
51                 fprintf(stderr, "Invalid audio format\n");
52                 goto no_audio_free;
53         }
54         Settings.Stereo = spec.channels == 2 ? TRUE : FALSE;
55
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",
59                 spec.samples);
60
61         return;
62
63 no_audio_free:
64         SDL_QuitSubSystem(SDL_INIT_AUDIO);
65         /* Fall through */
66 no_audio:
67         Config.enableAudio = false;
68         Settings.APUEnabled = FALSE;
69         printf("Audio: no audio\n");
70
71         return;
72 }
73
74 void S9xDeinitAudioOutput()
75 {
76         if (!Config.enableAudio) return;
77         SDL_CloseAudio();
78         SDL_QuitSubSystem(SDL_INIT_AUDIO);
79 }
80
81 void S9xAudioOutputEnable(bool enable)
82 {
83         if (!Config.enableAudio) return;
84         if (enable)     {
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);
91         } else {
92                 S9xSetSoundMute(TRUE);
93                 SDL_PauseAudio(TRUE);
94         }
95 }
96