-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen4.py
405 lines (326 loc) · 12.8 KB
/
gen4.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import numpy as np
import tkinter as tk
from tkinter import ttk
from scipy.io.wavfile import write
import sounddevice as sd
import matplotlib.pyplot as plt
import numba
from scipy.integrate import odeint
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import uuid
from scipy.signal import butter, lfilter, iirfilter
sampling_rate = 44100 # Sampling rate in Hz
current_sound = np.zeros(sampling_rate, dtype=np.float32)
current_freq = None # Create a variable to store the current frequency
previous_params = None
def create_slider(window, min_value, max_value, resolution, orient, label):
slider = tk.Scale(window, from_=min_value, to=max_value,
resolution=resolution, orient=orient, label=label)
return slider
# Additional functions for low pass filter and resonance
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
def butter_highpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='high', analog=False)
return b, a
def butter_highpass_filter(data, cutoff, fs, order=5):
b, a = butter_highpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
def distortion(signal, amount):
return np.tanh(signal * amount) / np.tanh(amount)
def parameters_changed():
global previous_params
current_params = (
duration_slider.get(),
mass_slider.get(),
stiffness_slider.get(),
damping_slider.get(),
friction_slider.get(),
attack_slider.get(),
decay_slider.get(),
sustain_slider.get(),
release_slider.get(),
pitch_slider.get(),
modulation_index_slider.get(),
modulation_frequency_slider.get(),
low_pass_filter_slider.get(),
resonance_slider.get(),
high_pass_filter_slider.get(),
distortion_slider.get(),
)
if previous_params is None or current_params != previous_params:
previous_params = current_params
return True
return False
def friction_force(velocity, friction_coeff):
if velocity > 0:
return -friction_coeff
elif velocity < 0:
return friction_coeff
else:
return 0
def acceleration(position, velocity, spring_const, damping_coeff, friction_coeff, mass):
spring_force = -spring_const * position
damping_force = -damping_coeff * velocity
friction = friction_force(velocity, friction_coeff)
net_force = spring_force + damping_force + friction
return net_force / mass
def slip_stick_simulation(t, init_position, init_velocity, mass, spring_const, damping_coeff, friction_coeff):
dt = t[1] - t[0]
position = np.zeros_like(t)
velocity = np.zeros_like(t)
position[0] = init_position
velocity[0] = init_velocity
for i in range(1, len(t)):
a = acceleration(position[i-1], velocity[i-1],
spring_const, damping_coeff, friction_coeff, mass)
velocity[i] = velocity[i-1] + a * dt
position[i] = position[i-1] + velocity[i-1] * dt
return position, velocity
def generate_waveform(freq_radians, mod_radians, modulation_index, dt, t):
if current_waveform_type.get() == "sine":
carrier = np.sin(freq_radians * np.cumsum(dt * np.ones_like(t)))
elif current_waveform_type.get() == "saw":
carrier = np.cumsum(dt * freq_radians / np.pi) % 2 - 1
elif current_waveform_type.get() == "square":
carrier = np.sign(
np.sin(freq_radians * np.cumsum(dt * np.ones_like(t))))
modulator = modulation_index * \
np.sin(mod_radians * np.cumsum(dt * np.ones_like(t)))
waveform = (1 + modulator) * carrier
return waveform
def generate_sound():
duration = duration_slider.get()
mass = mass_slider.get()
stiffness = stiffness_slider.get()
damping = damping_slider.get()
friction = friction_slider.get()
attack = attack_slider.get()
decay = decay_slider.get()
sustain = sustain_slider.get()
release = release_slider.get()
pitch = pitch_slider.get()
modulation_index = modulation_index_slider.get()
modulation_frequency = modulation_frequency_slider.get()
slip_stick_params = (
mass, stiffness, damping, friction, 0, 0)
dt = 1.0 / sampling_rate
t = np.arange(0, duration, dt)
# Generate slip-stick signal
position, velocity = slip_stick_simulation(
t, 0, 1, mass, stiffness, damping, friction)
slip_stick_signal = position
# Create and apply an ADSR envelope
total_samples = len(t)
attack_samples = int(attack * total_samples)
decay_samples = int(decay * total_samples)
sustain_samples = max(0, total_samples - attack_samples -
decay_samples - int(release * total_samples))
release_samples = total_samples - attack_samples - decay_samples - sustain_samples
envelope = np.concatenate((
np.linspace(0, 1, attack_samples),
np.linspace(1, sustain, decay_samples),
np.ones(sustain_samples) * sustain,
np.linspace(sustain, 0, release_samples),
))
slip_stick_signal = slip_stick_signal[:total_samples] * envelope
# Generate the waveform
freq_radians = pitch * 2 * np.pi
mod_radians = modulation_frequency * 2 * np.pi
waveform = generate_waveform(
freq_radians, mod_radians, modulation_index, dt, t)
# Combine the slip-stick signal with the waveform
sound = waveform * slip_stick_signal
# Apply the low pass filter
cutoff_frequency = low_pass_filter_slider.get()
sound = butter_lowpass_filter(sound, cutoff_frequency, sampling_rate)
# Apply the high-pass filter
cutoff_frequency_high = high_pass_filter_slider.get()
sound = butter_highpass_filter(sound, cutoff_frequency_high, sampling_rate)
# Apply the distortion effect
distortion_amount = distortion_slider.get()
sound = distortion(sound, distortion_amount)
# Apply the resonance
resonance = resonance_slider.get()
sound = sound * resonance
return sound
def normalize_signal(signal):
max_val = np.max(np.abs(signal))
if max_val == 0:
return signal
else:
return signal / max_val
def generate_new_sound():
global current_sound
current_sound = normalize_signal(generate_sound())
plot_sound()
def play_sound(freq=None):
global current_sound
global current_freq
if freq is not None:
pitch_slider.set(freq)
# Regenerate the sound with updated frequency value
if freq is not current_freq or parameters_changed():
generate_new_sound()
current_freq = freq
sd.play(current_sound, samplerate=sampling_rate)
plot_sound()
def stop_sound():
sd.stop()
def save_sound():
global current_sound
file_name = f"slipstick_ui_output_{str(uuid.uuid4())}.wav"
current_sound_normalized = normalize_signal(current_sound)
write(file_name, sampling_rate,
(current_sound_normalized * 32767).astype(np.int16))
def randomize_parameters():
global current_sound
duration_slider.set(np.random.uniform(2.0, 6.0))
mass_slider.set(np.random.uniform(0.001, 0.05))
stiffness_slider.set(np.random.uniform(500.0, 5000.0))
damping_slider.set(np.random.uniform(0.001, 0.1))
pitch_slider.set(np.random.uniform(20.0, 200.0))
attack_slider.set(np.random.uniform(0.005, 0.1))
decay_slider.set(np.random.uniform(0.1, 0.5))
sustain_slider.set(np.random.uniform(0.1, 0.8))
release_slider.set(np.random.uniform(0.1, 1.5))
modulation_index_slider.set(np.random.uniform(0, 10))
modulation_frequency_slider.set(np.random.uniform(0, 1000))
friction_slider.set(np.random.uniform(0.001, 0.01))
high_pass_filter_slider.set(np.random.uniform(500, 5000))
distortion_slider.set(np.random.uniform(1, 50))
generate_new_sound()
def plot_sound():
global current_sound
fig.clear()
ax = fig.add_subplot(111)
_, _, _, im = ax.specgram(
current_sound, NFFT=1024, Fs=sampling_rate, cmap="viridis", mode="magnitude")
fig.colorbar(im, ax=ax)
ax.set_xlabel("Time (s)", color="white", fontsize=12)
ax.set_ylabel("Frequency (Hz)", color="white", fontsize=12)
ax.tick_params(axis="x", colors="white")
ax.tick_params(axis="y", colors="white")
canvas.draw()
def keyboard_event(e):
key = e.char.lower()
if key == 'r':
randomize_parameters()
elif key in keyboard_mapping:
frequency = keyboard_mapping[key]
pitch_slider.set(frequency)
play_sound(frequency)
window = tk.Tk()
window.title("Slipstick Synthesis")
plt.style.use("dark_background")
fig = plt.Figure(figsize=(10, 6), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=window)
canvas.get_tk_widget().grid(row=0, rowspan=12, column=1)
duration_slider = create_slider(
window, 1, 10, 0.1, tk.HORIZONTAL, "Duration (s)")
mass_slider = create_slider(window, 0.001, 0.05, 0.001, tk.HORIZONTAL, "Mass")
stiffness_slider = create_slider(
window, 500, 5000, 10, tk.HORIZONTAL, "Spring Stiffness")
damping_slider = create_slider(
window, 0.001, 0.1, 0.001, tk.HORIZONTAL, "Damping")
pitch_slider = create_slider(window, 20, 200, 1, tk.HORIZONTAL, "Pitch")
attack_slider = create_slider(
window, 0.005, 0.1, 0.005, tk.HORIZONTAL, "Attack Time (s)")
decay_slider = create_slider(
window, 0.1, 0.5, 0.1, tk.HORIZONTAL, "Decay Time (s)")
sustain_slider = create_slider(
window, 0.1, 0.8, 0.1, tk.HORIZONTAL, "Sustain Level")
release_slider = create_slider(
window, 0.1, 1.5, 0.1, tk.HORIZONTAL, "Release Time (s)")
modulation_index_slider = create_slider(
window, 0.1, 10, 0.1, tk.HORIZONTAL, "Mod index")
modulation_frequency_slider = create_slider(
window, 1, 1000, 10, tk.HORIZONTAL, "Mod freq")
friction_slider = create_slider(
window, 0.001, 0.01, 0.001, tk.HORIZONTAL, "Friction")
low_pass_filter_slider = create_slider(
window, 20, 20000, 10, tk.HORIZONTAL, "Low Pass Filter (Hz)")
resonance_slider = create_slider(
window, 0.1, 10, 0.1, tk.HORIZONTAL, "Resonance")
high_pass_filter_slider = create_slider(
window, 20, 20000, 10, tk.HORIZONTAL, "High Pass Filter (Hz)")
distortion_slider = create_slider(
window, 1, 50, 1, tk.HORIZONTAL, "Distortion")
current_waveform_type = tk.StringVar(value="sine")
sine_wave_button = ttk.Radiobutton(
window, text="Sine wave", variable=current_waveform_type, value="sine")
saw_wave_button = ttk.Radiobutton(
window, text="Sawtooth wave", variable=current_waveform_type, value="saw")
square_wave_button = ttk.Radiobutton(
window, text="Square wave", variable=current_waveform_type, value="square")
duration_slider.set(4.0)
mass_slider.set(0.01)
stiffness_slider.set(1500)
damping_slider.set(0.08)
pitch_slider.set(100.0)
attack_slider.set(0.01)
decay_slider.set(0.2)
sustain_slider.set(0.1)
release_slider.set(0.8)
modulation_index_slider.set(1.0)
modulation_frequency_slider.set(50.0)
friction_slider.set(0.005)
low_pass_filter_slider.set(20000) # Set the default value to 20000 Hz
resonance_slider.set(1) # Set the default value to 1
high_pass_filter_slider.set(1000)
distortion_slider.set(10)
play_button = ttk.Button(window, text="Play Sound", command=play_sound)
stop_button = ttk.Button(window, text="Stop Sound", command=stop_sound)
save_button = ttk.Button(window, text="Save Sound", command=save_sound)
randomize_button = ttk.Button(
window, text="Randomize Parameters", command=randomize_parameters)
duration_slider.grid(row=0, column=0)
mass_slider.grid(row=1, column=0)
stiffness_slider.grid(row=2, column=0)
damping_slider.grid(row=3, column=0)
friction_slider.grid(row=4, column=0)
pitch_slider.grid(row=5, column=0)
attack_slider.grid(row=6, column=0)
decay_slider.grid(row=7, column=0)
sustain_slider.grid(row=8, column=0)
release_slider.grid(row=9, column=0)
modulation_index_slider.grid(row=10, column=0)
modulation_frequency_slider.grid(row=11, column=0)
play_button.grid(row=12, column=0)
stop_button.grid(row=13, column=0)
save_button.grid(row=14, column=0)
randomize_button.grid(row=15, column=0)
sine_wave_button.grid(row=16, column=0)
saw_wave_button.grid(row=17, column=0)
square_wave_button.grid(row=18, column=0)
low_pass_filter_slider.grid(row=12, column=1)
resonance_slider.grid(row=13, column=1)
high_pass_filter_slider.grid(row=14, column=1)
distortion_slider.grid(row=15, column=1)
keyboard_mapping = {
'a': 261.63, # C4
'w': 277.18, # C#4
's': 293.66, # D4
'e': 311.13, # D#4
'd': 329.63, # E4
'f': 349.23, # F4
't': 369.99, # F#4
'g': 392.00, # G4
'y': 415.30, # G#4
'h': 440.00, # A4
'u': 466.16, # A#4
'j': 493.88, # B4
'k': 523.25, # C5
}
window.bind("<KeyPress>", keyboard_event)
window.mainloop()