Skip to content

Instantly share code, notes, and snippets.

@marler8997
Created November 25, 2023 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marler8997/98b2793c206f98f7ebdd32e0d1394d56 to your computer and use it in GitHub Desktop.
Save marler8997/98b2793c206f98f7ebdd32e0d1394d56 to your computer and use it in GitHub Desktop.
WASM4 Change Pitch
fn changePitchOne(freq: u16, semitones: f32) u16 {
if (freq == 0) return 0;
const freq_f32 = @as(f32, @floatFromInt(freq));
return @intFromFloat(@round(freq_f32 * std.math.pow(
f32,
@exp2(1.0 / 12.0),
semitones,
)));
}
fn changePitch(tone_arg: u32, semitones: f32) u32 {
const upper: u32 = changePitchOne(@intCast(tone_arg >> 16), semitones);
const lower: u32 = changePitchOne(@intCast(tone_arg & 0xffff), semitones);
return upper << 16 | lower;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment