Add showControls, error handling, optimisations

Add a function to show button mappings using L + LEFT.
Added preliminary error handling that will be improved later to
determine playback issues.
Music is not stopped when an unsupported file is selected.
Some other minor modifications.

TODO:
Pause/play status does not show currently.
Obtain error status of playback thread.

Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
Mahyar Koshkouei
2017-02-10 11:41:01 +00:00
parent 888c4a1505
commit 5b433f2396
6 changed files with 101 additions and 33 deletions

29
source/error.c Normal file
View File

@@ -0,0 +1,29 @@
#include "error.h"
#include <errno.h>
#include <string.h>
char* ctrmus_strerror(int err)
{
char* error;
switch(err)
{
case NDSP_INIT_FAIL:
error = "NDSP Initialisation failed";
break;
case DECODER_INIT_FAIL:
error = "Unable to initialised decoder";
break;
case FILE_NOT_SUPPORTED:
error = "File type is not supported";
break;
default:
error = strerror(err);
break;
}
return error;
}

5
source/error.h Normal file
View File

@@ -0,0 +1,5 @@
/* Errors that can't be explained with errno */
#define NDSP_INIT_FAIL 1000
#define DECODER_INIT_FAIL 1001
#define FILE_NOT_SUPPORTED 1002

View File

@@ -78,12 +78,27 @@ int main(int argc, char **argv)
if(kDown)
mill = osGetTime();
if((kHeld & KEY_L) && (kDown & (KEY_R | KEY_UP)))
if(kHeld & KEY_L)
{
togglePlayback();
continue;
/* Pause/Play */
if(kDown & (KEY_R | KEY_UP))
{
consoleSelect(&topScreen);
printf("\r%s", togglePlayback() == true ? "Paused" : "");
consoleSelect(&bottomScreen);
continue;
}
/* Show controls */
else if(kDown & KEY_LEFT)
{
consoleSelect(&topScreen);
showControls();
consoleSelect(&bottomScreen);
continue;
}
}
/* Stop */
if(kDown & KEY_X)
{
stopPlayback();
@@ -217,12 +232,28 @@ err:
goto out;
}
static void showControls(void)
{
printf("Button mappings:\n"
"Pause: L+R or L+Up\n"
"Stop: X\n"
"A: Open File\n"
"B: Go up folder\n"
"Start: Exit\n");
}
static int changeFile(const char* ep_file)
{
s32 prio;
static Thread thread = NULL;
static char* file = NULL;
if(ep_file != NULL && getFileType(ep_file) < 0)
{
puts("Unsupported file.");
return -1;
}
/**
* If music is playing, stop it. Only one playback thread should be playing
* at any time.
@@ -231,7 +262,6 @@ static int changeFile(const char* ep_file)
{
/* Tell the thread to stop playback before we join it */
stopPlayback();
puts("Stopping");
threadJoin(thread, U64_MAX);
threadFree(thread);

View File

@@ -13,6 +13,8 @@
/* Maximum number of lines that can be displayed */
#define MAX_LIST 27
static void showControls(void);
static int changeFile(const char* ep_file);
/**

View File

@@ -3,6 +3,7 @@
#include <string.h>
#include "all.h"
#include "error.h"
#include "flac.h"
#include "mp3.h"
#include "opus.h"
@@ -27,8 +28,7 @@ void playFile(void* fileIn)
/* Reset previous stop command */
stop = false;
printf("Here %d\n", __LINE__);
switch(errno = getFileType(file))
switch(getFileType(file))
{
case FILE_TYPE_WAV:
setWav(&decoder);
@@ -51,27 +51,21 @@ void playFile(void* fileIn)
return;
}
printf("Here %d\n", __LINE__);
if(R_FAILED(ndspInit()))
{
printf("Initialising ndsp failed.");
errno = NDSP_INIT_FAIL;
goto out;
}
if((ret = (*decoder.init)(file)) != 0)
{
printf("Error initialising decoder: %d\n", ret);
errno = DECODER_INIT_FAIL;
goto out;
}
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
buffer2 = linearAlloc(decoder.buffSize * sizeof(int16_t));
printf("Here %d\n", __LINE__);
#ifdef DEBUG
printf("\nRate: %lu\tChan: %d\n", (*decoder.rate)(), (*decoder.channels)());
#endif
ndspChnReset(CHANNEL);
ndspChnWaveBufClear(CHANNEL);
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
@@ -90,7 +84,6 @@ void playFile(void* fileIn)
waveBuf[1].data_vaddr = &buffer2[0];
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
printf("Here %d\n", __LINE__);
/**
* 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.
@@ -147,9 +140,7 @@ void playFile(void* fileIn)
DSP_FlushDataCache(buffer2, decoder.buffSize * sizeof(int16_t));
}
printf("Here %d\n", __LINE__);
out:
printf("\nStopping playback.\n");
(*decoder.exit)();
ndspChnWaveBufClear(CHANNEL);
ndspExit();
@@ -159,9 +150,16 @@ out:
return;
}
void togglePlayback(void)
/**
* Pause or play current file.
*
* \return True if paused.
*/
bool togglePlayback(void)
{
ndspChnSetPaused(CHANNEL, !ndspChnIsPaused(CHANNEL));
bool paused = ndspChnIsPaused(CHANNEL);
ndspChnSetPaused(CHANNEL, !paused);
return !paused;
}
void stopPlayback(void)
@@ -173,7 +171,7 @@ void stopPlayback(void)
* Obtains file type.
*
* \param file File location.
* \return File type, else negative.
* \return File type, else negative and errno set.
*/
int getFileType(const char *file)
{
@@ -183,25 +181,22 @@ int getFileType(const char *file)
/* Failure opening file */
if(ftest == NULL)
return -errno;
return -1;
if(fread(&fileSig, 4, 1, ftest) == 0)
{
fclose(ftest);
return -errno;
}
goto err;
switch(fileSig)
{
// "RIFF"
case 0x46464952:
if(fseek(ftest, 4, SEEK_CUR) != 0)
return -errno;
break;
// "WAVE"
// Check required as AVI file format also uses "RIFF".
if(fread(&fileSig, 4, 1, ftest) == 0)
return -errno;
break;
if(fileSig != 0x45564157)
break;
@@ -220,8 +215,8 @@ int getFileType(const char *file)
file_type = FILE_TYPE_OPUS;
else
{
file_type = FILE_TYPE_OGG;
return -ENOTSUP;
//file_type = FILE_TYPE_OGG;
errno = FILE_NOT_SUPPORTED;
}
break;
@@ -241,10 +236,11 @@ int getFileType(const char *file)
/* TODO: Add this again at some point */
//printf("Unknown magic number: %#010x\n.", fileSig);
return -ENOTSUP;
errno = FILE_NOT_SUPPORTED;
break;
}
err:
fclose(ftest);
printf("Here %d\n", __LINE__);
return file_type;
}

View File

@@ -1,4 +1,5 @@
enum file_types {
enum file_types
{
FILE_TYPE_ERROR = -1,
FILE_TYPE_WAV,
FILE_TYPE_FLAC,
@@ -9,7 +10,12 @@ enum file_types {
void playFile(void* fileIn);
void togglePlayback(void);
/**
* Pause or play current file.
*
* \return True if paused.
*/
bool togglePlayback(void);
void stopPlayback(void);