Rebuild the air horn from a recording of a real handheld one.
The old horn was two reeds a major third apart, which is how car horns are built, and it sounded like one. A canned air horn is a single reed near D5 whose second harmonic is as strong as its fundamental. For its first 100 milliseconds the reed hasn't caught: the pitch jumps about above the note, air rushes through and it blares brighter, settling by 300 milliseconds. Then it holds with its harmonics wavering and sags a little in pitch as the can's pressure drops. air_horn takes a seed; the default of 7 is the blast that was approved by ear. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
30b42f3efe
commit
74406b9f78
2 files changed
+43
-22
No files matched your search
@@ -4,7 +4,7 @@ import math
|
||||
|
||||
import numpy as np
|
||||
|
||||
from .synth import Biquad, sampleRate, saw_wave, soften, square_wave
|
||||
from .synth import Biquad, rng_from, sampleRate, smooth_step, soften, square_wave
|
||||
|
||||
|
||||
def beep(frequency, seconds):
|
||||
@@ -22,31 +22,52 @@ def fanfare():
|
||||
return np.concatenate(parts)
|
||||
|
||||
|
||||
def air_horn(seconds=1.2):
|
||||
"""A loud stadium air horn, for something ending, such as time running out.
|
||||
def _wander(rng, length, depth, rate):
|
||||
"""Smooth random wandering between -depth and depth, changing course about rate times a second."""
|
||||
points = rng.uniform(-1.0, 1.0, int(length / sampleRate * rate) + 3)
|
||||
return depth * np.interp(np.arange(length), np.linspace(0, length, len(points)), points)
|
||||
|
||||
Two brassy reeds a major third apart, each a pair of slightly detuned
|
||||
sawtooths, scoop up into pitch as the air hits them, then blare with a
|
||||
faint flutter and cut off with a short tail.
|
||||
|
||||
def air_horn(seconds=1.2, seed=7):
|
||||
"""A loud handheld air horn, for something ending, such as time running out.
|
||||
|
||||
Modelled on a recording of a canned air horn: one reed near D5 whose second
|
||||
harmonic is as strong as its fundamental. For the first 100 milliseconds the
|
||||
reed hasn't caught: its pitch jumps about above the note, air rushes through
|
||||
and it blares brighter, settling over 300 milliseconds. Then it holds, its
|
||||
harmonics wavering, and sags a little in pitch as the can's pressure drops.
|
||||
"""
|
||||
rng = rng_from(seed)
|
||||
length = int(sampleRate * seconds)
|
||||
times = np.arange(length) / sampleRate
|
||||
# The pitch scoops up from a little flat over the first 80 milliseconds.
|
||||
scoop = 1.0 - 0.12 * np.exp(-times / 0.03)
|
||||
flutter = 1.0 + 0.004 * np.sin(2.0 * math.pi * 6.0 * times)
|
||||
horn = np.zeros(length)
|
||||
for frequency, level in ((349.2, 1.0), (440.0, 0.8)):
|
||||
for detune in (0.997, 1.004):
|
||||
phaseStep = frequency * detune * scoop * flutter / sampleRate
|
||||
horn += level * saw_wave(np.cumsum(phaseStep) % 1.0, phaseStep)
|
||||
# A reedy, nasal body: the band where an air horn blares, with the fizz above it taken off.
|
||||
body = Biquad("bandpass").process(horn, 1100.0, 0.9) + 0.35 * Biquad("lowpass").process(horn, 600.0)
|
||||
body = Biquad("lowpass").process(body, 3500.0)
|
||||
# Overblown: squash it, so it sounds strained.
|
||||
body = np.tanh(1.8 * body / np.max(np.abs(body)))
|
||||
attack = np.minimum(1.0, times / 0.02)
|
||||
release = np.clip((seconds - times) / 0.18, 0.0, 1.0) ** 1.5
|
||||
return 0.5 * body * attack * release
|
||||
# The reed jumps about well above the note until it catches at about 100 milliseconds.
|
||||
unlocked = 1.0 - smooth_step(0.07, 0.11, times)
|
||||
jumps = np.repeat(rng.uniform(-1.0, 1.0, length // 400 + 1), 400)[:length]
|
||||
jumps = Biquad("lowpass").process(jumps, 300.0)
|
||||
frequency = (588.0 - 10.0 * times / seconds) * (1.0 + _wander(rng, length, 0.003, 8.0))
|
||||
frequency = frequency * (1.0 + 1.3 * unlocked * (0.2 + 0.18 * jumps))
|
||||
phase = 2.0 * math.pi * np.cumsum(frequency) / sampleRate
|
||||
# Harmonic levels in decibels, from the recording; the upper ones blare brighter at first.
|
||||
harmonicDb = [-2, 0, -5, -14, -22, -22, -26, -26, -32, -32, -35, -37, -39, -41, -44, -46, -49, -52]
|
||||
brightness = 10.0 * (1.0 - smooth_step(0.12, 0.3, times))
|
||||
tone = np.zeros(length)
|
||||
for number, decibels in enumerate(harmonicDb, start=1):
|
||||
if 720.0 * number > sampleRate * 0.45:
|
||||
break
|
||||
boost = brightness * min(1.0, (number - 1) / 3.0)
|
||||
level = 10.0 ** ((decibels + boost + _wander(rng, length, 4.0, 4.0)) / 20.0)
|
||||
tone += level * np.sin(number * phase + rng.uniform(0.0, 2.0 * math.pi))
|
||||
sustain = slice(int(0.3 * sampleRate), None)
|
||||
tone /= np.sqrt(np.mean(tone[sustain] ** 2))
|
||||
# Air rushing through while it catches, louder than the tone, gone by 150 milliseconds.
|
||||
noise = Biquad("highpass").process(rng.standard_normal(length), 1500.0)
|
||||
noise *= 1.4 * (1.0 - smooth_step(0.06, 0.15, times)) / np.sqrt(np.mean(noise ** 2))
|
||||
sound = tone + noise
|
||||
sound /= np.sqrt(np.mean(sound[sustain] ** 2))
|
||||
levelDb = np.interp(times, [0.0, 0.01, 0.05, 0.1, 0.17, 0.3], [-40.0, -19.0, -11.0, -10.0, -5.0, 0.0])
|
||||
release = np.clip((seconds - times) / 0.25, 0.0, 1.0) ** 1.5
|
||||
sound *= 10.0 ** (levelDb / 20.0) * release
|
||||
return 0.5 * sound / np.abs(sound).max()
|
||||
|
||||
|
||||
def toggle_sweep(rising, seconds=0.14, low=500.0, high=1000.0):
|
||||
|
||||
Reference in new issue
Block a user