diff --git a/audio/ogg-vorbis-decoding/Makefile b/audio/ogg-vorbis-decoding/Makefile new file mode 100644 index 0000000..1c6ffd7 --- /dev/null +++ b/audio/ogg-vorbis-decoding/Makefile @@ -0,0 +1,229 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITARM)),) +$(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") +endif + +TOPDIR ?= $(CURDIR) +include $(DEVKITARM)/3ds_rules + +#--------------------------------------------------------------------------------- +# TARGET is the name of the output +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# DATA is a list of directories containing data files +# INCLUDES is a list of directories containing header files +# GRAPHICS is a list of directories containing graphics files +# GFXBUILD is the directory where converted graphics files will be placed +# If set to $(BUILD), it will statically link in the converted +# files as if they were data files. +# +# NO_SMDH: if set to anything, no SMDH file is generated. +# ROMFS is the directory which contains the RomFS, relative to the Makefile (Optional) +# APP_TITLE is the name of the app stored in the SMDH file (Optional) +# APP_DESCRIPTION is the description of the app stored in the SMDH file (Optional) +# APP_AUTHOR is the author of the app stored in the SMDH file (Optional) +# ICON is the filename of the icon (.png), relative to the project folder. +# If not set, it attempts to use one of the following (in this order): +# - .png +# - icon.png +# - /default_icon.png +#--------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build +SOURCES := source +DATA := data +INCLUDES := include +GRAPHICS := gfx +GFXBUILD := $(BUILD) +ROMFS := romfs +#GFXBUILD := $(ROMFS)/gfx + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft + +CFLAGS := -g -Wall -O2 -mword-relocations \ + -ffunction-sections \ + $(ARCH) + +CFLAGS += $(INCLUDE) -D__3DS__ `$(PREFIX)pkg-config vorbisidec --cflags` + +CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 + +ASFLAGS := -g $(ARCH) +LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) + +LIBS := -lctru -lm `$(PREFIX)pkg-config vorbisidec --libs` + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(PORTLIBS) $(CTRULIB) + + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export OUTPUT := $(CURDIR)/$(TARGET) +export TOPDIR := $(CURDIR) + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + +export DEPSDIR := $(CURDIR)/$(BUILD) + +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +PICAFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.v.pica))) +SHLISTFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.shlist))) +GFXFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.t3s))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) + +#--------------------------------------------------------------------------------- +# use CXX for linking C++ projects, CC for standard C +#--------------------------------------------------------------------------------- +ifeq ($(strip $(CPPFILES)),) +#--------------------------------------------------------------------------------- + export LD := $(CC) +#--------------------------------------------------------------------------------- +else +#--------------------------------------------------------------------------------- + export LD := $(CXX) +#--------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------- + +#--------------------------------------------------------------------------------- +ifeq ($(GFXBUILD),$(BUILD)) +#--------------------------------------------------------------------------------- +export T3XFILES := $(GFXFILES:.t3s=.t3x) +#--------------------------------------------------------------------------------- +else +#--------------------------------------------------------------------------------- +export ROMFS_T3XFILES := $(patsubst %.t3s, $(GFXBUILD)/%.t3x, $(GFXFILES)) +export T3XHFILES := $(patsubst %.t3s, $(BUILD)/%.h, $(GFXFILES)) +#--------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------- + +export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) + +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) \ + $(PICAFILES:.v.pica=.shbin.o) $(SHLISTFILES:.shlist=.shbin.o) \ + $(addsuffix .o,$(T3XFILES)) + +export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) + +export HFILES := $(PICAFILES:.v.pica=_shbin.h) $(SHLISTFILES:.shlist=_shbin.h) \ + $(addsuffix .h,$(subst .,_,$(BINFILES))) \ + $(GFXFILES:.t3s=.h) + +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I$(CURDIR)/$(BUILD) + +export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) + +export _3DSXDEPS := $(if $(NO_SMDH),,$(OUTPUT).smdh) + +ifeq ($(strip $(ICON)),) + icons := $(wildcard *.png) + ifneq (,$(findstring $(TARGET).png,$(icons))) + export APP_ICON := $(TOPDIR)/$(TARGET).png + else + ifneq (,$(findstring icon.png,$(icons))) + export APP_ICON := $(TOPDIR)/icon.png + endif + endif +else + export APP_ICON := $(TOPDIR)/$(ICON) +endif + +ifeq ($(strip $(NO_SMDH)),) + export _3DSXFLAGS += --smdh=$(CURDIR)/$(TARGET).smdh +endif + +ifneq ($(ROMFS),) + export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS) +endif + +.PHONY: all clean + +#--------------------------------------------------------------------------------- +all: $(BUILD) $(GFXBUILD) $(DEPSDIR) $(ROMFS_T3XFILES) $(T3XHFILES) + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile + +$(BUILD): + @mkdir -p $@ + +ifneq ($(GFXBUILD),$(BUILD)) +$(GFXBUILD): + @mkdir -p $@ +endif + +ifneq ($(DEPSDIR),$(BUILD)) +$(DEPSDIR): + @mkdir -p $@ +endif + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -fr $(BUILD) $(TARGET).3dsx $(OUTPUT).smdh $(TARGET).elf $(GFXBUILD) + +#--------------------------------------------------------------------------------- +$(GFXBUILD)/%.t3x $(BUILD)/%.h : %.t3s +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @tex3ds -i $< -H $(BUILD)/$*.h -d $(DEPSDIR)/$*.d -o $(GFXBUILD)/$*.t3x + +#--------------------------------------------------------------------------------- +else + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +$(OUTPUT).3dsx : $(OUTPUT).elf $(_3DSXDEPS) + +$(OFILES_SOURCES) : $(HFILES) + +$(OUTPUT).elf : $(OFILES) + +#--------------------------------------------------------------------------------- +# you need a rule like this for each extension you use as binary data +#--------------------------------------------------------------------------------- +%.bin.o %_bin.h : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + +#--------------------------------------------------------------------------------- +.PRECIOUS : %.t3x %.shbin +#--------------------------------------------------------------------------------- +%.t3x.o %_t3x.h : %.t3x +#--------------------------------------------------------------------------------- + $(SILENTMSG) $(notdir $<) + $(bin2o) + +#--------------------------------------------------------------------------------- +%.shbin.o %_shbin.h : %.shbin +#--------------------------------------------------------------------------------- + $(SILENTMSG) $(notdir $<) + $(bin2o) + +-include $(DEPSDIR)/*.d + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/audio/ogg-vorbis-decoding/README.md b/audio/ogg-vorbis-decoding/README.md new file mode 100644 index 0000000..258adfa --- /dev/null +++ b/audio/ogg-vorbis-decoding/README.md @@ -0,0 +1,36 @@ +# Ogg Vorbis decoding example + +This is a heavily-commented example of how to carry out fast, threaded Ogg Vorbis audio streaming from the filesystem (in this case, RomFS) on 3DS using libvorbisidec, even on O3DS. + +## Necessary packages + +This example uses `libvorbisidec` (which in turn depends on `libogg`) to read and decode Ogg Vorbis audio files. The makefile also uses `pkg-config` to discover the necessary include paths and flags for the library. + +You can install the necessary packages with the following command: +```bash +pacman -S 3ds-libvorbisidec 3ds-pkg-config +``` + +Note that on some systems, you may need to use `dkp-pacman` instead of `pacman`, and you may need to prefix the installation commands with `sudo`. + +Additionally, if you do not already have `pkg-config` installed on your *host system*, you will need to install it using your package manager. + +On Windows: +```bash +pacman -S pkg-config +``` + +On macOS: +```bash +sudo dkp-pacman -S pkg-config +``` + +## Further reading + +In addition to the detailed comments in `main.c`, see the docs for [libctru](https://libctru.devkitpro.org/) and the not-quite-matching-but-close ones for [libvorbisfile](https://www.xiph.org/vorbis/doc/vorbisfile/index.html). + +## Credits + +Originally written for Opus decoding by [Lauren Kelly (thejsa)](https://github.com/thejsa), with help from [mtheall](https://github.com/mtheall), adapted to Vorbis decoding by [Théo B. (LiquidFenrir)](https://github.com/LiquidFenrir) + +The sample audio included as `sample.ogg` is [The Internet Memory Foundation's recording of Johan Sebastian Bach's Orchestral Suite no. 2 in B minor, BWV 1067 - 7. Badinerie, sourced from Musopen](https://musopen.org/music/3774-orchestral-suite-no-2-in-b-minor-bwv-1067/) and in the public domain (transcoded to mono Ogg Vorbis using Audacity, as ffmpeg's vorbis encoder only supports stereo). diff --git a/audio/ogg-vorbis-decoding/romfs/sample.ogg b/audio/ogg-vorbis-decoding/romfs/sample.ogg new file mode 100644 index 0000000..42224ad Binary files /dev/null and b/audio/ogg-vorbis-decoding/romfs/sample.ogg differ diff --git a/audio/ogg-vorbis-decoding/source/main.c b/audio/ogg-vorbis-decoding/source/main.c new file mode 100644 index 0000000..da0efda --- /dev/null +++ b/audio/ogg-vorbis-decoding/source/main.c @@ -0,0 +1,339 @@ +/* + * Fast, threaded Ogg Vorbis audio streaming example using libvorbisidec + * (also known as libtremor) for libctru on Nintendo 3DS + * + * Adapted to Vorbis by Théo B. (LiquidFenrir), originally written for Opus + * by Lauren Kelly (thejsa) with help from mtheall. + * See the opus-decoding example for more details + * + * Last update: 2024-03-31 + */ + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +#include +#include +#include <3ds.h> + +#include +#include +#include +#include + +// ---- DEFINITIONS ---- + +static const char *PATH = "romfs:/sample.ogg"; // Path to Ogg Vorbis file to play + +static const int THREAD_AFFINITY = -1; // Execute thread on any core +static const int THREAD_STACK_SZ = 32 * 1024; // 32kB stack for audio thread + +// ---- END DEFINITIONS ---- + +ndspWaveBuf s_waveBufs[3]; +int16_t *s_audioBuffer = NULL; + +LightEvent s_event; +volatile bool s_quit = false; // Quit flag + +// ---- HELPER FUNCTIONS ---- + +// Retrieve strings for libvorbisidec errors +const char *vorbisStrError(int error) +{ + switch(error) { + case OV_FALSE: + return "OV_FALSE: A request did not succeed."; + case OV_HOLE: + return "OV_HOLE: There was a hole in the page sequence numbers."; + case OV_EREAD: + return "OV_EREAD: An underlying read, seek or tell operation " + "failed."; + case OV_EFAULT: + return "OV_EFAULT: A NULL pointer was passed where none was " + "expected, or an internal library error was encountered."; + case OV_EIMPL: + return "OV_EIMPL: The stream used a feature which is not " + "implemented."; + case OV_EINVAL: + return "OV_EINVAL: One or more parameters to a function were " + "invalid."; + case OV_ENOTVORBIS: + return "OV_ENOTVORBIS: This is not a valid Ogg Vorbis stream."; + case OV_EBADHEADER: + return "OV_EBADHEADER: A required header packet was not properly " + "formatted."; + case OV_EVERSION: + return "OV_EVERSION: The ID header contained an unrecognised " + "version number."; + case OV_EBADPACKET: + return "OV_EBADPACKET: An audio packet failed to decode properly."; + case OV_EBADLINK: + return "OV_EBADLINK: We failed to find data we had seen before or " + "the stream was sufficiently corrupt that seeking is " + "impossible."; + case OV_ENOSEEK: + return "OV_ENOSEEK: An operation that requires seeking was " + "requested on an unseekable stream."; + default: + return "Unknown error."; + } +} + +// Pause until user presses a button +void waitForInput(void) { + printf("Press any button to exit...\n"); + while(aptMainLoop()) + { + gspWaitForVBlank(); + gfxSwapBuffers(); + hidScanInput(); + + if(hidKeysDown()) + break; + } +} + +// ---- END HELPER FUNCTIONS ---- + +// Audio initialisation code +// This sets up NDSP and our primary audio buffer +bool audioInit(OggVorbis_File *vorbisFile_) { + vorbis_info *vi = ov_info(vorbisFile_, -1); + + // Setup NDSP + ndspChnReset(0); + ndspSetOutputMode(NDSP_OUTPUT_STEREO); + ndspChnSetInterp(0, NDSP_INTERP_POLYPHASE); + ndspChnSetRate(0, vi->rate); + ndspChnSetFormat(0, vi->channels == 1 + ? NDSP_FORMAT_MONO_PCM16 + : NDSP_FORMAT_STEREO_PCM16); + + // Allocate audio buffer + // 120ms buffer + const size_t SAMPLES_PER_BUF = vi->rate * 120 / 1000; + // mono (1) or stereo (2) + const size_t CHANNELS_PER_SAMPLE = vi->channels; + // s16 buffer + const size_t WAVEBUF_SIZE = SAMPLES_PER_BUF * CHANNELS_PER_SAMPLE * sizeof(s16); + const size_t bufferSize = WAVEBUF_SIZE * ARRAY_SIZE(s_waveBufs); + s_audioBuffer = (int16_t *)linearAlloc(bufferSize); + if(!s_audioBuffer) { + printf("Failed to allocate audio buffer\n"); + return false; + } + + // Setup waveBufs for NDSP + memset(&s_waveBufs, 0, sizeof(s_waveBufs)); + int16_t *buffer = s_audioBuffer; + + for(size_t i = 0; i < ARRAY_SIZE(s_waveBufs); ++i) { + s_waveBufs[i].data_vaddr = buffer; + s_waveBufs[i].nsamples = WAVEBUF_SIZE / sizeof(buffer[0]); + s_waveBufs[i].status = NDSP_WBUF_DONE; + + buffer += WAVEBUF_SIZE / sizeof(buffer[0]); + } + + return true; +} + +// Audio de-initialisation code +// Stops playback and frees the primary audio buffer +void audioExit(void) { + ndspChnReset(0); + linearFree(s_audioBuffer); +} + +// Main audio decoding logic +// This function pulls and decodes audio samples from vorbisFile_ to fill waveBuf_ +bool fillBuffer(OggVorbis_File *vorbisFile_, ndspWaveBuf *waveBuf_) { + #ifdef DEBUG + // Setup timer for performance stats + TickCounter timer; + osTickCounterStart(&timer); + #endif // DEBUG + + // Decode (2-byte) samples until our waveBuf is full + int totalBytes = 0; + while(totalBytes < waveBuf_->nsamples * sizeof(s16)) { + int16_t *buffer = waveBuf_->data_pcm16 + (totalBytes / sizeof(s16)); + const size_t bufferSize = (waveBuf_->nsamples * sizeof(s16) - totalBytes); + + // Decode bufferSize bytes from vorbisFile_ into buffer, + // storing the number of bytes that were read (or error) + const int bytesRead = ov_read(vorbisFile_, (char *)buffer, bufferSize, NULL); + if(bytesRead <= 0) { + if(bytesRead == 0) break; // No error here + + printf("ov_read: error %d (%s)", bytesRead, + vorbisStrError(bytesRead)); + break; + } + + totalBytes += bytesRead; + } + + // If no samples were read in the last decode cycle, we're done + if(totalBytes == 0) { + printf("Playback complete, press Start to exit\n"); + return false; + } + + // Pass samples to NDSP + // this calculation will make a number <= the previous nsamples + // = for most cases + // < for the last possible chunk of the file, which may have less samples before EOF + // after which we don't care to recover the length + waveBuf_->nsamples = totalBytes / sizeof(s16); + ndspChnWaveBufAdd(0, waveBuf_); + DSP_FlushDataCache(waveBuf_->data_pcm16, totalBytes); + + #ifdef DEBUG + // Print timing info + osTickCounterUpdate(&timer); + printf("fillBuffer %lfms in %lfms\n", totalSamples * 1000.0 / SAMPLE_RATE, + osTickCounterRead(&timer)); + #endif // DEBUG + + return true; +} + +// NDSP audio frame callback +// This signals the audioThread to decode more things +// once NDSP has played a sound frame, meaning that there should be +// one or more available waveBufs to fill with more data. +void audioCallback(void *const nul_) { + (void)nul_; // Unused + + if(s_quit) { // Quit flag + return; + } + + LightEvent_Signal(&s_event); +} + +// Audio thread +// This handles calling the decoder function to fill NDSP buffers as necessary +void audioThread(void *const vorbisFile_) { + OggVorbis_File *const vorbisFile = (OggVorbis_File *)vorbisFile_; + + while(!s_quit) { // Whilst the quit flag is unset, + // search our waveBufs and fill any that aren't currently + // queued for playback (i.e, those that are 'done') + for(size_t i = 0; i < ARRAY_SIZE(s_waveBufs); ++i) { + if(s_waveBufs[i].status != NDSP_WBUF_DONE) { + continue; + } + + if(!fillBuffer(vorbisFile, &s_waveBufs[i])) { // Playback complete + return; + } + } + + // Wait for a signal that we're needed again before continuing, + // so that we can yield to other things that want to run + // (Note that the 3DS uses cooperative threading) + LightEvent_Wait(&s_event); + } +} + +int main(int argc, char* argv[]) { + // Initialise platform features + romfsInit(); + ndspInit(); + gfxInitDefault(); + + consoleInit(GFX_TOP, NULL); + + // Setup LightEvent for synchronisation of audioThread + LightEvent_Init(&s_event, RESET_ONESHOT); + + printf("Ogg Vorbis audio streaming example\n" + "LiquidFenrir, March 2024\n" + "based on: Opus example\n" + "thejsa and mtheall, May 2020\n" + "Press START to exit\n" + "\n" + "Loading audio data from path: %s\n" + "\n", + PATH); + + // Open the Ogg Vorbis audio file + OggVorbis_File vorbisFile; + FILE *fh = fopen(PATH, "rb"); + int error = ov_open(fh, &vorbisFile, NULL, 0); + if(error) { + printf("Failed to open file: error %d (%s)\n", error, + vorbisStrError(error)); + // Only fclose manually if ov_open failed. + // If ov_open succeeds, fclose happens in ov_clear. + fclose(fh); + waitForInput(); + } + + // Attempt audioInit + if(!audioInit(&vorbisFile)) { + printf("Failed to initialise audio\n"); + ov_clear(&vorbisFile); + waitForInput(); + + gfxExit(); + ndspExit(); + romfsExit(); + return EXIT_FAILURE; + } + + // Set the ndsp sound frame callback which signals our audioThread + ndspSetCallback(audioCallback, NULL); + + // Spawn audio thread + + // Set the thread priority to the main thread's priority ... + int32_t priority = 0x30; + svcGetThreadPriority(&priority, CUR_THREAD_HANDLE); + // ... then subtract 1, as lower number => higher actual priority ... + priority -= 1; + // ... finally, clamp it between 0x18 and 0x3F to guarantee that it's valid. + priority = priority < 0x18 ? 0x18 : priority; + priority = priority > 0x3F ? 0x3F : priority; + + // Start the thread, passing the address of our vorbisFile as an argument. + const Thread threadId = threadCreate(audioThread, &vorbisFile, + THREAD_STACK_SZ, priority, + THREAD_AFFINITY, false); + printf("Created audio thread %p\n", threadId); + + // Standard main loop + while(aptMainLoop()) + { + gspWaitForVBlank(); + gfxSwapBuffers(); + hidScanInput(); + + // Your code goes here + u32 kDown = hidKeysDown(); + if(kDown & KEY_START) { + printf("\n** Quitting... **\n"); + break; + } + } + + // Signal audio thread to quit + s_quit = true; + LightEvent_Signal(&s_event); + + // Free the audio thread + threadJoin(threadId, UINT64_MAX); + threadFree(threadId); + + // Cleanup audio things and de-init platform features + audioExit(); + ndspExit(); + ov_clear(&vorbisFile); + + romfsExit(); + gfxExit(); + + return EXIT_SUCCESS; +}