Skip to content

Commit 15104f0

Browse files
committed
avoid using global variable $beat
1 parent 3444405 commit 15104f0

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

external_library/java/minim/drum_machine.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class DrumMachine < Processing::App
1414
java_import 'ddf.minim.ugens.Sampler'
1515
attr_reader :minim, :out, :kick, :snare, :hat, :bpm, :buttons
1616
attr_reader :kikRow, :snrRow, :hatRow
17+
attr_accessor :beat
1718
def setup
1819
sketch_title 'Drum Machine'
1920
minim = Minim.new(self)
@@ -23,7 +24,7 @@ def setup
2324
@kikRow = Array.new(16, false)
2425
@buttons = []
2526
@bpm = 120
26-
$beat = 0
27+
@beat = 0
2728
# load all of our samples, using 4 voices for each.
2829
# this will help ensure we have enough voices to handle even
2930
# very fast tempos.
@@ -50,9 +51,9 @@ def draw
5051
# text(frameRate, width - 60, 20)
5152
buttons.each(&:draw)
5253
stroke(128)
53-
($beat % 4).zero? ? fill(200, 0, 0) : fill(0, 200, 0)
54+
(beat % 4).zero? ? fill(200, 0, 0) : fill(0, 200, 0)
5455
# beat marker
55-
rect(10 + $beat * 24, 35, 14, 9)
56+
rect(10 + beat * 24, 35, 14, 9)
5657
end
5758

5859
def mouse_pressed

external_library/java/minim/library/tick/lib/tick.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# frozen_string_literal: true
22

33
java_import 'ddf.minim.ugens.Instrument'
4-
4+
# class Tick can access app variables by including Processing::App module
5+
# But we must use instance variable to set the beat
56
class Tick
67
include Instrument
78
include Processing::Proxy
89

910
def noteOn(_dur)
10-
hat.trigger if hatRow[$beat]
11-
snare.trigger if snrRow[$beat]
12-
kick.trigger if kikRow[$beat]
11+
hat.trigger if hatRow[beat]
12+
snare.trigger if snrRow[beat]
13+
kick.trigger if kikRow[beat]
1314
end
1415

1516
def noteOff
1617
# next beat
17-
$beat = ($beat + 1) % 16
18+
Processing.app.beat = (beat + 1) % 16
1819
# set the new tempo
1920
out.setTempo(bpm)
2021
# play this again right now, with a sixteenth note duration

0 commit comments

Comments
 (0)