Generated music: battle_music and moonlight_metal

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>
This commit is contained in:
Storm DragonandClaude Opus 5.5 committed 2026-11-28 20:33:35 -05:00
1 parent 00db02b236
commit d598818fb1
11 files changed
+581 -2

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
"""Moonlight metal: Beethoven's Moonlight Sonata, sped up and made heavy, in C sharp minor.
The rolling triplet arpeggios over deep octaves and the opening chords come
from the sonata; the lead melody and the drums are new.
"""
import numpy as np
from ...synth import pan_gains, place_looped, rng_from, sampleRate
from ..instruments import (bass_note, crash, finish_mix, guitar_note, hihat, kick, lead_note, power_chord, ride,
snare, tom)
# Moonlight metal is counted in triplets: twelve steps a bar, two chords a bar.
# Each chord is (bass note, rhythm guitar root, the three notes of the arpeggio).
moonlightChords = {
"C#m": (37, 37, (56, 61, 64)), "C#m/B": (35, 35, (56, 61, 64)), "A": (33, 33, (57, 61, 64)),
"D/F#": (42, 38, (57, 62, 66)), "G#7": (32, 44, (56, 60, 66)), "E": (40, 40, (56, 59, 64)),
"B/D#": (39, 35, (54, 59, 63)), "F#m": (42, 42, (57, 61, 66)), "D": (38, 38, (57, 62, 66)),
"C#m/G#": (44, 37, (56, 61, 64)),
}
moonlightHarmony = [
("C#m", "C#m"), ("C#m/B", "C#m/B"), ("A", "D/F#"), ("G#7", "G#7"),
("C#m", "C#m"), ("C#m/B", "C#m/B"), ("A", "D/F#"), ("G#7", "G#7"),
("E", "E"), ("B/D#", "B/D#"), ("C#m", "A"), ("F#m", "G#7"),
("A", "A"), ("F#m", "D"), ("C#m/G#", "G#7"), ("C#m", "G#7"),
]
# The lead rests through the four opening bars, then sings over the rest. None is a rest.
moonlightLead = [
[(None, 12)], [(None, 12)], [(None, 12)], [(None, 12)],
[(None, 6), (68, 2), (68, 1), (68, 3)],
[(68, 6), (68, 2), (69, 1), (68, 3)],
[(69, 3), (68, 3), (66, 3), (69, 3)],
[(68, 6), (72, 3), (75, 3)],
[(76, 6), (76, 2), (76, 1), (75, 3)],
[(75, 6), (73, 2), (75, 1), (71, 3)],
[(73, 6), (76, 3), (73, 3)],
[(78, 6), (75, 3), (72, 3)],
[(73, 3), (76, 3), (81, 6)],
[(80, 3), (78, 3), (81, 3), (78, 3)],
[(76, 3), (73, 3), (75, 3), (72, 3)],
[(73, 9), (None, 3)],
]
# The opening G#7 bar moves its arpeggio under the held bass, one shape a beat.
openingDominant = [(56, 60, 66), (56, 61, 64), (56, 61, 63), (54, 60, 63)]
def moonlight_drum_hits(bar):
"""{drum name: triplet steps it hits} for one bar of moonlight metal."""
if bar < 4:
# Half time to open: kick and snare a bar apart in feel, the ride counting triplets.
hits = {"kick": [0, 8], "snare": [6], "ride": list(range(12))}
else:
hits = {"kick": list(range(12)), "snare": [3, 9], "hihat": [0, 3, 6, 9], "ride": [0, 6]}
if bar % 4 == 3:
for drum in hits:
hits[drum] = [step for step in hits[drum] if step < 9]
hits["snare"] = hits["snare"] + [9]
hits["tom_high"], hits["tom_mid"], hits["tom_low"] = [10], [11], []
if bar == 3:
hits["tom_low"] = [11]
hits["tom_mid"] = []
return hits
def moonlight_metal(seed=0, tempo=140.0):
"""A sonata's moonlit triplets made heavy: C sharp minor metal, sixteen bars that loop seamlessly, in stereo.
The rolling triplet arpeggios over deep octaves and the opening chords
come from Beethoven's Moonlight Sonata, sped up and distorted; the lead
melody over them is new, sung in the sonata's long-short repeated notes.
At the default tempo of 140 beats a minute the loop is about 27 seconds long.
"""
rng = rng_from(seed)
triplet = 60.0 / tempo / 3.0
barCount = len(moonlightHarmony)
length = int(round(barCount * 12 * triplet * sampleRate))
left, right, centre = np.zeros(length), np.zeros(length), np.zeros(length)
def at(bar, step):
return int(round((bar * 12 + step) * triplet * sampleRate))
# Lead guitar in the centre, with a quiet echo to each side.
leadTrack = np.zeros(length)
for bar, notes in enumerate(moonlightLead):
step = 0
for midiNote, count in notes:
if midiNote is not None:
place_looped(leadTrack, lead_note(midiNote, count * triplet, rng), at(bar, step))
step += count
centre += 0.29 * leadTrack
echo = int(round(3 * triplet * sampleRate))
left += 0.085 * np.roll(leadTrack, echo)
right += 0.085 * np.roll(leadTrack, 2 * echo)
# Left guitar: the triplet arpeggios, every note picked. Right guitar: power chords,
# ringing through the opening and galloping after it. The bass plays deep octaves.
arpeggioTrack, chordTrack, bassTrack = np.zeros(length), np.zeros(length), np.zeros(length)
gallop = [(0, 2, False), (2, 1, True), (3, 2, True), (5, 1, True)]
for bar, chords in enumerate(moonlightHarmony):
for half, chord in enumerate(chords):
bassNote, guitarRoot, shape = moonlightChords[chord]
for beat in range(2):
if bar == 3:
shape = openingDominant[half * 2 + beat]
for index, midiNote in enumerate(shape):
step = half * 6 + beat * 3 + index
place_looped(arpeggioTrack, guitar_note([midiNote], 1.6 * triplet, rng, detune=5.0, drive=6.0),
at(bar, step))
if bar < 4:
place_looped(chordTrack, guitar_note(power_chord(guitarRoot), 6 * triplet, rng, detune=8.0),
at(bar, half * 6))
bass = bass_note(bassNote, 6 * triplet, rng)
place_looped(bassTrack, bass + bass_note(bassNote + 12, 6 * triplet, rng), at(bar, half * 6))
continue
for step, count, muted in gallop:
start = at(bar, half * 6 + step)
place_looped(chordTrack, guitar_note(power_chord(guitarRoot), count * triplet, rng, muted, 8.0),
start)
place_looped(bassTrack, bass_note(bassNote, count * triplet, rng, muted), start)
left += 0.2 * arpeggioTrack + 0.06 * chordTrack
right += 0.22 * chordTrack + 0.06 * arpeggioTrack
centre += 0.3 * bassTrack
drums = {
"kick": (kick, 0.0, 0.55), "snare": (snare, 0.0, 0.5), "hihat": (hihat, -0.4, 0.18),
"ride": (ride, 0.45, 0.2), "tom_high": (lambda r: tom(r, 220.0), -0.5, 0.5),
"tom_mid": (lambda r: tom(r, 160.0), 0.0, 0.5), "tom_low": (lambda r: tom(r, 110.0), 0.5, 0.5),
}
for bar in range(barCount):
for drum, steps in moonlight_drum_hits(bar).items():
make, pan, level = drums[drum]
leftGain, rightGain = pan_gains(pan)
for step in steps:
gain = level * (0.75 if drum == "kick" and bar >= 4 and step % 3 else 1.0)
sound = make(rng)
place_looped(left, sound, at(bar, step), gain * leftGain)
place_looped(right, sound, at(bar, step), gain * rightGain)
if bar in (0, 4, 8, 12):
for pan in (-0.6, 0.6):
leftGain, rightGain = pan_gains(pan)
sound = crash(rng)
place_looped(left, sound, at(bar, 0), 0.22 * leftGain)
place_looped(right, sound, at(bar, 0), 0.22 * rightGain)
return finish_mix(left, right, centre)