-
Notifications
You must be signed in to change notification settings - Fork 13
/
midi.go
61 lines (56 loc) · 1.81 KB
/
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
package sequences
import (
"fmt"
"github.com/bspaans/bleep/midi"
. "github.com/bspaans/bleep/sequencer/status"
"github.com/bspaans/bleep/synth"
"gitlab.com/gomidi/midi/midimessage/channel"
)
func MidiSequence(mid *midi.MIDISequences, inputChannels, outputChannels []int, speed float64, loop bool) Sequence {
inputCh := map[int]bool{}
for _, i := range inputChannels {
inputCh[i] = true
}
sendEvent := func(s chan *synth.Event, fromChannel int, ty synth.EventType, params []int) {
if len(outputChannels) == 0 {
s <- synth.NewEvent(ty, fromChannel, params)
} else {
for _, o := range outputChannels {
s <- synth.NewEvent(ty, o, params)
}
}
}
if speed == 0.0 {
speed = 1.0
}
return func(sequencer *Status, counter, t uint, s chan *synth.Event) {
tickRatio := float64(mid.TimeFormat) / float64(sequencer.Granularity)
length := mid.Length
timeInTrack := int(float64(t) * tickRatio * speed)
for channelNr, ch := range mid.Channels {
if ch == nil {
continue
}
if len(inputChannels) != 0 && !inputCh[channelNr] {
continue
}
for _, ev := range ch.Events {
if ev.Offset == timeInTrack || (loop && timeInTrack >= length && ((timeInTrack%length) == ev.Offset || ((timeInTrack%length) == 0 && ev.Offset == length))) {
switch ev.Message.(type) {
case channel.NoteOn:
n := ev.Message.(channel.NoteOn)
sendEvent(s, channelNr, synth.NoteOn, []int{int(n.Key()), int(n.Velocity())})
case channel.NoteOff:
n := ev.Message.(channel.NoteOff)
sendEvent(s, channelNr, synth.NoteOff, []int{int(n.Key())})
case channel.NoteOffVelocity:
n := ev.Message.(channel.NoteOffVelocity)
sendEvent(s, channelNr, synth.NoteOff, []int{int(n.Key()), int(n.Velocity())})
default:
fmt.Println("Do something", ev.Message)
}
}
}
}
}
}