Files
beatglow/input/audio/waveform.go
T
2026-10-29 17:28:51 +01:00

44 lines
784 B
Go

package audio
import (
"image"
"image/color"
"image/draw"
"math"
)
type Waveform struct {
bins []int8
height int
scale float64
decimate int
r image.Rectangle
}
func NewWaveform(x, y int) *Waveform {
return &Waveform{
bins: make([]int8, x),
height: y,
scale: float64(y>>1) / math.MaxInt16,
decimate: 1,
r: image.Rect(0, 0, x, y),
}
}
func (v *Waveform) Process(sample int16) {
v.shift()
v.bins[0] = int8(float64(sample) * v.scale)
}
func (v *Waveform) shift() {
copy(v.bins[1:], v.bins)
v.bins[0] = 0
}
func (v *Waveform) Draw(i draw.Image, o image.Point) {
draw.Draw(i, v.r.Add(o), image.Black, image.Point{}, draw.Src)
for x, h := range v.bins {
drawLine(i, o.X+x, o.Y+int(h), o.X+x, o.Y+v.height, color.White)
}
}