-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller-boilerplate.rb
More file actions
155 lines (137 loc) · 4.63 KB
/
controller-boilerplate.rb
File metadata and controls
155 lines (137 loc) · 4.63 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#####################################################
# _____ _ ____ _ #
# / ___/____ ____ (_)____ / __ \(_) ä
# \__ \/ __ \/ __ \/ / ___/ / /_/ / / #
# ___/ / /_/ / / / / / /__ / ____/ / #
# /____/\____/_/ /_/_/\___/ /_/ /_/ #
# #
# ______ __ ____ #
# / ____/___ ____ / /__________ / / /__ _____ #
# / / / __ \/ __ \/ __/ ___/ __ \/ / / _ \/ ___/ #
# / /___/ /_/ / / / / /_/ / / /_/ / / / __/ / #
# \____/\____/_/ /_/\__/_/ \____/_/_/\___/_/ #
# #
# Font: http://www.network-science.de/ascii/ #
#####################################################
#####################################################
# #
# #
# Configuration you might need to adapt #
# #
# You can see where MIDI messages arrive in the #
# Cues panel of Sonic Pi. #
#####################################################
midi_port = "/midi:loopmidi_port_0"
# Define slider ranges here after changing them inside
# the controller frontend
slider_ranges = [
Hash["min" => 0, "max" => 127, "step" => 1], # slider 0
Hash["min" => 0, "max" => 127, "step" => 1], # slider 1
Hash["min" => 0, "max" => 127, "step" => 1], # slider 2
Hash["min" => 0, "max" => 127, "step" => 1], # slider 3
Hash["min" => 0, "max" => 127, "step" => 1], # slider 4
Hash["min" => 0, "max" => 127, "step" => 1], # slider 5
Hash["min" => 0, "max" => 127, "step" => 1] # slider 6
]
# If you want to use samples, e.g., for the drums,
# configure their path here
samples = "\\path\\to\\drum-samples\\"
bass = samples + "bassdrum.wav"
snare = samples + "Snare.wav"
crash = samples + "crash_cymbal.wav"
tom1 = samples + "tomtom1.wav"
tom2 = samples + "tomtom2.wav"
tom3 = samples + "floortom.wav"
ride = samples + "ride_cymbal.wav"
#####################################################
# End of configuration #
#####################################################
drums_channel = "1"
keyboard_channel = "2"
button_channel = "3"
slider_channel = "4"
button_midi = midi_port + ":" + button_channel + "/note_on"
slider_midi = midi_port + ":" + slider_channel + "/note_on"
drums_midi = midi_port + ":" + drums_channel + "/note_on"
keyboard_midi = midi_port + ":" + keyboard_channel + "/note_on"
# Initial values of slider and button values
sliders = Array.new(100, 0)
buttons = Array.new(36, false)
# Scales a value from [0, 127] to [min, max] and
# rounds to next valid step
def scale_linear(min, max, step, value)
scaled = min + (value / 127.0) * (max - min)
stepped = scaled / step
rounded = stepped.round * step
end
# Update loops that keep slider and button states up-to-date
live_loop :button_update do
note, velocity = sync button_midi
if velocity > 0
buttons[note] = true
else
buttons[note] = false
end
puts "Received button update", note, velocity, buttons[note]
end
live_loop :slider_update do
note, velocity = sync slider_midi
puts "Received slider update", note, velocity
# Scale, if range is defined
scaled = velocity
if note < slider_ranges.size
r = slider_ranges[note]
scaled = scale_linear r["min"], r["max"], r["step"], velocity
end
puts "Scaled slider value to #{scaled}"
sliders[note] = scaled
end
# Live loops for instruments
live_loop :keyboard do
use_real_time
use_synth :piano
note, velocity = sync keyboard_midi
puts "Received keyboard note", note, velocity
play note
sleep 0.1
end
live_loop :drum do
use_real_time
note, velocity, channel = sync drums_midi
puts "Received drum hit", note, velocity
if note == 0
sample crash, amp: 50
elsif note == 1
sample snare, amp: 50
elsif note == 2
sample tom3, amp: 50
elsif note == 3
sample tom1, amp: 50
elsif note == 4
sample bass, amp: 50
elsif note == 5
sample tom2, amp: 50
elsif note == 6
sample ride, amp: 50
end
sleep 0.01
end
#####################################################
# End of Sonic Pi Controller boilerplate #
#####################################################
# Example for using a button
live_loop :example_loop do
use_synth :piano
if buttons[0]
# Do anything here
play 60
sleep 1
end
end
# Example for using a slider
live_loop :example_loop do
use_synth :piano
# Use slider values as parameter
play 60, amp: sliders[0]
sleep 1
end