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 } // ------------------- // // MIDIEndpoint contains the data of a MIDI endpoint type MIDIEndpoint struct { wg sync.WaitGroup inputPorts []midi.In outputsPorts []midi.Out info EndpointInfo // The endpoint info settings map[string]interface{} // The settings of the endpoint devices []MIDIDevice // All the MIDI devices that the endpoint can handle } // NewMIDIEndpoint creates a new MIDI endpoint func NewMIDIEndpoint(endpointData EndpointInfo, inputs []midi.In, outputs []midi.Out) *MIDIEndpoint { log.Trace().Str("file", "MIDIEndpoint").Str("name", endpointData.Name).Str("s/n", endpointData.SerialNumber).Msg("MIDI endpoint created") return &MIDIEndpoint{ info: endpointData, inputPorts: inputs, outputsPorts: outputs, settings: endpointData.Settings, } } // Connect connects the MIDI endpoint func (p *MIDIEndpoint) Connect(ctx context.Context) error { runtime.EventsEmit(ctx, string(EndpointStatusUpdated), p.GetInfo(), EndpointStatusConnecting) // Open input ports for _, port := range p.inputPorts { err := port.Open() if err != nil { runtime.EventsEmit(ctx, string(EndpointStatusUpdated), p.GetInfo(), EndpointStatusDisconnected) 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(EndpointEventEmitted), 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(EndpointStatusUpdated), p.GetInfo(), EndpointStatusDeactivated) return nil } // Disconnect disconnects the MIDI endpoint func (p *MIDIEndpoint) 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(EndpointStatusUpdated), p.GetInfo(), EndpointStatusDisconnected) return nil } // Activate activates the MIDI endpoint func (p *MIDIEndpoint) Activate(ctx context.Context) error { runtime.EventsEmit(ctx, string(EndpointStatusUpdated), p.GetInfo(), EndpointStatusActivated) return nil } // Deactivate deactivates the MIDI endpoint func (p *MIDIEndpoint) Deactivate(ctx context.Context) error { runtime.EventsEmit(ctx, string(EndpointStatusUpdated), p.GetInfo(), EndpointStatusDeactivated) return nil } // SetSettings sets a specific setting for this endpoint func (p *MIDIEndpoint) SetSettings(ctx context.Context, settings map[string]any) error { p.settings = settings return nil } // SetDeviceProperty - not implemented for this kind of endpoint func (p *MIDIEndpoint) SetDeviceProperty(context.Context, uint32, byte) error { return nil } // GetSettings gets the endpoint settings func (p *MIDIEndpoint) GetSettings() map[string]interface{} { return p.settings } // GetInfo gets the endpoint information func (p *MIDIEndpoint) GetInfo() EndpointInfo { return p.info } // WaitStop wait about the endpoint to close func (p *MIDIEndpoint) WaitStop() error { log.Info().Str("file", "MIDIEndpoint").Str("s/n", p.info.SerialNumber).Msg("waiting for MIDI endpoint to close...") p.wg.Wait() log.Info().Str("file", "MIDIEndpoint").Str("s/n", p.info.SerialNumber).Msg("MIDI endpoint closed!") return nil }