fix audio player stuff

This commit is contained in:
legop3
2025-12-22 19:47:48 -05:00
parent 020ddfea07
commit 01df853592
7 changed files with 143 additions and 17 deletions
+4 -4
View File
@@ -8,7 +8,7 @@ import (
"time"
)
func (c *WSClient) handleTTSPayload(payload *ttsPayload) error {
func (c *WSClient) handleTTSPayload(ctx context.Context, payload *ttsPayload) error {
if payload == nil {
return fmt.Errorf("tts payload required")
}
@@ -44,7 +44,7 @@ func (c *WSClient) handleTTSPayload(payload *ttsPayload) error {
}
pitch = clampInt(pitch, 0, 99)
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
runCtx, cancel := context.WithTimeout(ctx, 12*time.Second)
defer cancel()
var cmd *exec.Cmd
@@ -55,14 +55,14 @@ func (c *WSClient) handleTTSPayload(payload *ttsPayload) error {
args = append(args, "-p", fmt.Sprintf("%d", pitch))
}
args = append(args, text)
cmd = exec.CommandContext(ctx, "espeak", args...)
cmd = exec.CommandContext(runCtx, "espeak", args...)
case "flite", "f":
args := []string{}
if voice != "" {
args = append(args, "-voice", voice)
}
args = append(args, "-t", text)
cmd = exec.CommandContext(ctx, "flite", args...)
cmd = exec.CommandContext(runCtx, "flite", args...)
default:
return fmt.Errorf("unsupported tts engine: %s", engine)
}
+42 -1
View File
@@ -23,9 +23,14 @@ type WSClient struct {
log *log.Logger
recoverMu sync.Mutex
recovering bool
ttsQueue chan *ttsPayload
}
func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, events chan RoverEvent, media *MediaSupervisor, servo *CameraServo, nightVision *NightVisionLight, logger *log.Logger) *WSClient {
var ttsQueue chan *ttsPayload
if cfg.Audio.TTSEnabled {
ttsQueue = make(chan *ttsPayload, 2)
}
return &WSClient{
cfg: cfg,
adapter: adapter,
@@ -35,6 +40,7 @@ func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, even
servo: servo,
nightVision: nightVision,
log: logger,
ttsQueue: ttsQueue,
}
}
@@ -53,6 +59,7 @@ func (c *WSClient) Run(ctx context.Context) error {
}
errCh := make(chan error, 1)
c.startTTSWorker(ctx)
go func() {
errCh <- c.readLoop(ctx, conn)
}()
@@ -155,7 +162,7 @@ func (c *WSClient) dispatch(ctx context.Context, msg *inboundMessage) error {
}
return c.handleServoCommand(msg.Servo)
case msg.TTS != nil:
return c.handleTTSPayload(msg.TTS)
return c.enqueueTTS(msg.TTS)
case msg.NightVision != nil:
if c.nightVision == nil {
return fmt.Errorf("night vision disabled")
@@ -172,6 +179,40 @@ func (c *WSClient) dispatch(ctx context.Context, msg *inboundMessage) error {
}
}
func (c *WSClient) enqueueTTS(payload *ttsPayload) error {
if c.ttsQueue == nil {
return fmt.Errorf("tts disabled")
}
select {
case c.ttsQueue <- payload:
return nil
default:
return fmt.Errorf("tts busy")
}
}
func (c *WSClient) startTTSWorker(ctx context.Context) {
if c.ttsQueue == nil {
return
}
go func() {
for {
select {
case <-ctx.Done():
return
case payload := <-c.ttsQueue:
if payload == nil {
continue
}
if err := c.handleTTSPayload(ctx, payload); err != nil {
c.log.Printf("tts failed: %v", err)
c.emitEvent("tts.error", map[string]any{"error": err.Error()})
}
}
}
}()
}
func (c *WSClient) handleServoCommand(payload *servoPayload) error {
switch {
case payload.Angle != nil: