generated from thinkode/modelRepository
add MIDI discovery, connection and events
This commit is contained in:
@@ -2,43 +2,91 @@ package hardware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
"gitlab.com/gomidi/midi"
|
||||
)
|
||||
|
||||
// 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
|
||||
location int // The location of the peripheral
|
||||
settings map[string]interface{} // The settings of the peripheral
|
||||
}
|
||||
|
||||
// NewMIDIPeripheral creates a new MIDI peripheral
|
||||
func NewMIDIPeripheral(peripheralData PeripheralInfo) (*MIDIPeripheral, error) {
|
||||
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,
|
||||
settings: peripheralData.Settings,
|
||||
}, nil
|
||||
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() error {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -62,3 +110,11 @@ func (p *MIDIPeripheral) GetSettings() map[string]interface{} {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user