-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprmc-1-type-0.rb
374 lines (315 loc) · 10.4 KB
/
prmc-1-type-0.rb
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
=begin
MIDI Controller PRMC-1 (type-0)
===============================
**Version 0.2.1 (2025-04-26)**
MIDI Controller using PicoRuby/R2P2 by ISGK Instruments (Ryo Ishigaki)
Required Software
-----------------
- R2P2 0.3.0 https://github.com/picoruby/R2P2/releases/tag/0.3.0
Required Hardware
-----------------
- Raspberry Pi Pico https://www.raspberrypi.com/products/raspberry-pi-pico/
- Grove Shield for Pi Pico https://wiki.seeedstudio.com/Grove-Starter-Kit-for-Raspberry-Pi-Pico/ (5V)
- M5Stack Unit 8Angle https://docs.m5stack.com/en/unit/8angle (I2C1)
- M5Stack Unit MIDI https://docs.m5stack.com/en/unit/Unit-MIDI (Separate Mode, UART1)
Usage
-----
- CH1 Knob: Root of Step 1 Chord, 1 - 14 degree (C3 - B4 in C Major Scale)
- CH2 Knob: Root of Step 2 Chord, ditto
- CH3 Knob: Root of Step 3 Chord, ditto
- CH4 Knob: Root of Step 4 Chord, ditto
- CH5 Knob: Arpeggio Pattern, 1 - 6
- Pattern 1: Triad, Up, 8th Note
- Pattern 2: Triad, Up & Down, 8th Note
- Pattern 3: 7th Chord, Up, 8th Note
- Pattern 4: 7th Chord, Up & Down, 8th Note
- Pattern 5: Root + 4th + 5th, Up, 8th Note
- Pattern 6: Root + 4th + 5th, Up & Down, 8th Note
- CH6 Knob: Brightness (Cutoff), 0 - 127
- CH7 Knob: Harmonic Content (Resonance), 0 - 127
- CH8 Knob: BPM, 60 - 240
- SW Switch: 0 to Stop Sequencer, 1 to Start Sequencer
Change History
--------------
- Version 0.2.1 (2025-04-26): Fix comment
- Version 0.2.0 (2025-04-26): Change to send clocks while not playing; Add SEND_START_STOP option
- Version 0.1.2 (2025-04-22): Easier to modify for step division 16 (16th Note)
- Version 0.1.1 (2025-04-16): Improve style
- Version 0.1.0 (2025-04-13): Initial release (used in RubyKaigi 2025 LT)
License
-------
MIDI Controller PRMC-1 (type-0) by ISGK Instruments (Ryo Ishigaki) is marked with CC0 1.0.
To view a copy of this license, visit https://creativecommons.org/publicdomain/zero/1.0/
=end
require 'i2c'
require 'uart'
# options
MIDI_CHANNEL = 1
TRANSPOSE = 0
GATE_TIME = 3 # min: 1, max: 6
SEND_START_STOP = true
NOTE_ON_VELOCITY = 100
NOTE_OFF_VELOCITY = 64
LED_ON_VALUE = 1
FOR_SAM2695 = true
class M5UnitAngle8
# refs https://github.com/m5stack/M5Unit-8Angle
ANGLE8_I2C_ADDR = 0x43
ANGLE8_ANALOG_INPUT_8B_REG = 0x10
ANGLE8_DIGITAL_INPUT_REG = 0x20
ANGLE8_RGB_24B_REG = 0x30
def initialize(i2c:)
@i2c = i2c
end
def prepare_to_get_analog_input(ch)
@i2c.write(ANGLE8_I2C_ADDR, ANGLE8_ANALOG_INPUT_8B_REG + ch)
rescue
retry
end
def get_analog_input
@i2c.read(ANGLE8_I2C_ADDR, 1).bytes[0]
rescue
retry
end
def prepare_to_get_digital_input
@i2c.write(ANGLE8_I2C_ADDR, ANGLE8_DIGITAL_INPUT_REG)
rescue
retry
end
def get_digital_input
@i2c.read(ANGLE8_I2C_ADDR, 1).bytes[0]
rescue
retry
end
def set_red_led(ch, value)
@i2c.write(ANGLE8_I2C_ADDR, ANGLE8_RGB_24B_REG + ch * 4 + 0, value)
rescue
retry
end
def set_green_led(ch, value)
@i2c.write(ANGLE8_I2C_ADDR, ANGLE8_RGB_24B_REG + ch * 4 + 1, value)
rescue
retry
end
def set_blue_led(ch, value)
@i2c.write(ANGLE8_I2C_ADDR, ANGLE8_RGB_24B_REG + ch * 4 + 2, value)
rescue
retry
end
end
class MIDI
# refs https://github.com/FortySevenEffects/arduino_midi_library
def initialize(uart:)
@uart = uart
end
def send_note_on(note_number, velocity, channel)
@uart.write((0x90 + channel - 1).chr + note_number.chr + velocity.chr)
end
def send_note_off(note_number, velocity, channel)
@uart.write((0x80 + channel - 1).chr + note_number.chr + velocity.chr)
end
def send_control_change(control_number, control_value, channel)
@uart.write((0xB0 + channel - 1).chr + control_number.chr + control_value.chr)
end
def send_program_change(program_number, channel)
@uart.write((0xC0 + channel - 1).chr + program_number.chr)
end
def send_clock
@uart.write(0xF8.chr)
end
def send_start
@uart.write(0xFA.chr)
end
def send_stop
@uart.write(0xFC.chr)
end
end
class PRMC1Core
NUMBER_OF_STEPS = 4
CLOCKS_PER_STEP = 96
def initialize(midi:, midi_channel:)
@midi = midi
@midi_channel = midi_channel
@bpm = 120
@root_degrees = []
@root_degrees_candidate = []
@arpeggio_intervals = []
@arpeggio_intervals_candidate = []
@step_division = 8
@step_division_candidate = 8
@scale_notes = [-1, 48, 50, 52, 53, 55, 57, 59,
60, 62, 64, 65, 67, 69, 71,
72, 74, 76, 77, 79, 81, 83]
@playing = false
@playing_note = -1
@step = 0
@clock = 0
@usec = Time.now.usec
@usec_remain = 0
@step_status_bits = 0x0
@parameter_status_bits = 0x0
end
def process_sequencer
usec = Time.now.usec
@usec_remain += (usec - @usec + 1_000_000) % 1_000_000
@usec = usec
usec_per_clock = 2_500_000 / @bpm
while @usec_remain >= usec_per_clock
@usec_remain -= usec_per_clock
receive_midi_clock
end
end
def change_parameter(key, value)
case key
when 0..3
@root_degrees_candidate[key] = (value * (14 - 1) * 2 + 127) / 254 + 1
set_parameter_status((@root_degrees_candidate[key] - 1) % 7 + 1)
when 4
arpeggio_pattern = (value * (6 - 1) * 2 + 127) / 254 + 1
case arpeggio_pattern
when 1
@arpeggio_intervals_candidate = [1, 3, 5, 7, 1, 3, 5, 7]
@step_division_candidate = 8
when 2
@arpeggio_intervals_candidate = [1, 3, 5, 7, 5, 3, 1, 3]
@step_division_candidate = 8
when 3
@arpeggio_intervals_candidate = [1, 3, 5, 1, 3, 5, 1, 3]
@step_division_candidate = 8
when 4
@arpeggio_intervals_candidate = [1, 3, 5, 3, 1, 3, 5, 3]
@step_division_candidate = 8
when 5
@arpeggio_intervals_candidate = [1, 4, 5, 1, 4, 5, 1, 4]
@step_division_candidate = 8
when 6
@arpeggio_intervals_candidate = [1, 4, 5, 4, 1, 4, 5, 4]
@step_division_candidate = 8
end
set_parameter_status(arpeggio_pattern)
when 5
# filter cutoff
@midi.send_control_change(0x4A, value, @midi_channel)
if FOR_SAM2695
@midi.send_control_change(0x63, 0x01, @midi_channel)
@midi.send_control_change(0x62, 0x20, @midi_channel)
@midi.send_control_change(0x06, value, @midi_channel)
end
set_parameter_status((value * (7 - 1) * 2 + 127) / 254 + 1)
when 6
# filter resonance
@midi.send_control_change(0x47, value, @midi_channel)
if FOR_SAM2695
@midi.send_control_change(0x63, 0x01, @midi_channel)
@midi.send_control_change(0x62, 0x21, @midi_channel)
@midi.send_control_change(0x06, value, @midi_channel)
end
set_parameter_status((value * (7 - 1) * 2 + 127) / 254 + 1)
when 7
@bpm = value * 2 - 8
@bpm = 60 if @bpm < 60
@bpm = 240 if @bpm > 240
set_parameter_status((value * (7 - 1) * 2 + 127) / 254 + 1)
when 8
if value > 0
@midi.send_start if SEND_START_STOP
@playing = true
@playing_note = -1
@step = NUMBER_OF_STEPS - 1
@clock = CLOCKS_PER_STEP - 1
else
@midi.send_stop if SEND_START_STOP
@playing = false
@midi.send_note_off(@playing_note, NOTE_OFF_VELOCITY, @midi_channel) if @playing_note != -1
set_step_status(0)
end
end
end
def step_status_bits
@step_status_bits
end
def parameter_status_bits
@parameter_status_bits
end
# private
def receive_midi_clock
@midi.send_clock
return if !@playing
@clock += 1
if @clock == CLOCKS_PER_STEP
@clock = 0
@root_degrees_candidate.each_with_index {|item, index| @root_degrees[index] = item }
@arpeggio_intervals_candidate.each_with_index {|item, index| @arpeggio_intervals[index] = item }
@step_division = @step_division_candidate
@step += 1
@step = 0 if @step == NUMBER_OF_STEPS
set_step_status(@step + 1)
end
playing_note_old = @playing_note
if @clock % (CLOCKS_PER_STEP / @step_division) == 0
root = @root_degrees[@step]
interval = @arpeggio_intervals[@clock / (CLOCKS_PER_STEP / @step_division) % @arpeggio_intervals.length]
@playing_note = -1
@playing_note = @scale_notes[root + interval - 1] + TRANSPOSE if root > 0 && interval > 0
@midi.send_note_on(@playing_note, NOTE_ON_VELOCITY, @midi_channel) if @playing_note != -1
end
if @clock % (CLOCKS_PER_STEP / @step_division) ==
CLOCKS_PER_STEP * GATE_TIME / 6 / @step_division % (CLOCKS_PER_STEP / @step_division)
@midi.send_note_off(playing_note_old, NOTE_OFF_VELOCITY, @midi_channel) if playing_note_old != -1
end
end
def set_step_status(value)
@step_status_bits = [0x0, 0x1, 0x2, 0x4, 0x8].at(value)
end
def set_parameter_status(value)
@parameter_status_bits = [0x0, 0x1, 0x3, 0x2, 0x6, 0x4, 0xC, 0x8].at(value)
end
end
# setup
led_builtin = GPIO.new(25, GPIO::OUT)
led_builtin.write(1)
i2c1 = I2C.new(unit: :RP2040_I2C1, frequency: 20_000, sda_pin: 6, scl_pin: 7)
angle8 = M5UnitAngle8.new(i2c: i2c1)
uart1 = UART.new(unit: :RP2040_UART1, txd_pin: 4, rxd_pin: 5, baudrate: 31_250)
midi = MIDI.new(uart: uart1)
prmc_1_core = PRMC1Core.new(midi: midi, midi_channel: MIDI_CHANNEL)
current_inputs = [nil, nil, nil, nil, nil, nil, nil, nil, 0]
if FOR_SAM2695
midi.send_program_change(0x51, MIDI_CHANNEL)
midi.send_control_change(0x63, 0x01, MIDI_CHANNEL)
midi.send_control_change(0x62, 0x66, MIDI_CHANNEL)
midi.send_control_change(0x06, 0x60, MIDI_CHANNEL)
end
# loop
loop do
(0..7).each do |ch|
prmc_1_core.process_sequencer
angle8.prepare_to_get_analog_input(ch)
prmc_1_core.process_sequencer
analog_input = angle8.get_analog_input
if current_inputs[ch].nil? ||
analog_input > current_inputs[ch] + 1 ||
analog_input < current_inputs[ch] - 1
current_inputs[ch] = analog_input
prmc_1_core.change_parameter(ch, 127 - current_inputs[ch] / 2)
end
end
begin
prmc_1_core.process_sequencer
angle8.prepare_to_get_digital_input
prmc_1_core.process_sequencer
digital_input = angle8.get_digital_input
if current_inputs[8] != digital_input
current_inputs[8] = digital_input
prmc_1_core.change_parameter(8, current_inputs[8])
end
end
(0..3).each do |ch|
prmc_1_core.process_sequencer
angle8.set_blue_led(ch, (prmc_1_core.step_status_bits >> ch & 0x01) * LED_ON_VALUE)
end
(4..7).each do |ch|
prmc_1_core.process_sequencer
angle8.set_green_led(ch, (prmc_1_core.parameter_status_bits << 4 >> ch & 0x01) * LED_ON_VALUE)
end
end