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
LIBRARIES := mpg123 opusfile opus ogg ctru m
BUILD_FLAGS := -Os
BUILD_FLAGS :=
RUN_FLAGS :=
VERSION_PARTS := $(subst ., ,$(shell git describe --tags --abbrev=0))

View File

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

View File

@@ -13,7 +13,7 @@
/* Maximum number of lines that can be displayed */
#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

View File

@@ -9,20 +9,22 @@
#include "playback.h"
#include "wav.h"
void playFile(void* playbackInfoIn)
static bool stop = false;
void playFile(void* fileIn)
{
struct decoder_fn decoder;
struct playback_t* playbackInfo = playbackInfoIn;
int16_t* buffer1 = NULL;
int16_t* buffer2 = NULL;
ndspWaveBuf waveBuf[2];
bool playing = true;
bool lastbuf = false;
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))
{
case FILE_TYPE_WAV:
@@ -42,10 +44,10 @@ void playFile(void* playbackInfoIn)
break;
default:
playbackInfo->err = errno;
return;
}
printf("Here %d\n", __LINE__);
if(R_FAILED(ndspInit()))
{
printf("Initialising ndsp failed.");
@@ -61,6 +63,7 @@ void playFile(void* playbackInfoIn)
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
@@ -83,15 +86,14 @@ void playFile(void* playbackInfoIn)
waveBuf[1].data_vaddr = &buffer2[0];
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
* to the while loop. So we ensure that music has started here.
*/
while(ndspChnIsPlaying(CHANNEL) == false);
while(playing == false || ndspChnIsPlaying(CHANNEL) == true)
while(stop == false)
{
/* Number of bytes read from file.
* Static only for the purposes of the printf debug at the bottom.
@@ -102,23 +104,9 @@ void playFile(void* playbackInfoIn)
gfxFlushBuffers();
gspWaitForVBlank();
//svcSleepThread(100 * 1000);
svcSleepThread(100 * 1000);
if(playbackInfo->stop == 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)
if(ndspChnIsPaused(CHANNEL) == true || lastbuf == true)
continue;
if(waveBuf[0].status == NDSP_WBUF_DONE)
@@ -155,6 +143,7 @@ void playFile(void* playbackInfoIn)
DSP_FlushDataCache(buffer2, decoder.buffSize * sizeof(int16_t));
}
printf("Here %d\n", __LINE__);
out:
printf("\nStopping playback.\n");
(*decoder.exit)();
@@ -162,9 +151,20 @@ out:
ndspExit();
linearFree(buffer1);
linearFree(buffer2);
threadExit(0);
return;
}
void togglePlayback(void)
{
ndspChnSetPaused(CHANNEL, !ndspChnIsPaused(CHANNEL));
}
void stopPlayback(void)
{
stop = true;
}
/**
* Obtains file type.
*
@@ -241,5 +241,6 @@ int getFileType(const char *file)
}
fclose(ftest);
printf("Here %d\n", __LINE__);
return file_type;
}

View File

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