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:
1 parent
00db02b236
commit
d598818fb1
11 files changed
+581
-2
No files matched your search
@@ -0,0 +1,41 @@
|
||||
"""Music: looping pieces for a game's background.
|
||||
|
||||
from libstormgames import sfx
|
||||
from libstormgames.sfx import music
|
||||
|
||||
sg.play_bgm(sfx.make_music(music.battle_music()))
|
||||
|
||||
The pieces:
|
||||
|
||||
- battle_music: original heavy metal in D minor in the manner of a Bach
|
||||
keyboard concerto, a 24-second loop.
|
||||
- moonlight_metal: Beethoven's Moonlight Sonata sped up and made heavy, in
|
||||
C sharp minor, a 27-second loop.
|
||||
|
||||
Each piece takes a seed like the other generators, which changes only the
|
||||
noise in the drums and the small differences between the two rhythm guitars,
|
||||
never the notes, and a tempo in beats a minute.
|
||||
|
||||
Each piece lives in its own module in music.pieces, named after its function,
|
||||
and is loaded the first time it is asked for. To add a piece, add a module
|
||||
there with a function of the same name. The instruments the pieces share are
|
||||
in music.instruments.
|
||||
"""
|
||||
|
||||
import importlib
|
||||
|
||||
|
||||
def __getattr__(name):
|
||||
"""Load a piece the first time it is asked for, from the module of the same name in pieces."""
|
||||
if name.startswith("_"):
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
moduleName = f"{__name__}.pieces.{name}"
|
||||
try:
|
||||
module = importlib.import_module(moduleName)
|
||||
except ModuleNotFoundError as error:
|
||||
if error.name != moduleName:
|
||||
raise
|
||||
raise AttributeError(f"module {__name__!r} has no attribute or piece {name!r}") from None
|
||||
piece = getattr(module, name)
|
||||
globals()[name] = piece
|
||||
return piece
|
||||
@@ -0,0 +1,140 @@
|
||||
"""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)
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Every piece of music, one module each, named after the function that makes it.
|
||||
|
||||
libstormgames.sfx.music loads a piece from here the first time a game asks
|
||||
for it, so a game only ever loads the pieces it plays.
|
||||
"""
|
||||
@@ -0,0 +1,195 @@
|
||||
"""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)
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user