Fix debounce race and recorder callback write handling

This commit is contained in:
jahruz67
2026-05-14 19:49:01 -07:00
parent 54ec0fb197
commit 1639459293
2 changed files with 16 additions and 5 deletions
+5 -1
View File
@@ -149,11 +149,15 @@ var lastToggleUnixNano int64
func (a *App) ToggleRecording() {
// Debounce toggle calls to prevent rapid firing from double-binds or Wayland glitches
now := time.Now().UnixNano()
for {
last := atomic.LoadInt64(&lastToggleUnixNano)
if last != 0 && time.Duration(now-last) < 500*time.Millisecond {
return
}
atomic.StoreInt64(&lastToggleUnixNano, now)
if atomic.CompareAndSwapInt64(&lastToggleUnixNano, last, now) {
break
}
}
if atomic.LoadInt32(&a.recording) == 1 {
a.StopRecording()
+8 -1
View File
@@ -254,8 +254,15 @@ func (r *AudioRecorder) Cleanup() {
// onAudioData is called by miniaudio when audio data is available.
func (r *AudioRecorder) onAudioData(_, inputSamples []byte, _ uint32) {
r.mu.Lock()
defer r.mu.Unlock()
if r.outputFile != nil && len(inputSamples) > 0 {
n, _ := r.outputFile.Write(inputSamples)
n, err := r.outputFile.Write(inputSamples)
if err != nil {
logger.Error("Failed to write audio data: %v", err)
return
}
r.dataSize += uint32(n)
// Calculate volume if callback is set