• ### What is your GameDev Story?

#### Archived

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

# Stretching wave files

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

## 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 on other sites
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 wavefor(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 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 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 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 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 on other sites
I''m actually interested in stretching wave files without changing its pitch.

##### 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 on other sites
Try MusicDSP and dspGuru
Look for "Time streching" or "Pitch scaling".

Good luck !

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 10
• 9
• 35
• 16
• ### Forum Statistics

• Total Topics
634125
• Total Posts
3015674
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!