Skip to content

Commit

Permalink
avoid using global variable $beat
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Dec 16, 2019
1 parent 3444405 commit 15104f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
7 changes: 4 additions & 3 deletions external_library/java/minim/drum_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class DrumMachine < Processing::App
java_import 'ddf.minim.ugens.Sampler'
attr_reader :minim, :out, :kick, :snare, :hat, :bpm, :buttons
attr_reader :kikRow, :snrRow, :hatRow
attr_accessor :beat
def setup
sketch_title 'Drum Machine'
minim = Minim.new(self)
Expand All @@ -23,7 +24,7 @@ def setup
@kikRow = Array.new(16, false)
@buttons = []
@bpm = 120
$beat = 0
@beat = 0
# load all of our samples, using 4 voices for each.
# this will help ensure we have enough voices to handle even
# very fast tempos.
Expand All @@ -50,9 +51,9 @@ def draw
# text(frameRate, width - 60, 20)
buttons.each(&:draw)
stroke(128)
($beat % 4).zero? ? fill(200, 0, 0) : fill(0, 200, 0)
(beat % 4).zero? ? fill(200, 0, 0) : fill(0, 200, 0)
# beat marker
rect(10 + $beat * 24, 35, 14, 9)
rect(10 + beat * 24, 35, 14, 9)
end

def mouse_pressed
Expand Down
11 changes: 6 additions & 5 deletions external_library/java/minim/library/tick/lib/tick.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# frozen_string_literal: true

java_import 'ddf.minim.ugens.Instrument'

# class Tick can access app variables by including Processing::App module
# But we must use instance variable to set the beat
class Tick
include Instrument
include Processing::Proxy

def noteOn(_dur)
hat.trigger if hatRow[$beat]
snare.trigger if snrRow[$beat]
kick.trigger if kikRow[$beat]
hat.trigger if hatRow[beat]
snare.trigger if snrRow[beat]
kick.trigger if kikRow[beat]
end

def noteOff
# next beat
$beat = ($beat + 1) % 16
Processing.app.beat = (beat + 1) % 16
# set the new tempo
out.setTempo(bpm)
# play this again right now, with a sixteenth note duration
Expand Down

0 comments on commit 15104f0

Please sign in to comment.