From 0ab9190b1051f8f6c8b3449ca9a0ca066851a7a3 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 30 Aug 2015 10:04:04 -0700 Subject: [PATCH] 3DS: CSND first implementation --- src/platform/3ds/main.c | 45 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/platform/3ds/main.c b/src/platform/3ds/main.c index 7de8b540d..e1246a709 100644 --- a/src/platform/3ds/main.c +++ b/src/platform/3ds/main.c @@ -17,6 +17,8 @@ #include <3ds.h> #include +#define AUDIO_SAMPLES 0x800 + FS_archive sdmcArchive; struct GBA3DSRotationSource { @@ -88,6 +90,8 @@ static int32_t _readGyroZ(struct GBARotationSource* source) { } int main() { + bool hasSound = !csndInit(); + struct GBAContext context; struct GBA3DSRotationSource rotation; rotation.d.sample = _sampleRotation; @@ -99,6 +103,14 @@ int main() { return 1; } + int16_t* audioLeft = 0; + int16_t* audioRight = 0; + size_t audioPos = 0; + if (hasSound) { + audioLeft = linearAlloc(AUDIO_SAMPLES * 2 * sizeof(int16_t)); + audioRight = linearAlloc(AUDIO_SAMPLES * 2 * sizeof(int16_t)); + } + sf2d_init(); sf2d_set_clear_color(0); sf2d_texture* tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM); @@ -165,9 +177,11 @@ int main() { } #if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF - blip_set_rates(context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 48000); - blip_set_rates(context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000); + blip_set_rates(context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 0x8000); + blip_set_rates(context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 0x8000); #endif + memset(audioLeft, 0, AUDIO_SAMPLES * 2 * sizeof(int16_t)); + memset(audioRight, 0, AUDIO_SAMPLES * 2 * sizeof(int16_t)); while (aptMainLoop()) { hidScanInput(); @@ -179,8 +193,21 @@ int main() { GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202); GSPGPU_FlushDataCache(0, tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2); #if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF - blip_clear(context.gba->audio.left); - blip_clear(context.gba->audio.right); + if (hasSound) { + memset(&audioLeft[audioPos], 0, AUDIO_SAMPLES); + memset(&audioRight[audioPos], 0, AUDIO_SAMPLES); + size_t samples = blip_read_samples(context.gba->audio.left, &audioLeft[audioPos], AUDIO_SAMPLES, false); + blip_read_samples(context.gba->audio.right, &audioRight[audioPos], AUDIO_SAMPLES, false); + size_t audioPosNew = (audioPos + AUDIO_SAMPLES) % (AUDIO_SAMPLES * 2); + GSPGPU_FlushDataCache(0, (void*) audioLeft, AUDIO_SAMPLES * 2 * sizeof(int16_t)); + GSPGPU_FlushDataCache(0, (void*) audioRight, AUDIO_SAMPLES * 2 * sizeof(int16_t)); + csndPlaySound(0x8, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 0x8000, 1.0, -1.0, &audioLeft[audioPos], &audioLeft[audioPosNew], samples * 2); + csndPlaySound(0x9, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 0x8000, 1.0, 1.0, &audioRight[audioPos], &audioLeft[audioPosNew], samples * 2); + audioPos = audioPosNew; + } else { + blip_clear(context.gba->audio.left); + blip_clear(context.gba->audio.right); + } #endif gspWaitForPPF(); _drawStart(); @@ -189,6 +216,10 @@ int main() { } GBAContextStop(&context); + CSND_SetPlayState(8, 0); + CSND_SetPlayState(9, 0); + csndExecCmds(0); + if (context.gba->memory.hw.devices & HW_TILT) { HIDUSER_DisableAccelerometer(); } @@ -207,6 +238,12 @@ cleanup: sf2d_free_texture(tex); sf2d_fini(); + + if (hasSound) { + linearFree(audioLeft); + linearFree(audioRight); + } + csndExit(); return 0; }