diff options
author | Christian Pointner <equinox@spreadspace.org> | 2019-02-09 22:33:01 +0100 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2019-02-09 22:33:01 +0100 |
commit | 22d8f5888d062b0093b58bae934a6d78ad9ac846 (patch) | |
tree | 8ff985e37b7e5e67e6b229d0a76d1af380196f90 /cmd/dolmetschctld/statemachine.go | |
parent | language selection now returns result (diff) |
languages now have a state
Diffstat (limited to 'cmd/dolmetschctld/statemachine.go')
-rw-r--r-- | cmd/dolmetschctld/statemachine.go | 78 |
1 files changed, 56 insertions, 22 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go index 2aba4bb..97f29c0 100644 --- a/cmd/dolmetschctld/statemachine.go +++ b/cmd/dolmetschctld/statemachine.go @@ -23,7 +23,7 @@ package main import ( - "errors" + "fmt" "log" "spreadspace.org/dolmetschctl/pkg/mixer" @@ -52,9 +52,22 @@ func (s State) String() string { type Language string +func (l Language) String() string { + if l == "" { + return "none" + } + return string(l) +} + +type LanguageChannel struct { + Num mixer.Channel + currentLevel mixer.FaderLevel + muted bool +} + type LanguageChannels struct { - main mixer.Channel - voice mixer.Channel + main LanguageChannel + voice LanguageChannel } type selectLangRequest struct { @@ -68,34 +81,18 @@ type StateMachine struct { quitCh chan bool exitedCh chan struct{} - languages map[Language]LanguageChannels + languages map[Language]LanguageChannels + channel2lang map[mixer.Channel]Language currentState State currentLang Language targetLang Language } -func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { - sm := &StateMachine{mixer: m} - sm.selectLangCh = make(chan selectLangRequest, 10) - sm.quitCh = make(chan bool, 1) - sm.exitedCh = make(chan struct{}) - - sm.languages = make(map[Language]LanguageChannels) - sm.languages["en"] = LanguageChannels{0x20, 0x24} - sm.languages["de"] = LanguageChannels{0x22, 0x25} - - sm.currentState = StateNew - sm.currentLang = "" - sm.targetLang = "" - - return sm, nil -} - func (sm *StateMachine) selectLang(l Language) error { if l != "" { if _, exists := sm.languages[l]; !exists { - return errors.New("language '" + string(l) + "' does not exist") + return fmt.Errorf("language '%s' does not exist", l) } } sm.targetLang = l @@ -130,6 +127,43 @@ func (sm *StateMachine) run() { } } +func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { + sm := &StateMachine{mixer: m} + sm.selectLangCh = make(chan selectLangRequest, 10) + sm.quitCh = make(chan bool, 1) + sm.exitedCh = make(chan struct{}) + + sm.languages = make(map[Language]LanguageChannels) + sm.channel2lang = make(map[mixer.Channel]Language) + + sm.currentState = StateNew + sm.currentLang = "" + sm.targetLang = "" + + return sm, nil +} + +func (sm *StateMachine) AddLanguage(name Language, main, voice mixer.Channel) error { + if name == "none" { + return fmt.Errorf("language 'none' is reserved") + } + if _, exists := sm.languages[name]; exists { + return fmt.Errorf("language '%s' already exists", name) + } + for ch := range []mixer.Channel{main, voice} { + if l, exists := sm.languages[name]; exists { + return fmt.Errorf("mixer channel %v is already in use by language '%s'", ch, l) + } + } + + chMain := LanguageChannel{main, mixer.FaderLevel0db, false} + chVoice := LanguageChannel{voice, mixer.FaderLevel0db, false} + sm.languages[name] = LanguageChannels{chMain, chVoice} + sm.channel2lang[main] = name + sm.channel2lang[voice] = name + return nil +} + func (sm *StateMachine) Start() { go sm.run() } |