from ReadAudio import AudioReader import sys reader = AudioReader.open("./The_beach_boys__Kokomo9_5_AD9wXuYm4a_2.wav") print reader.duration() sys.exit(0); import soundtouch from array import array import wave # Open the file and convert it to have SoundTouch's required 2-byte samples reader = AudioReader.open(srcpath) reader2 = ConvertReader(reader, set_raw_width=2) # Create the SoundTouch object and set the given shift st = soundtouch.SoundTouch(reader2.sampling_rate(), reader2.channels()) st.set_pitch_shift(shift) #{{{ writer WAV # Create the .WAV file to write the result to writer = wave.open(dstpath, 'w') writer.setnchannels(reader2.channels()) writer.setframerate(reader2.sampling_rate()) writer.setsampwidth(reader2.raw_width()) #}}} # Read values and feed them into SoundTouch while True: data = reader2.raw_read() if not data: break print len(data) st.put_samples(data) while st.ready_count() > 0: writer.writeframes(st.get_samples(11025)) # Flush any remaining samples waiting = st.waiting_count() ready = st.ready_count() flushed = "" # Add silence until another chunk is pushed out silence = array('h', [0] * 64) while st.ready_count() == ready: st.put_samples(silence) # Get all of the additional samples while st.ready_count() > 0: flushed += st.get_samples(4000) st.clear() if len(flushed) > 2 * reader2.getnchannels() * waiting: flushed = flushed[0:(2 * reader2.getnchannels() * waiting)] writer.writeframes(flushed) # Clean up writer.close() reader2.close()