2015-02-26 22:39:38 -08:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-03-03 10:34:19 -08:00
|
|
|
// +build darwin linux
|
2015-02-26 22:39:38 -08:00
|
|
|
|
|
|
|
package al
|
|
|
|
|
2015-04-06 14:45:20 -04:00
|
|
|
import (
|
2015-05-05 21:57:21 -04:00
|
|
|
"errors"
|
|
|
|
"sync"
|
2015-04-06 14:45:20 -04:00
|
|
|
"unsafe"
|
|
|
|
)
|
2015-02-26 22:39:38 -08:00
|
|
|
|
2015-05-05 21:57:21 -04:00
|
|
|
var (
|
2015-07-07 16:28:47 -06:00
|
|
|
mu sync.Mutex
|
|
|
|
device unsafe.Pointer
|
|
|
|
context unsafe.Pointer
|
2015-05-05 21:57:21 -04:00
|
|
|
)
|
2015-02-26 22:39:38 -08:00
|
|
|
|
2015-05-05 21:57:21 -04:00
|
|
|
// DeviceError returns the last known error from the current device.
|
|
|
|
func DeviceError() int32 {
|
|
|
|
return alcGetError(device)
|
2015-02-26 22:39:38 -08:00
|
|
|
}
|
|
|
|
|
2015-05-05 21:57:21 -04:00
|
|
|
// TODO(jbd): Investigate the cases where multiple audio output
|
|
|
|
// devices might be needed.
|
|
|
|
|
|
|
|
// OpenDevice opens the default audio device.
|
2015-06-22 09:51:27 -07:00
|
|
|
// Calls to OpenDevice are safe for concurrent use.
|
2015-05-05 21:57:21 -04:00
|
|
|
func OpenDevice() error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
// already opened
|
|
|
|
if device != nil {
|
2015-02-26 22:39:38 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-07 16:28:47 -06:00
|
|
|
dev := alcOpenDevice("")
|
|
|
|
if dev == nil {
|
2015-05-05 21:57:21 -04:00
|
|
|
return errors.New("al: cannot open the default audio device")
|
2015-02-26 22:39:38 -08:00
|
|
|
}
|
2015-07-07 16:28:47 -06:00
|
|
|
ctx := alcCreateContext(dev, nil)
|
2015-06-22 09:51:27 -07:00
|
|
|
if ctx == nil {
|
2015-07-07 16:28:47 -06:00
|
|
|
alcCloseDevice(dev)
|
|
|
|
return errors.New("al: cannot create a new context")
|
2015-06-22 09:51:27 -07:00
|
|
|
}
|
2015-07-07 16:28:47 -06:00
|
|
|
if !alcMakeContextCurrent(ctx) {
|
|
|
|
alcCloseDevice(dev)
|
|
|
|
return errors.New("al: cannot make context current")
|
|
|
|
}
|
|
|
|
device = dev
|
|
|
|
context = ctx
|
|
|
|
return nil
|
2015-06-22 09:51:27 -07:00
|
|
|
}
|
|
|
|
|
2015-05-05 21:57:21 -04:00
|
|
|
// CloseDevice closes the device and frees related resources.
|
2015-06-22 09:51:27 -07:00
|
|
|
// Calls to CloseDevice are safe for concurrent use.
|
2015-05-05 21:57:21 -04:00
|
|
|
func CloseDevice() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2015-04-06 11:35:53 -04:00
|
|
|
|
2015-07-07 16:28:47 -06:00
|
|
|
if device == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
alcCloseDevice(device)
|
|
|
|
if context != nil {
|
|
|
|
alcDestroyContext(context)
|
2015-05-05 21:57:21 -04:00
|
|
|
}
|
2015-07-07 16:28:47 -06:00
|
|
|
device = nil
|
|
|
|
context = nil
|
2015-04-06 11:35:53 -04:00
|
|
|
}
|