Detach tone-test savers on reconnect

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-09 23:12:56 -04:00
committed by Brandon McGinty
parent b23afebdb3
commit bad4e8172a
4 changed files with 66 additions and 15 deletions
+24 -6
View File
@@ -65,10 +65,11 @@ func StartToneGenerator(client *gumble.Client, stop <-chan struct{}) {
// AudioFileSaver implements gumble.AudioListener and writes all incoming PCM
// audio to a single raw 16-bit little-endian stereo 48kHz file.
type AudioFileSaver struct {
file *os.File
stop chan struct{}
mu sync.Mutex
wg sync.WaitGroup
file *os.File
stop chan struct{}
mu sync.Mutex
stopped bool
wg sync.WaitGroup
}
// NewAudioFileSaver creates the output file and returns a configured saver.
@@ -95,15 +96,32 @@ func NewAudioFileSaver(path string) (*AudioFileSaver, error) {
// Stop closes the stop channel, waits for all stream goroutines to finish,
// and closes the output file.
func (s *AudioFileSaver) Stop() {
s.mu.Lock()
if s.stopped {
s.mu.Unlock()
return
}
s.stopped = true
close(s.stop)
s.mu.Unlock()
s.wg.Wait()
s.file.Close()
s.mu.Lock()
_ = s.file.Close()
s.mu.Unlock()
}
// OnAudioStream implements gumble.AudioListener.
func (s *AudioFileSaver) OnAudioStream(e *gumble.AudioStreamEvent) {
fmt.Fprintf(os.Stderr, "tonetest: incoming audio stream from %s\n", e.User.Name)
s.mu.Lock()
if s.stopped {
s.mu.Unlock()
return
}
s.wg.Add(1)
s.mu.Unlock()
fmt.Fprintf(os.Stderr, "tonetest: incoming audio stream from %s\n", e.User.Name)
go func() {
defer s.wg.Done()