Skip to content

Commit

Permalink
player: add logging via callback, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ValleyBell committed Nov 6, 2021
1 parent a7215f4 commit f9eb6a7
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 31 deletions.
30 changes: 28 additions & 2 deletions player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ static const char* GetFileTitle(const char* filePath);
static UINT32 FillBuffer(void* drvStruct, void* userParam, UINT32 bufSize, void* Data);
static UINT8 FilePlayCallback(PlayerBase* player, void* userParam, UINT8 evtType, void* evtParam);
static DATA_LOADER* RequestFileCallback(void* userParam, PlayerBase* player, const char* fileName);
static const char* LogLevel2Str(UINT8 level);
static void PlayerLogCallback(void* userParam, PlayerBase* player, UINT8 level, UINT8 srcType,
const char* srcTag, const char* message);
static UINT32 GetNthAudioDriver(UINT8 adrvType, INT32 drvNumber);
static UINT8 InitAudioSystem(void);
static UINT8 DeinitAudioSystem(void);
Expand Down Expand Up @@ -131,6 +134,7 @@ int main(int argc, char* argv[])
mainPlr.RegisterPlayerEngine(new GYMPlayer);
mainPlr.SetEventCallback(FilePlayCallback, NULL);
mainPlr.SetFileReqCallback(RequestFileCallback, NULL);
mainPlr.SetLogCallback(PlayerLogCallback, NULL);
//mainPlr.SetOutputSettings() is done in StartAudioDevice()
{
PlayerA::Config pCfg = mainPlr.GetConfiguration();
Expand Down Expand Up @@ -267,7 +271,8 @@ int main(int argc, char* argv[])
if (! (retVal & 0x80))
{
static const INT16 panPos[4] = {0x00, -0x80, +0x80, 0x00};
devOpts.emuCore[0] = FCC_MAXM;
if (! devOpts.emuCore[0])
devOpts.emuCore[0] = FCC_MAXM;
memcpy(devOpts.panOpts.chnPan, panPos, sizeof(panPos));
player->SetDeviceOptions(devOptID, devOpts);
}
Expand Down Expand Up @@ -296,7 +301,8 @@ int main(int argc, char* argv[])
retVal = player->GetDeviceOptions(devOptID, devOpts);
if (! (retVal & 0x80))
{
devOpts.emuCore[0] = FCC_MAME;
if (! devOpts.emuCore[0])
devOpts.emuCore[0] = FCC_MAME;
player->SetDeviceOptions(devOptID, devOpts);
}
}
Expand Down Expand Up @@ -943,6 +949,26 @@ static DATA_LOADER* RequestFileCallback(void* userParam, PlayerBase* player, con
return NULL;
}

static const char* LogLevel2Str(UINT8 level)
{
static const char* LVL_NAMES[6] = {" ??? ", "Error", "Warn ", "Info ", "Debug", "Trace"};
if (level >= (sizeof(LVL_NAMES) / sizeof(LVL_NAMES[0])))
level = 0;
return LVL_NAMES[level];
}

static void PlayerLogCallback(void* userParam, PlayerBase* player, UINT8 level, UINT8 srcType,
const char* srcTag, const char* message)
{
if (level >= PLRLOG_TRACE)
return; // don't print trace logs (debug is okay)
if (srcType == PLRLOGSRC_PLR)
printf("[%s] %s: %s", LogLevel2Str(level), player->GetPlayerName(), message);
else
printf("[%s] %s %s: %s", LogLevel2Str(level), player->GetPlayerName(), srcTag, message);
return;
}

static UINT32 GetNthAudioDriver(UINT8 adrvType, INT32 drvNumber)
{
// special numbers for drvNumber:
Expand Down
30 changes: 30 additions & 0 deletions player/droplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ UINT8 DROPlayer::UnloadFile(void)
_devPanning.clear();
_devCfgs.clear();
_devices.clear();
_devNames.clear();

return 0x00;
}
Expand Down Expand Up @@ -591,13 +592,27 @@ UINT32 DROPlayer::GetLoopTicks(void) const
return 0;
}

/*static*/ void DROPlayer::SndEmuLogCB(void* userParam, void* source, UINT8 level, const char* message)
{
DEVLOG_CB_DATA* cbData = (DEVLOG_CB_DATA*)userParam;
DROPlayer* player = cbData->player;
if (player->_logCbFunc == NULL)
return;
player->_logCbFunc(player->_logCbParam, player, level, PLRLOGSRC_EMU,
player->_devNames[cbData->chipDevID].c_str(), message);
return;
}


void DROPlayer::GenerateDeviceConfig(void)
{
size_t curDev;
const char* chipName;
char chipNameFull[0x10];

_devCfgs.clear();
_devCfgs.resize(_devTypes.size());
_devNames.clear();
for (curDev = 0; curDev < _devCfgs.size(); curDev ++)
{
DEV_GEN_CFG* devCfg = &_devCfgs[curDev];
Expand All @@ -607,6 +622,17 @@ void DROPlayer::GenerateDeviceConfig(void)
if (_devTypes[curDev] == DEVID_YMF262)
devCfg->clock *= 4; // OPL3 uses a 14 MHz clock
devCfg->flags = 0x00;

chipName = (_devTypes[curDev] == DEVID_YMF262) ? "OPL3" : "OPL2";
if (_devCfgs.size() <= 1)
{
_devNames.push_back(chipName);
}
else
{
snprintf(chipNameFull, 0x10, "%s #%u", chipName, 1 + (unsigned int)curDev);
_devNames.push_back(chipNameFull);
}
}

return;
Expand Down Expand Up @@ -650,6 +676,10 @@ UINT8 DROPlayer::Start(void)
}
SndEmu_GetDeviceFunc(cDev->base.defInf.devDef, RWF_REGISTER | RWF_WRITE, DEVRW_A8D8, 0, (void**)&cDev->write);

cDev->logCbData.player = this;
cDev->logCbData.chipDevID = curDev;
if (cDev->base.defInf.devDef->SetLogCB != NULL)
cDev->base.defInf.devDef->SetLogCB(cDev->base.defInf.dataPtr, DROPlayer::SndEmuLogCB, &cDev->logCbData);
SetupLinkedDevices(&cDev->base, NULL, NULL);

if (devOpts != NULL)
Expand Down
25 changes: 17 additions & 8 deletions player/droplayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,22 @@ struct DRO_PLAY_OPTIONS
};


typedef struct _dro_chip_device DRO_CHIPDEV;
struct _dro_chip_device
{
VGM_BASEDEV base;
size_t optID;
DEVFUNC_WRITE_A8D8 write;
};

class DROPlayer : public PlayerBase
{
private:
struct DEVLOG_CB_DATA
{
DROPlayer* player;
size_t chipDevID;
};
struct DRO_CHIPDEV
{
VGM_BASEDEV base;
size_t optID;
DEVFUNC_WRITE_A8D8 write;
DEVLOG_CB_DATA logCbData;
};

public:
DROPlayer();
~DROPlayer();
Expand Down Expand Up @@ -131,6 +137,8 @@ class DROPlayer : public PlayerBase

void ScanInitBlock(void);

static void SndEmuLogCB(void* userParam, void* source, UINT8 level, const char* message);

void GenerateDeviceConfig(void);
UINT8 SeekToTick(UINT32 tick);
UINT8 SeekToFilePos(UINT32 pos);
Expand Down Expand Up @@ -167,6 +175,7 @@ class DROPlayer : public PlayerBase
DRO_PLAY_OPTIONS _playOpts;
PLR_DEV_OPTS _devOpts[3]; // 0 = 1st OPL2, 1 = 2nd OPL2, 2 = 1st OPL3
std::vector<DRO_CHIPDEV> _devices;
std::vector<std::string> _devNames;
size_t _optDevMap[3]; // maps _devOpts vector index to _devices vector

UINT32 _filePos;
Expand Down
18 changes: 18 additions & 0 deletions player/gymplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,22 @@ UINT32 GYMPlayer::GetLoopTicks(void) const
return _totalTicks - _fileHdr.loopFrame;
}

/*static*/ void GYMPlayer::SndEmuLogCB(void* userParam, void* source, UINT8 level, const char* message)
{
DEVLOG_CB_DATA* cbData = (DEVLOG_CB_DATA*)userParam;
GYMPlayer* player = cbData->player;
if (player->_logCbFunc == NULL)
return;
player->_logCbFunc(player->_logCbParam, player, level, PLRLOGSRC_EMU,
player->_devNames[cbData->chipDevID].c_str(), message);
return;
}


void GYMPlayer::GenerateDeviceConfig(void)
{
_devCfgs.clear();
_devNames.clear();
_devCfgs.resize(2);
{
DEV_GEN_CFG devCfg;
Expand All @@ -539,6 +551,7 @@ void GYMPlayer::GenerateDeviceConfig(void)
_devCfgs[0].type = DEVID_YM2612;
_devCfgs[0].volume = 0x100;
SaveDeviceConfig(_devCfgs[0].data, &devCfg, sizeof(DEV_GEN_CFG));
_devNames.push_back("FM");
}
{
SN76496_CFG snCfg;
Expand All @@ -554,6 +567,7 @@ void GYMPlayer::GenerateDeviceConfig(void)
_devCfgs[1].type = DEVID_SN76496;
_devCfgs[1].volume = 0x80;
SaveDeviceConfig(_devCfgs[1].data, &snCfg, sizeof(SN76496_CFG));
_devNames.push_back("PSG");
}

return;
Expand Down Expand Up @@ -597,6 +611,10 @@ UINT8 GYMPlayer::Start(void)
}
SndEmu_GetDeviceFunc(cDev->base.defInf.devDef, RWF_REGISTER | RWF_WRITE, DEVRW_A8D8, 0, (void**)&cDev->write);

cDev->logCbData.player = this;
cDev->logCbData.chipDevID = curDev;
if (cDev->base.defInf.devDef->SetLogCB != NULL)
cDev->base.defInf.devDef->SetLogCB(cDev->base.defInf.dataPtr, GYMPlayer::SndEmuLogCB, &cDev->logCbData);
SetupLinkedDevices(&cDev->base, NULL, NULL);

if (devOpts != NULL)
Expand Down
24 changes: 16 additions & 8 deletions player/gymplayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ struct GYM_HEADER
UINT32 realFileSize; // internal file size after possible decompression
};

typedef struct _gym_chip_device GYM_CHIPDEV;
struct _gym_chip_device
{
VGM_BASEDEV base;
size_t optID;
DEVFUNC_WRITE_A8D8 write;
};

class GYMPlayer : public PlayerBase
{
private:
Expand All @@ -53,6 +45,19 @@ class GYMPlayer : public PlayerBase
UINT16 volume;
std::vector<UINT8> data;
};
struct DEVLOG_CB_DATA
{
GYMPlayer* player;
size_t chipDevID;
};
struct GYM_CHIPDEV
{
VGM_BASEDEV base;
size_t optID;
DEVFUNC_WRITE_A8D8 write;
DEVLOG_CB_DATA logCbData;
};

public:
GYMPlayer();
~GYMPlayer();
Expand Down Expand Up @@ -106,6 +111,8 @@ class GYMPlayer : public PlayerBase
void LoadTag(const char* tagName, const void* data, size_t maxlen);
std::string GetUTF8String(const char* startPtr, const char* endPtr);

static void SndEmuLogCB(void* userParam, void* source, UINT8 level, const char* message);

void GenerateDeviceConfig(void);
UINT8 SeekToTick(UINT32 tick);
UINT8 SeekToFilePos(UINT32 pos);
Expand Down Expand Up @@ -142,6 +149,7 @@ class GYMPlayer : public PlayerBase

PLR_DEV_OPTS _devOpts[2]; // 0 = YM2612, 1 = SEGA PSG
std::vector<GYM_CHIPDEV> _devices;
std::vector<std::string> _devNames;
size_t _optDevMap[2]; // maps _devOpts vector index to _devices vector

UINT32 _filePos;
Expand Down
7 changes: 7 additions & 0 deletions player/playera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ void PlayerA::SetFileReqCallback(PLAYER_FILEREQ_CB cbFunc, void* cbParam)
return;
}

void PlayerA::SetLogCallback(PLAYER_LOG_CB cbFunc, void* cbParam)
{
for (size_t curPlr = 0; curPlr < _avbPlrs.size(); curPlr ++)
_avbPlrs[curPlr]->SetLogCallback(cbFunc, cbParam);
return;
}

UINT8 PlayerA::GetState(void) const
{
if (_player == NULL)
Expand Down
1 change: 1 addition & 0 deletions player/playera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PlayerA

void SetEventCallback(PLAYER_EVENT_CB cbFunc, void* cbParam);
void SetFileReqCallback(PLAYER_FILEREQ_CB cbFunc, void* cbParam);
void SetLogCallback(PLAYER_LOG_CB cbFunc, void* cbParam);
UINT8 GetState(void) const;
UINT32 GetCurPos(UINT8 unit) const;
double GetCurTime(UINT8 includeLoops) const;
Expand Down
12 changes: 11 additions & 1 deletion player/playerbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ PlayerBase::PlayerBase() :
_eventCbFunc(NULL),
_eventCbParam(NULL),
_fileReqCbFunc(NULL),
_fileReqCbParam(NULL)
_fileReqCbParam(NULL),
_logCbFunc(NULL),
_logCbParam(NULL)
{
}

Expand Down Expand Up @@ -83,6 +85,14 @@ void PlayerBase::SetFileReqCallback(PLAYER_FILEREQ_CB cbFunc, void* cbParam)
return;
}

void PlayerBase::SetLogCallback(PLAYER_LOG_CB cbFunc, void* cbParam)
{
_logCbFunc = cbFunc;
_logCbParam = cbParam;

return;
}

double PlayerBase::Sample2Second(UINT32 samples) const
{
if (samples == (UINT32)-1)
Expand Down
16 changes: 16 additions & 0 deletions player/playerbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ typedef UINT8 (*PLAYER_EVENT_CB)(PlayerBase* player, void* userParam, UINT8 evtT

typedef DATA_LOADER* (*PLAYER_FILEREQ_CB)(void* userParam, PlayerBase* player, const char* fileName);

typedef void (*PLAYER_LOG_CB)(void* userParam, PlayerBase* player, UINT8 level, UINT8 srcType, const char* srcTag, const char* message);
// log levels
#define PLRLOG_OFF DEVLOG_OFF
#define PLRLOG_ERROR DEVLOG_ERROR
#define PLRLOG_WARN DEVLOG_WARN
#define PLRLOG_INFO DEVLOG_INFO
#define PLRLOG_DEBUG DEVLOG_DEBUG
#define PLRLOG_TRACE DEVLOG_TRACE
// log source types
#define PLRLOGSRC_PLR 0x00 // player
#define PLRLOGSRC_EMU 0x01 // sound emulation


struct PLR_SONG_INFO
{
UINT32 format; // four-character-code for file format
Expand Down Expand Up @@ -115,6 +128,7 @@ class PlayerBase
virtual UINT8 SetPlaybackSpeed(double speed);
virtual void SetEventCallback(PLAYER_EVENT_CB cbFunc, void* cbParam);
virtual void SetFileReqCallback(PLAYER_FILEREQ_CB cbFunc, void* cbParam);
virtual void SetLogCallback(PLAYER_LOG_CB cbFunc, void* cbParam);
virtual UINT32 Tick2Sample(UINT32 ticks) const = 0;
virtual UINT32 Sample2Tick(UINT32 samples) const = 0;
virtual double Tick2Second(UINT32 ticks) const = 0;
Expand All @@ -139,6 +153,8 @@ class PlayerBase
void* _eventCbParam;
PLAYER_FILEREQ_CB _fileReqCbFunc;
void* _fileReqCbParam;
PLAYER_LOG_CB _logCbFunc;
void* _logCbParam;
};

#endif // __PLAYERBASE_HPP__
Loading

0 comments on commit f9eb6a7

Please sign in to comment.