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.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE +21 -0
  4. data/README.md +122 -0
  5. data/bin/pixelsnake +8 -0
  6. data/lib/pixelsnake/abacus.rb +102 -0
  7. data/lib/pixelsnake/app.rb +130 -0
  8. data/lib/pixelsnake/art/big_font.rb +196 -0
  9. data/lib/pixelsnake/art/heads.rb +68 -0
  10. data/lib/pixelsnake/art/small_font.rb +73 -0
  11. data/lib/pixelsnake/autopilot.rb +34 -0
  12. data/lib/pixelsnake/award.rb +67 -0
  13. data/lib/pixelsnake/board.rb +36 -0
  14. data/lib/pixelsnake/canvas.rb +189 -0
  15. data/lib/pixelsnake/choice.rb +92 -0
  16. data/lib/pixelsnake/clockwork.rb +169 -0
  17. data/lib/pixelsnake/controls.rb +26 -0
  18. data/lib/pixelsnake/countdown.rb +80 -0
  19. data/lib/pixelsnake/courtyard.rb +306 -0
  20. data/lib/pixelsnake/day.rb +23 -0
  21. data/lib/pixelsnake/death.rb +115 -0
  22. data/lib/pixelsnake/debug_overlay.rb +53 -0
  23. data/lib/pixelsnake/demo.rb +85 -0
  24. data/lib/pixelsnake/density.rb +47 -0
  25. data/lib/pixelsnake/ease.rb +27 -0
  26. data/lib/pixelsnake/fade.rb +30 -0
  27. data/lib/pixelsnake/feats.rb +86 -0
  28. data/lib/pixelsnake/fine.rb +78 -0
  29. data/lib/pixelsnake/fireflies.rb +63 -0
  30. data/lib/pixelsnake/floating_text.rb +36 -0
  31. data/lib/pixelsnake/font.rb +62 -0
  32. data/lib/pixelsnake/fruit.rb +149 -0
  33. data/lib/pixelsnake/game.rb +294 -0
  34. data/lib/pixelsnake/gear.rb +132 -0
  35. data/lib/pixelsnake/high_scores.rb +91 -0
  36. data/lib/pixelsnake/hud.rb +217 -0
  37. data/lib/pixelsnake/iris.rb +76 -0
  38. data/lib/pixelsnake/jukebox.rb +143 -0
  39. data/lib/pixelsnake/keyboard.rb +96 -0
  40. data/lib/pixelsnake/laurel.rb +76 -0
  41. data/lib/pixelsnake/leaf.rb +73 -0
  42. data/lib/pixelsnake/lettering.rb +111 -0
  43. data/lib/pixelsnake/medal.rb +29 -0
  44. data/lib/pixelsnake/medal_view.rb +428 -0
  45. data/lib/pixelsnake/menu.rb +213 -0
  46. data/lib/pixelsnake/meter.rb +108 -0
  47. data/lib/pixelsnake/metronome.rb +24 -0
  48. data/lib/pixelsnake/name.rb +22 -0
  49. data/lib/pixelsnake/name_field.rb +156 -0
  50. data/lib/pixelsnake/odometer.rb +76 -0
  51. data/lib/pixelsnake/palette.rb +64 -0
  52. data/lib/pixelsnake/panel.rb +45 -0
  53. data/lib/pixelsnake/particles.rb +188 -0
  54. data/lib/pixelsnake/piece.rb +82 -0
  55. data/lib/pixelsnake/playback.rb +22 -0
  56. data/lib/pixelsnake/preferences.rb +96 -0
  57. data/lib/pixelsnake/rare_fruit.rb +268 -0
  58. data/lib/pixelsnake/recording.rb +44 -0
  59. data/lib/pixelsnake/ribbon.rb +110 -0
  60. data/lib/pixelsnake/save_file.rb +51 -0
  61. data/lib/pixelsnake/scenes/game_over.rb +249 -0
  62. data/lib/pixelsnake/scenes/medal_case.rb +268 -0
  63. data/lib/pixelsnake/scenes/pause.rb +171 -0
  64. data/lib/pixelsnake/scenes/play.rb +326 -0
  65. data/lib/pixelsnake/scenes/scores.rb +420 -0
  66. data/lib/pixelsnake/scenes/settings.rb +344 -0
  67. data/lib/pixelsnake/scenes/size_notice.rb +38 -0
  68. data/lib/pixelsnake/scenes/statistics.rb +420 -0
  69. data/lib/pixelsnake/scenes/title.rb +152 -0
  70. data/lib/pixelsnake/screen.rb +163 -0
  71. data/lib/pixelsnake/shed.rb +40 -0
  72. data/lib/pixelsnake/skin.rb +36 -0
  73. data/lib/pixelsnake/snake.rb +53 -0
  74. data/lib/pixelsnake/snake_view.rb +393 -0
  75. data/lib/pixelsnake/sound/band.rb +354 -0
  76. data/lib/pixelsnake/sound/instruments.rb +143 -0
  77. data/lib/pixelsnake/sound/mixer.rb +60 -0
  78. data/lib/pixelsnake/sound/music.rb +15 -0
  79. data/lib/pixelsnake/sound/pulse.rb +149 -0
  80. data/lib/pixelsnake/sound/song.rb +22 -0
  81. data/lib/pixelsnake/sound/sounds.rb +225 -0
  82. data/lib/pixelsnake/sound/speaker.rb +199 -0
  83. data/lib/pixelsnake/sound/synth.rb +277 -0
  84. data/lib/pixelsnake/sound/tune.rb +145 -0
  85. data/lib/pixelsnake/sound/tunes/dusk.rb +154 -0
  86. data/lib/pixelsnake/sound/tunes/fireflies.rb +106 -0
  87. data/lib/pixelsnake/sound/tunes/jade.rb +100 -0
  88. data/lib/pixelsnake/sound/tunes/lanterns.rb +126 -0
  89. data/lib/pixelsnake/sound/tunes.rb +40 -0
  90. data/lib/pixelsnake/sprite.rb +58 -0
  91. data/lib/pixelsnake/stage.rb +95 -0
  92. data/lib/pixelsnake/stats.rb +131 -0
  93. data/lib/pixelsnake/terminal.rb +84 -0
  94. data/lib/pixelsnake/terrarium.rb +206 -0
  95. data/lib/pixelsnake/tile.rb +32 -0
  96. data/lib/pixelsnake/toolbox.rb +251 -0
  97. data/lib/pixelsnake/trophy.rb +99 -0
  98. data/lib/pixelsnake/tuft.rb +55 -0
  99. data/lib/pixelsnake/upscale.rb +60 -0
  100. data/lib/pixelsnake/version.rb +5 -0
  101. data/lib/pixelsnake.rb +143 -0
  102. metadata +146 -0
@@ -0,0 +1,354 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Plays the music in the sound process, beside the sounds: whichever tune
5
+ # the game asks for, note by note on the instruments made for it, at the
6
+ # snake's pace and with the parts its level has earned. Notes land to the
7
+ # sample, now and then a hair late as from a hand, and a held note is let
8
+ # go when its time is up. The music has its own echo off the courtyard
9
+ # walls, and behind the pause panel it is heard as through a door.
10
+ #
11
+ # A new tune waits for the last to die away, and for every note it plays
12
+ # to have been made, then swells in from its start; a tune asked for
13
+ # again carries on where it is. Asking for no tune, or turning the music
14
+ # off, lets it die away.
15
+ #
16
+ # The notes can be handed over as they are made, down a pipe from a
17
+ # process of their own: each is a line saying what it is, the instrument,
18
+ # the pitch or a dash for a drum, and how many samples, then the samples
19
+ # as floats. They are kept packed as they came, half the size of an
20
+ # array of them, and a voice unpacks only what it is about to play.
21
+ class Band
22
+ Voice = Struct.new(:samples, :at, :left, :right, :part, :ends, :fade, :gain, :slope)
23
+
24
+ # The bytes a sample takes, packed.
25
+ FLOAT = 4
26
+
27
+ SEED = 23
28
+ VOICES = 48
29
+ # A part that plays one note at a time cuts the last one off this fast.
30
+ CHOKE = 0.03
31
+ # How late a note may come, at most.
32
+ SWAY = 0.004
33
+ # How soon the tempo catches up with the snake, a new volume is heard,
34
+ # and the door opens or shuts.
35
+ CATCH_UP = 1.2
36
+ TURN_UP = 0.15
37
+ DOOR = 0.12
38
+ # Through the door only what is under its pitch comes, and quieter.
39
+ OPEN = 14_000.0
40
+ SHUT = 650.0
41
+ MUFFLED = 0.55
42
+ # The walls answer left and right a little apart, each repeat quieter,
43
+ # by as much as the tune says, and duller than the last, as they do the
44
+ # bigger sounds. A busy tune is answered more softly, or its echoes
45
+ # would crowd its own notes.
46
+ ECHOES = [0.105, 0.135].freeze
47
+ TONE = 0.3
48
+ # Once nothing has played for this long, the echo has died away too.
49
+ STILL = 1.5
50
+
51
+ # A note as it goes down the pipe.
52
+ def self.pack(instrument, pitch, samples) = "#{instrument} #{pitch || "-"} #{samples.size}\n#{samples.pack("e*")}"
53
+
54
+ def initialize(tunes, bank = {}, rng = Random.new(SEED))
55
+ @tunes = tunes
56
+ @bank = bank
57
+ @arriving = "".b
58
+ @rng = rng
59
+ @voices = []
60
+ @tune = nil
61
+ @wanted = nil
62
+ @pace = 1.0
63
+ @level = 1
64
+ @asked_level = 1
65
+ @volume = 0.0
66
+ @gain = 0.0
67
+ @swell = 1.0
68
+ @muffle = 0.0
69
+ @muffled = 0.0
70
+ @door = [0.0, 0.0, 0.0, 0.0]
71
+ @lines = ECHOES.map { |delay| Array.new((delay * Synth::RATE).round, 0.0) }
72
+ @heads = [0, 0]
73
+ @memories = [0.0, 0.0]
74
+ @feedback = 0.0
75
+ @clock = 0
76
+ @still = STILL * Synth::RATE
77
+ end
78
+
79
+ # What the game wants heard: a tune by name, or none, at the snake's
80
+ # pace and level, behind the door or not, and how loud.
81
+ def ask(name, pace, level, muffled, volume)
82
+ @pace = pace
83
+ @asked_level = level
84
+ @muffled = muffled ? 1.0 : 0.0
85
+ @volume = volume
86
+ wanted = volume.positive? ? @tunes[name] : nil
87
+ return if wanted.equal?(@wanted)
88
+
89
+ @wanted = wanted
90
+ stop if @tune
91
+ end
92
+
93
+ def quiet? = @tune.nil? && @wanted.nil? && @voices.empty? && @still >= STILL * Synth::RATE
94
+
95
+ # Takes whatever notes have come down the pipe, keeping any half of one
96
+ # for when the rest comes. Gives the pipe back while there is more to
97
+ # come, and nothing once it has all come.
98
+ def learn(pipe)
99
+ chunk = pipe.read_nonblock(1 << 16, exception: false)
100
+ return if chunk.nil?
101
+
102
+ @arriving << chunk unless chunk == :wait_readable
103
+ take_notes
104
+ pipe
105
+ end
106
+
107
+ # Adds the next frames of music to the stream's floats, left and right
108
+ # in turn.
109
+ def play(out, frames)
110
+ return if quiet?
111
+
112
+ start(@wanted) if @tune.nil? && @wanted && @voices.empty? && ready?(@wanted)
113
+ bus = Array.new(frames * 2, 0.0)
114
+ advance(frames) if @tune
115
+ @voices.each { |voice| sound(voice, bus, frames) }
116
+ @voices.reject! { |voice| voice.at >= length(voice) || voice.gain <= 0.0 }
117
+ @still = @voices.empty? ? @still + frames : 0
118
+ swell(bus, frames) if @swell < 1.0
119
+ muffle(bus, frames)
120
+ echo(bus, frames)
121
+ lay(out, bus, frames)
122
+ @clock += frames
123
+ end
124
+
125
+ private
126
+
127
+ def take_notes
128
+ while (header = @arriving.index("\n"))
129
+ instrument, pitch, count = @arriving.byteslice(0, header).split
130
+ size = header + 1 + count.to_i * FLOAT
131
+ return if @arriving.bytesize < size
132
+
133
+ key = [instrument.to_sym, pitch == "-" ? nil : pitch.to_i]
134
+ (@bank[key] ||= []) << @arriving.byteslice(header + 1, count.to_i * FLOAT)
135
+ @arriving = @arriving.byteslice(size..)
136
+ end
137
+ end
138
+
139
+ def ready?(tune) = tune.keys.all? { |key| @bank.key?(key) }
140
+
141
+ def start(tune)
142
+ @tune = tune
143
+ @beat = 0.0
144
+ @next = 0
145
+ @bar = nil
146
+ @tempo = tune.beats_per_second(@pace)
147
+ @swell = tune.fade_in.positive? ? 0.0 : 1.0
148
+ @swelling = tune.fade_in
149
+ @gain = @volume
150
+ @feedback = tune.echo
151
+ end
152
+
153
+ # Every voice dies away over the tune's fade out, and the tune is done.
154
+ def stop
155
+ fade = [(@tune.fade_out * Synth::RATE).round, 1].max
156
+ @voices.each { |voice| let_go(voice, @clock, fade) }
157
+ @tune = nil
158
+ end
159
+
160
+ # Lets a voice go at a frame on the band's clock, dying away over so
161
+ # many frames, unless it is already going sooner and faster.
162
+ def let_go(voice, at, fade)
163
+ return if voice.ends && voice.ends <= at && voice.fade <= fade
164
+
165
+ voice.ends = at
166
+ voice.fade = fade
167
+ voice.slope = [voice.slope, -voice.gain / fade].min if voice.slope.negative?
168
+ end
169
+
170
+ # Strikes every note that falls in the next frames, where it falls,
171
+ # going round to where the tune returns to when it reaches the end.
172
+ def advance(frames)
173
+ @tempo += (@tune.beats_per_second(@pace) - @tempo) * [frames.fdiv(Synth::RATE) / CATCH_UP, 1.0].min
174
+ per_frame = @tempo / Synth::RATE
175
+ done = 0.0
176
+ while done < frames
177
+ stretch = [frames - done, (@tune.length - @beat) / per_frame].min
178
+ finish = @beat + stretch * per_frame
179
+ while (note = @tune.notes[@next]) && note.beat < finish
180
+ strike(note, done + (note.beat - @beat) / per_frame, per_frame)
181
+ @next += 1
182
+ end
183
+ @beat = finish
184
+ done += stretch
185
+ go_round if @tune.length - @beat < 1e-9
186
+ end
187
+ end
188
+
189
+ def go_round
190
+ @beat = @tune.again
191
+ @next = @tune.notes.index { |note| note.beat >= @tune.again - 1e-9 }
192
+ @bar = nil
193
+ end
194
+
195
+ # A part the snake earns comes in at the top of the next bar.
196
+ def follow_the_level(beat)
197
+ bar = (beat / @tune.meter + 1e-9).floor
198
+ return if bar == @bar
199
+
200
+ @bar = bar
201
+ @level = @asked_level
202
+ end
203
+
204
+ def strike(note, offset, per_frame)
205
+ follow_the_level(note.beat)
206
+ part = @tune.parts.fetch(note.part)
207
+ return if part.from > @level
208
+
209
+ offset = (offset + @rng.rand * SWAY * Synth::RATE).round
210
+ at = @clock + offset
211
+ @voices.each { |voice| let_go(voice, at, (CHOKE * Synth::RATE).round) if voice.part.equal?(part) } if part.mono
212
+ variants = @bank.fetch([part.instrument, note.pitch])
213
+ loudness = part.gain * note.accent * (0.94 + @rng.rand * 0.12)
214
+ left = loudness * (1 - [part.pan, 0.0].max)
215
+ right = loudness * (1 + [part.pan, 0.0].min)
216
+ ends = part.release && at + (note.beats / per_frame).round
217
+ fade = part.release && (part.release * Synth::RATE).round
218
+ @voices << Voice.new(variants[@rng.rand(variants.size)], -offset, left, right, part, ends, fade, 1.0, 0.0)
219
+ let_go(@voices.first, at, (CHOKE * Synth::RATE).round) if @voices.size > VOICES
220
+ end
221
+
222
+ # Adds a voice's share of the frames to the bus: at its loudness, or
223
+ # dying away once it has been let go. A voice yet to start waits.
224
+ def sound(voice, bus, frames)
225
+ wait = -[voice.at, 0].min
226
+ return voice.at += frames if wait >= frames
227
+
228
+ voice.slope = -1.0 / voice.fade if voice.ends && voice.slope.zero? && voice.ends < @clock + frames
229
+ from = [voice.at, 0].max
230
+ count = [frames - wait, length(voice) - from].min
231
+ count = [count, (voice.gain / -voice.slope).ceil].min if voice.slope.negative?
232
+ samples = voice.samples.unpack("e#{count}", offset: from * FLOAT)
233
+ voice.slope.zero? ? steady(voice, samples, bus, wait) : fading(voice, samples, bus, wait)
234
+ voice.at = from + count
235
+ end
236
+
237
+ def length(voice) = voice.samples.bytesize / FLOAT
238
+
239
+ def steady(voice, samples, bus, wait)
240
+ left = voice.left * voice.gain
241
+ right = voice.right * voice.gain
242
+ out = wait * 2
243
+ index = 0
244
+ last = samples.size
245
+ while index < last
246
+ sample = samples[index]
247
+ bus[out] += sample * left
248
+ bus[out + 1] += sample * right
249
+ out += 2
250
+ index += 1
251
+ end
252
+ end
253
+
254
+ def fading(voice, samples, bus, wait)
255
+ left = voice.left
256
+ right = voice.right
257
+ gain = voice.gain
258
+ slope = voice.slope
259
+ out = wait * 2
260
+ index = 0
261
+ last = samples.size
262
+ while index < last
263
+ sample = samples[index] * gain
264
+ bus[out] += sample * left
265
+ bus[out + 1] += sample * right
266
+ gain += slope
267
+ out += 2
268
+ index += 1
269
+ end
270
+ voice.gain = [gain, 0.0].max
271
+ end
272
+
273
+ # A tune that fades in grows from nothing over its first moments, and
274
+ # goes on growing as it dies away if it is stopped before it is done;
275
+ # what still echoes of the last one is left as it is.
276
+ def swell(bus, frames)
277
+ finish = [@swell + frames.fdiv(Synth::RATE) / @swelling, 1.0].min
278
+ step = (finish - @swell) / frames
279
+ gain = @swell
280
+ index = 0
281
+ last = frames * 2
282
+ while index < last
283
+ bus[index] *= gain
284
+ bus[index + 1] *= gain
285
+ gain += step
286
+ index += 2
287
+ end
288
+ @swell = finish
289
+ end
290
+
291
+ # Two gentle filters a side, closing smoothly as the door shuts.
292
+ def muffle(bus, frames)
293
+ @muffle += (@muffled - @muffle) * [frames.fdiv(Synth::RATE) / DOOR, 1.0].min
294
+ return @door.fill(0.0) if @muffle < 0.002
295
+
296
+ pull = 1 - Math.exp(-Synth::TURN * OPEN * (SHUT / OPEN)**@muffle / Synth::RATE)
297
+ quieter = 1 - (1 - MUFFLED) * @muffle
298
+ left_low, left, right_low, right = @door
299
+ index = 0
300
+ last = frames * 2
301
+ while index < last
302
+ left_low += pull * (bus[index] - left_low)
303
+ left += pull * (left_low - left)
304
+ right_low += pull * (bus[index + 1] - right_low)
305
+ right += pull * (right_low - right)
306
+ bus[index] = left * quieter
307
+ bus[index + 1] = right * quieter
308
+ index += 2
309
+ end
310
+ @door = [left_low, left, right_low, right]
311
+ end
312
+
313
+ def echo(bus, frames)
314
+ feedback = @feedback
315
+ 2.times do |side|
316
+ line = @lines[side]
317
+ head = @heads[side]
318
+ memory = @memories[side]
319
+ index = side
320
+ last = frames * 2
321
+ while index < last
322
+ memory += TONE * (line[head] - memory)
323
+ sample = bus[index] + memory * feedback
324
+ bus[index] = sample
325
+ line[head] = sample
326
+ head += 1
327
+ head = 0 if head == line.size
328
+ index += 2
329
+ end
330
+ @heads[side] = head
331
+ @memories[side] = memory
332
+ end
333
+ end
334
+
335
+ # The music into the stream, its loudness easing toward the volume
336
+ # while there is music to play, and left as it is under a dying echo.
337
+ def lay(out, bus, frames)
338
+ target = @tune || @voices.any? ? @volume : @gain
339
+ turn = frames.fdiv(Synth::RATE) / TURN_UP
340
+ finish = @gain < target ? [@gain + turn, target].min : [@gain - turn, target].max
341
+ step = (finish - @gain) / frames
342
+ gain = @gain
343
+ index = 0
344
+ last = frames * 2
345
+ while index < last
346
+ out[index] += bus[index] * gain
347
+ out[index + 1] += bus[index + 1] * gain
348
+ gain += step
349
+ index += 2
350
+ end
351
+ @gain = finish
352
+ end
353
+ end
354
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The instruments the songs are played on, each a recipe for one note at
5
+ # a pitch, or for one stroke of a drum. Every one is struck or plucked
6
+ # and dies away by itself as a real one does, so no note is ever heard
7
+ # held at one loudness, which in a made sound is heard as a beep.
8
+ #
9
+ # Struck things ring with a few partials: a music box's tine, a celesta,
10
+ # glass, a marimba's bar, a vibraphone's with the motor turning in its
11
+ # tubes, and a kalimba's. Strings are plucked and left to ring: a harp,
12
+ # a nylon guitar, a koto, and an upright bass.
13
+ module Instruments
14
+ # A music box's tine rings with a second, slightly out of tune, so the
15
+ # note shimmers, and the first overtone of a tine is far above it.
16
+ MUSIC_BOX = [[1.0, 1.0, 0.7], [1.0017, 0.22, 0.7], [2.0, 0.07, 0.3], [6.27, 0.22, 0.05], [17.55, 0.04, 0.012]].freeze
17
+ CELESTA = [[1.0, 1.0, 0.45], [2.0, 0.25, 0.22], [3.0, 0.08, 0.12], [4.2, 0.04, 0.06]].freeze
18
+ MARIMBA = [[1.0, 1.0, 0.36], [3.93, 0.2, 0.07], [9.4, 0.04, 0.02]].freeze
19
+ # An aluminium bar is tuned with its first overtone two octaves up, and
20
+ # soft mallets barely wake it.
21
+ VIBES = [[1.0, 1.0, 1.0], [4.0, 0.12, 0.2], [9.92, 0.025, 0.05]].freeze
22
+ # A kalimba's tine has a plink far above the note that is gone at
23
+ # once, and its box adds an octave.
24
+ KALIMBA = [[1.0, 1.0, 0.5], [2.0, 0.05, 0.25], [5.4, 0.28, 0.04], [13.1, 0.07, 0.012]].freeze
25
+ TRIANGLE = [[1.0, 1.0, 0.35], [2.76, 0.55, 0.3], [4.1, 0.45, 0.26], [5.9, 0.3, 0.2], [7.3, 0.2, 0.16]].freeze
26
+
27
+ module_function
28
+
29
+ # A note at a MIDI pitch on an instrument, or a stroke of a drum, which
30
+ # has none.
31
+ def make(instrument, pitch, rng)
32
+ frequency = pitch && Tune.frequency(pitch)
33
+ case instrument
34
+ when :music_box then Synth.strike(frequency, MUSIC_BOX, damped: (frequency / 523.25)**0.5)
35
+ when :celesta then Synth.strike(frequency, CELESTA)
36
+ when :glass then Synth.strike(frequency, Synth::GLASS)
37
+ when :marimba then marimba(frequency, rng)
38
+ when :vibes then vibes(frequency, rng)
39
+ when :kalimba then kalimba(frequency, rng)
40
+ when :harp then harp(frequency, rng)
41
+ when :guitar then guitar(frequency, rng)
42
+ when :koto then koto(frequency, rng)
43
+ when :bass then bass(frequency, rng)
44
+ else drum(instrument, rng)
45
+ end
46
+ end
47
+
48
+ def drum(instrument, rng)
49
+ case instrument
50
+ when :kick then kick(rng)
51
+ when :brush then brush(rng)
52
+ when :shaker then Synth.swell(Synth.noise(rng, decay: 0.02, low: 5000.0, high: 13_000.0), 0.008)
53
+ when :hat then Synth.noise(rng, decay: 0.022, low: 6500.0, high: 14_000.0)
54
+ when :rim then rim(rng)
55
+ when :woodblock then woodblock(rng)
56
+ when :triangle then triangle(rng)
57
+ when :frame_drum then frame_drum(rng)
58
+ end
59
+ end
60
+
61
+ # How a string's setting runs from the bass to the treble, evenly in
62
+ # pitch: low strings ring longer, high ones are smoothed less.
63
+ def by_pitch(frequency, low, high)
64
+ along = (Math.log(frequency / 65.0) / Math.log(1568.0 / 65.0)).clamp(0.0, 1.0)
65
+ low + (high - low) * along
66
+ end
67
+
68
+ def marimba(frequency, rng)
69
+ bar = Synth.strike(frequency, MARIMBA, damped: (frequency / 262.0)**0.7)
70
+ Synth.layer([0, bar], [0, Synth.noise(rng, decay: 0.002, low: 200.0, high: 2500.0), 0.04])
71
+ end
72
+
73
+ # The motor's discs turning in the tubes make the bar breathe.
74
+ def vibes(frequency, rng)
75
+ bar = Synth.strike(frequency, VIBES, damped: (frequency / 523.25)**0.4)
76
+ tick = Synth.noise(rng, decay: 0.003, low: 1500.0, high: 6000.0)
77
+ Synth.waver(Synth.layer([0, bar], [0, tick, 0.03]), speed: 5.2, depth: 0.32, after: 0.05)
78
+ end
79
+
80
+ def kalimba(frequency, rng)
81
+ tine = Synth.strike(frequency, KALIMBA, damped: (frequency / 523.25)**0.5)
82
+ Synth.layer([0, tine], [0, Synth.noise(rng, decay: 0.002, low: 300.0, high: 3000.0), 0.04])
83
+ end
84
+
85
+ # Plucked near the end of the string for a round tone with body, and
86
+ # dulled at the top so it never fizzes.
87
+ def harp(frequency, rng)
88
+ string = Synth.pluck(frequency, rng, decay: by_pitch(frequency, 5.0, 1.4), brightness: 0.25, position: 0.14,
89
+ stretch: by_pitch(frequency, 0.5, 0.1))
90
+ Synth.darken(string, 3500.0)
91
+ end
92
+
93
+ def guitar(frequency, rng)
94
+ string = Synth.pluck(frequency, rng, decay: by_pitch(frequency, 4.0, 1.5), brightness: 0.45, position: 0.16,
95
+ stretch: by_pitch(frequency, 0.5, 0.1))
96
+ Synth.darken(string, 5000.0)
97
+ end
98
+
99
+ # A silk string picked hard by the bridge, bright and long.
100
+ def koto(frequency, rng)
101
+ Synth.pluck(frequency, rng, decay: by_pitch(frequency, 4.0, 1.8), brightness: 0.85, position: 0.09,
102
+ stretch: by_pitch(frequency, 0.45, 0.06))
103
+ end
104
+
105
+ # A dark pluck in the middle of a thick string, and the thump of the
106
+ # finger against the fingerboard.
107
+ def bass(frequency, rng)
108
+ string = Synth.darken(Synth.pluck(frequency, rng, decay: 2.0, brightness: 0.07, position: 0.3, stretch: 0.5), 900.0)
109
+ Synth.layer([0, string], [0, Synth.noise(rng, decay: 0.01, low: 40.0, high: 200.0), 0.35])
110
+ end
111
+
112
+ def kick(rng)
113
+ body = Synth.strike(55.0, [[1.0, 1.0, 0.11]], bend: 1.3, settle: 0.022)
114
+ Synth.layer([0, body], [0, Synth.noise(rng, decay: 0.0015, low: 800.0, high: 5000.0), 0.06])
115
+ end
116
+
117
+ def brush(rng)
118
+ swish = Synth.swell(Synth.noise(rng, decay: 0.045, low: 1200.0, high: 7000.0), 0.006)
119
+ Synth.layer([0, swish], [0, Synth.strike(190.0, [[1.0, 1.0, 0.02]]), 0.3])
120
+ end
121
+
122
+ # A stick laid across the snare and its rim struck: a knock and a tick.
123
+ def rim(rng)
124
+ knock = Synth.strike(420.0, [[1.0, 1.0, 0.012], [1.9, 0.5, 0.008]])
125
+ Synth.layer([0, knock], [0, Synth.noise(rng, decay: 0.004, low: 1500.0, high: 8000.0), 0.35])
126
+ end
127
+
128
+ def woodblock(rng)
129
+ block = Synth.strike(760.0, [[1.0, 1.0, 0.025], [2.6, 0.3, 0.008]])
130
+ Synth.layer([0, block], [0, Synth.noise(rng, decay: 0.0015, low: 1000.0, high: 6000.0), 0.2])
131
+ end
132
+
133
+ def triangle(rng)
134
+ Synth.layer([0, Synth.strike(1250.0, TRIANGLE)], [0, Synth.noise(rng, decay: 0.002, low: 4000.0, high: 12_000.0), 0.15])
135
+ end
136
+
137
+ # A hand on a wide frame drum: a low note that sags, and the slap.
138
+ def frame_drum(rng)
139
+ body = Synth.strike(92.0, [[1.0, 1.0, 0.18], [1.5, 0.3, 0.08], [2.4, 0.12, 0.05]], bend: 0.12, settle: 0.03)
140
+ Synth.layer([0, body], [0, Synth.noise(rng, decay: 0.006, low: 300.0, high: 3000.0), 0.25])
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Mixes whatever is playing into the stream: the band's music, if there
5
+ # is a band, and over it the sounds. Each sound is a voice with its own
6
+ # loudness on the left and on the right, played from its start until it
7
+ # runs out. Where voices pile up past what the speaker can take, the
8
+ # peaks are rounded off rather than clipped.
9
+ class Mixer
10
+ Voice = Struct.new(:samples, :left, :right, :at)
11
+
12
+ VOICES = 16
13
+ # Under this nothing is touched; over it, peaks bend toward full scale.
14
+ KNEE = 0.6
15
+
16
+ def initialize(band = nil)
17
+ @voices = []
18
+ @band = band
19
+ end
20
+
21
+ def add(samples, left, right)
22
+ @voices << Voice.new(samples, left, right, 0)
23
+ @voices.shift if @voices.size > VOICES
24
+ end
25
+
26
+ def quiet? = @voices.empty? && (@band.nil? || @band.quiet?)
27
+
28
+ # The next frames as the stream takes them: a float for the left, a
29
+ # float for the right, and so on.
30
+ def mix(frames)
31
+ return "\0" * (frames * Pulse::FRAME) if quiet?
32
+
33
+ out = Array.new(frames * 2, 0.0)
34
+ @band&.play(out, frames)
35
+ @voices.each { |voice| play(voice, out, frames) }
36
+ @voices.reject! { |voice| voice.at == voice.samples.size }
37
+ out.map! { |sample| sample.abs > KNEE ? round_off(sample) : sample }.pack("e*")
38
+ end
39
+
40
+ private
41
+
42
+ def play(voice, out, frames)
43
+ samples, left, right, start = voice.to_a
44
+ count = [frames, samples.size - start].min
45
+ index = 0
46
+ while index < count
47
+ sample = samples[start + index]
48
+ out[index * 2] += sample * left
49
+ out[index * 2 + 1] += sample * right
50
+ index += 1
51
+ end
52
+ voice.at += count
53
+ end
54
+
55
+ def round_off(sample)
56
+ over = (sample.abs - KNEE) / (1 - KNEE)
57
+ (KNEE + (1 - KNEE) * Math.tanh(over)) * (sample.positive? ? 1 : -1)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # What a scene wants heard of the music: a tune by name, or none; the
5
+ # snake's pace, its speed against the slowest start, which a game's tune
6
+ # quickens with; the level the multiplier has reached, which brings parts
7
+ # in, nothing in the menus; and whether it is held behind the pause panel.
8
+ Music = Data.define(:tune, :pace, :level, :muffled) do
9
+ def initialize(tune:, pace: 1.0, level: 0, muffled: false) = super
10
+
11
+ def muffle = with(muffled: true)
12
+ end
13
+
14
+ Music::NONE = Music.new(tune: :none)
15
+ end