2026-03-26 12:03:06 +01:00
|
|
|
package synth
|
|
|
|
|
|
|
|
|
|
import "github.com/netsynth/netsynth/classify"
|
|
|
|
|
|
2026-03-26 17:37:44 +01:00
|
|
|
// OscillatorBank holds synthesis layers, one per TrafficClass in the injected config map.
|
2026-03-26 12:03:06 +01:00
|
|
|
// It consumes WindowSnapshot data and renders stereo PCM frames.
|
|
|
|
|
type OscillatorBank struct {
|
2026-03-26 17:37:44 +01:00
|
|
|
layers map[classify.TrafficClass]*Layer
|
|
|
|
|
tau float64
|
|
|
|
|
gainPerLayer float64
|
2026-03-26 12:03:06 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 17:37:44 +01:00
|
|
|
// NewBank creates an OscillatorBank with one Layer per entry in cfgs.
|
2026-03-26 12:03:06 +01:00
|
|
|
// tau is the EMA time constant in seconds (use 1.0 for D-07's "1-2 second" feel).
|
2026-03-26 17:37:44 +01:00
|
|
|
// gainPerLayer is computed dynamically as 1/len(cfgs) so that all layers at max
|
|
|
|
|
// amplitude sum to exactly 1.0 (no clipping), regardless of how many classes are active.
|
|
|
|
|
func NewBank(tau float64, cfgs map[classify.TrafficClass]FreqConfig) *OscillatorBank {
|
2026-03-26 12:03:06 +01:00
|
|
|
b := &OscillatorBank{
|
2026-03-26 17:37:44 +01:00
|
|
|
layers: make(map[classify.TrafficClass]*Layer, len(cfgs)),
|
|
|
|
|
tau: tau,
|
|
|
|
|
gainPerLayer: 1.0 / float64(len(cfgs)),
|
2026-03-26 12:03:06 +01:00
|
|
|
}
|
2026-03-26 17:37:44 +01:00
|
|
|
for class, cfg := range cfgs {
|
2026-03-26 12:03:06 +01:00
|
|
|
b.layers[class] = NewLayer(cfg, SampleRate, tau)
|
|
|
|
|
}
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RenderWindow updates amplitude targets from snap, then renders SamplesPerWindow
|
|
|
|
|
// stereo frames. Each frame is [2]float64{left, right} with values in [-1, 1].
|
2026-03-26 17:37:44 +01:00
|
|
|
// Each layer gets 1/N of the total gain where N is the number of layers.
|
2026-03-26 12:03:06 +01:00
|
|
|
func (b *OscillatorBank) RenderWindow(snap classify.WindowSnapshot) [][2]float64 {
|
|
|
|
|
// Find max count for normalization
|
|
|
|
|
var maxCount int64
|
|
|
|
|
for _, count := range snap.Counts {
|
|
|
|
|
if count > maxCount {
|
|
|
|
|
maxCount = count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update target amplitudes for all layers
|
2026-03-26 17:37:44 +01:00
|
|
|
for class, layer := range b.layers {
|
2026-03-26 12:03:06 +01:00
|
|
|
count := snap.Counts[class]
|
2026-03-26 17:37:44 +01:00
|
|
|
layer.UpdateTarget(count, maxCount)
|
2026-03-26 12:03:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Render frames
|
|
|
|
|
frames := make([][2]float64, SamplesPerWindow)
|
|
|
|
|
for i := range frames {
|
|
|
|
|
var sumL, sumR float64
|
2026-03-26 17:37:44 +01:00
|
|
|
for _, layer := range b.layers {
|
2026-03-26 12:03:06 +01:00
|
|
|
sample := layer.AdvanceSample()
|
|
|
|
|
gainL, gainR := PanGains(layer.Config.Pan)
|
2026-03-26 17:37:44 +01:00
|
|
|
sumL += sample * b.gainPerLayer * gainL
|
|
|
|
|
sumR += sample * b.gainPerLayer * gainR
|
2026-03-26 12:03:06 +01:00
|
|
|
}
|
|
|
|
|
frames[i] = [2]float64{sumL, sumR}
|
|
|
|
|
}
|
|
|
|
|
return frames
|
|
|
|
|
}
|