-
Notifications
You must be signed in to change notification settings - Fork 13
/
harmonics.go
31 lines (26 loc) · 898 Bytes
/
harmonics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package derived
import (
"math"
"github.com/bspaans/bleep/generators"
)
func NewHarmonicsGenerator(g func() generators.Generator, harmonics int) generators.Generator {
generators := []generators.Generator{g()}
for i := 2; i < harmonics+2; i++ {
generators = append(generators, NewTransposingGenerator(g(), float64(i), 1.0/float64(i*4)))
}
return NewCombinedGenerators(generators...)
}
func NewHarmonicGenerator(g generators.Generator, pitchFactor, gainFactor float64) generators.Generator {
result := NewWrappedGenerator(g)
result.SetPitchFunc = func(f float64) {
g.SetPitch(f * pitchFactor)
}
result.SetGainFunc = func(f float64) {
g.SetGain(f * gainFactor)
}
return result
}
func NewTransposingGenerator(g generators.Generator, semitones, gainFactor float64) generators.Generator {
factor := math.Pow(2, semitones/12)
return NewHarmonicGenerator(g, factor, gainFactor)
}