dubdutduc

how a block hash becomes MIDI

A Bitcoin block hash is 32 bytes of proof-of-work noise. dubdutduc does not map those bytes onto pitches. It turns the hash into a deterministic random stream, then walks a fixed composition pipeline — and every fork in that pipeline asks the stream for the next number.

01 // the hash

How the hash is used

Think of the hash as a playlist seed, not as music data. The composer never reads “byte 3 → MIDI note 60”. Instead:

  1. Take the block hash (lowercase hex). If it is exactly 32 bytes of hex, those 32 bytes are the seed. Otherwise hash the string with SHA-256 and use that.
  2. Feed the 32-byte seed into a ChaCha8 pseudorandom number generator. From here on, the hash itself is never looked at again — only this RNG stream.
  3. Run a fixed recipe (key → form → melody → harmony → bass → drums). Whenever the recipe must choose among prepared options (which mode? which chord row? which rhythm? which pitch among legal candidates?), it draws the next integer or float from the RNG.
block hash
    |
    v
 32-byte seed  --->  ChaCha8 RNG  --->  stream of numbers
                                              |
                                              v
                         choose(mode), choose(progression),
                         choose(rhythm), pick(pitch), ...
                                              |
                                              v
                                    MIDI notes (deterministic)

Same hash → same RNG stream → same choices → same MIDI. Change one bit of the hash → a different stream → different picks at every fork, so the piece changes, while it still obeys the same musical rules.

Weighted choices (e.g. Ionian more often than Lydian) roll IntN(totalWeight) and walk the weight table. Uniform lists (tempos, ensembles, pitch candidates) use xs[IntN(len(xs))]. Probabilistic rules (e.g. “70% chord tone on the strong beat”) roll a float in [0,1).

02 // draw order

What the RNG decides, in order

The stream is consumed left-to-right. Early draws set the piece’s identity; later draws fill in notes. Approximate order:

StepDrawn from the hash-seeded RNG
1. KeyRoot (0–11), then weighted mode
2. GrooveMeter (4/4 or 3/4), BPM from a fixed list, chip ensemble
3. Harmony planOne open / middle / cadence chord-degree row for that mode
4. Melody rhythmsBody patterns a,b / x,y and phrase endings
5. Melody pitchesAmong legal scale/chord tones under contour rules; floats for “which rule branch”
6. CompingPulse style, or arp direction / step size
7. BassWhich root/fifth pattern for the meter
8. DrumsGroove + fill patterns; small velocity jitter per hit
ThroughoutVelocity accents get a tiny ± jitter from the same stream

Everything not listed — 16-bar form, allowed progressions, scale grammar, MIDI registers — is hard-coded. The hash only steers which legal option wins.

03 // globals

Global parameters

ParameterHow the RNG picks it
RootIntN(12) — uniform pitch class
ModeAll 7 modes, weighted: Ionian/Dorian/Mixolydian/Aeolian 16, Phrygian 14, Lydian 12, Locrian 10
MeterWeighted: 4/4 (3) or 3/4 (1)
BPMUniform from {100, 112, 124, 136, 148, 160, 172, 184, 196, 208}
EnsembleUniform chip lead / chords / bass set (drums always on)

Timing uses PPQ = 480 ticks per quarter. One sixteenth = 120 ticks. A bar is beats × 480 ticks.

04 // form

Form — always 16 bars

Structure is fixed. The hash only picks which progression rows fill it.

bars  1–4   phrase A   open progression   <-- RNG picks one open row
bars  5–8   phrase A   same chords; melody mostly restated
bars  9–12  phrase B   middle / contrast  <-- RNG picks one middle row
bars 13–16  cadence    ends on tonic       <-- RNG picks one cadence row

Degrees are 0-based scale degrees — e.g. Ionian {0, 4, 5, 3} → I–V–vi–IV. If a bar is IV and the next is I, and the IV seventh is dominant in that key, it becomes a dominant 7th (secondary-dominant color into I). That rule is fixed, not random.

05 // voicing

Chord voicing

Voicings are not random. Each bar picks the close-position layout in MIDI 47–64 that best matches the previous bar (least motion, near center 55). The hash already chose the chord; this step only finds its register.

06 // melody

Melody

Rhythm (RNG picks patterns)

From meter-specific catalogs, the stream picks body patterns and endings:

BarsPattern
1–4a, b, a, end — a/b/end drawn from weighted lists
5–8reuse a, b, a; new end; bars 5–7 copy melody pitches from 1–3 (no new pitch draws)
9–12new x, y, x, end
13–16a, b, y, final (final = whole-bar hold, fixed)

Pitch (RNG picks among legal tones)

For each attack, hard rules first narrow the candidates (scale tones, chord tones, leap resolution, approach into the next bar, final tonic). Only then does the RNG break ties or take a probabilistic branch:

WhenRule (RNG role)
Last note of the pieceTonic — deterministic among nearest tonics
DownbeatChord tone; 40% roll asks to avoid repeating previous pitch
Long note (≥ 6 sixteenths)Chord tone, avoid repeat; pick among best-cost tones
Strong beat (beat 3 in 4/4)70% roll → chord tone, else walk
Last note filling a non-final barApproach tone into next chord (side may be random if tied)
OtherwiseWalk: float picks step / repeat / skip / leap among scale indices

Non-final notes are shortened to 85% of the slot. Velocity uses a fixed phrase swell plus a small hash-driven jitter.

07 // harmony

Harmony track

  • chip-arp — RNG: up (70%) vs down, step 2 vs 1 (35%). Notes are the bar’s voicing tones in order. Final bar holds the root.
  • chip-chord — RNG picks a pulsed rhythm; every hit plays all voicing pitches. Length ≈ 70% of the slot (full on the final bar).
08 // bass

Bass

RNG picks a meter-specific root/fifth/rest pattern. Pitch placement in MIDI 34–48 (nearest previous root) is deterministic. Final bar = one root for the whole bar. Non-final notes shortened to 92%.

09 // drums

Drums

General MIDI channel 10 pitches are fixed:

kick 36   snare 38   closed hat 42   open hat 46
  • RNG picks a body groove for the meter
  • RNG picks a fill for every 4th bar (not the last)
  • Final bar uses a fixed ending figure
  • Each hit gets velocity ±2 from the stream
10 // midi

MIDI file

After composition, notes are encoded as a format-1 Standard MIDI File (no further hash draws): conductor track with title Block <hash>, tempo and meter; then melody, harmony, bass, and drums on channel 10.

11 // summary

In one sentence

The block hash seeds a RNG; the composer asks that RNG at every choice point inside a fixed rulebook; the result is a unique but musically legal 16-bar chiptune for that block.