I am using the wave library in python to try to reduce the speed of sound by 50%. I was successful, but only on the right channel. in the left channel this is a whole bunch of statics.
import wave,os,math r=wave.open(r"C:\Users\A\My Documents\LiClipse Workspace\Audio compression\Audio compression\aha.wav","r") w=wave.open(r"C:\Users\A\My Documents\LiClipse Workspace\Audio compression\Audio compression\ahaout.wav","w") frames=r.readframes(r.getnframes()) newframes=bytearray() w.setparams(r.getparams()) for i in range(0,len(frames)-1): newframes.append(frames[i]) newframes.append(frames[i]) w.writeframesraw(newframes)
Why is this? since I just copy and paste the raw data, I can not create statics? edit: I searched for ages, and finally found a useful resource for the wave format: http://soundfile.sapp.org/doc/WaveFormat/ If I want to keep stereo sound, it looks like I need to copy the actual sample width 4 times. This is because there are two channels, and they occupy 4 bytes instead of 2.
`import wave r=wave.open(r"C:\Users\A\My Documents\LiClipse Workspace\Audio compression\Audio compression\aha.wav","r") w=wave.open(r"C:\Users\A\My Documents\LiClipse Workspace\Audio compression\Audio compression\ahaout.wav","w") frames=r.readframes(r.getnframes()) newframes=bytearray() w.setparams(r.getparams()) w.setframerate(r.getframerate()) print(r.getsampwidth()) for i in range(0,len(frames)-4,4): newframes.append(frames[i]) newframes.append(frames[i+1]) newframes.append(frames[i+2]) newframes.append(frames[i+3]) newframes.append(frames[i]) newframes.append(frames[i+1]) newframes.append(frames[i+2]) newframes.append(frames[i+3]) w.writeframesraw(newframes)`
Edit 2: Well, I have no idea what prompted me to do this, but I already enjoy the freedoms it gives me. I decided to copy the wav file to memory, edit the copy directly and write it to the output file. I am incredibly pleased with the results. I can import wav, repeat the sound once and write it to the output file in just 0.2 seconds. Reducing the speed by one and a half times now takes only 9 seconds instead of 30 + seconds with my old code using the wav plugin :) here the code is still not optimized, I think, but it is better than what it was.
import struct import time as t t.clock() r=open(r"C:/Users/apier/Documents/LiClipse Workspace/audio editing software/main/aha.wav","rb") w=open(r"C:/Users/apier/Documents/LiClipse Workspace/audio editing software/main/output.wav","wb") rbuff=bytearray(r.read()) def replacebytes(array,bites,stop): length=len(bites) start=stop-length for i in range(start,stop): array[i]=bites[i-start] def write(audio): w.write(audio) def repeat(audio,repeats): if(repeats==1): return(audio) if(repeats==0): return(audio[:44]) replacebytes(audio, struct.pack('<I', struct.unpack('<I',audio[40:44]) [0]*repeats), 44) return(audio+(audio[44:len(audio)-58]*(repeats-1))) def slowhalf(audio): buff=bytearray() replacebytes(audio, struct.pack('<I', struct.unpack('<I',audio[40:44]) [0]*2), 44) for i in range(44,len(audio)-62,4): buff.append(audio[i]) buff.append(audio[i+1]) buff.append(audio[i+2]) buff.append(audio[i+3]) buff.append(audio[i]) buff.append(audio[i+1]) buff.append(audio[i+2]) buff.append(audio[i+3]) return(audio[:44]+buff) rbuff=slowhalf(rbuff) write(rbuff) print(t.clock())
I am surprised how small the code is.