-
Notifications
You must be signed in to change notification settings - Fork 53
/
device_info.go
81 lines (71 loc) · 2.02 KB
/
device_info.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
80
81
package malgo
// #include "malgo.h"
import "C"
import (
"encoding/hex"
"fmt"
"unsafe"
)
// DeviceID type.
type DeviceID [C.sizeof_ma_device_id]byte
// String returns the string representation of the identifier.
// It is the hexadecimal form of the underlying bytes of a minimum length of 2 digits, with trailing zeroes removed.
func (d DeviceID) String() string {
displayLen := len(d)
for (displayLen > 1) && (d[displayLen-1] == 0) {
displayLen--
}
return hex.EncodeToString(d[:displayLen])
}
func (d *DeviceID) Pointer() unsafe.Pointer {
return C.CBytes(d[:])
}
func (d *DeviceID) cptr() *C.ma_device_id {
return (*C.ma_device_id)(unsafe.Pointer(d))
}
// DeviceInfo type.
type DeviceInfo struct {
ID DeviceID
name [256]byte
IsDefault uint32
FormatCount uint32
Formats []DataFormat
}
// Name returns the name of the device.
func (d *DeviceInfo) Name() string {
// find the first null byte in d.name
var end int
for end = 0; end < len(d.name) && d.name[end] != 0; end++ {
}
return string(d.name[:end])
}
// String returns string.
func (d *DeviceInfo) String() string {
return fmt.Sprintf("{ID: [%v], Name: %s}", d.ID, d.Name())
}
func deviceInfoFromPointer(ptr unsafe.Pointer) DeviceInfo {
device := (*C.ma_device_info)(ptr)
var newDevice DeviceInfo
newDevice.ID = DeviceID(device.id)
for i := 0; i < len(device.name); i++ {
newDevice.name[i] = (byte)(device.name[i])
}
newDevice.IsDefault = uint32(device.isDefault)
newDevice.FormatCount = uint32(device.nativeDataFormatCount)
newDevice.Formats = make([]DataFormat, newDevice.FormatCount)
for i := 0; i < int(newDevice.FormatCount); i++ {
newDevice.Formats[i] = DataFormat{
Format: FormatType(device.nativeDataFormats[i].format),
Channels: uint32(device.nativeDataFormats[i].channels),
SampleRate: uint32(device.nativeDataFormats[i].sampleRate),
Flags: uint32(device.nativeDataFormats[i].flags),
}
}
return newDevice
}
type DataFormat struct {
Format FormatType
Channels uint32
SampleRate uint32
Flags uint32
}