audio

package
v1.4.0-rc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2016 License: Apache-2.0 Imports: 8 Imported by: 0

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.

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

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.

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

func NewContext(sampleRate int) (*Context, error)

NewContext creates a new audio context with the given sample rate (e.g. 44100).

func (*Context) SampleRate

func (c *Context) SampleRate() int

SampleRate returns the sample rate. All audio source must have the same sample rate.

This function is concurrent-safe.

func (*Context) Update

func (c *Context) Update() error

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.

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.

This function is concurrent-safe.

func NewPlayerFromBytes

func NewPlayerFromBytes(context *Context, src []byte) (*Player, error)

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.

This function is concurrent-safe.

func (*Player) Close

func (p *Player) Close() error

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.

This function is concurrent-safe.

func (*Player) Current

func (p *Player) Current() time.Duration

Current returns the current position.

This function is concurrent-safe.

func (*Player) IsPlaying

func (p *Player) IsPlaying() bool

IsPlaying returns boolean indicating whether the player is playing.

This function is concurrent-safe.

func (*Player) Pause

func (p *Player) Pause() error

Pause pauses the playing.

This function is concurrent-safe.

func (*Player) Play

func (p *Player) Play() error

Play plays the stream.

This function is concurrent-safe.

func (*Player) Rewind

func (p *Player) Rewind() error

Rewind rewinds the current position to the start.

This function is concurrent-safe.

func (*Player) Seek

func (p *Player) Seek(offset time.Duration) error

Seek seeks the position with the given offset.

This function is concurrent-safe.

func (*Player) SetVolume

func (p *Player) SetVolume(volume float64)

SetVolume sets the volume of this player. volume must be in between 0 and 1. This function panics otherwise.

func (*Player) Volume

func (p *Player) Volume() float64

Volume returns the current volume of this player [0-1].

type ReadSeekCloser

type ReadSeekCloser interface {
	io.ReadSeeker
	io.Closer
}

ReadSeekCloser is an io.ReadSeeker and io.Closer.

Directories

Path Synopsis
internal
Package wav provides WAV (RIFF) decoder.
Package wav provides WAV (RIFF) decoder.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL