sfx.music holds looping background music pieces, made from code like the sound effects. There are two so far, both for distorted guitars, bass and drums: - battle_music: original heavy metal in D minor in the manner of Bach's D minor harpsichord concerto. A unison opening riff, arpeggios through a circle of fifths, a gallop over a held A, then the riff again. It loops every 24 seconds at 160 beats a minute. - moonlight_metal: Beethoven's Moonlight Sonata sped up and made heavy, in C sharp minor. The sonata's triplet arpeggios and opening chords, with a new lead melody, a triplet gallop and double kick. It loops every 27 seconds at 140 beats a minute. Each piece is its own module in sfx/music/pieces, named after its function, and is loaded the first time a game asks for it. A game only loads the pieces it plays, and a piece is added by adding its module, with nothing to register. The instruments the pieces share are in sfx/music/instruments.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
6.0 KiB
Python
141 lines
6.0 KiB
Python
"""The band every piece of music shares: guitars, bass, drums and cymbals, and the final mix.
|
|
|
|
Each instrument makes one note or hit as a numpy array at sampleRate, for a
|
|
piece to place into its tracks. Anything random takes rng, the piece's random
|
|
generator, so a piece's seed decides every hit.
|
|
"""
|
|
|
|
import math
|
|
|
|
import numpy as np
|
|
|
|
from ..synth import Biquad, note_frequency, saw_wave, sampleRate
|
|
|
|
|
|
def note_envelope(length, attackSeconds=0.003, releaseSeconds=0.02, decaySeconds=None):
|
|
"""Rise quickly, hold (or die away), and fall at the end, over length samples."""
|
|
times = np.arange(length) / sampleRate
|
|
envelope = np.minimum(1.0, times / attackSeconds) * np.clip((length / sampleRate - times) / releaseSeconds, 0, 1)
|
|
if decaySeconds is not None:
|
|
envelope *= np.exp(-times / decaySeconds)
|
|
return envelope
|
|
|
|
|
|
def distort(signal, drive, toneCutoff):
|
|
"""An amplifier pushed hard: trim the lows, clip, then darken like a speaker cabinet."""
|
|
tightened = Biquad("highpass").process(signal, 90.0)
|
|
clipped = np.tanh(drive * tightened)
|
|
darker = Biquad("lowpass").process(clipped, toneCutoff, 0.8)
|
|
return Biquad("lowpass").process(darker, toneCutoff * 1.4, 0.6)
|
|
|
|
|
|
def detuned_saws(midiNotes, length, detune, rng, vibrato=0.0):
|
|
"""Two slightly detuned sawtooths for each note, summed: the raw guitar string."""
|
|
times = np.arange(length) / sampleRate
|
|
wobble = 1.0 + vibrato * np.sin(2.0 * math.pi * 5.5 * times) * np.minimum(1.0, times / 0.25)
|
|
output = np.zeros(length)
|
|
for midiNote in midiNotes:
|
|
for cents in (-detune, detune):
|
|
frequency = note_frequency(midiNote) * 2.0 ** (cents / 1200.0) * wobble
|
|
phaseStep = frequency / sampleRate
|
|
phases = (rng.random() + np.cumsum(phaseStep)) % 1.0
|
|
output += saw_wave(phases, phaseStep)
|
|
return output
|
|
|
|
|
|
def guitar_note(midiNotes, seconds, rng, muted=False, detune=7.0, drive=9.0):
|
|
"""One rhythm guitar hit: an open, ringing chord or a tight palm-muted chug."""
|
|
tail = 0.03
|
|
length = int(sampleRate * (seconds + tail))
|
|
raw = detuned_saws(midiNotes, length, detune, rng)
|
|
if muted:
|
|
raw = Biquad("lowpass").process(raw, 900.0) * note_envelope(length, decaySeconds=0.07)
|
|
return 0.9 * distort(raw, drive * 0.8, 2600.0) * note_envelope(length, releaseSeconds=tail)
|
|
return distort(raw * note_envelope(length, releaseSeconds=tail), drive, 3400.0)
|
|
|
|
|
|
def lead_note(midiNote, seconds, rng):
|
|
"""One lead guitar note: brighter, singing, with vibrato on the longer notes."""
|
|
tail = 0.04
|
|
length = int(sampleRate * (seconds + tail))
|
|
vibrato = 0.004 if seconds > 0.2 else 0.0
|
|
raw = detuned_saws([midiNote], length, 4.0, rng, vibrato)
|
|
return distort(raw * note_envelope(length, releaseSeconds=tail), 14.0, 4800.0)
|
|
|
|
|
|
def bass_note(midiNote, seconds, rng, muted=False):
|
|
"""One bass note: a growling sawtooth over a round sine."""
|
|
length = int(sampleRate * (seconds + 0.02))
|
|
times = np.arange(length) / sampleRate
|
|
frequency = note_frequency(midiNote)
|
|
phaseStep = np.full(length, frequency / sampleRate)
|
|
saw = saw_wave(np.cumsum(phaseStep) % 1.0, phaseStep)
|
|
body = np.sin(2.0 * math.pi * frequency * times)
|
|
growl = Biquad("lowpass").process(np.tanh(2.5 * saw), 1100.0 if not muted else 600.0)
|
|
decay = 0.09 if muted else None
|
|
return (0.6 * body + 0.5 * growl) * note_envelope(length, releaseSeconds=0.02, decaySeconds=decay)
|
|
|
|
|
|
def power_chord(root):
|
|
return [root, root + 7, root + 12]
|
|
|
|
|
|
def kick(rng):
|
|
length = int(sampleRate * 0.25)
|
|
times = np.arange(length) / sampleRate
|
|
pitch = 48.0 + 110.0 * np.exp(-times / 0.03)
|
|
body = np.sin(2.0 * math.pi * np.cumsum(pitch) / sampleRate) * np.exp(-times / 0.09)
|
|
click = Biquad("highpass").process(rng.standard_normal(length), 2500.0) * np.exp(-times / 0.004)
|
|
return 0.9 * np.tanh(1.6 * body) + 0.25 * click
|
|
|
|
|
|
def snare(rng):
|
|
length = int(sampleRate * 0.3)
|
|
times = np.arange(length) / sampleRate
|
|
tone = np.sin(2.0 * math.pi * 190.0 * times) * np.exp(-times / 0.05)
|
|
rattle = Biquad("bandpass").process(rng.standard_normal(length), 3500.0, 0.6) * np.exp(-times / 0.11)
|
|
crack = Biquad("highpass").process(rng.standard_normal(length), 1200.0) * np.exp(-times / 0.02)
|
|
return 0.5 * tone + 0.9 * rattle + 0.4 * crack
|
|
|
|
|
|
def tom(rng, pitch):
|
|
length = int(sampleRate * 0.35)
|
|
times = np.arange(length) / sampleRate
|
|
frequency = pitch * (1.0 + 0.5 * np.exp(-times / 0.02))
|
|
body = np.sin(2.0 * math.pi * np.cumsum(frequency) / sampleRate) * np.exp(-times / 0.14)
|
|
stick = Biquad("highpass").process(rng.standard_normal(length), 2000.0) * np.exp(-times / 0.006)
|
|
return 0.8 * body + 0.15 * stick
|
|
|
|
|
|
def metal_ring(rng, seconds, partials, decaySeconds, noiseLevel, highpass):
|
|
"""A struck metal plate: inharmonic partials plus a wash of bright noise."""
|
|
length = int(sampleRate * seconds)
|
|
times = np.arange(length) / sampleRate
|
|
ring = np.zeros(length)
|
|
for frequency in partials:
|
|
ring += np.sin(2.0 * math.pi * frequency * times + rng.random() * 2.0 * math.pi)
|
|
ring /= len(partials)
|
|
wash = Biquad("highpass").process(rng.standard_normal(length), highpass)
|
|
return (0.5 * ring + noiseLevel * wash) * np.minimum(1.0, times / 0.001) * np.exp(-times / decaySeconds)
|
|
|
|
|
|
def crash(rng):
|
|
partials = [rng.uniform(3000.0, 9000.0) for _ in range(24)]
|
|
return Biquad("highpass").process(metal_ring(rng, 2.2, partials, 0.7, 1.0, 4500.0), 2500.0)
|
|
|
|
|
|
def ride(rng):
|
|
partials = [610.0, 1350.0, 2230.0, 3170.0, 4440.0, 5860.0]
|
|
return 0.6 * metal_ring(rng, 0.9, partials, 0.35, 0.25, 6000.0)
|
|
|
|
|
|
def hihat(rng):
|
|
return metal_ring(rng, 0.08, [rng.uniform(6000.0, 11000.0) for _ in range(8)], 0.02, 1.0, 7000.0)
|
|
|
|
|
|
def finish_mix(left, right, centre):
|
|
"""Stereo from the side and centre tracks, glued together the way a bus compressor would."""
|
|
stereo = np.column_stack((left + 0.7071 * centre, right + 0.7071 * centre))
|
|
peak = np.abs(stereo).max()
|
|
return 0.6 * np.tanh(1.4 * stereo / peak) / math.tanh(1.4)
|