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 <mgba/core/core.h>
9#include <mgba/core/thread.h>
10#include <mgba/internal/gba/audio.h>
11#include <mgba/internal/gba/gba.h>
12
13#include <mgba/core/blip_buf.h>
14
15#define BUFFER_SIZE (GBA_AUDIO_SAMPLES >> 2)
16
17mLOG_DEFINE_CATEGORY(SDL_AUDIO, "SDL Audio");
18
19static void _mSDLAudioCallback(void* context, Uint8* data, int len);
20
21bool mSDLInitAudio(struct mSDLAudio* context, struct mCoreThread* threadContext) {
22 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
23 mLOG(SDL_AUDIO, ERROR, "Could not initialize SDL sound system: %s", SDL_GetError());
24 return false;
25 }
26
27 context->desiredSpec.freq = context->sampleRate;
28 context->desiredSpec.format = AUDIO_S16SYS;
29 context->desiredSpec.channels = 2;
30 context->desiredSpec.samples = context->samples;
31 context->desiredSpec.callback = _mSDLAudioCallback;
32 context->desiredSpec.userdata = context;
33
34#if SDL_VERSION_ATLEAST(2, 0, 0)
35 context->deviceId = SDL_OpenAudioDevice(0, 0, &context->desiredSpec, &context->obtainedSpec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
36 if (context->deviceId == 0) {
37#else
38 if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) {
39#endif
40 mLOG(SDL_AUDIO, ERROR, "Could not open SDL sound system");
41 return false;
42 }
43 context->samples = context->obtainedSpec.samples;
44 context->core = 0;
45
46 if (threadContext) {
47 context->core = threadContext->core;
48 context->sync = &threadContext->sync;
49
50#if SDL_VERSION_ATLEAST(2, 0, 0)
51 SDL_PauseAudioDevice(context->deviceId, 0);
52#else
53 SDL_PauseAudio(0);
54#endif
55 }
56
57 return true;
58}
59
60void mSDLDeinitAudio(struct mSDLAudio* 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 mSDLPauseAudio(struct mSDLAudio* 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 mSDLResumeAudio(struct mSDLAudio* 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 _mSDLAudioCallback(void* context, Uint8* data, int len) {
91 struct mSDLAudio* audioContext = context;
92 if (!context || !audioContext->core) {
93 memset(data, 0, len);
94 return;
95 }
96 blip_t* left = NULL;
97 blip_t* right = NULL;
98 int32_t clockRate = GBA_ARM7TDMI_FREQUENCY;
99 if (audioContext->core) {
100 left = audioContext->core->getAudioChannel(audioContext->core, 0);
101 right = audioContext->core->getAudioChannel(audioContext->core, 1);
102 clockRate = audioContext->core->frequency(audioContext->core);
103 }
104 double fauxClock = 1;
105 if (audioContext->sync) {
106 if (audioContext->sync->fpsTarget > 0) {
107 fauxClock = GBAAudioCalculateRatio(1, audioContext->sync->fpsTarget, 1);
108 }
109 mCoreSyncLockAudio(audioContext->sync);
110 }
111 blip_set_rates(left, clockRate, audioContext->obtainedSpec.freq * fauxClock);
112 blip_set_rates(right, clockRate, audioContext->obtainedSpec.freq * fauxClock);
113 len /= 2 * audioContext->obtainedSpec.channels;
114 int available = blip_samples_avail(left);
115 if (available > len) {
116 available = len;
117 }
118 blip_read_samples(left, (short*) data, available, audioContext->obtainedSpec.channels == 2);
119 if (audioContext->obtainedSpec.channels == 2) {
120 blip_read_samples(right, ((short*) data) + 1, available, 1);
121 }
122
123 if (audioContext->sync) {
124 mCoreSyncConsumeAudio(audioContext->sync);
125 }
126 if (available < len) {
127 memset(((short*) data) + audioContext->obtainedSpec.channels * available, 0, (len - available) * audioContext->obtainedSpec.channels * sizeof(short));
128 }
129}