diff options
Diffstat (limited to 'cmd/dolmetschctld/statemachine.go')
-rw-r--r-- | cmd/dolmetschctld/statemachine.go | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go index 3e92f00..1abb5fd 100644 --- a/cmd/dolmetschctld/statemachine.go +++ b/cmd/dolmetschctld/statemachine.go @@ -42,6 +42,7 @@ type MixerChannel struct { num mixer.Channel target MixerChannelState current MixerChannelState + real MixerChannelState } type MixerChannels struct { @@ -133,7 +134,7 @@ func (sm *StateMachine) publishState() { sm.stateSubscribers.Remove(entry) default: select { - case sub.publish <- types.FullState{sm.state, sm.original2InterpreterRatio, sm.language}: + case sub.publish <- types.FullState{State: sm.state, Ratio: sm.original2InterpreterRatio, Language: sm.language}: default: // subscriber is not responding... log.Printf("statemachine: removing subscriber '%v', because it is not responding", sub.publish) @@ -169,21 +170,16 @@ func (sm *StateMachine) handleMixerEvent(ev mixer.Event) { switch ev.Type { case mixer.EventFaderChange: - mc.current.level = ev.Level + mc.real.level = ev.Level case mixer.EventMute: - mc.current.mute = ev.Mute + mc.real.mute = ev.Mute } } // make sure that our state and the mixer are in sync func (sm *StateMachine) initMixer() { for _, mcs := range sm.languages { - 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.interpreter.num, mixer.FaderLevelOff+1) - sm.mixer.SetLevel(mcs.interpreter.num, mixer.FaderLevelOff) mcs.interpreter.target.level = mixer.FaderLevelOff } sm.language = "" @@ -209,7 +205,6 @@ func (sm *StateMachine) setLanguage(l types.Language) error { } } sm.language = l - log.Printf("new target language: '%s'", sm.language) return nil } @@ -218,17 +213,16 @@ func (sm *StateMachine) setOriginal2InterpreterRatio(r float32) error { return fmt.Errorf("original-interpreter ratio '%1.3f' is invalid, must be between 0.0 and 1.0", r) } sm.original2InterpreterRatio = r - log.Printf("new original-interpreter ratio: '%1.3f'", r) return nil } func calcNextLevel(target, current mixer.FaderLevel) mixer.FaderLevel { next := target if current != mixer.FaderLevelUnknown { - if next > current { - next = current + 1 - } else { - next = current - 1 + if next > (current + mixer.FaderLevelIncrement/2) { + next = current + mixer.FaderLevelIncrement + } else if next < (current - mixer.FaderLevelIncrement/2) { + next = current - mixer.FaderLevelIncrement } } return next @@ -236,7 +230,7 @@ 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.interpreter.current.mute == mixer.MuteMuted { + if sm.language == "" || lang == sm.language { mcs.original.target.level = mixer.FaderLevel0db mcs.interpreter.target.level = mixer.FaderLevelOff } else { @@ -251,14 +245,19 @@ func (sm *StateMachine) reconcile(ticker bool) { if sm.state != types.StateSettled && !ticker { return } + sm.state = types.StateSettled for _, mcs := range sm.languages { if mcs.original.target.level != mcs.original.current.level { - sm.mixer.SetLevel(mcs.original.num, calcNextLevel(mcs.original.target.level, mcs.original.current.level)) + nextLevel := calcNextLevel(mcs.original.target.level, mcs.original.current.level) + sm.mixer.SetLevel(mcs.original.num, nextLevel) + mcs.original.current.level = nextLevel sm.state = types.StateSettling } if mcs.interpreter.target.level != mcs.interpreter.current.level { - sm.mixer.SetLevel(mcs.interpreter.num, calcNextLevel(mcs.interpreter.target.level, mcs.interpreter.current.level)) + nextLevel := calcNextLevel(mcs.interpreter.target.level, mcs.interpreter.current.level) + sm.mixer.SetLevel(mcs.interpreter.num, nextLevel) + mcs.interpreter.current.level = nextLevel sm.state = types.StateSettling } } @@ -292,7 +291,7 @@ func (sm *StateMachine) run() { case req := <-sm.getOriginal2InterpreterRatioCh: req.resCh <- sm.original2InterpreterRatio case req := <-sm.getStateCh: - req.resCh <- getStateRes{types.FullState{sm.state, sm.original2InterpreterRatio, sm.language}} + req.resCh <- getStateRes{types.FullState{State: sm.state, Ratio: sm.original2InterpreterRatio, Language: sm.language}} case ev := <-sm.mixerEventCh: sm.handleMixerEvent(ev) sm.reconcile(false) @@ -301,6 +300,7 @@ func (sm *StateMachine) run() { } if oldState != sm.state || oldOriginal2InterpreterRadio != sm.original2InterpreterRatio || oldLanguage != sm.language { + log.Printf("update: lang=%s, ratio=%1.3f (%s)", sm.language, sm.original2InterpreterRatio, sm.state) sm.publishState() } } @@ -344,8 +344,8 @@ func (sm *StateMachine) AddLanguage(name types.Language, original, interpreter m } unknown := MixerChannelState{level: mixer.FaderLevelUnknown, mute: mixer.MuteUnknown} - chOriginal := MixerChannel{num: original, current: unknown, target: unknown} - chInterpreter := MixerChannel{num: interpreter, current: unknown, target: unknown} + chOriginal := MixerChannel{num: original, target: unknown, current: unknown, real: unknown} + chInterpreter := MixerChannel{num: interpreter, target: unknown, current: unknown, real: unknown} sm.languages[name] = &MixerChannels{chOriginal, chInterpreter} sm.channel2lang[original] = name |