From 3e01c8a551318c8b998156bf2fe548d6376b5bb2 Mon Sep 17 00:00:00 2001 From: Roy Berardo Date: Sat, 26 Jul 2025 11:34:17 -0700 Subject: [PATCH] Fix random pitch upwards bias in AudioStreamRandomizer --- servers/audio/audio_stream.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 440acb72e57..18021482e58 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -799,10 +799,13 @@ AudioStreamRandomizer::AudioStreamRandomizer() { void AudioStreamPlaybackRandomizer::start(double p_from_pos) { playing = playback; { - float range_from = 1.0 / randomizer->random_pitch_scale; - float range_to = randomizer->random_pitch_scale; + // GH-10238 : Pitch_scale is multiplicative, so picking a random number for it without log + // conversion will bias it towards higher pitches (0.5 is down one octave, 2.0 is up one octave). + // See: https://pressbooks.pub/sound/chapter/pitch-and-frequency-in-music/ + float range_from = Math::log(1.0f / randomizer->random_pitch_scale); + float range_to = Math::log(randomizer->random_pitch_scale); - pitch_scale = range_from + Math::randf() * (range_to - range_from); + pitch_scale = Math::exp(range_from + Math::randf() * (range_to - range_from)); } { float range_from = -randomizer->random_volume_offset_db;