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#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
12#include "third-party/blip_buf/blip_buf.h"
13#endif
14
15#define BUFFER_SIZE (GBA_AUDIO_SAMPLES >> 2)
16
17static void _GBASDLAudioCallback(void* context, Uint8* data, int len);
18
19bool GBASDLInitAudio(struct GBASDLAudio* context, struct GBAThread* threadContext) {
20 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
21 GBALog(0, GBA_LOG_ERROR, "Could not initialize SDL sound system: %s", SDL_GetError());
22 return false;
23 }
24
25 context->desiredSpec.freq = context->sampleRate;
26 context->desiredSpec.format = AUDIO_S16SYS;
27 context->desiredSpec.channels = 2;
28 context->desiredSpec.samples = context->samples;
29 context->desiredSpec.callback = _GBASDLAudioCallback;
30 context->desiredSpec.userdata = context;
31#if RESAMPLE_LIBRARY == RESAMPLE_NN
32 context->drift = 0.f;
33#endif
34
35#if SDL_VERSION_ATLEAST(2, 0, 0)
36 context->deviceId = SDL_OpenAudioDevice(0, 0, &context->desiredSpec, &context->obtainedSpec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
37 if (context->deviceId == 0) {
38#else
39 if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) {
40#endif
41 GBALog(0, GBA_LOG_ERROR, "Could not open SDL sound system");
42 return false;
43 }
44 context->samples = context->obtainedSpec.samples;
45
46 if (threadContext) {
47 context->thread = threadContext;
48 float ratio = GBAAudioCalculateRatio(0x8000, threadContext->fpsTarget, 44100);
49 threadContext->audioBuffers = context->samples / ratio;
50 if (context->samples > threadContext->audioBuffers) {
51 threadContext->audioBuffers = context->samples * 2;
52 }
53
54#if SDL_VERSION_ATLEAST(2, 0, 0)
55 SDL_PauseAudioDevice(context->deviceId, 0);
56#else
57 SDL_PauseAudio(0);
58#endif
59 }
60
61 return true;
62}
63
64void GBASDLDeinitAudio(struct GBASDLAudio* context) {
65 UNUSED(context);
66#if SDL_VERSION_ATLEAST(2, 0, 0)
67 SDL_PauseAudioDevice(context->deviceId, 1);
68 SDL_CloseAudioDevice(context->deviceId);
69#else
70 SDL_PauseAudio(1);
71 SDL_CloseAudio();
72#endif
73 SDL_QuitSubSystem(SDL_INIT_AUDIO);
74}
75
76void GBASDLPauseAudio(struct GBASDLAudio* context) {
77#if SDL_VERSION_ATLEAST(2, 0, 0)
78 SDL_PauseAudioDevice(context->deviceId, 1);
79#else
80 UNUSED(context);
81 SDL_PauseAudio(1);
82#endif
83}
84
85void GBASDLResumeAudio(struct GBASDLAudio* context) {
86#if SDL_VERSION_ATLEAST(2, 0, 0)
87 SDL_PauseAudioDevice(context->deviceId, 0);
88#else
89 UNUSED(context);
90 SDL_PauseAudio(0);
91#endif
92}
93
94static void _GBASDLAudioCallback(void* context, Uint8* data, int len) {
95 struct GBASDLAudio* audioContext = context;
96 if (!context || (!audioContext->gba && (!audioContext->thread || !audioContext->thread->gba))) {
97 memset(data, 0, len);
98 return;
99 }
100 if (!audioContext->gba) {
101 audioContext->gba = audioContext->thread->gba;
102 }
103#if RESAMPLE_LIBRARY == RESAMPLE_NN
104 audioContext->ratio = GBAAudioCalculateRatio(audioContext->gba->audio.sampleRate, audioContext->fpsTarget, audioContext->obtainedSpec.freq);
105 if (audioContext->ratio == INFINITY) {
106 memset(data, 0, len);
107 return;
108 }
109 struct GBAStereoSample* ssamples = (struct GBAStereoSample*) data;
110 len /= 2 * audioContext->obtainedSpec.channels;
111 if (audioContext->obtainedSpec.channels == 2) {
112 GBAAudioResampleNN(&audioContext->gba->audio, audioContext->ratio, &audioContext->drift, ssamples, len);
113 }
114#elif RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
115 double fauxClock = 1;
116 if (audioContext->thread) {
117 GBAAudioCalculateRatio(1, audioContext->thread->fpsTarget, 1);
118 GBASyncLockAudio(&audioContext->thread->sync);
119 }
120 blip_set_rates(audioContext->gba->audio.left, GBA_ARM7TDMI_FREQUENCY, audioContext->obtainedSpec.freq * fauxClock);
121 blip_set_rates(audioContext->gba->audio.right, GBA_ARM7TDMI_FREQUENCY, audioContext->obtainedSpec.freq * fauxClock);
122 len /= 2 * audioContext->obtainedSpec.channels;
123 int available = blip_samples_avail(audioContext->gba->audio.left);
124 if (available > len) {
125 available = len;
126 }
127 blip_read_samples(audioContext->gba->audio.left, (short*) data, available, audioContext->obtainedSpec.channels == 2);
128 if (audioContext->obtainedSpec.channels == 2) {
129 blip_read_samples(audioContext->gba->audio.right, ((short*) data) + 1, available, 1);
130 }
131
132 if (audioContext->thread) {
133 GBASyncConsumeAudio(&audioContext->thread->sync);
134 }
135 if (available < len) {
136 memset(((short*) data) + audioContext->obtainedSpec.channels * available, 0, (len - available) * audioContext->obtainedSpec.channels * sizeof(short));
137 }
138#endif
139}