Merge branch 'refactor'
This commit is contained in:
@@ -36,7 +36,6 @@ Start = Return to Homebrew menu (Only when stopped playing).
|
||||
### Planned features
|
||||
* Playlist support.
|
||||
* Repeat and shuffle support.
|
||||
* OGG file support.
|
||||
* Metadata support.
|
||||
* Gain support.
|
||||
|
||||
|
||||
Submodule buildtools updated: 2ef982ddbd...29ab2234e7
Submodule include/dr_libs updated: f93c24e569...9b86194035
10
source/all.h
10
source/all.h
@@ -10,3 +10,13 @@
|
||||
#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)(void);
|
||||
int buffSize;
|
||||
uint64_t (* decode)(void*);
|
||||
void (* exit)(void);
|
||||
};
|
||||
|
||||
189
source/flac.c
189
source/flac.c
@@ -3,129 +3,74 @@
|
||||
#define DR_FLAC_IMPLEMENTATION
|
||||
#include <./dr_libs/dr_flac.h>
|
||||
#include "all.h"
|
||||
#include "flac.h"
|
||||
|
||||
#define SAMPLES_TO_READ (16 * 1024)
|
||||
static drflac* pFlac;
|
||||
static const int buffSize = 16 * 1024;
|
||||
|
||||
int playFlac(const char* in)
|
||||
/**
|
||||
* Set decoder parameters for flac.
|
||||
*
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
void setFlac(struct decoder_fn* decoder)
|
||||
{
|
||||
drflac* pFlac = drflac_open_file(in);
|
||||
s16* buffer1 = linearAlloc(SAMPLES_TO_READ * sizeof(s16));
|
||||
s16* buffer2 = linearAlloc(SAMPLES_TO_READ * sizeof(s16));
|
||||
ndspWaveBuf waveBuf[2];
|
||||
bool playing = true;
|
||||
bool lastbuf = false;
|
||||
|
||||
if (pFlac == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(R_FAILED(ndspInit()))
|
||||
{
|
||||
printf("Initialising ndsp failed.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("\nRate: %lu\tChan: %d\n", pFlac->sampleRate,
|
||||
pFlac->channels);
|
||||
#endif
|
||||
|
||||
ndspChnReset(CHANNEL);
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
||||
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
||||
ndspChnSetRate(CHANNEL, pFlac->sampleRate);
|
||||
ndspChnSetFormat(CHANNEL, pFlac->channels == 2 ? NDSP_FORMAT_STEREO_PCM16 :
|
||||
NDSP_FORMAT_MONO_PCM16);
|
||||
|
||||
memset(waveBuf, 0, sizeof(waveBuf));
|
||||
waveBuf[0].nsamples =
|
||||
drflac_read_s16(pFlac, SAMPLES_TO_READ, buffer1) / pFlac->channels;
|
||||
waveBuf[0].data_vaddr = &buffer1[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
|
||||
waveBuf[1].nsamples =
|
||||
drflac_read_s16(pFlac, SAMPLES_TO_READ, buffer2) / pFlac->channels;
|
||||
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 = drflac_read_s16(pFlac, SAMPLES_TO_READ, buffer1);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < SAMPLES_TO_READ)
|
||||
waveBuf[0].nsamples = read / pFlac->channels;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
}
|
||||
|
||||
if(waveBuf[1].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
read = drflac_read_s16(pFlac, SAMPLES_TO_READ, buffer2);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < SAMPLES_TO_READ)
|
||||
waveBuf[1].nsamples = read / pFlac->channels;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
}
|
||||
|
||||
DSP_FlushDataCache(buffer1, SAMPLES_TO_READ * sizeof(s16));
|
||||
DSP_FlushDataCache(buffer2, SAMPLES_TO_READ * sizeof(s16));
|
||||
}
|
||||
|
||||
printf("\nEnd of file.");
|
||||
|
||||
out:
|
||||
printf("\nStopping Flac playback.\n");
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
ndspExit();
|
||||
linearFree(buffer1);
|
||||
linearFree(buffer2);
|
||||
drflac_close(pFlac);
|
||||
return 0;
|
||||
decoder->init = &initFlac;
|
||||
decoder->rate = &rateFlac;
|
||||
decoder->channels = &channelFlac;
|
||||
decoder->buffSize = buffSize;
|
||||
decoder->decode = &decodeFlac;
|
||||
decoder->exit = &exitFlac;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise Flac decoder.
|
||||
*
|
||||
* \param file Location of flac file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initFlac(const char* file)
|
||||
{
|
||||
pFlac = drflac_open_file(file);
|
||||
|
||||
return pFlac == NULL ? -1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sampling rate of Flac file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateFlac(void)
|
||||
{
|
||||
return pFlac->sampleRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of channels of Flac file.
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelFlac(void)
|
||||
{
|
||||
return pFlac->channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode part of open Flac file.
|
||||
*
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeFlac(void* buffer)
|
||||
{
|
||||
return drflac_read_s16(pFlac, buffSize, buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free Flac decoder.
|
||||
*/
|
||||
void exitFlac(void)
|
||||
{
|
||||
drflac_close(pFlac);
|
||||
}
|
||||
|
||||
@@ -1 +1,41 @@
|
||||
int playFlac(const char* in);
|
||||
/**
|
||||
* Set decoder parameters for flac.
|
||||
*
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
void setFlac(struct decoder_fn* decoder);
|
||||
|
||||
/**
|
||||
* Initialise Flac decoder.
|
||||
*
|
||||
* \param file Location of flac file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initFlac(const char* file);
|
||||
|
||||
/**
|
||||
* Get sampling rate of Flac file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateFlac(void);
|
||||
|
||||
/**
|
||||
* Get number of channels of Flac file.
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelFlac(void);
|
||||
|
||||
/**
|
||||
* Decode part of open Flac file.
|
||||
*
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeFlac(void* buffer);
|
||||
|
||||
/**
|
||||
* Free Flac decoder.
|
||||
*/
|
||||
void exitFlac(void);
|
||||
|
||||
146
source/main.c
146
source/main.c
@@ -16,25 +16,8 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "all.h"
|
||||
#include "flac.h"
|
||||
#include "main.h"
|
||||
#include "mp3.h"
|
||||
#include "opus.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
|
||||
};
|
||||
#include "playback.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@@ -86,7 +69,10 @@ int main(int argc, char **argv)
|
||||
kHeld = hidKeysHeld();
|
||||
|
||||
if(kDown & KEY_START)
|
||||
{
|
||||
puts("Test.");
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
consoleSelect(&topScreen);
|
||||
@@ -204,32 +190,10 @@ int main(int argc, char **argv)
|
||||
else
|
||||
{
|
||||
consoleSelect(&topScreen);
|
||||
switch(getFileType(file))
|
||||
{
|
||||
case FILE_TYPE_WAV:
|
||||
playWav(file);
|
||||
break;
|
||||
|
||||
case FILE_TYPE_FLAC:
|
||||
playFlac(file);
|
||||
break;
|
||||
|
||||
case FILE_TYPE_OPUS:
|
||||
playOpus(file);
|
||||
break;
|
||||
|
||||
case FILE_TYPE_MP3:
|
||||
playMp3(file);
|
||||
break;
|
||||
|
||||
default:
|
||||
consoleSelect(&bottomScreen);
|
||||
printf("Unsupported File type.\n");
|
||||
}
|
||||
playFile(file);
|
||||
consoleSelect(&bottomScreen);
|
||||
}
|
||||
|
||||
consoleSelect(&bottomScreen);
|
||||
|
||||
free(file);
|
||||
free(wd);
|
||||
|
||||
@@ -240,6 +204,11 @@ int main(int argc, char **argv)
|
||||
err_print("Unable to open directory.");
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
consoleSelect(&topScreen);
|
||||
printf("\rNum: %d, Max: %d, from: %d ", fileNum, fileMax, from);
|
||||
consoleSelect(&bottomScreen);
|
||||
#endif
|
||||
|
||||
out:
|
||||
puts("Exiting...");
|
||||
@@ -284,7 +253,7 @@ int listDir(int from, int max, int select)
|
||||
if(wd == NULL)
|
||||
goto err;
|
||||
|
||||
printf("Dir: %.30s\n", wd);
|
||||
printf("Dir: %.33s\n", wd);
|
||||
|
||||
if((dp = opendir(wd)) == NULL)
|
||||
goto err;
|
||||
@@ -357,94 +326,3 @@ err:
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains file type.
|
||||
*
|
||||
* \param file File location.
|
||||
* \return File type, else negative.
|
||||
*/
|
||||
int getFileType(const char *file)
|
||||
{
|
||||
FILE* ftest = fopen(file, "rb");
|
||||
int fileSig = 0;
|
||||
enum file_types file_type = FILE_TYPE_ERROR;
|
||||
|
||||
if(ftest == NULL)
|
||||
{
|
||||
err_print("Opening file failed.");
|
||||
printf("file: %s\n", file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(fread(&fileSig, 4, 1, ftest) == 0)
|
||||
{
|
||||
err_print("Unable to read file.");
|
||||
fclose(ftest);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch(fileSig)
|
||||
{
|
||||
// "RIFF"
|
||||
case 0x46464952:
|
||||
if(fseek(ftest, 4, SEEK_CUR) != 0)
|
||||
{
|
||||
err_print("Unable to seek.");
|
||||
break;
|
||||
}
|
||||
|
||||
// "WAVE"
|
||||
// Check required as AVI file format also uses "RIFF".
|
||||
if(fread(&fileSig, 4, 1, ftest) == 0)
|
||||
{
|
||||
err_print("Unable to read potential WAV file.");
|
||||
break;
|
||||
}
|
||||
|
||||
if(fileSig != 0x45564157)
|
||||
break;
|
||||
|
||||
file_type = FILE_TYPE_WAV;
|
||||
printf("File type is WAV.");
|
||||
break;
|
||||
|
||||
// "fLaC"
|
||||
case 0x43614c66:
|
||||
file_type = FILE_TYPE_FLAC;
|
||||
printf("File type is FLAC.");
|
||||
break;
|
||||
|
||||
// "OggS"
|
||||
case 0x5367674f:
|
||||
if(isOpus(file) == 0)
|
||||
{
|
||||
printf("\nFile type is Opus.");
|
||||
file_type = FILE_TYPE_OPUS;
|
||||
}
|
||||
else
|
||||
{
|
||||
file_type = FILE_TYPE_OGG;
|
||||
printf("\nFile type is OGG.");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
/*
|
||||
* MP3 without ID3 tag, ID3v1 tag is at the end of file, or MP3
|
||||
* with ID3 tag at the beginning of the file.
|
||||
*/
|
||||
if((fileSig << 16) == 0xFBFF0000 || (fileSig << 8) == 0x33444900)
|
||||
{
|
||||
puts("File type is MP3.");
|
||||
file_type = FILE_TYPE_MP3;
|
||||
break;
|
||||
}
|
||||
|
||||
printf("Unknown magic number: %#010x\n.", fileSig);
|
||||
}
|
||||
|
||||
fclose(ftest);
|
||||
return file_type;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,12 @@
|
||||
* LICENSE file.
|
||||
*/
|
||||
|
||||
/* Default folder */
|
||||
#define DEFAULT_DIR "sdmc:/"
|
||||
|
||||
/* Maximum number of lines that can be displayed */
|
||||
#define MAX_LIST 27
|
||||
|
||||
/**
|
||||
* Get number of files in current working folder
|
||||
*
|
||||
@@ -24,11 +30,3 @@ int getNumberFiles(void);
|
||||
* \return Number of entries listed or negative on error.
|
||||
*/
|
||||
int listDir(int from, int max, int select);
|
||||
|
||||
/**
|
||||
* Obtains file type.
|
||||
*
|
||||
* \param file File location.
|
||||
* \return File type, else negative.
|
||||
*/
|
||||
int getFileType(const char *file);
|
||||
|
||||
211
source/mp3.c
211
source/mp3.c
@@ -1,4 +1,5 @@
|
||||
#include <3ds.h>
|
||||
#include <mpg123.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -6,154 +7,112 @@
|
||||
#include "all.h"
|
||||
#include "mp3.h"
|
||||
|
||||
int playMp3(const char* in)
|
||||
{
|
||||
int err = 0;
|
||||
mpg123_handle *mh = NULL;
|
||||
size_t buffer_size = 0;
|
||||
size_t done = 0;
|
||||
long rate = 0;
|
||||
int channels = 0;
|
||||
int encoding = 0;
|
||||
unsigned char* buffer1 = NULL;
|
||||
unsigned char* buffer2 = NULL;
|
||||
int ret = 0;
|
||||
ndspWaveBuf waveBuf[2];
|
||||
bool playing = true;
|
||||
bool lastbuf = false;
|
||||
static int* buffSize;
|
||||
static mpg123_handle *mh = NULL;
|
||||
static uint32_t rate;
|
||||
static uint8_t channels;
|
||||
|
||||
if(R_FAILED(ndspInit()))
|
||||
{
|
||||
printf("Initialising ndsp failed.");
|
||||
goto out;
|
||||
}
|
||||
/**
|
||||
* Set decoder parameters for MP3.
|
||||
*
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
void setMp3(struct decoder_fn* decoder)
|
||||
{
|
||||
decoder->init = &initMp3;
|
||||
decoder->rate = &rateMp3;
|
||||
decoder->channels = &channelMp3;
|
||||
/*
|
||||
* buffSize changes depending on input file. So we set buffSize later when
|
||||
* decoder is initialised.
|
||||
*/
|
||||
buffSize = &(decoder->buffSize);
|
||||
decoder->decode = &decodeMp3;
|
||||
decoder->exit = &exitMp3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise MP3 decoder.
|
||||
*
|
||||
* \param file Location of MP3 file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initMp3(const char* file)
|
||||
{
|
||||
int err = 0;
|
||||
int encoding = 0;
|
||||
|
||||
if((err = mpg123_init()) != MPG123_OK)
|
||||
return err;
|
||||
|
||||
if((mh = mpg123_new(NULL, &err)) == NULL)
|
||||
{
|
||||
printf("Error: %s\n", mpg123_plain_strerror(err));
|
||||
goto err;
|
||||
//printf("Error: %s\n", mpg123_plain_strerror(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
if(mpg123_open(mh, in) != MPG123_OK ||
|
||||
mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK)
|
||||
if(mpg123_open(mh, file) != MPG123_OK ||
|
||||
mpg123_getformat(mh, (long *) &rate, (int *) &channels, &encoding) != MPG123_OK)
|
||||
{
|
||||
printf("Trouble with mpg123: %s\n", mpg123_strerror(mh));
|
||||
goto err;
|
||||
//printf("Trouble with mpg123: %s\n", mpg123_strerror(mh));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Ensure that this output format will not change
|
||||
(it might, when we allow it). */
|
||||
/*
|
||||
* Ensure that this output format will not change (it might, when we allow
|
||||
* it).
|
||||
*/
|
||||
mpg123_format_none(mh);
|
||||
mpg123_format(mh, rate, channels, encoding);
|
||||
|
||||
/* Buffer could be almost any size here, mpg123_outblock() is just some
|
||||
recommendation. The size should be a multiple of the PCM frame size. */
|
||||
buffer_size = mpg123_outblock(mh) * 16;
|
||||
buffer1 = linearAlloc(buffer_size);
|
||||
buffer2 = linearAlloc(buffer_size);
|
||||
|
||||
/* I'm not sure if this error will ever occur. */
|
||||
if(channels > 2)
|
||||
{
|
||||
printf("Invalid number of channels %d\n", channels);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ndspChnReset(CHANNEL);
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
||||
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
||||
ndspChnSetRate(CHANNEL, rate);
|
||||
ndspChnSetFormat(CHANNEL,
|
||||
channels == 2 ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16);
|
||||
|
||||
memset(waveBuf, 0, sizeof(waveBuf));
|
||||
|
||||
mpg123_read(mh, buffer1, buffer_size, &done);
|
||||
waveBuf[0].nsamples = done / (sizeof(s16) * channels);
|
||||
waveBuf[0].data_vaddr = &buffer1[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
|
||||
mpg123_read(mh, buffer2, buffer_size, &done);
|
||||
waveBuf[1].nsamples = done / (sizeof(s16) * channels);
|
||||
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.
|
||||
/*
|
||||
* Buffer could be almost any size here, mpg123_outblock() is just some
|
||||
* recommendation. The size should be a multiple of the PCM frame size.
|
||||
*/
|
||||
while(ndspChnIsPlaying(CHANNEL) == false);
|
||||
*buffSize = mpg123_outblock(mh) * 16;
|
||||
|
||||
while(playing == false || ndspChnIsPlaying(CHANNEL) == true)
|
||||
{
|
||||
u32 kDown;
|
||||
return 0;
|
||||
}
|
||||
|
||||
gfxSwapBuffers();
|
||||
gfxFlushBuffers();
|
||||
gspWaitForVBlank();
|
||||
/**
|
||||
* Get sampling rate of MP3 file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateMp3(void)
|
||||
{
|
||||
return rate;
|
||||
}
|
||||
|
||||
hidScanInput();
|
||||
kDown = hidKeysDown();
|
||||
/**
|
||||
* Get number of channels of MP3 file.
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelMp3(void)
|
||||
{
|
||||
return channels;
|
||||
}
|
||||
|
||||
if(kDown & KEY_B)
|
||||
break;
|
||||
/**
|
||||
* Decode part of open MP3 file.
|
||||
*
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeMp3(void* buffer)
|
||||
{
|
||||
size_t done = 0;
|
||||
mpg123_read(mh, buffer, *buffSize, &done);
|
||||
return done / (sizeof(int16_t));
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
err = mpg123_read(mh, buffer1, buffer_size, &done);
|
||||
|
||||
if(err != MPG123_OK)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
}
|
||||
|
||||
if(waveBuf[1].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
err = mpg123_read(mh, buffer2, buffer_size, &done);
|
||||
|
||||
if(err != MPG123_OK)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
}
|
||||
|
||||
DSP_FlushDataCache(buffer1, buffer_size);
|
||||
DSP_FlushDataCache(buffer2, buffer_size);
|
||||
}
|
||||
|
||||
out:
|
||||
linearFree(buffer1);
|
||||
linearFree(buffer2);
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
ndspExit();
|
||||
/**
|
||||
* Free MP3 decoder.
|
||||
*/
|
||||
void exitMp3(void)
|
||||
{
|
||||
mpg123_close(mh);
|
||||
mpg123_delete(mh);
|
||||
mpg123_exit();
|
||||
printf("\nStopping MP3 playback.\n");
|
||||
return ret;
|
||||
|
||||
err:
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
42
source/mp3.h
42
source/mp3.h
@@ -1,3 +1,41 @@
|
||||
#include <mpg123.h>
|
||||
/**
|
||||
* Set decoder parameters for MP3.
|
||||
*
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
void setMp3(struct decoder_fn* decoder);
|
||||
|
||||
int playMp3(const char* in);
|
||||
/**
|
||||
* Initialise MP3 decoder.
|
||||
*
|
||||
* \param file Location of MP3 file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initMp3(const char* file);
|
||||
|
||||
/**
|
||||
* Get sampling rate of MP3 file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateMp3(void);
|
||||
|
||||
/**
|
||||
* Get number of channels of MP3 file.
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelMp3(void);
|
||||
|
||||
/**
|
||||
* Decode part of open MP3 file.
|
||||
*
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeMp3(void* buffer);
|
||||
|
||||
/**
|
||||
* Free MP3 decoder.
|
||||
*/
|
||||
void exitMp3(void);
|
||||
|
||||
221
source/opus.c
221
source/opus.c
@@ -5,153 +5,98 @@
|
||||
#include "all.h"
|
||||
#include "opus.h"
|
||||
|
||||
#define SAMPLES_TO_READ (32 * 1024)
|
||||
static OggOpusFile* opusFile;
|
||||
static const OpusHead* opusHead;
|
||||
static const int buffSize = 32 * 1024;
|
||||
|
||||
int playOpus(const char* in)
|
||||
/**
|
||||
* 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;
|
||||
decoder->channels = &channelOpus;
|
||||
decoder->buffSize = buffSize;
|
||||
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.
|
||||
*/
|
||||
uint64_t fillOpusBuffer(OggOpusFile* opusFile, uint64_t samplesToRead,
|
||||
int16_t* bufferOut)
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of channels of Opus file.
|
||||
*
|
||||
* \return Number of channels for opened file, so always be 2.
|
||||
*/
|
||||
uint8_t channelOpus(void)
|
||||
{
|
||||
/* Opus decoder always returns stereo stream */
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode part of open Opus file.
|
||||
*
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeOpus(void* buffer)
|
||||
{
|
||||
return fillOpusBuffer(opusFile, buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free Opus decoder.
|
||||
*/
|
||||
void exitOpus(void)
|
||||
{
|
||||
op_free(opusFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode Opus file to fill buffer.
|
||||
*
|
||||
* \param opusFile File to decode.
|
||||
* \param bufferOut Pointer to buffer.
|
||||
* \return Samples read per channel.
|
||||
*/
|
||||
uint64_t fillOpusBuffer(OggOpusFile* opusFile, int16_t* bufferOut)
|
||||
{
|
||||
uint64_t samplesRead = 0;
|
||||
int samplesToRead = buffSize;
|
||||
|
||||
while(samplesToRead > 0)
|
||||
{
|
||||
|
||||
@@ -1,8 +1,19 @@
|
||||
#include <opus/opusfile.h>
|
||||
|
||||
void setOpus(struct decoder_fn* decoder);
|
||||
|
||||
int initOpus(const char* file);
|
||||
|
||||
uint32_t rateOpus(void);
|
||||
|
||||
uint8_t channelOpus(void);
|
||||
|
||||
uint64_t decodeOpus(void* buffer);
|
||||
|
||||
void exitOpus(void);
|
||||
|
||||
int playOpus(const char* in);
|
||||
|
||||
uint64_t fillOpusBuffer(OggOpusFile* opusFile, uint64_t samplesToRead,
|
||||
int16_t* bufferOut);
|
||||
uint64_t fillOpusBuffer(OggOpusFile* opusFile, int16_t* bufferOut);
|
||||
|
||||
int isOpus(const char* in);
|
||||
|
||||
251
source/playback.c
Normal file
251
source/playback.c
Normal file
@@ -0,0 +1,251 @@
|
||||
#include <3ds.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "all.h"
|
||||
#include "flac.h"
|
||||
#include "mp3.h"
|
||||
#include "opus.h"
|
||||
#include "playback.h"
|
||||
#include "wav.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;
|
||||
int ret;
|
||||
|
||||
switch(getFileType(file))
|
||||
{
|
||||
case FILE_TYPE_WAV:
|
||||
setWav(&decoder);
|
||||
break;
|
||||
|
||||
case FILE_TYPE_FLAC:
|
||||
setFlac(&decoder);
|
||||
break;
|
||||
|
||||
case FILE_TYPE_OPUS:
|
||||
setOpus(&decoder);
|
||||
break;
|
||||
|
||||
case FILE_TYPE_MP3:
|
||||
setMp3(&decoder);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Unsupported File type.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(R_FAILED(ndspInit()))
|
||||
{
|
||||
printf("Initialising ndsp failed.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if((ret = (*decoder.init)(file)) != 0)
|
||||
{
|
||||
printf("Error initialising decoder: %d\n", ret);
|
||||
goto out;
|
||||
}
|
||||
|
||||
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||
buffer2 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||
|
||||
#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,
|
||||
(*decoder.channels)() == 2 ? NDSP_FORMAT_STEREO_PCM16 :
|
||||
NDSP_FORMAT_MONO_PCM16);
|
||||
|
||||
memset(waveBuf, 0, sizeof(waveBuf));
|
||||
waveBuf[0].nsamples = (*decoder.decode)(&buffer1[0]) / (*decoder.channels)();
|
||||
waveBuf[0].data_vaddr = &buffer1[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains file type.
|
||||
*
|
||||
* \param file File location.
|
||||
* \return File type, else negative.
|
||||
*/
|
||||
int getFileType(const char *file)
|
||||
{
|
||||
FILE* ftest = fopen(file, "rb");
|
||||
int fileSig = 0;
|
||||
enum file_types file_type = FILE_TYPE_ERROR;
|
||||
|
||||
if(ftest == NULL)
|
||||
{
|
||||
err_print("Opening file failed.");
|
||||
printf("file: %s\n", file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(fread(&fileSig, 4, 1, ftest) == 0)
|
||||
{
|
||||
err_print("Unable to read file.");
|
||||
fclose(ftest);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch(fileSig)
|
||||
{
|
||||
// "RIFF"
|
||||
case 0x46464952:
|
||||
if(fseek(ftest, 4, SEEK_CUR) != 0)
|
||||
{
|
||||
err_print("Unable to seek.");
|
||||
break;
|
||||
}
|
||||
|
||||
// "WAVE"
|
||||
// Check required as AVI file format also uses "RIFF".
|
||||
if(fread(&fileSig, 4, 1, ftest) == 0)
|
||||
{
|
||||
err_print("Unable to read potential WAV file.");
|
||||
break;
|
||||
}
|
||||
|
||||
if(fileSig != 0x45564157)
|
||||
break;
|
||||
|
||||
file_type = FILE_TYPE_WAV;
|
||||
printf("File type is WAV.");
|
||||
break;
|
||||
|
||||
// "fLaC"
|
||||
case 0x43614c66:
|
||||
file_type = FILE_TYPE_FLAC;
|
||||
printf("File type is FLAC.");
|
||||
break;
|
||||
|
||||
// "OggS"
|
||||
case 0x5367674f:
|
||||
if(isOpus(file) == 0)
|
||||
{
|
||||
printf("\nFile type is Opus.");
|
||||
file_type = FILE_TYPE_OPUS;
|
||||
}
|
||||
else
|
||||
{
|
||||
file_type = FILE_TYPE_OGG;
|
||||
printf("\nUnsupported audio in OGG container.");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
/*
|
||||
* MP3 without ID3 tag, ID3v1 tag is at the end of file, or MP3
|
||||
* with ID3 tag at the beginning of the file.
|
||||
*/
|
||||
if((fileSig << 16) == 0xFBFF0000 || (fileSig << 8) == 0x33444900)
|
||||
{
|
||||
puts("File type is MP3.");
|
||||
file_type = FILE_TYPE_MP3;
|
||||
break;
|
||||
}
|
||||
|
||||
printf("Unknown magic number: %#010x\n.", fileSig);
|
||||
}
|
||||
|
||||
fclose(ftest);
|
||||
return file_type;
|
||||
}
|
||||
18
source/playback.h
Normal file
18
source/playback.h
Normal file
@@ -0,0 +1,18 @@
|
||||
enum file_types {
|
||||
FILE_TYPE_ERROR = -1,
|
||||
FILE_TYPE_WAV,
|
||||
FILE_TYPE_FLAC,
|
||||
FILE_TYPE_OGG,
|
||||
FILE_TYPE_OPUS,
|
||||
FILE_TYPE_MP3
|
||||
};
|
||||
|
||||
int playFile(const char* file);
|
||||
|
||||
/**
|
||||
* Obtains file type.
|
||||
*
|
||||
* \param file File location.
|
||||
* \return File type, else negative.
|
||||
*/
|
||||
int getFileType(const char *file);
|
||||
255
source/wav.c
255
source/wav.c
@@ -6,196 +6,113 @@
|
||||
#include "all.h"
|
||||
#include "wav.h"
|
||||
|
||||
#define BUFFER_SIZE (16 * 1024)
|
||||
static const int buffSize = 16 * 1024;
|
||||
static FILE* pWav = NULL;
|
||||
static char header[45];
|
||||
static uint8_t channels;
|
||||
|
||||
/**
|
||||
* Plays a WAV file.
|
||||
* Set decoder parameters for WAV.
|
||||
*
|
||||
* \param file File location of WAV file.
|
||||
* \return Zero if successful, else failure.
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
int playWav(const char *wav)
|
||||
void setWav(struct decoder_fn* decoder)
|
||||
{
|
||||
FILE* file = fopen(wav, "rb");
|
||||
char header[45];
|
||||
u32 sample;
|
||||
u8 format;
|
||||
u8 channels;
|
||||
u8 bitness;
|
||||
u32 byterate; // TODO: Not used.
|
||||
u32 blockalign;
|
||||
s16* buffer1 = NULL;
|
||||
s16* buffer2 = NULL;
|
||||
ndspWaveBuf waveBuf[2];
|
||||
bool playing = true;
|
||||
bool lastbuf = false;
|
||||
decoder->init = &initWav;
|
||||
decoder->rate = &rateWav;
|
||||
decoder->channels = &channelWav;
|
||||
decoder->buffSize = buffSize;
|
||||
decoder->decode = &readWav;
|
||||
decoder->exit = &exitWav;
|
||||
}
|
||||
|
||||
if(R_FAILED(ndspInit()))
|
||||
{
|
||||
err_print("Initialising ndsp failed.");
|
||||
goto out;
|
||||
}
|
||||
/**
|
||||
* Initialise WAV playback.
|
||||
*
|
||||
* \param file Location of WAV file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initWav(const char* file)
|
||||
{
|
||||
pWav = fopen(file, "rb");
|
||||
|
||||
// TODO: Check if this is required.
|
||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
||||
|
||||
if(file == NULL)
|
||||
{
|
||||
err_print("Opening file failed.");
|
||||
goto out;
|
||||
}
|
||||
if(pWav == NULL)
|
||||
return -1;
|
||||
|
||||
/* TODO: No need to read the first number of bytes */
|
||||
if(fread(header, 1, 44, file) == 0)
|
||||
{
|
||||
err_print("Unable to read WAV file.");
|
||||
goto out;
|
||||
}
|
||||
if(fread(header, 1, 44, pWav) == 0)
|
||||
return -1;
|
||||
|
||||
/**
|
||||
* http://www.topherlee.com/software/pcm-tut-wavformat.html and
|
||||
* http://soundfile.sapp.org/doc/WaveFormat/ helped a lot.
|
||||
* format = (header[19]<<8) + (header[20]);
|
||||
* channels = (header[23]<<8) + (header[22]);
|
||||
* sample = (header[27]<<24) + (header[26]<<16) + (header[25]<<8) +
|
||||
* (header[24]);
|
||||
* byterate = (header[31]<<24) + (header[30]<<16) + (header[29]<<8) +
|
||||
* (header[28]);
|
||||
* blockalign = (header[33]<<8) + (header[32]);
|
||||
* bitness = (header[35]<<8) + (header[34]);
|
||||
*/
|
||||
format = (header[19]<<8) + (header[20]);
|
||||
|
||||
/* TODO: This should be moved to get file type */
|
||||
/* Only support 16 bit PCM WAV */
|
||||
if(((header[35]<<8) + (header[34])) != 16)
|
||||
return -1;
|
||||
|
||||
channels = (header[23]<<8) + (header[22]);
|
||||
sample = (header[27]<<24) + (header[26]<<16) + (header[25]<<8) +
|
||||
(header[24]);
|
||||
byterate = (header[31]<<24) + (header[30]<<16) + (header[29]<<8) +
|
||||
(header[28]);
|
||||
blockalign = (header[33]<<8) + (header[32]);
|
||||
bitness = (header[35]<<8) + (header[34]);
|
||||
#ifdef DEBUG
|
||||
printf("Format: %s(%d), Ch: %d, Sam: %lu, bit: %d, BR: %lu, BA: %lu\n",
|
||||
format == 1 ? "PCM" : "Other", format, channels, sample, bitness,
|
||||
byterate, blockalign);
|
||||
#endif
|
||||
|
||||
if(channels > 2)
|
||||
switch(channels)
|
||||
{
|
||||
puts("Error: Invalid number of channels.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Playing ADPCM, and 8 bit WAV files are disabled as they both sound like
|
||||
* complete garbage.
|
||||
*/
|
||||
switch(bitness)
|
||||
{
|
||||
case 8:
|
||||
bitness = channels == 2 ? NDSP_FORMAT_STEREO_PCM8 :
|
||||
NDSP_FORMAT_MONO_PCM8;
|
||||
puts("8bit playback disabled.");
|
||||
goto out;
|
||||
|
||||
case 16:
|
||||
bitness = channels == 2 ? NDSP_FORMAT_STEREO_PCM16 :
|
||||
NDSP_FORMAT_MONO_PCM16;
|
||||
/* Only Mono and Stereo allowed */
|
||||
case 1:
|
||||
case 2:
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Bitness of %d unsupported.\n", bitness);
|
||||
goto out;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ndspChnReset(CHANNEL);
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
/* Polyphase sounds much better than linear or no interpolation */
|
||||
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
||||
ndspChnSetRate(CHANNEL, sample);
|
||||
ndspChnSetFormat(CHANNEL, bitness);
|
||||
memset(waveBuf, 0, sizeof(waveBuf));
|
||||
|
||||
buffer1 = (s16*) linearAlloc(BUFFER_SIZE);
|
||||
buffer2 = (s16*) linearAlloc(BUFFER_SIZE);
|
||||
|
||||
fread(buffer1, 1, BUFFER_SIZE, file);
|
||||
waveBuf[0].nsamples = BUFFER_SIZE / blockalign;
|
||||
waveBuf[0].data_vaddr = &buffer1[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
|
||||
fread(buffer2, 1, BUFFER_SIZE, file);
|
||||
waveBuf[1].nsamples = BUFFER_SIZE / blockalign;
|
||||
waveBuf[1].data_vaddr = &buffer2[0];
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
|
||||
printf("Playing %s\n", wav);
|
||||
|
||||
/**
|
||||
* 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 = fread(buffer1, 1, BUFFER_SIZE, file);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < BUFFER_SIZE)
|
||||
waveBuf[0].nsamples = read / blockalign;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
}
|
||||
|
||||
if(waveBuf[1].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
read = fread(buffer2, 1, BUFFER_SIZE, file);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < BUFFER_SIZE)
|
||||
waveBuf[1].nsamples = read / blockalign;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
}
|
||||
|
||||
DSP_FlushDataCache(buffer1, BUFFER_SIZE);
|
||||
DSP_FlushDataCache(buffer2, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
ndspChnWaveBufClear(CHANNEL);
|
||||
|
||||
out:
|
||||
puts("Stopping playback.");
|
||||
|
||||
ndspExit();
|
||||
fclose(file);
|
||||
linearFree(buffer1);
|
||||
linearFree(buffer2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sampling rate of Wav file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateWav(void)
|
||||
{
|
||||
return (header[27]<<24) + (header[26]<<16) + (header[25]<<8) +
|
||||
(header[24]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of channels of Wav file.
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelWav(void)
|
||||
{
|
||||
return channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read part of open Wav file.
|
||||
*
|
||||
* \param buffer Output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t readWav(void* buffer)
|
||||
{
|
||||
return fread(buffer, 1, buffSize, pWav) / sizeof(int16_t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free Wav file.
|
||||
*/
|
||||
void exitWav(void)
|
||||
{
|
||||
fclose(pWav);
|
||||
}
|
||||
|
||||
42
source/wav.h
42
source/wav.h
@@ -1 +1,41 @@
|
||||
int playWav(const char *wav);
|
||||
/**
|
||||
* Set decoder parameters for WAV.
|
||||
*
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
void setWav(struct decoder_fn* decoder);
|
||||
|
||||
/**
|
||||
* Initialise WAV playback.
|
||||
*
|
||||
* \param file Location of WAV file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initWav(const char* file);
|
||||
|
||||
/**
|
||||
* Get sampling rate of Wav file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateWav(void);
|
||||
|
||||
/**
|
||||
* Get number of channels of Wav file.
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelWav(void);
|
||||
|
||||
/**
|
||||
* Read part of open Wav file.
|
||||
*
|
||||
* \param buffer Output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t readWav(void* buffer);
|
||||
|
||||
/**
|
||||
* Free Wav file.
|
||||
*/
|
||||
void exitWav(void);
|
||||
|
||||
Reference in New Issue
Block a user