diff options
author | sago35 <[email protected]> | 2022-07-27 09:28:28 +0900 |
---|---|---|
committer | Ron Evans <[email protected]> | 2022-07-27 16:19:13 +0200 |
commit | a4b22bd125e4a8ed3230b56158c5d1426b208911 (patch) | |
tree | 57fd0d54519a4b7808b54dcfa184961528cf0fcf | |
parent | 2ed75230013df01ff5e7da2de225789a4566dc62 (diff) | |
download | tinygo-a4b22bd125e4a8ed3230b56158c5d1426b208911.tar.gz tinygo-a4b22bd125e4a8ed3230b56158c5d1426b208911.zip |
usb/midi: add definition of MIDI note number
-rw-r--r-- | src/examples/usb-midi/main.go | 13 | ||||
-rw-r--r-- | src/machine/usb/midi/messages.go | 8 | ||||
-rw-r--r-- | src/machine/usb/midi/notes.go | 108 |
3 files changed, 120 insertions, 9 deletions
diff --git a/src/examples/usb-midi/main.go b/src/examples/usb-midi/main.go index c325a4486..ac0e15988 100644 --- a/src/examples/usb-midi/main.go +++ b/src/examples/usb-midi/main.go @@ -7,6 +7,9 @@ import ( "time" ) +// Try it easily by opening the following site in Chrome. +// https://www.onlinemusictools.com/kb/ + func main() { led := machine.LED led.Configure(machine.PinConfig{Mode: machine.PinOutput}) @@ -24,12 +27,12 @@ func main() { prev := true chords := []struct { name string - keys []byte + keys []midi.Note }{ - {name: "C ", keys: []byte{60, 64, 67}}, - {name: "G ", keys: []byte{55, 59, 62}}, - {name: "Am", keys: []byte{57, 60, 64}}, - {name: "F ", keys: []byte{53, 57, 60}}, + {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}}, } index := 0 diff --git a/src/machine/usb/midi/messages.go b/src/machine/usb/midi/messages.go index 81f9b5a41..bf15e25da 100644 --- a/src/machine/usb/midi/messages.go +++ b/src/machine/usb/midi/messages.go @@ -1,14 +1,14 @@ package midi // NoteOn sends a note on message. -func (m *midi) NoteOn(cable, channel, note, velocity uint8) { - m.msg[0], m.msg[1], m.msg[2], m.msg[3] = (cable&0xf<<4)|0x9, 0x90|(channel&0xf), note&0x7f, velocity&0x7f +func (m *midi) NoteOn(cable, channel uint8, note Note, velocity uint8) { + m.msg[0], m.msg[1], m.msg[2], m.msg[3] = (cable&0xf<<4)|0x9, 0x90|(channel&0xf), byte(note)&0x7f, velocity&0x7f m.Write(m.msg[:]) } // NoteOff sends a note off message. -func (m *midi) NoteOff(cable, channel, note, velocity uint8) { - m.msg[0], m.msg[1], m.msg[2], m.msg[3] = (cable&0xf<<4)|0x8, 0x80|(channel&0xf), note&0x7f, velocity&0x7f +func (m *midi) NoteOff(cable, channel uint8, note Note, velocity uint8) { + m.msg[0], m.msg[1], m.msg[2], m.msg[3] = (cable&0xf<<4)|0x8, 0x80|(channel&0xf), byte(note)&0x7f, velocity&0x7f m.Write(m.msg[:]) } diff --git a/src/machine/usb/midi/notes.go b/src/machine/usb/midi/notes.go new file mode 100644 index 000000000..05fa6819e --- /dev/null +++ b/src/machine/usb/midi/notes.go @@ -0,0 +1,108 @@ +package midi + +// Note represents a MIDI note number. For example, Note(69) is A4 or 440Hz. +type Note uint8 + +// Define all the notes in a format similar to the Tone library in the Arduino +// IDE. +const ( + A0 Note = iota + 21 // 27.5Hz + AS0 + B0 + C1 + CS1 + D1 + DS1 + E1 + F1 + FS1 + G1 + GS1 + A1 // 55Hz + AS1 + B1 + C2 + CS2 + D2 + DS2 + E2 + F2 + FS2 + G2 + GS2 + A2 // 110Hz + AS2 + B2 + C3 + CS3 + D3 + DS3 + E3 + F3 + FS3 + G3 + GS3 + A3 // 220Hz + AS3 + B3 + C4 + CS4 + D4 + DS4 + E4 + F4 + FS4 + G4 + GS4 + A4 // 440Hz + AS4 + B4 + C5 + CS5 + D5 + DS5 + E5 + F5 + FS5 + G5 + GS5 + A5 // 880Hz + AS5 + B5 + C6 + CS6 + D6 + DS6 + E6 + F6 + FS6 + G6 + GS6 + A6 // 1760Hz + AS6 + B6 + C7 + CS7 + D7 + DS7 + E7 + F7 + FS7 + G7 + GS7 + A7 // 3520Hz + AS7 + B7 + C8 + CS8 + D8 + DS8 + E8 + F8 + FS8 + G8 + GS8 + A8 // 7040Hz + AS8 + B8 +) |