Skip to content

Commit

Permalink
ptplayer: fix duplicate pointer for current mod, cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
tehKaiN committed Feb 17, 2024
1 parent 9b0d7c4 commit 3f07d75
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions src/ace/managers/ptplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
#define MOD_NOTES_PER_ROW 4
#define MOD_BYTES_PER_NOTE 4
// Length of single pattern.
#define MOD_PATTERN_LENGTH (MOD_ROWS_IN_PATTERN * MOD_NOTES_PER_ROW * MOD_BYTES_PER_NOTE)
#define MOD_PATTERN_BYTE_SIZE (MOD_ROWS_IN_PATTERN * MOD_NOTES_PER_ROW * MOD_BYTES_PER_NOTE)

// Size of period table.
#define MOD_PERIOD_TABLE_LENGTH 36
Expand Down Expand Up @@ -974,7 +974,7 @@ static volatile UBYTE s_isNextTimerBSetRep;

static tChannelStatus mt_chan[4];
static UWORD *mt_SampleStarts[31]; ///< Start address of each sample
static tPtplayerMod *mt_mod; ///< Currently played MOD.
static tPtplayerMod *s_pCurrentMod; ///< Currently played MOD.
static ULONG mt_timerval; ///< Base interrupt frequency of CIA-B timer A used to advance the song. Equals 125*50Hz.
static const UBYTE * mt_MasterVolTab;
static UWORD mt_PatternPos;
Expand All @@ -999,8 +999,6 @@ static UBYTE mt_SilCntValid;
*/
static UWORD mt_dmaon = 0;

static tPtplayerMod *s_pModCurr;

static void clearAudioDone(void) {
#if defined(PTPLAYER_USE_AUDIO_INT_HANDLERS)
// When channel is idle, original ptplayer loops playback of the first word
Expand Down Expand Up @@ -1132,7 +1130,7 @@ static void ptSongStep(void) {
UBYTE ubNextPos = (mt_SongPos + 1) & 0x7F;

// End of song reached?
if(ubNextPos >= mt_mod->ubArrangementLength) {
if(ubNextPos >= s_pCurrentMod->ubArrangementLength) {
ubNextPos = 0;
if(!s_isRepeat) {
ptplayerEnableMusic(0);
Expand Down Expand Up @@ -1275,7 +1273,7 @@ static void mt_playvoice(
--uwSampleIdx;
// Read length, volume and repeat from sample info table
UWORD *pSampleStart = mt_SampleStarts[uwSampleIdx];
tPtplayerSampleHeader *pSampleDef = &mt_mod->pSampleHeaders[uwSampleIdx];
tPtplayerSampleHeader *pSampleDef = &s_pCurrentMod->pSampleHeaders[uwSampleIdx];
UWORD uwSampleLength = pSampleDef->uwLength;
if(!uwSampleLength) {
// Use the first two bytes from the first sample for empty samples
Expand Down Expand Up @@ -1571,8 +1569,8 @@ static void mt_music(void) {
mt_Counter = 0;
if(mt_PattDelTime2 <= 0) {
// determine pointer to current pattern line
UBYTE *pPatternData = mt_mod->pPatterns;
UBYTE *pArrangement = mt_mod->pArrangement;
UBYTE *pPatternData = s_pCurrentMod->pPatterns;
UBYTE *pArrangement = s_pCurrentMod->pArrangement;
UBYTE ubPatternIdx = pArrangement[mt_SongPos];
UBYTE *pCurrentPattern = &pPatternData[ubPatternIdx * 1024];
tModVoice *pLineVoices = (tModVoice*)&pCurrentPattern[mt_PatternPos];
Expand Down Expand Up @@ -1660,7 +1658,7 @@ void ptplayerStop(void) {
}
resetChannel(&mt_chan[i]);
}
s_pModCurr = 0;
s_pCurrentMod = 0;

// Free the channels taken by SFX.
// Typically they would release themselves but turning off DMA prevents this.
Expand Down Expand Up @@ -1694,6 +1692,7 @@ static void mt_reset(void) {
mt_Speed = 6;
mt_Counter = 0;
mt_PatternPos = 0;
mt_SongPos = 0;
mt_PattDelTime = 0;
mt_PattDelTime2 = 0;
mt_PBreakPos = 0;
Expand Down Expand Up @@ -1762,7 +1761,7 @@ void ptplayerCreate(UBYTE isPal) {
s_isRepeat = 1;
s_cbSongEnd = 0;
s_cbOnE8 = 0;
s_pModCurr = 0;
s_pCurrentMod = 0;
ptplayerEnableMusic(0);
#if defined(PTPLAYER_USE_AUDIO_INT_HANDLERS)
for(UBYTE i = sizeof(s_pAudioChannelPendingDisable); i--;) {
Expand Down Expand Up @@ -1814,24 +1813,23 @@ void ptplayerLoadMod(
tPtplayerMod *pMod, tPtplayerSamplePack *pSamples, UWORD uwInitialSongPos
) {
logBlockBegin(
"ptplayerInit(pMod: %p, pSamples: %p, uwInitialSongPos: %hu)",
"ptplayerLoadMod(pMod: %p, pSamples: %p, uwInitialSongPos: %hu)",
pMod, pSamples, uwInitialSongPos
);

// Initialize new module.
// Reset speed to 6, tempo to 125 and start at given song position.
// Master volume is at 64 (maximum).
mt_mod = pMod;
s_pCurrentMod = pMod;
logWrite(
"Song name: '%s', arrangement length: %hhu, end pos: %hhu\n",
mt_mod->szSongName, mt_mod->ubArrangementLength, mt_mod->ubSongEndPos
s_pCurrentMod->szSongName, s_pCurrentMod->ubArrangementLength, s_pCurrentMod->ubSongEndPos
);

// set initial song position
if(uwInitialSongPos >= 950) {
uwInitialSongPos = 0;
}
mt_SongPos = uwInitialSongPos;

// sample data location is given?
UWORD *pSampleData = pSamples ? pSamples->pData : pMod->pSamples;
Expand All @@ -1841,24 +1839,27 @@ void ptplayerLoadMod(
ULONG ulOffs = 0;
for(UBYTE i = 0; i < 31; ++i) {
mt_SampleStarts[i] = &pSampleData[ulOffs];
if(mt_mod->pSampleHeaders[i].uwLength > 0) {
if(s_pCurrentMod->pSampleHeaders[i].uwLength > 0) {
logWrite(
"Sample %hhu name: '%.*s', word length: %hu, start: %p, repeat offs: %hu, repeat len:%hu\n",
i, 22, mt_mod->pSampleHeaders[i].szName, mt_mod->pSampleHeaders[i].uwLength,
mt_SampleStarts[i], mt_mod->pSampleHeaders[i].uwRepeatOffs, mt_mod->pSampleHeaders[i].uwRepeatLength
i, 22, s_pCurrentMod->pSampleHeaders[i].szName, s_pCurrentMod->pSampleHeaders[i].uwLength,
mt_SampleStarts[i], s_pCurrentMod->pSampleHeaders[i].uwRepeatOffs, s_pCurrentMod->pSampleHeaders[i].uwRepeatLength
);

// Make sure each sample starts with two 0-bytes
mt_SampleStarts[i][0] = 0;
}

// Go to next sample
ulOffs += mt_mod->pSampleHeaders[i].uwLength;
ulOffs += s_pCurrentMod->pSampleHeaders[i].uwLength;
};

mt_reset();
s_pModCurr = pMod;
logBlockEnd("ptplayerInit()");
// Fix what mt_reset has broken
s_pCurrentMod = pMod;
mt_SongPos = uwInitialSongPos;

logBlockEnd("ptplayerLoadMod()");
}

void ptplayerSetMusicChannelMask(UBYTE ChannelMask) {
Expand Down Expand Up @@ -2733,8 +2734,8 @@ void ptplayerProcess(void) {
}

const tModVoice *ptplayerGetCurrentVoices(void) {
UBYTE *pPatternData = mt_mod->pPatterns;
UBYTE *pArrangement = mt_mod->pArrangement;
UBYTE *pPatternData = s_pCurrentMod->pPatterns;
UBYTE *pArrangement = s_pCurrentMod->pArrangement;
UBYTE ubPatternIdx = pArrangement[mt_SongPos];
UBYTE *pCurrentPattern = &pPatternData[ubPatternIdx * 1024];
tModVoice *pLineVoices = (tModVoice*)&pCurrentPattern[mt_PatternPos];
Expand All @@ -2760,7 +2761,7 @@ void ptplayerReserveChannelsForMusic(UBYTE ubChannelCount) {
}

void ptplayerSetSampleVolume(UBYTE ubSampleIndex, UBYTE ubVolume) {
mt_mod->pSampleHeaders[ubSampleIndex].ubVolume = ubVolume;
s_pCurrentMod->pSampleHeaders[ubSampleIndex].ubVolume = ubVolume;
}

tPtplayerMod *ptplayerModCreate(const char *szPath) {
Expand Down Expand Up @@ -2799,7 +2800,7 @@ tPtplayerMod *ptplayerModCreate(const char *szPath) {
logWrite("Pattern count: %hhu\n", ubPatternCount);

// Read pattern data
pMod->ulPatternsSize = (ubPatternCount * MOD_PATTERN_LENGTH);
pMod->ulPatternsSize = (ubPatternCount * MOD_PATTERN_BYTE_SIZE);
pMod->pPatterns = memAllocFast(pMod->ulPatternsSize);
if(!pMod->pPatterns) {
logWrite("ERR: Couldn't allocate memory for pattern data!");
Expand Down Expand Up @@ -2838,7 +2839,7 @@ tPtplayerMod *ptplayerModCreate(const char *szPath) {
}

void ptplayerModDestroy(tPtplayerMod *pMod) {
if(s_pModCurr == pMod) {
if(s_pCurrentMod == pMod) {
ptplayerStop();
}
memFree(pMod->pPatterns, pMod->ulPatternsSize);
Expand Down Expand Up @@ -3005,7 +3006,7 @@ void ptplayerSfxPlay(
}

// Did we already calculate the n_freecnt values for all channels?
if(!mt_SilCntValid && mt_mod) {
if(!mt_SilCntValid && s_pCurrentMod) {
// Look at the next 8 pattern steps to find the longest sequence
// of silence (no new note or instrument).
UBYTE ubSteps = 8;
Expand All @@ -3023,15 +3024,15 @@ void ptplayerSfxPlay(
mt_chan[3].n_freecnt = 0;

// get pattern pointer
UBYTE *pPatterns = mt_mod->pPatterns;
UBYTE *pPatterns = s_pCurrentMod->pPatterns;

UBYTE ubSongPos = mt_SongPos;
UWORD uwPatternPos = mt_PatternPos;
UBYTE isEnd = 0;
UBYTE *pPatternStart = &pPatterns[
mt_mod->pArrangement[ubSongPos] * MOD_PATTERN_LENGTH
s_pCurrentMod->pArrangement[ubSongPos] * MOD_PATTERN_BYTE_SIZE
];
tModVoice *pPatternEnd = (tModVoice*)(pPatternStart + MOD_PATTERN_LENGTH);
tModVoice *pPatternEnd = (tModVoice*)(pPatternStart + MOD_PATTERN_BYTE_SIZE);
tModVoice *pPatternPos = (tModVoice*)(pPatternStart + uwPatternPos);
do {
UBYTE ubFreeChannelCnt = 4;
Expand All @@ -3057,13 +3058,13 @@ void ptplayerSfxPlay(
if(!isEnd && pPatternPos >= pPatternEnd) {
uwPatternPos = 0;
ubSongPos = (mt_SongPos + 1) & 127;
if(ubSongPos >= mt_mod->ubArrangementLength) {
if(ubSongPos >= s_pCurrentMod->ubArrangementLength) {
ubSongPos = 0;
}
pPatternStart = &pPatterns[
mt_mod->pArrangement[ubSongPos] * MOD_PATTERN_LENGTH
s_pCurrentMod->pArrangement[ubSongPos] * MOD_PATTERN_BYTE_SIZE
];
pPatternEnd = (tModVoice*)(pPatternStart + MOD_PATTERN_LENGTH);
pPatternEnd = (tModVoice*)(pPatternStart + MOD_PATTERN_BYTE_SIZE);
pPatternPos = (tModVoice*)pPatternStart;
}
} while(!isEnd);
Expand Down

0 comments on commit 3f07d75

Please sign in to comment.