mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-15 17:12:59 -04:00
guh
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package roverd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type AudioLevels struct {
|
||||
HornGain float64
|
||||
TTSGain float64
|
||||
@@ -34,9 +40,7 @@ func (c *WSClient) setAudioLevels(next AudioLevels) {
|
||||
c.audioMu.Lock()
|
||||
c.audioLevels = normalized
|
||||
c.audioMu.Unlock()
|
||||
if c.horn != nil {
|
||||
c.horn.SetGlobalGain(normalized.HornGain)
|
||||
}
|
||||
c.applyAudioLevelsToMixer(normalized)
|
||||
}
|
||||
|
||||
func (c *WSClient) handleAudioLevels(payload *audioLevelsPayload) error {
|
||||
@@ -56,3 +60,35 @@ func (c *WSClient) handleAudioLevels(payload *audioLevelsPayload) error {
|
||||
c.setAudioLevels(levels)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *WSClient) applyAudioLevelsToMixer(levels AudioLevels) {
|
||||
c.applyMixerGain("HornMaster", levels.HornGain)
|
||||
c.applyMixerGain("TTSMaster", levels.TTSGain)
|
||||
c.applyMixerGain("ForwardMaster", levels.ForwardGain)
|
||||
}
|
||||
|
||||
func (c *WSClient) applyMixerGain(control string, gain float64) {
|
||||
normalized := clampAudioGain(gain)
|
||||
if normalized <= 0 {
|
||||
out, err := exec.Command("amixer", "-q", "-c", "0", "sset", control, "0%").CombinedOutput()
|
||||
if err != nil {
|
||||
c.log.Printf("audio-levels: amixer mute %s failed: %v (%s)", control, err, string(out))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Convert linear gain to dB, matching softvol max_dB=12.0 in /etc/asound.conf.
|
||||
db := 20.0 * math.Log10(normalized)
|
||||
if db > 12.0 {
|
||||
db = 12.0
|
||||
}
|
||||
if db < -60.0 {
|
||||
db = -60.0
|
||||
}
|
||||
|
||||
dbArg := fmt.Sprintf("%.2fdB", db)
|
||||
out, err := exec.Command("amixer", "-q", "-c", "0", "sset", control, dbArg).CombinedOutput()
|
||||
if err != nil {
|
||||
c.log.Printf("audio-levels: amixer set %s=%s failed: %v (%s)", control, dbArg, err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -172,7 +172,7 @@ func LoadConfig(path string) (*Config, error) {
|
||||
Audio: AudioConfig{
|
||||
CaptureEnabled: false,
|
||||
CaptureDevice: "rovermic",
|
||||
PlaybackDevice: "default",
|
||||
PlaybackDevice: "forward",
|
||||
SampleRate: 48000,
|
||||
Channels: 2,
|
||||
Bitrate: 24000,
|
||||
@@ -314,8 +314,8 @@ func validateAudioConfig(cfg *AudioConfig) {
|
||||
if cfg.CaptureEnabled && cfg.CaptureDevice == "" {
|
||||
cfg.CaptureDevice = "hw:0,0"
|
||||
}
|
||||
if cfg.PlaybackDevice == "" {
|
||||
cfg.PlaybackDevice = "default"
|
||||
if cfg.PlaybackDevice == "" || cfg.PlaybackDevice == "default" {
|
||||
cfg.PlaybackDevice = "forward"
|
||||
}
|
||||
if cfg.SampleRate <= 0 {
|
||||
cfg.SampleRate = 48000
|
||||
|
||||
+4
-3
@@ -124,10 +124,11 @@ func (h *HornSynth) run(waveform string, freqs []float64, stop <-chan struct{})
|
||||
h.mu.Unlock()
|
||||
volume *= gain
|
||||
|
||||
args := []string{"-q", "-f", "S16_LE", "-c", fmt.Sprintf("%d", channels), "-r", fmt.Sprintf("%d", rate), "-t", "raw"}
|
||||
if h.cfg.Device != "" {
|
||||
args = append(args, "-D", h.cfg.Device)
|
||||
device := strings.TrimSpace(h.cfg.Device)
|
||||
if device == "" {
|
||||
device = "horn"
|
||||
}
|
||||
args := []string{"-q", "-D", device, "-f", "S16_LE", "-c", fmt.Sprintf("%d", channels), "-r", fmt.Sprintf("%d", rate), "-t", "raw"}
|
||||
cmd := exec.Command("aplay", args...)
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
|
||||
@@ -54,7 +54,7 @@ func UpdatePublisherEnv(media MediaConfig, audio AudioConfig) error {
|
||||
fmt.Fprintf(&buf, "AUDIO_DEVICE=%s\n", audioDevice)
|
||||
playbackDevice := audio.PlaybackDevice
|
||||
if playbackDevice == "" {
|
||||
playbackDevice = "default"
|
||||
playbackDevice = "forward"
|
||||
}
|
||||
fmt.Fprintf(&buf, "AUDIO_PLAYBACK_DEVICE=%s\n", playbackDevice)
|
||||
fmt.Fprintf(&buf, "AUDIO_RATE=%d\n", audio.SampleRate)
|
||||
|
||||
@@ -39,7 +39,7 @@ cameraServo:
|
||||
audio:
|
||||
captureEnabled: false
|
||||
captureDevice: hw:0,0
|
||||
playbackDevice: default
|
||||
playbackDevice: forward
|
||||
sampleRate: 48000
|
||||
channels: 2
|
||||
bitrate: 24000
|
||||
|
||||
+5
-52
@@ -3,7 +3,6 @@ package roverd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -48,75 +47,29 @@ func (c *WSClient) handleTTSPayload(ctx context.Context, payload *ttsPayload) er
|
||||
runCtx, cancel := context.WithTimeout(ctx, 12*time.Second)
|
||||
defer cancel()
|
||||
|
||||
tmp, err := os.CreateTemp("", "roverd-tts-*.wav")
|
||||
if err != nil {
|
||||
return fmt.Errorf("tts temp file: %w", err)
|
||||
}
|
||||
tmpPath := tmp.Name()
|
||||
_ = tmp.Close()
|
||||
defer os.Remove(tmpPath)
|
||||
|
||||
if err := synthTTS(runCtx, engine, voice, pitch, text, tmpPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := playTTSFile(runCtx, tmpPath, c.cfg.Audio.PlaybackDevice, c.getAudioLevels().TTSGain); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func synthTTS(ctx context.Context, engine, voice string, pitch int, text, outputWavPath string) error {
|
||||
var cmd *exec.Cmd
|
||||
switch engine {
|
||||
case "espeak", "e":
|
||||
args := []string{"-w", outputWavPath}
|
||||
args := []string{}
|
||||
if pitch > 0 {
|
||||
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, "-o", outputWavPath)
|
||||
cmd = exec.CommandContext(ctx, "flite", args...)
|
||||
args = append(args, "-t", text)
|
||||
cmd = exec.CommandContext(runCtx, "flite", args...)
|
||||
default:
|
||||
return fmt.Errorf("unsupported tts engine: %s", engine)
|
||||
}
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("tts synth failed: %w (%s)", err, string(out))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func playTTSFile(ctx context.Context, wavPath, playbackDevice string, gain float64) error {
|
||||
if playbackDevice == "" {
|
||||
playbackDevice = "default"
|
||||
}
|
||||
gain = clampAudioGain(gain)
|
||||
if gain == 0 {
|
||||
// Mute is an explicit value users may choose.
|
||||
return nil
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-hide_banner",
|
||||
"-loglevel", "warning",
|
||||
"-i", wavPath,
|
||||
"-af", fmt.Sprintf("aresample=16000,volume=%g", gain),
|
||||
"-ac", "1",
|
||||
"-ar", "16000",
|
||||
"-f", "alsa",
|
||||
playbackDevice,
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "ffmpeg", args...)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("tts playback failed: %w (%s)", err, string(out))
|
||||
return fmt.Errorf("tts exec failed: %w (%s)", err, string(out))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,9 +64,7 @@ func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, even
|
||||
ForwardGain: 1.0,
|
||||
},
|
||||
}
|
||||
if client.horn != nil {
|
||||
client.horn.SetGlobalGain(client.audioLevels.HornGain)
|
||||
}
|
||||
client.applyAudioLevelsToMixer(client.audioLevels)
|
||||
return client
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user