-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
209 lines (182 loc) · 5.11 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"flag"
"fmt"
"github.com/skelterjohn/go.matrix" // daa59528eefd43623a4c8e36373a86f9eef870a2
"github.com/youpy/go-wav"
"io"
"io/ioutil"
"log"
"math"
"os"
"strconv"
"strings"
)
var EncodeBlockSize int = 16
var PolySize int = 5
func main() {
// I have no idea what I am doing
Encoding := flag.Bool("encode", false, "true if you want to encode")
Decoding := flag.Bool("decode", false, "true if you want to decode")
Filename := flag.String("in", "", "What file the output sould be read from")
OutputFile := flag.String("out", "", "What file the output sould be written to")
flag.Parse()
if *Encoding && *Decoding {
log.Fatal("You can't do both!")
}
if *Filename == "" || *OutputFile == "" {
log.Fatal("Please give both input and output.")
}
fmt.Println("Bop")
if len(os.Args) <= 1 {
log.Fatal("You need to tell me what file to encode.")
}
if *Encoding {
Encode(*Filename, *OutputFile)
} else {
Decode(*Filename, *OutputFile)
}
}
func Encode(filename, OutputFile string) {
log.Println("Encoding file...")
file, _ := os.Open(filename)
outputpac, _ := os.OpenFile(OutputFile, os.O_CREATE, 600)
reader := wav.NewReader(file)
BlockesProcessed := 0
// End of settings for output
var SampleBlock []float64
SampleBlock = make([]float64, EncodeBlockSize)
for {
samplez := make([]wav.Sample, 0)
samples, err := reader.ReadSamples()
// Samples will return (usally) 2048 samples in a single sitting
log.Println(BlockesProcessed)
if err == io.EOF {
break
}
BlockesProcessed++
XBlock := make([]float64, EncodeBlockSize)
for k, _ := range XBlock {
XBlock[k] = float64(k)
}
var SamplePointer int = 0 // max is EncodeBlockSize
for _, sample := range samples {
sam := wav.Sample{}
L := reader.IntValue(sample, 0)
R := reader.IntValue(sample, 1)
MonoVal := (L + R) / 2
// Now we add it to the staging array
SampleBlock[SamplePointer] = float64(MonoVal)
SamplePointer++
if SamplePointer == EncodeBlockSize {
out := GetPolyResults(XBlock, SampleBlock)
Line := fmt.Sprintf("%f,%f,%f,%f,%f\n", out[0], out[1], out[2], out[3], out[4])
outputpac.Write([]byte(Line))
SamplePointer = 0
}
sam.Values[0] = L
sam.Values[0] = R
samplez = append(samplez, sam)
}
// fmt.Println(len(samplez), len(samples))
}
}
func Decode(filename, OutputFile string) {
log.Println("Decoding file")
b, e := ioutil.ReadFile(filename)
if e != nil {
log.Fatal("cannot read input file.")
}
outputwav, e := os.OpenFile(OutputFile, os.O_CREATE, 600)
if e != nil {
log.Fatal("cannot open output file")
}
var numSamples uint32 = 999999
var numChannels uint16 = 2
var sampleRate uint32 = 44100
var bitsPerSample uint16 = 16
writer := wav.NewWriter(outputwav, numSamples, numChannels, sampleRate, bitsPerSample)
lines := strings.Split(string(b), "\n")
samplez := make([]wav.Sample, 0)
for ln, line := range lines {
var prams []float64
prams = make([]float64, PolySize)
bits := strings.Split(line, ",")
if len(bits) != PolySize {
if len(bits) == 1 {
break
}
log.Fatal("Invalid PAC file. ", len(bits), "on line", ln)
}
for k, v := range bits {
prams[k], e = strconv.ParseFloat(v, 64)
if e != nil {
log.Fatal("unable to decode part of PAC file")
}
}
out := GetSamplesFromPoly(prams)
for _, v := range out {
sam := wav.Sample{}
sam.Values[0] = v
sam.Values[1] = v
samplez = append(samplez, sam)
}
}
writer.WriteSamples(samplez)
}
var LastBlockSample int = 0 // temp var to smooth stuff out
func GetSamplesFromPoly(prams []float64) (out []int) {
out = make([]int, EncodeBlockSize)
for k, _ := range out {
out[k] = int(
(prams[4] * math.Pow(float64(k), 4)) +
(prams[3] * math.Pow(float64(k), 3)) +
(prams[2] * math.Pow(float64(k), 2)) +
(prams[1] * float64(k)) + prams[0])
if k == 0 {
out[k] = (out[k] + LastBlockSample) / 2
}
}
LastBlockSample = out[EncodeBlockSize-1]
return out
}
func GetPolyResults(xGiven []float64, yGiven []float64) []float64 {
m := len(yGiven)
if m != len(xGiven) {
return make([]float64, PolySize) // Send it back, There is nothing sane here.
}
if m < 5 {
// Prevent the processing of really small datasets, This is becauase there
// appears to be a bug in the libary that will trigger a crash in the go.matrix
// if some (small) amount of values are entered. I don't know why this happens
// (Otherwise I would have fixed it) but the URL for the github issue is:
// https://github.com/skelterjohn/go.matrix/issues/11
return make([]float64, PolySize) // Send it back, There is nothing sane here.
}
n := PolySize + 1
y := matrix.MakeDenseMatrix(yGiven, m, 1)
x := matrix.Zeros(m, n)
for i := 0; i < m; i++ {
ip := float64(1)
for j := 0; j < n; j++ {
x.Set(i, j, ip)
ip *= xGiven[i]
}
}
q, r := x.QR()
qty, err := q.Transpose().Times(y)
if err != nil {
log.Println(err)
return []float64{0, 0, 0, 0, 0} // Send it back, There is nothing sane here.
}
c := make([]float64, n)
for i := n - 1; i >= 0; i-- {
c[i] = qty.Get(i, 0)
for j := i + 1; j < n; j++ {
c[i] -= c[j] * r.Get(i, j)
}
c[i] /= r.Get(i, i)
}
// log.Println(c)
return c
}