-
Notifications
You must be signed in to change notification settings - Fork 53
/
device_config.go
205 lines (174 loc) · 7.65 KB
/
device_config.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package malgo
// #include "malgo.h"
import "C"
import (
"unsafe"
)
// DeviceConfig type.
type DeviceConfig struct {
DeviceType DeviceType
SampleRate uint32
PeriodSizeInFrames uint32
PeriodSizeInMilliseconds uint32
Periods uint32
PerformanceProfile PerformanceProfile
NoPreSilencedOutputBuffer uint32
NoClip uint32
NoDisableDenormals uint32
NoFixedSizedCallback uint32
DataCallback *[0]byte
NotificationCallback *[0]byte
StopCallback *[0]byte
PUserData *byte
Resampling ResampleConfig
Playback SubConfig
Capture SubConfig
Wasapi WasapiDeviceConfig
Alsa AlsaDeviceConfig
Pulse PulseDeviceConfig
// TODO: Add support for coreaudio, opensl, aaudio
}
// DefaultDeviceConfig returns a default device config.
func DefaultDeviceConfig(deviceType DeviceType) DeviceConfig {
config := C.ma_device_config_init(C.ma_device_type(deviceType))
var deviceConfig DeviceConfig
deviceConfig.DeviceType = DeviceType(config.deviceType)
deviceConfig.SampleRate = uint32(config.sampleRate)
deviceConfig.PeriodSizeInFrames = uint32(config.periodSizeInFrames)
deviceConfig.PeriodSizeInMilliseconds = uint32(config.periodSizeInMilliseconds)
deviceConfig.Periods = uint32(config.periods)
deviceConfig.PerformanceProfile = PerformanceProfile(config.performanceProfile)
deviceConfig.NoPreSilencedOutputBuffer = uint32(config.noPreSilencedOutputBuffer)
deviceConfig.NoClip = uint32(config.noClip)
deviceConfig.NoDisableDenormals = uint32(config.noDisableDenormals)
deviceConfig.NoFixedSizedCallback = uint32(config.noFixedSizedCallback)
deviceConfig.DataCallback = config.dataCallback
deviceConfig.NotificationCallback = config.notificationCallback
deviceConfig.StopCallback = config.stopCallback
deviceConfig.PUserData = (*byte)(config.pUserData)
deviceConfig.Resampling.Algorithm = ResampleAlgorithm(config.resampling.algorithm)
deviceConfig.Resampling.Linear.LpfOrder = uint32(config.resampling.linear.lpfOrder)
deviceConfig.Playback.DeviceID = unsafe.Pointer(config.playback.pDeviceID)
deviceConfig.Playback.Format = FormatType(config.playback.format)
deviceConfig.Playback.Channels = uint32(config.playback.channels)
deviceConfig.Playback.ChannelMap = unsafe.Pointer(config.playback.pChannelMap)
deviceConfig.Playback.ShareMode = ShareMode(config.playback.shareMode)
deviceConfig.Capture.DeviceID = unsafe.Pointer(config.capture.pDeviceID)
deviceConfig.Capture.Format = FormatType(config.capture.format)
deviceConfig.Capture.Channels = uint32(config.capture.channels)
deviceConfig.Capture.ChannelMap = unsafe.Pointer(config.capture.pChannelMap)
deviceConfig.Capture.ShareMode = ShareMode(config.capture.shareMode)
deviceConfig.Wasapi.NoAutoConvertSRC = uint32(config.wasapi.noAutoConvertSRC)
deviceConfig.Wasapi.NoDefaultQualitySRC = uint32(config.wasapi.noDefaultQualitySRC)
deviceConfig.Wasapi.NoAutoStreamRouting = uint32(config.wasapi.noAutoStreamRouting)
deviceConfig.Wasapi.NoHardwareOffloading = uint32(config.wasapi.noHardwareOffloading)
deviceConfig.Alsa.NoMMap = uint32(config.alsa.noMMap)
deviceConfig.Alsa.NoAutoFormat = uint32(config.alsa.noAutoFormat)
deviceConfig.Alsa.NoAutoChannels = uint32(config.alsa.noAutoChannels)
deviceConfig.Alsa.NoAutoResample = uint32(config.alsa.noAutoResample)
if config.pulse.pStreamNameCapture != nil {
deviceConfig.Pulse.StreamNameCapture = C.GoString(config.pulse.pStreamNameCapture)
}
if config.pulse.pStreamNamePlayback != nil {
deviceConfig.Pulse.StreamNamePlayback = C.GoString(config.pulse.pStreamNamePlayback)
}
return deviceConfig
}
func (d *DeviceConfig) toC() (C.ma_device_config, func()) {
deviceConfig := C.ma_device_config_init(C.ma_device_type(d.DeviceType))
deviceConfig.sampleRate = C.uint(d.SampleRate)
deviceConfig.periodSizeInFrames = C.uint(d.PeriodSizeInFrames)
deviceConfig.periodSizeInMilliseconds = C.uint(d.PeriodSizeInMilliseconds)
deviceConfig.periods = C.uint(d.Periods)
deviceConfig.performanceProfile = C.ma_performance_profile(d.PerformanceProfile)
deviceConfig.noPreSilencedOutputBuffer = C.uchar(d.NoPreSilencedOutputBuffer)
deviceConfig.noClip = C.uchar(d.NoClip)
deviceConfig.noDisableDenormals = C.uchar(d.NoDisableDenormals)
deviceConfig.noFixedSizedCallback = C.uchar(d.NoFixedSizedCallback)
deviceConfig.dataCallback = d.DataCallback
deviceConfig.notificationCallback = d.NotificationCallback
deviceConfig.stopCallback = d.StopCallback
deviceConfig.pUserData = unsafe.Pointer(d.PUserData)
deviceConfig.resampling.algorithm = C.ma_resample_algorithm(d.Resampling.Algorithm)
deviceConfig.resampling.linear.lpfOrder = C.uint(d.Resampling.Linear.LpfOrder)
deviceConfig.playback.pDeviceID = (*C.ma_device_id)(d.Playback.DeviceID)
deviceConfig.playback.format = C.ma_format(d.Playback.Format)
deviceConfig.playback.channels = C.uint(d.Playback.Channels)
deviceConfig.playback.pChannelMap = (*C.ma_channel)(d.Playback.ChannelMap)
deviceConfig.playback.shareMode = C.ma_share_mode(d.Playback.ShareMode)
deviceConfig.capture.pDeviceID = (*C.ma_device_id)(d.Capture.DeviceID)
deviceConfig.capture.format = C.ma_format(d.Capture.Format)
deviceConfig.capture.channels = C.uint(d.Capture.Channels)
deviceConfig.capture.pChannelMap = (*C.ma_channel)(d.Capture.ChannelMap)
deviceConfig.capture.shareMode = C.ma_share_mode(d.Capture.ShareMode)
deviceConfig.wasapi.noAutoConvertSRC = C.uchar(d.Wasapi.NoAutoConvertSRC)
deviceConfig.wasapi.noDefaultQualitySRC = C.uchar(d.Wasapi.NoDefaultQualitySRC)
deviceConfig.wasapi.noAutoStreamRouting = C.uchar(d.Wasapi.NoAutoStreamRouting)
deviceConfig.wasapi.noHardwareOffloading = C.uchar(d.Wasapi.NoHardwareOffloading)
deviceConfig.alsa.noMMap = C.uint(d.Alsa.NoMMap)
deviceConfig.alsa.noAutoFormat = C.uint(d.Alsa.NoAutoFormat)
deviceConfig.alsa.noAutoChannels = C.uint(d.Alsa.NoAutoChannels)
deviceConfig.alsa.noAutoResample = C.uint(d.Alsa.NoAutoResample)
var releasers []func()
if d.Pulse.StreamNameCapture != "" {
streamNameCapturePtr := C.CString(d.Pulse.StreamNameCapture)
deviceConfig.pulse.pStreamNameCapture = streamNameCapturePtr
releasers = append(releasers, func() {
C.ma_free(unsafe.Pointer(streamNameCapturePtr), nil)
})
}
if d.Pulse.StreamNamePlayback != "" {
streamNamePlaybackPtr := C.CString(d.Pulse.StreamNamePlayback)
deviceConfig.pulse.pStreamNamePlayback = streamNamePlaybackPtr
releasers = append(releasers, func() {
C.ma_free(unsafe.Pointer(streamNamePlaybackPtr), nil)
})
}
return deviceConfig, func() {
for _, release := range releasers {
defer release()
}
}
}
// SubConfig type.
type SubConfig struct {
DeviceID unsafe.Pointer
Format FormatType
Channels uint32
ChannelMap unsafe.Pointer
ShareMode ShareMode
// Unexposed: channelMixMode, calculateLFEFromSpatialChannels
}
// WasapiDeviceConfig type.
type WasapiDeviceConfig struct {
NoAutoConvertSRC uint32
NoDefaultQualitySRC uint32
NoAutoStreamRouting uint32
NoHardwareOffloading uint32
}
// AlsaDeviceConfig type.
type AlsaDeviceConfig struct {
NoMMap uint32
NoAutoFormat uint32
NoAutoChannels uint32
NoAutoResample uint32
}
// PulseDeviceConfig type.
type PulseDeviceConfig struct {
StreamNamePlayback string
StreamNameCapture string
}
// ResampleConfig type.
type ResampleConfig struct {
Algorithm ResampleAlgorithm
Linear ResampleLinearConfig
// Unexposed: format, channels, sampleRateIn, sampleRateOut, pBackendVTable, pBackendUserData
}
// ResampleLinearConfig type.
type ResampleLinearConfig struct {
LpfOrder uint32
}
// ResampleSpeexConfig type.
type ResampleSpeexConfig struct {
Quality int
}