diff options
Diffstat (limited to 'example.py')
| -rwxr-xr-x | example.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/example.py b/example.py new file mode 100755 index 0000000..38486c0 --- /dev/null +++ b/example.py @@ -0,0 +1,52 @@ +#!/usr/bin/python2.7 +import wave +import soundtouch +from array import array +# Open a .WAV file +FILEPATH = "./The_beach_boys__Kokomo9_5_AD9wXuYm4a_2.wav" +wf = wave.open(FILEPATH) + +# Create the sountouch pointer +st = soundtouch.SoundTouch(wf.getframerate(), wf.getnchannels()) + + +# Specify the shift, as 1 whole step +st.set_pitch_shift(2) + + + +# Feed in samples and add processed samples to resstr +resstr = "" +while True: + buf = wf.readframes(4000) + if not buf: + break + + st.put_samples(buf) + while st.ready_count() > 0: + resstr += st.get_samples(4000) + +# Flush any additional 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 * wf.getnchannels() * waiting: + flushed = flushed[0:(2 * wf.getnchannels() * waiting)] + +resstr += flushed + +# Clean up +wf.close() +del st |
