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 = 44100;
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->thread = threadContext;
45 context->samples = context->obtainedSpec.samples;
46 float ratio = GBAAudioCalculateRatio(0x8000, threadContext->fpsTarget, 44100);
47 threadContext->audioBuffers = context->samples / ratio;
48 if (context->samples > threadContext->audioBuffers) {
49 threadContext->audioBuffers = context->samples * 2;
50 }
51
52#if SDL_VERSION_ATLEAST(2, 0, 0)
53 SDL_PauseAudioDevice(context->deviceId, 0);
54#else
55 SDL_PauseAudio(0);
56#endif
57 return true;
58}
59
60void GBASDLDeinitAudio(struct GBASDLAudio* context) {
61 UNUSED(context);
62#if SDL_VERSION_ATLEAST(2, 0, 0)
63 SDL_PauseAudioDevice(context->deviceId, 1);
64 SDL_CloseAudioDevice(context->deviceId);
65#else
66 SDL_PauseAudio(1);
67 SDL_CloseAudio();
68#endif
69 SDL_QuitSubSystem(SDL_INIT_AUDIO);
70}
71
72void GBASDLPauseAudio(struct GBASDLAudio* context) {
73#if SDL_VERSION_ATLEAST(2, 0, 0)
74 SDL_PauseAudioDevice(context->deviceId, 1);
75#else
76 UNUSED(context);
77 SDL_PauseAudio(1);
78#endif
79}
80
81void GBASDLResumeAudio(struct GBASDLAudio* context) {
82#if SDL_VERSION_ATLEAST(2, 0, 0)
83 SDL_PauseAudioDevice(context->deviceId, 0);
84#else
85 UNUSED(context);
86 SDL_PauseAudio(0);
87#endif
88}
89
90static void _GBASDLAudioCallback(void* context, Uint8* data, int len) {
91 struct GBASDLAudio* audioContext = context;
92 if (!context || !audioContext->thread || !audioContext->thread->gba) {
93 memset(data, 0, len);
94 return;
95 }
96#if RESAMPLE_LIBRARY == RESAMPLE_NN
97 audioContext->ratio = GBAAudioCalculateRatio(audioContext->thread->gba->audio.sampleRate, audioContext->thread->fpsTarget, audioContext->obtainedSpec.freq);
98 if (audioContext->ratio == INFINITY) {
99 memset(data, 0, len);
100 return;
101 }
102 struct GBAStereoSample* ssamples = (struct GBAStereoSample*) data;
103 len /= 2 * audioContext->obtainedSpec.channels;
104 if (audioContext->obtainedSpec.channels == 2) {
105 GBAAudioResampleNN(&audioContext->thread->gba->audio, audioContext->ratio, &audioContext->drift, ssamples, len);
106 }
107#elif RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
108 double fauxClock = GBAAudioCalculateRatio(1, audioContext->thread->fpsTarget, 1);
109 GBASyncLockAudio(&audioContext->thread->sync);
110 blip_set_rates(audioContext->thread->gba->audio.left, GBA_ARM7TDMI_FREQUENCY, audioContext->obtainedSpec.freq * fauxClock);
111 blip_set_rates(audioContext->thread->gba->audio.right, GBA_ARM7TDMI_FREQUENCY, audioContext->obtainedSpec.freq * fauxClock);
112 len /= 2 * audioContext->obtainedSpec.channels;
113 int available = blip_samples_avail(audioContext->thread->gba->audio.left);
114 if (available > len) {
115 available = len;
116 }
117 blip_read_samples(audioContext->thread->gba->audio.left, (short*) data, available, audioContext->obtainedSpec.channels == 2);
118 if (audioContext->obtainedSpec.channels == 2) {
119 blip_read_samples(audioContext->thread->gba->audio.right, ((short*) data) + 1, available, 1);
120 }
121 GBASyncConsumeAudio(&audioContext->thread->sync);
122 if (available < len) {
123 memset(((short*) data) + audioContext->obtainedSpec.channels * available, 0, (len - available) * audioContext->obtainedSpec.channels * sizeof(short));
124 }
125#endif
126}