diff --git a/src/device/input/parse/v01/tts.go b/src/device/input/parse/v01/tts.go index a01eac9..80112b8 100644 --- a/src/device/input/parse/v01/tts.go +++ b/src/device/input/parse/v01/tts.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "net/url" + "sync" "time" "github.com/faiface/beep" @@ -15,6 +16,10 @@ import ( "github.com/faiface/beep/wav" ) +var ( + ttsLock = &sync.RWMutex{} +) + func (v01 *V01) tts(text string) { if err := v01._tts(text); err != nil { log.Printf("failed to tts: %s: %v", text, err) @@ -58,7 +63,11 @@ func (v01 *V01) _tts(text string) error { if err != nil { return err } + ttsLock.Lock() + defer ttsLock.Unlock() speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/30)) speaker.Play(&effects.Volume{Streamer: beep.ResampleRatio(4, 1, &beep.Ctrl{Streamer: beep.Loop(1, decoder)})}) + duration := time.Duration(decoder.Len()) * format.SampleRate.D(1) + time.Sleep(duration) return nil } diff --git a/src/device/input/parse/v01/v01_exported_test.go b/src/device/input/parse/v01/v01_exported_test.go index b627d53..2a67684 100644 --- a/src/device/input/parse/v01/v01_exported_test.go +++ b/src/device/input/parse/v01/v01_exported_test.go @@ -169,7 +169,7 @@ func TestV01Feedback(t *testing.T) { } resp.Body.Close() } - time.Sleep(time.Second * 2) + time.Sleep(time.Second * 3) }) }