Merge branch 'flac'
This commit is contained in:
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "source/dr_libs"]
|
||||||
|
path = source/dr_libs
|
||||||
|
url = https://github.com/mackron/dr_libs
|
||||||
2
Makefile
2
Makefile
@@ -42,7 +42,7 @@ APP_AUTHOR = Deltabeard
|
|||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
|
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
|
||||||
|
|
||||||
CFLAGS := -g -Wall -Os -mword-relocations \
|
CFLAGS := -g -Wall -O3 -mword-relocations \
|
||||||
-fomit-frame-pointer -ffunction-sections \
|
-fomit-frame-pointer -ffunction-sections \
|
||||||
$(ARCH)
|
$(ARCH)
|
||||||
|
|
||||||
|
|||||||
1
source/dr_libs
Submodule
1
source/dr_libs
Submodule
Submodule source/dr_libs added at 329071a253
128
source/flac.c
Normal file
128
source/flac.c
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
#include <3ds.h>
|
||||||
|
|
||||||
|
#define DR_FLAC_IMPLEMENTATION
|
||||||
|
#include <../source/dr_libs/dr_flac.h>
|
||||||
|
|
||||||
|
#define SAMPLES_TO_READ 16 * 1024
|
||||||
|
#define CHANNEL 0x08
|
||||||
|
|
||||||
|
int playFlac(const char* in)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nRate: %lu\tChan: %d\n", pFlac->sampleRate,
|
||||||
|
pFlac->channels);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if(playing == false || lastbuf == true)
|
||||||
|
{
|
||||||
|
printf("\33[2K\rPaused");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\33[2K\r");
|
||||||
|
|
||||||
|
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
source/flac.h
Normal file
1
source/flac.h
Normal file
@@ -0,0 +1 @@
|
|||||||
|
int playFlac(const char* in);
|
||||||
154
source/main.c
154
source/main.c
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "trivial_example.h"
|
#include "trivial_example.h"
|
||||||
|
#include "flac.h"
|
||||||
|
|
||||||
#define BUFFER_SIZE 16 * 1024
|
#define BUFFER_SIZE 16 * 1024
|
||||||
#define AUDIO_FOLDER "sdmc:/MUSIC/"
|
#define AUDIO_FOLDER "sdmc:/MUSIC/"
|
||||||
@@ -35,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);
|
||||||
@@ -83,7 +92,6 @@ int main(int argc, char **argv)
|
|||||||
while(aptMainLoop())
|
while(aptMainLoop())
|
||||||
{
|
{
|
||||||
u32 kDown;
|
u32 kDown;
|
||||||
char* file = NULL;
|
|
||||||
|
|
||||||
hidScanInput();
|
hidScanInput();
|
||||||
|
|
||||||
@@ -97,21 +105,16 @@ int main(int argc, char **argv)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
if(kDown & KEY_UP && fileNum < fileMax)
|
if(kDown & KEY_UP && fileNum < fileMax)
|
||||||
{
|
printf("\33[2K\rSelected file %d", ++fileNum);
|
||||||
fileNum++;
|
|
||||||
printf("\rSelected file %d ", fileNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(kDown & KEY_DOWN && fileNum > 1)
|
if(kDown & KEY_DOWN && fileNum > 1)
|
||||||
{
|
printf("\33[2K\rSelected file %d", --fileNum);
|
||||||
fileNum--;
|
|
||||||
printf("\rSelected file %d ", fileNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(kDown & (KEY_A | KEY_R))
|
if(kDown & (KEY_A | KEY_R))
|
||||||
{
|
{
|
||||||
u8 audioFileNum = 0;
|
u8 audioFileNum = 0;
|
||||||
dp = opendir(AUDIO_FOLDER);
|
dp = opendir(AUDIO_FOLDER);
|
||||||
|
char* file = NULL;
|
||||||
|
|
||||||
if (dp != NULL)
|
if (dp != NULL)
|
||||||
{
|
{
|
||||||
@@ -125,36 +128,33 @@ int main(int argc, char **argv)
|
|||||||
if(closedir(dp) != 0)
|
if(closedir(dp) != 0)
|
||||||
err_print("Closing directory failed.");
|
err_print("Closing directory failed.");
|
||||||
|
|
||||||
|
/* TODO: There is an issue with Unicode files here.
|
||||||
|
* Playing a flac file then a file with a name containing the
|
||||||
|
* character 'é' will cause the asprint to not work properly. */
|
||||||
if(asprintf(&file, "%s%s", AUDIO_FOLDER, ep->d_name) == -1)
|
if(asprintf(&file, "%s%s", AUDIO_FOLDER, ep->d_name) == -1)
|
||||||
{
|
{
|
||||||
err_print("Constructing file name failed.");
|
err_print("Constructing file name failed.");
|
||||||
file = NULL;
|
file = NULL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(file == NULL)
|
|
||||||
err_print("Opening file failed.");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char* ext = strrchr(file, '.');
|
|
||||||
|
|
||||||
if(ext == NULL)
|
|
||||||
printf("\nUnable to obtain file type.");
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* To skip the dot */
|
switch(getFileType(file))
|
||||||
ext++;
|
{
|
||||||
|
case FILE_TYPE_WAV:
|
||||||
|
playWav(file);
|
||||||
|
break;
|
||||||
|
|
||||||
// TODO: Don't rely on file extension.
|
case FILE_TYPE_FLAC:
|
||||||
if(strncasecmp(ext, "opus", 4) == 0)
|
playFlac(file);
|
||||||
convOpus(file, "sdmc:/MUSIC/out.wav");
|
break;
|
||||||
else if(strncasecmp(ext, "wav", 3) == 0 ||
|
|
||||||
strncasecmp(ext, "aiff", 4) == 0)
|
default:
|
||||||
playWav(file);
|
printf("Unsupported File type.\n");
|
||||||
else
|
}
|
||||||
printf("\nFile type \"%s\" not recognised.", ext);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
err_print("Unable to open directory.");
|
||||||
|
|
||||||
free(file);
|
free(file);
|
||||||
}
|
}
|
||||||
@@ -167,6 +167,74 @@ 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(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:
|
||||||
|
file_type = FILE_TYPE_OGG;
|
||||||
|
printf("\nFile type is OGG.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(ftest);
|
||||||
|
return file_type;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays a WAV file.
|
* Plays a WAV file.
|
||||||
*
|
*
|
||||||
@@ -175,7 +243,7 @@ out:
|
|||||||
*/
|
*/
|
||||||
int playWav(const char *wav)
|
int playWav(const char *wav)
|
||||||
{
|
{
|
||||||
FILE *file = fopen(wav, "rb");
|
FILE* file = fopen(wav, "rb");
|
||||||
char header[45];
|
char header[45];
|
||||||
u32 sample;
|
u32 sample;
|
||||||
u8 format;
|
u8 format;
|
||||||
@@ -204,20 +272,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.
|
||||||
@@ -272,12 +333,13 @@ int playWav(const char *wav)
|
|||||||
buffer2 = (s16*) linearAlloc(BUFFER_SIZE);
|
buffer2 = (s16*) linearAlloc(BUFFER_SIZE);
|
||||||
|
|
||||||
fread(buffer1, 1, BUFFER_SIZE, file);
|
fread(buffer1, 1, BUFFER_SIZE, file);
|
||||||
fread(buffer2, 1, BUFFER_SIZE, file);
|
|
||||||
waveBuf[0].nsamples = BUFFER_SIZE / blockalign;
|
waveBuf[0].nsamples = BUFFER_SIZE / blockalign;
|
||||||
waveBuf[0].data_vaddr = &buffer1[0];
|
waveBuf[0].data_vaddr = &buffer1[0];
|
||||||
|
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||||
|
|
||||||
|
fread(buffer2, 1, BUFFER_SIZE, file);
|
||||||
waveBuf[1].nsamples = BUFFER_SIZE / blockalign;
|
waveBuf[1].nsamples = BUFFER_SIZE / blockalign;
|
||||||
waveBuf[1].data_vaddr = &buffer2[0];
|
waveBuf[1].data_vaddr = &buffer2[0];
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||||
|
|
||||||
printf("Playing %s\n", wav);
|
printf("Playing %s\n", wav);
|
||||||
@@ -307,7 +369,7 @@ int playWav(const char *wav)
|
|||||||
if(kDown & KEY_B)
|
if(kDown & KEY_B)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if(kDown & KEY_A)
|
if(kDown & (KEY_A | KEY_R))
|
||||||
playing = !playing;
|
playing = !playing;
|
||||||
|
|
||||||
if(playing == false || lastbuf == true)
|
if(playing == false || lastbuf == true)
|
||||||
@@ -348,12 +410,6 @@ int playWav(const char *wav)
|
|||||||
|
|
||||||
DSP_FlushDataCache(buffer1, BUFFER_SIZE);
|
DSP_FlushDataCache(buffer1, BUFFER_SIZE);
|
||||||
DSP_FlushDataCache(buffer2, BUFFER_SIZE);
|
DSP_FlushDataCache(buffer2, BUFFER_SIZE);
|
||||||
|
|
||||||
// TODO: Remove this printf.
|
|
||||||
// \33[2K clears the current line.
|
|
||||||
printf("\33[2K\rSamp: %lu\tBuf0: %s\tBuf1: %s", read / blockalign,
|
|
||||||
waveBuf[0].status == NDSP_WBUF_QUEUED ? "Q" : "P",
|
|
||||||
waveBuf[1].status == NDSP_WBUF_QUEUED ? "Q" : "P");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debug_print("Pos: %lx\n", ndspChnGetSamplePos(CHANNEL));
|
debug_print("Pos: %lx\n", ndspChnGetSamplePos(CHANNEL));
|
||||||
|
|||||||
@@ -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.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user