I am searching for a universal algorithm that shifts pitch and time while keeping the sample rate. (I am trying to program a sound generator (sine, triangle...) as an exercise)
I just want to squeeze the samples of the sound together so it appears to be shorter and pitched higher.
It's easy if you want to speed it up with a nice number like 2 (leave out every second sample) but what if you want to speed it up by 2.5?
Can someone name an algorithm for this? (preferably in C++, but other languages are also fine)
(I spent the last 30 minutes writing an algorithm that computes averages of decimals of a number (Like the average of every 2.5 numbers). Then I realized it's totally useless so please help)
Does this approach make sense?: (Will be adding pseudocode shortly)
N is the amount of the pitch
Divide the index of every sample of the sound by N (2/2.8=0.71; 3/2.5=1.07)
For every whole number, compute the distances to the next samples (0.29; 0.07)
Value portions of distance (1-(0.29/0.36)=19%; 1-(0.07/0.36)=81%
New number (New Sample = (19%*SampleA + 81%*SampleB)/2)
Code:
byte[] input;
double f = factor;
byte[] output = new byte[ceil(input / f)];
output[0] = input[0]
for(int i = 1; i < output.length) {
output[i] = (input[floor(i*factor)] + input[ceil(i*factor)]) / 2;
}