mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-14 02:54:37 -04:00
fix(tracker): avoid NaNs in volume analyzer better
This commit is contained in:
@ -149,7 +149,7 @@ func New(audioContext sointu.AudioContext, synthService sointu.SynthService, syn
|
|||||||
}
|
}
|
||||||
t.Model = tracker.NewModel()
|
t.Model = tracker.NewModel()
|
||||||
vuBufferObserver := make(chan []float32)
|
vuBufferObserver := make(chan []float32)
|
||||||
go tracker.VuAnalyzer(0.3, 1e-4, 1, -100, vuBufferObserver, t.volumeChan)
|
go tracker.VuAnalyzer(0.3, 1e-4, 1, -100, 20, vuBufferObserver, t.volumeChan)
|
||||||
t.Theme.Palette.Fg = primaryColor
|
t.Theme.Palette.Fg = primaryColor
|
||||||
t.Theme.Palette.ContrastFg = black
|
t.Theme.Palette.ContrastFg = black
|
||||||
t.SetEditMode(tracker.EditTracks)
|
t.SetEditMode(tracker.EditTracks)
|
||||||
|
@ -21,7 +21,7 @@ func (v VuMeter) Layout(gtx C) D {
|
|||||||
gtx.Constraints.Max.Y = gtx.Px(unit.Dp(12))
|
gtx.Constraints.Max.Y = gtx.Px(unit.Dp(12))
|
||||||
height := gtx.Px(unit.Dp(6))
|
height := gtx.Px(unit.Dp(6))
|
||||||
for j := 0; j < 2; j++ {
|
for j := 0; j < 2; j++ {
|
||||||
value := v.Volume.Average[j] + v.Range
|
value := float32(v.Volume.Average[j]) + v.Range
|
||||||
if value > 0 {
|
if value > 0 {
|
||||||
x := int(value/v.Range*float32(gtx.Constraints.Max.X) + 0.5)
|
x := int(value/v.Range*float32(gtx.Constraints.Max.X) + 0.5)
|
||||||
if x > gtx.Constraints.Max.X {
|
if x > gtx.Constraints.Max.X {
|
||||||
@ -29,7 +29,7 @@ func (v VuMeter) Layout(gtx C) D {
|
|||||||
}
|
}
|
||||||
paint.FillShape(gtx.Ops, mediumEmphasisTextColor, clip.Rect(image.Rect(0, 0, x, height)).Op())
|
paint.FillShape(gtx.Ops, mediumEmphasisTextColor, clip.Rect(image.Rect(0, 0, x, height)).Op())
|
||||||
}
|
}
|
||||||
valueMax := v.Volume.Peak[j] + v.Range
|
valueMax := float32(v.Volume.Peak[j]) + v.Range
|
||||||
if valueMax > 0 {
|
if valueMax > 0 {
|
||||||
color := white
|
color := white
|
||||||
if valueMax >= v.Range {
|
if valueMax >= v.Range {
|
||||||
|
@ -7,8 +7,8 @@ import (
|
|||||||
// Volume represents an average and peak volume measurement, in decibels. 0 dB =
|
// Volume represents an average and peak volume measurement, in decibels. 0 dB =
|
||||||
// signal level of +-1.
|
// signal level of +-1.
|
||||||
type Volume struct {
|
type Volume struct {
|
||||||
Average [2]float32
|
Average [2]float64
|
||||||
Peak [2]float32
|
Peak [2]float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// VuAnalyzer receives stereo from the bc channel and converts these into peak &
|
// VuAnalyzer receives stereo from the bc channel and converts these into peak &
|
||||||
@ -28,11 +28,11 @@ type Volume struct {
|
|||||||
//
|
//
|
||||||
// minVolume is just a hard limit for the vuanalyzer volumes, in decibels, just to
|
// minVolume is just a hard limit for the vuanalyzer volumes, in decibels, just to
|
||||||
// prevent negative infinities for volumes
|
// prevent negative infinities for volumes
|
||||||
func VuAnalyzer(tau float64, attack float64, release float64, minVolume float32, bc <-chan []float32, vc chan<- Volume) {
|
func VuAnalyzer(tau float64, attack float64, release float64, minVolume float64, maxVolume float64, bc <-chan []float32, vc chan<- Volume) {
|
||||||
v := Volume{Average: [2]float32{minVolume, minVolume}, Peak: [2]float32{minVolume, minVolume}}
|
v := Volume{Average: [2]float64{minVolume, minVolume}, Peak: [2]float64{minVolume, minVolume}}
|
||||||
alpha := 1 - float32(math.Exp(-1.0/(tau*44100))) // from https://en.wikipedia.org/wiki/Exponential_smoothing
|
alpha := 1 - math.Exp(-1.0/(tau*44100)) // from https://en.wikipedia.org/wiki/Exponential_smoothing
|
||||||
alphaAttack := 1 - float32(math.Exp(-1.0/(attack*44100)))
|
alphaAttack := 1 - math.Exp(-1.0/(attack*44100))
|
||||||
alphaRelease := 1 - float32(math.Exp(-1.0/(release*44100)))
|
alphaRelease := 1 - math.Exp(-1.0/(release*44100))
|
||||||
for buffer := range bc {
|
for buffer := range bc {
|
||||||
for j := 0; j < 2; j++ {
|
for j := 0; j < 2; j++ {
|
||||||
for i := 0; i < len(buffer); i += 2 {
|
for i := 0; i < len(buffer); i += 2 {
|
||||||
@ -40,10 +40,13 @@ func VuAnalyzer(tau float64, attack float64, release float64, minVolume float32,
|
|||||||
if math.IsNaN(sample2) {
|
if math.IsNaN(sample2) {
|
||||||
sample2 = float64(minVolume)
|
sample2 = float64(minVolume)
|
||||||
}
|
}
|
||||||
dB := float32(10 * math.Log10(float64(sample2)))
|
dB := 10 * math.Log10(float64(sample2))
|
||||||
if dB < minVolume {
|
if dB < minVolume || math.IsNaN(dB) {
|
||||||
dB = minVolume
|
dB = minVolume
|
||||||
}
|
}
|
||||||
|
if dB > maxVolume {
|
||||||
|
dB = maxVolume
|
||||||
|
}
|
||||||
v.Average[j] += (dB - v.Average[j]) * alpha
|
v.Average[j] += (dB - v.Average[j]) * alpha
|
||||||
alphaPeak := alphaAttack
|
alphaPeak := alphaAttack
|
||||||
if dB < v.Peak[j] {
|
if dB < v.Peak[j] {
|
||||||
|
Reference in New Issue
Block a user