Compare commits
5 Commits
356b492629
...
v1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0010bc6cf7 | ||
|
|
a14d068ada | ||
|
|
b66e0737d0 | ||
|
|
2860102627 | ||
|
|
ebe2b26ae9 |
204
cmd/tui/model.go
204
cmd/tui/model.go
@@ -97,12 +97,15 @@ type Model struct {
|
|||||||
talkingClients map[uint16]bool // ClientID -> isTalking
|
talkingClients map[uint16]bool // ClientID -> isTalking
|
||||||
|
|
||||||
// Audio state
|
// Audio state
|
||||||
audioPlayer *audio.Player
|
audioPlayer *audio.Player
|
||||||
audioCapturer *audio.Capturer
|
audioCapturer *audio.Capturer
|
||||||
playbackVol int // 0-100
|
playbackVol int // 0-100
|
||||||
micLevel int // 0-100 (current input level)
|
micLevel int // 0-100 (current input level)
|
||||||
isMuted bool // Mic muted
|
isMuted bool // Mic muted
|
||||||
isPTT bool // Push-to-talk active
|
isPTT bool // Push-to-talk active (Manual TX)
|
||||||
|
vadEnabled bool // Voice Activation Detection active
|
||||||
|
vadThreshold int // 0-100 threshold for VAD
|
||||||
|
vadLastTriggered time.Time // Last time VAD threshold was exceeded
|
||||||
|
|
||||||
// Popup State
|
// Popup State
|
||||||
showPokePopup bool
|
showPokePopup bool
|
||||||
@@ -140,6 +143,8 @@ func NewModel(serverAddr, nickname string) *Model {
|
|||||||
logMessages: []string{"Starting..."},
|
logMessages: []string{"Starting..."},
|
||||||
talkingClients: make(map[uint16]bool),
|
talkingClients: make(map[uint16]bool),
|
||||||
playbackVol: 80, // Default 80% volume
|
playbackVol: 80, // Default 80% volume
|
||||||
|
vadEnabled: true,
|
||||||
|
vadThreshold: 50,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,16 +304,53 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
} else {
|
} else {
|
||||||
m.audioCapturer = capturer
|
m.audioCapturer = capturer
|
||||||
// Set callback to send audio to server when PTT is active
|
// Set callback to send audio to server when PTT is active
|
||||||
|
// Set callback to send audio to server when PTT is active
|
||||||
m.audioCapturer.SetCallback(func(samples []int16) {
|
m.audioCapturer.SetCallback(func(samples []int16) {
|
||||||
if m.isPTT && m.client != nil && !m.isMuted {
|
// Calculate level of this frame for VAD decision
|
||||||
|
// Note: GetLevel() is smoothed, we might want instant frame level for VAD trigger?
|
||||||
|
// But pkg/audio/level.go is efficient. Let's re-calculate for precision.
|
||||||
|
level := audio.CalculateRMSLevel(samples)
|
||||||
|
|
||||||
|
// Determine if we should transmit
|
||||||
|
shouldTransmit := false
|
||||||
|
|
||||||
|
// Manual PTT (Locked on with 'v')
|
||||||
|
if m.isPTT {
|
||||||
|
shouldTransmit = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// VAD Logic
|
||||||
|
if m.vadEnabled && !m.isMuted {
|
||||||
|
if level > m.vadThreshold {
|
||||||
|
shouldTransmit = true
|
||||||
|
m.vadLastTriggered = time.Now()
|
||||||
|
} else if !m.vadLastTriggered.IsZero() && time.Since(m.vadLastTriggered) < 1*time.Second {
|
||||||
|
// Hold VAD open for 1 second (decay)
|
||||||
|
shouldTransmit = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow transmission if forced or VAD triggered
|
||||||
|
if shouldTransmit && m.client != nil && !m.isMuted {
|
||||||
m.client.SendAudio(samples)
|
m.client.SendAudio(samples)
|
||||||
}
|
}
|
||||||
// Update mic level for display
|
|
||||||
|
// Update mic level for display (use the calculated level)
|
||||||
if m.program != nil {
|
if m.program != nil {
|
||||||
m.program.Send(micLevelMsg(m.audioCapturer.GetLevel()))
|
// Use goroutine to prevent blocking the capture loop if the UI is busy (e.g. shutting down)
|
||||||
|
go m.program.Send(micLevelMsg(level))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
m.addLog("Audio capturer initialized")
|
m.addLog("Audio capturer initialized")
|
||||||
|
|
||||||
|
// Start capture immediately if VAD is enabled or PTT is active
|
||||||
|
if m.vadEnabled || m.isPTT {
|
||||||
|
if err := m.audioCapturer.Start(); err != nil {
|
||||||
|
m.addLog("Error starting audio capture: %v", err)
|
||||||
|
} else {
|
||||||
|
m.addLog("Audio capture started (VAD/PTT active)")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect asynchronously
|
// Connect asynchronously
|
||||||
@@ -348,16 +390,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update mic level when PTT is active (multiply for better visibility)
|
// Legacy mic level handling removed to support VAD event-driven updates
|
||||||
if m.isPTT && m.audioCapturer != nil {
|
|
||||||
level := m.audioCapturer.GetLevel() * 4 // Boost for visibility
|
|
||||||
if level > 100 {
|
|
||||||
level = 100
|
|
||||||
}
|
|
||||||
m.micLevel = level
|
|
||||||
} else {
|
|
||||||
m.micLevel = 0 // Reset when not transmitting
|
|
||||||
}
|
|
||||||
|
|
||||||
// Continue ticking (100ms for responsive mic level)
|
// Continue ticking (100ms for responsive mic level)
|
||||||
return m, tea.Tick(100*time.Millisecond, func(t time.Time) tea.Msg {
|
return m, tea.Tick(100*time.Millisecond, func(t time.Time) tea.Msg {
|
||||||
@@ -649,16 +682,58 @@ func (m *Model) handleKeyPress(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
|
|
||||||
|
case "ctrl+up", "ctrl+right":
|
||||||
|
// Increase VAD threshold
|
||||||
|
m.vadThreshold += 5
|
||||||
|
if m.vadThreshold > 100 {
|
||||||
|
m.vadThreshold = 100
|
||||||
|
}
|
||||||
|
m.addLog("VAD Threshold: %d", m.vadThreshold)
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case "ctrl+down", "ctrl+left":
|
||||||
|
// Decrease VAD threshold
|
||||||
|
m.vadThreshold -= 5
|
||||||
|
if m.vadThreshold < 0 {
|
||||||
|
m.vadThreshold = 0
|
||||||
|
}
|
||||||
|
m.addLog("VAD Threshold: %d", m.vadThreshold)
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case "g", "G":
|
||||||
|
// Toggle VAD (Gate)
|
||||||
|
m.vadEnabled = !m.vadEnabled
|
||||||
|
state := "OFF"
|
||||||
|
if m.vadEnabled {
|
||||||
|
state = "ON"
|
||||||
|
// Ensure capturer is running if VAD is on
|
||||||
|
if m.audioCapturer != nil && !m.audioCapturer.IsRunning() {
|
||||||
|
if err := m.audioCapturer.Start(); err != nil {
|
||||||
|
m.addLog("Error starting VAD capture: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Stop if PTT is also off
|
||||||
|
if !m.isPTT && m.audioCapturer != nil && m.audioCapturer.IsRunning() {
|
||||||
|
m.audioCapturer.Stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.addLog("Voice Activation (Gate): %s", state)
|
||||||
|
return m, nil
|
||||||
|
|
||||||
case "v", "V":
|
case "v", "V":
|
||||||
// Toggle voice (PTT)
|
// Toggle voice (PTT)
|
||||||
m.isPTT = !m.isPTT
|
m.isPTT = !m.isPTT
|
||||||
if m.isPTT {
|
if m.isPTT {
|
||||||
if m.audioCapturer != nil {
|
if m.audioCapturer != nil && !m.audioCapturer.IsRunning() {
|
||||||
m.audioCapturer.Start()
|
if err := m.audioCapturer.Start(); err != nil {
|
||||||
|
m.addLog("Audio capture error: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
m.addLog("🎤 Transmitting...")
|
m.addLog("🎤 Transmitting...")
|
||||||
} else {
|
} else {
|
||||||
if m.audioCapturer != nil {
|
// Stop only if VAD is also off
|
||||||
|
if !m.vadEnabled && m.audioCapturer != nil && m.audioCapturer.IsRunning() {
|
||||||
m.audioCapturer.Stop()
|
m.audioCapturer.Stop()
|
||||||
}
|
}
|
||||||
m.addLog("🎤 Stopped transmitting")
|
m.addLog("🎤 Stopped transmitting")
|
||||||
@@ -1010,7 +1085,7 @@ func (m *Model) View() string {
|
|||||||
if m.showLog {
|
if m.showLog {
|
||||||
logHelp = "L chat"
|
logHelp = "L chat"
|
||||||
}
|
}
|
||||||
help := lipgloss.NewStyle().Faint(true).Render(fmt.Sprintf("↑↓ navigate │ Enter join │ Tab switch │ %s │ V talk │ M mute │ +/- vol │ q quit", logHelp))
|
help := lipgloss.NewStyle().Faint(true).Render(fmt.Sprintf("↑↓ nav │ Ent join │ Tab switch │ %s │ V PTT │ G VAD │ ^↕↔ thresh │ M mute │ +/- vol │ q quit", logHelp))
|
||||||
|
|
||||||
// Combine panels
|
// Combine panels
|
||||||
panels := lipgloss.JoinHorizontal(lipgloss.Top, channelPanel, rightPanel)
|
panels := lipgloss.JoinHorizontal(lipgloss.Top, channelPanel, rightPanel)
|
||||||
@@ -1053,12 +1128,85 @@ func (m *Model) renderStatusBar() string {
|
|||||||
}
|
}
|
||||||
volPart := fmt.Sprintf("%s:%s%d%%", muteIcon, volBar, m.playbackVol)
|
volPart := fmt.Sprintf("%s:%s%d%%", muteIcon, volBar, m.playbackVol)
|
||||||
|
|
||||||
micBar := audio.LevelToBar(m.micLevel, 6)
|
// Custom Mic Bar with VAD Threshold
|
||||||
pttIcon := "MIC"
|
micBarWidth := 8
|
||||||
if m.isPTT {
|
var micBar string
|
||||||
pttIcon = "*TX*"
|
|
||||||
|
if m.vadEnabled {
|
||||||
|
// Calculate threshold position
|
||||||
|
threshPos := m.vadThreshold * micBarWidth / 100
|
||||||
|
if threshPos >= micBarWidth {
|
||||||
|
threshPos = micBarWidth - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate filled position based on current level
|
||||||
|
filled := m.micLevel * micBarWidth / 100
|
||||||
|
|
||||||
|
// Build bar
|
||||||
|
var sb strings.Builder
|
||||||
|
for i := 0; i < micBarWidth; i++ {
|
||||||
|
char := "░"
|
||||||
|
if i < filled {
|
||||||
|
char = "█"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overlay threshold marker
|
||||||
|
if i == threshPos {
|
||||||
|
if i < filled {
|
||||||
|
// Threshold is met
|
||||||
|
char = "▓"
|
||||||
|
} else {
|
||||||
|
// Threshold not met
|
||||||
|
char = "│"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.WriteString(char)
|
||||||
|
}
|
||||||
|
micBar = sb.String()
|
||||||
|
} else {
|
||||||
|
micBar = audio.LevelToBar(m.micLevel, micBarWidth)
|
||||||
}
|
}
|
||||||
micPart := fmt.Sprintf("%s:%s", pttIcon, micBar)
|
|
||||||
|
pttStyle := lipgloss.NewStyle()
|
||||||
|
pttIcon := "MIC"
|
||||||
|
|
||||||
|
if m.isPTT {
|
||||||
|
pttIcon = " ON" // Manual ON
|
||||||
|
pttStyle = pttStyle.Foreground(lipgloss.Color("196")).Bold(true) // Red
|
||||||
|
} else if m.vadEnabled {
|
||||||
|
pttIcon = "VAD"
|
||||||
|
// Check if actively transmitting (using logic with decay)
|
||||||
|
isTransmitting := false
|
||||||
|
if !m.isMuted {
|
||||||
|
if m.micLevel > m.vadThreshold {
|
||||||
|
isTransmitting = true
|
||||||
|
} else if !m.vadLastTriggered.IsZero() && time.Since(m.vadLastTriggered) < 1*time.Second {
|
||||||
|
isTransmitting = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if isTransmitting {
|
||||||
|
// Transmitting via VAD: Red/Bold
|
||||||
|
pttStyle = pttStyle.Foreground(lipgloss.Color("196")).Bold(true)
|
||||||
|
} else {
|
||||||
|
// Idle VAD: Gray/Faint
|
||||||
|
pttStyle = pttStyle.Foreground(lipgloss.Color("240")).Faint(true)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Standard Mic (PTT mode but off)
|
||||||
|
pttStyle = pttStyle.Foreground(lipgloss.Color("255")) // White
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply status bar background color to prevent cutting
|
||||||
|
pttStyle = pttStyle.Background(lipgloss.Color("57")) // Matches Top Bar Background
|
||||||
|
|
||||||
|
// Style for the bar itself to maintain background continuity
|
||||||
|
barStyle := lipgloss.NewStyle().Background(lipgloss.Color("57")).Foreground(lipgloss.Color("255"))
|
||||||
|
|
||||||
|
micPart := fmt.Sprintf("%s%s%s",
|
||||||
|
pttStyle.Render(pttIcon),
|
||||||
|
barStyle.Render(":"),
|
||||||
|
barStyle.Render(micBar))
|
||||||
|
|
||||||
rightPart := fmt.Sprintf("%s | %s ", volPart, micPart)
|
rightPart := fmt.Sprintf("%s | %s ", volPart, micPart)
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ type Capturer struct {
|
|||||||
running bool
|
running bool
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
|
wg sync.WaitGroup
|
||||||
|
|
||||||
// Callback for captured audio (called with 960-sample frames)
|
// Callback for captured audio (called with 960-sample frames)
|
||||||
onAudio func(samples []int16)
|
onAudio func(samples []int16)
|
||||||
@@ -136,6 +137,7 @@ func (c *Capturer) Start() error {
|
|||||||
return fmt.Errorf("failed to start audio client: %w", err)
|
return fmt.Errorf("failed to start audio client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.wg.Add(1)
|
||||||
go c.captureLoop()
|
go c.captureLoop()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -151,6 +153,7 @@ func (c *Capturer) Stop() {
|
|||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
close(c.stopChan)
|
close(c.stopChan)
|
||||||
|
c.wg.Wait() // Wait for capture loop to finish before proceeding
|
||||||
c.client.Stop()
|
c.client.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,6 +183,7 @@ func (c *Capturer) IsRunning() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Capturer) captureLoop() {
|
func (c *Capturer) captureLoop() {
|
||||||
|
defer c.wg.Done()
|
||||||
ticker := time.NewTicker(10 * time.Millisecond) // Check more often than 20ms
|
ticker := time.NewTicker(10 * time.Millisecond) // Check more often than 20ms
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CalculateRMSLevel calculates the RMS level of PCM samples and returns 0-100
|
// CalculateRMSLevel calculates the RMS level of PCM samples and returns 0-100 (Logarithmic/dB)
|
||||||
func CalculateRMSLevel(samples []int16) int {
|
func CalculateRMSLevel(samples []int16) int {
|
||||||
if len(samples) == 0 {
|
if len(samples) == 0 {
|
||||||
return 0
|
return 0
|
||||||
@@ -16,15 +16,33 @@ func CalculateRMSLevel(samples []int16) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rms := math.Sqrt(sum / float64(len(samples)))
|
rms := math.Sqrt(sum / float64(len(samples)))
|
||||||
// Normalize to 0-100 (max int16 is 32767)
|
|
||||||
level := int(rms / 32767.0 * 100.0)
|
// Normalize to 0.0 - 1.0
|
||||||
|
val := rms / 32768.0
|
||||||
|
if val < 0.000001 { // Avoid log(0)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to dB
|
||||||
|
db := 20 * math.Log10(val)
|
||||||
|
|
||||||
|
// Map -50dB (silence floor) to 0dB (max) to 0-100
|
||||||
|
const minDB = -50.0
|
||||||
|
|
||||||
|
if db < minDB {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scale
|
||||||
|
level := int((db - minDB) * (100.0 / (0 - minDB)))
|
||||||
|
|
||||||
if level > 100 {
|
if level > 100 {
|
||||||
level = 100
|
level = 100
|
||||||
}
|
}
|
||||||
return level
|
return level
|
||||||
}
|
}
|
||||||
|
|
||||||
// CalculatePeakLevel returns the peak level of PCM samples as 0-100
|
// CalculatePeakLevel returns the peak level of PCM samples as 0-100 (Logarithmic/dB)
|
||||||
func CalculatePeakLevel(samples []int16) int {
|
func CalculatePeakLevel(samples []int16) int {
|
||||||
if len(samples) == 0 {
|
if len(samples) == 0 {
|
||||||
return 0
|
return 0
|
||||||
@@ -40,7 +58,26 @@ func CalculatePeakLevel(samples []int16) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return int(float64(peak) / 32767.0 * 100.0)
|
// Normalize
|
||||||
|
val := float64(peak) / 32768.0
|
||||||
|
if val < 0.000001 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
db := 20 * math.Log10(val)
|
||||||
|
const minDB = -50.0
|
||||||
|
|
||||||
|
if db < minDB {
|
||||||
|
// Linear falloff for very low signals to avoid clutter
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
level := int((db - minDB) * (100.0 / (0 - minDB)))
|
||||||
|
if level > 100 {
|
||||||
|
level = 100
|
||||||
|
}
|
||||||
|
|
||||||
|
return level
|
||||||
}
|
}
|
||||||
|
|
||||||
// LevelToBar converts a 0-100 level to a visual bar string
|
// LevelToBar converts a 0-100 level to a visual bar string
|
||||||
|
|||||||
Reference in New Issue
Block a user