2025-01-18 14:53:29 +00:00
|
|
|
package hardware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-11-14 20:19:44 +01:00
|
|
|
"errors"
|
2025-01-18 14:53:29 +00:00
|
|
|
"fmt"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2025-11-14 20:19:44 +01:00
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
|
"gitlab.com/gomidi/rtmididrv"
|
2025-01-18 14:53:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MIDIFinder represents how the protocol is defined
|
|
|
|
|
type MIDIFinder struct {
|
2025-11-14 20:19:44 +01:00
|
|
|
wg sync.WaitGroup
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
|
|
saved map[string]PeripheralInfo // Peripherals saved in the project
|
|
|
|
|
detected map[string]*MIDIPeripheral // Detected peripherals
|
|
|
|
|
|
|
|
|
|
scanEvery time.Duration // Scans peripherals periodically
|
|
|
|
|
|
|
|
|
|
onArrival func(p PeripheralInfo) // When a peripheral arrives
|
|
|
|
|
onRemoval func(p PeripheralInfo) // When a peripheral goes away
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-14 20:19:44 +01:00
|
|
|
// NewMIDIFinder creates a new MIDI finder
|
|
|
|
|
func NewMIDIFinder(scanEvery time.Duration) *MIDIFinder {
|
2025-01-18 14:53:29 +00:00
|
|
|
log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder created")
|
|
|
|
|
return &MIDIFinder{
|
2025-11-14 20:19:44 +01:00
|
|
|
scanEvery: scanEvery,
|
|
|
|
|
saved: make(map[string]PeripheralInfo),
|
|
|
|
|
detected: make(map[string]*MIDIPeripheral),
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 20:19:44 +01:00
|
|
|
// OnArrival is the callback function when a new peripheral arrives
|
|
|
|
|
func (f *MIDIFinder) OnArrival(cb func(p PeripheralInfo)) {
|
|
|
|
|
f.onArrival = cb
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnRemoval i the callback when a peripheral goes away
|
|
|
|
|
func (f *MIDIFinder) OnRemoval(cb func(p PeripheralInfo)) {
|
|
|
|
|
f.onRemoval = cb
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// Initialize initializes the MIDI driver
|
|
|
|
|
func (f *MIDIFinder) Initialize() error {
|
|
|
|
|
log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder initialized")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 12:01:31 +01:00
|
|
|
// RegisterPeripheral registers a new peripheral
|
|
|
|
|
func (f *MIDIFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) {
|
2025-11-14 20:19:44 +01:00
|
|
|
f.mu.Lock()
|
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
f.saved[peripheralData.SerialNumber] = peripheralData
|
|
|
|
|
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
|
|
|
|
|
|
|
|
|
|
// If already detected, connect it
|
|
|
|
|
if peripheral, ok := f.detected[peripheralData.SerialNumber]; ok {
|
|
|
|
|
f.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
err := peripheral.Connect(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Peripheral connected, activate it
|
|
|
|
|
err = peripheral.Activate(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the FTDI peripheral")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}()
|
2025-01-26 12:01:31 +01:00
|
|
|
}
|
2025-11-14 20:19:44 +01:00
|
|
|
|
|
|
|
|
// Emits the event in the hardware
|
|
|
|
|
runtime.EventsEmit(ctx, "LOAD_PERIPHERAL", peripheralData)
|
2025-01-26 12:01:31 +01:00
|
|
|
return peripheralData.SerialNumber, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnregisterPeripheral unregisters an existing peripheral
|
2025-11-14 20:19:44 +01:00
|
|
|
func (f *MIDIFinder) UnregisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) error {
|
|
|
|
|
f.mu.Lock()
|
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
if peripheral, detected := f.detected[peripheralData.SerialNumber]; detected {
|
|
|
|
|
// Deactivating peripheral
|
|
|
|
|
err := peripheral.Deactivate(ctx)
|
2025-01-26 12:01:31 +01:00
|
|
|
if err != nil {
|
2025-11-14 20:19:44 +01:00
|
|
|
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to deactivate the peripheral")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
// Disconnecting peripheral
|
|
|
|
|
err = peripheral.Disconnect(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to disconnect the peripheral")
|
|
|
|
|
return nil
|
2025-01-26 12:01:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-14 20:19:44 +01:00
|
|
|
delete(f.saved, peripheralData.SerialNumber)
|
|
|
|
|
runtime.EventsEmit(ctx, "UNLOAD_PERIPHERAL", peripheralData)
|
2025-01-26 12:01:31 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// Start starts the finder and search for peripherals
|
|
|
|
|
func (f *MIDIFinder) Start(ctx context.Context) error {
|
2025-11-14 20:19:44 +01:00
|
|
|
f.wg.Add(1)
|
2025-01-18 14:53:29 +00:00
|
|
|
go func() {
|
2025-11-14 20:19:44 +01:00
|
|
|
ticker := time.NewTicker(f.scanEvery)
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
2025-11-14 20:19:44 +01:00
|
|
|
case <-ticker.C:
|
2025-01-18 14:53:29 +00:00
|
|
|
// Scan the peripherals
|
|
|
|
|
err := f.scanPeripherals(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("file", "MIDIFinder").Msg("unable to scan MIDI peripherals")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 20:19:44 +01:00
|
|
|
// WaitStop stops the finder
|
|
|
|
|
func (f *MIDIFinder) WaitStop() error {
|
2025-01-18 14:53:29 +00:00
|
|
|
log.Trace().Str("file", "MIDIFinder").Msg("stopping the MIDI finder...")
|
2025-11-14 20:19:44 +01:00
|
|
|
|
|
|
|
|
// Close the channel
|
|
|
|
|
// close(f.scanChannel)
|
|
|
|
|
|
|
|
|
|
// Wait for all the peripherals to close
|
|
|
|
|
log.Trace().Str("file", "MIDIFinder").Msg("closing all MIDI peripherals")
|
|
|
|
|
var errs []error
|
|
|
|
|
for registeredPeripheralSN, registeredPeripheral := range f.detected {
|
|
|
|
|
err := registeredPeripheral.WaitStop()
|
|
|
|
|
if err != nil {
|
|
|
|
|
errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// Wait for goroutines to stop
|
2025-11-14 20:19:44 +01:00
|
|
|
f.wg.Wait()
|
|
|
|
|
|
|
|
|
|
// Returning errors
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
|
return errors.Join(errs...)
|
|
|
|
|
}
|
2025-01-18 14:53:29 +00:00
|
|
|
log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder stopped")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetName returns the name of the driver
|
|
|
|
|
func (f *MIDIFinder) GetName() string {
|
|
|
|
|
return "MIDI"
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 12:01:31 +01:00
|
|
|
// GetPeripheralSettings gets the peripheral settings
|
|
|
|
|
func (f *MIDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) {
|
|
|
|
|
// Return the specified peripheral
|
2025-11-14 20:19:44 +01:00
|
|
|
peripheral, found := f.detected[peripheralID]
|
2025-01-26 12:01:31 +01:00
|
|
|
if !found {
|
2025-11-14 20:19:44 +01:00
|
|
|
// FTDI not detected, return the last settings saved
|
|
|
|
|
if savedPeripheral, isFound := f.saved[peripheralID]; isFound {
|
|
|
|
|
return savedPeripheral.Settings, nil
|
|
|
|
|
}
|
2025-01-26 12:01:31 +01:00
|
|
|
return nil, fmt.Errorf("unable to found the peripheral")
|
|
|
|
|
}
|
|
|
|
|
return peripheral.GetSettings(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetPeripheralSettings sets the peripheral settings
|
2025-11-14 20:19:44 +01:00
|
|
|
func (f *MIDIFinder) SetPeripheralSettings(ctx context.Context, peripheralID string, settings map[string]interface{}) error {
|
2025-01-18 14:53:29 +00:00
|
|
|
// Return the specified peripheral
|
2025-11-14 20:19:44 +01:00
|
|
|
peripheral, found := f.detected[peripheralID]
|
2025-01-18 14:53:29 +00:00
|
|
|
if !found {
|
2025-11-14 20:19:44 +01:00
|
|
|
return fmt.Errorf("unable to found the MIDI peripheral")
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-14 20:19:44 +01:00
|
|
|
log.Debug().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the MIDI finder")
|
2025-01-26 12:01:31 +01:00
|
|
|
return peripheral.SetSettings(settings)
|
2025-01-18 14:53:29 +00: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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ForceScan explicily asks for scanning peripherals
|
|
|
|
|
func (f *MIDIFinder) ForceScan() {
|
2025-11-14 20:19:44 +01:00
|
|
|
// f.scanChannel <- struct{}{}
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// scanPeripherals scans the MIDI peripherals
|
|
|
|
|
func (f *MIDIFinder) scanPeripherals(ctx context.Context) error {
|
2025-11-14 20:19:44 +01:00
|
|
|
currentMap := make(map[string]*MIDIPeripheral)
|
|
|
|
|
|
|
|
|
|
drv, err := rtmididrv.New()
|
2025-01-18 14:53:29 +00:00
|
|
|
if err != nil {
|
2025-11-14 20:19:44 +01:00
|
|
|
return fmt.Errorf("unable to open the MIDI driver")
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-14 20:19:44 +01:00
|
|
|
defer drv.Close()
|
|
|
|
|
|
|
|
|
|
// Get MIDI INPUT ports
|
|
|
|
|
ins, err := drv.Ins()
|
2025-01-18 14:53:29 +00:00
|
|
|
if err != nil {
|
2025-11-14 20:19:44 +01:00
|
|
|
return fmt.Errorf("unable to scan MIDI IN ports: %s", err)
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-14 20:19:44 +01:00
|
|
|
|
|
|
|
|
for _, port := range ins {
|
|
|
|
|
// Exclude microsoft wavetable from the list
|
|
|
|
|
if strings.Contains(port.String(), "GS Wavetable") {
|
|
|
|
|
continue
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-14 20:19:44 +01:00
|
|
|
|
|
|
|
|
baseName := normalizeName(port.String())
|
|
|
|
|
|
|
|
|
|
if _, ok := currentMap[baseName]; !ok {
|
|
|
|
|
currentMap[baseName] = &MIDIPeripheral{
|
|
|
|
|
info: PeripheralInfo{
|
|
|
|
|
Name: baseName,
|
|
|
|
|
SerialNumber: baseName,
|
|
|
|
|
ProtocolName: "MIDI",
|
|
|
|
|
},
|
|
|
|
|
}
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-14 20:19:44 +01:00
|
|
|
|
|
|
|
|
currentMap[baseName].inputPorts = append(currentMap[baseName].inputPorts, port)
|
|
|
|
|
log.Info().Any("peripherals", currentMap).Msg("available MIDI IN ports")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get MIDI OUTPUT ports
|
|
|
|
|
outs, err := drv.Outs()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("unable to scan MIDI OUT ports: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, port := range outs {
|
|
|
|
|
// Exclude microsoft wavetable from the list
|
|
|
|
|
if strings.Contains(port.String(), "GS Wavetable") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
baseName := normalizeName(port.String())
|
|
|
|
|
|
|
|
|
|
if _, ok := currentMap[baseName]; !ok {
|
|
|
|
|
currentMap[baseName] = &MIDIPeripheral{
|
|
|
|
|
info: PeripheralInfo{
|
|
|
|
|
Name: baseName,
|
|
|
|
|
SerialNumber: baseName,
|
|
|
|
|
ProtocolName: "MIDI",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentMap[baseName].outputsPorts = append(currentMap[baseName].outputsPorts, port)
|
|
|
|
|
log.Info().Any("peripherals", currentMap).Msg("available MIDI OUT ports")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Debug().Any("value", currentMap).Msg("MIDI peripherals map")
|
|
|
|
|
|
|
|
|
|
// Detect arrivals
|
|
|
|
|
for sn, discovery := range currentMap {
|
|
|
|
|
if _, known := f.detected[sn]; !known {
|
|
|
|
|
|
|
|
|
|
peripheral := NewMIDIPeripheral(discovery.info, discovery.inputPorts, discovery.outputsPorts)
|
|
|
|
|
|
|
|
|
|
f.detected[sn] = peripheral
|
|
|
|
|
|
|
|
|
|
if f.onArrival != nil {
|
|
|
|
|
f.onArrival(discovery.GetInfo())
|
|
|
|
|
}
|
|
|
|
|
log.Info().Str("sn", sn).Str("name", discovery.GetInfo().SerialNumber).Msg("[MIDI] New peripheral detected")
|
|
|
|
|
|
|
|
|
|
// If the peripheral is saved in the project => connect
|
|
|
|
|
if _, saved := f.saved[sn]; saved {
|
|
|
|
|
f.wg.Add(1)
|
|
|
|
|
go func(p PeripheralInfo) {
|
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
err := peripheral.Connect(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to connect the MIDI peripheral")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = peripheral.Activate(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("sn", p.SerialNumber).Msg("unable to activate the MIDI peripheral")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}(discovery.GetInfo())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Detect removals
|
|
|
|
|
for sn, oldPeripheral := range f.detected {
|
|
|
|
|
if _, still := currentMap[sn]; !still {
|
|
|
|
|
|
|
|
|
|
// Properly clean the DMX device
|
|
|
|
|
err := oldPeripheral.Deactivate(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("sn", sn).Msg("unable to deactivate the MIDI peripheral after disconnection")
|
|
|
|
|
}
|
|
|
|
|
err = oldPeripheral.Disconnect(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("sn", sn).Msg("unable to disconnect the MIDI peripheral after disconnection")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete it from the detected list
|
|
|
|
|
delete(f.detected, sn)
|
|
|
|
|
log.Info().Str("sn", sn).Str("name", oldPeripheral.GetInfo().Name).Msg("[MIDI] peripheral removed")
|
|
|
|
|
|
|
|
|
|
// Execute the removal callback
|
|
|
|
|
if f.onRemoval != nil {
|
|
|
|
|
f.onRemoval(oldPeripheral.GetInfo())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-18 14:53:29 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 20:19:44 +01:00
|
|
|
func normalizeName(raw string) string {
|
|
|
|
|
name := strings.TrimSpace(raw)
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-14 20:19:44 +01:00
|
|
|
// Si parenthèses, prendre le texte à l'intérieur
|
|
|
|
|
start := strings.Index(name, "(")
|
|
|
|
|
end := strings.LastIndex(name, ")")
|
|
|
|
|
if start != -1 && end != -1 && start < end {
|
|
|
|
|
name = name[start+1 : end]
|
|
|
|
|
return strings.TrimSpace(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sinon, supprimer le dernier mot s'il est un entier
|
|
|
|
|
parts := strings.Fields(name) // découpe en mots
|
|
|
|
|
if len(parts) > 1 {
|
|
|
|
|
if _, err := strconv.Atoi(parts[len(parts)-1]); err == nil {
|
|
|
|
|
parts = parts[:len(parts)-1] // retirer le dernier mot
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.Join(parts, " ")
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-14 20:19:44 +01:00
|
|
|
|
|
|
|
|
// func normalizeName(name string) string {
|
|
|
|
|
// // Cas communs : "MIDIIN2 (XXX)" → "XXX"
|
|
|
|
|
// if strings.Contains(name, "(") && strings.Contains(name, ")") {
|
|
|
|
|
// start := strings.Index(name, "(")
|
|
|
|
|
// end := strings.Index(name, ")")
|
|
|
|
|
// if start < end {
|
|
|
|
|
// return strings.TrimSpace(name[start+1 : end])
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Sinon, on retourne tel quel
|
|
|
|
|
// return strings.TrimSpace(name)
|
|
|
|
|
// }
|