From 74406b9f785dea676f361bd7c8c75ae6f1e033e1 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Mon, 30 Nov 2026 03:51:59 -0500 Subject: [PATCH] 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 --- sfx/ui.py | 65 +++++++++++++++++++++----------- tests/data/sfx_fingerprints.npz | Bin 179983 -> 179984 bytes 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/sfx/ui.py b/sfx/ui.py index a7bea70..492251d 100644 --- a/sfx/ui.py +++ b/sfx/ui.py @@ -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): diff --git a/tests/data/sfx_fingerprints.npz b/tests/data/sfx_fingerprints.npz index c39896909a8385b8db35195e4f2a78de1a9ea8d1..56066674f1f32fc10c5b691cbe12b890c9aabc7c 100644 GIT binary patch delta 1932 zcmeDG$2H*}*M>_WEdDz2K9jG7sMpuCFo40_-oM6II2n$8&+)oBW!d)Esgo{E3UwDg zeQfi+PqR)f-FB>NkIvIWm$*GI=DfKmqP^NuUwL|Y@x8lOllHEeUi(+C=w8;+cc*rq z*m30Ug_r+Ye*gJ2^ZCs5XMzfb z4m|xVcxS-_{l6C*KD>DF)XvI8!KlJgdba$gB{MJZzcyjthu18h?I+tE_!|6V z!LdC8U&WW*;upViu-tiJnecDPXZtso?zGiBRwaG7{o)+HmbHtlt3%5VsB}8+OjTvv z`874%bSHCnv8nx%oHq?`S<(+qyxyp3=K5uVT4Q}}-a6y@UBW5$Ud-A{rPFtP`ZC9; z`oZ~(H}*gMx;WT%`>&e@U0Af1&0Ng8FjO|udeeqEn!J}jh-hAFJ-+IdX^_lQ1GBkN z_q0E)w|Evit?}vopkt3u8w!{DE!Y^YAhT3*v6QNd?#|Gk3L7jhg(OdQRo_+85_r$$Bt{qpb z-E))sE%P+FP5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