Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/ace/managers/ptplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,13 +1059,20 @@ static void printVoices(const tModVoice *pVoices) {
}

static inline UBYTE findPeriod(const UWORD *pPeriods, UWORD uwNote) {
// Find nearest period for a note value
for(UBYTE ubPeriodPos = 0; ubPeriodPos < MOD_PERIOD_TABLE_LENGTH; ++ubPeriodPos) {
if (uwNote >= pPeriods[ubPeriodPos]) {
return ubPeriodPos;
// Find nearest period (equal or lower) for a note value using a binary search lookup
UBYTE ubPeriodPos = 0;
UBYTE maxBound = MOD_PERIOD_TABLE_LENGTH;
while (ubPeriodPos != maxBound) {
UBYTE currentIndex = (ubPeriodPos + maxBound) >> 1;
// if this period is greater, skip 'bottom half' of the array and continue searching
if (pPeriods[currentIndex] > uwNote) {
ubPeriodPos = currentIndex + 1;
}
else { // else, the period we seek is between ubPeriodPos and currentIndex
maxBound = currentIndex;
}
}
return 0;
return ubPeriodPos;
}

static void ptSongStep(void) {
Expand Down