diff options
author | Christian Pointner <equinox@spreadspace.org> | 2019-02-11 00:19:06 +0100 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2019-02-11 00:19:06 +0100 |
commit | b8aa110705d2ef81028386f5d7bd1d1f2d086715 (patch) | |
tree | dfe778dd7dcfd2e52970e71aedd5c4eb3646b645 /cmd/dolmetschctld/statemachine.go | |
parent | added function to get state (diff) |
better nameing than main and voice
Diffstat (limited to 'cmd/dolmetschctld/statemachine.go')
-rw-r--r-- | cmd/dolmetschctld/statemachine.go | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go index c7f4ac5..3bf31a5 100644 --- a/cmd/dolmetschctld/statemachine.go +++ b/cmd/dolmetschctld/statemachine.go @@ -72,8 +72,8 @@ type MixerChannel struct { } type MixerChannels struct { - main MixerChannel - voice MixerChannel + original MixerChannel + interpreter MixerChannel } type setLanguageRequest struct { @@ -85,12 +85,12 @@ type getLanguageRequest struct { resultCh chan Language } -type setMain2VoiceRatioRequest struct { +type setOriginal2InterpreterRatioRequest struct { ratio float32 resultCh chan error } -type getMain2VoiceRatioRequest struct { +type getOriginal2InterpreterRatioRequest struct { resultCh chan float32 } @@ -101,19 +101,19 @@ type getStateRequest struct { type StateMachine struct { mixer *mixer.Mixer - setLanguageCh chan setLanguageRequest - getLanguageCh chan getLanguageRequest - setMain2VoiceRatioCh chan setMain2VoiceRatioRequest - getMain2VoiceRatioCh chan getMain2VoiceRatioRequest - getStateCh chan getStateRequest - quitCh chan bool - exitedCh chan struct{} - mixerEventCh chan mixer.Event + setLanguageCh chan setLanguageRequest + getLanguageCh chan getLanguageRequest + setOriginal2InterpreterRatioCh chan setOriginal2InterpreterRatioRequest + getOriginal2InterpreterRatioCh chan getOriginal2InterpreterRatioRequest + getStateCh chan getStateRequest + quitCh chan bool + exitedCh chan struct{} + mixerEventCh chan mixer.Event languages map[Language]*MixerChannels channel2lang map[mixer.Channel]Language - main2VoiceRatio float32 + original2InterpreterRatio float32 state State language Language @@ -134,10 +134,10 @@ func (sm *StateMachine) handleMixerEvent(ev mixer.Event) { var mc *MixerChannel switch ev.Channel { - case mcs.main.num: - mc = &mcs.main - case mcs.voice.num: - mc = &mcs.voice + case mcs.original.num: + mc = &mcs.original + case mcs.interpreter.num: + mc = &mcs.interpreter default: panic(fmt.Sprintf("channel2lang points to language that does not use the channel!")) } @@ -153,13 +153,13 @@ func (sm *StateMachine) handleMixerEvent(ev mixer.Event) { // make sure that our state and the mixer are in sync func (sm *StateMachine) initMixer() { for _, mcs := range sm.languages { - sm.mixer.SetLevel(mcs.main.num, mixer.FaderLevel0db-1) - sm.mixer.SetLevel(mcs.main.num, mixer.FaderLevel0db) - mcs.main.target.level = mixer.FaderLevel0db + sm.mixer.SetLevel(mcs.original.num, mixer.FaderLevel0db-1) + sm.mixer.SetLevel(mcs.original.num, mixer.FaderLevel0db) + mcs.original.target.level = mixer.FaderLevel0db - sm.mixer.SetLevel(mcs.voice.num, mixer.FaderLevelOff+1) - sm.mixer.SetLevel(mcs.voice.num, mixer.FaderLevelOff) - mcs.voice.target.level = mixer.FaderLevelOff + sm.mixer.SetLevel(mcs.interpreter.num, mixer.FaderLevelOff+1) + sm.mixer.SetLevel(mcs.interpreter.num, mixer.FaderLevelOff) + mcs.interpreter.target.level = mixer.FaderLevelOff } sm.language = "" sm.state = StateSettled @@ -177,12 +177,12 @@ func (sm *StateMachine) setLanguage(l Language) error { return nil } -func (sm *StateMachine) setMain2VoiceRatio(r float32) error { +func (sm *StateMachine) setOriginal2InterpreterRatio(r float32) error { if r < 0.0 || r > 1.0 { - return fmt.Errorf("voice-to-main ratio '%1.3f' is invalid, must be between 0.0 and 1.0", r) + return fmt.Errorf("original-interpreter ratio '%1.3f' is invalid, must be between 0.0 and 1.0", r) } - sm.main2VoiceRatio = r - log.Printf("new voice-to-main ratio: '%1.3f'", r) + sm.original2InterpreterRatio = r + log.Printf("new original-interpreter ratio: '%1.3f'", r) return nil } @@ -200,15 +200,15 @@ func calcNextLevel(target, current mixer.FaderLevel) mixer.FaderLevel { func (sm *StateMachine) reconcile(ticker bool) { for lang, mcs := range sm.languages { - if sm.language == "" || lang == sm.language || mcs.voice.current.mute == mixer.MuteMuted { - mcs.main.target.level = mixer.FaderLevel0db - mcs.voice.target.level = mixer.FaderLevelOff + if sm.language == "" || lang == sm.language || mcs.interpreter.current.mute == mixer.MuteMuted { + mcs.original.target.level = mixer.FaderLevel0db + mcs.interpreter.target.level = mixer.FaderLevelOff } else { - mcs.main.target.level = mixer.FaderLevel(float32(mixer.FaderLevel0db) * sm.main2VoiceRatio) - if mcs.main.target.level > mixer.FaderLevelMax { - mcs.main.target.level = mixer.FaderLevelMax + mcs.original.target.level = mixer.FaderLevel(float32(mixer.FaderLevel0db) * sm.original2InterpreterRatio) + if mcs.original.target.level > mixer.FaderLevelMax { + mcs.original.target.level = mixer.FaderLevelMax } - mcs.voice.target.level = mixer.FaderLevel0db + mcs.interpreter.target.level = mixer.FaderLevel0db } } @@ -217,12 +217,12 @@ func (sm *StateMachine) reconcile(ticker bool) { } sm.state = StateSettled for _, mcs := range sm.languages { - if mcs.main.target.level != mcs.main.current.level { - sm.mixer.SetLevel(mcs.main.num, calcNextLevel(mcs.main.target.level, mcs.main.current.level)) + if mcs.original.target.level != mcs.original.current.level { + sm.mixer.SetLevel(mcs.original.num, calcNextLevel(mcs.original.target.level, mcs.original.current.level)) sm.state = StateSettling } - if mcs.voice.target.level != mcs.voice.current.level { - sm.mixer.SetLevel(mcs.voice.num, calcNextLevel(mcs.voice.target.level, mcs.voice.current.level)) + if mcs.interpreter.target.level != mcs.interpreter.current.level { + sm.mixer.SetLevel(mcs.interpreter.num, calcNextLevel(mcs.interpreter.target.level, mcs.interpreter.current.level)) sm.state = StateSettling } } @@ -244,11 +244,11 @@ func (sm *StateMachine) run() { sm.reconcile(false) case req := <-sm.getLanguageCh: req.resultCh <- sm.language - case req := <-sm.setMain2VoiceRatioCh: - req.resultCh <- sm.setMain2VoiceRatio(req.ratio) + case req := <-sm.setOriginal2InterpreterRatioCh: + req.resultCh <- sm.setOriginal2InterpreterRatio(req.ratio) sm.reconcile(false) - case req := <-sm.getMain2VoiceRatioCh: - req.resultCh <- sm.main2VoiceRatio + case req := <-sm.getOriginal2InterpreterRatioCh: + req.resultCh <- sm.original2InterpreterRatio case req := <-sm.getStateCh: req.resultCh <- sm.state case ev := <-sm.mixerEventCh: @@ -264,8 +264,8 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { sm := &StateMachine{mixer: m} 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.setOriginal2InterpreterRatioCh = make(chan setOriginal2InterpreterRatioRequest, 10) + sm.getOriginal2InterpreterRatioCh = make(chan getOriginal2InterpreterRatioRequest, 10) sm.getStateCh = make(chan getStateRequest, 10) sm.quitCh = make(chan bool, 1) sm.exitedCh = make(chan struct{}) @@ -280,7 +280,7 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { } // TODO: currently we can only deal with 2 languages... -func (sm *StateMachine) AddLanguage(name Language, main, voice mixer.Channel) error { +func (sm *StateMachine) AddLanguage(name Language, original, interpreter mixer.Channel) error { if sm.state != StateNew { return fmt.Errorf("adding languages is only allowed during startup") } @@ -290,23 +290,23 @@ func (sm *StateMachine) AddLanguage(name Language, main, voice mixer.Channel) er if _, exists := sm.languages[name]; exists { return fmt.Errorf("language '%s' already exists", name) } - for ch := range []mixer.Channel{main, voice} { + for ch := range []mixer.Channel{original, interpreter} { if _, exists := sm.languages[name]; exists { return fmt.Errorf("mixer channel %v is already in use by language '%s'", ch, name) } } unknown := MixerChannelState{level: mixer.FaderLevelUnknown, mute: mixer.MuteUnknown} - chMain := MixerChannel{num: main, current: unknown, target: unknown} - chVoice := MixerChannel{num: voice, current: unknown, target: unknown} + chOriginal := MixerChannel{num: original, current: unknown, target: unknown} + chInterpreter := MixerChannel{num: interpreter, current: unknown, target: unknown} - sm.languages[name] = &MixerChannels{chMain, chVoice} - sm.channel2lang[main] = name - sm.channel2lang[voice] = name - sm.mixer.Subscribe(main, sm.mixerEventCh) - sm.mixer.Subscribe(voice, sm.mixerEventCh) + sm.languages[name] = &MixerChannels{chOriginal, chInterpreter} + sm.channel2lang[original] = name + sm.channel2lang[interpreter] = name + sm.mixer.Subscribe(original, sm.mixerEventCh) + sm.mixer.Subscribe(interpreter, sm.mixerEventCh) - sm.main2VoiceRatio = 0.5 // TODO: hardcoded value + sm.original2InterpreterRatio = 0.5 // TODO: hardcoded value return nil } @@ -327,15 +327,15 @@ func (sm *StateMachine) GetLanguage() Language { return <-resultCh } -func (sm *StateMachine) SetMain2VoiceRatio(r float32) error { +func (sm *StateMachine) SetOriginal2InterpreterRatio(r float32) error { resultCh := make(chan error) - sm.setMain2VoiceRatioCh <- setMain2VoiceRatioRequest{r, resultCh} + sm.setOriginal2InterpreterRatioCh <- setOriginal2InterpreterRatioRequest{r, resultCh} return <-resultCh } -func (sm *StateMachine) GetMain2VoiceRatio() float32 { +func (sm *StateMachine) GetOriginal2InterpreterRatio() float32 { resultCh := make(chan float32) - sm.getMain2VoiceRatioCh <- getMain2VoiceRatioRequest{resultCh} + sm.getOriginal2InterpreterRatioCh <- getOriginal2InterpreterRatioRequest{resultCh} return <-resultCh } |