Merge branch 'refactor'
This commit is contained in:
@@ -36,7 +36,6 @@ Start = Return to Homebrew menu (Only when stopped playing).
|
|||||||
### Planned features
|
### Planned features
|
||||||
* Playlist support.
|
* Playlist support.
|
||||||
* Repeat and shuffle support.
|
* Repeat and shuffle support.
|
||||||
* OGG file support.
|
|
||||||
* Metadata support.
|
* Metadata support.
|
||||||
* Gain 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) \
|
#define err_print(err) \
|
||||||
do { fprintf(stderr, "\nError %d:%s(): %s %s\n", __LINE__, __func__, \
|
do { fprintf(stderr, "\nError %d:%s(): %s %s\n", __LINE__, __func__, \
|
||||||
err, strerror(errno)); } while (0)
|
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
|
#define DR_FLAC_IMPLEMENTATION
|
||||||
#include <./dr_libs/dr_flac.h>
|
#include <./dr_libs/dr_flac.h>
|
||||||
#include "all.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);
|
decoder->init = &initFlac;
|
||||||
s16* buffer1 = linearAlloc(SAMPLES_TO_READ * sizeof(s16));
|
decoder->rate = &rateFlac;
|
||||||
s16* buffer2 = linearAlloc(SAMPLES_TO_READ * sizeof(s16));
|
decoder->channels = &channelFlac;
|
||||||
ndspWaveBuf waveBuf[2];
|
decoder->buffSize = buffSize;
|
||||||
bool playing = true;
|
decoder->decode = &decodeFlac;
|
||||||
bool lastbuf = false;
|
decoder->exit = &exitFlac;
|
||||||
|
}
|
||||||
if (pFlac == NULL) {
|
|
||||||
return -1;
|
/**
|
||||||
}
|
* Initialise Flac decoder.
|
||||||
|
*
|
||||||
if(R_FAILED(ndspInit()))
|
* \param file Location of flac file to play.
|
||||||
{
|
* \return 0 on success, else failure.
|
||||||
printf("Initialising ndsp failed.");
|
*/
|
||||||
goto out;
|
int initFlac(const char* file)
|
||||||
}
|
{
|
||||||
|
pFlac = drflac_open_file(file);
|
||||||
#ifdef DEBUG
|
|
||||||
printf("\nRate: %lu\tChan: %d\n", pFlac->sampleRate,
|
return pFlac == NULL ? -1 : 0;
|
||||||
pFlac->channels);
|
}
|
||||||
#endif
|
|
||||||
|
/**
|
||||||
ndspChnReset(CHANNEL);
|
* Get sampling rate of Flac file.
|
||||||
ndspChnWaveBufClear(CHANNEL);
|
*
|
||||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
* \return Sampling rate.
|
||||||
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
*/
|
||||||
ndspChnSetRate(CHANNEL, pFlac->sampleRate);
|
uint32_t rateFlac(void)
|
||||||
ndspChnSetFormat(CHANNEL, pFlac->channels == 2 ? NDSP_FORMAT_STEREO_PCM16 :
|
{
|
||||||
NDSP_FORMAT_MONO_PCM16);
|
return pFlac->sampleRate;
|
||||||
|
}
|
||||||
memset(waveBuf, 0, sizeof(waveBuf));
|
|
||||||
waveBuf[0].nsamples =
|
/**
|
||||||
drflac_read_s16(pFlac, SAMPLES_TO_READ, buffer1) / pFlac->channels;
|
* Get number of channels of Flac file.
|
||||||
waveBuf[0].data_vaddr = &buffer1[0];
|
*
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
* \return Number of channels for opened file.
|
||||||
|
*/
|
||||||
waveBuf[1].nsamples =
|
uint8_t channelFlac(void)
|
||||||
drflac_read_s16(pFlac, SAMPLES_TO_READ, buffer2) / pFlac->channels;
|
{
|
||||||
waveBuf[1].data_vaddr = &buffer2[0];
|
return pFlac->channels;
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
}
|
||||||
|
|
||||||
printf("Playing %s\n", in);
|
/**
|
||||||
/**
|
* Decode part of open Flac 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.
|
* \param buffer Decoded output.
|
||||||
*/
|
* \return Samples read for each channel.
|
||||||
while(ndspChnIsPlaying(CHANNEL) == false);
|
*/
|
||||||
|
uint64_t decodeFlac(void* buffer)
|
||||||
while(playing == false || ndspChnIsPlaying(CHANNEL) == true)
|
{
|
||||||
{
|
return drflac_read_s16(pFlac, buffSize, buffer);
|
||||||
u32 kDown;
|
}
|
||||||
|
|
||||||
/* Number of bytes read from file.
|
/**
|
||||||
* Static only for the purposes of the printf debug at the bottom.
|
* Free Flac decoder.
|
||||||
*/
|
*/
|
||||||
static size_t read = 0;
|
void exitFlac(void)
|
||||||
|
{
|
||||||
gfxSwapBuffers();
|
drflac_close(pFlac);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 <unistd.h>
|
||||||
|
|
||||||
#include "all.h"
|
#include "all.h"
|
||||||
#include "flac.h"
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "mp3.h"
|
#include "playback.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
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@@ -86,7 +69,10 @@ int main(int argc, char **argv)
|
|||||||
kHeld = hidKeysHeld();
|
kHeld = hidKeysHeld();
|
||||||
|
|
||||||
if(kDown & KEY_START)
|
if(kDown & KEY_START)
|
||||||
|
{
|
||||||
|
puts("Test.");
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
consoleSelect(&topScreen);
|
consoleSelect(&topScreen);
|
||||||
@@ -204,32 +190,10 @@ int main(int argc, char **argv)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
consoleSelect(&topScreen);
|
consoleSelect(&topScreen);
|
||||||
switch(getFileType(file))
|
playFile(file);
|
||||||
{
|
consoleSelect(&bottomScreen);
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
consoleSelect(&bottomScreen);
|
|
||||||
|
|
||||||
free(file);
|
free(file);
|
||||||
free(wd);
|
free(wd);
|
||||||
|
|
||||||
@@ -240,6 +204,11 @@ int main(int argc, char **argv)
|
|||||||
err_print("Unable to open directory.");
|
err_print("Unable to open directory.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#ifdef DEBUG
|
||||||
|
consoleSelect(&topScreen);
|
||||||
|
printf("\rNum: %d, Max: %d, from: %d ", fileNum, fileMax, from);
|
||||||
|
consoleSelect(&bottomScreen);
|
||||||
|
#endif
|
||||||
|
|
||||||
out:
|
out:
|
||||||
puts("Exiting...");
|
puts("Exiting...");
|
||||||
@@ -284,7 +253,7 @@ int listDir(int from, int max, int select)
|
|||||||
if(wd == NULL)
|
if(wd == NULL)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
printf("Dir: %.30s\n", wd);
|
printf("Dir: %.33s\n", wd);
|
||||||
|
|
||||||
if((dp = opendir(wd)) == NULL)
|
if((dp = opendir(wd)) == NULL)
|
||||||
goto err;
|
goto err;
|
||||||
@@ -357,94 +326,3 @@ err:
|
|||||||
ret = -1;
|
ret = -1;
|
||||||
goto out;
|
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.
|
* 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
|
* Get number of files in current working folder
|
||||||
*
|
*
|
||||||
@@ -24,11 +30,3 @@ int getNumberFiles(void);
|
|||||||
* \return Number of entries listed or negative on error.
|
* \return Number of entries listed or negative on error.
|
||||||
*/
|
*/
|
||||||
int listDir(int from, int max, int select);
|
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 <3ds.h>
|
||||||
|
#include <mpg123.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -6,154 +7,112 @@
|
|||||||
#include "all.h"
|
#include "all.h"
|
||||||
#include "mp3.h"
|
#include "mp3.h"
|
||||||
|
|
||||||
int playMp3(const char* in)
|
static int* buffSize;
|
||||||
{
|
static mpg123_handle *mh = NULL;
|
||||||
int err = 0;
|
static uint32_t rate;
|
||||||
mpg123_handle *mh = NULL;
|
static uint8_t channels;
|
||||||
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;
|
|
||||||
|
|
||||||
if(R_FAILED(ndspInit()))
|
/**
|
||||||
{
|
* Set decoder parameters for MP3.
|
||||||
printf("Initialising ndsp failed.");
|
*
|
||||||
goto out;
|
* \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)
|
if((err = mpg123_init()) != MPG123_OK)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
if((mh = mpg123_new(NULL, &err)) == NULL)
|
if((mh = mpg123_new(NULL, &err)) == NULL)
|
||||||
{
|
{
|
||||||
printf("Error: %s\n", mpg123_plain_strerror(err));
|
//printf("Error: %s\n", mpg123_plain_strerror(err));
|
||||||
goto err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mpg123_open(mh, in) != MPG123_OK ||
|
if(mpg123_open(mh, file) != MPG123_OK ||
|
||||||
mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK)
|
mpg123_getformat(mh, (long *) &rate, (int *) &channels, &encoding) != MPG123_OK)
|
||||||
{
|
{
|
||||||
printf("Trouble with mpg123: %s\n", mpg123_strerror(mh));
|
//printf("Trouble with mpg123: %s\n", mpg123_strerror(mh));
|
||||||
goto err;
|
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_none(mh);
|
||||||
mpg123_format(mh, rate, channels, encoding);
|
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 could be almost any size here, mpg123_outblock() is just some
|
||||||
buffer_size = mpg123_outblock(mh) * 16;
|
* recommendation. The size should be a multiple of the PCM frame size.
|
||||||
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.
|
|
||||||
*/
|
*/
|
||||||
while(ndspChnIsPlaying(CHANNEL) == false);
|
*buffSize = mpg123_outblock(mh) * 16;
|
||||||
|
|
||||||
while(playing == false || ndspChnIsPlaying(CHANNEL) == true)
|
return 0;
|
||||||
{
|
}
|
||||||
u32 kDown;
|
|
||||||
|
|
||||||
gfxSwapBuffers();
|
/**
|
||||||
gfxFlushBuffers();
|
* Get sampling rate of MP3 file.
|
||||||
gspWaitForVBlank();
|
*
|
||||||
|
* \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))
|
/**
|
||||||
{
|
* Free MP3 decoder.
|
||||||
playing = !playing;
|
*/
|
||||||
printf("\33[2K\r%s", playing == false ? "Paused" : "");
|
void exitMp3(void)
|
||||||
}
|
{
|
||||||
|
|
||||||
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();
|
|
||||||
mpg123_close(mh);
|
mpg123_close(mh);
|
||||||
mpg123_delete(mh);
|
mpg123_delete(mh);
|
||||||
mpg123_exit();
|
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 "all.h"
|
||||||
#include "opus.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;
|
decoder->init = &initOpus;
|
||||||
int16_t* buffer1 = linearAlloc(SAMPLES_TO_READ * sizeof(int16_t));
|
decoder->rate = &rateOpus;
|
||||||
int16_t* buffer2 = linearAlloc(SAMPLES_TO_READ * sizeof(int16_t));
|
decoder->channels = &channelOpus;
|
||||||
ndspWaveBuf waveBuf[2];
|
decoder->buffSize = buffSize;
|
||||||
bool playing = true;
|
decoder->decode = &decodeOpus;
|
||||||
bool lastbuf = false;
|
decoder->exit = &exitOpus;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fill a buffer with decoded samples.
|
* Initialise Opus decoder.
|
||||||
*
|
*
|
||||||
* \param opusFile OggOpusFile pointer.
|
* \param file Location of opus file to play.
|
||||||
* \param samplesToRead Number of samples to read in to buffer.
|
* \return 0 on success, else failure.
|
||||||
* \param bufferOut Pointer to output buffer.
|
|
||||||
* \return Number of samples read per channel.
|
|
||||||
*/
|
*/
|
||||||
uint64_t fillOpusBuffer(OggOpusFile* opusFile, uint64_t samplesToRead,
|
int initOpus(const char* file)
|
||||||
int16_t* bufferOut)
|
{
|
||||||
|
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;
|
uint64_t samplesRead = 0;
|
||||||
|
int samplesToRead = buffSize;
|
||||||
|
|
||||||
while(samplesToRead > 0)
|
while(samplesToRead > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,8 +1,19 @@
|
|||||||
#include <opus/opusfile.h>
|
#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);
|
int playOpus(const char* in);
|
||||||
|
|
||||||
uint64_t fillOpusBuffer(OggOpusFile* opusFile, uint64_t samplesToRead,
|
uint64_t fillOpusBuffer(OggOpusFile* opusFile, int16_t* bufferOut);
|
||||||
int16_t* bufferOut);
|
|
||||||
|
|
||||||
int isOpus(const char* in);
|
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 "all.h"
|
||||||
#include "wav.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.
|
* \param decoder Structure to store parameters.
|
||||||
* \return Zero if successful, else failure.
|
|
||||||
*/
|
*/
|
||||||
int playWav(const char *wav)
|
void setWav(struct decoder_fn* decoder)
|
||||||
{
|
{
|
||||||
FILE* file = fopen(wav, "rb");
|
decoder->init = &initWav;
|
||||||
char header[45];
|
decoder->rate = &rateWav;
|
||||||
u32 sample;
|
decoder->channels = &channelWav;
|
||||||
u8 format;
|
decoder->buffSize = buffSize;
|
||||||
u8 channels;
|
decoder->decode = &readWav;
|
||||||
u8 bitness;
|
decoder->exit = &exitWav;
|
||||||
u32 byterate; // TODO: Not used.
|
}
|
||||||
u32 blockalign;
|
|
||||||
s16* buffer1 = NULL;
|
|
||||||
s16* buffer2 = NULL;
|
|
||||||
ndspWaveBuf waveBuf[2];
|
|
||||||
bool playing = true;
|
|
||||||
bool lastbuf = false;
|
|
||||||
|
|
||||||
if(R_FAILED(ndspInit()))
|
/**
|
||||||
{
|
* Initialise WAV playback.
|
||||||
err_print("Initialising ndsp failed.");
|
*
|
||||||
goto out;
|
* \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.
|
if(pWav == NULL)
|
||||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
return -1;
|
||||||
|
|
||||||
if(file == NULL)
|
|
||||||
{
|
|
||||||
err_print("Opening file failed.");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO: No need to read the first number of bytes */
|
/* TODO: No need to read the first number of bytes */
|
||||||
if(fread(header, 1, 44, file) == 0)
|
if(fread(header, 1, 44, pWav) == 0)
|
||||||
{
|
return -1;
|
||||||
err_print("Unable to read WAV file.");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* http://www.topherlee.com/software/pcm-tut-wavformat.html and
|
* http://www.topherlee.com/software/pcm-tut-wavformat.html and
|
||||||
* http://soundfile.sapp.org/doc/WaveFormat/ helped a lot.
|
* 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]);
|
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.");
|
/* Only Mono and Stereo allowed */
|
||||||
goto out;
|
case 1:
|
||||||
}
|
case 2:
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
printf("Bitness of %d unsupported.\n", bitness);
|
return -1;
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
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