-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathengine.rb
More file actions
90 lines (69 loc) · 1.79 KB
/
engine.rb
File metadata and controls
90 lines (69 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require_relative 'lib/surface'
require 'artoo'
require 'unimidi'
class DrumSet < Artoo::MainRobot
connection :leapmotion, adaptor: :leapmotion, port: '127.0.0.1:6437'
device :leapmotion, driver: :leapmotion
work do
on leapmotion, frame: :on_frame
on leapmotion, close: :on_close
end
def initialize
@bass_drum = Surface.new(:kick)
@snare = Surface.new(:snare)
@hi_hat = Surface.new(:hat)
@drums = [@snare, @bass_drum, @hi_hat]
@finger = nil
@previous = {}
super
end
def on_frame(*args)
frame = args[1]
return if frame.pointables.empty?
set_finger_from(frame)
if @finger
x_position = @finger.tipPosition[0]
y_position = @finger.tipPosition[1]
y_velocity = @finger.tipVelocity[1]
end
puts "*" * (y_position/10).to_i
if is_a_hit?(y_position)
drum_for(x_position, y_velocity)
end
@previous[:y_position] = y_position
end
def on_close(*args)
puts args
end
private
def set_finger_from(frame)
@finger = frame.pointables.reduce(nil) do |old, point|
if old == nil || (point.tipPosition[1] < old.tipPosition[1]) && point.length > 40
point
else
old
end
end
end
def is_a_hit?(y_position)
if @previous[:y_position] && @previous[:y_position] > 150 && y_position < 150
puts "#{y_position} -- #{@previous[:y_position]}"
true
else
false
end
end
def drum_for(x_position, y_velocity)
drum_hit = @drums.detect do |drum|
x_position > drum.left_boundary && x_position < drum.right_boundary
end
if drum_hit
determine_volume_from(y_velocity)
drum_hit.play(@volume)
end
end
def determine_volume_from(velocity)
@volume = [[ 0, (velocity.abs/30).to_i ].max, 100].min
end
end
DrumSet.work!