play: add current and total time of music

Signed-off-by: Mahyar Koshkouei <mk@deltabeard.com>
This commit is contained in:
Mahyar Koshkouei
2023-07-08 16:05:21 +01:00
parent ca583a5487
commit cdf49f8dcf
8 changed files with 142 additions and 30 deletions

View File

@@ -35,15 +35,6 @@ struct dirList_t
char* currentDir; char* currentDir;
}; };
/**
* Allows the playback thread to return any error messages that it may
* encounter.
*
* \param infoIn Struct containing addresses of the event, the error code,
* and an optional error string.
*/
void playbackWatchdog(void* infoIn);
/** /**
* Get number of files in current working folder * Get number of files in current working folder
* *

View File

@@ -43,12 +43,23 @@ struct decoder_fn
* Free codec resources. * Free codec resources.
*/ */
void (* exit)(void); void (* exit)(void);
/**
* Optional. Set to NULL if unavailable.
* Get number of samples in audio file.
*/
size_t (* getFileSamples)(void);
}; };
struct playbackInfo_t struct playbackInfo_t
{ {
char* file; char *file;
struct errInfo_t* errInfo; struct errInfo_t *errInfo;
/* If 0, then the duration of file is unavailable. */
size_t samples_total;
size_t samples_played;
size_t samples_per_second;
}; };
/** /**

View File

@@ -12,6 +12,7 @@ static uint32_t rateFlac(void);
static uint8_t channelFlac(void); static uint8_t channelFlac(void);
static uint64_t decodeFlac(void* buffer); static uint64_t decodeFlac(void* buffer);
static void exitFlac(void); static void exitFlac(void);
static size_t getFileSamplesFlac(void);
/** /**
* Set decoder parameters for flac. * Set decoder parameters for flac.
@@ -26,6 +27,7 @@ void setFlac(struct decoder_fn* decoder)
decoder->buffSize = buffSize; decoder->buffSize = buffSize;
decoder->decode = &decodeFlac; decoder->decode = &decodeFlac;
decoder->exit = &exitFlac; decoder->exit = &exitFlac;
decoder->getFileSamples = &getFileSamplesFlac;
} }
/** /**
@@ -34,19 +36,24 @@ void setFlac(struct decoder_fn* decoder)
* \param file Location of flac file to play. * \param file Location of flac file to play.
* \return 0 on success, else failure. * \return 0 on success, else failure.
*/ */
int initFlac(const char* file) static int initFlac(const char* file)
{ {
pFlac = drflac_open_file(file, NULL); pFlac = drflac_open_file(file, NULL);
return pFlac == NULL ? -1 : 0; return pFlac == NULL ? -1 : 0;
} }
static size_t getFileSamplesFlac(void)
{
return pFlac->totalPCMFrameCount * (size_t)pFlac->channels;
}
/** /**
* Get sampling rate of Flac file. * Get sampling rate of Flac file.
* *
* \return Sampling rate. * \return Sampling rate.
*/ */
uint32_t rateFlac(void) static uint32_t rateFlac(void)
{ {
return pFlac->sampleRate; return pFlac->sampleRate;
} }
@@ -56,7 +63,7 @@ uint32_t rateFlac(void)
* *
* \return Number of channels for opened file. * \return Number of channels for opened file.
*/ */
uint8_t channelFlac(void) static uint8_t channelFlac(void)
{ {
return pFlac->channels; return pFlac->channels;
} }
@@ -67,7 +74,7 @@ uint8_t channelFlac(void)
* \param buffer Decoded output. * \param buffer Decoded output.
* \return Samples read for each channel. * \return Samples read for each channel.
*/ */
uint64_t decodeFlac(void* buffer) static uint64_t decodeFlac(void* buffer)
{ {
size_t buffSizeFrames; size_t buffSizeFrames;
uint64_t samplesRead; uint64_t samplesRead;
@@ -81,7 +88,7 @@ uint64_t decodeFlac(void* buffer)
/** /**
* Free Flac decoder. * Free Flac decoder.
*/ */
void exitFlac(void) static void exitFlac(void)
{ {
drflac_close(pFlac); drflac_close(pFlac);
} }

View File

@@ -119,6 +119,10 @@ static int changeFile(const char* ep_file, struct playbackInfo_t* playbackInfo)
playbackInfo->file = strdup(ep_file); playbackInfo->file = strdup(ep_file);
printf("Playing: %s\n", playbackInfo->file); printf("Playing: %s\n", playbackInfo->file);
playbackInfo->samples_total = 0;
playbackInfo->samples_played = 0;
playbackInfo->samples_per_second = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE); svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
thread = threadCreate(playFile, playbackInfo, 32 * 1024, prio - 1, -2, false); thread = threadCreate(playFile, playbackInfo, 32 * 1024, prio - 1, -2, false);
@@ -286,22 +290,27 @@ err:
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
PrintConsole topScreen; PrintConsole topScreenLog, topScreenInfo, bottomScreen;
PrintConsole bottomScreen; int fileMax;
int fileMax; int fileNum = 0;
int fileNum = 0; int from = 0;
int from = 0;
Thread watchdogThread; Thread watchdogThread;
Handle playbackFailEvent; Handle playbackFailEvent;
struct watchdogInfo watchdogInfoIn; struct watchdogInfo watchdogInfoIn;
struct errInfo_t errInfo; struct errInfo_t errInfo;
struct playbackInfo_t playbackInfo; struct playbackInfo_t playbackInfo = { 0 };
volatile int error = 0; volatile int error = 0;
struct dirList_t dirList = {NULL, 0, NULL, 0, NULL}; struct dirList_t dirList = { 0 };
gfxInitDefault(); gfxInitDefault();
consoleInit(GFX_TOP, &topScreen); consoleInit(GFX_TOP, &topScreenLog);
consoleInit(GFX_TOP, &topScreenInfo);
consoleInit(GFX_BOTTOM, &bottomScreen); consoleInit(GFX_BOTTOM, &bottomScreen);
/* Set console sizes. */
consoleSetWindow(&topScreenLog, 1, 3, 50, 36);
consoleSetWindow(&topScreenInfo, 1, 1, 50, 2);
consoleSelect(&bottomScreen); consoleSelect(&bottomScreen);
svcCreateEvent(&playbackFailEvent, RESET_ONESHOT); svcCreateEvent(&playbackFailEvent, RESET_ONESHOT);
@@ -309,7 +318,7 @@ int main(int argc, char **argv)
errInfo.failEvent = &playbackFailEvent; errInfo.failEvent = &playbackFailEvent;
errInfo.errstr = NULL; errInfo.errstr = NULL;
watchdogInfoIn.screen = &topScreen; watchdogInfoIn.screen = &topScreenLog;
watchdogInfoIn.errInfo = &errInfo; watchdogInfoIn.errInfo = &errInfo;
watchdogThread = threadCreate(playbackWatchdog, watchdogThread = threadCreate(playbackWatchdog,
&watchdogInfoIn, 4 * 1024, 0x20, -2, true); &watchdogInfoIn, 4 * 1024, 0x20, -2, true);
@@ -362,7 +371,7 @@ int main(int argc, char **argv)
break; break;
#ifdef DEBUG #ifdef DEBUG
consoleSelect(&topScreen); consoleSelect(&topScreenLog);
printf("\rNum: %d, Max: %d, from: %d ", fileNum, fileMax, from); printf("\rNum: %d, Max: %d, from: %d ", fileNum, fileMax, from);
consoleSelect(&bottomScreen); consoleSelect(&bottomScreen);
#endif #endif
@@ -377,7 +386,7 @@ int main(int argc, char **argv)
if(isPlaying() == false) if(isPlaying() == false)
continue; continue;
consoleSelect(&topScreen); consoleSelect(&topScreenLog);
if(togglePlayback() == true) if(togglePlayback() == true)
puts("Paused"); puts("Paused");
else else
@@ -389,7 +398,7 @@ int main(int argc, char **argv)
/* Show controls */ /* Show controls */
if(kDown & KEY_LEFT) if(kDown & KEY_LEFT)
{ {
consoleSelect(&topScreen); consoleSelect(&topScreenLog);
showControls(); showControls();
continue; continue;
} }
@@ -398,7 +407,15 @@ int main(int argc, char **argv)
if(kDown & KEY_B) if(kDown & KEY_B)
{ {
stopPlayback(); stopPlayback();
/* Clear playback information. */
consoleSelect(&topScreenInfo);
consoleClear();
consoleSelect(&topScreenLog);
consoleClear();
changeFile(NULL, &playbackInfo); changeFile(NULL, &playbackInfo);
/* If the playback thread is currently playing, it will now /* If the playback thread is currently playing, it will now
* stop and tell the Watchdog thread to display "Stopped". * stop and tell the Watchdog thread to display "Stopped".
*/ */
@@ -518,11 +535,56 @@ int main(int argc, char **argv)
if(dirList.dirNum < fileNum) if(dirList.dirNum < fileNum)
{ {
consoleSelect(&topScreen); consoleSelect(&topScreenInfo);
consoleClear();
consoleSelect(&topScreenLog);
consoleClear();
changeFile(dirList.files[fileNum - dirList.dirNum - 1], &playbackInfo); changeFile(dirList.files[fileNum - dirList.dirNum - 1], &playbackInfo);
continue; continue;
} }
} }
/* After 1000ms, update playback time. */
while(osGetTime() - mill > 1000)
{
consoleSelect(&topScreenInfo);
/* Reset cursor position and print status. */
printf("\033[0;0H");
/* Avoid divide by zero. */
if(playbackInfo.samples_per_second == 0)
break;
{
unsigned hr, min, sec;
size_t seconds_played;
seconds_played = playbackInfo.samples_played / playbackInfo.samples_per_second;
hr = (seconds_played/3600);
min = (seconds_played - (3600*hr))/60;
sec = (seconds_played -(3600*hr)-(min*60));
printf("%02d:%02d:%02d", hr, min, sec);
}
if(playbackInfo.samples_total != 0)
{
unsigned hr, min, sec;
size_t seconds_total;
seconds_total = playbackInfo.samples_total / playbackInfo.samples_per_second;
hr = (seconds_total/3600);
min = (seconds_total - (3600*hr))/60;
sec = (seconds_total -(3600*hr)-(min*60));
printf(" %02d:%02d:%02d", hr, min, sec);
}
break;
}
} }
out: out:

View File

@@ -16,6 +16,7 @@ static uint32_t rateMp3(void);
static uint8_t channelMp3(void); static uint8_t channelMp3(void);
static uint64_t decodeMp3(void* buffer); static uint64_t decodeMp3(void* buffer);
static void exitMp3(void); static void exitMp3(void);
static size_t getFileSamplesMp3(void);
/** /**
* Set decoder parameters for MP3. * Set decoder parameters for MP3.
@@ -34,6 +35,16 @@ void setMp3(struct decoder_fn* decoder)
buffSize = &(decoder->buffSize); buffSize = &(decoder->buffSize);
decoder->decode = &decodeMp3; decoder->decode = &decodeMp3;
decoder->exit = &exitMp3; decoder->exit = &exitMp3;
decoder->getFileSamples = &getFileSamplesMp3;
}
static size_t getFileSamplesMp3(void)
{
off_t len = mpg123_length(mh);
if(len == MPG123_ERR)
return 0;
return len * (size_t)channels;
} }
/** /**

View File

@@ -14,6 +14,7 @@ static uint8_t channelOpus(void);
static uint64_t decodeOpus(void* buffer); static uint64_t decodeOpus(void* buffer);
static void exitOpus(void); static void exitOpus(void);
static uint64_t fillOpusBuffer(int16_t* bufferOut); static uint64_t fillOpusBuffer(int16_t* bufferOut);
static size_t getFileSamplesOpus(void);
/** /**
* Set decoder parameters for Opus. * Set decoder parameters for Opus.
@@ -28,6 +29,17 @@ void setOpus(struct decoder_fn* decoder)
decoder->buffSize = buffSize; decoder->buffSize = buffSize;
decoder->decode = &decodeOpus; decoder->decode = &decodeOpus;
decoder->exit = &exitOpus; decoder->exit = &exitOpus;
decoder->getFileSamples = &getFileSamplesOpus;
}
static size_t getFileSamplesOpus(void)
{
ogg_int64_t len = op_pcm_total(opusFile, -1);
if(len == OP_EINVAL)
return 0;
return len * (size_t)channelOpus();
} }
/** /**

View File

@@ -53,7 +53,7 @@ bool isPlaying(void)
*/ */
void playFile(void* infoIn) void playFile(void* infoIn)
{ {
struct decoder_fn decoder; struct decoder_fn decoder = { 0 };
struct playbackInfo_t* info = infoIn; struct playbackInfo_t* info = infoIn;
int16_t* buffer1 = NULL; int16_t* buffer1 = NULL;
int16_t* buffer2 = NULL; int16_t* buffer2 = NULL;
@@ -116,6 +116,10 @@ void playFile(void* infoIn)
goto err; goto err;
} }
if(decoder.getFileSamples != NULL)
info->samples_total = decoder.getFileSamples();
info->samples_per_second = decoder.rate() * decoder.channels();
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));
@@ -158,6 +162,9 @@ void playFile(void* infoIn)
if(waveBuf[0].status == NDSP_WBUF_DONE) if(waveBuf[0].status == NDSP_WBUF_DONE)
{ {
size_t read = (*decoder.decode)(&buffer1[0]); size_t read = (*decoder.decode)(&buffer1[0]);
/* The previous block of samples have finished playing,
* so accumulate them here. */
info->samples_played += waveBuf[0].nsamples * decoder.channels();
if(read <= 0) if(read <= 0)
{ {
@@ -173,6 +180,7 @@ void playFile(void* infoIn)
if(waveBuf[1].status == NDSP_WBUF_DONE) if(waveBuf[1].status == NDSP_WBUF_DONE)
{ {
size_t read = (*decoder.decode)(&buffer2[0]); size_t read = (*decoder.decode)(&buffer2[0]);
info->samples_played += waveBuf[0].nsamples * decoder.channels();
if(read <= 0) if(read <= 0)
{ {
@@ -189,6 +197,9 @@ void playFile(void* infoIn)
DSP_FlushDataCache(buffer2, decoder.buffSize * sizeof(int16_t)); DSP_FlushDataCache(buffer2, decoder.buffSize * sizeof(int16_t));
} }
info->samples_played += waveBuf[0].nsamples * decoder.channels();
info->samples_played += waveBuf[0].nsamples * decoder.channels();
(*decoder.exit)(); (*decoder.exit)();
out: out:
if(isNdspInit == true) if(isNdspInit == true)

View File

@@ -16,6 +16,7 @@ static uint32_t rateWav(void);
static uint8_t channelWav(void); static uint8_t channelWav(void);
static uint64_t readWav(void* buffer); static uint64_t readWav(void* buffer);
static void exitWav(void); static void exitWav(void);
static size_t getFileSamplesWav(void);
/** /**
* Set decoder parameters for WAV. * Set decoder parameters for WAV.
@@ -30,6 +31,7 @@ void setWav(struct decoder_fn* decoder)
decoder->buffSize = buffSize; decoder->buffSize = buffSize;
decoder->decode = &readWav; decoder->decode = &readWav;
decoder->exit = &exitWav; decoder->exit = &exitWav;
decoder->getFileSamples = &getFileSamplesWav;
} }
/** /**
@@ -43,6 +45,11 @@ int initWav(const char* file)
return !drwav_init_file(&wav, file, NULL); return !drwav_init_file(&wav, file, NULL);
} }
static size_t getFileSamplesWav(void)
{
return wav.totalPCMFrameCount * (size_t)wav.channels;
}
/** /**
* Get sampling rate of Wav file. * Get sampling rate of Wav file.
* *