Skip to content

Commit

Permalink
Properly patching starttick in BGM_Pitch.
Browse files Browse the repository at this point in the history
Properly patching starttick in BGM_Pitch.
  • Loading branch information
Rinnegatamante committed Apr 13, 2016
1 parent b5ed9c9 commit 27a537f
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/audio_3ds.cpp
Expand Up @@ -326,21 +326,31 @@ void CtrAudio::BGM_Pitch(int pitch) {
if (BGM == NULL) return;
svcLockMutex(BGM_Mutex);

// Getting music ptr based on old samplerate
u32 curPos;
if (BGM->handle != NULL) curPos = BGM->samplerate * BGM->bytepersample * ((osGetTime() - BGM->starttick) / 1000);
// Pausing playback to not broke audio streaming
if (BGM->handle != NULL) BGM_Pause();

// Calculating and setting new samplerate
BGM->samplerate = (BGM->orig_samplerate * pitch) / 100;
// Calculating new samplerate
u32 new_samplerate = (BGM->orig_samplerate * pitch) / 100;

// Patching starting tick to not cause issues with audio streaming
if (BGM->handle != NULL){
u64 oldDelta = BGM->starttick;
u64 newDelta = (BGM->samplerate * oldDelta) / new_samplerate;
BGM->starttick = newDelta;
}

// Setting new samplerate
BGM->samplerate = new_samplerate;
if (isDSP) ndspChnSetRate(SOUND_CHANNELS, float(BGM->samplerate));
else{
if (BGM->isStereo) CSND_SetTimer(0x1E, CSND_TIMER(BGM->samplerate));
CSND_SetTimer(0x1F, CSND_TIMER(BGM->samplerate));
CSND_UpdateInfo(0);
}

// Patching starting tick to not cause issues with audio streaming
if (BGM->handle != NULL) BGM->starttick = osGetTime() - ((1000 * curPos) / (BGM->samplerate * BGM->bytepersample));
// Resuming playback
if (BGM->handle != NULL) BGM_Resume();

svcReleaseMutex(BGM_Mutex);

}
Expand Down

0 comments on commit 27a537f

Please sign in to comment.