Skip to content

Commit

Permalink
Use accurate semitone algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicfind committed Dec 22, 2023
1 parent 7c2219f commit 2e96d8c
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions Assets/Script/Audio/Bass/BassStemChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,10 @@ public int Load(float speed)
// Have to handle pitch separately for some reason
if (_manager.Options.IsChipmunkSpeedup)
{
float semitoneShift = speed switch
{
> 1f => speed / 9f - 1f / 9f,
< 1f => speed / 3f - 1f / 3f,
_ => 0f
};

semitoneShift = Math.Clamp(semitoneShift, -60f, 60f);

if (!Bass.ChannelSetAttribute(StreamHandle, ChannelAttribute.Pitch, semitoneShift) ||
!Bass.ChannelSetAttribute(ReverbStreamHandle, ChannelAttribute.Pitch, semitoneShift))
double accurateSemitoneShift = 12 * Math.Log(speed, 2);
float finalSemitoneShift = (float) Math.Clamp(accurateSemitoneShift, -60, 60);
if (!Bass.ChannelSetAttribute(StreamHandle, ChannelAttribute.Pitch, finalSemitoneShift) ||
!Bass.ChannelSetAttribute(ReverbStreamHandle, ChannelAttribute.Pitch, finalSemitoneShift))
Debug.LogError($"Failed to set channel pitch: {Bass.LastError}");
}
}
Expand Down

0 comments on commit 2e96d8c

Please sign in to comment.