Files
dmxconnect/hardware/MIDIPeripheral.go

130 lines
3.9 KiB
Go
Raw Normal View History

package hardware
import (
"context"
"fmt"
"sync"
"github.com/rs/zerolog/log"
"github.com/wailsapp/wails/v2/pkg/runtime"
"gitlab.com/gomidi/midi"
)
2025-11-16 21:44:48 +01:00
// 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
2025-11-16 21:44:48 +01:00
info PeripheralInfo // The peripheral info
settings map[string]interface{} // The settings of the peripheral
2025-11-16 21:44:48 +01:00
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 {
2025-01-26 12:01:31 +01:00
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
}
2025-01-26 12:01:31 +01:00
// SetSettings sets a specific setting for this peripheral
func (p *MIDIPeripheral) SetSettings(settings map[string]interface{}) error {
p.settings = settings
return nil
}
// SetDeviceProperty - not implemented for this kind of peripheral
func (p *MIDIPeripheral) SetDeviceProperty(context.Context, uint32, 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 {
2025-01-26 12:01:31 +01:00
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
}