mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
audio: an inner player should be in the context
This commit is contained in:
parent
13461aa455
commit
66052b5b5d
@ -91,9 +91,10 @@ func (s *mixedPlayersStream) Read(b []byte) (int, error) {
|
|||||||
// TODO: Enable to specify the format like Mono8?
|
// TODO: Enable to specify the format like Mono8?
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
sampleRate int
|
sampleRate int
|
||||||
stream *mixedPlayersStream
|
stream *mixedPlayersStream
|
||||||
players map[*Player]struct{}
|
players map[*Player]struct{}
|
||||||
|
innerPlayer *player
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,9 +105,11 @@ func NewContext(sampleRate int) *Context {
|
|||||||
players: map[*Player]struct{}{},
|
players: map[*Player]struct{}{},
|
||||||
}
|
}
|
||||||
c.stream = &mixedPlayersStream{c}
|
c.stream = &mixedPlayersStream{c}
|
||||||
if err := startPlaying(c.stream, c.sampleRate); err != nil {
|
p, err := startPlaying(c.stream, c.sampleRate)
|
||||||
|
if err != nil {
|
||||||
panic(fmt.Sprintf("audio: NewContext error: %v", err))
|
panic(fmt.Sprintf("audio: NewContext error: %v", err))
|
||||||
}
|
}
|
||||||
|
c.innerPlayer = p
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,17 +31,12 @@ type player struct {
|
|||||||
bufferSource *js.Object
|
bufferSource *js.Object
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentPlayer *player
|
func startPlaying(src io.Reader, sampleRate int) (*player, error) {
|
||||||
|
|
||||||
func startPlaying(src io.Reader, sampleRate int) error {
|
|
||||||
// Do nothing in node.js.
|
// Do nothing in node.js.
|
||||||
if js.Global.Get("require") != js.Undefined {
|
if js.Global.Get("require") != js.Undefined {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if currentPlayer != nil {
|
|
||||||
panic("audio: currentPlayer already exists")
|
|
||||||
}
|
|
||||||
class := js.Global.Get("AudioContext")
|
class := js.Global.Get("AudioContext")
|
||||||
if class == js.Undefined {
|
if class == js.Undefined {
|
||||||
class = js.Global.Get("webkitAudioContext")
|
class = js.Global.Get("webkitAudioContext")
|
||||||
@ -49,17 +44,17 @@ func startPlaying(src io.Reader, sampleRate int) error {
|
|||||||
if class == js.Undefined {
|
if class == js.Undefined {
|
||||||
panic("audio: audio couldn't be initialized")
|
panic("audio: audio couldn't be initialized")
|
||||||
}
|
}
|
||||||
currentPlayer = &player{
|
p := &player{
|
||||||
src: src,
|
src: src,
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
bufferSource: nil,
|
bufferSource: nil,
|
||||||
context: class.New(),
|
context: class.New(),
|
||||||
}
|
}
|
||||||
currentPlayer.position = currentPlayer.context.Get("currentTime").Float()
|
p.position = p.context.Get("currentTime").Float()
|
||||||
if err := currentPlayer.start(); err != nil {
|
if err := p.start(); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
return nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toLR(data []byte) ([]int16, []int16) {
|
func toLR(data []byte) ([]int16, []int16) {
|
||||||
|
@ -39,30 +39,25 @@ type player struct {
|
|||||||
isClosed bool
|
isClosed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentPlayer *player
|
func startPlaying(src io.Reader, sampleRate int) (*player, error) {
|
||||||
|
|
||||||
func startPlaying(src io.Reader, sampleRate int) error {
|
|
||||||
if currentPlayer != nil {
|
|
||||||
panic("audio: currentPlayer already exists")
|
|
||||||
}
|
|
||||||
if e := al.OpenDevice(); e != nil {
|
if e := al.OpenDevice(); e != nil {
|
||||||
return fmt.Errorf("audio: OpenAL initialization failed: %v", e)
|
return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e)
|
||||||
}
|
}
|
||||||
s := al.GenSources(1)
|
s := al.GenSources(1)
|
||||||
if err := al.Error(); err != 0 {
|
if err := al.Error(); err != 0 {
|
||||||
panic(fmt.Sprintf("audio: al.GenSources error: %d", err))
|
panic(fmt.Sprintf("audio: al.GenSources error: %d", err))
|
||||||
}
|
}
|
||||||
currentPlayer = &player{
|
p := &player{
|
||||||
alSource: s[0],
|
alSource: s[0],
|
||||||
alBuffers: []al.Buffer{},
|
alBuffers: []al.Buffer{},
|
||||||
source: src,
|
source: src,
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(currentPlayer, (*player).close)
|
runtime.SetFinalizer(p, (*player).close)
|
||||||
if err := currentPlayer.start(); err != nil {
|
if err := p.start(); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
return nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const bufferSize = 1024
|
const bufferSize = 1024
|
||||||
|
Loading…
Reference in New Issue
Block a user