mirror of
https://github.com/vsariola/sointu.git
synced 2025-06-04 01:28:45 -04:00
refactor(tracker): split Volume to PeakVolume and AverageVolume
This commit is contained in:
parent
1a8a317464
commit
50ccfe03da
@ -218,6 +218,6 @@ func (t *Tracker) layoutSongOptions(gtx C) D {
|
|||||||
gtx.Constraints.Min = image.Pt(0, 0)
|
gtx.Constraints.Min = image.Pt(0, 0)
|
||||||
return recordBtnStyle.Layout(gtx)
|
return recordBtnStyle.Layout(gtx)
|
||||||
}),
|
}),
|
||||||
layout.Rigid(VuMeter{Volume: t.lastVolume, Range: 100}.Layout),
|
layout.Rigid(VuMeter{AverageVolume: t.lastAvgVolume, PeakVolume: t.lastPeakVolume, Range: 100}.Layout),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,8 @@ type Tracker struct {
|
|||||||
TrackEditor *TrackEditor
|
TrackEditor *TrackEditor
|
||||||
Explorer *explorer.Explorer
|
Explorer *explorer.Explorer
|
||||||
|
|
||||||
lastVolume tracker.Volume
|
lastAvgVolume tracker.Volume
|
||||||
|
lastPeakVolume tracker.Volume
|
||||||
|
|
||||||
wavFilePath string
|
wavFilePath string
|
||||||
quitChannel chan struct{}
|
quitChannel chan struct{}
|
||||||
@ -198,7 +199,8 @@ mainloop:
|
|||||||
if err, ok := e.Inner.(tracker.PlayerVolumeErrorMessage); ok {
|
if err, ok := e.Inner.(tracker.PlayerVolumeErrorMessage); ok {
|
||||||
t.Alert.Update(err.Error(), Warning, time.Second*3)
|
t.Alert.Update(err.Error(), Warning, time.Second*3)
|
||||||
}
|
}
|
||||||
t.lastVolume = e.Volume
|
t.lastAvgVolume = e.AverageVolume
|
||||||
|
t.lastPeakVolume = e.PeakVolume
|
||||||
t.InstrumentEditor.voiceStates = e.VoiceStates
|
t.InstrumentEditor.voiceStates = e.VoiceStates
|
||||||
t.ProcessPlayerMessage(e)
|
t.ProcessPlayerMessage(e)
|
||||||
w.Invalidate()
|
w.Invalidate()
|
||||||
|
@ -11,7 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type VuMeter struct {
|
type VuMeter struct {
|
||||||
Volume tracker.Volume
|
AverageVolume tracker.Volume
|
||||||
|
PeakVolume tracker.Volume
|
||||||
Range float32
|
Range float32
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ func (v VuMeter) Layout(gtx C) D {
|
|||||||
gtx.Constraints.Max.Y = gtx.Dp(unit.Dp(12))
|
gtx.Constraints.Max.Y = gtx.Dp(unit.Dp(12))
|
||||||
height := gtx.Dp(unit.Dp(6))
|
height := gtx.Dp(unit.Dp(6))
|
||||||
for j := 0; j < 2; j++ {
|
for j := 0; j < 2; j++ {
|
||||||
value := float32(v.Volume.Average[j]) + v.Range
|
value := float32(v.AverageVolume[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 {
|
||||||
@ -28,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 := float32(v.Volume.Peak[j]) + v.Range
|
valueMax := float32(v.PeakVolume[j]) + v.Range
|
||||||
if valueMax > 0 {
|
if valueMax > 0 {
|
||||||
color := white
|
color := white
|
||||||
if valueMax >= v.Range {
|
if valueMax >= v.Range {
|
||||||
|
@ -21,7 +21,8 @@ type (
|
|||||||
samplesSinceEvent []int
|
samplesSinceEvent []int
|
||||||
samplesPerRow int
|
samplesPerRow int
|
||||||
bpm int
|
bpm int
|
||||||
volume Volume
|
avgVolumeMeter VolumeAnalyzer
|
||||||
|
peakVolumeMeter VolumeAnalyzer
|
||||||
voiceStates [vm.MAX_VOICES]float32
|
voiceStates [vm.MAX_VOICES]float32
|
||||||
|
|
||||||
recording bool
|
recording bool
|
||||||
@ -59,7 +60,8 @@ type (
|
|||||||
// Volume and SongRow are transmitted so frequently that they are treated specially, to avoid boxing. All the
|
// Volume and SongRow are transmitted so frequently that they are treated specially, to avoid boxing. All the
|
||||||
// rest messages can be boxed to interface{}
|
// rest messages can be boxed to interface{}
|
||||||
PlayerMessage struct {
|
PlayerMessage struct {
|
||||||
Volume Volume
|
AverageVolume Volume
|
||||||
|
PeakVolume Volume
|
||||||
SongRow SongRow
|
SongRow SongRow
|
||||||
VoiceStates [vm.MAX_VOICES]float32
|
VoiceStates [vm.MAX_VOICES]float32
|
||||||
Inner interface{}
|
Inner interface{}
|
||||||
@ -90,7 +92,8 @@ func NewPlayer(synther sointu.Synther, playerMessages chan<- PlayerMessage, mode
|
|||||||
playerMessages: playerMessages,
|
playerMessages: playerMessages,
|
||||||
modelMessages: modelMessages,
|
modelMessages: modelMessages,
|
||||||
synther: synther,
|
synther: synther,
|
||||||
volume: Volume{Average: [2]float64{1e-9, 1e-9}, Peak: [2]float64{1e-9, 1e-9}},
|
avgVolumeMeter: VolumeAnalyzer{Attack: 0.3, Release: 0.3, Min: -100, Max: 20},
|
||||||
|
peakVolumeMeter: VolumeAnalyzer{Attack: 1e-4, Release: 1, Min: -100, Max: 20},
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
@ -170,11 +173,15 @@ func (p *Player) Process(buffer sointu.AudioBuffer, context PlayerProcessContext
|
|||||||
}
|
}
|
||||||
// when the buffer is full, return
|
// when the buffer is full, return
|
||||||
if len(buffer) == 0 {
|
if len(buffer) == 0 {
|
||||||
err := p.volume.Analyze(oldBuffer, 0.3, 1e-4, 1, -100, 20)
|
err := p.avgVolumeMeter.Update(oldBuffer)
|
||||||
|
err2 := p.peakVolumeMeter.Update(oldBuffer)
|
||||||
var msg interface{}
|
var msg interface{}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg = PlayerVolumeErrorMessage{err}
|
msg = PlayerVolumeErrorMessage{err}
|
||||||
}
|
}
|
||||||
|
if err2 != nil {
|
||||||
|
msg = PlayerVolumeErrorMessage{err}
|
||||||
|
}
|
||||||
p.trySend(msg)
|
p.trySend(msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -323,7 +330,7 @@ func (p *Player) compileOrUpdateSynth() {
|
|||||||
// all sends from player are always non-blocking, to ensure that the player thread cannot end up in a dead-lock
|
// all sends from player are always non-blocking, to ensure that the player thread cannot end up in a dead-lock
|
||||||
func (p *Player) trySend(message interface{}) {
|
func (p *Player) trySend(message interface{}) {
|
||||||
select {
|
select {
|
||||||
case p.playerMessages <- PlayerMessage{Volume: p.volume, SongRow: p.position, VoiceStates: p.voiceStates, Inner: message}:
|
case p.playerMessages <- PlayerMessage{AverageVolume: p.avgVolumeMeter.Level, PeakVolume: p.peakVolumeMeter.Level, SongRow: p.position, VoiceStates: p.voiceStates, Inner: message}:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,53 +7,61 @@ import (
|
|||||||
"github.com/vsariola/sointu"
|
"github.com/vsariola/sointu"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Volume represents an average and peak volume measurement, in decibels. 0 dB =
|
type (
|
||||||
// signal level of +-1.
|
Volume [2]float64
|
||||||
type Volume struct {
|
|
||||||
Average [2]float64
|
|
||||||
Peak [2]float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Analyze updates Average and Peak fields, by analyzing the given buffer.
|
// VolumeAnalyzer measures the volume in an AudioBuffer, in decibels relative to
|
||||||
|
// full scale (0 dB = signal level of +-1)
|
||||||
|
VolumeAnalyzer struct {
|
||||||
|
Level Volume // current volume level of left and right channels
|
||||||
|
Attack float64 // attack time constant in seconds
|
||||||
|
Release float64 // release time constant in seconds
|
||||||
|
Min float64 // minimum volume in decibels
|
||||||
|
Max float64 // maximum volume in decibels
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var nanError = errors.New("NaN detected in master output")
|
||||||
|
|
||||||
|
// Update updates the Level field, by analyzing the given buffer.
|
||||||
//
|
//
|
||||||
// Internally, it first converts the signal to decibels (0 dB = +-1). Then, the
|
// Internally, it first converts the signal to decibels (0 dB = +-1). Then, the
|
||||||
// average volume level is computed by smoothing the decibel values with a
|
// average volume level is computed by smoothing the decibel values with a
|
||||||
// exponentially decaying average, with a time constant tau (in seconds).
|
// exponentially decaying average, with a time constant Attack (in seconds) if
|
||||||
// Typical value could be 0.3 (seconds).
|
// the decibel value is greater than current level and time constant Decay (in
|
||||||
|
// seconds) if the decibel value is less than current level.
|
||||||
//
|
//
|
||||||
// Peak volume detection is similar exponential smoothing, but the time
|
// Typical time constants for average level detection would be 0.3 seconds for
|
||||||
// constants for attack and release are different. Generally attack << release.
|
// both attack and release. For peak level detection, attack could be 1.5e-3 and
|
||||||
// Typical values could be attack 1.5e-3 and release 1.5 (seconds)
|
// release 1.5 (seconds)
|
||||||
//
|
//
|
||||||
// minVolume and maxVolume are hard limits in decibels to prevent negative
|
// MinVolume and MaxVolume are hard limits in decibels to prevent negative
|
||||||
// infinities for volumes
|
// infinities for volumes
|
||||||
func (v *Volume) Analyze(buffer sointu.AudioBuffer, tau float64, attack float64, release float64, minVolume float64, maxVolume float64) error {
|
func (v *VolumeAnalyzer) Update(buffer sointu.AudioBuffer) (err error) {
|
||||||
alpha := 1 - math.Exp(-1.0/(tau*44100)) // from https://en.wikipedia.org/wiki/Exponential_smoothing
|
// from https://en.wikipedia.org/wiki/Exponential_smoothing
|
||||||
alphaAttack := 1 - math.Exp(-1.0/(attack*44100))
|
alphaAttack := 1 - math.Exp(-1.0/(v.Attack*44100))
|
||||||
alphaRelease := 1 - math.Exp(-1.0/(release*44100))
|
alphaRelease := 1 - math.Exp(-1.0/(v.Release*44100))
|
||||||
var err error
|
|
||||||
for j := 0; j < 2; j++ {
|
for j := 0; j < 2; j++ {
|
||||||
for i := 0; i < len(buffer); i++ {
|
for i := 0; i < len(buffer); i++ {
|
||||||
sample2 := float64(buffer[i][j] * buffer[i][j])
|
sample2 := float64(buffer[i][j] * buffer[i][j])
|
||||||
if math.IsNaN(sample2) {
|
if math.IsNaN(sample2) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = errors.New("NaN detected in master output")
|
err = nanError
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
dB := 10 * math.Log10(float64(sample2))
|
dB := 10 * math.Log10(sample2)
|
||||||
if dB < minVolume || math.IsNaN(dB) {
|
if dB < v.Min || math.IsNaN(dB) {
|
||||||
dB = minVolume
|
dB = v.Min
|
||||||
}
|
}
|
||||||
if dB > maxVolume {
|
if dB > v.Max {
|
||||||
dB = maxVolume
|
dB = v.Max
|
||||||
}
|
}
|
||||||
v.Average[j] += (dB - v.Average[j]) * alpha
|
a := alphaAttack
|
||||||
alphaPeak := alphaAttack
|
if dB < v.Level[j] {
|
||||||
if dB < v.Peak[j] {
|
a = alphaRelease
|
||||||
alphaPeak = alphaRelease
|
|
||||||
}
|
}
|
||||||
v.Peak[j] += (dB - v.Peak[j]) * alphaPeak
|
v.Level[j] += (dB - v.Level[j]) * a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user