audio

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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 SetVolume

func SetVolume(v float64)

SetVolume sets the volume of everything played, a gain: 1 (the default) as decoded, 0 silent.

func Volume

func Volume() float64

Volume returns the volume of everything played.

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 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

func NewPlayer(d Decoder) *Player

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) Duration

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

Duration returns how long the player's audio is, or 0 if unknown.

func (*Player) Loop

func (p *Player) Loop() bool

Loop reports whether the player loops.

func (*Player) Pan

func (p *Player) Pan() float64

Pan returns the player's pan.

func (*Player) Pause

func (p *Player) Pause()

Pause pauses the player where it is.

func (*Player) Play

func (p *Player) Play()

Play plays from where the player is, or from the start if it ended.

func (*Player) Playing

func (p *Player) Playing() bool

Playing reports whether the player plays: false once paused, stopped, closed, or at its end.

func (*Player) Position

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

Position returns how far the player is from the start.

func (*Player) Seek

func (p *Player) Seek(t time.Duration)

Seek moves the player to t from the start.

func (*Player) SetLoop

func (p *Player) SetLoop(loop bool) *Player

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

func (p *Player) SetLoopRange(start, end time.Duration) *Player

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

func (p *Player) SetPan(pan float64) *Player

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

func (p *Player) SetSpeed(s float64) *Player

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.

func (*Player) SetVolume

func (p *Player) SetVolume(v float64) *Player

SetVolume sets the player's volume, a gain: 1 (the default) as decoded, 0 silent. Changes ramp over a few milliseconds, without a click.

func (*Player) Speed

func (p *Player) Speed() float64

Speed returns the player's speed.

func (*Player) Stop

func (p *Player) Stop()

Stop pauses the player and moves it to the start.

func (*Player) Volume

func (p *Player) Volume() float64

Volume returns the player's volume.

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

func DecodeSound(data []byte) (*Sound, error)

DecodeSound decodes data of a registered format into memory.

func NewSound

func NewSound(sampleRate, channels int, samples []float32) *Sound

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.

func ReadSound

func ReadSound(d Decoder) (*Sound, error)

ReadSound decodes everything d has into memory.

func (*Sound) Duration

func (s *Sound) Duration() time.Duration

Duration returns how long the sound plays.

func (*Sound) NewPlayer

func (s *Sound) NewPlayer() *Player

NewPlayer returns a paused player of the sound.

func (*Sound) Play

func (s *Sound) Play() *Player

Play plays the sound from its start, over any other of its players, and returns the player playing it.

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.

Jump to

Keyboard shortcuts

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