forked from synthia-synth/synthia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.go
39 lines (36 loc) · 736 Bytes
/
play.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
package main
import (
"github.com/Orion90/portaudio"
)
const playBufferSize = 8192
func playTune(tune []int32, sampleRate float64) error {
err := portaudio.Initialize()
if err != nil {
return err
}
defer portaudio.Terminate()
buffer := make([]int32, playBufferSize)
stream, err := portaudio.OpenDefaultStream(0, 1, sampleRate, len(buffer), &buffer)
if err != nil {
return err
}
defer stream.Close()
err = stream.Start()
if err != nil {
return err
}
defer stream.Stop()
for i := 0; i < len(tune); i += len(buffer) {
end := i + playBufferSize
if end > len(tune) {
copy(buffer, tune[i:])
} else {
copy(buffer, tune[i:end])
}
err = stream.Write()
if err != nil {
return err
}
}
return nil
}