Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MOVSW

Stretching wave files

This topic is 5684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wonder if anyone here tried stretching wave files. In theory it would be possible. Suppose I have a sound sample containing this data 23281819071 that takes a second to play. Now, it would be possible to take it and stretch to 2233228811881199007711. Now it has twice many samples. So, every sound in the sample is now playing 2 times longer. Has anyone tried programming something like this or know a program that is capable of doing this kind of thing?

Share this post


Link to post
Share on other sites
Advertisement
It was a long time since I did this. Do you want the sound to be twice as long but keep it''s pitch? Or should the pitch decrease? The latter is what happens if you just stretch the wave. I do not know which technique allow you to keep the pitch.

I''m in a hurry, so this may make no sense

I plotted down an algorithm which extrapolates a wave. It''s C++ but should be read as pseudo-code. This should be able to correctly extrapolate any wave which is equal or longer than the original wave. If you know the length to always be a power of two times the original length, it can be done a lot cleaner and faster.



Original: +--+--+--+--+
(stretched): + - - + - - + - - + - - +
Re-sampled: +-----+-----+-----+-----+

// smp_old[], array with the original wave''s samples
// smp_new[], array for the new wave''s samples
// length_old, number of samples in the old wave
// length_new, number of samples in the new wave

// Loop through and set each sample in the new wave
for(long i = 0; i < length_new; i++) {
// The continous position in the old sample
float i_old = (i / (length_new - 1)) * (length_old - 1);
// (Bad?) way to get a 0.0 - 0.999... value of it
float a = i_old - floor(i_old);
// The integer index of the index left and right to the real position
long sli = (long)(i_old);
long sri = (long)(i_old + 1);
// Calculate sample''s value
smp_new = smp_old[sli] * (1 - a) + smp_old[sri] * a;
}



Share this post


Link to post
Share on other sites
Well, um, sure, if you really want to I suppose, but unless you add in some interesting algorithms you''re not going to improve anything by it. Unless you''re very sophisticated about it, adding samples and evening it out makes things "mushy".

If all you''re trying to do is decrease the frequency so it sounds lower and slower, all you have to do is ask DirectSound to make a frequency shift for you.

-fel

Share this post


Link to post
Share on other sites
quote:
Original post by felisandria
If all you''re trying to do is decrease the frequency so it sounds lower and slower, all you have to do is ask DirectSound to make a frequency shift for you.


... and then it sounds like low batt.

Share this post


Link to post
Share on other sites
I''m curious as to how one would go about stretching a sound file without changing the frequency. Would a discrete fourier transform help here? Are there other ways?

Share this post


Link to post
Share on other sites
The SOX utilities are great for resampling/rescaling audio files (and it supports just about every audio format known to man)...

http://www.spies.com/Sox/

botman

Share this post


Link to post
Share on other sites
For resampling and maintaining the same sound, all you have to do is interpolate between the known values to get the in-between values. A linear interpolation is the easiest, and gives pretty good results for the human ear (you probably won''t notice a major change in the sound).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!