Flac refactor WIP
Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
@@ -15,7 +15,7 @@ struct decoder_fn
|
|||||||
{
|
{
|
||||||
int (* init)(const char* file);
|
int (* init)(const char* file);
|
||||||
uint32_t (* rate)(void);
|
uint32_t (* rate)(void);
|
||||||
uint8_t channels;
|
uint8_t (*channels)(void);
|
||||||
int buffSize;
|
int buffSize;
|
||||||
uint64_t (* decode)(void*);
|
uint64_t (* decode)(void*);
|
||||||
void (* exit)(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) / pFlac->channels;
|
||||||
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);
|
||||||
|
|||||||
130
source/main.c
130
source/main.c
@@ -16,12 +16,9 @@
|
|||||||
#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 "opus.h"
|
#include "opus.h"
|
||||||
#include "playback.h"
|
#include "playback.h"
|
||||||
#include "wav.h"
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@@ -73,7 +70,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);
|
||||||
@@ -191,33 +191,10 @@ int main(int argc, char **argv)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
consoleSelect(&topScreen);
|
consoleSelect(&topScreen);
|
||||||
/* Move this to playback.c */
|
playFile(file);
|
||||||
switch(getFileType(file))
|
consoleSelect(&bottomScreen);
|
||||||
{
|
|
||||||
case FILE_TYPE_WAV:
|
|
||||||
playWav(file);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FILE_TYPE_FLAC:
|
|
||||||
playFlac(file);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FILE_TYPE_OPUS:
|
|
||||||
playFile(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);
|
||||||
|
|
||||||
@@ -228,12 +205,18 @@ 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...");
|
||||||
|
|
||||||
gfxExit();
|
gfxExit();
|
||||||
sdmcExit();
|
sdmcExit();
|
||||||
|
puts("Return");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
@@ -345,94 +328,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;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -13,15 +13,6 @@
|
|||||||
/* Maximum number of lines that can be displayed */
|
/* Maximum number of lines that can be displayed */
|
||||||
#define MAX_LIST 27
|
#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
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get number of files in current working folder
|
* Get number of files in current working folder
|
||||||
*
|
*
|
||||||
@@ -39,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);
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
static OggOpusFile* opusFile;
|
static OggOpusFile* opusFile;
|
||||||
static const OpusHead* opusHead;
|
static const OpusHead* opusHead;
|
||||||
static const int buffSize = 32 * 1024;
|
static const int buffSize = 32 * 1024;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set decoder parameters for Opus.
|
* Set decoder parameters for Opus.
|
||||||
@@ -16,13 +16,12 @@ static const int buffSize = 32 * 1024;
|
|||||||
*/
|
*/
|
||||||
void setOpus(struct decoder_fn* decoder)
|
void setOpus(struct decoder_fn* decoder)
|
||||||
{
|
{
|
||||||
decoder->init = initOpus;
|
decoder->init = &initOpus;
|
||||||
decoder->rate = rateOpus;
|
decoder->rate = &rateOpus;
|
||||||
/* Opus decoder always returns stereo stream */
|
decoder->channels = &channelOpus;
|
||||||
decoder->channels = 2;
|
|
||||||
decoder->buffSize = buffSize;
|
decoder->buffSize = buffSize;
|
||||||
decoder->decode = decodeOpus;
|
decoder->decode = &decodeOpus;
|
||||||
decoder->exit = exitOpus;
|
decoder->exit = &exitOpus;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,6 +56,17 @@ uint32_t rateOpus(void)
|
|||||||
return opusHead->input_sample_rate;
|
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.
|
* Decode part of open Opus file.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ int initOpus(const char* file);
|
|||||||
|
|
||||||
uint32_t rateOpus(void);
|
uint32_t rateOpus(void);
|
||||||
|
|
||||||
|
uint8_t channelOpus(void);
|
||||||
|
|
||||||
uint64_t decodeOpus(void* buffer);
|
uint64_t decodeOpus(void* buffer);
|
||||||
|
|
||||||
void exitOpus(void);
|
void exitOpus(void);
|
||||||
|
|||||||
@@ -3,8 +3,11 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "all.h"
|
#include "all.h"
|
||||||
|
#include "flac.h"
|
||||||
|
#include "mp3.h"
|
||||||
#include "opus.h"
|
#include "opus.h"
|
||||||
#include "playback.h"
|
#include "playback.h"
|
||||||
|
#include "wav.h"
|
||||||
|
|
||||||
int playFile(const char* file)
|
int playFile(const char* file)
|
||||||
{
|
{
|
||||||
@@ -22,8 +25,30 @@ int playFile(const char* file)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Here: %d\n", __LINE__);
|
switch(getFileType(file))
|
||||||
setOpus(&decoder);
|
{
|
||||||
|
case FILE_TYPE_WAV:
|
||||||
|
playWav(file);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
case FILE_TYPE_FLAC:
|
||||||
|
setFlac(&decoder);
|
||||||
|
break;
|
||||||
|
//playFlac(file);
|
||||||
|
//return 0;
|
||||||
|
|
||||||
|
case FILE_TYPE_OPUS:
|
||||||
|
setOpus(&decoder);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FILE_TYPE_MP3:
|
||||||
|
playMp3(file);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("Unsupported File type.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
printf("Here: %d\n", __LINE__);
|
printf("Here: %d\n", __LINE__);
|
||||||
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||||
@@ -36,7 +61,7 @@ int playFile(const char* file)
|
|||||||
printf("Here: %d\n", __LINE__);
|
printf("Here: %d\n", __LINE__);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("\nRate: %lu\tChan: %d\n", (*decoder.rate)(), decoder.channels);
|
printf("\nRate: %lu\tChan: %d\n", (*decoder.rate)(), (*decoder.channels)());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ndspChnReset(CHANNEL);
|
ndspChnReset(CHANNEL);
|
||||||
@@ -44,16 +69,18 @@ int playFile(const char* file)
|
|||||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
||||||
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
||||||
ndspChnSetRate(CHANNEL, (*decoder.rate)());
|
ndspChnSetRate(CHANNEL, (*decoder.rate)());
|
||||||
ndspChnSetFormat(CHANNEL, NDSP_FORMAT_STEREO_PCM16);
|
ndspChnSetFormat(CHANNEL,
|
||||||
|
(*decoder.channels)() == 2 ? NDSP_FORMAT_STEREO_PCM16 :
|
||||||
|
NDSP_FORMAT_MONO_PCM16);
|
||||||
printf("Here: %d\n", __LINE__);
|
printf("Here: %d\n", __LINE__);
|
||||||
|
|
||||||
memset(waveBuf, 0, sizeof(waveBuf));
|
memset(waveBuf, 0, sizeof(waveBuf));
|
||||||
waveBuf[0].nsamples = (*decoder.decode)(&buffer1[0]) / decoder.channels;
|
waveBuf[0].nsamples = (*decoder.decode)(&buffer1[0]) / (*decoder.channels)();
|
||||||
waveBuf[0].data_vaddr = &buffer1[0];
|
waveBuf[0].data_vaddr = &buffer1[0];
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||||
printf("Here: %d\n", __LINE__);
|
printf("Here: %d\n", __LINE__);
|
||||||
|
|
||||||
waveBuf[1].nsamples = (*decoder.decode)(&buffer2[0]) / decoder.channels;
|
waveBuf[1].nsamples = (*decoder.decode)(&buffer2[0]) / (*decoder.channels)();
|
||||||
waveBuf[1].data_vaddr = &buffer2[0];
|
waveBuf[1].data_vaddr = &buffer2[0];
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||||
|
|
||||||
@@ -103,7 +130,7 @@ int playFile(const char* file)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if(read < decoder.buffSize)
|
else if(read < decoder.buffSize)
|
||||||
waveBuf[0].nsamples = read / decoder.channels;
|
waveBuf[0].nsamples = read / (*decoder.channels)();
|
||||||
|
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
||||||
}
|
}
|
||||||
@@ -118,7 +145,7 @@ int playFile(const char* file)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if(read < decoder.buffSize)
|
else if(read < decoder.buffSize)
|
||||||
waveBuf[1].nsamples = read / decoder.channels;
|
waveBuf[1].nsamples = read / (*decoder.channels)();
|
||||||
|
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
||||||
}
|
}
|
||||||
@@ -136,3 +163,94 @@ out:
|
|||||||
linearFree(buffer2);
|
linearFree(buffer2);
|
||||||
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:
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1 +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);
|
int playFile(const char* file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains file type.
|
||||||
|
*
|
||||||
|
* \param file File location.
|
||||||
|
* \return File type, else negative.
|
||||||
|
*/
|
||||||
|
int getFileType(const char *file);
|
||||||
|
|||||||
Reference in New Issue
Block a user