The kit.
Paste this into the first cell of a new notebook and run it.
SR = 48000
from math import exp
from random import random
def vca(wave, signal):
if len(wave) != len(signal):
raise ValueError("The sound and control signal must have equal lengths.")
return [x * gain for x, gain in zip(wave, signal)]
def noise(duration=0.05, amplitude=0.15, sample_rate=SR):
n = round(duration * sample_rate)
return [amplitude * (2 * random() - 1) for _ in range(n)]
def envelope_lin(duration=0.05, sample_rate=SR):
n = round(duration * sample_rate)
denominator = max(1, n - 1)
return [1 - i / denominator for i in range(n)]
def square_wave_f(f, sample_rate=SR, amplitude=0.15):
phase = 0.0
wave = []
for frequency in f:
if not 0 < frequency < sample_rate / 2:
raise ValueError("Each frequency must be positive and below half the sample rate.")
wave.append(-amplitude if phase < 0.5 else amplitude)
phase = (phase + frequency / sample_rate) % 1
return wave
def edge_fade(wave, attack=0.001, release=0.005, sample_rate=SR):
n = len(wave)
attack_samples = max(1, round(attack * sample_rate))
release_samples = max(1, round(release * sample_rate))
edge = [
min(1.0, i / attack_samples, (n - 1 - i) / release_samples)
for i in range(n)
]
return vca(wave, edge)
def envelope_exp(duration=0.05, tau=0.012, sample_rate=SR):
if tau <= 0:
raise ValueError("tau must be positive.")
n = round(duration * sample_rate)
return [exp(-(i / sample_rate) / tau) for i in range(n)]
def closed_hat(duration=0.06, tau=0.010, amplitude=0.15, sample_rate=SR):
source = noise(duration, amplitude=amplitude, sample_rate=sample_rate)
shape = envelope_exp(duration, tau=tau, sample_rate=sample_rate)
return edge_fade(vca(source, shape), sample_rate=sample_rate)
def open_hat(duration=0.30, tau=0.070, amplitude=0.15, sample_rate=SR):
source = noise(duration, amplitude=amplitude, sample_rate=sample_rate)
shape = envelope_exp(duration, tau=tau, sample_rate=sample_rate)
return edge_fade(vca(source, shape), sample_rate=sample_rate)
def kick_raw(
duration=0.25, start_hz=180, end_hz=45,
pitch_tau=0.025, amp_tau=0.060,
amplitude=0.20, sample_rate=SR,
):
pitch_shape = envelope_exp(duration, tau=pitch_tau, sample_rate=sample_rate)
pitch = [end_hz + (start_hz - end_hz) * value for value in pitch_shape]
body = square_wave_f(pitch, sample_rate=sample_rate, amplitude=amplitude)
shape = envelope_exp(duration, tau=amp_tau, sample_rate=sample_rate)
return vca(body, shape)
def kick(
duration=0.25, start_hz=180, end_hz=45,
pitch_tau=0.025, amp_tau=0.060,
amplitude=0.20, sample_rate=SR,
):
wave = kick_raw(
duration=duration, start_hz=start_hz, end_hz=end_hz,
pitch_tau=pitch_tau, amp_tau=amp_tau,
amplitude=amplitude, sample_rate=sample_rate,
)
return edge_fade(wave, sample_rate=sample_rate)
def mix(a, b, ga=1.0, gb=1.0):
if len(a) != len(b):
raise ValueError("Pad the shorter sound with silence before mixing.")
return [ga * x + gb * y for x, y in zip(a, b)]
def snare(
duration=0.20, amplitude=0.15, noise_gain=0.5,
sample_rate=SR,
):
pitch = [
180 + 120 * value
for value in envelope_exp(duration, tau=0.010, sample_rate=sample_rate)
]
body = vca(
square_wave_f(pitch, amplitude=amplitude, sample_rate=sample_rate),
envelope_exp(duration, tau=0.025, sample_rate=sample_rate),
)
wires = vca(
noise(duration, amplitude=amplitude, sample_rate=sample_rate),
envelope_exp(duration, tau=0.055, sample_rate=sample_rate),
)
return edge_fade(
mix(body, wires, ga=1.0, gb=noise_gain),
sample_rate=sample_rate,
)