package widget import ( "image" "image/draw" "math" "sync/atomic" "github.com/sirupsen/logrus" "github.com/BeatGlow/audio" "github.com/BeatGlow/audio/dsp" "github.com/BeatGlow/audio/dsp/window" "github.com/BeatGlow/audio/filter" "github.com/BeatGlow/display/pixel" "github.com/BeatGlow/beatglow/app" "github.com/BeatGlow/beatglow/internal/calc" "github.com/BeatGlow/beatglow/player" ) // Spectrum analyzer widget. // // Can render mono and stereo spectrum analyzer or waterfall plots. type Spectrum struct { Base minFrequency int maxFrequency int channels int hold int bins int waterfall bool analyzer *dsp.FrequencyPowerCalculator[float64] samples [2]audio.Samples[float64] // samples without DC component powers [2][]dsp.FrequencyPower // spectral powers bands [2][]float64 // summarized spectral powers peaks [2][][]float64 // peak levels changed atomic.Bool } func NewSpectrum(size image.Point) *Spectrum { bins := size.X / 4 / 1 w := &Spectrum{ minFrequency: 150, maxFrequency: 15000, channels: 1, hold: 20, bins: bins, bands: makeMonoBands(bins), peaks: makeMonoPeaks(bins), analyzer: dsp.NewFrequencyPowerCalculator[float64](44100, window.Blackman1024), } w.Extend(w) w.Resize(size) return w } func (w *Spectrum) String() string { channels := "mono" variant := "analyzer" if w.channels > 1 { channels = "stereo" } if w.waterfall { variant = "waterfall" } return channels + " " + variant } func (w *Spectrum) update(channels, bins int) { w.mu.Lock() w.channels = channels w.bins = bins if w.waterfall { w.bins = w.Bounds().Dx() / w.channels } else { w.bins = w.Bounds().Dx() / 4 / w.channels } switch channels { case 1: w.bands = makeMonoBands(w.bins) w.peaks = makeMonoPeaks(w.bins) case 2: w.bands = makeStereoBands(w.bins) w.peaks = makeStereoPeaks(w.bins) } w.mu.Unlock() w.changed.Store(true) } func (w *Spectrum) SetChannels(channels int) *Spectrum { if w.waterfall { w.update(channels, w.bounds.Load().Dx()/channels) } else { w.update(channels, w.bounds.Load().Dx()/channels/4) } return w } func (w *Spectrum) SetWaterfall(on bool) *Spectrum { w.mu.Lock() w.waterfall = on w.mu.Unlock() if w.waterfall { w.update(w.channels, w.bounds.Load().Dx()/w.channels) } else { w.update(w.channels, w.bounds.Load().Dx()/w.channels/4) } return w } func (w *Spectrum) Resize(size image.Point) app.Widget { w.update(w.channels, w.bins) return w.Base.Resize(size) } func (w *Spectrum) Refresh(ctx app.Context) bool { if !w.IsVisible() { return false } bounds := w.bounds.Load() if w.changed.CompareAndSwap(true, false) { if w.buffer == nil { w.buffer = app.BufferFor(ctx, bounds.Size()) app.ImageCopy(w.buffer, w.styles.Window.Background()) } if w.waterfall { w.refreshWaterfall(bounds) } else { w.refreshAnalyzer(bounds) } } app.ImageCopyBounds(ctx.Display(), w.buffer, bounds.Add(w.position.Load())) return true } func (w *Spectrum) refreshAnalyzer(bounds image.Rectangle) { app.ImageCopy(w.buffer, w.styles.Window.Background()) var ( width = bounds.Dx() height = bounds.Dy() ) switch w.channels { case 1: for i := range w.bands[0] { w.drawBarMono(i, height) } case 2: for i := range w.bands[0] { w.drawBarStereo(0, i, width, height) w.drawBarStereo(1, i, width, height) } } } func (w *Spectrum) drawBarMono(i, maxHeight int) { offset := i * 4 height := int(float64(maxHeight)*w.bands[0][i] + .5) for y := maxHeight - height; y < maxHeight; y++ { colorY := pixel.Gray4{Y: 0xf - uint8(float64(y)/float64(maxHeight)*12+.5)} for x := 0; x < 3; x++ { w.buffer.Set(offset+x, y, colorY) } } if w.hold <= 0 { return } height = maxHeight - int(float64(maxHeight)*calc.Max(w.peaks[0][i])+5) for x := 0; x < 3; x++ { w.buffer.Set(offset+x, height, pixel.On) } } func (w *Spectrum) drawBarStereo(channel, i, width, maxHeight int) { var offset int if channel == 0 { offset = (width / 2) - (i+1)*4 } else { offset = (width / 2) + i*4 } height := int(float64(maxHeight)*w.bands[channel][i] + .5) for y := maxHeight - height; y < maxHeight; y++ { colorY := pixel.Gray4{Y: 0xf - uint8(float64(y)/float64(maxHeight)*12+.5)} for x := 0; x < 3; x++ { w.buffer.Set(offset+x, y, colorY) } } if w.hold <= 0 { return } height = maxHeight - int(float64(maxHeight)*calc.Max(w.peaks[channel][i])+5) for x := 0; x < 3; x++ { w.buffer.Set(offset+x, height, pixel.On) } } func (w *Spectrum) refreshWaterfall(bounds image.Rectangle) { // Move waterfall up. switch buffer := w.buffer.(type) { case *pixel.Gray2Image: copy(buffer.Pix, buffer.Pix[buffer.Stride:]) case *pixel.Gray4Image: copy(buffer.Pix, buffer.Pix[buffer.Stride:]) case *pixel.CRGB15Image: copy(buffer.Pix, buffer.Pix[buffer.Stride:]) case *pixel.CRGB16Image: copy(buffer.Pix, buffer.Pix[buffer.Stride:]) case *image.RGBA: copy(buffer.Pix, buffer.Pix[buffer.Stride:]) default: for y := bounds.Dy() - 1; y > 0; y-- { draw.Draw(w.buffer, image.Rect(0, y, bounds.Dx(), y+1), w.buffer, image.Pt(0, y-1), draw.Src) } } height := bounds.Dy() if w.channels == 1 { for i, v := range w.bands[0] { w.drawMonoPoint(i, v, height) } } } func (w *Spectrum) drawMonoPoint(i int, level float64, maxHeight int) { intesity := uint8(16*level + .5) w.buffer.Set(i, maxHeight-1, pixel.Gray4{Y: intesity}) } func (w *Spectrum) StatusChanged(ctx app.Context, status *player.Status) { } func (w *Spectrum) ProcessBuffer(buffer audio.Buffer[float64]) { if w.changed.Load() { // Previous rendition hasn't finished, discard buffer. return } log := logrus.WithFields(logrus.Fields{ "widget": w.String(), "channels": buffer.Channels(), "samples": buffer.Samples(), }) log.Trace("process audio buffer") switch w.channels { case 1: // High pass filter on 50Hz to remove DC bias. w.samples[0] = filter.HighPass(w.samples[0], buffer[0], 50, 44100) // Calculate spectral densities. w.powers[0] = w.analyzer.Apply(w.powers[0], w.samples[0]) case 2: // High pass filter on 50Hz to remove DC bias. w.samples[0] = filter.HighPass(w.samples[0], buffer[0], 50, 44100) w.samples[1] = filter.HighPass(w.samples[1], buffer[1], 50, 44100) // Calculate spectral densities. w.powers[0] = w.analyzer.Apply(w.powers[0], w.samples[0]) w.powers[1] = w.analyzer.Apply(w.powers[1], w.samples[1]) } for c := 0; c < w.channels; c++ { // Adjust scale. for i := range w.powers[c] { w.powers[c][i].Magnitude /= 8 } // Compress powers to visible bars. var ( hpIndex = frequencyAbove(w.powers[c], w.minFrequency) lpIndex = frequencyBelow(w.powers[c], w.maxFrequency) ) summarizePowerBands(w.bands[c], w.powers[c][hpIndex:lpIndex], lpIndex-hpIndex) // Calculate peaks per band. if w.hold > 0 { for i, v := range w.bands[c] { w.peaks[c][i] = append(w.peaks[c][i], v) for len(w.peaks[c][i]) > w.hold { w.peaks[c][i] = w.peaks[c][i][1:] } } } } // We're done here w.changed.Store(true) } func toMono(dst []float64, src audio.Buffer[float64]) []float64 { if dst == nil { dst = make([]float64, len(src[0])) } for i := range dst { dst[i] = (src[0][i] + src[1][i]) / 2 } return dst } func summarizePowerBands(bands []float64, powers []dsp.FrequencyPower, samples int) { var ( frequencyBinSize = 44100 / float64(samples) frequencyMin = float64(powers[0].Frequency) frequencyMax = float64(powers[len(powers)-1].Frequency) frequencyScale = (frequencyMax - frequencyMin) / float64(len(bands)) ) for i := range bands { var ( frequencyStart = frequencyMin + float64(i)*frequencyScale frequencyEnd = frequencyMin + float64(i+1)*frequencyScale binStart = int((frequencyStart / frequencyBinSize) + .5) binEnd = int((frequencyEnd / frequencyBinSize) + .5) ) var total float64 var count int for bin := binStart; bin < binEnd; bin++ { total += math.Min(powers[bin].Magnitude, 1) count += 1 } if count == 0 { bands[i] = 0 } else { bands[i] = total / float64(count) } } } func frequencyAbove(powers []dsp.FrequencyPower, frequency int) (i int) { var power dsp.FrequencyPower for i, power = range powers { if power.Frequency >= frequency { return } } return } func frequencyBelow(powers []dsp.FrequencyPower, frequency int) (i int) { var power dsp.FrequencyPower for i = len(powers) - 1; i >= 0; i-- { if power.Frequency <= frequency { return } } return } func makeMonoBands(n int) [2][]float64 { return [2][]float64{ make([]float64, n), nil, } } func makeStereoBands(n int) [2][]float64 { return [2][]float64{ make([]float64, n), make([]float64, n), } } func makeMonoPeaks(n int) [2][][]float64 { return [2][][]float64{ make([][]float64, n), nil, } } func makeStereoPeaks(n int) [2][][]float64 { return [2][][]float64{ make([][]float64, n), make([][]float64, n), } } var ( _ app.Widget = (*Spectrum)(nil) _ app.StatusChangeHandler = (*Spectrum)(nil) _ app.BufferProcessor = (*Spectrum)(nil) )