<!-- Triggering is decided entirely by the `description` field above (ClawHub
and Anthropic both work this way); there is deliberately no separate
"trigger" section in the body. Python deps: numpy, scipy, mido. -->
<h2>Light game BGM composer</h2>
<p>Produce original, nostalgic, loopable instrumental music and render it to an
actual audio file the user can play. This skill captures a pipeline that was
tuned on a real cozy 2D-game town theme, including the mistakes that taught
each lesson.</p>
<h2>The core insight</h2>
<p>Two things, in order, decide whether the music sounds real:</p>
<ol>
<li><strong>Use sampled instruments, not hand-built oscillators.</strong> From-scratch
synthesis can only ever sound like a synth. Write the piece as <strong>MIDI</strong> and
render it through <strong>FluidSynth + a soundfont</strong> so every note plays a recorded
instrument sample.</li>
<li><strong>Perform the MIDI; don't just place notes.</strong> A correct-but-static MIDI
played through a great soundfont still sounds like the stiff, stock
General-MIDI cliché. The realism comes from <em>how the notes are played</em> —
dynamics, timing, articulation. This is the part people underestimate.</li>
</ol>
<p>There is a hard ceiling: truly recorded-section realism needs dedicated VST
libraries in a DAW, which the CLI can't load. Be upfront about this and offer
the MIDI export as the bridge. See <code>references/soundfonts.md</code> (realism ladder).</p>
<h2>Environment check</h2>
<pre><code class="language-bash">python3 -c "import numpy, scipy" && echo ok # synthesis + reverb math
python3 -c "import mido" || pip install mido # MIDI authoring
which fluidsynth || brew install fluid-synth # soundfont renderer
which ffmpeg # wav -> mp3
</code></pre>
<p>Then fetch a soundfont (see <code>references/soundfonts.md</code>). <strong>Default to
GeneralUser GS</strong> for light/cozy pieces — it's pre-balanced and blends; the
bigger FluidR3_GM is brighter and more forward (good for leads, can over-expose
strings). Bigger is not better.</p>
<h2>Workflow</h2>
<h3>1. Decide the musical brief</h3>
<p>Key (major = bright/cozy), tempo (~100–120 BPM for town themes), instrument
roster, and form. A satisfying loop form is <strong>A → A′ (variation) → B (contrast)
→ A″ (return)</strong>, ~32 bars ≈ 70 s at 110 BPM.</p>
<h3>2. Write the composition as a per-song Python script</h3>
<p>Import <code>scripts/midi_helpers.py</code> and build one <code>NoteBuilder</code> per voice. Keep the
note data (melody, chords, counter-lines) in the song script — it changes every
time — and lean on the helpers for the reusable expression machinery. See the
<strong>template</strong> below.</p>
<h3>3. Apply the expression layer (this is what kills the fake feeling)</h3>
<ul>
<li><strong>Humanize timing</strong> — nudge note starts a few ticks (<code>hum=</code>). Perfectly
quantized = robotic.</li>
<li><strong>Velocity variation</strong> (<code>vvar=</code>) — no two notes identical.</li>
<li><strong>CC11 swells</strong> (<code>.swell(...)</code>) — phrases breathe louder/softer. The single
biggest fix for stiff string pads.</li>
<li><strong>Legato / held common tones</strong> (<code>held_runs(...)</code>) — sustain a chord tone
across bar lines instead of re-attacking it every bar. Block chords that
re-hit every downbeat are <em>the</em> stock-library giveaway.</li>
<li><strong>Slow-attack patch for pads</strong> (Slow Strings, prog 49) — mimics bowing in.</li>
<li><strong>A little vibrato</strong> (<code>vib=</code>, CC1) on strings.</li>
<li><strong>Pan voices</strong> (CC10) — e.g. cello left, violin right, so a two-part string
<strong>dialogue</strong> (call-and-response) reads spatially. Giving strings their own
conversing lines beats parking them on block-chord pads.</li>
</ul>
<h3>4. Render dry</h3>
<pre><code class="language-bash">fluidsynth -ni -R 0 -C 0 -g 0.8 -r 44100 -F song_dry.wav GeneralUser.sf2 song.mid
</code></pre>
<p>Reverb/chorus OFF — the next step owns the space.</p>
<h3>5. Concert-hall reverb + seamless loop</h3>
<pre><code class="language-bash">python3 scripts/hall_reverb_loop.py --input song_dry.wav --output song_loop.wav
--bpm 110 --beats 128 --decay 2.1 --x2
</code></pre>
<p><code>--beats</code> = bars × beats/bar (the loop body length). The script convolves a
synthesised hall impulse response and, for a loop, <strong>wraps the post-loop tail
(note releases + reverb) back onto the start</strong> so it repeats with no click and
no fade. <code>--decay</code> sets room size (1.2 room · 2.1 hall · 3.5 cathedral).
<code>--x2</code> writes a two-loop file to audition the seam.</p>
<h3>6. Verify, then encode</h3>
<pre><code class="language-bash">python3 scripts/verify_loop.py --input song_loop_x2.wav --bpm 110 --beats 128
ffmpeg -y -i song_loop.wav -af "loudnorm=I=-15:TP=-1.5" -b:a 192k song_loop.mp3
</code></pre>
<p>Don't ship a loop you haven't verified — <code>verify_loop.py</code> confirms the seam jump
is inaudible (jump/rms well under 0.06) and nothing clips.</p>
<h3>7. Deliver</h3>
<p>Give the user the <strong>mp3</strong> (plays everywhere), and offer the <strong>.mid</strong> (for a DAW)
and the per-song <strong>.py</strong> (to tweak the composition). Present concrete next-step
options (longer bridge, wider panning, bigger/smaller hall, different lead
instrument) rather than asking open-ended questions.</p>
<h2>Per-song script template</h2>
<pre><code class="language-python">import sys; sys.path.insert(0, 'scripts')
from midi_helpers import NoteBuilder, new_midi, held_runs, realize, midi
BPM, TPB = 110, 480
mid = new_midi(BPM, TPB)
--- melody (music box, centered) ---
lead = NoteBuilder(0, 10, TPB, pan=64, rev=55, vol=104, seed=1)
lead.line([(0,'A4',1),(1,'F#4',1),(2,'D4',1.5),(3.5,'E4',.5)], vel=90, gate=0.9, hum=8)
# ... more phrases ...
--- strings as a dialogue: cello left, violin right ---
cello = NoteBuilder(4, 42, TPB, pan=36, vib=30, rev=68, vol=92, seed=2)
violin = NoteBuilder(1, 40, TPB, pan=92, vib=38, rev=70, vol=84, seed=3)
cello.line([(64,'G3',1),(65,'A3',1),(66,'B3',2)], vel=66, gate=0.97) # call
violin.line([(72,'B4',1),(73,'C#5',1),(74,'D5',2)], vel=62, gate=0.96) # answer
cello.swell(128, base=76, amp=28); violin.swell(128, base=72, amp=30)
--- soft pad via held common tones (legato, no re-attacks) ---
voicing = [['D3','F#3','A3'], ['C#3','E3','A3'], ...] # one per bar
pad = NoteBuilder(5, 49, TPB, pan=64, vib=18, rev=90, vol=50, seed=4)
for start, pitch, dur in held_runs(voicing):
pad.note(start, pitch, dur, vel=30, gate=0.99, hum=20)
pad.swell(128, base=55, amp=22)
for nb in (lead, violin, cello, pad):
mid.tracks.append(nb.track())
mid.save('song.mid')
</code></pre>
<p>(A complete, working realisation of this exact arrangement — a full
cello/violin-dialogue town theme with the four-section loop form — was built in
the parent project; look for the per-song MIDI builder and post-processing
script there as a reference implementation if present.)</p>
<h2>Bundled resources</h2>
<ul>
<li><code>scripts/midi_helpers.py</code> — <code>NoteBuilder</code> (humanize, velocity, CC11 swells,
panning), <code>held_runs</code> (legato pads), <code>realize</code> (arps), <code>midi</code>/<code>new_midi</code>.</li>
<li><code>scripts/hall_reverb_loop.py</code> — convolution hall reverb + seamless-loop wrap.</li>
<li><code>scripts/verify_loop.py</code> — objective seam/clipping check.</li>
<li><code>references/soundfonts.md</code> — realism ladder, GeneralUser GS vs FluidR3,
download + validation, GM program numbers, CC reference.</li>
</ul>
<h2>Fallback: pure synthesis (no soundfont)</h2>
<p>If FluidSynth/soundfonts are unavailable, you can still synthesise from
oscillators with numpy (sine/triangle + ADSR + simple reverb) and write a WAV
directly. Accept that it will sound chiptune/synthetic — set that expectation
with the user rather than presenting it as realistic.</p>




