mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
add basic MIDI message encode/decode
This commit is contained in:
37
internal/processing/midi-message-decode.go
Normal file
37
internal/processing/midi-message-decode.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package processing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
type MIDIMessageDecode struct {
|
||||
config ProcessorConfig
|
||||
}
|
||||
|
||||
func (mmd *MIDIMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage := midi.Message(payloadBytes)
|
||||
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (mmd *MIDIMessageDecode) Type() string {
|
||||
return mmd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.message.decode",
|
||||
New: func(config ProcessorConfig) (Processor, error) {
|
||||
return &MIDIMessageDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
35
internal/processing/midi-message-encode.go
Normal file
35
internal/processing/midi-message-encode.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package processing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
type MIDIMessageEncode struct {
|
||||
config ProcessorConfig
|
||||
}
|
||||
|
||||
func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.encode processor only accepts an mqtt.Message")
|
||||
}
|
||||
|
||||
return payloadMessage.Bytes(), nil
|
||||
}
|
||||
|
||||
func (mme *MIDIMessageEncode) Type() string {
|
||||
return mme.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.message.encode",
|
||||
New: func(config ProcessorConfig) (Processor, error) {
|
||||
return &MIDIMessageEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user