| 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:build darwin || linux || windows |
| 6 | |
| 7 | package alscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/ |
| 8 | |
| 9 | import ( |
| 10 | "errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/" |
| 11 | "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/" |
| 12 | "unsafescip-go gomod github.com/golang/go/src go1.24.0 unsafe/" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | muscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/mu. syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex# |
| 17 | devicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/device. unsafescip-go gomod github.com/golang/go/src go1.24.0 unsafe/.Pointer |
| 18 | contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/context. unsafescip-go gomod github.com/golang/go/src go1.24.0 unsafe/.Pointer |
| 19 | ) |
| 20 | |
| 21 | // DeviceError returns the last known error from the current device. |
| 22 | func DeviceErrorscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/DeviceError().() int32 { |
| 23 | return alcGetErrorscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/alcGetError().(devicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/device.) |
| 24 | } |
| 25 | |
| 26 | // TODO(jbd): Investigate the cases where multiple audio output |
| 27 | // devices might be needed. |
| 28 | |
| 29 | // OpenDevice opens the default audio device. |
| 30 | // Calls to OpenDevice are safe for concurrent use. |
| 31 | func OpenDevicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/OpenDevice().() error { |
| 32 | muscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/mu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Lock().() |
| 33 | defer muscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/mu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Unlock().() |
| 34 | |
| 35 | // already opened |
| 36 | if devicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/device. != nil { |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | dev := alcOpenDevicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/alcOpenDevice().("") |
| 41 | if dev == nil { |
| 42 | return errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("al: cannot open the default audio device") |
| 43 | } |
| 44 | ctx := alcCreateContextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/alcCreateContext().(dev, nil) |
| 45 | if ctx == nil { |
| 46 | alcCloseDevicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/alcCloseDevice().(dev) |
| 47 | return errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("al: cannot create a new context") |
| 48 | } |
| 49 | if !alcMakeContextCurrentscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/alcMakeContextCurrent().(ctx) { |
| 50 | alcCloseDevicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/alcCloseDevice().(dev) |
| 51 | return errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("al: cannot make context current") |
| 52 | } |
| 53 | devicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/device. = dev |
| 54 | contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/context. = ctx |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | // CloseDevice closes the device and frees related resources. |
| 59 | // Calls to CloseDevice are safe for concurrent use. |
| 60 | func CloseDevicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/CloseDevice().() { |
| 61 | muscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/mu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Lock().() |
| 62 | defer muscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/mu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Unlock().() |
| 63 | |
| 64 | if devicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/device. == nil { |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | alcCloseDevicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/alcCloseDevice().(devicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/device.) |
| 69 | if contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/context. != nil { |
| 70 | alcDestroyContextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/alcDestroyContext().(contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/context.) |
| 71 | } |
| 72 | devicescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/device. = nil |
| 73 | contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/audio/al`/context. = nil |
| 74 | } |