Fix Flac playing
Fixed an issue whereby the size of the buffer was incorrect causing malformed audio output when playing a flac music file. Additionally removed some debugging code and old chaff. Tested working with stereo and mono 16 bit flac files. Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
@@ -1,27 +1,19 @@
|
||||
#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 128 * 1024
|
||||
#define SAMPLES_TO_READ 16 * 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;
|
||||
bool playing = true;
|
||||
bool lastbuf = false;
|
||||
|
||||
if (pFlac == NULL) {
|
||||
return -1;
|
||||
@@ -42,13 +34,15 @@ int playFlac(const char* in)
|
||||
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, chunkSize, buffer1) / pFlac->channels;
|
||||
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, chunkSize, buffer2) / pFlac->channels;
|
||||
//waveBuf[1].data_vaddr = &buffer2[0];
|
||||
//ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
|
||||
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);
|
||||
/**
|
||||
@@ -58,7 +52,7 @@ int playFlac(const char* in)
|
||||
while(ndspChnIsPlaying(CHANNEL) == false)
|
||||
{}
|
||||
|
||||
while(true || playing == false || ndspChnIsPlaying(CHANNEL) == true)
|
||||
while(playing == false || ndspChnIsPlaying(CHANNEL) == true)
|
||||
{
|
||||
u32 kDown;
|
||||
/* Number of bytes read from file.
|
||||
@@ -76,7 +70,7 @@ int playFlac(const char* in)
|
||||
if(kDown & KEY_B)
|
||||
break;
|
||||
|
||||
if(kDown & KEY_A)
|
||||
if(kDown & (KEY_A | KEY_R))
|
||||
playing = !playing;
|
||||
|
||||
if(playing == false || lastbuf == true)
|
||||
@@ -85,47 +79,40 @@ int playFlac(const char* in)
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("\33[2K\r");
|
||||
|
||||
if(waveBuf[0].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
printf("\rBeginning Decode.");
|
||||
read = drflac_read_s16(pFlac, chunkSize, buffer1);
|
||||
printf("\rDone decode.");
|
||||
read = drflac_read_s16(pFlac, SAMPLES_TO_READ, buffer1);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < chunkSize)
|
||||
else if(read < SAMPLES_TO_READ)
|
||||
waveBuf[0].nsamples = read / pFlac->channels;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||
}
|
||||
|
||||
#if 0
|
||||
if(waveBuf[1].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
read = drflac_read_s16(pFlac, chunkSize, buffer2);
|
||||
read = drflac_read_s16(pFlac, SAMPLES_TO_READ, buffer2);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
lastbuf = true;
|
||||
continue;
|
||||
}
|
||||
else if(read < chunkSize)
|
||||
else if(read < SAMPLES_TO_READ)
|
||||
waveBuf[1].nsamples = read / pFlac->channels;
|
||||
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
}
|
||||
// TODO: Remove this printf.
|
||||
// \33[2K clears the current line.
|
||||
printf("\33[2K\rRead: %u\tBuf0: %s\tBuf1: %s", read,
|
||||
waveBuf[0].status == NDSP_WBUF_QUEUED ? "Q" : "P",
|
||||
waveBuf[1].status == NDSP_WBUF_QUEUED ? "Q" : "P");
|
||||
#endif
|
||||
|
||||
DSP_FlushDataCache(buffer1, chunkSize);
|
||||
DSP_FlushDataCache(buffer2, chunkSize);
|
||||
DSP_FlushDataCache(buffer1, SAMPLES_TO_READ * sizeof(s16));
|
||||
DSP_FlushDataCache(buffer2, SAMPLES_TO_READ * sizeof(s16));
|
||||
}
|
||||
|
||||
printf("\nEnd of file.");
|
||||
|
||||
@@ -243,7 +243,7 @@ int getFileType(const char *file)
|
||||
*/
|
||||
int playWav(const char *wav)
|
||||
{
|
||||
FILE *file = fopen(wav, "rb");
|
||||
FILE* file = fopen(wav, "rb");
|
||||
char header[45];
|
||||
u32 sample;
|
||||
u8 format;
|
||||
@@ -333,12 +333,13 @@ int playWav(const char *wav)
|
||||
buffer2 = (s16*) linearAlloc(BUFFER_SIZE);
|
||||
|
||||
fread(buffer1, 1, BUFFER_SIZE, file);
|
||||
fread(buffer2, 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[0]);
|
||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||
|
||||
printf("Playing %s\n", wav);
|
||||
@@ -368,7 +369,7 @@ int playWav(const char *wav)
|
||||
if(kDown & KEY_B)
|
||||
break;
|
||||
|
||||
if(kDown & KEY_A)
|
||||
if(kDown & (KEY_A | KEY_R))
|
||||
playing = !playing;
|
||||
|
||||
if(playing == false || lastbuf == true)
|
||||
@@ -409,12 +410,6 @@ int playWav(const char *wav)
|
||||
|
||||
DSP_FlushDataCache(buffer1, 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));
|
||||
|
||||
Reference in New Issue
Block a user