oto: github.com/hajimehoshi/oto Index | Files

package oto

import "github.com/hajimehoshi/oto"

Package oto offers io.Writer to play sound on multiple platforms.


Package Files

player.go player_linux.go

type Player Uses

type Player struct {
    // contains filtered or unexported fields

Player is a PCM (pulse-code modulation) audio player. It implements io.Writer, use Write method to play samples.

func NewPlayer Uses

func NewPlayer(sampleRate, channelNum, bytesPerSample, bufferSizeInBytes int) (*Player, error)

NewPlayer creates a new, ready-to-use Player.

The sampleRate argument specifies the number of samples that should be played during one second. Usual numbers are 44100 or 48000.

The channelNum argument specifies the number of channels. One channel is mono playback. Two channels are stereo playback. No other values are supported.

The bytesPerSample argument specifies the number of bytes per sample per channel. The usual value is 2. Only values 1 and 2 are supported.

The bufferSizeInBytes argument specifies the size of the buffer of the Player. This means, how many bytes can Player remember before actually playing them. Bigger buffer can reduce the number of Write calls, thus reducing CPU time. Smaller buffer enables more precise timing. The longest delay between when samples were written and when they started playing is equal to the size of the buffer.

func (*Player) Close Uses

func (p *Player) Close() error

Close closes the Player and frees any resources associated with it. The Player is no longer usable after calling Close.

func (*Player) SetUnderrunCallback Uses

func (p *Player) SetUnderrunCallback(f func())

SetUnderrunCallback sets a function which will be called whenever an underrun occurs. This is mostly for debugging and optimization purposes.

Underrun occurs when not enough samples is written to the player in a certain amount of time and thus there's nothing to play. This usually happens when there's too much audio data processing, or the audio data processing code gets stuck for a while, or the player's buffer is too small.


player.SetUnderrunCallback(func() {
    log.Println("UNDERRUN, YOUR CODE IS SLOW")

Supported platforms: Linux.

func (*Player) Write Uses

func (p *Player) Write(data []uint8) (int, error)

Write writes PCM samples to the Player.

The format is as follows:

[data]      = [sample 1] [sample 2] [sample 3] ...
[sample *]  = [channel 1] ...
[channel *] = [byte 1] [byte 2] ...

Byte ordering is little endian.

The data is first put into the Player's buffer. Once the buffer is full, Player starts playing the data and empties the buffer.

If the supplied data doesn't fit into the Player's buffer, Write block until a sufficient amount of data has been played (or at least started playing) and the remaining unplayed data fits into the buffer.

Note, that the Player won't start playing anything until the buffer is full.

Package oto imports 4 packages (graph) and is imported by 6 packages. Updated 2017-08-19. Refresh now. Tools for package owners.