2015-01-10 17:23:43 +01:00
|
|
|
// Copyright 2015 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-10-01 10:24:30 +02:00
|
|
|
// Package audio provides audio players.
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// The stream format must be 16-bit little endian and 2 channels. The format is as follows:
|
2022-08-03 13:48:02 +02:00
|
|
|
//
|
|
|
|
// [data] = [sample 1] [sample 2] [sample 3] ...
|
|
|
|
// [sample *] = [channel 1] ...
|
|
|
|
// [channel *] = [byte 1] [byte 2] ...
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// An audio context (audio.Context object) has a sample rate you can specify and all streams you want to play must have the same
|
|
|
|
// sample rate. However, decoders in e.g. audio/mp3 package adjust sample rate automatically,
|
2017-01-15 10:02:21 +01:00
|
|
|
// and you don't have to care about it as long as you use those decoders.
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// An audio context can generate 'players' (audio.Player objects),
|
2016-04-10 10:07:58 +02:00
|
|
|
// and you can play sound by calling Play function of players.
|
|
|
|
// When multiple players play, mixing is automatically done.
|
|
|
|
// Note that too many players may cause distortion.
|
2017-07-13 19:26:41 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// For the simplest example to play sound, see wav package in the examples.
|
2015-01-23 15:04:56 +01:00
|
|
|
package audio
|
2015-01-10 17:23:43 +01:00
|
|
|
|
|
|
|
import (
|
2016-06-26 19:22:44 +02:00
|
|
|
"bytes"
|
2019-04-28 19:27:12 +02:00
|
|
|
"errors"
|
2017-12-23 10:29:43 +01:00
|
|
|
"fmt"
|
2016-02-10 18:04:23 +01:00
|
|
|
"io"
|
2024-07-15 06:40:37 +02:00
|
|
|
"reflect"
|
2016-03-28 17:06:37 +02:00
|
|
|
"runtime"
|
2018-05-09 05:14:17 +02:00
|
|
|
"sync"
|
2016-03-06 10:55:20 +01:00
|
|
|
"time"
|
2021-01-06 16:26:20 +01:00
|
|
|
|
2022-07-23 11:32:22 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
|
2023-10-06 06:49:42 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hook"
|
2019-04-28 19:27:12 +02:00
|
|
|
)
|
2016-03-12 20:48:13 +01:00
|
|
|
|
2019-04-28 19:27:12 +02:00
|
|
|
const (
|
2024-07-07 11:55:37 +02:00
|
|
|
channelCount = 2
|
|
|
|
bitDepthInBytesInt16 = 2
|
|
|
|
bitDepthInBytesFloat32 = 4
|
2015-01-10 17:23:43 +01:00
|
|
|
)
|
|
|
|
|
2017-10-01 10:24:30 +02:00
|
|
|
// A Context represents a current state of audio.
|
2016-04-10 10:07:58 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// At most one Context object can exist in one process.
|
2016-09-26 16:08:34 +02:00
|
|
|
// This means only one constant sample rate is valid in your one application.
|
|
|
|
//
|
2017-11-30 17:38:47 +01:00
|
|
|
// For a typical usage example, see examples/wav/main.go.
|
2016-03-02 16:48:59 +01:00
|
|
|
type Context struct {
|
2021-08-21 12:52:11 +02:00
|
|
|
playerFactory *playerFactory
|
2019-01-26 19:29:41 +01:00
|
|
|
|
2017-12-23 16:23:57 +01:00
|
|
|
sampleRate int
|
2018-02-04 09:33:17 +01:00
|
|
|
err error
|
2018-10-13 15:41:37 +02:00
|
|
|
ready bool
|
2018-02-04 09:33:17 +01:00
|
|
|
|
2024-02-01 06:24:07 +01:00
|
|
|
playingPlayers map[*playerImpl]struct{}
|
2019-04-28 19:27:12 +02:00
|
|
|
|
2019-11-09 07:19:27 +01:00
|
|
|
m sync.Mutex
|
|
|
|
semaphore chan struct{}
|
2016-03-02 16:48:59 +01:00
|
|
|
}
|
|
|
|
|
2016-10-02 15:18:44 +02:00
|
|
|
var (
|
|
|
|
theContext *Context
|
|
|
|
theContextLock sync.Mutex
|
|
|
|
)
|
|
|
|
|
2017-09-25 17:38:50 +02:00
|
|
|
// NewContext creates a new audio context with the given sample rate.
|
|
|
|
//
|
2023-01-09 10:53:19 +01:00
|
|
|
// sampleRate specifies the number of samples that should be played during one second.
|
|
|
|
// Usual numbers are 44100 or 48000. One context has only one sample rate. You cannot play multiple audio
|
|
|
|
// sources with different sample rates at the same time.
|
2017-04-04 18:12:02 +02:00
|
|
|
//
|
|
|
|
// NewContext panics when an audio context is already created.
|
2020-10-04 19:25:11 +02:00
|
|
|
func NewContext(sampleRate int) *Context {
|
2016-10-02 15:18:44 +02:00
|
|
|
theContextLock.Lock()
|
|
|
|
defer theContextLock.Unlock()
|
2021-01-06 17:30:03 +01:00
|
|
|
|
2016-10-02 15:18:44 +02:00
|
|
|
if theContext != nil {
|
2017-04-04 18:12:02 +02:00
|
|
|
panic("audio: context is already created")
|
2016-10-02 15:18:44 +02:00
|
|
|
}
|
2019-01-26 19:29:41 +01:00
|
|
|
|
2016-04-15 17:52:07 +02:00
|
|
|
c := &Context{
|
2024-02-01 06:24:07 +01:00
|
|
|
sampleRate: sampleRate,
|
|
|
|
playerFactory: newPlayerFactory(sampleRate),
|
|
|
|
playingPlayers: map[*playerImpl]struct{}{},
|
|
|
|
semaphore: make(chan struct{}, 1),
|
2016-04-15 17:52:07 +02:00
|
|
|
}
|
2016-10-02 15:18:44 +02:00
|
|
|
theContext = c
|
2017-07-10 17:20:47 +02:00
|
|
|
|
2019-04-28 12:35:59 +02:00
|
|
|
h := getHook()
|
2021-05-04 15:24:31 +02:00
|
|
|
h.OnSuspendAudio(func() error {
|
2019-11-09 07:19:27 +01:00
|
|
|
c.semaphore <- struct{}{}
|
2022-08-31 05:11:50 +02:00
|
|
|
if err := c.playerFactory.suspend(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-01 12:28:52 +01:00
|
|
|
if err := c.onSuspend(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-04 15:24:31 +02:00
|
|
|
return nil
|
2018-05-26 11:04:20 +02:00
|
|
|
})
|
2021-05-04 15:24:31 +02:00
|
|
|
h.OnResumeAudio(func() error {
|
2019-11-09 07:19:27 +01:00
|
|
|
<-c.semaphore
|
2022-08-31 05:11:50 +02:00
|
|
|
if err := c.playerFactory.resume(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-01 12:28:52 +01:00
|
|
|
if err := c.onResume(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-04 15:24:31 +02:00
|
|
|
return nil
|
2018-05-26 11:04:20 +02:00
|
|
|
})
|
2019-01-09 16:44:53 +01:00
|
|
|
|
2019-04-28 12:35:59 +02:00
|
|
|
h.AppendHookOnBeforeUpdate(func() error {
|
2019-01-09 16:44:53 +01:00
|
|
|
var err error
|
|
|
|
theContextLock.Lock()
|
|
|
|
if theContext != nil {
|
2021-10-22 08:29:00 +02:00
|
|
|
err = theContext.error()
|
2019-01-09 16:44:53 +01:00
|
|
|
}
|
|
|
|
theContextLock.Unlock()
|
2021-03-21 18:57:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-16 14:39:38 +01:00
|
|
|
// Initialize the context here in the case when there is no player and
|
|
|
|
// the program waits for IsReady() to be true (#969, #970, #2715).
|
|
|
|
ready, err := c.playerFactory.initContextIfNeeded()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ready != nil {
|
|
|
|
go func() {
|
|
|
|
<-ready
|
|
|
|
c.setReady()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-02-01 07:43:02 +01:00
|
|
|
if err := c.updatePlayers(); err != nil {
|
2021-03-28 14:57:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-03-21 18:57:52 +01:00
|
|
|
return nil
|
2018-05-26 11:04:20 +02:00
|
|
|
})
|
2017-07-13 18:38:22 +02:00
|
|
|
|
2020-10-04 19:25:11 +02:00
|
|
|
return c
|
2019-04-27 15:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentContext returns the current context or nil if there is no context.
|
|
|
|
func CurrentContext() *Context {
|
|
|
|
theContextLock.Lock()
|
|
|
|
c := theContext
|
|
|
|
theContextLock.Unlock()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-04-28 13:31:20 +02:00
|
|
|
func (c *Context) setError(err error) {
|
|
|
|
// TODO: What if c.err already exists?
|
|
|
|
c.m.Lock()
|
|
|
|
c.err = err
|
|
|
|
c.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-10-22 08:29:00 +02:00
|
|
|
func (c *Context) error() error {
|
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
if c.err != nil {
|
|
|
|
return c.err
|
|
|
|
}
|
|
|
|
return c.playerFactory.error()
|
|
|
|
}
|
|
|
|
|
2019-04-28 13:31:20 +02:00
|
|
|
func (c *Context) setReady() {
|
|
|
|
c.m.Lock()
|
|
|
|
c.ready = true
|
|
|
|
c.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2024-02-01 06:24:07 +01:00
|
|
|
func (c *Context) addPlayingPlayer(p *playerImpl) {
|
2019-04-28 19:27:12 +02:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2024-02-01 06:24:07 +01:00
|
|
|
c.playingPlayers[p] = struct{}{}
|
2019-04-28 19:27:12 +02:00
|
|
|
|
2024-07-15 06:40:37 +02:00
|
|
|
// (reflect.Type).Comparable() is enough here, as reflect.TypeOf should always return a dynamic (non-interface) type.
|
|
|
|
// If reflect.TypeOf returned an interface type, this check would be meaningless.
|
|
|
|
// See these for more details:
|
|
|
|
// * https://pkg.go.dev/reflect#TypeOf
|
|
|
|
// * https://pkg.go.dev/reflect#Type.Comparable
|
|
|
|
//
|
|
|
|
// (*reflect.Value).Comparable() is more intuitive but this was introduced in Go 1.20.
|
|
|
|
if !reflect.TypeOf(p.sourceIdent()).Comparable() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:27:12 +02:00
|
|
|
// Check the source duplication
|
2024-07-07 11:55:37 +02:00
|
|
|
srcs := map[any]struct{}{}
|
2024-02-01 06:24:07 +01:00
|
|
|
for p := range c.playingPlayers {
|
2024-07-07 11:55:37 +02:00
|
|
|
if _, ok := srcs[p.sourceIdent()]; ok {
|
2024-07-15 06:40:37 +02:00
|
|
|
c.err = errors.New("audio: the same source must not be used by multiple Player objects")
|
2019-04-27 15:00:12 +02:00
|
|
|
return
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
2024-07-07 11:55:37 +02:00
|
|
|
srcs[p.sourceIdent()] = struct{}{}
|
2016-05-24 20:47:59 +02:00
|
|
|
}
|
2017-07-10 17:20:47 +02:00
|
|
|
}
|
|
|
|
|
2024-02-01 06:24:07 +01:00
|
|
|
func (c *Context) removePlayingPlayer(p *playerImpl) {
|
2019-04-28 19:27:12 +02:00
|
|
|
c.m.Lock()
|
2024-02-01 06:24:07 +01:00
|
|
|
delete(c.playingPlayers, p)
|
2019-04-28 19:27:12 +02:00
|
|
|
c.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2024-02-01 12:28:52 +01:00
|
|
|
func (c *Context) onSuspend() error {
|
|
|
|
// A Context must not call playerImpl's functions with a lock, or this causes a deadlock (#2737).
|
|
|
|
// Copy the playerImpls and iterate them without a lock.
|
|
|
|
var players []*playerImpl
|
|
|
|
c.m.Lock()
|
|
|
|
players = make([]*playerImpl, 0, len(c.playingPlayers))
|
|
|
|
for p := range c.playingPlayers {
|
|
|
|
players = append(players, p)
|
|
|
|
}
|
|
|
|
c.m.Unlock()
|
|
|
|
|
|
|
|
for _, p := range players {
|
|
|
|
if err := p.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.onContextSuspended()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) onResume() error {
|
|
|
|
// A Context must not call playerImpl's functions with a lock, or this causes a deadlock (#2737).
|
|
|
|
// Copy the playerImpls and iterate them without a lock.
|
|
|
|
var players []*playerImpl
|
|
|
|
c.m.Lock()
|
|
|
|
players = make([]*playerImpl, 0, len(c.playingPlayers))
|
|
|
|
for p := range c.playingPlayers {
|
|
|
|
players = append(players, p)
|
|
|
|
}
|
|
|
|
c.m.Unlock()
|
|
|
|
|
|
|
|
for _, p := range players {
|
|
|
|
if err := p.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.onContextResumed()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 07:43:02 +01:00
|
|
|
func (c *Context) updatePlayers() error {
|
2023-08-29 06:40:21 +02:00
|
|
|
// A Context must not call playerImpl's functions with a lock, or this causes a deadlock (#2737).
|
|
|
|
// Copy the playerImpls and iterate them without a lock.
|
|
|
|
var players []*playerImpl
|
2021-03-24 15:41:14 +01:00
|
|
|
c.m.Lock()
|
2024-02-01 06:24:07 +01:00
|
|
|
players = make([]*playerImpl, 0, len(c.playingPlayers))
|
|
|
|
for p := range c.playingPlayers {
|
2023-08-29 06:40:21 +02:00
|
|
|
players = append(players, p)
|
|
|
|
}
|
|
|
|
c.m.Unlock()
|
|
|
|
|
|
|
|
var playersToRemove []*playerImpl
|
2021-03-24 15:41:14 +01:00
|
|
|
|
|
|
|
// Now reader players cannot call removePlayers from themselves in the current implementation.
|
|
|
|
// Underlying playering can be the pause state after fishing its playing,
|
2021-08-21 12:52:11 +02:00
|
|
|
// but there is no way to notify this to players so far.
|
2021-03-24 15:41:14 +01:00
|
|
|
// Instead, let's check the states proactively every frame.
|
2023-08-29 06:40:21 +02:00
|
|
|
for _, p := range players {
|
2021-08-21 12:52:11 +02:00
|
|
|
if err := p.Err(); err != nil {
|
2021-03-28 14:57:39 +02:00
|
|
|
return err
|
2021-03-24 15:41:14 +01:00
|
|
|
}
|
2024-02-01 07:43:02 +01:00
|
|
|
p.updatePosition()
|
2021-08-21 12:52:11 +02:00
|
|
|
if !p.IsPlaying() {
|
2023-08-29 06:40:21 +02:00
|
|
|
playersToRemove = append(playersToRemove, p)
|
2021-03-24 15:41:14 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-28 14:57:39 +02:00
|
|
|
|
2023-08-29 06:40:21 +02:00
|
|
|
c.m.Lock()
|
|
|
|
for _, p := range playersToRemove {
|
2024-02-01 06:24:07 +01:00
|
|
|
delete(c.playingPlayers, p)
|
2023-08-29 06:40:21 +02:00
|
|
|
}
|
|
|
|
c.m.Unlock()
|
|
|
|
|
2021-03-28 14:57:39 +02:00
|
|
|
return nil
|
2021-03-24 15:41:14 +01:00
|
|
|
}
|
|
|
|
|
2018-10-13 15:41:37 +02:00
|
|
|
// IsReady returns a boolean value indicating whether the audio is ready or not.
|
|
|
|
//
|
|
|
|
// On some browsers, user interaction like click or pressing keys is required to start audio.
|
|
|
|
func (c *Context) IsReady() bool {
|
|
|
|
c.m.Lock()
|
2019-10-22 16:45:00 +02:00
|
|
|
defer c.m.Unlock()
|
2024-03-16 14:39:38 +01:00
|
|
|
return c.ready
|
2018-10-13 15:41:37 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 19:00:05 +01:00
|
|
|
// SampleRate returns the sample rate.
|
|
|
|
func (c *Context) SampleRate() int {
|
2016-04-15 17:52:07 +02:00
|
|
|
return c.sampleRate
|
2016-03-12 19:00:05 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 19:24:54 +02:00
|
|
|
// Player is an audio player which has one stream.
|
2018-12-18 15:07:19 +01:00
|
|
|
//
|
|
|
|
// Even when all references to a Player object is gone,
|
|
|
|
// the object is not GCed until the player finishes playing.
|
|
|
|
// This means that if a Player plays an infinite stream,
|
|
|
|
// the object is never GCed unless Close is called.
|
2016-02-10 18:18:39 +01:00
|
|
|
type Player struct {
|
2021-12-17 08:01:24 +01:00
|
|
|
p *playerImpl
|
2016-02-10 18:18:39 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// NewPlayer creates a new player with the given stream.
|
2015-01-24 07:48:48 +01:00
|
|
|
//
|
2022-07-23 11:17:05 +02:00
|
|
|
// src's format must be linear PCM (signed 16bits little endian, 2 channel stereo)
|
2016-02-07 16:51:25 +01:00
|
|
|
// without a header (e.g. RIFF header).
|
2016-04-10 10:07:58 +02:00
|
|
|
// The sample rate must be same as that of the audio context.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2018-03-21 16:33:17 +01:00
|
|
|
// The player is seekable when src is io.Seeker.
|
|
|
|
// Attempt to seek the player that is not io.Seeker causes panic.
|
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// Note that the given src can't be shared with other Player objects.
|
2016-06-26 19:02:01 +02:00
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// NewPlayer tries to call Seek of src to get the current position.
|
2017-04-17 17:59:10 +02:00
|
|
|
// NewPlayer returns error when the Seek returns error.
|
2019-03-31 18:59:10 +02:00
|
|
|
//
|
2020-10-07 16:09:02 +02:00
|
|
|
// A Player doesn't close src even if src implements io.Closer.
|
|
|
|
// Closing the source is src owner's responsibility.
|
2021-07-22 09:39:02 +02:00
|
|
|
func (c *Context) NewPlayer(src io.Reader) (*Player, error) {
|
2024-07-07 11:55:37 +02:00
|
|
|
_, seekable := src.(io.Seeker)
|
|
|
|
f32Src := convert.NewFloat32BytesReaderFromInt16BytesReader(src)
|
|
|
|
pi, err := c.playerFactory.newPlayer(c, f32Src, seekable, src, bitDepthInBytesFloat32)
|
2021-01-06 16:59:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-04-15 17:52:07 +02:00
|
|
|
}
|
2021-01-06 16:59:13 +01:00
|
|
|
|
|
|
|
p := &Player{pi}
|
|
|
|
|
2018-12-16 19:37:00 +01:00
|
|
|
runtime.SetFinalizer(p, (*Player).finalize)
|
2017-12-23 09:24:51 +01:00
|
|
|
|
2016-04-15 17:52:07 +02:00
|
|
|
return p, nil
|
2016-03-03 03:57:25 +01:00
|
|
|
}
|
2016-02-12 13:39:48 +01:00
|
|
|
|
2021-07-22 09:39:02 +02:00
|
|
|
// NewPlayer creates a new player with the given stream.
|
|
|
|
//
|
|
|
|
// Deprecated: as of v2.2. Use (*Context).NewPlayer instead.
|
|
|
|
func NewPlayer(context *Context, src io.Reader) (*Player, error) {
|
|
|
|
return context.NewPlayer(src)
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:22:44 +02:00
|
|
|
// NewPlayerFromBytes creates a new player with the given bytes.
|
|
|
|
//
|
|
|
|
// As opposed to NewPlayer, you don't have to care if src is already used by another player or not.
|
|
|
|
// src can be shared by multiple players.
|
|
|
|
//
|
|
|
|
// The format of src should be same as noted at NewPlayer.
|
2021-07-22 09:39:02 +02:00
|
|
|
func (c *Context) NewPlayerFromBytes(src []byte) *Player {
|
|
|
|
p, err := c.NewPlayer(bytes.NewReader(src))
|
2017-06-02 18:12:58 +02:00
|
|
|
if err != nil {
|
|
|
|
// Errors should never happen.
|
2019-02-07 09:19:24 +01:00
|
|
|
panic(fmt.Sprintf("audio: %v at NewPlayerFromBytes", err))
|
2017-06-02 18:12:58 +02:00
|
|
|
}
|
2020-10-04 19:14:45 +02:00
|
|
|
return p
|
2016-06-26 19:22:44 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:39:02 +02:00
|
|
|
// NewPlayerFromBytes creates a new player with the given bytes.
|
|
|
|
//
|
|
|
|
// Deprecated: as of v2.2. Use (*Context).NewPlayerFromBytes instead.
|
|
|
|
func NewPlayerFromBytes(context *Context, src []byte) *Player {
|
|
|
|
return context.NewPlayerFromBytes(src)
|
|
|
|
}
|
|
|
|
|
2018-12-16 19:37:00 +01:00
|
|
|
func (p *Player) finalize() {
|
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
if !p.IsPlaying() {
|
2022-09-09 18:52:46 +02:00
|
|
|
_ = p.Close()
|
2018-12-16 19:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 10:24:30 +02:00
|
|
|
// Close closes the stream.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2020-10-07 16:43:33 +02:00
|
|
|
// When Close is called, the stream owned by the player is NOT closed,
|
|
|
|
// even if the stream implements io.Closer.
|
2017-06-03 19:52:11 +02:00
|
|
|
//
|
2020-10-07 16:09:02 +02:00
|
|
|
// Close returns error when the player is already closed.
|
2016-03-28 17:06:37 +02:00
|
|
|
func (p *Player) Close() error {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.Close()
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Play plays the stream.
|
2020-10-10 16:45:18 +02:00
|
|
|
func (p *Player) Play() {
|
2018-12-15 14:14:42 +01:00
|
|
|
p.p.Play()
|
2015-01-22 19:02:23 +01:00
|
|
|
}
|
2016-02-11 11:55:59 +01:00
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// IsPlaying returns boolean indicating whether the player is playing.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) IsPlaying() bool {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.IsPlaying()
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Rewind rewinds the current position to the start.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2018-03-21 16:33:17 +01:00
|
|
|
// The passed source to NewPlayer must be io.Seeker, or Rewind panics.
|
|
|
|
//
|
2017-10-01 10:24:30 +02:00
|
|
|
// Rewind returns error when seeking the source stream returns error.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Rewind() error {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.Rewind()
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:20:57 +02:00
|
|
|
// SetPosition sets the position with the given offset.
|
|
|
|
//
|
|
|
|
// The passed source to NewPlayer must be io.Seeker, or SetPosition panics.
|
2016-04-15 17:52:07 +02:00
|
|
|
//
|
2023-08-01 18:20:57 +02:00
|
|
|
// SetPosition returns error when seeking the source stream returns an error.
|
|
|
|
func (p *Player) SetPosition(offset time.Duration) error {
|
|
|
|
return p.p.SetPosition(offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek seeks the position with the given offset.
|
2018-03-21 16:33:17 +01:00
|
|
|
//
|
2023-08-01 18:20:57 +02:00
|
|
|
// Deprecated: as of v2.6. Use SetPosition instead.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Seek(offset time.Duration) error {
|
2023-08-01 18:20:57 +02:00
|
|
|
return p.SetPosition(offset)
|
2018-12-15 14:14:42 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Pause pauses the playing.
|
2020-10-10 16:45:18 +02:00
|
|
|
func (p *Player) Pause() {
|
2018-12-15 14:14:42 +01:00
|
|
|
p.p.Pause()
|
2016-02-11 11:55:59 +01:00
|
|
|
}
|
2016-03-06 10:55:20 +01:00
|
|
|
|
2023-08-01 18:20:57 +02:00
|
|
|
// Position returns the current position in time.
|
2021-03-31 16:41:24 +02:00
|
|
|
//
|
2023-08-01 18:20:57 +02:00
|
|
|
// As long as the player continues to play, Position's returning value is increased monotonically,
|
2021-03-31 16:41:24 +02:00
|
|
|
// even though the source stream loops and its position goes back.
|
2023-08-01 18:20:57 +02:00
|
|
|
func (p *Player) Position() time.Duration {
|
|
|
|
return p.p.Position()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Current returns the current position in time.
|
|
|
|
//
|
|
|
|
// Deprecated: as of v2.6. Use Position instead.
|
2016-03-06 10:55:20 +01:00
|
|
|
func (p *Player) Current() time.Duration {
|
2023-08-01 18:20:57 +02:00
|
|
|
return p.Position()
|
2018-12-15 14:14:42 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// Volume returns the current volume of this player [0-1].
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) Volume() float64 {
|
2018-12-15 14:14:42 +01:00
|
|
|
return p.p.Volume()
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:07:58 +02:00
|
|
|
// SetVolume sets the volume of this player.
|
2018-07-14 18:04:46 +02:00
|
|
|
// volume must be in between 0 and 1. SetVolume panics otherwise.
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) SetVolume(volume float64) {
|
2018-12-15 14:14:42 +01:00
|
|
|
p.p.SetVolume(volume)
|
|
|
|
}
|
|
|
|
|
2022-03-24 19:03:46 +01:00
|
|
|
// SetBufferSize adjusts the buffer size of the player.
|
|
|
|
// If 0 is specified, the default buffer size is used.
|
|
|
|
// A small buffer size is useful if you want to play a real-time PCM for example.
|
|
|
|
// Note that the audio quality might be affected if you modify the buffer size.
|
2022-03-25 12:46:59 +01:00
|
|
|
func (p *Player) SetBufferSize(bufferSize time.Duration) {
|
2022-03-24 19:03:46 +01:00
|
|
|
p.p.SetBufferSize(bufferSize)
|
|
|
|
}
|
|
|
|
|
2023-10-06 06:49:42 +02:00
|
|
|
type hooker interface {
|
2021-05-04 15:24:31 +02:00
|
|
|
OnSuspendAudio(f func() error)
|
|
|
|
OnResumeAudio(f func() error)
|
2021-01-06 16:26:20 +01:00
|
|
|
AppendHookOnBeforeUpdate(f func() error)
|
|
|
|
}
|
|
|
|
|
2023-10-06 06:49:42 +02:00
|
|
|
var hookerForTesting hooker
|
2021-01-06 16:26:20 +01:00
|
|
|
|
2023-10-06 06:49:42 +02:00
|
|
|
func getHook() hooker {
|
|
|
|
if hookerForTesting != nil {
|
|
|
|
return hookerForTesting
|
2021-01-06 16:26:20 +01:00
|
|
|
}
|
2023-10-06 06:49:42 +02:00
|
|
|
return &hookerImpl{}
|
2021-01-06 16:26:20 +01:00
|
|
|
}
|
|
|
|
|
2023-10-06 06:49:42 +02:00
|
|
|
type hookerImpl struct{}
|
2021-01-06 16:26:20 +01:00
|
|
|
|
2023-10-06 06:49:42 +02:00
|
|
|
func (h *hookerImpl) OnSuspendAudio(f func() error) {
|
|
|
|
hook.OnSuspendAudio(f)
|
2021-01-06 16:26:20 +01:00
|
|
|
}
|
|
|
|
|
2023-10-06 06:49:42 +02:00
|
|
|
func (h *hookerImpl) OnResumeAudio(f func() error) {
|
|
|
|
hook.OnResumeAudio(f)
|
2021-01-06 16:26:20 +01:00
|
|
|
}
|
|
|
|
|
2023-10-06 06:49:42 +02:00
|
|
|
func (h *hookerImpl) AppendHookOnBeforeUpdate(f func() error) {
|
|
|
|
hook.AppendHookOnBeforeUpdate(f)
|
2021-01-06 16:26:20 +01:00
|
|
|
}
|
2022-07-23 11:32:22 +02:00
|
|
|
|
2022-07-23 11:35:53 +02:00
|
|
|
// Resample converts the sample rate of the given stream.
|
|
|
|
// size is the length of the source stream in bytes.
|
|
|
|
// from is the original sample rate.
|
|
|
|
// to is the target sample rate.
|
2022-07-23 11:32:22 +02:00
|
|
|
//
|
2022-07-23 11:35:53 +02:00
|
|
|
// If the original sample rate equals to the new one, Resample returns source as it is.
|
2022-07-23 11:32:22 +02:00
|
|
|
func Resample(source io.ReadSeeker, size int64, from, to int) io.ReadSeeker {
|
|
|
|
if from == to {
|
|
|
|
return source
|
|
|
|
}
|
2024-07-13 17:24:36 +02:00
|
|
|
return convert.NewResampling(source, size, from, to, bitDepthInBytesInt16)
|
2022-07-23 11:32:22 +02:00
|
|
|
}
|