Skip to content

Commit 38f8e18

Browse files
committed
Merge pull request #109006 from RoyBerardo/random_pitch_bias_fix
Fix random pitch upward bias in `AudioStreamRandomizer`
2 parents 0d6c1c3 + 3e01c8a commit 38f8e18

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

servers/audio/audio_stream.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,13 @@ AudioStreamRandomizer::AudioStreamRandomizer() {
799799
void AudioStreamPlaybackRandomizer::start(double p_from_pos) {
800800
playing = playback;
801801
{
802-
float range_from = 1.0 / randomizer->random_pitch_scale;
803-
float range_to = randomizer->random_pitch_scale;
802+
// GH-10238 : Pitch_scale is multiplicative, so picking a random number for it without log
803+
// conversion will bias it towards higher pitches (0.5 is down one octave, 2.0 is up one octave).
804+
// See: https://pressbooks.pub/sound/chapter/pitch-and-frequency-in-music/
805+
float range_from = Math::log(1.0f / randomizer->random_pitch_scale);
806+
float range_to = Math::log(randomizer->random_pitch_scale);
804807

805-
pitch_scale = range_from + Math::randf() * (range_to - range_from);
808+
pitch_scale = Math::exp(range_from + Math::randf() * (range_to - range_from));
806809
}
807810
{
808811
float range_from = -randomizer->random_volume_offset_db;

0 commit comments

Comments
 (0)