Skip to content

Commit

Permalink
machine/usb/adc/midi: improve code readability and error handling tha…
Browse files Browse the repository at this point in the history
…nks to helpful feedback from community

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Sep 5, 2023
1 parent 7973f06 commit e138256
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 83 deletions.
126 changes: 47 additions & 79 deletions src/machine/usb/adc/midi/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,12 @@ var (
// The cable parameter is the cable number 0-15.
// The channel parameter is the MIDI channel number 1-16.
func (m *midi) NoteOn(cable, channel uint8, note Note, velocity uint8) error {
if cable > 15 {
switch {
case cable > 15:
return errInvalidMIDICable
}

if channel == 0 || channel > 16 {
case channel == 0 || channel > 16:
return errInvalidMIDIChannel
}

if velocity > 127 {
case velocity > 127:
return errInvalidMIDIVelocity
}

Expand All @@ -109,15 +106,12 @@ func (m *midi) NoteOn(cable, channel uint8, note Note, velocity uint8) error {
// The cable parameter is the cable number 0-15.
// The channel parameter is the MIDI channel number 1-16.
func (m *midi) NoteOff(cable, channel uint8, note Note, velocity uint8) error {
if cable > 15 {
switch {
case cable > 15:
return errInvalidMIDICable
}

if channel == 0 || channel > 16 {
case channel == 0 || channel > 16:
return errInvalidMIDIChannel
}

if velocity > 127 {
case velocity > 127:
return errInvalidMIDIVelocity
}

Expand All @@ -132,19 +126,14 @@ func (m *midi) NoteOff(cable, channel uint8, note Note, velocity uint8) error {
// The control parameter is the controller number 0-127.
// The value parameter is the controller value 0-127.
func (m *midi) ControlChange(cable, channel, control, value uint8) error {
if cable > 15 {
switch {
case cable > 15:
return errInvalidMIDICable
}

if channel == 0 || channel > 16 {
case channel == 0 || channel > 16:
return errInvalidMIDIChannel
}

if control > 127 {
case control > 127:
return errInvalidMIDIControl
}

if value > 127 {
case value > 127:
return errInvalidMIDIControlValue
}

Expand All @@ -158,15 +147,12 @@ func (m *midi) ControlChange(cable, channel, control, value uint8) error {
// The channel parameter is the MIDI channel number 1-16.
// The patch parameter is the program number 0-127.
func (m *midi) ProgramChange(cable, channel uint8, patch uint8) error {
if cable > 15 {
switch {
case cable > 15:
return errInvalidMIDICable
}

if channel == 0 || channel > 16 {
case channel == 0 || channel > 16:
return errInvalidMIDIChannel
}

if patch > 127 {
case patch > 127:
return errInvalidMIDIPatch
}

Expand All @@ -182,15 +168,12 @@ func (m *midi) ProgramChange(cable, channel uint8, patch uint8) error {
// Setting bend above 0x2000 (up to 0x3FFF) will increase the pitch.
// Setting bend below 0x2000 (down to 0x0000) will decrease the pitch.
func (m *midi) PitchBend(cable, channel uint8, bend uint16) error {
if cable > 15 {
switch {
case cable > 15:
return errInvalidMIDICable
}

if channel == 0 || channel > 16 {
case channel == 0 || channel > 16:
return errInvalidMIDIChannel
}

if bend > 0x3FFF {
case bend > 0x3FFF:
return errInvalidMIDIPitchBend
}

Expand All @@ -207,59 +190,44 @@ func (m *midi) PitchBend(cable, channel uint8, bend uint16) error {
// The data slice should not include the SysEx start (0xF0) or
// end (0xF7) bytes, only the data in between.
func (m *midi) SysEx(cable uint8, data []byte) error {
if cable > 15 {
switch {
case cable > 15:
return errInvalidMIDICable
}

if len(data) < 3 {
case len(data) < 3:
return errInvalidMIDISysExData
}

// write start
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExStart, MsgSysExStart
m.msg[2], m.msg[3] = data[0], data[1]

if _, err := m.Write(m.msg[:]); err != nil {
return err
}

for i := 2; i <= len(data); i += 3 {
switch len(data) - i {
case 2:
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExEnd3, data[i]
m.msg[2], m.msg[3] = data[i+1], MsgSysExEnd

if _, err := m.Write(m.msg[:]); err != nil {
return err
}

break
case 1:
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExEnd2, data[i]
m.msg[2], m.msg[3] = MsgSysExEnd, 0

if _, err := m.Write(m.msg[:]); err != nil {
return err
}

break
case 0:
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExEnd1, MsgSysExEnd
m.msg[2], m.msg[3] = 0, 0

if _, err := m.Write(m.msg[:]); err != nil {
return err
}

break
default:
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExStart, data[i]
m.msg[2], m.msg[3] = data[i+1], data[i+2]

if _, err := m.Write(m.msg[:]); err != nil {
return err
}
// write middle
i := 2
for ; i < len(data)-2; i += 3 {
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExStart, data[i]
m.msg[2], m.msg[3] = data[i+1], data[i+2]
if _, err := m.Write(m.msg[:]); err != nil {
return err
}
}
// write end
switch len(data) - i {
case 2:
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExEnd3, data[i]
m.msg[2], m.msg[3] = data[i+1], MsgSysExEnd
case 1:
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExEnd2, data[i]
m.msg[2], m.msg[3] = MsgSysExEnd, 0
case 0:
m.msg[0], m.msg[1] = (cable&0xf<<4)|CINSysExEnd1, MsgSysExEnd
m.msg[2], m.msg[3] = 0, 0
}
if _, err := m.Write(m.msg[:]); err != nil {
return err
}

return nil
}
8 changes: 4 additions & 4 deletions src/machine/usb/adc/midi/midi.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ func (m *midi) sendUSBPacket(b []byte) {

// from BulkIn
func (m *midi) TxHandler() {
if m.txHandler != nil {
m.txHandler()
}

m.waitTxc = false
if b, ok := m.buf.Get(); ok {
m.waitTxc = true
m.sendUSBPacket(b)
}

if m.txHandler != nil {
m.txHandler()
}
}

func (m *midi) tx(b []byte) {
Expand Down

0 comments on commit e138256

Please sign in to comment.