-
Notifications
You must be signed in to change notification settings - Fork 2
/
rtaudio_test.go
79 lines (71 loc) · 1.45 KB
/
rtaudio_test.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
package rtaudio
import (
"log"
"math"
"testing"
"time"
)
func ExampleCompiledAPI() {
log.Println("RtAudio version: ", Version())
for _, api := range CompiledAPI() {
log.Println("Compiled API: ", api)
}
}
func ExampleRtAudio_Devices() {
audio, err := Create(APIUnspecified)
if err != nil {
log.Fatal(err)
}
defer audio.Destroy()
devices, err := audio.Devices()
if err != nil {
log.Fatal(err)
}
for _, d := range devices {
log.Printf("Audio device: %#v\n", d)
}
}
func ExampleRtAudio_Open() {
const (
sampleRate = 48000
bufSz = 512
freq = 440.0
)
phase := 0.0
audio, err := Create(APIUnspecified)
if err != nil {
log.Fatal(err)
}
defer audio.Destroy()
params := StreamParams{
DeviceID: uint(audio.DefaultOutputDevice()),
NumChannels: 2,
FirstChannel: 0,
}
options := StreamOptions{
Flags: FlagsAlsaUseDefault,
}
cb := func(out, in Buffer, dur time.Duration, status StreamStatus) int {
samples := out.Float32()
for i := 0; i < len(samples)/2; i++ {
sample := float32(math.Sin(2 * math.Pi * phase))
phase += freq / sampleRate
samples[i*2] = sample
samples[i*2+1] = sample
}
return 0
}
err = audio.Open(¶ms, nil, FormatFloat32, sampleRate, bufSz, cb, &options)
if err != nil {
log.Fatal(err)
}
defer audio.Close()
audio.Start()
defer audio.Stop()
time.Sleep(3 * time.Second)
}
func TestAudio(*testing.T) {
ExampleCompiledAPI()
ExampleRtAudio_Devices()
ExampleRtAudio_Open()
}