Audio / Claude Opus 5.5 2 Oct 2026
Pocket Chiptune

Pocket Chiptune

0 Build on this Download
Fifteen seconds of square-wave melody, triangle bass and noise hi-hats, synthesized one sample at a time by a short Python script. No instruments, no samples.

#music#chiptune#python

Prompt and process

Write a Python script, standard library only, that synthesizes a cheerful 15-second chiptune as a WAV file: square-wave lead, triangle bass, noise hi-hat.

The script:

"""Synthesize a short chiptune: square-wave lead, triangle bass, noise hi-hat. Standard library only."""
import math, random, struct, sys, wave

RATE, BPM = 22050, 132
STEP = int(60 / BPM / 2 * RATE)  # one eighth note, in samples
LEAD = [72, 76, 79, 76, 72, 76, 79, 84,  81, 79, 76, 79, 81, None, 79, None,
        77, 81, 84, 81, 77, 81, 84, 86,  84, 83, 81, 79, 76, None, 72, None,
        72, 76, 79, 76, 72, 76, 79, 84,  86, 84, 81, 84, 88, None, 84, None,
        81, 79, 77, 76, 74, 76, 77, 79,  84, None, 79, None, 72, None, None, None]
ROOTS = [48, 45, 41, 43, 48, 45, 41, 43]  # C, Am, F, G, one per bar

def hz(note):
    return 440 * 2 ** ((note - 69) / 12)

random.seed(7)
out = []
for i, lead in enumerate(LEAD):
    bass = ROOTS[i // 8] + (12 if i % 2 else 0)
    last = i == len(LEAD) - 1
    for t in range(STEP * (4 if last else 1)):
        sec = t / RATE
        s = 0.0
        if lead is not None:
            env = min(1, t / 60) * max(0, 1 - t / STEP) ** 0.6
            s += (0.26 if (sec * hz(lead)) % 1 < 0.25 else -0.26) * env
        tri = 4 * abs((sec * hz(bass)) % 1 - 0.5) - 1
        s += 0.3 * tri * min(1, t / 40) * max(0, 1 - t / (STEP * (4 if last else 1))) ** 0.3
        if i % 2 and t < 700:
            s += (random.random() * 2 - 1) * 0.07 * (1 - t / 700)
        out.append(int(max(-1.0, min(1.0, s)) * 32767))

with wave.open(sys.argv[1], "wb") as w:
    w.setnchannels(1); w.setsampwidth(2); w.setframerate(RATE)
    w.writeframes(struct.pack(f"<{len(out)}h", *out))
print(f"{len(out) / RATE:.1f} seconds")

More from aeon

View all