src/platform/sdl/sdl-audio.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "sdl-audio.h"
7
8#include "gba/gba.h"
9#include "gba/supervisor/thread.h"
10
11#include "third-party/blip_buf/blip_buf.h"
12
13#define BUFFER_SIZE (GBA_AUDIO_SAMPLES >> 2)
14
15static void _GBSDLAudioCallback(void* context, Uint8* data, int len);
16
17bool GBSDLInitAudio(struct GBSDLAudio* context, struct GBAThread* threadContext) {
18 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
19 GBALog(0, GBA_LOG_ERROR, "Could not initialize SDL sound system: %s", SDL_GetError());
20 return false;
21 }
22
23 context->desiredSpec.freq = context->sampleRate;
24 context->desiredSpec.format = AUDIO_S16SYS;
25 context->desiredSpec.channels = 2;
26 context->desiredSpec.samples = context->samples;
27 context->desiredSpec.callback = _GBSDLAudioCallback;
28 context->desiredSpec.userdata = context;
29
30#if SDL_VERSION_ATLEAST(2, 0, 0)
31 context->deviceId = SDL_OpenAudioDevice(0, 0, &context->desiredSpec, &context->obtainedSpec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
32 if (context->deviceId == 0) {
33#else
34 if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) {
35#endif
36 GBALog(0, GBA_LOG_ERROR, "Could not open SDL sound system");
37 return false;
38 }
39 context->samples = context->obtainedSpec.samples;
40 context->psg = 0;
41 context->thread = 0;
42
43 if (threadContext) {
44 context->thread = threadContext;
45 float ratio = GBAAudioCalculateRatio(0x8000, threadContext->fpsTarget, 44100);
46 threadContext->audioBuffers = context->samples / ratio;
47 if (context->samples > threadContext->audioBuffers) {
48 threadContext->audioBuffers = context->samples * 2;
49 }
50
51#if SDL_VERSION_ATLEAST(2, 0, 0)
52 SDL_PauseAudioDevice(context->deviceId, 0);
53#else
54 SDL_PauseAudio(0);
55#endif
56 }
57
58 return true;
59}
60
61void GBSDLDeinitAudio(struct GBSDLAudio* context) {
62 UNUSED(context);
63 context->psg = 0;
64#if SDL_VERSION_ATLEAST(2, 0, 0)
65 SDL_PauseAudioDevice(context->deviceId, 1);
66 SDL_CloseAudioDevice(context->deviceId);
67#else
68 SDL_PauseAudio(1);
69 SDL_CloseAudio();
70#endif
71 SDL_QuitSubSystem(SDL_INIT_AUDIO);
72}
73
74void GBSDLPauseAudio(struct GBSDLAudio* context) {
75#if SDL_VERSION_ATLEAST(2, 0, 0)
76 SDL_PauseAudioDevice(context->deviceId, 1);
77#else
78 UNUSED(context);
79 SDL_PauseAudio(1);
80#endif
81}
82
83void GBSDLResumeAudio(struct GBSDLAudio* context) {
84#if SDL_VERSION_ATLEAST(2, 0, 0)
85 SDL_PauseAudioDevice(context->deviceId, 0);
86#else
87 UNUSED(context);
88 SDL_PauseAudio(0);
89#endif
90}
91
92static void _GBSDLAudioCallback(void* context, Uint8* data, int len) {
93 struct GBSDLAudio* audioContext = context;
94 if (!context || (!audioContext->psg && !audioContext->thread)) {
95 memset(data, 0, len);
96 return;
97 }
98 struct GBAudio* psg = audioContext->psg;
99 if (!psg) {
100 psg = &audioContext->thread->gba->audio.psg;
101 }
102 double fauxClock = 1;
103 if (audioContext->thread) {
104 fauxClock = GBAAudioCalculateRatio(1, audioContext->thread->fpsTarget, 1);
105 mCoreSyncLockAudio(&audioContext->thread->sync);
106 }
107 blip_set_rates(psg->left, psg->clockRate, audioContext->obtainedSpec.freq * fauxClock);
108 blip_set_rates(psg->right, psg->clockRate, audioContext->obtainedSpec.freq * fauxClock);
109 len /= 2 * audioContext->obtainedSpec.channels;
110 int available = blip_samples_avail(psg->left);
111 if (available > len) {
112 available = len;
113 }
114 blip_read_samples(psg->left, (short*) data, available, audioContext->obtainedSpec.channels == 2);
115 if (audioContext->obtainedSpec.channels == 2) {
116 blip_read_samples(psg->right, ((short*) data) + 1, available, 1);
117 }
118
119 if (audioContext->thread) {
120 mCoreSyncConsumeAudio(&audioContext->thread->sync);
121 }
122 if (available < len) {
123 memset(((short*) data) + audioContext->obtainedSpec.channels * available, 0, (len - available) * audioContext->obtainedSpec.channels * sizeof(short));
124 }
125}