generated from thinkode/modelRepository
Closes #34 Implement MIDI peripherals Implement device concept Cleaning project Reviewed-on: #35
141 lines
4.3 KiB
Go
141 lines
4.3 KiB
Go
package genericmidi
|
|
|
|
import (
|
|
"context"
|
|
"dmxconnect/hardware"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
"gitlab.com/gomidi/midi"
|
|
)
|
|
|
|
// Device represents the device to control
|
|
type Device struct {
|
|
ID string // Device ID
|
|
Mapping hardware.MappingInfo // Device mapping configuration
|
|
}
|
|
|
|
// ------------------- //
|
|
|
|
// Endpoint contains the data of a MIDI endpoint
|
|
type Endpoint struct {
|
|
wg sync.WaitGroup
|
|
|
|
inputPorts []midi.In
|
|
outputsPorts []midi.Out
|
|
info hardware.EndpointInfo // The endpoint info
|
|
settings map[string]interface{} // The settings of the endpoint
|
|
|
|
devices []Device // All the MIDI devices that the endpoint can handle
|
|
}
|
|
|
|
// NewEndpoint creates a new MIDI endpoint
|
|
func NewEndpoint(endpointData hardware.EndpointInfo, inputs []midi.In, outputs []midi.Out) *Endpoint {
|
|
log.Trace().Str("file", "MIDIEndpoint").Str("name", endpointData.Name).Str("s/n", endpointData.SerialNumber).Msg("MIDI endpoint created")
|
|
return &Endpoint{
|
|
info: endpointData,
|
|
inputPorts: inputs,
|
|
outputsPorts: outputs,
|
|
settings: endpointData.Settings,
|
|
}
|
|
}
|
|
|
|
// SetDeviceArrivalCallback is called when we need to add a new device to the hardware
|
|
func (p *Endpoint) SetDeviceArrivalCallback(adc func(context.Context, hardware.DeviceInfo, hardware.Endpoint) error) {
|
|
|
|
}
|
|
|
|
// SetDeviceRemovalCallback is called when we need to remove a device from the hardware
|
|
func (p *Endpoint) SetDeviceRemovalCallback(rdc func(context.Context, string) error) {
|
|
|
|
}
|
|
|
|
// Connect connects the MIDI endpoint
|
|
func (p *Endpoint) Connect(ctx context.Context) error {
|
|
runtime.EventsEmit(ctx, string(hardware.EndpointStatusUpdated), p.GetInfo(), hardware.EndpointStatusConnecting)
|
|
|
|
// Open input ports
|
|
for _, port := range p.inputPorts {
|
|
err := port.Open()
|
|
if err != nil {
|
|
runtime.EventsEmit(ctx, string(hardware.EndpointStatusUpdated), p.GetInfo(), hardware.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(hardware.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(hardware.EndpointStatusUpdated), p.GetInfo(), hardware.EndpointStatusDeactivated)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Disconnect disconnects the MIDI endpoint
|
|
func (p *Endpoint) 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(hardware.EndpointStatusUpdated), p.GetInfo(), hardware.EndpointStatusDisconnected)
|
|
return nil
|
|
}
|
|
|
|
// Activate activates the MIDI endpoint
|
|
func (p *Endpoint) Activate(ctx context.Context) error {
|
|
runtime.EventsEmit(ctx, string(hardware.EndpointStatusUpdated), p.GetInfo(), hardware.EndpointStatusActivated)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Deactivate deactivates the MIDI endpoint
|
|
func (p *Endpoint) Deactivate(ctx context.Context) error {
|
|
runtime.EventsEmit(ctx, string(hardware.EndpointStatusUpdated), p.GetInfo(), hardware.EndpointStatusDeactivated)
|
|
|
|
return nil
|
|
}
|
|
|
|
// SetSettings sets a specific setting for this endpoint
|
|
func (p *Endpoint) SetSettings(ctx context.Context, settings map[string]any) error {
|
|
p.settings = settings
|
|
return nil
|
|
}
|
|
|
|
// SetDeviceProperty - not implemented for this kind of endpoint
|
|
func (p *Endpoint) SetDeviceProperty(context.Context, uint32, byte) error {
|
|
return nil
|
|
}
|
|
|
|
// GetSettings gets the endpoint settings
|
|
func (p *Endpoint) GetSettings() map[string]interface{} {
|
|
return p.settings
|
|
}
|
|
|
|
// GetInfo gets the endpoint information
|
|
func (p *Endpoint) GetInfo() hardware.EndpointInfo {
|
|
return p.info
|
|
}
|
|
|
|
// WaitStop wait about the endpoint to close
|
|
func (p *Endpoint) 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
|
|
}
|