2021-05-01 09:15:36 +02:00
|
|
|
// Copyright 2021 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package oboe
|
|
|
|
|
2021-05-05 11:09:41 +02:00
|
|
|
// Disable AAudio (#1634).
|
|
|
|
// AAudio doesn't care about plugging in/out of a headphone.
|
|
|
|
// See https://github.com/google/oboe/blob/master/docs/notes/disconnect.md
|
|
|
|
|
|
|
|
// #cgo CXXFLAGS: -std=c++17 -DOBOE_ENABLE_AAUDIO=0
|
2021-05-01 09:15:36 +02:00
|
|
|
// #cgo LDFLAGS: -llog -lOpenSLES -static-libstdc++
|
|
|
|
//
|
|
|
|
// #include "binding_android.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2021-05-31 19:59:36 +02:00
|
|
|
"sync"
|
2021-05-01 09:15:36 +02:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2021-06-01 18:09:21 +02:00
|
|
|
func Play(sampleRate, channelNum, bitDepthInBytes int) error {
|
|
|
|
if msg := C.ebiten_oboe_Play(C.int(sampleRate), C.int(channelNum), C.int(bitDepthInBytes)); msg != nil {
|
|
|
|
return fmt.Errorf("oboe: Play failed: %s", C.GoString(msg))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:15:36 +02:00
|
|
|
func Suspend() error {
|
2021-05-15 20:00:36 +02:00
|
|
|
if msg := C.ebiten_oboe_Suspend(); msg != nil {
|
2021-05-01 09:15:36 +02:00
|
|
|
return fmt.Errorf("oboe: Suspend failed: %s", C.GoString(msg))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Resume() error {
|
2021-05-15 20:00:36 +02:00
|
|
|
if msg := C.ebiten_oboe_Resume(); msg != nil {
|
2021-05-01 09:15:36 +02:00
|
|
|
return fmt.Errorf("oboe: Resume failed: %s", C.GoString(msg))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Player struct {
|
|
|
|
player C.PlayerID
|
|
|
|
onWritten func()
|
2021-05-31 19:59:36 +02:00
|
|
|
|
|
|
|
// m is the mutex for this player.
|
|
|
|
// This is necessary as Close can be invoked from the finalizer goroutine.
|
|
|
|
m sync.Mutex
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 18:09:21 +02:00
|
|
|
func NewPlayer(volume float64, onWritten func()) *Player {
|
2021-05-01 09:15:36 +02:00
|
|
|
p := &Player{
|
|
|
|
onWritten: onWritten,
|
|
|
|
}
|
2021-06-01 18:09:21 +02:00
|
|
|
p.player = C.ebiten_oboe_Player_Create(C.double(volume), C.uintptr_t(uintptr(unsafe.Pointer(p))))
|
2021-05-01 09:15:36 +02:00
|
|
|
runtime.SetFinalizer(p, (*Player).Close)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:09:21 +02:00
|
|
|
//export ebiten_oboe_onWrittenCallback
|
|
|
|
func ebiten_oboe_onWrittenCallback(player C.uintptr_t) {
|
2021-05-01 09:15:36 +02:00
|
|
|
p := (*Player)(unsafe.Pointer(uintptr(player)))
|
|
|
|
p.onWritten()
|
|
|
|
}
|
|
|
|
|
2021-05-05 08:32:50 +02:00
|
|
|
func (p *Player) IsPlaying() bool {
|
2021-05-31 19:59:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-15 20:00:36 +02:00
|
|
|
return bool(C.ebiten_oboe_Player_IsPlaying(p.player))
|
2021-05-05 08:32:50 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:15:36 +02:00
|
|
|
func (p *Player) AppendBuffer(buf []byte) {
|
2021-05-31 19:59:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2021-05-01 09:15:36 +02:00
|
|
|
ptr := C.CBytes(buf)
|
|
|
|
defer C.free(ptr)
|
|
|
|
|
2021-05-15 20:00:36 +02:00
|
|
|
C.ebiten_oboe_Player_AppendBuffer(p.player, (*C.uint8_t)(ptr), C.int(len(buf)))
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Play() error {
|
2021-05-31 19:59:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2021-05-15 18:53:44 +02:00
|
|
|
if p.player == 0 {
|
|
|
|
return fmt.Errorf("oboe: player is already closed at Play")
|
|
|
|
}
|
2021-06-01 18:09:21 +02:00
|
|
|
C.ebiten_oboe_Player_Play(p.player)
|
2021-05-01 09:15:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Pause() error {
|
2021-05-31 19:59:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2021-05-15 18:53:44 +02:00
|
|
|
if p.player == 0 {
|
|
|
|
return fmt.Errorf("oboe: player is already closed at Pause")
|
|
|
|
}
|
2021-06-01 18:09:21 +02:00
|
|
|
C.ebiten_oboe_Player_Pause(p.player)
|
2021-05-01 09:15:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) SetVolume(volume float64) {
|
2021-05-31 19:59:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-15 20:00:36 +02:00
|
|
|
C.ebiten_oboe_Player_SetVolume(p.player, C.double(volume))
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Close() error {
|
2021-05-31 19:59:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2021-05-01 09:15:36 +02:00
|
|
|
runtime.SetFinalizer(p, nil)
|
2021-05-15 18:53:44 +02:00
|
|
|
if p.player == 0 {
|
|
|
|
return fmt.Errorf("oboe: player is already closed at Close")
|
|
|
|
}
|
2021-06-01 18:09:21 +02:00
|
|
|
C.ebiten_oboe_Player_Close(p.player)
|
2021-05-15 18:53:44 +02:00
|
|
|
p.player = 0
|
2021-05-01 09:15:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:22:19 +02:00
|
|
|
func (p *Player) UnplayedBufferSize() int {
|
2021-05-31 19:59:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-15 20:00:36 +02:00
|
|
|
return int(C.ebiten_oboe_Player_UnplayedBufferSize(p.player))
|
2021-05-01 09:15:36 +02:00
|
|
|
}
|