PicoRubyKaigi 2026 Assembleオリジナル基板 「Adventure board」 Advent Calendar 2026の記事です。
大江戸Ruby会議11 できいたOmarchyの歌がずっと心に残りまして、「Adventure board」でチップチューンでハッピーな感じにアレンジして奏でています。
require 'adventure_board'
GC.start
matrix = AdventureBoard.matrix
speaker = AdventureBoard.speaker
buttons = AdventureBoard.buttons
speaker.reset
buttons.clear_presses
STEP_US = 60_000_000 / (115 * 4)
ROOTS = [
5, 7, 5, 7,
5, 0, 7, 2, 5, 0, 7, 2,
9, 0, 7, 7,
5, 0, 7, 2, 5, 0, 7, 2,
5, 5, 5, 5,
5, 0, 7, 2
]
QUALITY = [
0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2,
0, 0, 0, 0
]
TONES = [[0, 4, 7], [0, 3, 7], [0, 4, 7, 10]]
MELODY = [
-1, 0, -1, 0,
-1, -4, 0, -3, -1, -4, 0, -3,
-6, -4, 0, 0,
-1, -4, 0, -3, -1, -4, 0, -3,
-1, -1, -1, -1,
-1, -4, 0, -3
]
RHYTHM = [
[2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0],
[1, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
SCALE = [0, 2, 4, 5, 7, 9, 11]
BAND_TOP = [5, 12, 0]
BAND_BOTTOM = [10, 15, 3]
def play(speaker, notes, lefts, levels, xs, ch, note, velocity, length)
speaker.note_off(notes[ch], channel: ch) if notes[ch]
speaker.note_on(note, velocity: velocity, channel: ch)
notes[ch] = note
lefts[ch] = length
levels[ch] = 255
xs[ch] = (note % 12) * 4 / 3
end
def velocity_of(volume)
volume = 1 if volume < 1
(volume * 127 + 7) / 15
end
def silence(speaker, notes, levels)
ch = 0
while ch < 3
speaker.note_off(notes[ch], channel: ch) if notes[ch]
notes[ch] = nil
levels[ch] = 0
ch += 1
end
end
notes = [nil, nil, nil]
lefts = [0, 0, 0]
levels = [0, 0, 0]
xs = [0, 0, 0]
step = 0
playing = false
volume = 11
blink = 24
while true
started = Machine.uptime_us
name = buttons.take_press
while name
if name == :a
playing = !playing
silence(speaker, notes, levels)
step = 0
blink = 24
elsif name == :up
volume += 1 if volume < 15
elsif name == :down
volume -= 1 if 1 < volume
end
name = buttons.take_press
end
unless playing
blink += 1
if blink == 25
matrix.fill(1)
matrix.show
elsif 50 <= blink
matrix.clear
matrix.show
blink = 0
end
sleep_ms 20
next
end
bar = step / 16
pos = step % 16
root = ROOTS[bar]
tones = TONES[QUALITY[bar]]
intro = bar < 4
held = 24 <= bar && bar < 28
ch = 0
while ch < 3
if notes[ch]
lefts[ch] -= 1
if lefts[ch] <= 0
speaker.note_off(notes[ch], channel: ch)
notes[ch] = nil
end
end
ch += 1
end
length = RHYTHM[bar % 2][pos]
if 0 < length
degree = 11 + MELODY[bar]
play(speaker, notes, lefts, levels, xs, 0, 60 + SCALE[degree % 7] + 12 * (degree / 7), velocity_of(volume), length)
end
unless intro
if pos % 4 == 0
play(speaker, notes, lefts, levels, xs, 1, 36 + root, velocity_of(volume - 1), 1)
elsif pos % 4 == 2
play(speaker, notes, lefts, levels, xs, 1, 48 + root, velocity_of(volume - 2), 1)
elsif pos == 15 && !held
play(speaker, notes, lefts, levels, xs, 1, 36 + root + tones[1], velocity_of(volume - 2), 1)
end
if held || pos % 2 == 0
index = held ? step % 4 : (step / 2) % 4
interval = tones[index % tones.size] + 12 * (index / tones.size)
play(speaker, notes, lefts, levels, xs, 2, 72 + root + interval, velocity_of(volume - 4), held ? 1 : 2)
end
end
matrix.clear_drawing
ch = 0
while ch < 3
level = levels[ch]
if 0 < level
x = xs[ch]
matrix.draw_line(x, BAND_TOP[ch], x, BAND_BOTTOM[ch], level)
matrix.draw_line(x + 1, BAND_TOP[ch], x + 1, BAND_BOTTOM[ch], level)
level = level * 5 / 8
level = 0 if level < 8
levels[ch] = level
end
ch += 1
end
matrix.blit
matrix.show
step = (step + 1) % 512
elapsed = Machine.uptime_us - started
elapsed = 0 if elapsed < 0
sleep_ms((STEP_US - elapsed + 500) / 1000) if elapsed < STEP_US
end
