From c4bf287cc3991e4e52706d3cc73ecf4a577adeeb Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 10 Feb 2019 17:06:04 +0100 Subject: refactored fader levels and mute state --- cmd/dolmetschctld/statemachine.go | 64 ++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 14 deletions(-) (limited to 'cmd') diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go index 1961303..e5ea0a9 100644 --- a/cmd/dolmetschctld/statemachine.go +++ b/cmd/dolmetschctld/statemachine.go @@ -59,15 +59,15 @@ func (l Language) String() string { return string(l) } -type LanguageChannel struct { - Num mixer.Channel - currentLevel mixer.FaderLevel - muted bool +type MixerChannel struct { + num mixer.Channel + level mixer.FaderLevel + mute mixer.Mute } -type LanguageChannels struct { - main LanguageChannel - voice LanguageChannel +type MixerChannels struct { + main MixerChannel + voice MixerChannel } type selectLangRequest struct { @@ -82,7 +82,7 @@ type StateMachine struct { exitedCh chan struct{} mixerEventCh chan mixer.Event - languages map[Language]LanguageChannels + languages map[Language]*MixerChannels channel2lang map[mixer.Channel]Language currentState State @@ -101,6 +101,43 @@ func (sm *StateMachine) selectLang(l Language) error { return nil } +func (sm *StateMachine) handleMixerEvent(ev mixer.Event) { + lang, exists := sm.channel2lang[ev.Channel] + if !exists { + // TODO: make this panic? + log.Printf("got mixer-event for unknown channel: %s", ev) + return + } + + mcs, exists := sm.languages[lang] + if !exists { + panic(fmt.Sprintf("channel2lang map contains unknown language entries!")) + return + } + + var mc *MixerChannel + switch ev.Channel { + case mcs.main.num: + mc = &mcs.main + case mcs.voice.num: + mc = &mcs.voice + default: + panic(fmt.Sprintf("channel2lang points to language that does not use the channel!")) + return + } + + switch ev.Type { + case mixer.EventFaderChange: + mc.level = ev.Level + case mixer.EventMute: + mc.mute = ev.Mute + } + + log.Printf("status of mixer channels for language '%s' is now:", lang) + log.Printf(" main(%d): level=%s, muted=%s", mcs.main.num, mcs.main.level, mcs.main.mute) + log.Printf(" voice(%d): level=%s, muted=%s", mcs.voice.num, mcs.voice.level, mcs.voice.mute) +} + func (sm *StateMachine) reconcile() { if sm.targetLang == sm.currentLang { sm.currentState = StateSettled @@ -117,8 +154,7 @@ func (sm *StateMachine) run() { case req := <-sm.selectLangCh: req.resultCh <- sm.selectLang(req.l) case ev := <-sm.mixerEventCh: - log.Printf("got event from mixer: %v", ev) - // TODO: update mixer channel states... + sm.handleMixerEvent(ev) case <-sm.quitCh: return } @@ -138,7 +174,7 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) { sm.exitedCh = make(chan struct{}) sm.mixerEventCh = make(chan mixer.Event, 1000) - sm.languages = make(map[Language]LanguageChannels) + sm.languages = make(map[Language]*MixerChannels) sm.channel2lang = make(map[mixer.Channel]Language) sm.currentState = StateNew @@ -161,9 +197,9 @@ func (sm *StateMachine) AddLanguage(name Language, main, voice mixer.Channel) er } } - chMain := LanguageChannel{main, mixer.FaderLevel0db, false} - chVoice := LanguageChannel{voice, mixer.FaderLevel0db, false} - sm.languages[name] = LanguageChannels{chMain, chVoice} + chMain := MixerChannel{main, mixer.FaderLevelUnknown, mixer.MuteUnknown} + chVoice := MixerChannel{voice, mixer.FaderLevelUnknown, mixer.MuteUnknown} + sm.languages[name] = &MixerChannels{chMain, chVoice} sm.channel2lang[main] = name sm.channel2lang[voice] = name -- cgit v1.2.3