Files
dmxconnect/hardware/MIDIDriver.go

128 lines
4.5 KiB
Go
Raw Normal View History

2024-12-15 13:45:46 +01:00
package hardware
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/mattrtaylor/go-rtmidi"
2024-12-29 13:09:46 +01:00
"github.com/rs/zerolog/log"
2024-12-15 13:45:46 +01:00
)
2024-12-23 17:22:37 +01:00
// MIDIDriver represents how the protocol is defined
type MIDIDriver struct {
2024-12-15 13:45:46 +01:00
peripherals map[string]Peripheral // The list of peripherals
}
2024-12-23 17:22:37 +01:00
// NewMIDIDriver creates a new DMXUSB protocol
func NewMIDIDriver() *MIDIDriver {
2024-12-29 13:09:46 +01:00
log.Trace().Str("file", "MIDIDriver").Msg("MIDI driver created")
2024-12-23 17:22:37 +01:00
return &MIDIDriver{
2024-12-15 13:45:46 +01:00
peripherals: make(map[string]Peripheral),
}
}
2024-12-23 17:22:37 +01:00
// Initialize initializes the MIDI driver
func (d *MIDIDriver) Initialize() error {
2024-12-29 13:09:46 +01:00
log.Trace().Str("file", "MIDIDriver").Msg("MIDI driver initialized")
2024-12-15 13:45:46 +01:00
return nil
}
2024-12-23 17:22:37 +01:00
// GetName returns the name of the driver
func (d *MIDIDriver) GetName() string {
2024-12-15 13:45:46 +01:00
return "MIDI"
}
2024-12-20 17:18:57 +01:00
// GetPeripheral gets the peripheral that correspond to the specified ID
2024-12-23 17:22:37 +01:00
func (d *MIDIDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
2024-12-20 17:18:57 +01:00
// Return the specified peripheral
2024-12-29 13:09:46 +01:00
peripheral, found := d.peripherals[peripheralID]
if !found {
log.Error().Str("file", "MIDIDriver").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the MIDI driver")
2024-12-20 17:18:57 +01:00
return nil, false
}
2024-12-29 13:09:46 +01:00
log.Trace().Str("file", "MIDIDriver").Str("peripheralID", peripheralID).Msg("MIDI peripheral found in the driver")
2024-12-20 17:18:57 +01:00
return peripheral, true
}
2024-12-15 13:45:46 +01:00
func splitStringAndNumber(input string) (string, int, error) {
// Regular expression to match the text part and the number at the end
re := regexp.MustCompile(`^(.*?)(\d+)$`)
matches := re.FindStringSubmatch(input)
// Check if the regex found both a text part and a number
if len(matches) == 3 {
// matches[1]: text part (might contain trailing spaces)
// matches[2]: numeric part as a string
textPart := strings.TrimSpace(matches[1]) // Remove any trailing spaces from the text
numberPart, err := strconv.Atoi(matches[2])
if err != nil {
return "", 0, err // Return error if the number conversion fails
}
return textPart, numberPart, nil
}
// Return an error if no trailing number is found
return "", 0, fmt.Errorf("no number found at the end of the string")
}
// Scan scans the interfaces compatible with the MIDI protocol
2024-12-23 17:22:37 +01:00
func (d *MIDIDriver) Scan(ctx context.Context) error {
2024-12-15 13:45:46 +01:00
midiPeripherals := make(map[string]Peripheral)
2024-12-29 13:09:46 +01:00
log.Trace().Str("file", "MIDIDriver").Msg("opening MIDI scanner port...")
2024-12-15 13:45:46 +01:00
midiScanner, err := rtmidi.NewMIDIInDefault()
if err != nil {
2024-12-29 13:09:46 +01:00
log.Err(err).Str("file", "MIDIDriver").Msg("unable to open the MIDI scanner port...")
return fmt.Errorf("unable to open the MIDI scanner: %s", err)
2024-12-15 13:45:46 +01:00
}
defer midiScanner.Close()
2024-12-29 13:09:46 +01:00
midiScanner.SetCallback(func(m rtmidi.MIDIIn, b []byte, f float64) {
})
log.Trace().Str("file", "MIDIDriver").Msg("scanning MIDI peripherals...")
2024-12-15 13:45:46 +01:00
devicesCount, err := midiScanner.PortCount()
if err != nil {
2024-12-29 13:09:46 +01:00
log.Err(err).Str("file", "MIDIDriver").Msg("unable to scan MIDI peripherals...")
return fmt.Errorf("unable to scan MIDI peripherals: %s", err)
2024-12-15 13:45:46 +01:00
}
for i := 0; i < devicesCount; i++ {
portName, err := midiScanner.PortName(i)
if err != nil {
2024-12-29 13:09:46 +01:00
log.Warn().Str("file", "MIDIPeripheral").Msg("found peripheral without a correct name, set it to unknown")
2024-12-15 13:45:46 +01:00
portName = "Unknown device 0"
}
// Separate data
name, location, err := splitStringAndNumber(portName)
if err != nil {
2024-12-29 13:09:46 +01:00
log.Err(err).Str("file", "MIDIDriver").Str("description", portName).Msg("invalid peripheral description")
return fmt.Errorf("invalid pripheral description: %s", err)
2024-12-15 13:45:46 +01:00
}
2024-12-29 13:09:46 +01:00
log.Info().Str("file", "MIDIDriver").Str("name", name).Int("location", location).Msg("MIDI peripheral found")
2024-12-15 13:45:46 +01:00
// Add the peripheral to the temporary list
2024-12-26 14:55:55 +01:00
sn := strings.ToLower(strings.Replace(name, " ", "_", -1))
midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn)
2024-12-15 13:45:46 +01:00
}
// Compare with the current peripherals to detect arrivals/removals
2024-12-23 17:22:37 +01:00
removedList, addedList := comparePeripherals(d.peripherals, midiPeripherals)
2024-12-15 13:45:46 +01:00
// Emit the events
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
2024-12-29 13:09:46 +01:00
log.Info().Str("file", "MIDIDriver").Msg("MIDI remove list emitted to the front")
2024-12-15 13:45:46 +01:00
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
2024-12-29 13:09:46 +01:00
log.Info().Str("file", "MIDIDriver").Msg("MIDI add list emitted to the front")
2024-12-15 13:45:46 +01:00
// Store the new peripherals list
2024-12-23 17:22:37 +01:00
d.peripherals = midiPeripherals
return nil
}
// CreatePeripheral is not implemented here
func (d *MIDIDriver) CreatePeripheral(context.Context) (Peripheral, error) {
return nil, nil
}
// RemovePeripheral is not implemented here
func (d *MIDIDriver) RemovePeripheral(serialNumber string) error {
2024-12-15 13:45:46 +01:00
return nil
}