forked from DylanMeeus/GoAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guardtable.go
81 lines (70 loc) · 1.83 KB
/
guardtable.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package synthesizer
import (
"errors"
"math"
)
// Gtable is a Guard-table for oscillator lookup
type Gtable struct {
data []float64
}
// Len returns the length of the data segment without the guard point
func Len(g *Gtable) int {
return len(g.data) - 1
}
func NewGtable(data []float64) *Gtable {
return &Gtable{data}
}
// NewSineTable returns a lookup table populated for sine-wave generation.
func NewSineTable(length int) *Gtable {
g := &Gtable{}
if length == 0 {
return g
}
g.data = make([]float64, length+1) // one extra for the guard point.
step := tau / float64(Len(g))
for i := 0; i < Len(g); i++ {
g.data[i] = math.Sin(step * float64(i))
}
// store a guard point
g.data[len(g.data)-1] = g.data[0]
return g
}
// NewTriangleTable generates a lookup table for a triangle wave
// of the specified length and with the requested number of harmonics.
func NewTriangleTable(length int, nharmonics int) (*Gtable, error) {
if length == 0 || nharmonics == 0 || nharmonics >= length/2 {
return nil, errors.New("Invalid arguments for creation of Triangle Table")
}
g := &Gtable{}
g.data = make([]float64, length+1)
step := tau / float64(length)
// generate triangle waveform
harmonic := 1.0
for i := 0; i < nharmonics; i++ {
amp := 1.0 / (harmonic * harmonic)
for j := 0; j < length; j++ {
g.data[j] += amp * math.Cos(step*harmonic*float64(j))
}
harmonic += 2 // triangle wave has only odd harmonics
}
// normalize the values to be in the [-1;1] range
g.data = normalize(g.data)
return g, nil
}
// normalize the functions to the range -1, 1
func normalize(xs []float64) []float64 {
length := len(xs)
maxamp := 0.0
for i := 0; i < length; i++ {
amp := math.Abs(xs[i])
if amp > maxamp {
maxamp = amp
}
}
maxamp = 1.0 / maxamp
for i := 0; i < length; i++ {
xs[i] *= maxamp
}
xs[len(xs)-1] = xs[0]
return xs
}