Add flac support

Not working properly due to decoding flac in to 32 bit buffer instead of
16 bit.

Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
Mahyar Koshkouei
2016-12-21 20:25:26 +00:00
parent a6c43d6a7c
commit b0a1eb8283
5 changed files with 160 additions and 8 deletions

138
source/flac.c Normal file
View File

@@ -0,0 +1,138 @@
#include <3ds.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <errno.h>
//#include <string.h>
#define DR_FLAC_IMPLEMENTATION
#include <../source/dr_libs/dr_flac.h>
#define SAMPLES_TO_READ 256 * 1024
#define CHANNEL 0x08
int playFlac(const char* in)
{
drflac* pFlac = drflac_open_file(in);
/* Number of samples actually read */
uint64_t nsamples = 0;
int chunkSize = SAMPLES_TO_READ * sizeof(s16);
s16* buffer1 = linearAlloc(SAMPLES_TO_READ * sizeof(s16));
s16* buffer2 = linearAlloc(SAMPLES_TO_READ * sizeof(s16));
uint64_t readsamples = 0;
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_s32(pFlac, chunkSize, buffer1);
waveBuf[0].data_vaddr = &buffer1[0];
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
waveBuf[1].nsamples = drflac_read_s32(pFlac, chunkSize, buffer2);
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)
playing = !playing;
if(playing == false || lastbuf == true)
{
printf("\33[2K\rPaused");
continue;
}
if(waveBuf[0].status == NDSP_WBUF_DONE)
{
read = drflac_read_s32(pFlac, chunkSize, buffer1);
if(read == 0)
{
lastbuf = true;
continue;
}
else if(read < chunkSize)
waveBuf[0].nsamples = read;
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
}
if(waveBuf[1].status == NDSP_WBUF_DONE)
{
read = drflac_read_s32(pFlac, chunkSize, buffer2);
if(read == 0)
{
lastbuf = true;
continue;
}
else if(read < chunkSize)
waveBuf[1].nsamples = read;
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
}
DSP_FlushDataCache(buffer1, chunkSize);
DSP_FlushDataCache(buffer2, chunkSize);
// TODO: Remove this printf.
// \33[2K clears the current line.
printf("\33[2K\rRead: %lu\tBuf0: %s\tBuf1: %s", read,
waveBuf[0].status == NDSP_WBUF_QUEUED ? "Q" : "P",
waveBuf[1].status == NDSP_WBUF_QUEUED ? "Q" : "P");
}
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
View File

@@ -0,0 +1 @@
int playFlac(const char* in);

View File

@@ -18,6 +18,7 @@
#include "main.h"
#include "trivial_example.h"
#include "flac.h"
#define BUFFER_SIZE 16 * 1024
#define AUDIO_FOLDER "sdmc:/MUSIC/"
@@ -136,12 +137,26 @@ int main(int argc, char **argv)
err_print("Opening file failed.");
else
{
int ret;
// TODO: make this dynamic
if((ret = convOpus(file, "sdmc:/MUSIC/out.wav")) != 0)
playWav(file);
char* ext = strrchr(file, '.');
printf("ret=%d\n", ret);
if(ext == NULL)
printf("\nUnable to obtain file type.");
else
{
/* To skip the dot */
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);
else
printf("\nFile type \"%s\" not recognised.", ext);
}
}
free(file);

View File

@@ -14,5 +14,3 @@
* \return Zero if successful, else failure.
*/
int playWav(const char *wav);
int playOpus(const char *opus);