all repos — mgba @ d09d0e505fc69bf2255360f0c6bb0ab9931fc9fe

mGBA Game Boy Advance Emulator

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");
 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}
 81
 82void GBASDLResumeAudio(struct GBASDLAudio* context) {
 83#if SDL_VERSION_ATLEAST(2, 0, 0)
 84	SDL_PauseAudioDevice(context->deviceId, 0);
 85#else
 86	UNUSED(context);
 87	SDL_PauseAudio(0);
 88#endif
 89}
 90
 91static void _GBASDLAudioCallback(void* context, Uint8* data, int len) {
 92	struct GBASDLAudio* audioContext = context;
 93	if (!context || !audioContext->thread || !audioContext->thread->gba) {
 94		memset(data, 0, len);
 95		return;
 96	}
 97#if RESAMPLE_LIBRARY == RESAMPLE_NN
 98	audioContext->ratio = GBAAudioCalculateRatio(audioContext->thread->gba->audio.sampleRate, audioContext->thread->fpsTarget, audioContext->obtainedSpec.freq);
 99	if (audioContext->ratio == INFINITY) {
100		memset(data, 0, len);
101		return;
102	}
103	struct GBAStereoSample* ssamples = (struct GBAStereoSample*) data;
104	len /= 2 * audioContext->obtainedSpec.channels;
105	if (audioContext->obtainedSpec.channels == 2) {
106		GBAAudioResampleNN(&audioContext->thread->gba->audio, audioContext->ratio, &audioContext->drift, ssamples, len);
107	}
108#elif RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
109	double fauxClock = GBAAudioCalculateRatio(1, audioContext->thread->fpsTarget, 1);
110	GBASyncLockAudio(&audioContext->thread->sync);
111	blip_set_rates(audioContext->thread->gba->audio.left, GBA_ARM7TDMI_FREQUENCY, audioContext->obtainedSpec.freq * fauxClock);
112	blip_set_rates(audioContext->thread->gba->audio.right, GBA_ARM7TDMI_FREQUENCY, audioContext->obtainedSpec.freq * fauxClock);
113	len /= 2 * audioContext->obtainedSpec.channels;
114	int available = blip_samples_avail(audioContext->thread->gba->audio.left);
115	if (available > len) {
116		available = len;
117	}
118	blip_read_samples(audioContext->thread->gba->audio.left, (short*) data, available, audioContext->obtainedSpec.channels == 2);
119	if (audioContext->obtainedSpec.channels == 2) {
120		blip_read_samples(audioContext->thread->gba->audio.right, ((short*) data) + 1, available, 1);
121	}
122	GBASyncConsumeAudio(&audioContext->thread->sync);
123	if (available < len) {
124		memset(((short*) data) + audioContext->obtainedSpec.channels * available, 0, (len - available) * audioContext->obtainedSpec.channels * sizeof(short));
125	}
126#endif
127}