Documentation ¶
Overview ¶
Package audio provides audio players. This can be used with or without ebiten package.
The stream format must be 16-bit little endian and 2 channels.
An audio context has a sample rate you can set and all streams you want to play must have the same sample rate. However, decoders like audio/vorbis and audio/wav adjust sample rate, and you don't have to care about it as long as you use those decoders.
An audio context can generate 'players' (instances of audio.Player), 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.
Index ¶
- type Context
- type InfiniteLoop
- type Player
- func (p *Player) Close() error
- func (p *Player) Current() time.Duration
- func (p *Player) IsPlaying() bool
- func (p *Player) Pause() error
- func (p *Player) Play() error
- func (p *Player) Rewind() error
- func (p *Player) Seek(offset time.Duration) error
- func (p *Player) SetVolume(volume float64)
- func (p *Player) Volume() float64
- type ReadSeekCloser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
A Context is a current state of audio.
There should be at most one Context object. This means only one constant sample rate is valid in your one application.
The typical usage with ebiten package is:
var audioContext *audio.Context func update(screen *ebiten.Image) error { // Update updates the audio stream by 1/60 [sec]. if err := audioContext.Update(); err != nil { return err } // ... } func main() { audioContext, err = audio.NewContext(sampleRate) if err != nil { panic(err) } ebiten.Run(run, update, 320, 240, 2, "Audio test") }
This is 'sync mode' in that game's (logical) time and audio time are synchronized. You can also call Update independently from the game loop as 'async mode'. In this case, audio goes on even when the game stops e.g. by diactivating the screen.
func NewContext ¶
NewContext creates a new audio context with the given sample rate (e.g. 44100).
Error returned by NewContext is always nil as of 1.5.0-alpha.
NewContext panics when an audio context is already created.
func (*Context) Update ¶
Update proceeds the inner (logical) time of the context by 1/60 second.
This is expected to be called in the game's updating function (sync mode) or an independent goroutine with timers (async mode). In sync mode, the game logical time syncs the audio logical time and you will find audio stops when the game stops e.g. when the window is deactivated. In async mode, the audio never stops even when the game stops.
Update returns error when IO error occurs in the underlying IO object.
type InfiniteLoop ¶ added in v1.5.0
type InfiniteLoop struct {
// contains filtered or unexported fields
}
InfiniteLoop represents a loop which never ends.
func NewInfiniteLoop ¶ added in v1.5.0
func NewInfiniteLoop(stream ReadSeekCloser, size int64) *InfiniteLoop
NewInfiniteLoop creates a new infinite loop stream with a stream and size in bytes.
func (*InfiniteLoop) Close ¶ added in v1.5.0
func (l *InfiniteLoop) Close() error
Close is implementation of ReadSeekCloser.
type Player ¶
type Player struct {
// contains filtered or unexported fields
}
Player is an audio player which has one stream.
func NewPlayer ¶
func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error)
NewPlayer creates a new player with the given stream.
src's format must be linear PCM (16bits little endian, 2 channel stereo) without a header (e.g. RIFF header). The sample rate must be same as that of the audio context.
Note that the given src can't be shared with other Players.
NewPlayer tries to rewind src by calling Seek to get the current position. NewPlayer returns error when the Seek returns error.
func NewPlayerFromBytes ¶ added in v1.5.0
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.
NewPlayerFromBytes's error is always nil as of 1.5.0-alpha.
func (*Player) Close ¶
Close closes the stream. Ths source stream passed by NewPlayer will also be closed.
When closing, the stream owned by the player will also be closed by calling its Close.
Close is concurrent safe.
Close returns error when closing the source returns error.
func (*Player) IsPlaying ¶
IsPlaying returns boolean indicating whether the player is playing.
IsPlaying is concurrent safe.
func (*Player) Pause ¶
Pause pauses the playing.
Pause is concurrent safe.
Pause always returns nil.
func (*Player) Rewind ¶
Rewind rewinds the current position to the start.
Rewind is concurrent safe.
Rewind returns error when seeking the source returns error.
func (*Player) Seek ¶
Seek seeks the position with the given offset.
Seek is concurrent safe.
Seek returns error when seeking the source returns error.
type ReadSeekCloser ¶
type ReadSeekCloser interface { io.ReadSeeker io.Closer }
ReadSeekCloser is an io.ReadSeeker and io.Closer.
func BytesReadSeekCloser ¶ added in v1.5.0
func BytesReadSeekCloser(b []uint8) ReadSeekCloser
BytesReadSeekCloser creates ReadSeekCloser from bytes.
A returned stream is concurrent safe.