summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/dolmetschctld/statemachine.go53
-rw-r--r--cmd/dolmetschctld/telnet.go22
2 files changed, 62 insertions, 13 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go
index d094875..5ded49f 100644
--- a/cmd/dolmetschctld/statemachine.go
+++ b/cmd/dolmetschctld/statemachine.go
@@ -77,20 +77,29 @@ type MixerChannels struct {
}
type selectLangRequest struct {
- l Language
+ lang Language
+ resultCh chan error
+}
+
+type setMain2VoiceRatioRequest struct {
+ ratio float32
resultCh chan error
}
type StateMachine struct {
- mixer *mixer.Mixer
- selectLangCh chan selectLangRequest
- quitCh chan bool
- exitedCh chan struct{}
- mixerEventCh chan mixer.Event
+ mixer *mixer.Mixer
+
+ selectLangCh chan selectLangRequest
+ setMain2VoiceRatioCh chan setMain2VoiceRatioRequest
+ quitCh chan bool
+ exitedCh chan struct{}
+ mixerEventCh chan mixer.Event
languages map[Language]*MixerChannels
channel2lang map[mixer.Channel]Language
+ main2VoiceRatio float32
+
currentState State
currentLang Language
targetLang Language
@@ -107,7 +116,6 @@ func (sm *StateMachine) handleMixerEvent(ev mixer.Event) {
mcs, exists := sm.languages[lang]
if !exists {
panic(fmt.Sprintf("channel2lang map contains unknown language entries!"))
- return
}
var mc *MixerChannel
@@ -118,7 +126,6 @@ func (sm *StateMachine) handleMixerEvent(ev mixer.Event) {
mc = &mcs.voice
default:
panic(fmt.Sprintf("channel2lang points to language that does not use the channel!"))
- return
}
switch ev.Type {
@@ -157,6 +164,15 @@ func (sm *StateMachine) selectLang(l Language) error {
return nil
}
+func (sm *StateMachine) setMain2VoiceRatio(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)
+ }
+ sm.main2VoiceRatio = r
+ log.Printf("new voice-to-main ratio: '%1.3f'", r)
+ return nil
+}
+
func calcNextLevel(target, current mixer.FaderLevel) mixer.FaderLevel {
next := target
if current != mixer.FaderLevelUnknown {
@@ -175,7 +191,10 @@ func (sm *StateMachine) reconcile(ticker bool) {
mcs.main.target.level = mixer.FaderLevel0db
mcs.voice.target.level = mixer.FaderLevelOff
} else {
- mcs.main.target.level = mixer.FaderLevel(float32(mixer.FaderLevel0db) * 0.5) // TODO: hardcoded Value!!
+ 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.voice.target.level = mixer.FaderLevel0db
}
}
@@ -208,7 +227,10 @@ func (sm *StateMachine) run() {
sm.reconcile(true)
}
case req := <-sm.selectLangCh:
- req.resultCh <- sm.selectLang(req.l)
+ req.resultCh <- sm.selectLang(req.lang)
+ sm.reconcile(false)
+ case req := <-sm.setMain2VoiceRatioCh:
+ req.resultCh <- sm.setMain2VoiceRatio(req.ratio)
sm.reconcile(false)
case ev := <-sm.mixerEventCh:
sm.handleMixerEvent(ev)
@@ -222,6 +244,7 @@ func (sm *StateMachine) run() {
func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) {
sm := &StateMachine{mixer: m}
sm.selectLangCh = make(chan selectLangRequest, 10)
+ sm.setMain2VoiceRatioCh = make(chan setMain2VoiceRatioRequest, 10)
sm.quitCh = make(chan bool, 1)
sm.exitedCh = make(chan struct{})
sm.mixerEventCh = make(chan mixer.Event, 1000)
@@ -259,9 +282,11 @@ func (sm *StateMachine) AddLanguage(name Language, main, voice mixer.Channel) er
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.main2VoiceRatio = 0.5 // TODO: hardcoded value
+
return nil
}
@@ -275,6 +300,12 @@ func (sm *StateMachine) SelectLanguage(l Language) error {
return <-resultCh
}
+func (sm *StateMachine) SetMain2VoiceRatio(r float32) error {
+ resultCh := make(chan error)
+ sm.setMain2VoiceRatioCh <- setMain2VoiceRatioRequest{r, 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 d2f7fc3..19e0a26 100644
--- a/cmd/dolmetschctld/telnet.go
+++ b/cmd/dolmetschctld/telnet.go
@@ -23,7 +23,7 @@
package main
import (
- "fmt"
+ "strconv"
"github.com/spreadspace/telgo"
)
@@ -42,7 +42,24 @@ func telnetCmdLang(c *telgo.Client, args []string, sm *StateMachine) bool {
lang = ""
}
if err := sm.SelectLanguage(Language(lang)); err != nil {
- c.Sayln(fmt.Sprintf("selecting language failed: %v", err))
+ c.Sayln("selecting language failed: %v", err)
+ }
+ return false
+}
+
+func telnetCmdRatio(c *telgo.Client, args []string, sm *StateMachine) bool {
+ if len(args) != 2 {
+ c.Sayln("usage: ratio <ratio>")
+ return false
+ }
+ r, err := strconv.ParseFloat(args[1], 32)
+ if err != nil {
+ c.Sayln("invalid ratio: %v", err)
+ return false
+ }
+
+ if err := sm.SetMain2VoiceRatio(float32(r)); err != nil {
+ c.Sayln("setting ratio failed: %v", err)
}
return false
}
@@ -60,6 +77,7 @@ func NewTelnetInterface(addr string, sm *StateMachine) (telnet *TelnetInterface,
cmdlist := make(telgo.CmdList)
cmdlist["lang"] = func(c *telgo.Client, args []string) bool { return telnetCmdLang(c, args, sm) }
+ cmdlist["ratio"] = func(c *telgo.Client, args []string) bool { return telnetCmdRatio(c, args, sm) }
cmdlist["quit"] = telnetQuit
telnet.server, err = telgo.NewServer(addr, "dolmetschctl> ", cmdlist, nil)