The code, in parts.
The code of the notebook, in the order it appears. Paste each part into a cell of a new notebook and run them in order. In a local Jupyter, run %pip install sounddevice first.
Setup
import sounddevice as sd
import numpy as np
import asyncio
SR = 48000
Clips, scales and instruments
from IPython.display import Audio
from dataclasses import dataclass
def play(wave):
return Audio(wave, rate=SR, normalize=False, autoplay=False)
def pluck(f, dur_s=1.0, tau=0.3, amplitude=0.25):
t = np.arange(round(dur_s * SR)) / SR
env = np.exp(-t / tau)
wave = np.sin(2 * np.pi * f * t)
return amplitude * env * wave
def mix_sounds(sounds): # a sound is (onset_s, wave)
dur_s = max([onset_s + len(wave) / SR for onset_s, wave in sounds])
out = np.zeros(round(dur_s * SR))
for onset_s, wave in sounds:
onset = round(onset_s * SR)
out[onset:onset + len(wave)] += wave
return out
note_names = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
class Pitch:
def __init__(self, value):
if isinstance(value, str): # a name like "C4": the octave last, the note before
value = 12 * (int(value[-1]) + 1) + note_names.index(value[:-1])
self.midi = value
@property
def f(self): # the frequency in Hz
return 440 * 2 ** ((self.midi - 69) / 12)
def __add__(self, semitones):
if isinstance(semitones, Pitch):
raise TypeError("Add semitones, not a pitch.")
return Pitch(self.midi + semitones)
def __sub__(self, other):
if isinstance(other, Pitch):
return self.midi - other.midi # an interval
return Pitch(self.midi - other) # a lower pitch
def __eq__(self, other):
return isinstance(other, Pitch) and self.midi == other.midi
def __repr__(self):
return note_names[self.midi % 12] + str(self.midi // 12 - 1)
@dataclass
class Clip:
events: list # (onset, data): onset in beats
length: float # in beats: where the next clip starts
def __rshift__(self, other): # then
moved = [(onset + self.length, data) for onset, data in other.events]
return Clip(self.events + moved, self.length + other.length)
def __mul__(self, times): # again
out = Clip([], 0)
for _ in range(times):
out = out >> self
return out
def __add__(self, n):
return Clip([(onset, data + n) for onset, data in self.events], self.length)
def __or__(self, other): # together
return Clip(self.events + other.events, max(self.length, other.length))
def note(data, length=1):
return Clip([(0, data)], length)
def rest(length):
return Clip([], length)
class Scale:
def __init__(self, steps, root):
self.root = root
self.distances = []
distance = 0
for step in steps:
self.distances.append(distance)
distance += step
def __getitem__(self, degree):
octave = degree // len(self.distances)
step = degree % len(self.distances)
return self.root + (12 * octave) + self.distances[step]
def play_clip(clip, scale, beat_s=0.5):
sounds = [(onset * beat_s, pluck(scale[data].f)) for onset, data in clip.events]
return play(mix_sounds(sounds))
def pad(f, dur_s=10.0, tau=4, amplitude=0.25, attack=3):
t = np.arange(round(dur_s * SR)) / SR
env = np.exp(-t / tau)
wave1 = np.sin(2 * np.pi * f * t)
wave2 = np.sin(2 * np.pi * (f + 0.5) * t)
wave = wave1 + wave2
rise = np.minimum(1, t/attack)
return amplitude * env * wave * rise
a_major = Scale([2, 2, 1, 2, 2, 2, 1], Pitch("A4"))
a_minor = Scale([2, 1, 2, 2, 1, 2, 2], Pitch("A4"))
Drum sounds
rng = np.random.default_rng(808)
def decay(dur_s, tau):
t = np.arange(round(dur_s * SR)) / SR
return np.exp(-t / tau)
def kick(dur_s=0.3):
t = np.arange(round(dur_s * SR)) / SR
f = 45 + 135 * np.exp(-t / 0.03) # the pitch falls fast
return 0.5 * np.sin(2 * np.pi * np.cumsum(f) / SR) * decay(dur_s, 0.07)
def snare(dur_s=0.2):
t = np.arange(round(dur_s * SR)) / SR
tone = 0.5 * np.sin(2 * np.pi * 190 * t) * decay(dur_s, 0.03)
noise = 0.7 * rng.uniform(-1, 1, len(t)) * decay(dur_s, 0.05)
return 0.25 * (tone + noise)
def hat(dur_s=0.05):
return 0.12 * np.diff(rng.uniform(-1, 1, round(dur_s * SR)), prepend=0) * decay(dur_s, 0.008)
kit = {"k": kick(), "s": snare(), "h": hat()}
def drum(name):
return kit[name]
Sources, from lesson 09
class Sine:
def __init__(self, f, amp=0.2):
self.f = f
self.amp = amp
self.phase = 0.0
def block(self, frames):
phases = self.phase + 2 * np.pi * self.f * np.arange(frames) / SR
self.phase += 2 * np.pi * self.f * frames / SR
return self.amp * np.sin(phases)
class Voice:
def __init__(self, wave):
self.wave = wave
self.i = 0
def block(self, frames):
out = np.zeros(frames)
chunk = self.wave[self.i : self.i + frames]
out[:len(chunk)] = chunk
self.i += frames
return out
@property
def done(self):
return self.i >= len(self.wave)
class Mixer:
def __init__(self, sources):
self.sources = sources
def block(self, frames):
out = np.zeros(frames)
for source in list(self.sources):
out += source.block(frames)
if getattr(source, "done", False):
self.sources.remove(source)
return out
def stream_of(source, blocksize=512):
def callback(outdata, frames, time, status):
outdata[:, 0] = np.clip(source.block(frames), -1, 1)
return sd.OutputStream(samplerate=SR, channels=1, blocksize=blocksize, callback=callback)
One stream, always on
m = Mixer([])
out = stream_of(m)
out.start()
a_minor = Scale([2, 1, 2, 2, 1, 2, 2], Pitch("A4"))
A clock in beats
class Clock:
def __init__(self, bpm=120):
self.bpm = bpm
@property
def beat_s(self):
return 60 / self.bpm
clock = Clock(110)
tick = pluck(Pitch("A5").f, dur_s=0.05)
A metronome
class Metronome:
def __init__(self, sound, clock):
self.sound = sound
self.clock = clock
self.position = 0.0 # in beats
self.voices = Mixer([])
def block(self, frames):
end = self.position + frames / SR / self.clock.beat_s
if int(end) > int(self.position): # a beat falls inside this block
self.voices.sources.append(Voice(self.sound))
self.position = end
return self.voices.block(frames)
m.sources = [Metronome(pluck(Pitch("A4").f, dur_s=2), clock)] # now they ring together
Clips and an instrument
call = note(0) >> note(1) >> note(2) >> note(0)
climb = note(2) >> note(3) >> note(4, 2)
bells = note(4, 0.5) >> note(5, 0.5) >> note(4, 0.5) >> note(3, 0.5) >> note(2) >> note(0)
ending = note(0) >> note(-3) >> note(0, 2)
fm = call * 2 >> climb * 2 >> bells * 2 >> ending * 2
def plucked(degree):
return pluck(a_minor[degree].f, dur_s=0.8)
Voices that wait
class Voice:
def __init__(self, wave, delay=0):
self.wave = np.concatenate([np.zeros(delay), wave]) # wait delay samples, then play
self.i = 0
def block(self, frames):
out = np.zeros(frames)
chunk = self.wave[self.i : self.i + frames]
out[:len(chunk)] = chunk
self.i += frames
return out
@property
def done(self):
return self.i >= len(self.wave)
A loop, on time, that can change
class Loop:
def __init__(self, clip, sound, clock):
self.clip = clip
self.sound = sound
self.clock = clock
self.next = None # a clip waiting for the end of this one
self.position = 0.0
self.voices = Mixer([])
def start_voices(self, a, b, offset):
for onset, data in self.clip.events:
if a <= onset < b:
delay = round((offset + onset - a) * self.clock.beat_s * SR) # beats into the block, in samples
self.voices.sources.append(Voice(self.sound(data), delay))
def block(self, frames):
end = self.position + frames / SR / self.clock.beat_s
self.start_voices(self.position, end, 0)
if end >= self.clip.length:
offset = self.clip.length - self.position # beats of this block before the end of the clip
end = end - self.clip.length
if self.next is not None: # a waiting clip starts here
self.clip, self.next = self.next, None
self.start_voices(0, end, offset)
self.position = end
return self.voices.block(frames)
melody = Loop(fm, plucked, clock)
m.sources = [melody]
Two loops
def soft(degree):
return pad(a_minor[degree].f, dur_s=4, tau=2, amplitude=0.1, attack=0.3)
first = Loop(call + (-7) >> rest(3), soft, clock)
second = Loop(climb >> rest(2), plucked, clock)
m.sources = [first, second]
A step sequencer
class Steps:
def __init__(self, text, name, step=0.25):
self.steps = [symbol == "x" for symbol in text]
self.name = name
self.step = step # in beats: 0.25 is a sixteenth
@property
def events(self):
return [(i * self.step, self.name) for i, on in enumerate(self.steps) if on]
@property
def length(self):
return len(self.steps) * self.step
def toggle(self, i):
self.steps[i] = not self.steps[i]
def __repr__(self):
return "".join("x" if on else "." for on in self.steps)
kicks = Steps("x...x...x...x...", "k")
snares = Steps("....x.......x...", "s")
hats = Steps("x.x.x.x.x.x.x.x.", "h")
melody = Loop(fm, plucked, clock)
m.sources = [Loop(kicks, drum, clock), Loop(snares, drum, clock), Loop(hats, drum, clock), melody]