mirror of
https://github.com/vsariola/sointu.git
synced 2026-02-13 11:43:19 -05:00
feat(tracker): enum-style values and menus to choose one option
This commit is contained in:
parent
ca4b87d43d
commit
4bb5df9c87
162
tracker/midi.go
162
tracker/midi.go
@ -2,27 +2,123 @@ package tracker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
MIDIModel Model
|
||||
MIDIContext interface {
|
||||
InputDevices(yield func(deviceName string) bool)
|
||||
Open(deviceName string) error
|
||||
Close()
|
||||
IsOpen() bool
|
||||
}
|
||||
)
|
||||
type MIDIModel Model
|
||||
|
||||
func (m *Model) MIDI() *MIDIModel { return (*MIDIModel)(m) }
|
||||
|
||||
type (
|
||||
midiState struct {
|
||||
currentInput MIDIInputDevice
|
||||
context MIDIContext
|
||||
inputs []MIDIInputDevice
|
||||
}
|
||||
|
||||
MIDIContext interface {
|
||||
Inputs(yield func(input MIDIInputDevice) bool)
|
||||
Close()
|
||||
Support() MIDISupport
|
||||
}
|
||||
|
||||
MIDIInputDevice interface {
|
||||
Open() error
|
||||
Close() error
|
||||
IsOpen() bool
|
||||
String() string
|
||||
}
|
||||
|
||||
MIDISupport int
|
||||
)
|
||||
|
||||
const (
|
||||
MIDISupportNotCompiled MIDISupport = iota
|
||||
MIDISupportNoDriver
|
||||
MIDISupported
|
||||
)
|
||||
|
||||
// Refresh
|
||||
func (m *MIDIModel) Refresh() Action { return MakeAction((*midiRefresh)(m)) }
|
||||
|
||||
type midiRefresh MIDIModel
|
||||
|
||||
func (m *midiRefresh) Do() {
|
||||
if m.midi.context == nil {
|
||||
return
|
||||
}
|
||||
m.midi.inputs = m.midi.inputs[:0]
|
||||
for i := range m.midi.context.Inputs {
|
||||
m.midi.inputs = append(m.midi.inputs, i)
|
||||
if m.midi.currentInput != nil && i.String() == m.midi.currentInput.String() {
|
||||
m.midi.currentInput.Close()
|
||||
m.midi.currentInput = nil
|
||||
if err := i.Open(); err != nil {
|
||||
(*Model)(m).Alerts().Add(fmt.Sprintf("Failed to reopen MIDI input port: %s", err.Error()), Error)
|
||||
continue
|
||||
}
|
||||
m.midi.currentInput = i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// InputDevices can be iterated to get string names of all the MIDI input
|
||||
// devices.
|
||||
func (m *MIDIModel) InputDevices(yield func(deviceName string) bool) { m.midi.InputDevices(yield) }
|
||||
func (m *MIDIModel) Input() Int { return MakeInt((*midiInputDevices)(m)) }
|
||||
|
||||
// IsOpen returns true if a midi device is currently open.
|
||||
func (m *MIDIModel) IsOpen() bool { return m.midi.IsOpen() }
|
||||
type midiInputDevices MIDIModel
|
||||
|
||||
func (m *midiInputDevices) Value() int {
|
||||
if m.midi.currentInput == nil {
|
||||
return 0
|
||||
}
|
||||
for i, d := range m.midi.inputs {
|
||||
if d == m.midi.currentInput {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
func (m *midiInputDevices) SetValue(val int) bool {
|
||||
if val < 0 || val > len(m.midi.inputs) {
|
||||
return false
|
||||
}
|
||||
if m.midi.currentInput != nil {
|
||||
if err := m.midi.currentInput.Close(); err != nil {
|
||||
(*Model)(m).Alerts().Add(fmt.Sprintf("Failed to close current MIDI input port: %s", err.Error()), Error)
|
||||
}
|
||||
m.midi.currentInput = nil
|
||||
}
|
||||
if val == 0 {
|
||||
return true
|
||||
}
|
||||
newInput := m.midi.inputs[val-1]
|
||||
if err := newInput.Open(); err != nil {
|
||||
(*Model)(m).Alerts().Add(fmt.Sprintf("Failed to open MIDI input port: %s", err.Error()), Error)
|
||||
return false
|
||||
}
|
||||
m.midi.currentInput = newInput
|
||||
(*Model)(m).Alerts().Add(fmt.Sprintf("Opened MIDI input port: %s", newInput.String()), Info)
|
||||
return true
|
||||
}
|
||||
func (m *midiInputDevices) Range() RangeInclusive {
|
||||
return RangeInclusive{Min: 0, Max: len(m.midi.inputs)}
|
||||
}
|
||||
func (m *midiInputDevices) StringOf(value int) string {
|
||||
if value < 0 || value > len(m.midi.inputs) {
|
||||
return ""
|
||||
}
|
||||
if value == 0 {
|
||||
switch m.midi.context.Support() {
|
||||
case MIDISupportNotCompiled:
|
||||
return "Not compiled"
|
||||
case MIDISupportNoDriver:
|
||||
return "No driver"
|
||||
default:
|
||||
return "Closed"
|
||||
}
|
||||
}
|
||||
return m.midi.inputs[value-1].String()
|
||||
}
|
||||
|
||||
// InputtingNotes returns a Bool controlling whether the MIDI events are used
|
||||
// just to trigger instruments, or if the note events are used to input notes to
|
||||
@ -34,44 +130,10 @@ type midiInputtingNotes Model
|
||||
func (m *midiInputtingNotes) Value() bool { return m.broker.mIDIEventsToGUI.Load() }
|
||||
func (m *midiInputtingNotes) SetValue(val bool) { m.broker.mIDIEventsToGUI.Store(val) }
|
||||
|
||||
// Open returns an Action to open the MIDI input device with a given name.
|
||||
func (m *MIDIModel) Open(deviceName string) Action {
|
||||
return MakeAction(openMIDI{Item: deviceName, Model: (*Model)(m)})
|
||||
}
|
||||
|
||||
type openMIDI struct {
|
||||
Item string
|
||||
*Model
|
||||
}
|
||||
|
||||
func (s openMIDI) Do() {
|
||||
m := s.Model
|
||||
if err := s.Model.midi.Open(s.Item); err == nil {
|
||||
message := fmt.Sprintf("Opened MIDI device: %s", s.Item)
|
||||
m.Alerts().Add(message, Info)
|
||||
} else {
|
||||
message := fmt.Sprintf("Could not open MIDI device: %s", s.Item)
|
||||
m.Alerts().Add(message, Error)
|
||||
}
|
||||
}
|
||||
|
||||
// FindMIDIDeviceByPrefix finds the MIDI input device whose name starts with the given
|
||||
// prefix. It returns the full device name and true if found, or an empty string
|
||||
// and false if not found.
|
||||
func FindMIDIDeviceByPrefix(c MIDIContext, prefix string) (deviceName string, ok bool) {
|
||||
for input := range c.InputDevices {
|
||||
if strings.HasPrefix(input, prefix) {
|
||||
return input, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// NullMIDIContext is a mockup MIDIContext if you don't want to create a real
|
||||
// one.
|
||||
type NullMIDIContext struct{}
|
||||
|
||||
func (m NullMIDIContext) InputDevices(yield func(string) bool) {}
|
||||
func (m NullMIDIContext) Open(deviceName string) error { return nil }
|
||||
func (m NullMIDIContext) Close() {}
|
||||
func (m NullMIDIContext) IsOpen() bool { return false }
|
||||
func (m NullMIDIContext) Inputs(yield func(input MIDIInputDevice) bool) {}
|
||||
func (m NullMIDIContext) Close() {}
|
||||
func (m NullMIDIContext) Support() MIDISupport { return MIDISupportNotCompiled }
|
||||
|
||||
Reference in New Issue
Block a user