Refactor for testing

Added basic decoding functionality to test.

Refactored some code to stop 3DS specific code from compiling on
GNU/Linux.

Tested working by running the test executable on GNU/Linux. Vorbis
decoding has many errors detected by valgrind (but still decodes
successfully).
Opus, flac, and mp3 decoding work without errors.

Signed-off-by: Mahyar Koshkouei <mk@deltabeard.com>
This commit is contained in:
Mahyar Koshkouei
2017-08-14 23:14:45 +01:00
parent 11d7bede77
commit ef0289bceb
8 changed files with 190 additions and 105 deletions

View File

@@ -29,6 +29,7 @@ ODIR=./build/$(HOST_ARCH)
SDIR=./source
_DEPS = all.h \
file.h \
flac.h \
mp3.h \
opus.h \
@@ -37,7 +38,8 @@ _DEPS = all.h \
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = flac.o \
_OBJ = file.o \
flac.o \
mp3.o \
opus.o \
test.o \

View File

@@ -1,4 +1,6 @@
#if defined __arm__
#include <3ds.h>
#endif
/* Errors that can't be explained with errno */
#define NDSP_INIT_FAIL 1000
@@ -6,6 +8,8 @@
#define FILE_NOT_SUPPORTED 1002
#define UNSUPPORTED_CHANNELS 1003
#if defined __arm__
/**
* Struct to help error handling across threads.
*/
@@ -28,3 +32,5 @@ struct errInfo_t
* \param err Error number.
*/
char* ctrmus_strerror(int err);
#endif

111
source/file.c Normal file
View File

@@ -0,0 +1,111 @@
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include "error.h"
#include "file.h"
#include "flac.h"
#include "mp3.h"
#include "opus.h"
#include "vorbis.h"
#include "wav.h"
/**
* Obtain file type string from file_types enum.
*
* \param ft File type enum.
* \return String representation of enum.
*/
const char* fileToStr(enum file_types ft)
{
static const char *file_types_str[] = {
"UNKNOWN",
"WAV",
"FLAC",
"VORBIS",
"OPUS",
"MP3"
};
return file_types_str[ft];
}
/**
* Obtains file type.
*
* \param file File location.
* \return file_types enum or 0 on error and errno set.
*/
enum file_types getFileType(const char *file)
{
FILE* ftest = fopen(file, "rb");
uint32_t fileSig;
enum file_types file_type = FILE_TYPE_ERROR;
/* Failure opening file */
if(ftest == NULL)
return -1;
if(fread(&fileSig, 4, 1, ftest) == 0)
goto err;
switch(fileSig)
{
// "RIFF"
case 0x46464952:
if(fseek(ftest, 4, SEEK_CUR) != 0)
break;
// "WAVE"
// Check required as AVI file format also uses "RIFF".
if(fread(&fileSig, 4, 1, ftest) == 0)
break;
if(fileSig != 0x45564157)
break;
file_type = FILE_TYPE_WAV;
break;
// "fLaC"
case 0x43614c66:
file_type = FILE_TYPE_FLAC;
break;
// "OggS"
case 0x5367674F:
if(isOpus(file) == 0)
file_type = FILE_TYPE_OPUS;
else if(isFlac(file) == 0)
file_type = FILE_TYPE_FLAC;
else if(isVorbis(file) == 0)
file_type = FILE_TYPE_VORBIS;
else
errno = FILE_NOT_SUPPORTED;
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 << 16) == 0xFAFF0000 ||
(fileSig << 8) == 0x33444900)
{
file_type = FILE_TYPE_MP3;
break;
}
/* TODO: Add this again at some point */
//printf("Unknown magic number: %#010x\n.", fileSig);
errno = FILE_NOT_SUPPORTED;
break;
}
err:
fclose(ftest);
return file_type;
}

25
source/file.h Normal file
View File

@@ -0,0 +1,25 @@
enum file_types
{
FILE_TYPE_ERROR = 0,
FILE_TYPE_WAV,
FILE_TYPE_FLAC,
FILE_TYPE_VORBIS,
FILE_TYPE_OPUS,
FILE_TYPE_MP3
};
/**
* Obtain file type string from file_types enum.
*
* \param ft File type enum.
* \return String representation of enum.
*/
const char* fileToStr(enum file_types ft);
/**
* Obtains file type.
*
* \param file File location.
* \return file_types enum or 0 on error and errno set.
*/
enum file_types getFileType(const char *file);

View File

@@ -17,6 +17,7 @@
#include "all.h"
#include "error.h"
#include "file.h"
#include "main.h"
#include "playback.h"

View File

@@ -5,6 +5,7 @@
#include "all.h"
#include "error.h"
#include "file.h"
#include "flac.h"
#include "mp3.h"
#include "opus.h"
@@ -42,87 +43,6 @@ bool isPlaying(void)
return !stop;
}
/**
* Obtains file type.
*
* \param file File location.
* \return File type, else negative and errno set.
*/
int getFileType(const char *file)
{
FILE* ftest = fopen(file, "rb");
uint32_t fileSig;
enum file_types file_type = FILE_TYPE_ERROR;
/* Failure opening file */
if(ftest == NULL)
return -1;
if(fread(&fileSig, 4, 1, ftest) == 0)
goto err;
switch(fileSig)
{
// "RIFF"
case 0x46464952:
if(fseek(ftest, 4, SEEK_CUR) != 0)
break;
// "WAVE"
// Check required as AVI file format also uses "RIFF".
if(fread(&fileSig, 4, 1, ftest) == 0)
break;
if(fileSig != 0x45564157)
break;
file_type = FILE_TYPE_WAV;
break;
// "fLaC"
case 0x43614c66:
file_type = FILE_TYPE_FLAC;
break;
// "OggS"
case 0x5367674F:
if(isOpus(file) == 0)
file_type = FILE_TYPE_OPUS;
else if(isFlac(file) == 0)
file_type = FILE_TYPE_FLAC;
else if(isVorbis(file) == 0)
file_type = FILE_TYPE_VORBIS;
else
{
errno = FILE_NOT_SUPPORTED;
}
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 << 16) == 0xFAFF0000 ||
(fileSig << 8) == 0x33444900)
{
file_type = FILE_TYPE_MP3;
break;
}
/* TODO: Add this again at some point */
//printf("Unknown magic number: %#010x\n.", fileSig);
errno = FILE_NOT_SUPPORTED;
break;
}
err:
fclose(ftest);
return file_type;
}
/**
* Should only be called from a new thread only, and have only one playback
* thread at time. This function has not been written for more than one

View File

@@ -6,16 +6,6 @@
/* Channel to play music on */
#define CHANNEL 0x08
enum file_types
{
FILE_TYPE_ERROR = -1,
FILE_TYPE_WAV,
FILE_TYPE_FLAC,
FILE_TYPE_VORBIS,
FILE_TYPE_OPUS,
FILE_TYPE_MP3
};
struct decoder_fn
{
int (* init)(const char* file);
@@ -49,14 +39,6 @@ void stopPlayback(void);
*/
bool isPlaying(void);
/**
* Obtains file type.
*
* \param file File location.
* \return File type, else negative and errno set.
*/
int getFileType(const char *file);
/**
* Should only be called from a new thread only, and have only one playback
* thread at time. This function has not been written for more than one

View File

@@ -1,21 +1,28 @@
#if defined __gnu_linux__
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "error.h"
#include "file.h"
#include "flac.h"
#include "mp3.h"
#include "opus.h"
#include "playback.h"
#include "vorbis.h"
#include "wav.h"
#include "playback.h"
/**
* Test the various decoder modules in ctrmus.
*/
int main(int argc, char *argv[])
{
struct decoder_fn decoder;
struct decoder_fn decoder;
enum file_types ft;
const char *file = argv[1];
int16_t *buffer = NULL;
FILE *out;
if(argc != 2)
{
@@ -23,8 +30,8 @@ int main(int argc, char *argv[])
printf("%s FILE\n", argv[0]);
return 0;
}
#if 0
switch(getFileType(argv[1]))
switch(ft = getFileType(file))
{
case FILE_TYPE_WAV:
setWav(&decoder);
@@ -47,9 +54,40 @@ int main(int argc, char *argv[])
break;
default:
puts("Unsupported file.");
goto err;
}
#endif
printf("Type: %s\n", fileToStr(ft));
if((*decoder.init)(file) != 0)
{
puts("Unable to initialise decoder.");
goto err;
}
if((*decoder.channels)() > 2 || (*decoder.channels)() < 1)
{
puts("Unable to obtain number of channels.");
goto err;
}
out = fopen("out", "wb");
buffer = malloc(decoder.buffSize * sizeof(int16_t));
while(true)
{
size_t read = (*decoder.decode)(&buffer[0]);
if(read <= 0)
break;
fwrite(buffer, read * sizeof(int16_t), 1, out);
}
(*decoder.exit)();
free(buffer);
fclose(out);
return 0;