From 825db2f713612f1bb3bd027f4256c8a9f4e748b7 Mon Sep 17 00:00:00 2001 From: Mahyar Koshkouei Date: Wed, 15 Feb 2017 15:28:07 +0000 Subject: [PATCH 1/5] Fix build warnings Signed-off-by: Mahyar Koshkouei --- Makefile | 2 +- source/all.h | 13 ------------- source/flac.c | 4 ++-- source/flac.h | 2 ++ source/mp3.c | 4 ++-- source/mp3.h | 2 ++ source/opus.c | 4 ++-- source/opus.h | 1 + source/playback.c | 7 ++++--- source/playback.h | 13 +++++++++++++ source/wav.c | 4 ++-- source/wav.h | 2 ++ 12 files changed, 33 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 18475d6..77fff73 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ EXTRA_OUTPUT_FILES := LIBRARY_DIRS := $(DEVKITPRO)/libctru $(DEVKITPRO)/portlibs/armv6k LIBRARIES := mpg123 opusfile opus ogg ctru m -BUILD_FLAGS := +BUILD_FLAGS := -Wall -Wextra RUN_FLAGS := VERSION_PARTS := $(subst ., ,$(shell git describe --tags --abbrev=0)) diff --git a/source/all.h b/source/all.h index 89c4901..b7ef44f 100644 --- a/source/all.h +++ b/source/all.h @@ -1,9 +1,6 @@ #include #include -/* Channel to play music on */ -#define CHANNEL 0x08 - /* Adds extra debugging text */ //#define DEBUG @@ -14,13 +11,3 @@ #define delete(ptr) \ free((void*) ptr); ptr = NULL - -struct decoder_fn -{ - int (* init)(const char* file); - uint32_t (* rate)(void); - uint8_t (* channels)(void); - int buffSize; - uint64_t (* decode)(void*); - void (* exit)(void); -}; diff --git a/source/flac.c b/source/flac.c index 6368fea..49f922c 100644 --- a/source/flac.c +++ b/source/flac.c @@ -3,11 +3,11 @@ #define DR_FLAC_IMPLEMENTATION #include <./dr_libs/dr_flac.h> -#include "all.h" #include "flac.h" +#include "playback.h" static drflac* pFlac; -static const int buffSize = 16 * 1024; +static const size_t buffSize = 16 * 1024; /** * Set decoder parameters for flac. diff --git a/source/flac.h b/source/flac.h index dc734c9..5da31ec 100644 --- a/source/flac.h +++ b/source/flac.h @@ -1,3 +1,5 @@ +#include "playback.h" + /** * Set decoder parameters for flac. * diff --git a/source/mp3.c b/source/mp3.c index 3cc1f3a..2535a22 100644 --- a/source/mp3.c +++ b/source/mp3.c @@ -4,10 +4,10 @@ #include #include -#include "all.h" #include "mp3.h" +#include "playback.h" -static int* buffSize; +static size_t* buffSize; static mpg123_handle *mh = NULL; static uint32_t rate; static uint8_t channels; diff --git a/source/mp3.h b/source/mp3.h index 06218ca..223440a 100644 --- a/source/mp3.h +++ b/source/mp3.h @@ -1,3 +1,5 @@ +#include "playback.h" + /** * Set decoder parameters for MP3. * diff --git a/source/opus.c b/source/opus.c index 8e98d88..2285f9d 100644 --- a/source/opus.c +++ b/source/opus.c @@ -2,12 +2,12 @@ #include #include -#include "all.h" #include "opus.h" +#include "playback.h" static OggOpusFile* opusFile; static const OpusHead* opusHead; -static const int buffSize = 32 * 1024; +static const size_t buffSize = 32 * 1024; /** * Set decoder parameters for Opus. diff --git a/source/opus.h b/source/opus.h index 98e2c09..4e88f02 100644 --- a/source/opus.h +++ b/source/opus.h @@ -1,4 +1,5 @@ #include +#include "playback.h" void setOpus(struct decoder_fn* decoder); diff --git a/source/playback.c b/source/playback.c index 8a81b34..109b6d6 100644 --- a/source/playback.c +++ b/source/playback.c @@ -1,4 +1,5 @@ #include <3ds.h> +#include #include #include @@ -49,7 +50,7 @@ bool isPlaying(void) static int getFileType(const char *file) { FILE* ftest = fopen(file, "rb"); - int fileSig = 0; + uint32_t fileSig; enum file_types file_type = FILE_TYPE_ERROR; /* Failure opening file */ @@ -83,7 +84,7 @@ static int getFileType(const char *file) break; // "OggS" - case 0x5367674f: + case 0x5367674F: if(isOpus(file) == 0) file_type = FILE_TYPE_OPUS; else @@ -161,7 +162,7 @@ void playFile(void* infoIn) goto err; } - if(R_FAILED(ndspInit())) + if(ndspInit() < 0) { errno = NDSP_INIT_FAIL; goto err; diff --git a/source/playback.h b/source/playback.h index 4779f58..60f0d24 100644 --- a/source/playback.h +++ b/source/playback.h @@ -1,6 +1,9 @@ #ifndef ctrmus_playback_h #define ctrmus_playback_h +/* Channel to play music on */ +#define CHANNEL 0x08 + enum file_types { FILE_TYPE_ERROR = -1, @@ -11,6 +14,16 @@ enum file_types FILE_TYPE_MP3 }; +struct decoder_fn +{ + int (* init)(const char* file); + uint32_t (* rate)(void); + uint8_t (* channels)(void); + size_t buffSize; + uint64_t (* decode)(void*); + void (* exit)(void); +}; + struct playbackInfo_t { char* file; diff --git a/source/wav.c b/source/wav.c index e27f9dc..c21f700 100644 --- a/source/wav.c +++ b/source/wav.c @@ -3,10 +3,10 @@ #include #include -#include "all.h" #include "wav.h" +#include "playback.h" -static const int buffSize = 16 * 1024; +static const size_t buffSize = 16 * 1024; static FILE* pWav = NULL; static char header[45]; static uint8_t channels; diff --git a/source/wav.h b/source/wav.h index 5971c21..6814a94 100644 --- a/source/wav.h +++ b/source/wav.h @@ -1,3 +1,5 @@ +#include "playback.h" + /** * Set decoder parameters for WAV. * From 02746ec1d0a948fde133b126eeed766644f5ee73 Mon Sep 17 00:00:00 2001 From: Mahyar Koshkouei Date: Wed, 15 Feb 2017 15:50:20 +0000 Subject: [PATCH 2/5] Fix playback bug & don't stop on every file select Fixed a bug where ctrmus would freeze after selecting an unsupported file. Fixed a bug whereby selecting an unsupported file would stop playback. Signed-off-by: Mahyar Koshkouei --- source/main.c | 10 +++++++--- source/playback.c | 14 +++++++++++--- source/playback.h | 26 +++++++++++++++++--------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/source/main.c b/source/main.c index 04f9b7b..1315b42 100644 --- a/source/main.c +++ b/source/main.c @@ -82,6 +82,13 @@ static int changeFile(const char* ep_file, struct playbackInfo_t* playbackInfo) s32 prio; static Thread thread = NULL; + if(ep_file != NULL && getFileType(ep_file) == FILE_TYPE_ERROR) + { + *playbackInfo->errInfo->error = errno; + svcSignalEvent(*playbackInfo->errInfo->failEvent); + return -1; + } + /** * If music is playing, stop it. Only one playback thread should be playing * at any time. @@ -94,9 +101,6 @@ static int changeFile(const char* ep_file, struct playbackInfo_t* playbackInfo) threadJoin(thread, U64_MAX); threadFree(thread); thread = NULL; - - /* free allocated file string */ - delete(playbackInfo->file); } if(ep_file == NULL || playbackInfo == NULL) diff --git a/source/playback.c b/source/playback.c index 109b6d6..6695e60 100644 --- a/source/playback.c +++ b/source/playback.c @@ -47,7 +47,7 @@ bool isPlaying(void) * \param file File location. * \return File type, else negative and errno set. */ -static int getFileType(const char *file) +int getFileType(const char *file) { FILE* ftest = fopen(file, "rb"); uint32_t fileSig; @@ -136,6 +136,7 @@ void playFile(void* infoIn) bool lastbuf = false; int ret = -1; const char* file = info->file; + bool isNdspInit = false; /* Reset previous stop command */ stop = false; @@ -168,6 +169,8 @@ void playFile(void* infoIn) goto err; } + isNdspInit = true; + if((ret = (*decoder.init)(file)) != 0) { errno = DECODER_INIT_FAIL; @@ -253,8 +256,13 @@ void playFile(void* infoIn) (*decoder.exit)(); out: - ndspChnWaveBufClear(CHANNEL); - ndspExit(); + if(isNdspInit == true) + { + ndspChnWaveBufClear(CHANNEL); + ndspExit(); + } + + delete(info->file); linearFree(buffer1); linearFree(buffer2); threadExit(0); diff --git a/source/playback.h b/source/playback.h index 60f0d24..2b4abda 100644 --- a/source/playback.h +++ b/source/playback.h @@ -30,15 +30,6 @@ struct playbackInfo_t struct errInfo_t* errInfo; }; -/** - * 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 - * playback thread in mind. - * - * \param infoIn Playback information. - */ -void playFile(void* infoIn); - /** * Pause or play current file. * @@ -56,4 +47,21 @@ 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 + * playback thread in mind. + * + * \param infoIn Playback information. + */ +void playFile(void* infoIn); + #endif From 073b5071186a042a3a0f914f627b4a6f592c844e Mon Sep 17 00:00:00 2001 From: Mahyar Koshkouei Date: Wed, 15 Feb 2017 16:34:50 +0000 Subject: [PATCH 3/5] Browse files with Left & Right Added feature to browse a folder with Left & Right buttons to skip over half a screens worth of files (13 files). Signed-off-by: Mahyar Koshkouei --- source/main.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ source/playback.c | 2 -- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/source/main.c b/source/main.c index 1315b42..aed0372 100644 --- a/source/main.c +++ b/source/main.c @@ -347,6 +347,52 @@ int main(int argc, char **argv) err_print("Unable to list directory."); } + if((kDown & KEY_LEFT || + ((kHeld & KEY_LEFT) && (osGetTime() - mill > 500))) && + fileNum > 0) + { + int skip = MAX_LIST / 2; + + if(fileNum < skip) + skip = fileNum; + + fileNum -= skip; + + /* 26 is the maximum number of entries that can be printed */ + if(fileMax - fileNum > 26 && from != 0) + { + from -= skip; + if(from < 0) + from = 0; + } + + if(listDir(from, MAX_LIST, fileNum) < 0) + err_print("Unable to list directory."); + } + + if((kDown & KEY_RIGHT || + ((kHeld & KEY_RIGHT) && (osGetTime() - mill > 500))) && + fileNum < fileMax) + { + int skip = fileMax - fileNum; + + if(skip > MAX_LIST / 2) + skip = MAX_LIST / 2; + + fileNum += skip; + + if(fileNum >= MAX_LIST && fileMax - fileNum >= 0 && + from < fileMax - MAX_LIST) + { + from += skip; + if(from > fileMax - MAX_LIST) + from = fileMax - MAX_LIST; + } + + if(listDir(from, MAX_LIST, fileNum) < 0) + err_print("Unable to list directory."); + } + /* * Pressing B goes up a folder, as well as pressing A or R when ".." * is selected. diff --git a/source/playback.c b/source/playback.c index 6695e60..4c1990f 100644 --- a/source/playback.c +++ b/source/playback.c @@ -210,8 +210,6 @@ void playFile(void* infoIn) gfxFlushBuffers(); gspWaitForVBlank(); - svcSleepThread(100 * 1000); - /* When the last buffer has finished playing, break. */ if(lastbuf == true && waveBuf[0].status == NDSP_WBUF_DONE && waveBuf[1].status == NDSP_WBUF_DONE) From 59688aaf0e1ad2f569bb799bb730de70088d0720 Mon Sep 17 00:00:00 2001 From: Mahyar Koshkouei Date: Wed, 15 Feb 2017 16:53:33 +0000 Subject: [PATCH 4/5] Remove error on going up directory Removed an error that would briefly appear on the screen if the user were to attempt to change to the above directory whilst being in the root directory. Signed-off-by: Mahyar Koshkouei --- source/main.c | 8 +++----- source/playback.c | 4 +--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/source/main.c b/source/main.c index aed0372..d486ece 100644 --- a/source/main.c +++ b/source/main.c @@ -260,12 +260,11 @@ int main(int argc, char **argv) u32 kHeld; static u64 mill = 0; - hidScanInput(); - - gfxSwapBuffers(); gfxFlushBuffers(); gspWaitForVBlank(); + gfxSwapBuffers(); + hidScanInput(); kDown = hidKeysDown(); kHeld = hidKeysHeld(); @@ -400,8 +399,7 @@ int main(int argc, char **argv) if((kDown & KEY_B) || ((kDown & KEY_A) && (from == 0 && fileNum == 0))) { - if(chdir("..") != 0) - err_print("chdir"); + chdir(".."); fileNum = 0; from = 0; diff --git a/source/playback.c b/source/playback.c index 4c1990f..ed4d5eb 100644 --- a/source/playback.c +++ b/source/playback.c @@ -206,9 +206,7 @@ void playFile(void* infoIn) while(stop == false) { - gfxSwapBuffers(); - gfxFlushBuffers(); - gspWaitForVBlank(); + svcSleepThread(100 * 1000); /* When the last buffer has finished playing, break. */ if(lastbuf == true && waveBuf[0].status == NDSP_WBUF_DONE && From ae4b713bf631a2dfa73fe153d9df376b307bac2d Mon Sep 17 00:00:00 2001 From: Mahyar Koshkouei Date: Wed, 15 Feb 2017 17:25:02 +0000 Subject: [PATCH 5/5] Add new key mappings Signed-off-by: Mahyar Koshkouei --- source/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/main.c b/source/main.c index d486ece..013e715 100644 --- a/source/main.c +++ b/source/main.c @@ -32,7 +32,8 @@ static void showControls(void) "Stop: L+B\n" "A: Open File\n" "B: Go up folder\n" - "Start: Exit\n"); + "Start: Exit\n" + "Browse: Up, Down, Left or Right\n"); } /**