summaryrefslogtreecommitdiff
path: root/example2.py
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2016-03-16 21:27:00 -0700
committeryo mama <pepper@scannerjammer.com>2016-03-16 21:27:00 -0700
commitc6d5e8f0e368c96bfeecb7501344d82820601588 (patch)
treea1339289aa5bb6ddeb0e6cf02bc8925097df199c /example2.py
parent8adfb3bd99b4dcff2459756af090a640fd7a4b4a (diff)
Diffstat (limited to 'example2.py')
-rw-r--r--example2.py61
1 files changed, 61 insertions, 0 deletions
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()