diff --git a/cmd/sointu-track/main.go b/cmd/sointu-track/main.go index 7b0a891..0800725 100644 --- a/cmd/sointu-track/main.go +++ b/cmd/sointu-track/main.go @@ -70,6 +70,7 @@ func main() { go func() { trackerUi.Main() audioCloser.Close() + detector.Close() if *cpuprofile != "" { pprof.StopCPUProfile() f.Close() diff --git a/cmd/sointu-vsti/main.go b/cmd/sointu-vsti/main.go index e54f064..bd9ed91 100644 --- a/cmd/sointu-vsti/main.go +++ b/cmd/sointu-vsti/main.go @@ -135,6 +135,7 @@ func init() { CloseFunc: func() { t.Exec() <- func() { t.ForceQuit().Do() } t.WaitQuitted() + detector.Close() }, GetChunkFunc: func(isPreset bool) []byte { retChn := make(chan []byte) diff --git a/tracker/broker.go b/tracker/broker.go index c5fbede..dacae6b 100644 --- a/tracker/broker.go +++ b/tracker/broker.go @@ -49,6 +49,7 @@ type ( // which gets executed in the detector goroutine. MsgToDetector struct { Reset bool + Quit bool Data any // TODO: consider using a sum type here, for a bit more type safety. See: https://www.jerf.org/iri/post/2917/ } ) @@ -62,12 +63,6 @@ func NewBroker() *Broker { } } -func (b *Broker) Close() { - close(b.ToPlayer) - close(b.ToModel) - close(b.ToDetector) -} - // GetAudioBuffer returns an audio buffer from the buffer pool. The buffer is // guaranteed to be empty. After using the buffer, it should be returned to the // pool with PutAudioBuffer. diff --git a/tracker/detector.go b/tracker/detector.go index 99b7b3b..e8cb925 100644 --- a/tracker/detector.go +++ b/tracker/detector.go @@ -106,6 +106,9 @@ func (s *Detector) Run() { s.loudnessDetector.reset() s.peakDetector.reset() } + if msg.Quit { + return + } switch data := msg.Data.(type) { case *sointu.AudioBuffer: buf := *data @@ -144,6 +147,11 @@ func (s *Detector) Run() { } } +// Close may theoretically block if the broker is full, but it should not happen in practice +func (s *Detector) Close() { + s.broker.ToDetector <- MsgToDetector{Quit: true} +} + func makeLoudnessDetector(weighting WeightingType) loudnessDetector { return loudnessDetector{ weighting: weightings[weighting], diff --git a/tracker/gioui/tracker.go b/tracker/gioui/tracker.go index 7bed2ac..b310114 100644 --- a/tracker/gioui/tracker.go +++ b/tracker/gioui/tracker.go @@ -166,7 +166,6 @@ func (t *Tracker) Main() { w.Perform(system.ActionClose) t.SaveRecovery() t.quitWG.Done() - t.Broker().Close() } func eventLoop(w *app.Window, events chan<- event.Event, acks <-chan struct{}) { diff --git a/tracker/model_test.go b/tracker/model_test.go index 4915903..0810294 100644 --- a/tracker/model_test.go +++ b/tracker/model_test.go @@ -265,7 +265,6 @@ func FuzzModel(f *testing.F) { reader := bytes.NewReader(slice) synther := vm.GoSynther{} broker := tracker.NewBroker() - defer broker.Close() model, player := tracker.NewModelPlayer(broker, synther, NullContext{}, "") buf := make([][2]float32, 2048) closeChan := make(chan struct{}) @@ -309,5 +308,6 @@ func FuzzModel(f *testing.F) { } } closeChan <- struct{}{} + broker.ToDetector <- tracker.MsgToDetector{Quit: true} }) }