()
| 193 | } |
| 194 | |
| 195 | func (c *wasapiContext) startOnCOMThread() (ferr error) { |
| 196 | if c.enumerator == nil { |
| 197 | e, err := _CoCreateInstance(&uuidMMDeviceEnumerator, nil, uint32(_CLSCTX_ALL), &uuidIMMDeviceEnumerator) |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
| 201 | c.enumerator = (*_IMMDeviceEnumerator)(e) |
| 202 | defer func() { |
| 203 | if ferr != nil { |
| 204 | c.enumerator.Release() |
| 205 | c.enumerator = nil |
| 206 | } |
| 207 | }() |
| 208 | } |
| 209 | |
| 210 | device, err := c.enumerator.GetDefaultAudioEndPoint(eRender, eConsole) |
| 211 | if err != nil { |
| 212 | if errors.Is(err, _E_NOTFOUND) { |
| 213 | return errDeviceNotFound |
| 214 | } |
| 215 | return err |
| 216 | } |
| 217 | defer device.Release() |
| 218 | |
| 219 | id, err := device.GetId() |
| 220 | if err != nil { |
| 221 | return err |
| 222 | } |
| 223 | c.currentDeviceID = id |
| 224 | |
| 225 | if c.client != nil { |
| 226 | c.client.Release() |
| 227 | c.client = nil |
| 228 | } |
| 229 | |
| 230 | client, err := device.Activate(&uuidIAudioClient2, uint32(_CLSCTX_ALL), nil) |
| 231 | if err != nil { |
| 232 | return err |
| 233 | } |
| 234 | c.client = (*_IAudioClient2)(client) |
| 235 | |
| 236 | if err := c.client.SetClientProperties(&_AudioClientProperties{ |
| 237 | cbSize: uint32(unsafe.Sizeof(_AudioClientProperties{})), |
| 238 | bIsOffload: 0, // false |
| 239 | eCategory: _AudioCategory_Other, // In the example, AudioCategory_ForegroundOnlyMedia was used, but this value is deprecated. |
| 240 | }); err != nil { |
| 241 | return err |
| 242 | } |
| 243 | |
| 244 | // Check the format is supported by WASAPI. |
| 245 | // Stereo with 48000 [Hz] is likely supported, but mono and/or other sample rates are unlikely supported. |
| 246 | // Fallback to WinMM in this case anyway. |
| 247 | const bitsPerSample = 32 |
| 248 | nBlockAlign := c.channelCount * bitsPerSample / 8 |
| 249 | var channelMask uint32 |
| 250 | switch c.channelCount { |
| 251 | case 1: |
| 252 | channelMask = _SPEAKER_FRONT_CENTER |
no test coverage detected