-
Notifications
You must be signed in to change notification settings - Fork 0
/
microbit-reader.rb
184 lines (155 loc) · 4 KB
/
microbit-reader.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
require 'serialport'
require 'json'
require "unimidi"
require 'platform'
PORT_SEARCH = '/dev/cu.usbmodem*'
case Platform::IMPL
when :linux
PORT_SEARCH = '/dev/ttyACM*'
when :mac
PORT_SEARCH = '/dev/cu.usbmodem*'
end
files = Dir[PORT_SEARCH]
if files.length > 1
puts "What port is your Microbit on?\nChoose a number\n"
files.each_with_index do |f, index|
puts "#{index + 1}: #{f}"
end
choice = gets
choice = choice.chomp.to_i
exit
PORT = files[choice]
else
PORT = files.first
end
puts "PORT: #{PORT}"
BAUD = 115200
serial = SerialPort.new(PORT, BAUD, 8, 1, SerialPort::NONE)
UniMIDI::Output.all.each { |d| puts "#{d.inspect}" }
output = UniMIDI::Output.gets
def print_exception(exception, explicit)
puts "[#{explicit ? 'EXPLICIT' : 'INEXPLICIT'}] #{exception.class}: #{exception.message}"
puts exception.backtrace.join("\n")
end
# -35 to 35
mapRoll = lambda { |value|
min = -35
max = 35
offset = 180
input = [value, min].max
input = [input, max].min
((value.to_f + offset) * ( 127.0 / (max - min).to_f)).to_i
}
map180 = lambda { |value|
((value * 1.0 + 180) * ( 127.0 / 360.0)).to_i
}
map360 = lambda { |value|
((value * 1.0) * ( 127.0 / 360.0)).to_i
}
notes_1 = [36, 40, 43, 48, 52, 55, 60, 64, 67] # C E G arpeggios
notes_2 = [36, 38, 40, 41, 43, 45, 47, 48].map { |n| n + 24 }
mapNotes = lambda { |value, notes|
input = [value, 0].max
input = [input, 100].min
index = ((input.to_f / 101) * notes.length).to_i
notes[index]
}
midi_map = {
cutoff: 74, #16,
resonance: 71, #82,
modulation: 1,
overdrive: 114,
distortion: 94
}
control_map_1 = {
roll: { cc: :cutoff, fn: mapRoll },
# compass: { cc: :distortion, fn: map360 },
}
control_map_2 = {
roll: { cc: :cutoff, fn: mapRoll },
# compass: { cc: :distortion, fn: map360 },
}
note_map_1 = {
# pitch: { fn: mapNotes, notes: notes_1 }
}
note_map_2 = {
pitch: { fn: mapNotes, notes: notes_2 }
}
last_note = nil
id_map = {
1901794473 => {
channel: 2,
note_map: note_map_1,
control_map: control_map_1,
last_note: nil
},
1689304789 => {
channel: 1,
note_map: note_map_2,
control_map: control_map_2,
last_note: nil
}
}
id_map.each { |k, v| puts "keys: '#{k}'" }
puts "id map: #{id_map[1689304789].inspect}"
output.puts(0x91, 36, 100)
output.puts(0x92, 48, 100)
begin
loop do
line = serial.readline()
begin
packet = JSON.parse(line)
# byebug
name = packet['n']
controller = id_map[packet['s'].to_i]
# puts " controller for '#{packet['s'].to_i}': #{controller.inspect}"
unless controller
next
end
control_map = controller[:control_map]
note_map = controller[:note_map]
channel = controller[:channel] || 0
control = control_map[name.to_sym]
note = note_map[name.to_sym]
if control
cc = midi_map[control[:cc]]
fn = control[:fn]
value = fn.call(packet['v'])
# puts "#{cc} (#{control[:cc]}) : #{packet['v']} = #{value}"
output.puts(176 + channel, cc, value)
elsif note
fn = note[:fn]
value = fn.call(packet['v'], note[:notes])
puts "#{name} channel: #{channel} msg: #{0x90 + channel} note: #{packet['v']} = #{value}"
if controller[:last_note]
output.puts(0x80 + channel, controller[:last_note], 0)
end
if controller[:last_note] != value
output.puts(0x90 + channel, value, 100)
controller[:last_note] = value
end
else
if name.to_s == "input"
puts "name! #{packet['v']}"
# else
# puts "name: #{name} packet #{packet}"
end
end
rescue JSON::ParserError => e
puts "invalid packet: #{line}"
rescue NoMethodError
next
end
end
rescue Interrupt => e
print_exception(e, true)
rescue SignalException => e
print_exception(e, false)
rescue StandardError => e
print_exception(e, false)
end
id_map.each do |c|
if c[:last_note]
output.puts(0x80 + c[:channel], c[:last_note], 0)
end
end