Checkpoint
This commit is contained in:
160 files changed
+76293
-387
No files matched your search
@@ -0,0 +1,111 @@
|
||||
package widget
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
|
||||
"github.com/BeatGlow/audio"
|
||||
"github.com/BeatGlow/beatglow/ui"
|
||||
"github.com/BeatGlow/display"
|
||||
"github.com/BeatGlow/display/pixel"
|
||||
)
|
||||
|
||||
type WaveForm struct {
|
||||
Display display.Display
|
||||
Position image.Point
|
||||
buffer draw.Image
|
||||
height int
|
||||
width int
|
||||
bins int
|
||||
level [2][]float64
|
||||
changed bool
|
||||
}
|
||||
|
||||
func NewWaveform(display display.Display, at image.Point, width, height int) *WaveForm {
|
||||
bins := width / 2
|
||||
return &WaveForm{
|
||||
Display: display,
|
||||
Position: at,
|
||||
buffer: ui.BufferFor(display.ColorModel(), width, height),
|
||||
width: width,
|
||||
height: height,
|
||||
bins: bins,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WaveForm) Render(refresh bool) bool {
|
||||
if !(refresh || w.changed) {
|
||||
return false
|
||||
}
|
||||
|
||||
draw.Draw(w.buffer, w.buffer.Bounds(), image.Black, image.Point{}, draw.Src)
|
||||
|
||||
for i := range w.level[0] {
|
||||
w.drawLevel(0, i)
|
||||
w.drawLevel(1, i)
|
||||
}
|
||||
|
||||
draw.Draw(w.Display, w.buffer.Bounds().Add(w.Position), w.buffer, image.Point{}, draw.Src)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *WaveForm) drawLevel(channel, i int) {
|
||||
var offset = (w.width / 2)
|
||||
if channel == 0 {
|
||||
offset -= i
|
||||
} else {
|
||||
offset += i
|
||||
}
|
||||
|
||||
y := w.height/2 + int(float64(w.height/2)*w.level[channel][i]+.5)
|
||||
w.buffer.Set(offset, y, pixel.On)
|
||||
}
|
||||
|
||||
func (w *WaveForm) ProcessBuffer(buffer audio.Buffer[float64]) {
|
||||
var (
|
||||
channels = buffer.Channels()
|
||||
)
|
||||
|
||||
if channels == 1 {
|
||||
w.level[0] = condenseSamples(w.level[0], buffer[0], w.bins)
|
||||
w.level[1] = w.level[0]
|
||||
} else {
|
||||
w.level[0] = condenseSamples(w.level[0], buffer[0], w.bins)
|
||||
w.level[1] = condenseSamples(w.level[1], buffer[1], w.bins)
|
||||
}
|
||||
|
||||
w.changed = true
|
||||
}
|
||||
|
||||
// condenseSamples reduces the input samples to a fixed number of points.
|
||||
// It aggregates values within each segment using averaging.
|
||||
func condenseSamples(dst, src []float64, targetWidth int) []float64 {
|
||||
if len(dst) != targetWidth {
|
||||
dst = make([]float64, targetWidth)
|
||||
}
|
||||
|
||||
if len(src) <= targetWidth {
|
||||
// If the buffer is smaller than or equal to the target width, return as is.
|
||||
copy(dst, src)
|
||||
return dst
|
||||
}
|
||||
|
||||
binSize := float64(len(src)) / float64(targetWidth)
|
||||
for i := 0; i < targetWidth; i++ {
|
||||
start := int(float64(i) * binSize)
|
||||
end := int(float64(i+1) * binSize)
|
||||
if end > len(src) {
|
||||
end = len(src)
|
||||
}
|
||||
|
||||
// Compute the average for the segment.
|
||||
sum := 0.0
|
||||
for j := start; j < end; j++ {
|
||||
sum += src[j]
|
||||
}
|
||||
dst[i] = sum / float64(end-start)
|
||||
}
|
||||
|
||||
return dst
|
||||
}
|
||||
Reference in new issue
Block a user