6

I want to change play speed (increase or decrease) of a certain WAV audio file using python wave module.

I tried below thing :

  1. Read frame rate of input file.
  2. Double the frame rate.
  3. Write a new wave file with increased frame rate using output_wave.setparams() function.

But its not working out.

Please suggest.

Thanks in Advance,

Dev.K.
  • 2,428
  • 5
  • 35
  • 49
  • It would help if you could post a) the code you're using, and b) a more detailed description of the problem - ie, *how* is it 'not working out'? – lvc Mar 31 '14 at 07:21
  • I know It's late please see my answer. [I wrote the whole program for different playback speed](https://stackoverflow.com/a/62715422/2866150) – Lokesh Deshmukh Jul 03 '20 at 12:32

2 Answers2

14

WOW!

if you no matter to change the pitch when you increase or decrease the speed, you can just change the sample rate !

Can be very simple using python:

import wave

CHANNELS = 1
swidth = 2
Change_RATE = 2

spf = wave.open('VOZ.wav', 'rb')
RATE=spf.getframerate()
signal = spf.readframes(-1)

wf = wave.open('changed.wav', 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(swidth)
wf.setframerate(RATE*Change_RATE)
wf.writeframes(signal)
wf.close()

increase or decrease the variable Change_RATE !

Now if you need keep the pitch untouched, you need do same type of overlap-add method !

ederwander
  • 3,410
  • 1
  • 18
  • 23
  • Another question, if I craft a wave file which has total 20000 frames and the frame rate is 500, then the period of the wav sound should be 40 seconds, But actually its becoming 1 min 19 seconds.. NUMBER_OF_FRAME = 20000 FRAME_RATE = 500 op = wave.open('random.wav', 'w') op.setparams((1, 2, FRAME_RATE, NUMBER_OF_FRAME, 'NONE', 'not compressed')) for i in range(NUMBER_OF_FRAME): byte1 = struct.pack('h',randint(0,255)) byte2 = struct.pack('h',randint(0,255)) op.writeframes(byte1+byte2) op.close() Why its happening ? – Dev.K. Apr 01 '14 at 06:53
1

If you change the sampling frequency it has no influence on audiable playback speed. You can play around with this using SoX Sound eXchange, the Swiss Army knife of audio manipulation

There is pySonic library for python look at UserSpeed method of the Song object. pySonic Python wrapper of FMOD Sound library

mcer
  • 11
  • 2