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>
196 lines
8.7 KiB
Python
196 lines
8.7 KiB
Python
"""Battle music: heavy metal in D minor, in the manner of a Bach keyboard concerto.
|
|
|
|
An original piece: a unison opening riff, running arpeggios through a circle
|
|
of fifths, a long build over a held A, and the riff again to close, for
|
|
distorted guitars, bass and drums.
|
|
"""
|
|
|
|
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)
|
|
|
|
# Notes are (MIDI note, length in sixteenths); every bar holds sixteen.
|
|
openingRiff = [
|
|
[(62, 2), (57, 2), (62, 2), (65, 2), (69, 2), (65, 1), (62, 1), (57, 2), (61, 2)],
|
|
[(62, 1), (64, 1), (65, 1), (67, 1), (69, 1), (70, 1), (69, 1), (67, 1),
|
|
(65, 1), (67, 1), (65, 1), (64, 1), (62, 1), (64, 1), (61, 1), (57, 1)],
|
|
[(67, 1), (62, 1), (70, 1), (62, 1), (69, 1), (62, 1), (67, 1), (62, 1),
|
|
(65, 1), (62, 1), (69, 1), (62, 1), (67, 1), (62, 1), (65, 1), (62, 1)],
|
|
[(64, 1), (61, 1), (64, 1), (67, 1), (70, 1), (69, 1), (67, 1), (65, 1), (64, 2), (57, 2), (61, 2), (64, 2)],
|
|
]
|
|
|
|
closingCadence = [
|
|
[(70, 1), (69, 1), (67, 1), (65, 1), (64, 1), (65, 1), (67, 1), (64, 1), (61, 2), (64, 2), (69, 2), (67, 2)],
|
|
[(62, 4), (65, 2), (62, 2), (57, 2), (61, 2), (64, 2), (61, 2)],
|
|
]
|
|
|
|
# Chord tones for the running arpeggios, as close voicings around the lead's register.
|
|
chordTones = {
|
|
"Dm": (62, 65, 69, 74), "Gm": (62, 67, 70, 74), "C": (64, 67, 72, 76), "F": (65, 69, 72, 77),
|
|
"Bb": (62, 65, 70, 74), "A": (61, 64, 69, 73),
|
|
}
|
|
|
|
# The lowest note of each chord for the rhythm guitars, which play it as a power chord.
|
|
chordRoots = {"Dm": 38, "Gm": 43, "C": 36, "F": 41, "Bb": 46, "A": 45}
|
|
|
|
|
|
def arpeggio(chord, falling):
|
|
"""Eight sixteenths running up and down a chord, or down and up."""
|
|
low, second, third, high = chordTones[chord]
|
|
pattern = (high, third, second, third, low, second, third, second) if falling else \
|
|
(low, second, third, high, third, second, third, second)
|
|
return [(note, 1) for note in pattern]
|
|
|
|
|
|
def over_pedal(melody, pedal=69):
|
|
"""Each melody note followed by the pedal note, as sixteenths."""
|
|
notes = []
|
|
for note in melody:
|
|
notes += [(note, 1), (pedal, 1)]
|
|
return notes
|
|
|
|
|
|
def lead_part():
|
|
"""The lead line, bar by bar: sixteen bars."""
|
|
episode = []
|
|
for index, chord in enumerate(("Dm", "Gm", "C", "F", "Bb", "Gm", "A")):
|
|
episode += arpeggio(chord, index % 2 == 1)
|
|
episode += [(69, 1), (67, 1), (65, 1), (64, 1), (62, 1), (61, 1), (64, 1), (57, 1)]
|
|
pedal = over_pedal([74, 73, 74, 76, 77, 76, 74, 73]) + over_pedal([74, 76, 77, 79, 81, 79, 77, 76])
|
|
build = arpeggio("Bb", False) + arpeggio("Gm", True)
|
|
run = [(note, 1) for note in (81, 79, 77, 76, 74, 73, 70, 69, 67, 65, 64, 62, 61, 64, 69, 73)]
|
|
bars = openingRiff + split_bars(episode) + split_bars(pedal + build + run) + openingRiff[:2] + closingCadence
|
|
return bars
|
|
|
|
|
|
def split_bars(notes):
|
|
"""Cut a run of notes into bars of sixteen sixteenths."""
|
|
bars, current, filled = [], [], 0
|
|
for note in notes:
|
|
current.append(note)
|
|
filled += note[1]
|
|
if filled == 16:
|
|
bars.append(current)
|
|
current, filled = [], 0
|
|
if current:
|
|
raise ValueError("The notes do not fill whole bars.")
|
|
return bars
|
|
|
|
|
|
# What the rhythm section does in each bar: "unison" doubles the lead's riff,
|
|
# otherwise two chords a bar, played as "chug" (eighths) or "gallop" (eighth, two sixteenths).
|
|
rhythmPlan = (
|
|
[("unison", None)] * 4
|
|
+ [("chug", ("Dm", "Gm")), ("chug", ("C", "F")), ("chug", ("Bb", "Gm")), ("chug", ("A", "A"))]
|
|
+ [("gallop", ("A", "A"))] * 2 + [("gallop", ("Bb", "Gm")), ("gallop", ("A", "A"))]
|
|
+ [("unison", None)] * 2 + [("chug", ("Gm", "A")), ("chug", ("Dm", "A"))]
|
|
)
|
|
|
|
# Drum patterns per bar, as which sixteenths each drum hits.
|
|
drumPlan = (
|
|
["riff"] * 3 + ["riff_fill"] + ["drive"] * 3 + ["drive_fill"] + ["gallop"] * 3 + ["gallop_fill"]
|
|
+ ["riff"] * 2 + ["drive", "drive_fill"]
|
|
)
|
|
crashBars = {0, 4, 6, 8, 10, 12, 14}
|
|
|
|
|
|
def drum_hits(pattern):
|
|
"""{drum name: sixteenths it hits} for one bar of a drum pattern."""
|
|
backbeat = [4, 12]
|
|
if pattern.startswith("riff"):
|
|
hits = {"kick": [0, 2, 6, 8, 10, 14], "snare": backbeat, "ride": list(range(0, 16, 2))}
|
|
elif pattern.startswith("drive"):
|
|
hits = {"kick": list(range(16)), "snare": backbeat, "hihat": list(range(0, 16, 2))}
|
|
else:
|
|
hits = {"kick": [0, 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15], "snare": backbeat, "ride": [0, 4, 8, 12]}
|
|
if pattern.endswith("fill"):
|
|
for drum in hits:
|
|
hits[drum] = [step for step in hits[drum] if step < 12]
|
|
hits["snare"] = hits["snare"] + [12, 13]
|
|
hits["tom_high"], hits["tom_mid"], hits["tom_low"] = [14], [15], []
|
|
hits["kick"] = hits["kick"] + [15]
|
|
return hits
|
|
|
|
|
|
def battle_music(seed=0, tempo=160.0):
|
|
"""Heavy metal battle music in D minor, sixteen bars that loop seamlessly, in stereo.
|
|
|
|
At the default tempo of 160 beats a minute the loop is 24 seconds long.
|
|
"""
|
|
rng = rng_from(seed)
|
|
sixteenth = 60.0 / tempo / 4.0
|
|
barCount = len(rhythmPlan)
|
|
length = int(round(barCount * 16 * sixteenth * sampleRate))
|
|
left, right, centre = np.zeros(length), np.zeros(length), np.zeros(length)
|
|
|
|
def at(bar, step):
|
|
return int(round((bar * 16 + step) * sixteenth * sampleRate))
|
|
|
|
# Lead guitar, in the centre with a quiet echo to each side.
|
|
leadTrack = np.zeros(length)
|
|
for bar, notes in enumerate(lead_part()):
|
|
step = 0
|
|
for midiNote, count in notes:
|
|
place_looped(leadTrack, lead_note(midiNote, count * sixteenth, rng), at(bar, step))
|
|
step += count
|
|
centre += 0.32 * leadTrack
|
|
echo = int(round(3 * sixteenth * sampleRate))
|
|
left += 0.1 * np.roll(leadTrack, echo)
|
|
right += 0.1 * np.roll(leadTrack, 2 * echo)
|
|
|
|
# Two rhythm guitars hard left and right, each played a little differently, and the bass.
|
|
bassTrack = np.zeros(length)
|
|
for side, detune in ((left, 6.0), (right, 9.0)):
|
|
guitarTrack = np.zeros(length)
|
|
for bar, (style, chords) in enumerate(rhythmPlan):
|
|
if style == "unison":
|
|
step = 0
|
|
for midiNote, count in openingRiff[bar % len(openingRiff)]:
|
|
hit = guitar_note([midiNote - 12, midiNote], count * sixteenth, rng, detune=detune)
|
|
place_looped(guitarTrack, hit, at(bar, step))
|
|
if side is left:
|
|
place_looped(bassTrack, bass_note(midiNote - 24, count * sixteenth, rng), at(bar, step))
|
|
step += count
|
|
continue
|
|
hits = [(0, 2, False), (2, 2, True), (4, 2, True), (6, 2, True)] if style == "chug" else \
|
|
[(0, 2, False), (2, 1, True), (3, 1, True), (4, 2, True), (6, 1, True), (7, 1, True)]
|
|
for half, chord in enumerate(chords):
|
|
root = chordRoots[chord]
|
|
for step, count, muted in hits:
|
|
start = at(bar, half * 8 + step)
|
|
place_looped(guitarTrack, guitar_note(power_chord(root), count * sixteenth, rng, muted, detune),
|
|
start)
|
|
if side is left:
|
|
place_looped(bassTrack, bass_note(root - 12, count * sixteenth, rng, muted), start)
|
|
side += 0.22 * guitarTrack
|
|
centre += 0.35 * bassTrack
|
|
|
|
# Drums, from the drummer's seat: hi-hat a little left, ride a little right, toms across.
|
|
drums = {
|
|
"kick": (kick, 0.0, 0.55), "snare": (snare, 0.0, 0.5), "hihat": (hihat, -0.4, 0.18),
|
|
"ride": (ride, 0.45, 0.22), "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, pattern in enumerate(drumPlan):
|
|
for drum, steps in drum_hits(pattern).items():
|
|
make, pan, level = drums[drum]
|
|
leftGain, rightGain = pan_gains(pan)
|
|
for step in steps:
|
|
if drum == "kick" and pattern.startswith("drive"):
|
|
gain = level * (1.0 if step % 4 == 0 else 0.75)
|
|
else:
|
|
gain = level
|
|
sound = make(rng)
|
|
place_looped(left, sound, at(bar, step), gain * leftGain)
|
|
place_looped(right, sound, at(bar, step), gain * rightGain)
|
|
if bar in crashBars:
|
|
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)
|