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>
116 lines
5.4 KiB
Python
116 lines
5.4 KiB
Python
"""Interface sounds: beeps, menu moves, confirmations, on/off sweeps and an air horn."""
|
|
|
|
import math
|
|
|
|
import numpy as np
|
|
|
|
from .synth import Biquad, rng_from, sampleRate, smooth_step, soften, square_wave
|
|
|
|
|
|
def beep(frequency, seconds):
|
|
"""A clean beep with a touch of its octave, clickless at both ends."""
|
|
times = np.arange(int(sampleRate * seconds)) / sampleRate
|
|
envelope = np.minimum(1.0, times / 0.005) * np.minimum(1.0, (seconds - times) / 0.03)
|
|
tone = np.sin(2.0 * math.pi * frequency * times) + 0.3 * np.sin(4.0 * math.pi * frequency * times)
|
|
return 0.25 * tone * envelope
|
|
|
|
|
|
def fanfare():
|
|
"""A quick rising C major arpeggio of beeps, holding the top note."""
|
|
notes = [523.3, 659.3, 784.0, 1046.5]
|
|
parts = [beep(frequency, 0.14) for frequency in notes[:-1]] + [beep(notes[-1], 0.5)]
|
|
return np.concatenate(parts)
|
|
|
|
|
|
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)
|
|
|
|
|
|
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 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):
|
|
"""A short triangle tone gliding up an octave for something turning on, or down for turning off."""
|
|
times = np.arange(int(sampleRate * seconds)) / sampleRate
|
|
progress = times / seconds
|
|
if not rising:
|
|
progress = 1.0 - progress
|
|
frequency = low * (high / low) ** progress
|
|
phases = np.cumsum(frequency) / sampleRate
|
|
triangle = 2.0 * np.abs(2.0 * (phases % 1.0) - 1.0) - 1.0
|
|
envelope = np.minimum(1.0, times / 0.005) * np.minimum(1.0, (seconds - times) / 0.03)
|
|
return 0.3 * triangle * envelope
|
|
|
|
|
|
def denied_click():
|
|
"""A dull click: nothing to use, or not allowed."""
|
|
times = np.arange(int(sampleRate * 0.06)) / sampleRate
|
|
return 0.3 * np.sin(2.0 * math.pi * 180.0 * times) * np.exp(-times / 0.012)
|
|
|
|
|
|
def menu_move():
|
|
"""Moving between menu items: a short hollow knock, like tapping a bone."""
|
|
times = np.arange(int(sampleRate * 0.08)) / sampleRate
|
|
tone = np.sin(2.0 * math.pi * 620.0 * times) + 0.4 * np.sin(2.0 * math.pi * 1710.0 * times)
|
|
return 0.2 * tone * np.exp(-times / 0.018) * np.minimum(1.0, times / 0.001)
|
|
|
|
|
|
def confirm_chime(step=0):
|
|
"""Choosing or confirming: a short low square blip, then a tone an octave up that fades slowly away.
|
|
|
|
step raises it by that many semitones, so a count can be heard, such as
|
|
one step higher for each item held.
|
|
"""
|
|
lift = 2.0 ** (step / 12.0)
|
|
blip = square_wave(500.0 * lift, 0.07)
|
|
tone = square_wave(1000.0 * lift, 0.8)
|
|
# The tail falls steeply at first, then lingers quietly.
|
|
tailProgress = np.arange(len(tone)) / len(tone)
|
|
tone *= (1.0 - tailProgress) ** 2.5
|
|
chime = np.concatenate((blip, tone))
|
|
# Soften the very start and the join, so neither clicks.
|
|
attack = int(sampleRate * 0.003)
|
|
chime[:attack] *= np.linspace(0.0, 1.0, attack)
|
|
return 0.22 * soften(chime)
|