-
Notifications
You must be signed in to change notification settings - Fork 1
/
pvc_jack.py
176 lines (128 loc) · 5.08 KB
/
pvc_jack.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import tkinter
import numpy as np
import jack
import pitchshift
class CircularBuffer:
def __init__(self, n):
self.arr = np.zeros(n)
self.widx = 0
self.ridx = 0
def get_indices(self, n, offset, idx):
start = (idx + offset) % self.arr.size
end = start + n
if end > self.arr.size:
l_start = start
l_end = self.arr.size
r_start = 0
r_end = end - self.arr.size
split = l_end - l_start
return l_start, l_end, r_start, r_end, split
else:
return start, end, end, end, n
def peek(self, n, offset=0):
l_start, l_end, r_start, r_end, split = self.get_indices(n, offset, self.ridx)
out = np.zeros(n)
out[:split] = self.arr[l_start:l_end]
out[split:] = self.arr[r_start:r_end]
return out
def write(self, data, offset=0):
n = data.size
l_start, l_end, r_start, r_end, split = self.get_indices(n, offset, self.widx)
self.arr[l_start:l_end] = data[:split]
self.arr[r_start:r_end] = data[split:]
def write_add(self, data, offset=0):
n = data.size
l_start, l_end, r_start, r_end, split = self.get_indices(n, offset, self.widx)
self.arr[l_start:l_end] += data[:split]
self.arr[r_start:r_end] += data[split:]
def radvance(self, n):
self.ridx = (self.ridx + n) % self.arr.size
def wadvance(self, n):
self.widx = (self.widx + n) % self.arr.size
def get_unread(self):
n = self.widx - self.ridx
if n < 0:
n += self.arr.size
return n
class PVCJack:
def __init__(self):
self.client = jack.Client("PVCJack")
self.client.inports.register("input")
self.client.outports.register("output")
self.window_size = 2048
self.n_windows = 4
self.gain = 2
self.inbuffer = CircularBuffer(self.window_size * 2)
self.outbuffer = CircularBuffer(self.window_size * 2)
# NOTE: this might not work when window size isn't even multiple of JACK block size
self.outbuffer.widx = self.outbuffer.arr.size // 2
print("Sample rate:", self.client.samplerate)
# self.pvc = pitchshift.PeakPitchShifter(self.client.samplerate, self.window_size, 2, 1, True, 8)
self.pvc = pitchshift.PitchShifter(
self.client.samplerate, self.window_size, 2, 1, True, 8, True
)
self.client.set_process_callback(self.process)
self.client.set_shutdown_callback(self.shutdown)
def process(self, frames):
# TODO: This breaks if client.blocksize is bigger than window_size
assert self.client.blocksize <= self.window_size
assert frames == self.client.blocksize
data = self.client.inports[0].get_array()
self.inbuffer.write(data)
self.inbuffer.wadvance(frames)
while self.inbuffer.get_unread() >= self.window_size:
advance = self.window_size // self.n_windows
block = self.inbuffer.peek(self.window_size)
out_block = self.pvc.process(block, advance, advance)
self.outbuffer.write(np.zeros(advance), advance * (self.n_windows - 1))
self.outbuffer.write_add(out_block)
self.inbuffer.radvance(advance)
self.outbuffer.wadvance(advance)
self.client.outports[0].get_array()[:] = self.outbuffer.peek(frames) * self.gain
self.outbuffer.radvance(frames)
def shutdown(self, status, reason):
print("JACK shutdown!")
print("status:", status)
print("reason:", reason)
class GUI:
def __init__(self, pvc_jack):
self.sliders = []
self.labels = []
self.pvc_jack = pvc_jack
self.tk = tkinter.Tk()
self.tk.title("PVCJack")
self.tk.columnconfigure(1, weight=1)
self.create_slider(self.set_pitch, "pitch", self.pvc_jack.pvc.pitch_mult)
self.create_slider(self.set_formant, "formant", self.pvc_jack.pvc.f_pitch_mult)
self.tk.bind("<Return>", self.reset_phase)
def set_pitch(self, x):
self.pvc_jack.pvc.pitch_mult = float(x)
def set_formant(self, x):
self.pvc_jack.pvc.f_pitch_mult = float(x)
def reset_phase(self, evt):
print("reset phase")
#self.pvc_jack.pvc.last_phase_out = np.array(self.pvc_jack.pvc.last_phase)
self.pvc_jack.pvc.last_phase.fill(0)
self.pvc_jack.pvc.last_phase_out.fill(0)
def create_slider(self, cmd, name, default):
i = len(self.sliders)
label = tkinter.Label(self.tk, text=name)
self.labels.append(label)
label.grid(row=i, column=0, sticky="SE")
slider = tkinter.Scale(
self.tk,
from_=0.1,
to=4,
resolution=0.1,
orient=tkinter.HORIZONTAL,
command=cmd,
)
slider.set(default)
self.sliders.append(slider)
slider.grid(row=i, column=1, sticky="EW")
if __name__ == "__main__":
pvc_jack = PVCJack()
gui = GUI(pvc_jack)
# pvc_jack.run()
with pvc_jack.client:
gui.tk.mainloop()