From c6d5e8f0e368c96bfeecb7501344d82820601588 Mon Sep 17 00:00:00 2001 From: yo mama Date: Wed, 16 Mar 2016 21:27:00 -0700 Subject: ok --- example2.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 example2.py (limited to 'example2.py') diff --git a/example2.py b/example2.py new file mode 100644 index 0000000..27835f8 --- /dev/null +++ b/example2.py @@ -0,0 +1,61 @@ +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() -- cgit v1.2.3-70-g09d2