summaryrefslogtreecommitdiff
path: root/example.py
blob: 38486c0f25286dce5a7262ea7549559dbf1aca55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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