all repos — mgba @ 33a4c45f3ff72c83a3f68b5f4e5730764018a743

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#include "third-party/blip_buf/blip_buf.h"
 12
 13#define BUFFER_SIZE (GBA_AUDIO_SAMPLES >> 2)
 14
 15static void _mSDLAudioCallback(void* context, Uint8* data, int len);
 16
 17bool mSDLInitAudio(struct mSDLAudio* 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 = _mSDLAudioCallback;
 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->core = 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		context->sync = &threadContext->sync;
 51
 52#if SDL_VERSION_ATLEAST(2, 0, 0)
 53		SDL_PauseAudioDevice(context->deviceId, 0);
 54#else
 55		SDL_PauseAudio(0);
 56#endif
 57	}
 58
 59	return true;
 60}
 61
 62void mSDLDeinitAudio(struct mSDLAudio* context) {
 63	UNUSED(context);
 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 mSDLPauseAudio(struct mSDLAudio* 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 mSDLResumeAudio(struct mSDLAudio* 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 _mSDLAudioCallback(void* context, Uint8* data, int len) {
 93	struct mSDLAudio* audioContext = context;
 94	if (!context || (!audioContext->core && !audioContext->thread)) {
 95		memset(data, 0, len);
 96		return;
 97	}
 98	blip_t* left = NULL;
 99	blip_t* right = NULL;
100	int32_t clockRate;
101	if (audioContext->core) {
102		left = audioContext->core->getAudioChannel(audioContext->core, 0);
103		right = audioContext->core->getAudioChannel(audioContext->core, 1);
104		clockRate = audioContext->core->frequency(audioContext->core);
105	} else if (audioContext->thread) {
106		left = audioContext->thread->gba->audio.psg.left;
107		right = audioContext->thread->gba->audio.psg.right;
108		clockRate = GBA_ARM7TDMI_FREQUENCY;
109	}
110	double fauxClock = 1;
111	if (audioContext->thread) {
112		fauxClock = GBAAudioCalculateRatio(1, audioContext->thread->fpsTarget, 1);
113		mCoreSyncLockAudio(&audioContext->thread->sync);
114	}
115	blip_set_rates(left, clockRate, audioContext->obtainedSpec.freq * fauxClock);
116	blip_set_rates(right, clockRate, audioContext->obtainedSpec.freq * fauxClock);
117	len /= 2 * audioContext->obtainedSpec.channels;
118	int available = blip_samples_avail(left);
119	if (available > len) {
120		available = len;
121	}
122	blip_read_samples(left, (short*) data, available, audioContext->obtainedSpec.channels == 2);
123	if (audioContext->obtainedSpec.channels == 2) {
124		blip_read_samples(right, ((short*) data) + 1, available, 1);
125	}
126
127	if (audioContext->sync) {
128		mCoreSyncConsumeAudio(audioContext->sync);
129	}
130	if (available < len) {
131		memset(((short*) data) + audioContext->obtainedSpec.channels * available, 0, (len - available) * audioContext->obtainedSpec.channels * sizeof(short));
132	}
133}