Skip to content

Commit

Permalink
player: add time measurement options
Browse files Browse the repository at this point in the history
  • Loading branch information
ValleyBell committed Dec 22, 2023
1 parent 91b6542 commit 77909e3
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 20 deletions.
14 changes: 11 additions & 3 deletions player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ static UINT32 masterVol = 0x10000; // fixed point 16.16
static UINT8 showTags = 1;
static bool showFileInfo = false;
static UINT8 logLevel = DEVLOG_INFO;
static UINT8 pbTimeMode = PLAYTIME_LOOP_INCL | PLAYTIME_TIME_FILE;

static PlayerA mainPlr;

Expand Down Expand Up @@ -364,7 +365,7 @@ int main(int argc, char* argv[])
pState = "Playing";
if (vgmPcmStrms == NULL || vgmPcmStrms->empty())
{
printf("%s %.2f / %.2f ... \r", pState, mainPlr.GetCurTime(1), mainPlr.GetTotalTime(1));
printf("%s %.2f / %.2f ... \r", pState, mainPlr.GetCurTime(pbTimeMode), mainPlr.GetTotalTime(pbTimeMode));
}
else
{
Expand All @@ -377,7 +378,7 @@ int main(int argc, char* argv[])
if (pbMode.length() == 1)
pbMode = "";
printf("%s %.2f / %.2f [%02X / %02X at %4.1f KHz%s] ... \r",
pState, mainPlr.GetCurTime(1), mainPlr.GetTotalTime(1),
pState, mainPlr.GetCurTime(pbTimeMode), mainPlr.GetTotalTime(pbTimeMode),
1 + strmDev->lastItem, strmDev->maxItems, strmDev->freq / 1000.0,
pbMode.c_str());
}
Expand Down Expand Up @@ -509,6 +510,7 @@ Sound Chip ID:
T param - show tags (0/D/OFF - off, 1/E/ON - on)
FI param - show file information (see above)
LL param - set log level (0..5 = off/error/warn/info/debug/trace, see emu/EmuStructs.h)
TD param - set time display mode (bit mask: 0/1 = exclude/include loops, 0/2 = file/playback time, 4 = with fade time)
Q - quit
P - player configuration
SPD param - set playback speed (1.0 = 100%)
Expand Down Expand Up @@ -913,7 +915,7 @@ static void DoChipControlMode(PlayerBase* player)
char* tokenStr;

// Tags / FileInfo
printf("Command [T/FI/LL data]: ");
printf("Command [T/FI/LL/TD data]: ");
fgets(line, 0x80, stdin);
StripNewline(line);

Expand Down Expand Up @@ -951,6 +953,12 @@ static void DoChipControlMode(PlayerBase* player)
if (endPtr > tokenStr)
logLevel = newLevel;
}
else if (! strcmp(line, "TD"))
{
UINT8 newTimeMode = (UINT8)strtoul(tokenStr, &endPtr, 0);
if (endPtr > tokenStr)
pbTimeMode = newTimeMode;
}
else if (! strcmp(line, "Q"))
mode = -1;
else
Expand Down
5 changes: 5 additions & 0 deletions player/droplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ UINT8 DROPlayer::SetSampleRate(UINT32 sampleRate)
return 0x00;
}

double DROPlayer::GetPlaybackSpeed(void) const
{
return _playOpts.genOpts.pbSpeed / (double)0x10000;
}

UINT8 DROPlayer::SetPlaybackSpeed(double speed)
{
_playOpts.genOpts.pbSpeed = (UINT32)(0x10000 * speed);
Expand Down
1 change: 1 addition & 0 deletions player/droplayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class DROPlayer : public PlayerBase

//UINT32 GetSampleRate(void) const;
UINT8 SetSampleRate(UINT32 sampleRate);
double GetPlaybackSpeed(void) const;
UINT8 SetPlaybackSpeed(double speed);
//void SetEventCallback(PLAYER_EVENT_CB cbFunc, void* cbParam);
UINT32 Tick2Sample(UINT32 ticks) const;
Expand Down
5 changes: 5 additions & 0 deletions player/gymplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ UINT8 GYMPlayer::SetSampleRate(UINT32 sampleRate)
return 0x00;
}

double GYMPlayer::GetPlaybackSpeed(void) const
{
return _playOpts.genOpts.pbSpeed / (double)0x10000;
}

UINT8 GYMPlayer::SetPlaybackSpeed(double speed)
{
_playOpts.genOpts.pbSpeed = (UINT32)(0x10000 * speed);
Expand Down
1 change: 1 addition & 0 deletions player/gymplayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class GYMPlayer : public PlayerBase

//UINT32 GetSampleRate(void) const;
UINT8 SetSampleRate(UINT32 sampleRate);
double GetPlaybackSpeed(void) const;
UINT8 SetPlaybackSpeed(double speed);
//void SetEventCallback(PLAYER_EVENT_CB cbFunc, void* cbParam);
UINT32 Tick2Sample(UINT32 ticks) const;
Expand Down
41 changes: 28 additions & 13 deletions player/playera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,29 +300,44 @@ UINT32 PlayerA::GetCurPos(UINT8 unit) const
return _player->GetCurPos(unit);
}

double PlayerA::GetCurTime(UINT8 includeLoops) const
double PlayerA::GetCurTime(UINT8 flags) const
{
if (_player == NULL)
return -1.0;

// using samples here, as it may be more accurate than the (possibly low-resolution) ticks
double ticks = _player->Sample2Second(_player->GetCurPos(PLAYPOS_SAMPLE));
if (! includeLoops)
{
UINT32 curLoop = _player->GetCurLoop();
if (curLoop > 0)
ticks -= _player->Tick2Second(_player->GetLoopTicks() * curLoop);
}
return ticks;
double secs = _player->Sample2Second(_player->GetCurPos(PLAYPOS_SAMPLE));
UINT32 curLoop = _player->GetCurLoop();
if (! (flags & PLAYTIME_LOOP_INCL) && curLoop > 0)
secs -= _player->Tick2Second(_player->GetLoopTicks() * curLoop);

if (! (flags & PLAYTIME_TIME_PBK))
secs *= _player->GetPlaybackSpeed();
return secs;
}

double PlayerA::GetTotalTime(UINT8 includeLoops) const
double PlayerA::GetTotalTime(UINT8 flags) const
{
if (_player == NULL)
return -1.0;
if (includeLoops)
return _player->Tick2Second(_player->GetTotalPlayTicks(_config.loopCount));

double secs;
if (flags & PLAYTIME_LOOP_INCL)
secs = _player->Tick2Second(_player->GetTotalPlayTicks(_config.loopCount));
else
return _player->Tick2Second(_player->GetTotalPlayTicks(1));
secs = _player->Tick2Second(_player->GetTotalPlayTicks(1));
if (secs < 0.0) // indicates infinite runtime
return secs;

// Fade and silence time are unaffected by playback speed and thus must be applied before speed scaling.
if ((flags & PLAYTIME_WITH_FADE) && _player->GetLoopTicks() > 0)
secs += _player->Sample2Second(GetFadeSamples());
if (flags & PLAYTIME_WITH_SLNC)
secs += _player->Sample2Second(GetEndSilenceSamples());

if (! (flags & PLAYTIME_TIME_PBK))
secs *= _player->GetPlaybackSpeed();
return secs;
}

UINT32 PlayerA::GetCurLoop(void) const
Expand Down
11 changes: 9 additions & 2 deletions player/playera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
#define PLAYSTATE_FADE 0x10 // is fading
#define PLAYSTATE_FIN 0x20 // finished playing (file end + fading + trailing silence)

#define PLAYTIME_LOOP_EXCL 0x00 // excluding loops, jumps back in time when the file loops
#define PLAYTIME_LOOP_INCL 0x01 // including loops, no jumping back
#define PLAYTIME_TIME_FILE 0x00 // file time, progresses slower/faster when playback speed is adjusted
#define PLAYTIME_TIME_PBK 0x02 // playback time, file duration will be longer/shorter when playback speed is adjusted
#define PLAYTIME_WITH_FADE 0x10 // include fade out time (looping songs only)
#define PLAYTIME_WITH_SLNC 0x20 // include silence after songs

// TODO: find a proper name for this class
class PlayerA
{
Expand Down Expand Up @@ -54,8 +61,8 @@ class PlayerA
void SetLogCallback(PLAYER_LOG_CB cbFunc, void* cbParam);
UINT8 GetState(void) const;
UINT32 GetCurPos(UINT8 unit) const;
double GetCurTime(UINT8 includeLoops) const;
double GetTotalTime(UINT8 includeLoops) const;
double GetCurTime(UINT8 flags) const;
double GetTotalTime(UINT8 flags) const;
UINT32 GetCurLoop(void) const;
double GetLoopTime(void) const;
PlayerBase* GetPlayer(void);
Expand Down
7 changes: 6 additions & 1 deletion player/playerbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ UINT8 PlayerBase::SetSampleRate(UINT32 sampleRate)
return 0x00;
}

double PlayerBase::GetPlaybackSpeed(void) const
{
return -1; // not implemented
}

UINT8 PlayerBase::SetPlaybackSpeed(double speed)
{
return 0xFF; // not yet supported
return 0xFF; // not implemented
}

void PlayerBase::SetEventCallback(PLAYER_EVENT_CB cbFunc, void* cbParam)
Expand Down
1 change: 1 addition & 0 deletions player/playerbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class PlayerBase

virtual UINT32 GetSampleRate(void) const;
virtual UINT8 SetSampleRate(UINT32 sampleRate);
virtual double GetPlaybackSpeed(void) const;
virtual UINT8 SetPlaybackSpeed(double speed);
virtual void SetEventCallback(PLAYER_EVENT_CB cbFunc, void* cbParam);
virtual void SetFileReqCallback(PLAYER_FILEREQ_CB cbFunc, void* cbParam);
Expand Down
5 changes: 5 additions & 0 deletions player/s98player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,11 @@ UINT8 S98Player::SetSampleRate(UINT32 sampleRate)
return 0x00;
}

double S98Player::GetPlaybackSpeed(void) const
{
return _playOpts.genOpts.pbSpeed / (double)0x10000;
}

UINT8 S98Player::SetPlaybackSpeed(double speed)
{
_playOpts.genOpts.pbSpeed = (UINT32)(0x10000 * speed);
Expand Down
1 change: 1 addition & 0 deletions player/s98player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class S98Player : public PlayerBase

//UINT32 GetSampleRate(void) const;
UINT8 SetSampleRate(UINT32 sampleRate);
double GetPlaybackSpeed(void) const;
UINT8 SetPlaybackSpeed(double speed);
//void SetEventCallback(PLAYER_EVENT_CB cbFunc, void* cbParam);
UINT32 Tick2Sample(UINT32 ticks) const;
Expand Down
5 changes: 5 additions & 0 deletions player/vgmplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,11 @@ UINT8 VGMPlayer::SetSampleRate(UINT32 sampleRate)
return 0x00;
}

double VGMPlayer::GetPlaybackSpeed(void) const
{
return _playOpts.genOpts.pbSpeed / (double)0x10000;
}

UINT8 VGMPlayer::SetPlaybackSpeed(double speed)
{
_playOpts.genOpts.pbSpeed = (UINT32)(0x10000 * speed);
Expand Down
1 change: 1 addition & 0 deletions player/vgmplayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class VGMPlayer : public PlayerBase

//UINT32 GetSampleRate(void) const;
UINT8 SetSampleRate(UINT32 sampleRate);
double GetPlaybackSpeed(void) const;
UINT8 SetPlaybackSpeed(double speed);
//void SetEventCallback(PLAYER_EVENT_CB cbFunc, void* cbParam);
//void SetFileReqCallback(PLAYER_FILEREQ_CB cbFunc, void* cbParam);
Expand Down
2 changes: 1 addition & 1 deletion vgm2wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ static void frames_to_little_endian(UINT8 *data, unsigned int frame_count) {
repack_int24le(&data[3], &data[3]);
break;
}
defafult: /* 16 */ {
default: /* 16 */ {
repack_int16le(&data[0], &data[0]);
repack_int16le(&data[2], &data[2]);
break;
Expand Down

0 comments on commit 77909e3

Please sign in to comment.