-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathspectrogramshift.go
162 lines (138 loc) · 3.95 KB
/
spectrogramshift.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// go run spectrogramshift.go --shift=<semitones> <file>
package main
import (
"flag"
"fmt"
"math"
"math/cmplx"
"runtime"
"github.com/padster/go-sound/cq"
f "github.com/padster/go-sound/file"
"github.com/padster/go-sound/output"
s "github.com/padster/go-sound/sounds"
)
// Takes a spectrogram, applies a shift, inverts back and plays the result.
func main() {
// Needs to be at least 2 when doing openGL + sound output at the same time.
runtime.GOMAXPROCS(3)
sampleRate := s.CyclesPerSecond
octaves := flag.Int("octaves", 7, "Range in octaves")
minFreq := flag.Float64("minFreq", 55.0, "Minimum frequency")
semitones := flag.Int("shift", 0, "Semitones to shift")
bpo := flag.Int("bpo", 24, "Buckets per octave")
flag.Parse()
remainingArgs := flag.Args()
argCount := len(remainingArgs)
if argCount < 1 || argCount > 2 {
panic("Required: <input> [output] argument")
}
inputFile := remainingArgs[0]
// Note: scale the output frequency by this to change pitch dilation into time dilation
// shift := math.Pow(2.0, float64(-*semitones) / 12.0)
paramsIn := cq.NewCQParams(sampleRate, *octaves, *minFreq, *bpo)
paramsOut := cq.NewCQParams(sampleRate, *octaves, *minFreq, *bpo)
spectrogram := cq.NewSpectrogram(paramsIn)
cqInverse := cq.NewCQInverse(paramsOut)
inputSound := f.Read(inputFile)
inputSound.Start()
defer inputSound.Stop()
fmt.Printf("Running...\n")
columns := spectrogram.ProcessChannel(inputSound.GetSamples())
outColumns := shiftSpectrogram(
*semitones*(*bpo/12), 0, flipSpectrogram(columns, *octaves, *bpo), *octaves, *bpo)
soundChannel := cqInverse.ProcessChannel(outColumns)
resultSound := s.WrapChannelAsSound(soundChannel)
// HACK: Amplify for now.
resultSound = s.MultiplyWithClip(resultSound, 3.0)
if argCount == 2 {
f.Write(resultSound, remainingArgs[1])
} else {
output.Play(resultSound)
}
}
func shiftSpectrogram(binOffset int, sampleOffset int, samples <-chan []complex128, octaves int, bpo int) <-chan []complex128 {
result := make(chan []complex128)
go func() {
ignoreSamples := sampleOffset
at := 0
for s := range samples {
if ignoreSamples > 0 {
ignoreSamples--
continue
}
octaveCount := octaves
if at > 0 {
octaveCount = numOctaves(at)
if octaveCount == octaves {
at = 0
}
}
at++
toFill := octaveCount * bpo
column := make([]complex128, toFill, toFill)
// NOTE: Zero-padded, not the best...
if binOffset >= 0 {
copy(column, s[binOffset:])
} else {
copy(column[-binOffset:], s)
}
result <- column
}
close(result)
}()
return result
}
func numOctaves(at int) int {
result := 1
for at%2 == 0 {
at /= 2
result++
}
return result
}
func clone(values []complex128) []complex128 {
result := make([]complex128, len(values), len(values))
for i, v := range values {
result[i] = v
}
return result
}
func flipSpectrogram(samples <-chan []complex128, octaves int, bpo int) <-chan []complex128 {
result := make(chan []complex128)
go func() {
var phaseAt []float64 = nil
for s := range samples {
if phaseAt == nil {
phaseAt = make([]float64, len(s), len(s))
}
for i, v := range s {
vp := cmplx.Phase(v)
vp = makeCloser(phaseAt[i], vp)
phaseAt[i] = vp
}
newSample := make([]complex128, len(s), len(s))
for i := 0; i < len(s); i++ {
newSample[i] = s[i]
}
for i := 0; i < len(s); i++ {
other := len(s) - 1 - i
pFactor := float64(octaves) - float64(2*i+1)/float64(bpo)
phase := phaseAt[other] / math.Pow(2.0, pFactor)
newSample[i] = cmplx.Rect(cmplx.Abs(s[other]), phase)
}
result <- newSample
}
close(result)
}()
return result
}
// Return the closest number X to toShift, such that X mod Tau == modTwoPi
func makeCloser(toShift, modTau float64) float64 {
if math.IsNaN(modTau) {
modTau = 0.0
}
// Minimize |toShift - (modTau + tau * cyclesToAdd)|
// toShift - modTau - tau * CTA = 0
cyclesToAdd := (toShift - modTau) / cq.TAU
return modTau + float64(cq.Round(cyclesToAdd))*cq.TAU
}