-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmago_test.go
More file actions
148 lines (129 loc) · 3.21 KB
/
mago_test.go
File metadata and controls
148 lines (129 loc) · 3.21 KB
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
package mago
import (
"errors"
"testing"
"time"
"unsafe"
"github.com/darkliquid/mago/internal/testlib"
)
func TestVersionAndNullBackendPlayback(t *testing.T) {
t.Parallel()
libPath := testlib.BuildRuntimeLibrary(t, ".")
lib, err := Open(WithLibraryPath(libPath))
if err != nil {
t.Fatalf("open library: %v", err)
}
defer func() {
if err := lib.Close(); err != nil {
t.Fatalf("close library: %v", err)
}
}()
version, err := lib.Version()
if err != nil {
t.Fatalf("version: %v", err)
}
if version != (Version{
Major: ExpectedMiniaudioVersionMajor,
Minor: ExpectedMiniaudioVersionMinor,
Revision: ExpectedMiniaudioVersionRevision,
}) {
t.Fatalf("unexpected version: %+v", version)
}
versionString, err := lib.VersionString()
if err != nil {
t.Fatalf("version string: %v", err)
}
if versionString != version.String() {
t.Fatalf("unexpected version string: %q", versionString)
}
ctx, err := lib.NewContext(BackendNull)
if err != nil {
t.Fatalf("new context: %v", err)
}
defer func() {
if err := ctx.Close(); err != nil {
t.Fatalf("close context: %v", err)
}
}()
playbackDevices, captureDevices, err := ctx.Devices()
if err != nil {
t.Fatalf("enumerate devices: %v", err)
}
if len(playbackDevices) == 0 {
t.Fatal("expected at least one playback device from the null backend")
}
_ = captureDevices
callbacks := make(chan uint32, 8)
notifications := make(chan NotificationType, 8)
config := DefaultPlaybackDeviceConfig()
config.DeviceIndex = 0
config.Channels = 1
config.SampleRate = 48000
config.PeriodSizeInFrames = 64
config.DataCallback = func(_ *Device, output unsafe.Pointer, _ unsafe.Pointer, frameCount uint32) {
if output != nil {
samples := unsafe.Slice((*float32)(output), int(frameCount))
for i := range samples {
samples[i] = 0
}
}
select {
case callbacks <- frameCount:
default:
}
}
config.NotificationCallback = func(_ *Device, notification NotificationType) {
select {
case notifications <- notification:
default:
}
}
device, err := ctx.NewPlaybackDevice(config)
if err != nil {
t.Fatalf("new playback device: %v", err)
}
defer func() {
if err := device.Close(); err != nil {
t.Fatalf("close device: %v", err)
}
}()
if err := device.Start(); err != nil {
t.Fatalf("start device: %v", err)
}
select {
case frames := <-callbacks:
if frames == 0 {
t.Fatal("expected a non-zero callback frame count")
}
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for audio callback")
}
if err := device.Stop(); err != nil {
t.Fatalf("stop device: %v", err)
}
select {
case got := <-notifications:
if got != NotificationStarted && got != NotificationStopped {
t.Fatalf("unexpected notification: %v", got)
}
default:
}
}
func TestValidateLoadedVersionRejectsMismatch(t *testing.T) {
t.Parallel()
err := validateLoadedVersion(
Version{
Major: ExpectedMiniaudioVersionMajor,
Minor: ExpectedMiniaudioVersionMinor + 1,
Revision: ExpectedMiniaudioVersionRevision,
},
"0.12.25",
)
if err == nil {
t.Fatal("expected a version mismatch error")
}
var mismatch *VersionMismatchError
if !errors.As(err, &mismatch) {
t.Fatalf("expected VersionMismatchError, got %T", err)
}
}