-
Notifications
You must be signed in to change notification settings - Fork 1
/
output.go
61 lines (53 loc) · 1.16 KB
/
output.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
package main
import (
"encoding/binary"
"math"
"os"
"github.com/gordonklaus/portaudio"
)
func StreamOut(samples []float64) (*portaudio.Stream, error) {
if err := portaudio.Initialize(); err != nil {
return nil, err
}
defer portaudio.Terminate()
chunks := 8192
buf := make([]float32, chunks)
stream, err := portaudio.OpenDefaultStream(0, 1, sampleRate, chunks, &buf)
if err != nil {
return stream, err
}
if err := stream.Start(); err != nil {
return stream, err
}
defer stream.Stop()
for i, s := range samples {
idx := i % chunks
// buf = append(buf, float32(s)) // iiinteresting click tempo thing goin here...
if idx == 0 {
if err := stream.Write(); err != nil {
return stream, err
}
buf = make([]float32, chunks)
}
buf[idx] = float32(s)
}
return stream, nil
}
func WriteBin(filename string, samples []float64) error {
f, err := os.Create(filename)
if err != nil {
return err
}
for _, sample := range samples {
var buf [8]byte
binary.LittleEndian.PutUint32(
buf[:],
math.Float32bits(float32(sample)), // todo: we're 32 bits now?
)
_, err := f.Write(buf[:])
if err != nil {
return err
}
}
return nil
}