play: add current and total time of music
Signed-off-by: Mahyar Koshkouei <mk@deltabeard.com>
This commit is contained in:
@@ -35,15 +35,6 @@ struct dirList_t
|
||||
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
|
||||
*
|
||||
|
||||
@@ -43,12 +43,23 @@ struct decoder_fn
|
||||
* Free codec resources.
|
||||
*/
|
||||
void (* exit)(void);
|
||||
|
||||
/**
|
||||
* Optional. Set to NULL if unavailable.
|
||||
* Get number of samples in audio file.
|
||||
*/
|
||||
size_t (* getFileSamples)(void);
|
||||
};
|
||||
|
||||
struct playbackInfo_t
|
||||
{
|
||||
char* file;
|
||||
struct errInfo_t* errInfo;
|
||||
char *file;
|
||||
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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,6 +12,7 @@ static uint32_t rateFlac(void);
|
||||
static uint8_t channelFlac(void);
|
||||
static uint64_t decodeFlac(void* buffer);
|
||||
static void exitFlac(void);
|
||||
static size_t getFileSamplesFlac(void);
|
||||
|
||||
/**
|
||||
* Set decoder parameters for flac.
|
||||
@@ -26,6 +27,7 @@ void setFlac(struct decoder_fn* decoder)
|
||||
decoder->buffSize = buffSize;
|
||||
decoder->decode = &decodeFlac;
|
||||
decoder->exit = &exitFlac;
|
||||
decoder->getFileSamples = &getFileSamplesFlac;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,19 +36,24 @@ void setFlac(struct decoder_fn* decoder)
|
||||
* \param file Location of flac file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initFlac(const char* file)
|
||||
static int initFlac(const char* file)
|
||||
{
|
||||
pFlac = drflac_open_file(file, NULL);
|
||||
|
||||
return pFlac == NULL ? -1 : 0;
|
||||
}
|
||||
|
||||
static size_t getFileSamplesFlac(void)
|
||||
{
|
||||
return pFlac->totalPCMFrameCount * (size_t)pFlac->channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sampling rate of Flac file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateFlac(void)
|
||||
static uint32_t rateFlac(void)
|
||||
{
|
||||
return pFlac->sampleRate;
|
||||
}
|
||||
@@ -56,7 +63,7 @@ uint32_t rateFlac(void)
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelFlac(void)
|
||||
static uint8_t channelFlac(void)
|
||||
{
|
||||
return pFlac->channels;
|
||||
}
|
||||
@@ -67,7 +74,7 @@ uint8_t channelFlac(void)
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeFlac(void* buffer)
|
||||
static uint64_t decodeFlac(void* buffer)
|
||||
{
|
||||
size_t buffSizeFrames;
|
||||
uint64_t samplesRead;
|
||||
@@ -81,7 +88,7 @@ uint64_t decodeFlac(void* buffer)
|
||||
/**
|
||||
* Free Flac decoder.
|
||||
*/
|
||||
void exitFlac(void)
|
||||
static void exitFlac(void)
|
||||
{
|
||||
drflac_close(pFlac);
|
||||
}
|
||||
|
||||
@@ -119,6 +119,10 @@ static int changeFile(const char* ep_file, struct playbackInfo_t* playbackInfo)
|
||||
playbackInfo->file = strdup(ep_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);
|
||||
thread = threadCreate(playFile, playbackInfo, 32 * 1024, prio - 1, -2, false);
|
||||
|
||||
@@ -286,8 +290,7 @@ err:
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
PrintConsole topScreen;
|
||||
PrintConsole bottomScreen;
|
||||
PrintConsole topScreenLog, topScreenInfo, bottomScreen;
|
||||
int fileMax;
|
||||
int fileNum = 0;
|
||||
int from = 0;
|
||||
@@ -295,13 +298,19 @@ int main(int argc, char **argv)
|
||||
Handle playbackFailEvent;
|
||||
struct watchdogInfo watchdogInfoIn;
|
||||
struct errInfo_t errInfo;
|
||||
struct playbackInfo_t playbackInfo;
|
||||
struct playbackInfo_t playbackInfo = { 0 };
|
||||
volatile int error = 0;
|
||||
struct dirList_t dirList = {NULL, 0, NULL, 0, NULL};
|
||||
struct dirList_t dirList = { 0 };
|
||||
|
||||
gfxInitDefault();
|
||||
consoleInit(GFX_TOP, &topScreen);
|
||||
consoleInit(GFX_TOP, &topScreenLog);
|
||||
consoleInit(GFX_TOP, &topScreenInfo);
|
||||
consoleInit(GFX_BOTTOM, &bottomScreen);
|
||||
|
||||
/* Set console sizes. */
|
||||
consoleSetWindow(&topScreenLog, 1, 3, 50, 36);
|
||||
consoleSetWindow(&topScreenInfo, 1, 1, 50, 2);
|
||||
|
||||
consoleSelect(&bottomScreen);
|
||||
|
||||
svcCreateEvent(&playbackFailEvent, RESET_ONESHOT);
|
||||
@@ -309,7 +318,7 @@ int main(int argc, char **argv)
|
||||
errInfo.failEvent = &playbackFailEvent;
|
||||
errInfo.errstr = NULL;
|
||||
|
||||
watchdogInfoIn.screen = &topScreen;
|
||||
watchdogInfoIn.screen = &topScreenLog;
|
||||
watchdogInfoIn.errInfo = &errInfo;
|
||||
watchdogThread = threadCreate(playbackWatchdog,
|
||||
&watchdogInfoIn, 4 * 1024, 0x20, -2, true);
|
||||
@@ -362,7 +371,7 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
|
||||
#ifdef DEBUG
|
||||
consoleSelect(&topScreen);
|
||||
consoleSelect(&topScreenLog);
|
||||
printf("\rNum: %d, Max: %d, from: %d ", fileNum, fileMax, from);
|
||||
consoleSelect(&bottomScreen);
|
||||
#endif
|
||||
@@ -377,7 +386,7 @@ int main(int argc, char **argv)
|
||||
if(isPlaying() == false)
|
||||
continue;
|
||||
|
||||
consoleSelect(&topScreen);
|
||||
consoleSelect(&topScreenLog);
|
||||
if(togglePlayback() == true)
|
||||
puts("Paused");
|
||||
else
|
||||
@@ -389,7 +398,7 @@ int main(int argc, char **argv)
|
||||
/* Show controls */
|
||||
if(kDown & KEY_LEFT)
|
||||
{
|
||||
consoleSelect(&topScreen);
|
||||
consoleSelect(&topScreenLog);
|
||||
showControls();
|
||||
continue;
|
||||
}
|
||||
@@ -398,7 +407,15 @@ int main(int argc, char **argv)
|
||||
if(kDown & KEY_B)
|
||||
{
|
||||
stopPlayback();
|
||||
|
||||
/* Clear playback information. */
|
||||
consoleSelect(&topScreenInfo);
|
||||
consoleClear();
|
||||
consoleSelect(&topScreenLog);
|
||||
consoleClear();
|
||||
|
||||
changeFile(NULL, &playbackInfo);
|
||||
|
||||
/* If the playback thread is currently playing, it will now
|
||||
* stop and tell the Watchdog thread to display "Stopped".
|
||||
*/
|
||||
@@ -518,11 +535,56 @@ int main(int argc, char **argv)
|
||||
|
||||
if(dirList.dirNum < fileNum)
|
||||
{
|
||||
consoleSelect(&topScreen);
|
||||
consoleSelect(&topScreenInfo);
|
||||
consoleClear();
|
||||
consoleSelect(&topScreenLog);
|
||||
consoleClear();
|
||||
|
||||
changeFile(dirList.files[fileNum - dirList.dirNum - 1], &playbackInfo);
|
||||
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:
|
||||
|
||||
11
source/mp3.c
11
source/mp3.c
@@ -16,6 +16,7 @@ static uint32_t rateMp3(void);
|
||||
static uint8_t channelMp3(void);
|
||||
static uint64_t decodeMp3(void* buffer);
|
||||
static void exitMp3(void);
|
||||
static size_t getFileSamplesMp3(void);
|
||||
|
||||
/**
|
||||
* Set decoder parameters for MP3.
|
||||
@@ -34,6 +35,16 @@ void setMp3(struct decoder_fn* decoder)
|
||||
buffSize = &(decoder->buffSize);
|
||||
decoder->decode = &decodeMp3;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,7 @@ static uint8_t channelOpus(void);
|
||||
static uint64_t decodeOpus(void* buffer);
|
||||
static void exitOpus(void);
|
||||
static uint64_t fillOpusBuffer(int16_t* bufferOut);
|
||||
static size_t getFileSamplesOpus(void);
|
||||
|
||||
/**
|
||||
* Set decoder parameters for Opus.
|
||||
@@ -28,6 +29,17 @@ void setOpus(struct decoder_fn* decoder)
|
||||
decoder->buffSize = buffSize;
|
||||
decoder->decode = &decodeOpus;
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -53,7 +53,7 @@ bool isPlaying(void)
|
||||
*/
|
||||
void playFile(void* infoIn)
|
||||
{
|
||||
struct decoder_fn decoder;
|
||||
struct decoder_fn decoder = { 0 };
|
||||
struct playbackInfo_t* info = infoIn;
|
||||
int16_t* buffer1 = NULL;
|
||||
int16_t* buffer2 = NULL;
|
||||
@@ -116,6 +116,10 @@ void playFile(void* infoIn)
|
||||
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));
|
||||
buffer2 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||
|
||||
@@ -158,6 +162,9 @@ void playFile(void* infoIn)
|
||||
if(waveBuf[0].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -173,6 +180,7 @@ void playFile(void* infoIn)
|
||||
if(waveBuf[1].status == NDSP_WBUF_DONE)
|
||||
{
|
||||
size_t read = (*decoder.decode)(&buffer2[0]);
|
||||
info->samples_played += waveBuf[0].nsamples * decoder.channels();
|
||||
|
||||
if(read <= 0)
|
||||
{
|
||||
@@ -189,6 +197,9 @@ void playFile(void* infoIn)
|
||||
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)();
|
||||
out:
|
||||
if(isNdspInit == true)
|
||||
|
||||
@@ -16,6 +16,7 @@ static uint32_t rateWav(void);
|
||||
static uint8_t channelWav(void);
|
||||
static uint64_t readWav(void* buffer);
|
||||
static void exitWav(void);
|
||||
static size_t getFileSamplesWav(void);
|
||||
|
||||
/**
|
||||
* Set decoder parameters for WAV.
|
||||
@@ -30,6 +31,7 @@ void setWav(struct decoder_fn* decoder)
|
||||
decoder->buffSize = buffSize;
|
||||
decoder->decode = &readWav;
|
||||
decoder->exit = &exitWav;
|
||||
decoder->getFileSamples = &getFileSamplesWav;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,6 +45,11 @@ int initWav(const char* file)
|
||||
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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user