From ce52aa0ee9a51f455a06217aa026e2cd5cc11b0b Mon Sep 17 00:00:00 2001 From: vsariola <5684185+vsariola@users.noreply.github.com> Date: Mon, 12 Apr 2021 20:05:18 +0300 Subject: [PATCH] feat(tracker, gioui): add error message if vuanalyzer detects a NaN Closes #50 --- tracker/gioui/run.go | 4 ++++ tracker/gioui/tracker.go | 4 +++- tracker/vuanalyzer.go | 9 +++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/tracker/gioui/run.go b/tracker/gioui/run.go index c6b8caa..aaf7f58 100644 --- a/tracker/gioui/run.go +++ b/tracker/gioui/run.go @@ -3,6 +3,7 @@ package gioui import ( "fmt" "os" + "time" "gioui.org/app" "gioui.org/io/clipboard" @@ -29,6 +30,9 @@ func (t *Tracker) Run(w *app.Window) error { case v := <-t.volumeChan: t.lastVolume = v w.Invalidate() + case e := <-t.errorChannel: + t.Alert.Update(e.Error(), Error, time.Second*5) + w.Invalidate() case e := <-w.Events(): switch e := e.(type) { case system.DestroyEvent: diff --git a/tracker/gioui/tracker.go b/tracker/gioui/tracker.go index c79c1fd..eb23524 100644 --- a/tracker/gioui/tracker.go +++ b/tracker/gioui/tracker.go @@ -69,6 +69,7 @@ type Tracker struct { player *tracker.Player refresh chan struct{} playerCloser chan struct{} + errorChannel chan error audioContext sointu.AudioContext *tracker.Model @@ -152,10 +153,11 @@ func New(audioContext sointu.AudioContext, synthService sointu.SynthService, syn PatternOrderList: &layout.List{Axis: layout.Vertical}, PatternOrderScrollBar: &ScrollBar{Axis: layout.Vertical}, ConfirmInstrDelete: new(Dialog), + errorChannel: make(chan error, 32), } t.Model = tracker.NewModel() vuBufferObserver := make(chan []float32) - go tracker.VuAnalyzer(0.3, 1e-4, 1, -100, 20, vuBufferObserver, t.volumeChan) + go tracker.VuAnalyzer(0.3, 1e-4, 1, -100, 20, vuBufferObserver, t.volumeChan, t.errorChannel) t.Theme.Palette.Fg = primaryColor t.Theme.Palette.ContrastFg = black t.SetEditMode(tracker.EditTracks) diff --git a/tracker/vuanalyzer.go b/tracker/vuanalyzer.go index 9168ca6..222d45e 100644 --- a/tracker/vuanalyzer.go +++ b/tracker/vuanalyzer.go @@ -1,6 +1,7 @@ package tracker import ( + "errors" "math" ) @@ -28,7 +29,7 @@ type Volume struct { // // minVolume is just a hard limit for the vuanalyzer volumes, in decibels, just to // prevent negative infinities for volumes -func VuAnalyzer(tau float64, attack float64, release float64, minVolume float64, maxVolume float64, bc <-chan []float32, vc chan<- Volume) { +func VuAnalyzer(tau float64, attack float64, release float64, minVolume float64, maxVolume float64, bc <-chan []float32, vc chan<- Volume, ec chan<- error) { v := Volume{Average: [2]float64{minVolume, minVolume}, Peak: [2]float64{minVolume, minVolume}} alpha := 1 - math.Exp(-1.0/(tau*44100)) // from https://en.wikipedia.org/wiki/Exponential_smoothing alphaAttack := 1 - math.Exp(-1.0/(attack*44100)) @@ -38,7 +39,11 @@ func VuAnalyzer(tau float64, attack float64, release float64, minVolume float64, for i := 0; i < len(buffer); i += 2 { sample2 := float64(buffer[i+j] * buffer[i+j]) if math.IsNaN(sample2) { - sample2 = float64(minVolume) + select { + case ec <- errors.New("NaN detected in master output"): + default: + } + continue } dB := 10 * math.Log10(float64(sample2)) if dB < minVolume || math.IsNaN(dB) {