Checkpoint
This commit is contained in:
54 files changed
+6855
-526
No files matched your search
@@ -0,0 +1,220 @@
|
||||
package visual
|
||||
|
||||
import (
|
||||
"image"
|
||||
"math"
|
||||
|
||||
"periph.io/x/conn/v3/physic"
|
||||
|
||||
"go.maze.io/harmon/audio"
|
||||
"go.maze.io/harmon/hardware"
|
||||
"go.maze.io/harmon/hardware/display"
|
||||
"go.maze.io/harmon/internal/pixel"
|
||||
)
|
||||
|
||||
type VUMeter struct {
|
||||
display hardware.Display
|
||||
rate physic.Frequency
|
||||
channel int
|
||||
channels int
|
||||
level float32 // in dBFS
|
||||
alpha float32 // smoothing factor
|
||||
samples []float32
|
||||
}
|
||||
|
||||
func NewVUMeter(display hardware.Display, rate physic.Frequency, channel, channels int) *VUMeter {
|
||||
if channel < 0 {
|
||||
channel = 0
|
||||
} else if channel > 2 {
|
||||
channel = 2
|
||||
}
|
||||
|
||||
if channels < 1 {
|
||||
channels = 1
|
||||
} else if channels > 2 {
|
||||
channels = 2
|
||||
}
|
||||
|
||||
v := &VUMeter{
|
||||
display: display,
|
||||
rate: rate,
|
||||
channel: channel,
|
||||
channels: channels,
|
||||
}
|
||||
v.alpha = calculateAlpha(rate, 0.01)
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *VUMeter) Process(samples audio.Buffer) {
|
||||
floats := samples.Samples()
|
||||
if len(v.samples) != len(floats.Buf) {
|
||||
v.samples = make([]float32, len(floats.Buf))
|
||||
}
|
||||
copy(v.samples, floats.Buf)
|
||||
//audio.Normalize(v.samples) // [-1..+1]
|
||||
//audio.Abs(v.samples) // [ 0..+1]
|
||||
|
||||
rms := audio.RMS(v.samples, v.channel, v.channels) // in [0..1]
|
||||
//logrus.WithFields(logrus.Fields{"rms": rms, "dBFS": rms, "alpha": v.alpha}).Debug("got samples")
|
||||
v.level = float32(rms) // smoothValue(v.level, float32(rms), v.alpha)
|
||||
}
|
||||
|
||||
// smoothValue applies simple exponential smoothing to the signal
|
||||
func smoothValue(previous, current, alpha float32) float32 {
|
||||
return alpha*current + (1-alpha)*previous
|
||||
}
|
||||
|
||||
// calculateAlpha computes the smoothing factor based on sample rate and time constant
|
||||
func calculateAlpha(rate physic.Frequency, timeConstant float64) float32 {
|
||||
// Calculate the smoothing factor alpha
|
||||
return float32(1 - math.Exp(-1/(float64(rate/physic.Hertz)*timeConstant)))
|
||||
}
|
||||
|
||||
func (v *VUMeter) Render() error {
|
||||
// Clear buffer
|
||||
v.display.Clear()
|
||||
|
||||
// Draw background
|
||||
display.DrawSprite(v.display, 0, 0, vumeterBackground[0], pixel.On)
|
||||
|
||||
angle := uint8(float32(len(needleCoordinates)) * v.level)
|
||||
//if v.channel == 1 {
|
||||
// angle = 127 - angle
|
||||
//}
|
||||
point := needleCoordinates[angle]
|
||||
display.DrawLine(v.display, point.X, point.Y, 64, 96, pixel.On)
|
||||
|
||||
return v.display.Refresh()
|
||||
}
|
||||
|
||||
var needleCoordinates = [...]image.Point{
|
||||
{0, 23},
|
||||
{1, 23},
|
||||
{2, 22},
|
||||
{3, 21},
|
||||
{3, 21},
|
||||
{4, 20},
|
||||
{5, 19},
|
||||
{6, 19},
|
||||
{7, 18},
|
||||
{8, 17},
|
||||
{9, 17},
|
||||
{10, 16},
|
||||
{10, 16},
|
||||
{11, 15},
|
||||
{12, 14},
|
||||
{13, 14},
|
||||
{14, 13},
|
||||
{15, 13},
|
||||
{16, 12},
|
||||
{17, 12},
|
||||
{18, 11},
|
||||
{19, 11},
|
||||
{20, 10},
|
||||
{21, 10},
|
||||
{22, 9},
|
||||
{23, 9},
|
||||
{24, 8},
|
||||
{25, 8},
|
||||
{26, 7},
|
||||
{27, 7},
|
||||
{28, 6},
|
||||
{29, 6},
|
||||
{30, 6},
|
||||
{31, 5},
|
||||
{32, 5},
|
||||
{33, 5},
|
||||
{34, 4},
|
||||
{35, 4},
|
||||
{36, 4},
|
||||
{37, 3},
|
||||
{38, 3},
|
||||
{39, 3},
|
||||
{40, 2},
|
||||
{41, 2},
|
||||
{42, 2},
|
||||
{43, 2},
|
||||
{45, 1},
|
||||
{46, 1},
|
||||
{47, 1},
|
||||
{48, 1},
|
||||
{49, 1},
|
||||
{50, 0},
|
||||
{51, 0},
|
||||
{52, 0},
|
||||
{53, 0},
|
||||
{54, 0},
|
||||
{55, 0},
|
||||
{56, 0},
|
||||
{57, 0},
|
||||
{59, 0},
|
||||
{60, 0},
|
||||
{61, 0},
|
||||
{62, 0},
|
||||
{63, 0},
|
||||
{64, 0},
|
||||
{65, 0},
|
||||
{66, 0},
|
||||
{67, 0},
|
||||
{68, 0},
|
||||
{70, 0},
|
||||
{71, 0},
|
||||
{72, 0},
|
||||
{73, 0},
|
||||
{74, 0},
|
||||
{75, 0},
|
||||
{76, 0},
|
||||
{77, 0},
|
||||
{78, 1},
|
||||
{79, 1},
|
||||
{80, 1},
|
||||
{81, 1},
|
||||
{82, 1},
|
||||
{84, 2},
|
||||
{85, 2},
|
||||
{86, 2},
|
||||
{87, 2},
|
||||
{88, 3},
|
||||
{89, 3},
|
||||
{90, 3},
|
||||
{91, 4},
|
||||
{92, 4},
|
||||
{93, 4},
|
||||
{94, 5},
|
||||
{95, 5},
|
||||
{96, 5},
|
||||
{97, 6},
|
||||
{98, 6},
|
||||
{99, 6},
|
||||
{100, 7},
|
||||
{101, 7},
|
||||
{102, 8},
|
||||
{103, 8},
|
||||
{104, 9},
|
||||
{105, 9},
|
||||
{106, 10},
|
||||
{107, 10},
|
||||
{108, 11},
|
||||
{109, 11},
|
||||
{110, 12},
|
||||
{111, 12},
|
||||
{112, 13},
|
||||
{113, 13},
|
||||
{114, 14},
|
||||
{115, 14},
|
||||
{116, 15},
|
||||
{117, 16},
|
||||
{117, 16},
|
||||
{118, 17},
|
||||
{119, 17},
|
||||
{120, 18},
|
||||
{121, 19},
|
||||
{122, 19},
|
||||
{123, 20},
|
||||
{124, 21},
|
||||
{124, 21},
|
||||
{125, 22},
|
||||
{126, 23},
|
||||
{127, 23},
|
||||
}
|
||||
Reference in new issue
Block a user