Created
August 31, 2020 07:18
-
-
Save sroccaserra/b9752f00a3d8f4ff14165b6f1f93385c to your computer and use it in GitHub Desktop.
Basic resampling / audio pitch shifting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
// from: https://discourse.libsdl.org/t/audio-pitch-question/4822/5 | |
// resample without interpolation | |
for (i=0;i<len;i++) | |
{ | |
pos=(int)wav_position; | |
assert(pos>=0); | |
assert(pos<=wav_length); | |
my_sample[i]=wav_buffer[pos]; | |
wav_position=wav_position+speed; | |
while (wav_position>=wav_length) | |
wav_position=wav_position-wav_length; | |
} | |
// resample with linear interpolation | |
float fract_pos; | |
float wav_position; | |
for (i=0;i<len;i++) | |
{ | |
pos=(int)wav_position; | |
fract_pos=wav_position - pos; | |
assert(pos>=0); | |
assert(pos<=wav_length); | |
my_sample[i]=wav_buffer[pos] + fract_pos*(wav_buffer[pos+1]-wav_buffer[pos]); | |
wav_position=wav_position+speed; | |
while (wav_position>=wav_length) | |
wav_position=wav_position-wav_length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment