Refactor Opus decoder
Created generic playback handler, first with Opus support. Other decoders to follow. This is to remove duplicated code. 3DSX tested working with citra and N3DS. Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
12
source/all.h
12
source/all.h
@@ -4,9 +4,19 @@
|
||||
#define CHANNEL 0x08
|
||||
|
||||
/* Adds extra debugging text */
|
||||
//#define DEBUG
|
||||
#define DEBUG
|
||||
|
||||
/* Prints more error information */
|
||||
#define err_print(err) \
|
||||
do { fprintf(stderr, "\nError %d:%s(): %s %s\n", __LINE__, __func__, \
|
||||
err, strerror(errno)); } while (0)
|
||||
|
||||
struct decoder_fn
|
||||
{
|
||||
int (* init)(const char* file);
|
||||
uint32_t (* rate)(void);
|
||||
uint8_t channels;
|
||||
int buffSize;
|
||||
uint64_t (* decode)(void*);
|
||||
void (* exit)(void);
|
||||
};
|
||||
|
||||
@@ -20,22 +20,9 @@
|
||||
#include "main.h"
|
||||
#include "mp3.h"
|
||||
#include "opus.h"
|
||||
#include "playback.h"
|
||||
#include "wav.h"
|
||||
|
||||
/* Default folder */
|
||||
#define DEFAULT_DIR "sdmc:/"
|
||||
/* Maximum number of lines that can be displayed */
|
||||
#define MAX_LIST 27
|
||||
|
||||
enum file_types {
|
||||
FILE_TYPE_ERROR = -1,
|
||||
FILE_TYPE_WAV,
|
||||
FILE_TYPE_FLAC,
|
||||
FILE_TYPE_OGG,
|
||||
FILE_TYPE_OPUS,
|
||||
FILE_TYPE_MP3
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
PrintConsole topScreen;
|
||||
@@ -204,6 +191,7 @@ int main(int argc, char **argv)
|
||||
else
|
||||
{
|
||||
consoleSelect(&topScreen);
|
||||
/* Move this to playback.c */
|
||||
switch(getFileType(file))
|
||||
{
|
||||
case FILE_TYPE_WAV:
|
||||
@@ -215,7 +203,7 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
|
||||
case FILE_TYPE_OPUS:
|
||||
playOpus(file);
|
||||
playFile(file);
|
||||
break;
|
||||
|
||||
case FILE_TYPE_MP3:
|
||||
|
||||
@@ -7,6 +7,21 @@
|
||||
* LICENSE file.
|
||||
*/
|
||||
|
||||
/* Default folder */
|
||||
#define DEFAULT_DIR "sdmc:/"
|
||||
|
||||
/* Maximum number of lines that can be displayed */
|
||||
#define MAX_LIST 27
|
||||
|
||||
enum file_types {
|
||||
FILE_TYPE_ERROR = -1,
|
||||
FILE_TYPE_WAV,
|
||||
FILE_TYPE_FLAC,
|
||||
FILE_TYPE_OGG,
|
||||
FILE_TYPE_OPUS,
|
||||
FILE_TYPE_MP3
|
||||
};
|
||||
|
||||
/**
|
||||
* Get number of files in current working folder
|
||||
*
|
||||
|
||||
208
source/opus.c
208
source/opus.c
@@ -7,146 +7,84 @@
|
||||
|
||||
#define SAMPLES_TO_READ (32 * 1024)
|
||||
|
||||
int playOpus(const char* in)
|
||||
static OggOpusFile* opusFile;
|
||||
static const OpusHead* opusHead;
|
||||
|
||||
/**
|
||||
* Set decoder parameters for Opus.
|
||||
*
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
void setOpus(struct decoder_fn* decoder)
|
||||
{
|
||||
int err = 0;
|
||||
int16_t* buffer1 = linearAlloc(SAMPLES_TO_READ * sizeof(int16_t));
|
||||
int16_t* buffer2 = linearAlloc(SAMPLES_TO_READ * sizeof(int16_t));
|
||||
ndspWaveBuf waveBuf[2];
|
||||
bool playing = true;
|
||||
bool lastbuf = false;
|
||||
OggOpusFile* opusFile = op_open_file(in, &err);
|
||||
const OpusHead* opusHead;
|
||||
int link;
|
||||
|
||||
if(err != 0)
|
||||
{
|
||||
printf("libopusfile failed with error %d.", err);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(R_FAILED(ndspInit()))
|
||||
{
|
||||
printf("Initialising ndsp failed.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if((link = op_current_link(opusFile)) < 0)
|
||||
{
|
||||
printf("Error getting current link: %d\n", link);
|
||||
goto out;
|
||||
}
|
||||
|
||||
opusHead = op_head(opusFile, link);
|
||||
#ifdef DEBUG
|
||||
printf("\nRate: %lu\tChan: %d\n", opusHead->input_sample_rate,
|
||||
opusHead->channel_count);
|
||||
#endif
|
||||
|
||||
ndspChnReset(CHANNEL);
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
||||
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
||||
ndspChnSetRate(CHANNEL, opusHead->input_sample_rate);
|
||||
ndspChnSetFormat(CHANNEL, NDSP_FORMAT_STEREO_PCM16);
|
||||
|
||||
memset(waveBuf, 0, sizeof(waveBuf));
|
||||
waveBuf[0].nsamples =
|
||||
fillOpusBuffer(opusFile, SAMPLES_TO_READ, buffer1) / 2;
|
||||
waveBuf[0].data_vaddr = &buffer1[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
|
||||
waveBuf[1].nsamples =
|
||||
fillOpusBuffer(opusFile, SAMPLES_TO_READ, buffer2) / 2;
|
||||
waveBuf[1].data_vaddr = &buffer2[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
|
||||
printf("Playing %s\n", in);
|
||||
/**
|
||||
* There may be a chance that the music has not started by the time we get
|
||||
* to the while loop. So we ensure that music has started here.
|
||||
*/
|
||||
while(ndspChnIsPlaying(CHANNEL) == false);
|
||||
|
||||
while(playing == false || ndspChnIsPlaying(CHANNEL) == true)
|
||||
{
|
||||
u32 kDown;
|
||||
|
||||
/* Number of bytes read from file.
|
||||
* Static only for the purposes of the printf debug at the bottom.
|
||||
*/
|
||||
static size_t read = 0;
|
||||
|
||||
gfxSwapBuffers();
|
||||
gfxFlushBuffers();
|
||||
gspWaitForVBlank();
|
||||
|
||||
hidScanInput();
|
||||
kDown = hidKeysDown();
|
||||
|
||||
if(kDown & KEY_B)
|
||||
break;
|
||||
|
||||
if(kDown & (KEY_A | KEY_R))
|
||||
{
|
||||
playing = !playing;
|
||||
printf("\33[2K\r%s", playing == false ? "Paused" : "");
|
||||
}
|
||||
|
||||
if(playing == false || lastbuf == true)
|
||||
continue;
|
||||
|
||||
if(waveBuf[0].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
read = fillOpusBuffer(opusFile, SAMPLES_TO_READ, buffer1);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < SAMPLES_TO_READ)
|
||||
waveBuf[0].nsamples = read / opusHead->channel_count;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
}
|
||||
|
||||
if(waveBuf[1].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
read = fillOpusBuffer(opusFile, SAMPLES_TO_READ, buffer2);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < SAMPLES_TO_READ)
|
||||
waveBuf[1].nsamples = read / opusHead->channel_count;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
}
|
||||
|
||||
DSP_FlushDataCache(buffer1, SAMPLES_TO_READ * sizeof(s16));
|
||||
DSP_FlushDataCache(buffer2, SAMPLES_TO_READ * sizeof(s16));
|
||||
}
|
||||
|
||||
out:
|
||||
printf("\nStopping Opus playback.\n");
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
ndspExit();
|
||||
linearFree(buffer1);
|
||||
linearFree(buffer2);
|
||||
op_free(opusFile);
|
||||
return 0;
|
||||
decoder->init = initOpus;
|
||||
decoder->rate = rateOpus;
|
||||
/* Opus decoder always returns stereo stream */
|
||||
decoder->channels = 2;
|
||||
decoder->buffSize = SAMPLES_TO_READ;
|
||||
decoder->decode = decodeOpus;
|
||||
decoder->exit = exitOpus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill a buffer with decoded samples.
|
||||
* Initialise Opus decoder.
|
||||
*
|
||||
* \param opusFile OggOpusFile pointer.
|
||||
* \param samplesToRead Number of samples to read in to buffer.
|
||||
* \param bufferOut Pointer to output buffer.
|
||||
* \return Number of samples read per channel.
|
||||
* \param file Location of opus file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initOpus(const char* file)
|
||||
{
|
||||
int err = 0;
|
||||
|
||||
if((opusFile = op_open_file(file, &err)) == NULL)
|
||||
goto out;
|
||||
|
||||
if((err = op_current_link(opusFile)) < 0)
|
||||
goto out;
|
||||
|
||||
opusHead = op_head(opusFile, err);
|
||||
|
||||
out:
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sampling rate of Opus file.
|
||||
*
|
||||
* \return Sampling rate. Should be 48000.
|
||||
*/
|
||||
uint32_t rateOpus(void)
|
||||
{
|
||||
return opusHead->input_sample_rate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode part of open Opus file.
|
||||
*
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeOpus(void* buffer)
|
||||
{
|
||||
return fillOpusBuffer(opusFile, SAMPLES_TO_READ, buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free Opus decoder.
|
||||
*/
|
||||
void exitOpus(void)
|
||||
{
|
||||
op_free(opusFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode Opus file to fill buffer.
|
||||
*
|
||||
* \param opusFile File to decode.
|
||||
* \param samplesToRead Number of samples to read in to buffer. Must not exceed
|
||||
* size of buffer.
|
||||
* \param bufferOut Pointer to buffer.
|
||||
* \return Samples read per channel.
|
||||
*/
|
||||
uint64_t fillOpusBuffer(OggOpusFile* opusFile, uint64_t samplesToRead,
|
||||
int16_t* bufferOut)
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
#include <opus/opusfile.h>
|
||||
|
||||
void setOpus(struct decoder_fn* decoder);
|
||||
|
||||
int initOpus(const char* file);
|
||||
|
||||
uint32_t rateOpus(void);
|
||||
|
||||
uint64_t decodeOpus(void* buffer);
|
||||
|
||||
void exitOpus(void);
|
||||
|
||||
int playOpus(const char* in);
|
||||
|
||||
uint64_t fillOpusBuffer(OggOpusFile* opusFile, uint64_t samplesToRead,
|
||||
|
||||
138
source/playback.c
Normal file
138
source/playback.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <3ds.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "all.h"
|
||||
#include "opus.h"
|
||||
#include "playback.h"
|
||||
|
||||
int playFile(const char* file)
|
||||
{
|
||||
struct decoder_fn decoder;
|
||||
int16_t* buffer1 = NULL;
|
||||
int16_t* buffer2 = NULL;
|
||||
ndspWaveBuf waveBuf[2];
|
||||
bool playing = true;
|
||||
bool lastbuf = false;
|
||||
|
||||
printf("Here: %d\n", __LINE__);
|
||||
if(R_FAILED(ndspInit()))
|
||||
{
|
||||
printf("Initialising ndsp failed.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
printf("Here: %d\n", __LINE__);
|
||||
setOpus(&decoder);
|
||||
|
||||
printf("Here: %d\n", __LINE__);
|
||||
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||
buffer2 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||
|
||||
printf("Here: %d\n", __LINE__);
|
||||
if((*decoder.init)(file) != 0)
|
||||
goto out;
|
||||
|
||||
printf("Here: %d\n", __LINE__);
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("\nRate: %lu\tChan: %d\n", (*decoder.rate)(), decoder.channels);
|
||||
#endif
|
||||
|
||||
ndspChnReset(CHANNEL);
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
||||
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
||||
ndspChnSetRate(CHANNEL, (*decoder.rate)());
|
||||
ndspChnSetFormat(CHANNEL, NDSP_FORMAT_STEREO_PCM16);
|
||||
printf("Here: %d\n", __LINE__);
|
||||
|
||||
memset(waveBuf, 0, sizeof(waveBuf));
|
||||
waveBuf[0].nsamples = (*decoder.decode)(&buffer1[0]) / decoder.channels;
|
||||
waveBuf[0].data_vaddr = &buffer1[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
printf("Here: %d\n", __LINE__);
|
||||
|
||||
waveBuf[1].nsamples = (*decoder.decode)(&buffer2[0]) / decoder.channels;
|
||||
waveBuf[1].data_vaddr = &buffer2[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
|
||||
printf("Playing %s\n", file);
|
||||
|
||||
/**
|
||||
* There may be a chance that the music has not started by the time we get
|
||||
* to the while loop. So we ensure that music has started here.
|
||||
*/
|
||||
while(ndspChnIsPlaying(CHANNEL) == false);
|
||||
|
||||
while(playing == false || ndspChnIsPlaying(CHANNEL) == true)
|
||||
{
|
||||
u32 kDown;
|
||||
|
||||
/* Number of bytes read from file.
|
||||
* Static only for the purposes of the printf debug at the bottom.
|
||||
*/
|
||||
static size_t read = 0;
|
||||
|
||||
gfxSwapBuffers();
|
||||
gfxFlushBuffers();
|
||||
gspWaitForVBlank();
|
||||
|
||||
hidScanInput();
|
||||
kDown = hidKeysDown();
|
||||
|
||||
if(kDown & KEY_B)
|
||||
break;
|
||||
|
||||
if(kDown & (KEY_A | KEY_R))
|
||||
{
|
||||
playing = !playing;
|
||||
printf("\33[2K\r%s", playing == false ? "Paused" : "");
|
||||
}
|
||||
|
||||
if(playing == false || lastbuf == true)
|
||||
continue;
|
||||
|
||||
if(waveBuf[0].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
read = (*decoder.decode)(&buffer1[0]);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < decoder.buffSize)
|
||||
waveBuf[0].nsamples = read / decoder.channels;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
}
|
||||
|
||||
if(waveBuf[1].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
read = (*decoder.decode)(&buffer2[0]);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < decoder.buffSize)
|
||||
waveBuf[1].nsamples = read / decoder.channels;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
}
|
||||
|
||||
DSP_FlushDataCache(buffer1, decoder.buffSize * sizeof(int16_t));
|
||||
DSP_FlushDataCache(buffer2, decoder.buffSize * sizeof(int16_t));
|
||||
}
|
||||
|
||||
out:
|
||||
printf("\nStopping playback.\n");
|
||||
(*decoder.exit)();
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
ndspExit();
|
||||
linearFree(buffer1);
|
||||
linearFree(buffer2);
|
||||
return 0;
|
||||
}
|
||||
1
source/playback.h
Normal file
1
source/playback.h
Normal file
@@ -0,0 +1 @@
|
||||
int playFile(const char* file);
|
||||
Reference in New Issue
Block a user