pixelsnake 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE +21 -0
- data/README.md +122 -0
- data/bin/pixelsnake +8 -0
- data/lib/pixelsnake/abacus.rb +102 -0
- data/lib/pixelsnake/app.rb +130 -0
- data/lib/pixelsnake/art/big_font.rb +196 -0
- data/lib/pixelsnake/art/heads.rb +68 -0
- data/lib/pixelsnake/art/small_font.rb +73 -0
- data/lib/pixelsnake/autopilot.rb +34 -0
- data/lib/pixelsnake/award.rb +67 -0
- data/lib/pixelsnake/board.rb +36 -0
- data/lib/pixelsnake/canvas.rb +189 -0
- data/lib/pixelsnake/choice.rb +92 -0
- data/lib/pixelsnake/clockwork.rb +169 -0
- data/lib/pixelsnake/controls.rb +26 -0
- data/lib/pixelsnake/countdown.rb +80 -0
- data/lib/pixelsnake/courtyard.rb +306 -0
- data/lib/pixelsnake/day.rb +23 -0
- data/lib/pixelsnake/death.rb +115 -0
- data/lib/pixelsnake/debug_overlay.rb +53 -0
- data/lib/pixelsnake/demo.rb +85 -0
- data/lib/pixelsnake/density.rb +47 -0
- data/lib/pixelsnake/ease.rb +27 -0
- data/lib/pixelsnake/fade.rb +30 -0
- data/lib/pixelsnake/feats.rb +86 -0
- data/lib/pixelsnake/fine.rb +78 -0
- data/lib/pixelsnake/fireflies.rb +63 -0
- data/lib/pixelsnake/floating_text.rb +36 -0
- data/lib/pixelsnake/font.rb +62 -0
- data/lib/pixelsnake/fruit.rb +149 -0
- data/lib/pixelsnake/game.rb +294 -0
- data/lib/pixelsnake/gear.rb +132 -0
- data/lib/pixelsnake/high_scores.rb +91 -0
- data/lib/pixelsnake/hud.rb +217 -0
- data/lib/pixelsnake/iris.rb +76 -0
- data/lib/pixelsnake/jukebox.rb +143 -0
- data/lib/pixelsnake/keyboard.rb +96 -0
- data/lib/pixelsnake/laurel.rb +76 -0
- data/lib/pixelsnake/leaf.rb +73 -0
- data/lib/pixelsnake/lettering.rb +111 -0
- data/lib/pixelsnake/medal.rb +29 -0
- data/lib/pixelsnake/medal_view.rb +428 -0
- data/lib/pixelsnake/menu.rb +213 -0
- data/lib/pixelsnake/meter.rb +108 -0
- data/lib/pixelsnake/metronome.rb +24 -0
- data/lib/pixelsnake/name.rb +22 -0
- data/lib/pixelsnake/name_field.rb +156 -0
- data/lib/pixelsnake/odometer.rb +76 -0
- data/lib/pixelsnake/palette.rb +64 -0
- data/lib/pixelsnake/panel.rb +45 -0
- data/lib/pixelsnake/particles.rb +188 -0
- data/lib/pixelsnake/piece.rb +82 -0
- data/lib/pixelsnake/playback.rb +22 -0
- data/lib/pixelsnake/preferences.rb +96 -0
- data/lib/pixelsnake/rare_fruit.rb +268 -0
- data/lib/pixelsnake/recording.rb +44 -0
- data/lib/pixelsnake/ribbon.rb +110 -0
- data/lib/pixelsnake/save_file.rb +51 -0
- data/lib/pixelsnake/scenes/game_over.rb +249 -0
- data/lib/pixelsnake/scenes/medal_case.rb +268 -0
- data/lib/pixelsnake/scenes/pause.rb +171 -0
- data/lib/pixelsnake/scenes/play.rb +326 -0
- data/lib/pixelsnake/scenes/scores.rb +420 -0
- data/lib/pixelsnake/scenes/settings.rb +344 -0
- data/lib/pixelsnake/scenes/size_notice.rb +38 -0
- data/lib/pixelsnake/scenes/statistics.rb +420 -0
- data/lib/pixelsnake/scenes/title.rb +152 -0
- data/lib/pixelsnake/screen.rb +163 -0
- data/lib/pixelsnake/shed.rb +40 -0
- data/lib/pixelsnake/skin.rb +36 -0
- data/lib/pixelsnake/snake.rb +53 -0
- data/lib/pixelsnake/snake_view.rb +393 -0
- data/lib/pixelsnake/sound/band.rb +354 -0
- data/lib/pixelsnake/sound/instruments.rb +143 -0
- data/lib/pixelsnake/sound/mixer.rb +60 -0
- data/lib/pixelsnake/sound/music.rb +15 -0
- data/lib/pixelsnake/sound/pulse.rb +149 -0
- data/lib/pixelsnake/sound/song.rb +22 -0
- data/lib/pixelsnake/sound/sounds.rb +225 -0
- data/lib/pixelsnake/sound/speaker.rb +199 -0
- data/lib/pixelsnake/sound/synth.rb +277 -0
- data/lib/pixelsnake/sound/tune.rb +145 -0
- data/lib/pixelsnake/sound/tunes/dusk.rb +154 -0
- data/lib/pixelsnake/sound/tunes/fireflies.rb +106 -0
- data/lib/pixelsnake/sound/tunes/jade.rb +100 -0
- data/lib/pixelsnake/sound/tunes/lanterns.rb +126 -0
- data/lib/pixelsnake/sound/tunes.rb +40 -0
- data/lib/pixelsnake/sprite.rb +58 -0
- data/lib/pixelsnake/stage.rb +95 -0
- data/lib/pixelsnake/stats.rb +131 -0
- data/lib/pixelsnake/terminal.rb +84 -0
- data/lib/pixelsnake/terrarium.rb +206 -0
- data/lib/pixelsnake/tile.rb +32 -0
- data/lib/pixelsnake/toolbox.rb +251 -0
- data/lib/pixelsnake/trophy.rb +99 -0
- data/lib/pixelsnake/tuft.rb +55 -0
- data/lib/pixelsnake/upscale.rb +60 -0
- data/lib/pixelsnake/version.rb +5 -0
- data/lib/pixelsnake.rb +143 -0
- metadata +146 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# The instruments every sound and every note of the music is played on,
|
|
5
|
+
# made from nothing when the sound starts up rather than loaded from
|
|
6
|
+
# files: struck things that ring with a few partials, plucked strings,
|
|
7
|
+
# noise, and the courtyard's echo. A sound is an array of samples at
|
|
8
|
+
# RATE, one channel, about -1 to 1.
|
|
9
|
+
module Synth
|
|
10
|
+
RATE = 32_000
|
|
11
|
+
TURN = Math::PI * 2
|
|
12
|
+
|
|
13
|
+
# How a struck thing rings: each partial as its ratio to the note, how
|
|
14
|
+
# loud it starts, and the seconds it takes to die away to a third.
|
|
15
|
+
# Wood is a marimba's bar, glass a glockenspiel's, a bell is warmer and
|
|
16
|
+
# rings longer, and a reed is a soft, hollow tone with odd harmonics.
|
|
17
|
+
WOOD = [[1.0, 1.0, 0.07], [3.93, 0.35, 0.018], [9.4, 0.12, 0.006]].freeze
|
|
18
|
+
GLASS = [[1.0, 1.0, 0.24], [2.76, 0.45, 0.1], [5.4, 0.2, 0.045], [8.93, 0.08, 0.02]].freeze
|
|
19
|
+
BELL = [[1.0, 1.0, 0.28], [2.0, 0.35, 0.15], [3.0, 0.12, 0.08], [4.2, 0.06, 0.04]].freeze
|
|
20
|
+
REED = [[1.0, 1.0, 0.12], [3.0, 0.3, 0.07], [5.0, 0.12, 0.04]].freeze
|
|
21
|
+
|
|
22
|
+
# A partial rings for four of its lives, and the last of that fades
|
|
23
|
+
# out, so it is never heard to stop.
|
|
24
|
+
LIVES = 4
|
|
25
|
+
ATTACK = 0.0015
|
|
26
|
+
# A bent pitch this close to its note is taken to have settled.
|
|
27
|
+
SETTLED = 0.001
|
|
28
|
+
# A finished sound ends where it falls this far under its loudest.
|
|
29
|
+
QUIET = 0.004
|
|
30
|
+
FADE = 0.02
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
def silence(seconds) = Array.new((seconds * RATE).round, 0.0)
|
|
35
|
+
|
|
36
|
+
# A note struck on an instrument, its partials ringing down together.
|
|
37
|
+
# A bend starts the pitch that share above the note, or below it when
|
|
38
|
+
# negative, and lets it settle onto the note: a falling thud, or a
|
|
39
|
+
# rising bloop. Damped makes everything die away that many times as
|
|
40
|
+
# fast, for a shorter, duller knock.
|
|
41
|
+
def strike(frequency, partials, bend: 0.0, settle: 0.02, damped: 1.0)
|
|
42
|
+
out = silence(partials.map { |_, _, decay| decay / damped }.max * LIVES)
|
|
43
|
+
partials.each do |ratio, loudness, decay|
|
|
44
|
+
pitch = frequency * ratio
|
|
45
|
+
next if pitch * (1 + [bend, 0.0].max) > RATE * 0.45
|
|
46
|
+
|
|
47
|
+
if bend.zero? then ring(out, pitch, loudness, decay / damped)
|
|
48
|
+
else glide(out, pitch, loudness, decay / damped, bend, settle)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
shape(out)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# A string plucked and left to ring, worked out the way a string moves:
|
|
55
|
+
# the shape the finger leaves it in goes round a loop as long as the
|
|
56
|
+
# note's period, losing a little each time, the highs most, until it is
|
|
57
|
+
# the note alone dying away. Where along the string it is plucked
|
|
58
|
+
# thins out the harmonics that would have a node there, brightness is
|
|
59
|
+
# how rough the pluck is, and the decay is the seconds the note takes
|
|
60
|
+
# to die away to a thousandth. Stretch is how much each turn of the
|
|
61
|
+
# loop smooths: a half for a low string, less for a high one, which
|
|
62
|
+
# would otherwise lose its brightness at once.
|
|
63
|
+
def pluck(frequency, rng, decay:, brightness: 0.5, position: 0.2, stretch: 0.5)
|
|
64
|
+
period = RATE / frequency - stretch
|
|
65
|
+
length = period.floor
|
|
66
|
+
fraction = period - length
|
|
67
|
+
length, fraction = length - 1, fraction + 1 if fraction < 0.1
|
|
68
|
+
tune = (1 - fraction) / (1 + fraction)
|
|
69
|
+
smoothed = Math.sqrt((1 - stretch)**2 + stretch**2 + 2 * (1 - stretch) * stretch * Math.cos(TURN * frequency / RATE))
|
|
70
|
+
loss = [10.0**(-3.0 / (frequency * decay)) / smoothed, 0.99999].min
|
|
71
|
+
string(pulled(length, position, brightness, rng), (decay * 0.8 * RATE).round, tune, loss, stretch)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The sound with its highs rolled off above a frequency, twice over.
|
|
75
|
+
def darken(samples, frequency)
|
|
76
|
+
pull = pass(frequency)
|
|
77
|
+
first = 0.0
|
|
78
|
+
second = 0.0
|
|
79
|
+
samples.map do |sample|
|
|
80
|
+
first += pull * (sample - first)
|
|
81
|
+
second += pull * (first - second)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The sound wavering in loudness, the way a vibraphone's does as its
|
|
86
|
+
# motor turns: the waver comes in after a moment and grows to its depth
|
|
87
|
+
# over as long again. The sine is turned on sample by sample, as a
|
|
88
|
+
# partial's is.
|
|
89
|
+
def waver(samples, speed:, depth:, after:)
|
|
90
|
+
out = samples.dup
|
|
91
|
+
step = TURN * speed / RATE
|
|
92
|
+
turn = 2 * Math.cos(step)
|
|
93
|
+
now = 0.0
|
|
94
|
+
before = -Math.sin(step)
|
|
95
|
+
start = (after * RATE).round
|
|
96
|
+
grow = start.nonzero? || 1
|
|
97
|
+
index = start
|
|
98
|
+
while index < out.size
|
|
99
|
+
reach = index - start < grow ? (index - start).fdiv(grow) : 1.0
|
|
100
|
+
out[index] *= 1 + depth * reach * now
|
|
101
|
+
now, before = turn * now - before, now
|
|
102
|
+
index += 1
|
|
103
|
+
end
|
|
104
|
+
out
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# A burst of noise with its lows and highs rolled off, dying away.
|
|
108
|
+
def noise(rng, decay:, low: 20.0, high: RATE / 2.0)
|
|
109
|
+
out = silence(decay * LIVES)
|
|
110
|
+
open = pass(high)
|
|
111
|
+
floor = pass(low)
|
|
112
|
+
fade = Math.exp(-1.0 / (decay * RATE))
|
|
113
|
+
smooth = 0.0
|
|
114
|
+
under = 0.0
|
|
115
|
+
level = 1.0
|
|
116
|
+
index = 0
|
|
117
|
+
while index < out.size
|
|
118
|
+
smooth += open * (rng.rand * 2 - 1 - smooth)
|
|
119
|
+
under += floor * (smooth - under)
|
|
120
|
+
out[index] = (smooth - under) * level
|
|
121
|
+
level *= fade
|
|
122
|
+
index += 1
|
|
123
|
+
end
|
|
124
|
+
shape(out)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Sounds laid over each other, each as [seconds from the start, samples]
|
|
128
|
+
# and optionally how loud to lay it.
|
|
129
|
+
def layer(*parts)
|
|
130
|
+
out = silence(parts.map { |at, samples| at + samples.size.fdiv(RATE) }.max)
|
|
131
|
+
parts.each do |at, samples, gain = 1.0|
|
|
132
|
+
start = (at * RATE).round
|
|
133
|
+
index = 0
|
|
134
|
+
while index < samples.size
|
|
135
|
+
out[start + index] += samples[index] * gain
|
|
136
|
+
index += 1
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
out
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# The sound swelling in over its first moments instead of striking.
|
|
143
|
+
def swell(samples, seconds)
|
|
144
|
+
out = samples.dup
|
|
145
|
+
rise = (seconds * RATE).round
|
|
146
|
+
[rise, out.size].min.times { |index| out[index] *= index.fdiv(rise) }
|
|
147
|
+
out
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The stone walls answering: each repeat quieter and duller than the
|
|
151
|
+
# one before it.
|
|
152
|
+
def echo(samples, delay: 0.11, feedback: 0.3, tone: 0.3)
|
|
153
|
+
gap = (delay * RATE).round
|
|
154
|
+
out = samples + silence(delay * LIVES)
|
|
155
|
+
memory = 0.0
|
|
156
|
+
index = gap
|
|
157
|
+
while index < out.size
|
|
158
|
+
memory += tone * (out[index - gap] - memory)
|
|
159
|
+
out[index] += memory * feedback
|
|
160
|
+
index += 1
|
|
161
|
+
end
|
|
162
|
+
out
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# The sound made as loud as asked at its loudest moment, and ended
|
|
166
|
+
# where it has died away, fading out the last of it.
|
|
167
|
+
def finish(samples, peak)
|
|
168
|
+
loudest = samples.minmax.map(&:abs).max
|
|
169
|
+
gain = peak / loudest
|
|
170
|
+
last = samples.rindex { |sample| sample.abs > loudest * QUIET }
|
|
171
|
+
out = samples.first(last + 1)
|
|
172
|
+
index = 0
|
|
173
|
+
while index < out.size
|
|
174
|
+
out[index] *= gain
|
|
175
|
+
index += 1
|
|
176
|
+
end
|
|
177
|
+
fade = [(FADE * RATE).round, out.size].min
|
|
178
|
+
fade.times { |index| out[-1 - index] *= index.fdiv(fade) }
|
|
179
|
+
out
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# A decaying sine from a sample on, at a loudness and a phase, until
|
|
183
|
+
# its lives are spent. It is worked out by the recurrence that turns a
|
|
184
|
+
# sine on by one sample from the two before it, which is much quicker
|
|
185
|
+
# than finding every sine.
|
|
186
|
+
def ring(out, pitch, loudness, decay, from: 0, phase: 0.0)
|
|
187
|
+
step = TURN * pitch / RATE
|
|
188
|
+
fade = Math.exp(-1.0 / (decay * RATE))
|
|
189
|
+
turn = 2 * fade * Math.cos(step)
|
|
190
|
+
pull = fade * fade
|
|
191
|
+
now = loudness * Math.sin(phase)
|
|
192
|
+
before = loudness / fade * Math.sin(phase - step)
|
|
193
|
+
last = [out.size, (decay * LIVES * RATE).round].min
|
|
194
|
+
index = from
|
|
195
|
+
while index < last
|
|
196
|
+
out[index] += now
|
|
197
|
+
now, before = turn * now - pull * before, now
|
|
198
|
+
index += 1
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# A partial whose pitch starts off its note and settles onto it, sine
|
|
203
|
+
# by sine; once it has settled it rings on like any other.
|
|
204
|
+
def glide(out, pitch, loudness, decay, bend, settle)
|
|
205
|
+
step = TURN * pitch / RATE
|
|
206
|
+
fade = Math.exp(-1.0 / (decay * RATE))
|
|
207
|
+
ease = Math.exp(-1.0 / (settle * RATE))
|
|
208
|
+
phase = 0.0
|
|
209
|
+
level = loudness
|
|
210
|
+
index = 0
|
|
211
|
+
while index < out.size && bend.abs > SETTLED
|
|
212
|
+
out[index] += Math.sin(phase) * level
|
|
213
|
+
phase += step * (1 + bend)
|
|
214
|
+
bend *= ease
|
|
215
|
+
level *= fade
|
|
216
|
+
index += 1
|
|
217
|
+
end
|
|
218
|
+
ring(out, pitch, level, decay, from: index, phase:)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# The string as a finger leaves it: pulled into a triangle with its
|
|
222
|
+
# corner where it was plucked, a little rough where the nail caught
|
|
223
|
+
# it, and centred so it never thumps.
|
|
224
|
+
def pulled(length, position, brightness, rng)
|
|
225
|
+
corner = [position * length, 1.0].max
|
|
226
|
+
rough = 0.0
|
|
227
|
+
shape = Array.new(length) do |index|
|
|
228
|
+
rough += 0.5 * (rng.rand * 2 - 1 - rough)
|
|
229
|
+
rise = index < corner ? index / corner : (length - index) / (length - corner)
|
|
230
|
+
rise + brightness * 0.35 * rough
|
|
231
|
+
end
|
|
232
|
+
middle = shape.sum / length
|
|
233
|
+
shape.map! { |sample| sample - middle }
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# The loop: each turn the string is smoothed, delayed the last part of
|
|
237
|
+
# a sample to stay in tune, and loses its share; what goes round is
|
|
238
|
+
# heard.
|
|
239
|
+
def string(line, count, tune, loss, stretch)
|
|
240
|
+
out = silence(count.fdiv(RATE))
|
|
241
|
+
keep = 1 - stretch
|
|
242
|
+
head = 0
|
|
243
|
+
last = 0.0
|
|
244
|
+
into = 0.0
|
|
245
|
+
past = 0.0
|
|
246
|
+
index = 0
|
|
247
|
+
while index < count
|
|
248
|
+
now = line[head]
|
|
249
|
+
smooth = keep * now + stretch * last
|
|
250
|
+
last = now
|
|
251
|
+
past = tune * (smooth - past) + into
|
|
252
|
+
into = smooth
|
|
253
|
+
line[head] = past * loss
|
|
254
|
+
out[index] = now
|
|
255
|
+
head += 1
|
|
256
|
+
head = 0 if head == line.size
|
|
257
|
+
index += 1
|
|
258
|
+
end
|
|
259
|
+
shape(out, fall: false)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# A struck sound still needs a moment to start, or the speaker clicks,
|
|
263
|
+
# and its last quarter fades away, unless it dies away by itself.
|
|
264
|
+
def shape(out, fall: true)
|
|
265
|
+
rise = (ATTACK * RATE).round
|
|
266
|
+
rise.times { |index| out[index] *= index.fdiv(rise) } if out.size > rise
|
|
267
|
+
(out.size / 4).times { |index| out[-1 - index] *= index.fdiv(out.size / 4) } if fall
|
|
268
|
+
out
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# How far a one-pole filter moves toward each sample, to pass what is
|
|
272
|
+
# under a frequency.
|
|
273
|
+
def pass(frequency) = 1 - Math.exp(-TURN * frequency / RATE)
|
|
274
|
+
|
|
275
|
+
private_class_method :ring, :glide, :pulled, :string, :shape, :pass
|
|
276
|
+
end
|
|
277
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# A piece of music written as text, the way the art is drawn as grids.
|
|
5
|
+
# It comes in sections, each a name on a line of its own over the parts
|
|
6
|
+
# it is played on, one line a voice, one character a step:
|
|
7
|
+
#
|
|
8
|
+
# verse
|
|
9
|
+
# melody 5 | E--DE-G- | A---G-E- |
|
|
10
|
+
# | G--ED-C- | D-----.. |
|
|
11
|
+
# bass 2 | c------- | A------- |
|
|
12
|
+
# | F------- | G------- |
|
|
13
|
+
# kick | X...x... | X...x... |
|
|
14
|
+
# | X...x... | X...x... |
|
|
15
|
+
#
|
|
16
|
+
# A letter starts a note: capitals in the octave after the part's name,
|
|
17
|
+
# small letters in the one above it. A dash holds the note and a dot
|
|
18
|
+
# lets it go; a drum is struck with an x, or hard with an X. A line with
|
|
19
|
+
# no name goes on with the voice above it, and two lines with the same
|
|
20
|
+
# name are two voices of one part, for a chord. Bars and spaces are only
|
|
21
|
+
# for reading.
|
|
22
|
+
#
|
|
23
|
+
# The sections play in the order they are written, and after the last
|
|
24
|
+
# the tune goes round again from the one it names, so an opening is
|
|
25
|
+
# heard once. Steps fall evenly on the beat unless the tune swings, when
|
|
26
|
+
# the first of each pair is held longer than the second. A bar is four
|
|
27
|
+
# beats unless the tune says otherwise, as a waltz says three.
|
|
28
|
+
class Tune
|
|
29
|
+
Part = Data.define(:instrument, :gain, :pan, :mono, :release, :from) do
|
|
30
|
+
def initialize(instrument:, gain: 1.0, pan: 0.0, mono: false, release: nil, from: 0) = super
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# A note: the beat it starts on, the part it is for, its pitch as a
|
|
34
|
+
# MIDI number (none for a drum), how many beats it is held, and how
|
|
35
|
+
# hard it is struck.
|
|
36
|
+
Note = Data.define(:beat, :part, :pitch, :beats, :accent)
|
|
37
|
+
|
|
38
|
+
LETTERS = { "C" => 0, "D" => 2, "E" => 4, "F" => 5, "G" => 7, "A" => 9 }.freeze
|
|
39
|
+
# A note on the bar is played a little harder than one on the beat, and
|
|
40
|
+
# that than one between beats, as a player would without counting.
|
|
41
|
+
STRESS = [1.0, 0.9, 0.82].freeze
|
|
42
|
+
STROKES = { "x" => 0.7, "X" => 1.0 }.freeze
|
|
43
|
+
|
|
44
|
+
attr_reader :name, :tempo, :meter, :quickens, :fade_in, :fade_out, :echo, :parts, :notes, :length, :again
|
|
45
|
+
|
|
46
|
+
# The tempo is in beats a minute at the snake's first pace, and the
|
|
47
|
+
# tune quickens by the pace to the given power as the snake does. It
|
|
48
|
+
# swells in over the fade in as it starts and dies away over the fade
|
|
49
|
+
# out when it is stopped, and the walls answer it as loudly as its echo
|
|
50
|
+
# says. Lengths and places are counted in beats.
|
|
51
|
+
def initialize(name, text, parts:, tempo:, steps:, meter: 4, swing: 0.5, again: nil, quickens: 0.0, fade_in: 0.0,
|
|
52
|
+
fade_out: 0.05, echo: 0.2)
|
|
53
|
+
@name = name
|
|
54
|
+
@parts = parts
|
|
55
|
+
@tempo = tempo
|
|
56
|
+
@steps = steps
|
|
57
|
+
@meter = meter
|
|
58
|
+
@swing = swing
|
|
59
|
+
@quickens = quickens
|
|
60
|
+
@fade_in = fade_in
|
|
61
|
+
@fade_out = fade_out
|
|
62
|
+
@echo = echo
|
|
63
|
+
@notes = []
|
|
64
|
+
@length = 0.0
|
|
65
|
+
read(text).each do |section, voices|
|
|
66
|
+
@again = @length if section == again
|
|
67
|
+
voices.each { |part, octave, marks| @notes.concat(notes_in(part, octave, marks, @length)) }
|
|
68
|
+
@length += voices.first.last.size.fdiv(@steps)
|
|
69
|
+
end
|
|
70
|
+
raise ArgumentError, "#{name} has no section #{again} to go round from" if again && !@again
|
|
71
|
+
|
|
72
|
+
@again ||= 0.0
|
|
73
|
+
@notes.sort_by!.with_index { |note, index| [note.beat, index] }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# How fast the tune goes at a pace of the snake's, in beats a second.
|
|
77
|
+
def beats_per_second(pace) = @tempo * pace**@quickens / 60.0
|
|
78
|
+
|
|
79
|
+
# Every instrument the tune is played on, at every pitch it plays, as
|
|
80
|
+
# [instrument, pitch], each once and in the order they are first heard.
|
|
81
|
+
def keys = @keys ||= @notes.map { |note| [@parts.fetch(note.part).instrument, note.pitch] }.uniq
|
|
82
|
+
|
|
83
|
+
def self.frequency(pitch) = 440.0 * 2**((pitch - 69) / 12.0)
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
# The sections as [name, voices], each voice [part, octave, marks].
|
|
88
|
+
def read(text)
|
|
89
|
+
sections = []
|
|
90
|
+
text.each_line(chomp: true) do |line|
|
|
91
|
+
voices = sections.last&.last
|
|
92
|
+
case line
|
|
93
|
+
when /\A\s*\z/ then next
|
|
94
|
+
when /\A(\S+)\s*\z/ then sections << [Regexp.last_match(1), []]
|
|
95
|
+
when /\A\s+\|(.*)\z/ then voices.last.last.concat(marks(Regexp.last_match(1)))
|
|
96
|
+
when /\A\s+(\w+)(?:\s+(\d))?\s*\|(.*)\z/
|
|
97
|
+
part, octave, rest = Regexp.last_match.captures
|
|
98
|
+
voices << [part.to_sym, octave&.to_i, marks(rest)]
|
|
99
|
+
else raise ArgumentError, "unreadable tune line: #{line.inspect}"
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
sections.each { |section, voices| check(section, voices) }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def marks(text) = text.delete(" |").chars
|
|
106
|
+
|
|
107
|
+
def check(section, voices)
|
|
108
|
+
sizes = voices.map { |_, _, marks| marks.size }.uniq
|
|
109
|
+
raise ArgumentError, "ragged tune lines in #{section}" unless sizes.size == 1
|
|
110
|
+
raise ArgumentError, "#{section} is not whole bars" unless (sizes.first % (@steps * @meter)).zero?
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def notes_in(part, octave, marks, start)
|
|
114
|
+
marks.each_index.filter_map do |step|
|
|
115
|
+
mark = marks[step]
|
|
116
|
+
next if "-.".include?(mark)
|
|
117
|
+
|
|
118
|
+
held = marks[(step + 1)..].take_while { |later| later == "-" }.size + 1
|
|
119
|
+
Note.new(beat: start + beat(step), part:, pitch: octave && pitch(mark, octave),
|
|
120
|
+
beats: beat(step + held) - beat(step), accent: accent(mark, step, octave))
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def pitch(letter, octave)
|
|
125
|
+
semitone = LETTERS.fetch(letter.upcase) { raise ArgumentError, "no note #{letter} in a tune" }
|
|
126
|
+
12 * (octave + (letter == letter.upcase ? 1 : 2)) + semitone
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Where a step falls, in beats. Steps go in pairs, and in a swung tune
|
|
130
|
+
# the second of a pair comes late.
|
|
131
|
+
def beat(step)
|
|
132
|
+
pair = 2.0 / @steps
|
|
133
|
+
(step / 2) * pair + (step.odd? ? @swing * pair : 0.0)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def accent(mark, step, octave)
|
|
137
|
+
return STROKES.fetch(mark) { raise ArgumentError, "a drum is struck with x or X, not #{mark}" } unless octave
|
|
138
|
+
|
|
139
|
+
if (step % (@steps * @meter)).zero? then STRESS[0]
|
|
140
|
+
elsif (step % @steps).zero? then STRESS[1]
|
|
141
|
+
else STRESS[2]
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
module Tunes
|
|
5
|
+
# Dusk plays behind the menus: a lullaby on a music box over a harp and
|
|
6
|
+
# a plucked bass, with a vibraphone for its middle. The chase plays
|
|
7
|
+
# while the snake runs: the same tune, bouncing over marimba, bass and
|
|
8
|
+
# brushes, quickening with the snake, and growing a shaker, a bell and
|
|
9
|
+
# glass as the multiplier climbs.
|
|
10
|
+
DUSK = Tune.new(:dusk, <<~MUSIC, tempo: 76, steps: 2, swing: 0.62, again: "a1", fade_in: 1.5, fade_out: 0.5, echo: 0.3,
|
|
11
|
+
intro
|
|
12
|
+
harp 3 | Fcegagec | Gcdeaedc |
|
|
13
|
+
bass 2 | F---c--- | G---d--- |
|
|
14
|
+
|
|
15
|
+
a1
|
|
16
|
+
melody 5 | E--DE-G- | A---G-E- | G--ED-C- | D-----.. |
|
|
17
|
+
| E--DE-G- | A---c-A- | G--ED-E- | C-----.. |
|
|
18
|
+
harp 3 | CGdegedG | Acegagec | Fcegagec | Gcdeaedc |
|
|
19
|
+
| CGdegedG | Acegagec | FcecGded | CGdegedG |
|
|
20
|
+
bass 2 | c---G--- | A---E--- | F---c--- | G---D--- |
|
|
21
|
+
| c---G--- | A---E--- | F---G--- | c---G--- |
|
|
22
|
+
|
|
23
|
+
a2
|
|
24
|
+
melody 5 | E--DE-G- | A---G-E- | G--ED-C- | D-----.. |
|
|
25
|
+
| E--DE-G- | A---c-d- | e--dc-A- | G--EC--- |
|
|
26
|
+
counter 5 | ........ | ..c..... | ........ | ..G.A.c. |
|
|
27
|
+
| ........ | ........ | ........ | ....e.g. |
|
|
28
|
+
harp 3 | CGdegedG | Acegagec | Fcegagec | Gcdeaedc |
|
|
29
|
+
| CGdegedG | Acegagec | FcecGded | CGdegedG |
|
|
30
|
+
bass 2 | c---G--- | A---E--- | F---c--- | G---D--- |
|
|
31
|
+
| c---G--- | A---E--- | F---G--- | c---G--- |
|
|
32
|
+
|
|
33
|
+
b
|
|
34
|
+
vibes 5 | c---A-G- | G-----E- | A---G-E- | D-----.. |
|
|
35
|
+
| c---d-c- | A---G-A- | G---E-D- | D-----CD |
|
|
36
|
+
harp 3 | F-c-e-g- | E-G-d-g- | D-A-c-e- | C-G-d-e- |
|
|
37
|
+
| F-c-e-a- | E-G-d-a- | D-A-c-g- | G-c-d-a- |
|
|
38
|
+
bass 2 | F---c--- | E---G--- | D---A--- | C---G--- |
|
|
39
|
+
| F---c--- | E---G--- | D---A--- | G---d--- |
|
|
40
|
+
sparkle 6 | ........ | .....A.. | ........ | ...E.... |
|
|
41
|
+
| ........ | ......G. | ........ | ........ |
|
|
42
|
+
|
|
43
|
+
a3
|
|
44
|
+
melody 5 | E--DE-G- | A---G-E- | G--ED-C- | D-----.. |
|
|
45
|
+
| E--DE-G- | A---c-d- | e--dc-A- | G--EC--- |
|
|
46
|
+
counter 6 | E--DE-G- | A---G-E- | ........ | ..G.A.c. |
|
|
47
|
+
| ........ | ........ | ........ | ........ |
|
|
48
|
+
harp 3 | CGdegedG | Acegagec | Fcegagec | Gcdeaedc |
|
|
49
|
+
| CGdegedG | Acegagec | FcecGded | CGdegedG |
|
|
50
|
+
bass 2 | c---G--- | A---E--- | F---c--- | G---D--- |
|
|
51
|
+
| c---G--- | A---E--- | F---G--- | c---G--- |
|
|
52
|
+
sparkle 6 | ........ | ........ | ........ | ........ |
|
|
53
|
+
| ..E..... | ........ | ....G... | ......c. |
|
|
54
|
+
|
|
55
|
+
tag
|
|
56
|
+
melody 5 | A-G-E-D- | D-----.. |
|
|
57
|
+
harp 3 | Fcegagec | Gcdeaedc |
|
|
58
|
+
bass 2 | F---c--- | G---d--- |
|
|
59
|
+
MUSIC
|
|
60
|
+
parts: {
|
|
61
|
+
melody: Tune::Part.new(instrument: :music_box, gain: 0.9, pan: 0.15),
|
|
62
|
+
counter: Tune::Part.new(instrument: :celesta, gain: 0.4, pan: -0.25),
|
|
63
|
+
vibes: Tune::Part.new(instrument: :vibes, gain: 0.76, pan: 0.1, release: 0.25),
|
|
64
|
+
harp: Tune::Part.new(instrument: :harp, gain: 0.47, pan: -0.3),
|
|
65
|
+
bass: Tune::Part.new(instrument: :bass, gain: 0.89),
|
|
66
|
+
sparkle: Tune::Part.new(instrument: :glass, gain: 0.4, pan: 0.5)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
CHASE = Tune.new(:chase, <<~MUSIC, tempo: 120, steps: 4, swing: 0.56, quickens: 0.3, fade_out: 0.04, echo: 0.12,
|
|
70
|
+
a
|
|
71
|
+
melody 5 | E..D..E.G------- | A-----G..E------ | G..E..D.C------- | D-----------C.D. |
|
|
72
|
+
| E..D..E.G------- | A-----c..A------ | G..E..D.E------- | C-----------C.D. |
|
|
73
|
+
marimba 4 | ..G...G...G...G. | ..A...A...A...A. | ..A...A...A...A. | ..A...A...A...A. |
|
|
74
|
+
| ..G...G...G...G. | ..A...A...A...A. | ..A...A...A...A. | ..G...G...G...G. |
|
|
75
|
+
marimba 4 | ..E...E...E...E. | ..E...E...E...E. | ..E...E...E...E. | ..D...D...D...D. |
|
|
76
|
+
| ..E...E...E...E. | ..E...E...E...E. | ..E...E...D...D. | ..E...E...E...E. |
|
|
77
|
+
bass 2 | c.....G.c.....G. | A.....E.A.....G. | F.....c.F.....A. | G.....d.G.....A. |
|
|
78
|
+
| c.....G.c.....G. | A.....E.A.....G. | F.....c.G.....A. | c.....G.c.....G. |
|
|
79
|
+
kick | X.......X.x..... | X.......X.x..... | X.......X.x..... | X.......X.x..... |
|
|
80
|
+
| X.......X.x..... | X.......X.x..... | X.......X.x..... | X.......X.x.X... |
|
|
81
|
+
brush | ....X.......X..x | ....X.......X..x | ....X.......X..x | ....X.......X..x |
|
|
82
|
+
| ....X.......X..x | ....X.......X..x | ....X.......X..x | ....X.......XxXx |
|
|
83
|
+
shaker | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx |
|
|
84
|
+
| xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx |
|
|
85
|
+
|
|
86
|
+
a2
|
|
87
|
+
melody 5 | E..D..E.G------- | A-----G..E------ | G..E..D.C------- | D-----------C.D. |
|
|
88
|
+
| E..D..E.G------- | A-----c..d------ | e..d..c.A------- | G..E..C-----C.D. |
|
|
89
|
+
counter 5 | ................ | ..c............. | ................ | ....G..A..c..... |
|
|
90
|
+
| ................ | ..e............. | ................ | ........e...g... |
|
|
91
|
+
marimba 4 | ..G...G...G...G. | ..A...A...A...A. | ..A...A...A...A. | ..A...A...A...A. |
|
|
92
|
+
| ..G...G...G...G. | ..A...A...A...A. | ..A...A...A...A. | ..G...G...G...G. |
|
|
93
|
+
marimba 4 | ..E...E...E...E. | ..E...E...E...E. | ..E...E...E...E. | ..D...D...D...D. |
|
|
94
|
+
| ..E...E...E...E. | ..E...E...E...E. | ..E...E...D...D. | ..E...E...E...E. |
|
|
95
|
+
bass 2 | c.....G.c.....G. | A.....E.A.....G. | F.....c.F.....A. | G.....d.G.....A. |
|
|
96
|
+
| c.....G.c.....G. | A.....E.A.....G. | F.....c.G.....A. | c.....G.c.....E. |
|
|
97
|
+
kick | X.......X.x..... | X.......X.x..... | X.......X.x..... | X.......X.x..... |
|
|
98
|
+
| X.......X.x..... | X.......X.x..... | X.......X.x..... | X.......X.x.X... |
|
|
99
|
+
brush | ....X.......X..x | ....X.......X..x | ....X.......X..x | ....X.......X..x |
|
|
100
|
+
| ....X.......X..x | ....X.......X..x | ....X.......X..x | ....X.......XxXx |
|
|
101
|
+
shaker | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx |
|
|
102
|
+
| xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx |
|
|
103
|
+
|
|
104
|
+
b
|
|
105
|
+
vibes 5 | c-------A---G--- | G-----------E--- | A-------G---E--- | D-----------.... |
|
|
106
|
+
| c-------d---c--- | A-------G---A--- | G-------E---D--- | D-----------C-D- |
|
|
107
|
+
marimba 4 | ..A...A...A...A. | ..G...G...G...G. | ..E...E...E...E. | ..G...G...G...G. |
|
|
108
|
+
| ..A...A...A...A. | ..G...G...G...G. | ..E...E...E...E. | ..A...A...A...A. |
|
|
109
|
+
marimba 4 | ..E...E...E...E. | ..D...D...D...D. | ..C...C...C...C. | ..E...E...E...E. |
|
|
110
|
+
| ..E...E...E...E. | ..D...D...D...D. | ..C...C...C...C. | ..D...D...D...D. |
|
|
111
|
+
bass 2 | F.....c.F.....F. | E.....A.E.....E. | D.....A.D.....D. | C.....G.C.....E. |
|
|
112
|
+
| F.....c.F.....F. | E.....A.E.....E. | D.....A.D.....A. | G.....d.G.....A. |
|
|
113
|
+
kick | X.......X.x..... | X.......X.x..... | X.......X.x..... | X.......X.x..... |
|
|
114
|
+
| X.......X.x..... | X.......X.x..... | X.......X.x..... | X.......X.x.X... |
|
|
115
|
+
brush | ....X.......X..x | ....X.......X..x | ....X.......X..x | ....X.......X..x |
|
|
116
|
+
| ....X.......X..x | ....X.......X..x | ....X.......X..x | ....X.......XxXx |
|
|
117
|
+
shaker | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx |
|
|
118
|
+
| xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx |
|
|
119
|
+
sparkle 6 | ................ | ..........A..... | ................ | ......E......... |
|
|
120
|
+
| ................ | ............G... | ................ | ................ |
|
|
121
|
+
|
|
122
|
+
a3
|
|
123
|
+
melody 5 | E..D..E.G------- | A-----G..E------ | G..E..D.C------- | D-----------C.D. |
|
|
124
|
+
| E..D..E.G------- | A-----c..d------ | e..d..c.A------- | G..E..C--------- |
|
|
125
|
+
counter 6 | E..D..E.G------- | A-----G..E------ | ................ | ....G..A..c..... |
|
|
126
|
+
| ................ | ..e............. | ................ | ........e...g... |
|
|
127
|
+
marimba 4 | ..G...G...G...G. | ..A...A...A...A. | ..A...A...A...A. | ..A...A...A...A. |
|
|
128
|
+
| ..G...G...G...G. | ..A...A...A...A. | ..A...A...A...A. | ..G...G...G...G. |
|
|
129
|
+
marimba 4 | ..E...E...E...E. | ..E...E...E...E. | ..E...E...E...E. | ..D...D...D...D. |
|
|
130
|
+
| ..E...E...E...E. | ..E...E...E...E. | ..E...E...D...D. | ..E...E...E...E. |
|
|
131
|
+
bass 2 | c.....G.c.....G. | A.....E.A.....G. | F.....c.F.....A. | G.....d.G.....A. |
|
|
132
|
+
| c.....G.c.....G. | A.....E.A.....G. | F.....c.G.....A. | c.....G.c.....G. |
|
|
133
|
+
kick | X.......X.x..... | X.......X.x..... | X.......X.x..... | X.......X.x..... |
|
|
134
|
+
| X.......X.x..... | X.......X.x..... | X.......X.x..... | X.......X.x.X... |
|
|
135
|
+
brush | ....X.......X..x | ....X.......X..x | ....X.......X..x | ....X.......X..x |
|
|
136
|
+
| ....X.......X..x | ....X.......X..x | ....X.......X..x | ....X.......XxXx |
|
|
137
|
+
shaker | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx |
|
|
138
|
+
| xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx | xxXxxxXxxxXxxxXx |
|
|
139
|
+
sparkle 6 | ................ | ................ | ..........E..... | ................ |
|
|
140
|
+
| ......G......... | ................ | ..........A..... | ............c... |
|
|
141
|
+
MUSIC
|
|
142
|
+
parts: {
|
|
143
|
+
melody: Tune::Part.new(instrument: :music_box, gain: 0.67, pan: 0.15),
|
|
144
|
+
vibes: Tune::Part.new(instrument: :vibes, gain: 0.56, pan: 0.1, release: 0.15),
|
|
145
|
+
counter: Tune::Part.new(instrument: :celesta, gain: 0.3, pan: -0.25, from: 3),
|
|
146
|
+
marimba: Tune::Part.new(instrument: :marimba, gain: 0.38, pan: -0.2),
|
|
147
|
+
bass: Tune::Part.new(instrument: :bass, gain: 0.65, mono: true),
|
|
148
|
+
kick: Tune::Part.new(instrument: :kick, gain: 0.67),
|
|
149
|
+
brush: Tune::Part.new(instrument: :brush, gain: 0.46, pan: 0.1),
|
|
150
|
+
shaker: Tune::Part.new(instrument: :shaker, gain: 0.3, pan: 0.35, from: 2),
|
|
151
|
+
sparkle: Tune::Part.new(instrument: :glass, gain: 0.3, pan: 0.5, from: 5)
|
|
152
|
+
})
|
|
153
|
+
end
|
|
154
|
+
end
|