-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_supported.go
More file actions
118 lines (100 loc) · 2.69 KB
/
context_supported.go
File metadata and controls
118 lines (100 loc) · 2.69 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
//go:build darwin || freebsd || linux || netbsd || windows
package mago
import (
"bytes"
"fmt"
"runtime"
"strconv"
"unsafe"
)
type Context struct {
lib *Library
handle *contextHandle
}
func (lib *Library) NewContext(backends ...Backend) (*Context, error) {
if err := lib.ensureOpen(); err != nil {
return nil, err
}
var handle *contextHandle
var result Result
if len(backends) == 0 {
result = lib.bindings.magoContextInitDefault(&handle)
} else {
backendCount, err := intToUint32(len(backends))
if err != nil {
return nil, fmt.Errorf("mago: %w", err)
}
result = lib.bindings.magoContextInitWithBackends(&backends[0], backendCount, &handle)
runtime.KeepAlive(backends)
}
if result != Success {
return nil, lib.resultError("ma_context_init", result)
}
return &Context{lib: lib, handle: handle}, nil
}
func (ctx *Context) Close() error {
if ctx == nil || ctx.handle == nil {
return nil
}
if err := ctx.lib.ensureOpen(); err != nil {
return err
}
ctx.lib.bindings.magoContextUninitFree(ctx.handle)
ctx.handle = nil
return nil
}
func (ctx *Context) Devices() ([]DeviceInfo, []DeviceInfo, error) {
if ctx == nil || ctx.handle == nil {
return nil, nil, nil
}
if err := ctx.lib.ensureOpen(); err != nil {
return nil, nil, err
}
var playbackNative *deviceInfoNative
var playbackCount uint32
var captureNative *deviceInfoNative
var captureCount uint32
result := ctx.lib.bindings.magoContextGetDevices(ctx.handle, &playbackNative, &playbackCount, &captureNative, &captureCount)
if result != Success {
return nil, nil, ctx.lib.resultError("ma_context_get_devices", result)
}
defer func() {
if playbackNative != nil {
ctx.lib.bindings.magoContextFreeDeviceInfos(playbackNative)
}
if captureNative != nil {
ctx.lib.bindings.magoContextFreeDeviceInfos(captureNative)
}
}()
playback := copyDeviceInfos(playbackNative, playbackCount)
capture := copyDeviceInfos(captureNative, captureCount)
return playback, capture, nil
}
func copyDeviceInfos(native *deviceInfoNative, count uint32) []DeviceInfo {
if native == nil || count == 0 {
return nil
}
items := unsafe.Slice(native, count)
out := make([]DeviceInfo, 0, count)
for _, item := range items {
nameBytes := item.Name[:]
if idx := bytes.IndexByte(nameBytes, 0); idx >= 0 {
nameBytes = nameBytes[:idx]
}
out = append(out, DeviceInfo{
Name: string(nameBytes),
IsDefault: item.IsDefault != 0,
})
}
return out
}
func intToUint32(v int) (uint32, error) {
if v < 0 {
return 0, fmt.Errorf("value %d exceeds uint32 range", v)
}
var out uint32
if _, err := fmt.Sscan(strconv.Itoa(v), &out); err != nil {
return 0, fmt.Errorf("value %d exceeds uint32 range", v)
}
return out, nil
}