16-bit flac decoding and improve file type checker

Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
Mahyar Koshkouei
2016-12-26 12:58:01 +00:00
parent b0a1eb8283
commit ee1f4bfe20
4 changed files with 105 additions and 36 deletions

View File

@@ -7,7 +7,7 @@
#define DR_FLAC_IMPLEMENTATION #define DR_FLAC_IMPLEMENTATION
#include <../source/dr_libs/dr_flac.h> #include <../source/dr_libs/dr_flac.h>
#define SAMPLES_TO_READ 256 * 1024 #define SAMPLES_TO_READ 128 * 1024
#define CHANNEL 0x08 #define CHANNEL 0x08
int playFlac(const char* in) int playFlac(const char* in)
@@ -43,10 +43,10 @@ int playFlac(const char* in)
ndspChnSetRate(CHANNEL, pFlac->sampleRate); ndspChnSetRate(CHANNEL, pFlac->sampleRate);
ndspChnSetFormat(CHANNEL, pFlac->channels == 2 ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16); ndspChnSetFormat(CHANNEL, pFlac->channels == 2 ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16);
memset(waveBuf, 0, sizeof(waveBuf)); memset(waveBuf, 0, sizeof(waveBuf));
waveBuf[0].nsamples = drflac_read_s32(pFlac, chunkSize, buffer1); waveBuf[0].nsamples = drflac_read_s16(pFlac, chunkSize, buffer1) / pFlac->channels;
waveBuf[0].data_vaddr = &buffer1[0]; waveBuf[0].data_vaddr = &buffer1[0];
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]); ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
waveBuf[1].nsamples = drflac_read_s32(pFlac, chunkSize, buffer2); waveBuf[1].nsamples = drflac_read_s16(pFlac, chunkSize, buffer2) / pFlac->channels;
waveBuf[1].data_vaddr = &buffer2[0]; waveBuf[1].data_vaddr = &buffer2[0];
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]); ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
@@ -87,7 +87,7 @@ int playFlac(const char* in)
if(waveBuf[0].status == NDSP_WBUF_DONE) if(waveBuf[0].status == NDSP_WBUF_DONE)
{ {
read = drflac_read_s32(pFlac, chunkSize, buffer1); read = drflac_read_s16(pFlac, chunkSize, buffer1);
if(read == 0) if(read == 0)
{ {
@@ -95,14 +95,14 @@ int playFlac(const char* in)
continue; continue;
} }
else if(read < chunkSize) else if(read < chunkSize)
waveBuf[0].nsamples = read; waveBuf[0].nsamples = read / pFlac->channels;
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]); ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
} }
if(waveBuf[1].status == NDSP_WBUF_DONE) if(waveBuf[1].status == NDSP_WBUF_DONE)
{ {
read = drflac_read_s32(pFlac, chunkSize, buffer2); read = drflac_read_s16(pFlac, chunkSize, buffer2);
if(read == 0) if(read == 0)
{ {
@@ -110,7 +110,7 @@ int playFlac(const char* in)
continue; continue;
} }
else if(read < chunkSize) else if(read < chunkSize)
waveBuf[1].nsamples = read; waveBuf[1].nsamples = read / pFlac->channels;
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]); ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
} }
@@ -120,7 +120,7 @@ int playFlac(const char* in)
// TODO: Remove this printf. // TODO: Remove this printf.
// \33[2K clears the current line. // \33[2K clears the current line.
printf("\33[2K\rRead: %lu\tBuf0: %s\tBuf1: %s", read, printf("\33[2K\rRead: %u\tBuf0: %s\tBuf1: %s", read,
waveBuf[0].status == NDSP_WBUF_QUEUED ? "Q" : "P", waveBuf[0].status == NDSP_WBUF_QUEUED ? "Q" : "P",
waveBuf[1].status == NDSP_WBUF_QUEUED ? "Q" : "P"); waveBuf[1].status == NDSP_WBUF_QUEUED ? "Q" : "P");
} }

View File

@@ -36,14 +36,22 @@
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)
enum file_types {
FILE_TYPE_ERROR = -1,
FILE_TYPE_WAV,
FILE_TYPE_FLAC,
FILE_TYPE_OGG,
FILE_TYPE_OPUS
};
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
DIR *dp; DIR *dp;
struct dirent *ep; struct dirent *ep;
PrintConsole topScreen; PrintConsole topScreen;
PrintConsole bottomScreen; PrintConsole bottomScreen;
u8 fileMax = 0; u8 fileMax = 0;
u8 fileNum = 1; u8 fileNum = 1;
gfxInitDefault(); gfxInitDefault();
consoleInit(GFX_TOP, &topScreen); consoleInit(GFX_TOP, &topScreen);
@@ -137,25 +145,18 @@ int main(int argc, char **argv)
err_print("Opening file failed."); err_print("Opening file failed.");
else else
{ {
char* ext = strrchr(file, '.'); switch(getFileType(file))
if(ext == NULL)
printf("\nUnable to obtain file type.");
else
{ {
/* To skip the dot */ case FILE_TYPE_WAV:
ext++;
// TODO: Don't rely on file extension.
if(strncasecmp(ext, "opus", 4) == 0)
convOpus(file, "sdmc:/MUSIC/out.wav");
else if(strncasecmp(ext, "flac", 4) == 0)
playFlac(file);
else if(strncasecmp(ext, "wav", 3) == 0 ||
strncasecmp(ext, "aiff", 4) == 0)
playWav(file); playWav(file);
else break;
printf("\nFile type \"%s\" not recognised.", ext);
case FILE_TYPE_FLAC:
playFlac(file);
break;
default:
printf("Unsupported File type.\n");
} }
} }
@@ -170,6 +171,73 @@ out:
return 0; 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(file == NULL)
{
err_print("Opening file failed.");
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("\nFile type is WAV.");
break;
// "fLaC"
case 0x43614c66:
file_type = FILE_TYPE_FLAC;
printf("\nFile type is FLAC.");
break;
// "OggS"
case 0x5367674f:
file_type = FILE_TYPE_OGG;
printf("\nFile type is OGG.");
break;
}
fclose(ftest);
return file_type;
}
/** /**
* Plays a WAV file. * Plays a WAV file.
* *
@@ -207,20 +275,13 @@ int playWav(const char *wav)
goto out; goto out;
} }
/* TODO: No need to read the first number of bytes */
if(fread(header, 1, 44, file) == 0) if(fread(header, 1, 44, file) == 0)
{ {
err_print("Unable to read WAV file."); err_print("Unable to read WAV file.");
goto out; goto out;
} }
if(strncmp(header + 8, "WAVE", 4) == 0)
puts("Valid WAV file.");
else
{
puts("Invalid 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.

View File

@@ -7,6 +7,14 @@
* LICENSE file. * LICENSE file.
*/ */
/**
* Obtains file type.
*
* \param file File location.
* \return File type, else negative.
*/
int getFileType(const char *file);
/** /**
* Plays a WAV file. * Plays a WAV file.
* *