machine/usb/adc/midi: improve implementation to include several new messages

such as program changes and pitch bend. Also add error handling for invalid
parameter values such as MIDI channel. This however makes a somewhat breaking
change to the current implementation, in that we now use the typical MIDI user
system of counting MIDI channels from 1-16 instead of from 0-15 as the lower
level USB-MIDI API itself expects.

Also add constant values for continuous controller messages, rename SendCC
function, and add SysEx function.

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2023-09-03 12:06:00 +02:00
committed by Ron Evans
parent 4643401a1d
commit 9d6eb1ff06
3 changed files with 278 additions and 33 deletions
+25 -15
View File
@@ -10,47 +10,57 @@ import (
// Try it easily by opening the following site in Chrome.
// https://www.onlinemusictools.com/kb/
const (
cable = 0
channel = 1
velocity = 0x40
)
func main() {
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
button := machine.BUTTON
button.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
button.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
m := midi.Port()
m.SetHandler(func(b []byte) {
m.SetRxHandler(func(b []byte) {
// blink when we receive a MIDI message
led.Set(!led.Get())
})
m.SetTxHandler(func() {
// blink when we send a MIDI message
led.Set(!led.Get())
m.Write(b)
})
prev := true
chords := []struct {
name string
keys []midi.Note
name string
notes []midi.Note
}{
{name: "C ", keys: []midi.Note{midi.C4, midi.E4, midi.G4}},
{name: "G ", keys: []midi.Note{midi.G3, midi.B3, midi.D4}},
{name: "Am", keys: []midi.Note{midi.A3, midi.C4, midi.E4}},
{name: "F ", keys: []midi.Note{midi.F3, midi.A3, midi.C4}},
{name: "C ", notes: []midi.Note{midi.C4, midi.E4, midi.G4}},
{name: "G ", notes: []midi.Note{midi.G3, midi.B3, midi.D4}},
{name: "Am", notes: []midi.Note{midi.A3, midi.C4, midi.E4}},
{name: "F ", notes: []midi.Note{midi.F3, midi.A3, midi.C4}},
}
index := 0
for {
current := button.Get()
if prev != current {
led.Set(current)
if current {
for _, c := range chords[index].keys {
m.NoteOff(0, 0, c, 0x40)
for _, note := range chords[index].notes {
m.NoteOff(cable, channel, note, velocity)
}
index = (index + 1) % len(chords)
} else {
for _, c := range chords[index].keys {
m.NoteOn(0, 0, c, 0x40)
for _, note := range chords[index].notes {
m.NoteOn(cable, channel, note, velocity)
}
}
prev = current
}
time.Sleep(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
}
}