package hardware import ( "context" "fmt" "sync" "github.com/rs/zerolog/log" "github.com/wailsapp/wails/v2/pkg/runtime" "gitlab.com/gomidi/midi" ) // MIDIDevice represents the device to control type MIDIDevice struct { ID string // Device ID Mapping MappingInfo // Device mapping configuration } // ------------------- // // MIDIPeripheral contains the data of a MIDI peripheral type MIDIPeripheral struct { wg sync.WaitGroup inputPorts []midi.In outputsPorts []midi.Out info PeripheralInfo // The peripheral info settings map[string]interface{} // The settings of the peripheral devices []MIDIDevice // All the MIDI devices that the peripheral can handle } // NewMIDIPeripheral creates a new MIDI peripheral func NewMIDIPeripheral(peripheralData PeripheralInfo, inputs []midi.In, outputs []midi.Out) *MIDIPeripheral { log.Trace().Str("file", "MIDIPeripheral").Str("name", peripheralData.Name).Str("s/n", peripheralData.SerialNumber).Msg("MIDI peripheral created") return &MIDIPeripheral{ info: peripheralData, inputPorts: inputs, outputsPorts: outputs, settings: peripheralData.Settings, } } // Connect connects the MIDI peripheral func (p *MIDIPeripheral) Connect(ctx context.Context) error { runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusConnecting) // Open input ports for _, port := range p.inputPorts { err := port.Open() if err != nil { runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusDisconnected) return fmt.Errorf("unable to open the MIDI IN port: %w", err) } port.SetListener(func(msg []byte, delta int64) { // Emit the event to the front runtime.EventsEmit(ctx, string(PeripheralEventEmitted), p.info.SerialNumber, msg) log.Debug().Str("message", string(msg)).Int64("delta", delta).Msg("message received") }) log.Info().Str("name", port.String()).Msg("port open successfully") } p.wg.Add(1) go func() { defer p.wg.Done() <-ctx.Done() _ = p.Disconnect(ctx) }() runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusDeactivated) return nil } // Disconnect disconnects the MIDI peripheral func (p *MIDIPeripheral) Disconnect(ctx context.Context) error { // Close all inputs ports for _, port := range p.inputPorts { err := port.Close() if err != nil { return fmt.Errorf("unable to close the MIDI IN port: %w", err) } } runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusDisconnected) return nil } // Activate activates the MIDI peripheral func (p *MIDIPeripheral) Activate(ctx context.Context) error { runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusActivated) return nil } // Deactivate deactivates the MIDI peripheral func (p *MIDIPeripheral) Deactivate(ctx context.Context) error { runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusDeactivated) return nil } // SetSettings sets a specific setting for this peripheral func (p *MIDIPeripheral) SetSettings(ctx context.Context, settings map[string]any) error { p.settings = settings return nil } // SetDeviceProperty - not implemented for this kind of peripheral func (p *MIDIPeripheral) SetDeviceProperty(context.Context, uint32, byte) error { return nil } // GetSettings gets the peripheral settings func (p *MIDIPeripheral) GetSettings() map[string]interface{} { return p.settings } // GetInfo gets the peripheral information func (p *MIDIPeripheral) GetInfo() PeripheralInfo { return p.info } // WaitStop wait about the peripheral to close func (p *MIDIPeripheral) WaitStop() error { log.Info().Str("file", "MIDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("waiting for MIDI peripheral to close...") p.wg.Wait() log.Info().Str("file", "MIDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("MIDI peripheral closed!") return nil }