/
main.go
315 lines (281 loc) · 5.98 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"image"
"image/png"
"math"
"os"
"runtime"
"sync"
"time"
)
const FPS = 60
type WAVfile struct {
filepath string
channels uint16
samplerate uint32
bps uint16
samples uint32
reader *bufio.Reader
}
type lrLevel struct {
left int16
right int16
}
func (me *WAVfile) ReadSample() (*lrLevel, error) {
var ret lrLevel
buf := make([]byte, me.bps/8)
n, err := me.reader.Read(buf)
if err != nil {
return nil, err
}
if uint16(n) != me.bps/8 {
return nil, errors.New("malformed WAV file")
}
if me.bps == 8 {
ret.left = int16(buf[0])
} else if me.bps == 16 {
ret.left = int16(binary.LittleEndian.Uint16(buf))
} else {
return nil, errors.New("WAV data not supported")
}
if me.channels == 1 {
ret.right = ret.left
return &ret, nil
} else if me.channels == 2 {
n, err := me.reader.Read(buf)
if err != nil {
return nil, err
}
if uint16(n) != me.bps/8 {
return nil, errors.New("malformed WAV file")
}
if me.bps == 8 {
ret.right = int16(buf[0])
} else if me.bps == 16 {
ret.right = int16(binary.LittleEndian.Uint16(buf))
} else {
return nil, errors.New("WAV data not supported")
}
return &ret, nil
} else {
return nil, errors.New("WAV data not supported")
}
}
func NewWAVfile(fp string) (*WAVfile, error) {
var me WAVfile
var n int
buf2 := make([]byte, 2)
buf4 := make([]byte, 4)
readCheck := func(number int) (err error) {
if number == 2 {
n, err = me.reader.Read(buf2)
if err != nil {
return err
}
if n != 2 {
return errors.New("malformed WAV file")
}
} else {
n, err = me.reader.Read(buf4)
if err != nil {
return err
}
if n != 4 {
return errors.New("malformed WAV file")
}
}
return nil
}
me.filepath = fp
f, err := os.Open("test.wav")
if err != nil {
return &me, err
}
me.reader = bufio.NewReader(f)
if err := readCheck(4); err != nil {
return &me, err
}
if string(buf4) != "RIFF" {
return &me, errors.New("malformed WAV file")
}
if err := readCheck(4); err != nil {
return &me, err
}
if err := readCheck(4); err != nil {
return &me, err
}
if string(buf4) != "WAVE" {
return &me, errors.New("malformed WAV file")
}
if err := readCheck(4); err != nil {
return &me, err
}
if string(buf4) != "fmt " {
return &me, errors.New("malformed WAV file")
}
if err := readCheck(4); err != nil {
return &me, err
}
if binary.LittleEndian.Uint32(buf4) != 16 {
return &me, errors.New("malformed WAV file")
}
if err := readCheck(2); err != nil {
return &me, err
}
if binary.LittleEndian.Uint16(buf2) != 1 {
return &me, errors.New("this WAV format is not supported")
}
if err := readCheck(2); err != nil {
return &me, err
}
me.channels = binary.LittleEndian.Uint16(buf2)
if err := readCheck(4); err != nil {
return &me, err
}
me.samplerate = binary.LittleEndian.Uint32(buf4)
if err := readCheck(4); err != nil {
return &me, err
}
if err := readCheck(2); err != nil {
return &me, err
}
if err := readCheck(2); err != nil {
return &me, err
}
me.bps = binary.LittleEndian.Uint16(buf2)
if err := readCheck(4); err != nil {
return &me, err
}
if string(buf4) != "data" {
return &me, errors.New("malformed WAV file")
}
if err := readCheck(4); err != nil {
return &me, err
}
me.samples = binary.LittleEndian.Uint32(buf4) / uint32(me.channels) / uint32(me.bps) * 8
return &me, nil
}
func lineDraw(img *image.RGBA, x1, y1, x2, y2 int, br uint8) {
dy := y2 - y1
dx := x2 - x1
f64dx := float64(dx)
tmp := f64dx / float64(dy<<1)
if tmp < 0 {
tmp *= -1
}
subtr := 0.5 - tmp
if subtr < 0 {
subtr *= -1
}
stridex := 0.5 + tmp - subtr
stridey := stridex * float64(dy) / f64dx
tmp2 := dx >> 63
dx = (dx ^ tmp2) - tmp2
tmp2 = dy >> 63
dy = (dy ^ tmp2) - tmp2
x := float64(x1+x2-dx) / 2
y := float64(y1+y2-dy) / 2
if stridey < 0 {
y = float64(y1+y2+dy) / 2
}
boundx := (x1 + x2 + dx) / 2
boundy := (y1 + y2 + dy) / 2
intx := int(x)
inty := int(y)
if dx == 0 {
stridex = 0
stridey = 1
}
if dy == 0 {
stridex = 1
stridey = 0
}
for intx <= boundx && inty <= boundy {
start := (inty * img.Stride) + (intx * 4)
brcp := br
if start > 4194303 {
continue
}
curbr := img.Pix[start]
if curbr == 0 {
img.Pix[start+3] = 255
}
if 255-curbr <= brcp {
img.Pix[start] = 255
img.Pix[start+1] = 255
img.Pix[start+2] = 255
} else {
brcp += curbr
img.Pix[start] = brcp
img.Pix[start+1] = brcp
img.Pix[start+2] = brcp
}
y += stridey
x += stridex
intx = int(x + 0.5)
inty = int(y + 0.5)
}
}
func plotter(pxs chan *lrLevel, frameid uint32, wg *sync.WaitGroup) {
defer wg.Done()
img := image.NewRGBA(image.Rect(0, 0, 1024, 1024))
var x, y, lastx, lasty int
px := <-pxs
x = int(px.left>>6) + 512
y = int(px.right>>6) + 512
for px = range pxs {
lastx = x
lasty = y
x = int(px.left>>6) + 512
y = 512 - int(px.right>>6)
distance := float64((lastx - x) * (lastx - x))
distance += float64((lasty - y) * (lasty - y))
bri := 255 - math.Pow(distance, 0.6)
if bri < 0 {
bri = 0
}
_ = lineDraw //(img, lastx, lasty, x, y, uint8(bri))
}
f, err := os.Create(fmt.Sprintf("out/img%04v.png", frameid))
if err != nil {
panic(err)
}
defer f.Close()
if err := png.Encode(f, img); err != nil {
panic(err)
}
}
func main() {
wf, err := NewWAVfile("test.wav")
if err != nil {
panic(err)
}
samplesPerFrame := wf.samplerate / FPS
var wg sync.WaitGroup
process := make(chan *lrLevel, samplesPerFrame)
wg.Add(1)
go plotter(process, 0, &wg)
for sample := uint32(0); sample < wf.samples; sample++ {
levels, err := wf.ReadSample()
if err != nil {
panic(err)
}
process <- levels
if sample%samplesPerFrame == 0 {
fmt.Printf("%f%%\r", float32(sample*100)/float32(wf.samples))
close(process)
process = make(chan *lrLevel, samplesPerFrame)
for runtime.NumGoroutine() > 32 {
time.Sleep(10 * time.Millisecond)
}
wg.Add(1)
go plotter(process, sample/samplesPerFrame, &wg)
}
}
close(process)
wg.Wait()
}