Skip to content

Instantly share code, notes, and snippets.

@sroccaserra
Created August 31, 2020 07:18
Show Gist options
  • Save sroccaserra/b9752f00a3d8f4ff14165b6f1f93385c to your computer and use it in GitHub Desktop.
Save sroccaserra/b9752f00a3d8f4ff14165b6f1f93385c to your computer and use it in GitHub Desktop.
Basic resampling / audio pitch shifting
///
// 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