Fix pausing

Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
Mahyar Koshkouei
2017-02-09 17:36:29 +00:00
parent a06e17a66a
commit 3df3461027
6 changed files with 56 additions and 62 deletions

View File

@@ -51,7 +51,7 @@ EXTRA_OUTPUT_FILES :=
LIBRARY_DIRS := $(DEVKITPRO)/libctru $(DEVKITPRO)/portlibs/armv6k LIBRARY_DIRS := $(DEVKITPRO)/libctru $(DEVKITPRO)/portlibs/armv6k
LIBRARIES := mpg123 opusfile opus ogg ctru m LIBRARIES := mpg123 opusfile opus ogg ctru m
BUILD_FLAGS := -Os BUILD_FLAGS :=
RUN_FLAGS := RUN_FLAGS :=
VERSION_PARTS := $(subst ., ,$(shell git describe --tags --abbrev=0)) VERSION_PARTS := $(subst ., ,$(shell git describe --tags --abbrev=0))

View File

@@ -23,12 +23,3 @@ struct decoder_fn
uint64_t (* decode)(void*); uint64_t (* decode)(void*);
void (* exit)(void); void (* exit)(void);
}; };
/* Controls playback status */
struct playback_t
{
bool stop;
bool pause;
char* file;
int err;
};

View File

@@ -26,8 +26,6 @@ int main(int argc, char **argv)
int fileMax; int fileMax;
int fileNum = 0; int fileNum = 0;
int from = 0; int from = 0;
static Thread playbackThread = NULL;
struct playback_t playbackInfo;
gfxInitDefault(); gfxInitDefault();
sdmcInit(); sdmcInit();
@@ -80,16 +78,17 @@ int main(int argc, char **argv)
if(kDown) if(kDown)
mill = osGetTime(); mill = osGetTime();
if((kHeld & KEY_L) && (kDown & KEY_R)) if((kHeld & KEY_L) && (kDown & (KEY_R | KEY_UP)))
{ {
printf("Stopping? %p\n", &playbackInfo); togglePlayback();
playbackInfo.stop = true; continue;
} }
if(kDown & KEY_X) if(kDown & KEY_X)
{ {
playbackInfo.pause = !playbackInfo.pause; stopPlayback();
printf("Pausing?"); changeFile(NULL);
continue;
} }
if((kDown & KEY_UP || if((kDown & KEY_UP ||
@@ -175,7 +174,7 @@ int main(int argc, char **argv)
} }
consoleSelect(&topScreen); consoleSelect(&topScreen);
changeFile(ep->d_name, &playbackThread, &playbackInfo); changeFile(ep->d_name);
consoleSelect(&bottomScreen); consoleSelect(&bottomScreen);
if(closedir(dp) != 0) if(closedir(dp) != 0)
@@ -194,9 +193,8 @@ int main(int argc, char **argv)
out: out:
puts("Exiting..."); puts("Exiting...");
/* TODO: Stop all threads */ /* TODO: Stop all threads */
playbackInfo.stop = true; stopPlayback();
threadJoin(playbackThread, 5 * 1000 * 1000); changeFile(NULL);
threadFree(playbackThread);
gfxExit(); gfxExit();
sdmcExit(); sdmcExit();
@@ -219,36 +217,36 @@ err:
goto out; goto out;
} }
static int changeFile(const char* ep_file, Thread* playbackThread, struct playback_t* playbackInfoIn) static int changeFile(const char* ep_file)
{ {
s32 prio; s32 prio;
Thread thread = *playbackThread; static Thread thread = NULL;
struct playback_t playbackInfo = *playbackInfoIn; static char* file = NULL;
printf("struct: %p, %p\n", playbackInfoIn, &playbackInfo);
/* If music is playing, stop it */ /* If music is playing, stop it */
if(thread != NULL) if(thread != NULL)
{ {
/* Tell the thread to stop playback before we join it */ /* Tell the thread to stop playback before we join it */
playbackInfo.stop = true; stopPlayback();
puts("Stopping"); puts("Stopping");
/* Wait 5 seconds for playback to stop */ threadJoin(thread, U64_MAX);
threadJoin(thread, 5 * 1000 * 1000);
threadFree(thread); threadFree(thread);
thread = NULL;
/* free allocated file string */ /* free allocated file string */
delete(playbackInfo.file); delete(file);
} }
memset(&playbackInfo, 0, sizeof(playbackInfo)); if(ep_file == NULL)
playbackInfo.file = strdup(ep_file); return 0;
printf("file: %s\n", ep_file);
printf("file: %s\n", playbackInfo.file); file = strdup(ep_file);
printf("Playing: %s\n", file);
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE); svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
thread = threadCreate(playFile, &playbackInfo, 32 * 1024, prio - 1, thread = threadCreate(playFile, file, 32 * 1024, prio - 1,
-2, false); -2, true);
return 0; return 0;
} }

View File

@@ -13,7 +13,7 @@
/* 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 int changeFile(const char* ep_file, Thread* thread, struct playback_t* playbackInfo); static int changeFile(const char* ep_file);
/** /**
* Get number of files in current working folder * Get number of files in current working folder

View File

@@ -9,20 +9,22 @@
#include "playback.h" #include "playback.h"
#include "wav.h" #include "wav.h"
void playFile(void* playbackInfoIn) static bool stop = false;
void playFile(void* fileIn)
{ {
struct decoder_fn decoder; struct decoder_fn decoder;
struct playback_t* playbackInfo = playbackInfoIn;
int16_t* buffer1 = NULL; int16_t* buffer1 = NULL;
int16_t* buffer2 = NULL; int16_t* buffer2 = NULL;
ndspWaveBuf waveBuf[2]; ndspWaveBuf waveBuf[2];
bool playing = true;
bool lastbuf = false; bool lastbuf = false;
int ret = -1; int ret = -1;
const char* file = playbackInfo->file; const char* file = fileIn;
printf("info: %p,%p\n", playbackInfo, playbackInfoIn); /* Reset previous stop command */
stop = false;
printf("Here %d\n", __LINE__);
switch(errno = getFileType(file)) switch(errno = getFileType(file))
{ {
case FILE_TYPE_WAV: case FILE_TYPE_WAV:
@@ -42,10 +44,10 @@ void playFile(void* playbackInfoIn)
break; break;
default: default:
playbackInfo->err = errno;
return; return;
} }
printf("Here %d\n", __LINE__);
if(R_FAILED(ndspInit())) if(R_FAILED(ndspInit()))
{ {
printf("Initialising ndsp failed."); printf("Initialising ndsp failed.");
@@ -61,6 +63,7 @@ void playFile(void* playbackInfoIn)
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 #ifdef DEBUG
printf("\nRate: %lu\tChan: %d\n", (*decoder.rate)(), (*decoder.channels)()); printf("\nRate: %lu\tChan: %d\n", (*decoder.rate)(), (*decoder.channels)());
#endif #endif
@@ -83,15 +86,14 @@ void playFile(void* playbackInfoIn)
waveBuf[1].data_vaddr = &buffer2[0]; waveBuf[1].data_vaddr = &buffer2[0];
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]); ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
printf("Playing %s\n", file); 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.
*/ */
while(ndspChnIsPlaying(CHANNEL) == false); while(ndspChnIsPlaying(CHANNEL) == false);
while(playing == false || ndspChnIsPlaying(CHANNEL) == true) while(stop == false)
{ {
/* Number of bytes read from file. /* Number of bytes read from file.
* Static only for the purposes of the printf debug at the bottom. * Static only for the purposes of the printf debug at the bottom.
@@ -102,23 +104,9 @@ void playFile(void* playbackInfoIn)
gfxFlushBuffers(); gfxFlushBuffers();
gspWaitForVBlank(); gspWaitForVBlank();
//svcSleepThread(100 * 1000); svcSleepThread(100 * 1000);
if(playbackInfo->stop == true) if(ndspChnIsPaused(CHANNEL) == true || lastbuf == true)
{
puts("Stop recieved");
break;
}
if(playbackInfo->pause != ndspChnIsPaused(CHANNEL))
{
puts("Toggle");
/* TODO: Change playing to paused */
playing = !playbackInfo->pause;
ndspChnSetPaused(CHANNEL, playbackInfo->pause);
}
if(playing == false || lastbuf == true)
continue; continue;
if(waveBuf[0].status == NDSP_WBUF_DONE) if(waveBuf[0].status == NDSP_WBUF_DONE)
@@ -155,6 +143,7 @@ void playFile(void* playbackInfoIn)
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"); printf("\nStopping playback.\n");
(*decoder.exit)(); (*decoder.exit)();
@@ -162,9 +151,20 @@ out:
ndspExit(); ndspExit();
linearFree(buffer1); linearFree(buffer1);
linearFree(buffer2); linearFree(buffer2);
threadExit(0);
return; return;
} }
void togglePlayback(void)
{
ndspChnSetPaused(CHANNEL, !ndspChnIsPaused(CHANNEL));
}
void stopPlayback(void)
{
stop = true;
}
/** /**
* Obtains file type. * Obtains file type.
* *
@@ -241,5 +241,6 @@ int getFileType(const char *file)
} }
fclose(ftest); fclose(ftest);
printf("Here %d\n", __LINE__);
return file_type; return file_type;
} }

View File

@@ -7,7 +7,11 @@ enum file_types {
FILE_TYPE_MP3 FILE_TYPE_MP3
}; };
void playFile(void* playbackInfoIn); void playFile(void* fileIn);
void togglePlayback(void);
void stopPlayback(void);
/** /**
* Obtains file type. * Obtains file type.