forked from longears/pixelslinger
/
pattern-basic-midi.go
202 lines (182 loc) · 5.17 KB
/
pattern-basic-midi.go
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
package opc
// Basic Midi
// Turns on one LED for each of the 128 MIDI pitches when that node is being played.
// LEDs are colored in rainbow order according to the circle of fifths.
import (
"github.com/longears/pixelslinger/colorutils"
"github.com/longears/pixelslinger/midi"
"time"
)
func MakePatternBasicMidi(locations []float64) ByteThread {
var (
MIDI_VOLUME_GAIN = 1.5 // multiply incoming midi volumes by this much
MIDI_BRIGHTNESS_MIN = 0.3 // midi volume 1/127, after MIDI_VOLUME_GAIN, is remapped to this
MIDI_BRIGHTNESS_MAX = 1.0 // midi volume 127, after MIDI_VOLUME_GAIN, is remapped to this
SECONDS_TO_FADE = 0.3 // how long it takes to fade to black after key is lifted
COLOR_BLEEDING_RAD = 3 // radius of glow effect around pressed keys. set to 0 for no bleeding
COLOR_BLEEDING_GAIN = 0.2 // brightness of glow effect (range 0-1)
MIN_VISIBLE_COLOR = 0.04 // min pixel brightness which is actually visible (range 0-1)
SUSTAIN = true // leave lights on while keys are held down
)
return func(bytesIn chan []byte, bytesOut chan []byte, midiState *midi.MidiState) {
// the current volume of each key, from 0 to 1, after applying MIDI_* adjustments
keyVolumes := make([]float64, 128)
// smoothed value: like keyVolumes, but fades away slowly when key is off
smoothedVolumes := make([]float64, 128)
last_t := float64(0)
for bytes := range bytesIn {
n_pixels := len(bytes) / 3
t := float64(time.Now().UnixNano())/1.0e9 - 9.4e8
tDiff := colorutils.Clamp(t-last_t, 0, 5) // limit to max of 5 second to avoid pathological value at startup
// update keyVolumes from MidiState
if !SUSTAIN {
// if not sustaining, reset volumes to zero each frame
// causing it to be on for 1 frame only when the key is initially pressed
for ii, _ := range keyVolumes {
keyVolumes[ii] = 0
}
}
// read midiState into keyVolumes and apply gain adjustments
for ii, v := range midiState.KeyVolumes {
if v == 0 {
keyVolumes[ii] = 0
} else {
keyVolumes[ii] = colorutils.Clamp(colorutils.Remap(float64(v)/127*MIDI_VOLUME_GAIN, 0, 1, MIDI_BRIGHTNESS_MIN, MIDI_BRIGHTNESS_MAX), 0, 1)
}
}
// update smoothedVolumes
for ii, v := range smoothedVolumes {
// fade old values
smoothedVolumes[ii] = colorutils.Clamp(v-tDiff/SECONDS_TO_FADE, 0, 1)
// re-apply current values if greater
if keyVolumes[ii] > smoothedVolumes[ii] {
smoothedVolumes[ii] = keyVolumes[ii]
}
}
// fill in bytes array
for ii := 0; ii < n_pixels; ii++ {
//--------------------------------------------------------------------------------
if ii < len(keyVolumes) {
k := keyVolumes[ii]
s := smoothedVolumes[ii]
r := float64(0)
g := float64(0)
b := float64(0)
// get color based on pitch
pr, pg, pb := pitchToRGB(ii)
// apply brightness from keyVolumes or smoothedVolumes
if k > 0 {
// key is currently down
r = pr * k
g = pg * k
b = pb * k
} else {
// key not currently down. use smoothed value which is fading away over time
r = pr * s
g = pg * s
b = pb * s
}
// color bleeding
if COLOR_BLEEDING_RAD > 0 {
for offset := -COLOR_BLEEDING_RAD; offset <= COLOR_BLEEDING_RAD; offset++ {
if ii == 0 || ii+offset < 0 || ii+offset >= len(keyVolumes) {
continue
}
if keyVolumes[ii+offset] > 0 {
brightness := float64(offset) / (float64(COLOR_BLEEDING_RAD) + 1)
if brightness < 0 {
brightness = -brightness
}
brightness = 1 - brightness
brightness *= COLOR_BLEEDING_GAIN
pr2, pg2, pb2 := pitchToRGB(ii + offset)
r += pr2 * keyVolumes[ii+offset] * brightness
g += pg2 * keyVolumes[ii+offset] * brightness
b += pb2 * keyVolumes[ii+offset] * brightness
}
}
}
// avoid black clipping
if r > 0 {
r = colorutils.Remap(r, 0, 1, MIN_VISIBLE_COLOR, 1)
}
if g > 0 {
g = colorutils.Remap(g, 0, 1, MIN_VISIBLE_COLOR, 1)
}
if b > 0 {
b = colorutils.Remap(b, 0, 1, MIN_VISIBLE_COLOR, 1)
}
bytes[ii*3+0] = colorutils.FloatToByte(r)
bytes[ii*3+1] = colorutils.FloatToByte(g)
bytes[ii*3+2] = colorutils.FloatToByte(b)
} else {
// if we have more LEDs than MIDI keys
bytes[ii*3+0] = 0
bytes[ii*3+1] = 0
bytes[ii*3+2] = 0
}
//--------------------------------------------------------------------------------
}
last_t = t
bytesOut <- bytes
}
}
}
// Convert a midi pitch to a color
func pitchToRGB(pitch int) (float64, float64, float64) {
var r, g, b float64
pitchClass := pitch % 12
// circle of fifths
pitchClass = (pitchClass*5 + 1) % 12
switch pitchClass {
case 0:
r = 1
g = 0
b = 0
case 1:
r = 0.9
g = 0.4
b = 0
case 2:
r = 0.8
g = 0.8
b = 0
case 3:
r = 0.4
g = 0.9
b = 0
case 4:
r = 0
g = 1
b = 0
case 5:
r = 0
g = 0.9
b = 0.4
case 6:
r = 0
g = 0.8
b = 0.8
case 7:
r = 0
g = 0.4
b = 0.9
case 8:
r = 0
g = 0
b = 1
case 9:
r = 0.4
g = 0
b = 0.9
case 10:
r = 0.8
g = 0
b = 0.8
case 11:
r = 0.9
g = 0
b = 0.4
}
return r, g, b
}