Documentation
¶
Overview ¶
Package audio plays sound: effects decoded into memory, music streamed as it plays, and sound an app makes itself, mixed in Go and sent to the system's audio output:
click, err := audio.DecodeSound(clickWAV) // decoded once ... click.Play() // as often as it sounds, over itself music, err := audio.OpenStream(f) // decoded as it plays music.SetLoop(true).SetVolume(0.6).Play()
WAV decodes out of the box; import audio/vorbis for Ogg Vorbis and audio/mp3 for MP3, which register themselves as image's formats do.
The output opens when something first plays and closes a few seconds after everything stopped. Where no output can open, players still play on time, unheard, and Err says why. Every function and method is safe to call from any goroutine.
Index ¶
- Variables
- func Err() error
- func RegisterFormat(name, magic string, open func(r io.ReadSeeker) (Decoder, error))
- func SetLatency(l Latency)
- func SetVolume(v float64)
- func Volume() float64
- type Decoder
- type Latency
- type Player
- func (p *Player) Close()
- func (p *Player) Duration() time.Duration
- func (p *Player) Loop() bool
- func (p *Player) OnEnd(fn func()) (off func())
- func (p *Player) Pan() float64
- func (p *Player) Pause()
- func (p *Player) Play()
- func (p *Player) Playing() bool
- func (p *Player) Position() time.Duration
- func (p *Player) Seek(t time.Duration)
- func (p *Player) SetLoop(loop bool) *Player
- func (p *Player) SetLoopRange(start, end time.Duration) *Player
- func (p *Player) SetPan(pan float64) *Player
- func (p *Player) SetSpeed(s float64) *Player
- func (p *Player) SetVolume(v float64) *Player
- func (p *Player) Speed() float64
- func (p *Player) Stop()
- func (p *Player) Volume() float64
- type Sound
Constants ¶
This section is empty.
Variables ¶
var ErrFormat = errors.New("audio: unknown format")
ErrFormat reports data of no format registered.
Functions ¶
func Err ¶
func Err() error
Err returns why the last output to open is not the system's audio output, as on a machine with none, or nil.
func RegisterFormat ¶
func RegisterFormat(name, magic string, open func(r io.ReadSeeker) (Decoder, error))
RegisterFormat registers a format for DecodeSound, OpenStream and NewDecoder: magic is the start of its files, where '?' matches any byte, and open returns a decoder reading r from its start. A package for a format calls it in its init function.
func SetLatency ¶ added in v0.1.13
func SetLatency(l Latency)
SetLatency sets how far ahead the output mixes. An output already open opens again, which drops what it held.
Types ¶
type Decoder ¶
type Decoder interface {
// SampleRate returns the frames per second.
SampleRate() int
// Channels returns the samples per frame: 1 for mono, 2 for stereo
// (left, right); later channels of more are not played.
Channels() int
// Read decodes the next samples into buf, a whole number of frames,
// and returns how many it wrote, with io.EOF at the end.
Read(buf []float32) (int, error)
// SetPosition moves to frame, counted from the start.
SetPosition(frame int64) error
// Len returns the frames from the start to the end, or -1 if unknown.
Len() int64
}
A Decoder decodes audio as interleaved float32 samples in [-1, 1], one per channel for each frame. Formats registered with RegisterFormat return one; implement one to play sound an app generates itself.
func NewDecoder ¶
func NewDecoder(r io.ReadSeeker) (Decoder, string, error)
NewDecoder returns a decoder of r's format, and the format's name.
type Latency ¶ added in v0.1.13
type Latency int
Latency is how far ahead of what is heard the output mixes, as Web Audio's latencyHint.
const ( // LatencyInteractive mixes about 40 ms ahead, so that sounds start, // stop and change as soon as they are asked to, as games and // interfaces want. It is the default. LatencyInteractive Latency = iota // LatencyPlayback mixes about 250 ms ahead, for apps that play music // and other long audio: it plays on through the moments the system // keeps an app waiting, as macOS does while its appearance changes, // where the interactive latency runs dry and is heard to stop. Plays, // pauses, seeks and volume changes are heard that much later. LatencyPlayback )
type Player ¶
type Player struct {
// contains filtered or unexported fields
}
A Player plays a sound or a stream: its position, volume, pan, speed and looping are its own.
func NewPlayer ¶
NewPlayer returns a paused player decoding d as it plays, about a second ahead. d is used by one goroutine at a time. Closing the player closes d if d is an io.Closer.
func OpenStream ¶
func OpenStream(r io.ReadSeeker) (*Player, error)
OpenStream returns a paused player decoding r as it plays, for music and other long audio: it decodes about a second ahead. Closing the player closes r if r is an io.Closer.
func (*Player) Close ¶
func (p *Player) Close()
Close stops the player for good and frees what it decodes with. A sound's players need no closing.
func (*Player) OnEnd ¶ added in v0.1.13
func (p *Player) OnEnd(fn func()) (off func())
OnEnd calls fn on a goroutine of its own each time the player plays to its end, which a looping player never does, as a playlist plays the next track then. off removes it.
func (*Player) Play ¶
func (p *Player) Play()
Play plays from where the player is, or from the start if it ended.
func (*Player) Playing ¶
Playing reports whether the player plays: false once paused, stopped, closed, or at its end.
func (*Player) Position ¶
Position returns how far the player is from the start, in what is heard: the output mixes ahead of it (SetLatency), and the player goes on for as long after a pause, but it is where Seek moved it at once.
func (*Player) SetLoop ¶
SetLoop sets whether the player starts again from the start when it ends, without a gap, or from its loop's start (SetLoopRange). A change is heard after what the player decoded ahead: a few milliseconds of a sound, up to a second of a stream.
func (*Player) SetLoopRange ¶
SetLoopRange loops the player between start and end, without a gap, end 0 being the end of its audio: what is before start, as a tune's intro, plays once, and playing past end goes on from start. It sets the player looping; SetLoop(false) plays on past end, as far as the end.
func (*Player) SetPan ¶
SetPan places the player between the left speaker (-1) and the right (1); 0, the default, is the middle. A mono sound pans keeping its power; a stereo one keeps both channels, as Web Audio's StereoPannerNode does.
func (*Player) SetSpeed ¶
SetSpeed sets how fast the player plays, which changes its pitch with it, as a record's speed does: 1 (the default) as decoded, 2 twice as fast and an octave higher. Games vary it a little for effects that play often.
type Sound ¶
type Sound struct {
// contains filtered or unexported fields
}
A Sound is audio decoded into memory, for effects that play often and over themselves: each Play is a new Player.
func DecodeSound ¶
DecodeSound decodes data of a registered format into memory.
func NewSound ¶
NewSound returns a sound of samples, interleaved frames of 1 (mono) or 2 (stereo) channels at sampleRate frames per second, as an app generates them. The sound keeps samples: don't change them afterwards.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package mp3 decodes MP3 files for package audio, with github.com/hajimehoshi/go-mp3.
|
Package mp3 decodes MP3 files for package audio, with github.com/hajimehoshi/go-mp3. |
|
Package vorbis decodes Ogg Vorbis files for package audio, with github.com/jfreymuth/oggvorbis.
|
Package vorbis decodes Ogg Vorbis files for package audio, with github.com/jfreymuth/oggvorbis. |