Increase "pitch" of sound based on percentage?

Hi. I’m trying to come up with a way of increasing a sound’s “pitch” (or speed, since its tempo and pitch increase) based on a percent,
which should match the percent of it’s duration as well.

Keep in mind, the duration of each clip is a whole number (no decimal), and they are in .ogg format, if that matters somehow.

For instance, let’s say I want a clip to be 60% faster in duration (exactly 4s). My current method is to take 100 (normal pitch), and add it to 100 * 0.6, thus being 60% faster.
Its duration should then last for 1.4 seconds. But it seems to be ending at 2.5 seconds!

This works for lower percentages, but will gradually start becoming more and more incorrect with accordance to what it’s final duration should be. Any help would be appreciated.


By multiplying it by 0.6, you’re making it play at 60% of it’s current speed. You’re making it slower. What you want is 160% speed, or 1.6.

I am adding the 100% (normal pitch) to the 60%, so its total speed is 160% (hence being 60% faster).

The issue lies in it not matching up with the duration.

The wiki doesn’t seem to specify that the number received for pitch is actually treated like a percentage. How would 0% work? It probably is clamped between two other percentages that’s scaled from (input/255) between the lower limit and the upper limit. What those are though, I have no idea.

I’d suggest timing the duration if you give it 0, and timing the duration if you give it 255. Then you can compare them to your original duration to get the limits it actually uses and formulate your math from that instead.

I found out that the speed of audio (or “pitch” in source) is calculated by dividing the duration by the multiple (eg 1.25).

Thus I was able to align the duration with the speed. However…
This method isn’t actually accurate to duration (4/1.5 is 2.66… but half would really be 2 sec.)

This is really nitpicking for my gamemode, but it just doesn’t feel right. To the average user, this wouldn’t matter really.