Checkpoint
This commit is contained in:
160 files changed
+76293
-387
No files matched your search
@@ -0,0 +1,113 @@
|
||||
package visual
|
||||
|
||||
import (
|
||||
"math/cmplx"
|
||||
|
||||
"github.com/BeatGlow/beatglow/audio"
|
||||
"github.com/BeatGlow/beatglow/draw"
|
||||
"github.com/BeatGlow/beatglow/draw/pixel"
|
||||
"github.com/BeatGlow/beatglow/internal/calc"
|
||||
"github.com/BeatGlow/display"
|
||||
"gonum.org/v1/gonum/dsp/fourier"
|
||||
)
|
||||
|
||||
type Analyzer struct {
|
||||
display display.Display
|
||||
canvas *draw.Canvas
|
||||
rate audio.Rate
|
||||
step int
|
||||
fft *fourier.FFT
|
||||
complex []complex128
|
||||
samples []float64
|
||||
levels []float64
|
||||
}
|
||||
|
||||
func NewAnalyzer(display display.Display, rate audio.Rate, step int) *Analyzer {
|
||||
return &Analyzer{
|
||||
display: display,
|
||||
canvas: draw.NewCanvas(display),
|
||||
rate: rate,
|
||||
step: step,
|
||||
levels: make([]float64, display.Bounds().Dx()),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Analyzer) ProcessBuffer(buffer *audio.Buffer) {
|
||||
a.frequencySpectrum(buffer.ToMono().Samples[0], a.rate, a.step)
|
||||
}
|
||||
|
||||
func (a *Analyzer) Halt() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (Analyzer) Type() string {
|
||||
return "visual:analyzer"
|
||||
}
|
||||
|
||||
func (a *Analyzer) Render() error {
|
||||
//a.display.Clear()
|
||||
|
||||
for x, level := range a.levels {
|
||||
h := int(56*level + .5)
|
||||
draw.Line(a.display, pixel.On, x, 56-h, x+1, 56)
|
||||
}
|
||||
|
||||
return a.display.Refresh()
|
||||
}
|
||||
|
||||
func (a *Analyzer) resize(n int) {
|
||||
if s := n/2 + 1; len(a.complex) != s {
|
||||
a.fft = fourier.NewFFT(n)
|
||||
a.samples = make([]float64, n)
|
||||
a.complex = make([]complex128, s)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Analyzer) convert(samples []audio.Sample) {
|
||||
for i, v := range samples {
|
||||
a.samples[i] = float64(v)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Analyzer) frequencySpectrum(samples []audio.Sample, rate audio.Rate, step int) error {
|
||||
n := len(samples)
|
||||
a.resize(n)
|
||||
a.convert(samples)
|
||||
a.fft.Coefficients(a.complex, a.samples)
|
||||
|
||||
/*
|
||||
if err := transform(csamples); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
fmt.Printf("%d: %#+v", len(a.complex), a.complex)
|
||||
freqSamplingStep := float64(rate) / float64(n)
|
||||
scaleStep := float64(step) / freqSamplingStep
|
||||
freqLimit := float64(rate) / 2.0
|
||||
|
||||
for i := 0.0; i*freqSamplingStep <= freqLimit; i += scaleStep {
|
||||
//result[int(i*freqSamplingStep)] = math.Abs(real(a.complex[int(i)])) // cmplx.Abs(a.complex[int(i)])
|
||||
a.levels[int(i*freqSamplingStep)] = cmplx.Abs(a.complex[int(i)])
|
||||
}
|
||||
*/
|
||||
for i := 0; i < a.display.Bounds().Size().X; i++ {
|
||||
o := i << 1
|
||||
a.levels[i] = (cmplx.Abs(a.complex[o]) + cmplx.Abs(a.complex[o+1])) / 2
|
||||
}
|
||||
calc.Hamming(a.levels, a.levels)
|
||||
|
||||
a.normalizeSpectrum()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Analyzer) normalizeSpectrum() {
|
||||
max := calc.Max(a.levels)
|
||||
if max > 0 {
|
||||
for i := range a.levels {
|
||||
a.levels[i] /= max
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user