-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
77 lines (65 loc) · 1.88 KB
/
writer.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
package waveform
import (
"image"
"unsafe"
)
// NewWriter creates a Writer filled with unbounded waveforms. If you want
// to work with a different WaveFormer, construct the Writer yourself.
//
// The amount is given by channels, and the amount of samples per resulting
// pixel is given by samplesPerPair.
func NewWriter(channels, samplesPerPair int) *Writer {
ch := make([]WaveFormer, channels)
for i := range ch {
ch[i] = NewUnboundedWaveForm(samplesPerPair)
}
return &Writer{
WaveForms: ch,
}
}
// Writer is an io.Writer that wraps around one or more WaveFormer.
//
// See the Write method for how it interprets data that is written.
type Writer struct {
WaveForms []WaveFormer
}
// Image returns an image.Image that represents all WaveForms
// stacked vertically.
func (wf *Writer) Image(width, height int) image.Image {
var imgs []image.Image
for i := range wf.WaveForms {
former := wf.WaveForms[i]
i, ok := former.(image.Image)
if ok {
imgs = append(imgs, i)
}
type imager interface {
Image(width, height int) image.Image
}
ir, ok := former.(imager)
if ok {
imgs = append(imgs, ir.Image(width, height))
}
}
return NewStack(imgs...)
}
// Write adds len(p)/len(wf.WaveForms) amount of pairs to each WaveFormer
// in the WaveForms slice.
//
// Write works by interpreting p as audio data that has len(wf.WaveForms)
// amount of channels interleaved together.
//
// NOTE: Make sure to properly cutoff the end of your p on the end of samples,
// p should basically adhere to len(p) % (sampleSize * len(wf.WaveForms)) == 0
func (wf *Writer) Write(p []byte) (n int, err error) {
// assume float32 and interleaved channels for now
floats := *(*[]float32)(unsafe.Pointer(&p))
floats = floats[0 : len(p)/4 : len(p)/4]
for f := 0; f < len(floats); {
for i := range wf.WaveForms {
wf.WaveForms[i].AddPair(Pair{floats[f], floats[f]})
f++
}
}
return len(p), nil
}