Debian Codegraph @

exp/audio/al/alc.go

raw symbols SCIP
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 al
8
9 import (
10 "errors"
11 "sync"
12 "unsafe"
13 )
14
15 var (
16 mu sync.Mutex
17 device unsafe.Pointer
18 context unsafe.Pointer
19 )
20
21 // DeviceError returns the last known error from the current device.
22 func DeviceError() int32 {
23 return alcGetError(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 OpenDevice() error {
32 mu.Lock()
33 defer mu.Unlock()
34
35 // already opened
36 if device != nil {
37 return nil
38 }
39
40 dev := alcOpenDevice("")
41 if dev == nil {
42 return errors.New("al: cannot open the default audio device")
43 }
44 ctx := alcCreateContext(dev, nil)
45 if ctx == nil {
46 alcCloseDevice(dev)
47 return errors.New("al: cannot create a new context")
48 }
49 if !alcMakeContextCurrent(ctx) {
50 alcCloseDevice(dev)
51 return errors.New("al: cannot make context current")
52 }
53 device = dev
54 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 CloseDevice() {
61 mu.Lock()
62 defer mu.Unlock()
63
64 if device == nil {
65 return
66 }
67
68 alcCloseDevice(device)
69 if context != nil {
70 alcDestroyContext(context)
71 }
72 device = nil
73 context = nil
74 }