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:
29
source/error.c
Normal file
29
source/error.c
Normal 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
5
source/error.h
Normal 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
|
||||||
@@ -78,12 +78,27 @@ int main(int argc, char **argv)
|
|||||||
if(kDown)
|
if(kDown)
|
||||||
mill = osGetTime();
|
mill = osGetTime();
|
||||||
|
|
||||||
if((kHeld & KEY_L) && (kDown & (KEY_R | KEY_UP)))
|
if(kHeld & KEY_L)
|
||||||
{
|
{
|
||||||
togglePlayback();
|
/* Pause/Play */
|
||||||
continue;
|
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)
|
if(kDown & KEY_X)
|
||||||
{
|
{
|
||||||
stopPlayback();
|
stopPlayback();
|
||||||
@@ -217,12 +232,28 @@ err:
|
|||||||
goto out;
|
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)
|
static int changeFile(const char* ep_file)
|
||||||
{
|
{
|
||||||
s32 prio;
|
s32 prio;
|
||||||
static Thread thread = NULL;
|
static Thread thread = NULL;
|
||||||
static char* file = 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
|
* If music is playing, stop it. Only one playback thread should be playing
|
||||||
* at any time.
|
* at any time.
|
||||||
@@ -231,7 +262,6 @@ static int changeFile(const char* ep_file)
|
|||||||
{
|
{
|
||||||
/* Tell the thread to stop playback before we join it */
|
/* Tell the thread to stop playback before we join it */
|
||||||
stopPlayback();
|
stopPlayback();
|
||||||
puts("Stopping");
|
|
||||||
|
|
||||||
threadJoin(thread, U64_MAX);
|
threadJoin(thread, U64_MAX);
|
||||||
threadFree(thread);
|
threadFree(thread);
|
||||||
|
|||||||
@@ -13,6 +13,8 @@
|
|||||||
/* Maximum number of lines that can be displayed */
|
/* Maximum number of lines that can be displayed */
|
||||||
#define MAX_LIST 27
|
#define MAX_LIST 27
|
||||||
|
|
||||||
|
static void showControls(void);
|
||||||
|
|
||||||
static int changeFile(const char* ep_file);
|
static int changeFile(const char* ep_file);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "all.h"
|
#include "all.h"
|
||||||
|
#include "error.h"
|
||||||
#include "flac.h"
|
#include "flac.h"
|
||||||
#include "mp3.h"
|
#include "mp3.h"
|
||||||
#include "opus.h"
|
#include "opus.h"
|
||||||
@@ -27,8 +28,7 @@ void playFile(void* fileIn)
|
|||||||
/* Reset previous stop command */
|
/* Reset previous stop command */
|
||||||
stop = false;
|
stop = false;
|
||||||
|
|
||||||
printf("Here %d\n", __LINE__);
|
switch(getFileType(file))
|
||||||
switch(errno = getFileType(file))
|
|
||||||
{
|
{
|
||||||
case FILE_TYPE_WAV:
|
case FILE_TYPE_WAV:
|
||||||
setWav(&decoder);
|
setWav(&decoder);
|
||||||
@@ -51,27 +51,21 @@ void playFile(void* fileIn)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Here %d\n", __LINE__);
|
|
||||||
if(R_FAILED(ndspInit()))
|
if(R_FAILED(ndspInit()))
|
||||||
{
|
{
|
||||||
printf("Initialising ndsp failed.");
|
errno = NDSP_INIT_FAIL;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((ret = (*decoder.init)(file)) != 0)
|
if((ret = (*decoder.init)(file)) != 0)
|
||||||
{
|
{
|
||||||
printf("Error initialising decoder: %d\n", ret);
|
errno = DECODER_INIT_FAIL;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||||
buffer2 = 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);
|
ndspChnReset(CHANNEL);
|
||||||
ndspChnWaveBufClear(CHANNEL);
|
ndspChnWaveBufClear(CHANNEL);
|
||||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
||||||
@@ -90,7 +84,6 @@ void playFile(void* fileIn)
|
|||||||
waveBuf[1].data_vaddr = &buffer2[0];
|
waveBuf[1].data_vaddr = &buffer2[0];
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
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
|
* 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.
|
* 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));
|
DSP_FlushDataCache(buffer2, decoder.buffSize * sizeof(int16_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Here %d\n", __LINE__);
|
|
||||||
out:
|
out:
|
||||||
printf("\nStopping playback.\n");
|
|
||||||
(*decoder.exit)();
|
(*decoder.exit)();
|
||||||
ndspChnWaveBufClear(CHANNEL);
|
ndspChnWaveBufClear(CHANNEL);
|
||||||
ndspExit();
|
ndspExit();
|
||||||
@@ -159,9 +150,16 @@ out:
|
|||||||
return;
|
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)
|
void stopPlayback(void)
|
||||||
@@ -173,7 +171,7 @@ void stopPlayback(void)
|
|||||||
* Obtains file type.
|
* Obtains file type.
|
||||||
*
|
*
|
||||||
* \param file File location.
|
* \param file File location.
|
||||||
* \return File type, else negative.
|
* \return File type, else negative and errno set.
|
||||||
*/
|
*/
|
||||||
int getFileType(const char *file)
|
int getFileType(const char *file)
|
||||||
{
|
{
|
||||||
@@ -183,25 +181,22 @@ int getFileType(const char *file)
|
|||||||
|
|
||||||
/* Failure opening file */
|
/* Failure opening file */
|
||||||
if(ftest == NULL)
|
if(ftest == NULL)
|
||||||
return -errno;
|
return -1;
|
||||||
|
|
||||||
if(fread(&fileSig, 4, 1, ftest) == 0)
|
if(fread(&fileSig, 4, 1, ftest) == 0)
|
||||||
{
|
goto err;
|
||||||
fclose(ftest);
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(fileSig)
|
switch(fileSig)
|
||||||
{
|
{
|
||||||
// "RIFF"
|
// "RIFF"
|
||||||
case 0x46464952:
|
case 0x46464952:
|
||||||
if(fseek(ftest, 4, SEEK_CUR) != 0)
|
if(fseek(ftest, 4, SEEK_CUR) != 0)
|
||||||
return -errno;
|
break;
|
||||||
|
|
||||||
// "WAVE"
|
// "WAVE"
|
||||||
// Check required as AVI file format also uses "RIFF".
|
// Check required as AVI file format also uses "RIFF".
|
||||||
if(fread(&fileSig, 4, 1, ftest) == 0)
|
if(fread(&fileSig, 4, 1, ftest) == 0)
|
||||||
return -errno;
|
break;
|
||||||
|
|
||||||
if(fileSig != 0x45564157)
|
if(fileSig != 0x45564157)
|
||||||
break;
|
break;
|
||||||
@@ -220,8 +215,8 @@ int getFileType(const char *file)
|
|||||||
file_type = FILE_TYPE_OPUS;
|
file_type = FILE_TYPE_OPUS;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
file_type = FILE_TYPE_OGG;
|
//file_type = FILE_TYPE_OGG;
|
||||||
return -ENOTSUP;
|
errno = FILE_NOT_SUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -241,10 +236,11 @@ int getFileType(const char *file)
|
|||||||
|
|
||||||
/* TODO: Add this again at some point */
|
/* TODO: Add this again at some point */
|
||||||
//printf("Unknown magic number: %#010x\n.", fileSig);
|
//printf("Unknown magic number: %#010x\n.", fileSig);
|
||||||
return -ENOTSUP;
|
errno = FILE_NOT_SUPPORTED;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err:
|
||||||
fclose(ftest);
|
fclose(ftest);
|
||||||
printf("Here %d\n", __LINE__);
|
|
||||||
return file_type;
|
return file_type;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
enum file_types {
|
enum file_types
|
||||||
|
{
|
||||||
FILE_TYPE_ERROR = -1,
|
FILE_TYPE_ERROR = -1,
|
||||||
FILE_TYPE_WAV,
|
FILE_TYPE_WAV,
|
||||||
FILE_TYPE_FLAC,
|
FILE_TYPE_FLAC,
|
||||||
@@ -9,7 +10,12 @@ enum file_types {
|
|||||||
|
|
||||||
void playFile(void* fileIn);
|
void playFile(void* fileIn);
|
||||||
|
|
||||||
void togglePlayback(void);
|
/**
|
||||||
|
* Pause or play current file.
|
||||||
|
*
|
||||||
|
* \return True if paused.
|
||||||
|
*/
|
||||||
|
bool togglePlayback(void);
|
||||||
|
|
||||||
void stopPlayback(void);
|
void stopPlayback(void);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user