diff options
Diffstat (limited to 'cmd/dolmetschctld')
-rw-r--r-- | cmd/dolmetschctld/statemachine.go | 59 | ||||
-rw-r--r-- | cmd/dolmetschctld/telnet.go | 16 |
2 files changed, 55 insertions, 20 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go index 5ded49f..5e27359 100644 --- a/cmd/dolmetschctld/statemachine.go +++ b/cmd/dolmetschctld/statemachine.go @@ -76,21 +76,31 @@ type MixerChannels struct { voice MixerChannel } -type selectLangRequest struct { +type setLanguageRequest struct { lang Language resultCh chan error } +type getLanguageRequest struct { + resultCh chan Language +} + type setMain2VoiceRatioRequest struct { ratio float32 resultCh chan error } +type getMain2VoiceRatioRequest struct { + resultCh chan float32 +} + type StateMachine struct { mixer *mixer.Mixer - selectLangCh chan selectLangRequest + setLanguageCh chan setLanguageRequest + getLanguageCh chan getLanguageRequest setMain2VoiceRatioCh chan setMain2VoiceRatioRequest + getMain2VoiceRatioCh chan getMain2VoiceRatioRequest quitCh chan bool exitedCh chan struct{} mixerEventCh chan mixer.Event @@ -101,8 +111,7 @@ type StateMachine struct { main2VoiceRatio float32 currentState State - currentLang Language - targetLang Language + language Language } func (sm *StateMachine) handleMixerEvent(ev mixer.Event) { @@ -147,20 +156,19 @@ func (sm *StateMachine) initMixer() { sm.mixer.SetLevel(mcs.voice.num, mixer.FaderLevelOff) mcs.voice.target.level = mixer.FaderLevelOff } - sm.currentLang = "" - sm.targetLang = "" + sm.language = "" sm.currentState = StateSettled } // the "current language" is what is currently spoken on stage -func (sm *StateMachine) selectLang(l Language) error { +func (sm *StateMachine) setLanguage(l Language) error { if l != "" { if _, exists := sm.languages[l]; !exists { return fmt.Errorf("language '%s' does not exist", l) } } - sm.targetLang = l - log.Printf("new target language: '%s'", sm.targetLang) + sm.language = l + log.Printf("new target language: '%s'", sm.language) return nil } @@ -187,7 +195,7 @@ func calcNextLevel(target, current mixer.FaderLevel) mixer.FaderLevel { func (sm *StateMachine) reconcile(ticker bool) { for lang, mcs := range sm.languages { - if sm.targetLang == "" || lang == sm.targetLang || mcs.voice.current.mute == mixer.MuteMuted { + if sm.language == "" || lang == sm.language || mcs.voice.current.mute == mixer.MuteMuted { mcs.main.target.level = mixer.FaderLevel0db mcs.voice.target.level = mixer.FaderLevelOff } else { @@ -226,12 +234,16 @@ func (sm *StateMachine) run() { if sm.currentState == StateSettling { sm.reconcile(true) } - case req := <-sm.selectLangCh: - req.resultCh <- sm.selectLang(req.lang) + case req := <-sm.setLanguageCh: + req.resultCh <- sm.setLanguage(req.lang) sm.reconcile(false) + case req := <-sm.getLanguageCh: + req.resultCh <- sm.language case req := <-sm.setMain2VoiceRatioCh: req.resultCh <- sm.setMain2VoiceRatio(req.ratio) sm.reconcile(false) + case req := <-sm.getMain2VoiceRatioCh: + req.resultCh <- sm.main2VoiceRatio case ev := <-sm.mixerEventCh: sm.handleMixerEvent(ev) sm.reconcile(false) @@ -243,8 +255,10 @@ func (sm *StateMachine) run() { func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { sm := &StateMachine{mixer: m} - sm.selectLangCh = make(chan selectLangRequest, 10) + sm.setLanguageCh = make(chan setLanguageRequest, 10) + sm.getLanguageCh = make(chan getLanguageRequest, 10) sm.setMain2VoiceRatioCh = make(chan setMain2VoiceRatioRequest, 10) + sm.getMain2VoiceRatioCh = make(chan getMain2VoiceRatioRequest, 10) sm.quitCh = make(chan bool, 1) sm.exitedCh = make(chan struct{}) sm.mixerEventCh = make(chan mixer.Event, 1000) @@ -253,8 +267,7 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { sm.channel2lang = make(map[mixer.Channel]Language) sm.currentState = StateNew - sm.currentLang = "" - sm.targetLang = "" + sm.language = "" return sm, nil } @@ -294,9 +307,15 @@ func (sm *StateMachine) Start() { go sm.run() } -func (sm *StateMachine) SelectLanguage(l Language) error { +func (sm *StateMachine) SetLanguage(l Language) error { resultCh := make(chan error) - sm.selectLangCh <- selectLangRequest{l, resultCh} + sm.setLanguageCh <- setLanguageRequest{l, resultCh} + return <-resultCh +} + +func (sm *StateMachine) GetLanguage() Language { + resultCh := make(chan Language) + sm.getLanguageCh <- getLanguageRequest{resultCh} return <-resultCh } @@ -306,6 +325,12 @@ func (sm *StateMachine) SetMain2VoiceRatio(r float32) error { return <-resultCh } +func (sm *StateMachine) GetMain2VoiceRatio() float32 { + resultCh := make(chan float32) + sm.getMain2VoiceRatioCh <- getMain2VoiceRatioRequest{resultCh} + return <-resultCh +} + func (sm *StateMachine) Shutdown() { select { case sm.quitCh <- true: diff --git a/cmd/dolmetschctld/telnet.go b/cmd/dolmetschctld/telnet.go index 19e0a26..901c226 100644 --- a/cmd/dolmetschctld/telnet.go +++ b/cmd/dolmetschctld/telnet.go @@ -33,25 +33,35 @@ type TelnetInterface struct { } func telnetCmdLang(c *telgo.Client, args []string, sm *StateMachine) bool { - if len(args) != 2 { + if len(args) > 2 { c.Sayln("usage: lang <language>") return false } + if len(args) == 1 { + c.Sayln("current language: %s", sm.GetLanguage()) + return false + } + lang := args[1] if lang == "none" { lang = "" } - if err := sm.SelectLanguage(Language(lang)); err != nil { + if err := sm.SetLanguage(Language(lang)); err != nil { c.Sayln("selecting language failed: %v", err) } return false } func telnetCmdRatio(c *telgo.Client, args []string, sm *StateMachine) bool { - if len(args) != 2 { + if len(args) > 2 { c.Sayln("usage: ratio <ratio>") return false } + if len(args) == 1 { + c.Sayln("current ratio: %1.3f", sm.GetMain2VoiceRatio()) + return false + } + r, err := strconv.ParseFloat(args[1], 32) if err != nil { c.Sayln("invalid ratio: %v", err) |