oto

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

Oto (v2)

Go Reference

A low-level library to play sound.

Platforms

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • OpenBSD
  • Android
  • iOS
  • WebAssembly

Prerequisite

macOS

Oto requies AudioToolbox.framework, but this is automatically linked.

iOS

Oto requies these frameworks:

  • AVFoundation.framework
  • AudioToolbox.framework

Add them to "Linked Frameworks and Libraries" on your Xcode project.

Linux

ALSA is required. On Ubuntu or Debian, run this command:

apt install libasound2-dev

In most cases this command must be run by root user or through sudo command.

FreeBSD, OpenBSD

BSD systems are not tested well. If ALSA works, Oto should work.

Crosscompiling

To crosscompile, make sure the libraries for the target architecture are installed, and set CGO_ENABLED=1 as Go disables Cgo on crosscompiles by default.

Documentation

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
}

Context is the main object in Oto. It interacts with the audio drivers.

To play sound with Oto, first create a context. Then use the context to create an arbitrary number of players. Then use the players to play sound.

There can only be one context at any time. Closing a context and opening a new one is allowed.

func NewContext

func NewContext(sampleRate int, channelNum int, bitDepthInBytes int) (*Context, chan struct{}, error)

NewContext creates a new context, that creates and holds ready-to-use Player objects, and returns a context, a channel that is closed when the context is ready, and an error if it exists.

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 bitDepthInBytes argument specifies the number of bytes per sample per channel. The usual value is 2. Only values 1 and 2 are supported.

func (*Context) NewPlayer

func (c *Context) NewPlayer(r io.Reader) Player

NewPlayer creates a new, ready-to-use Player belonging to the Context.

The r's format is as follows:

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

Byte ordering is little endian.

A player has some amount of an underlying buffer. Read data from r is queued to the player's underlying buffer. The underlying buffer is consumed by its playing. Then, r's position and the current playing position don't necessarily match. If you want to clear the underlying buffer for some reasons e.g., you want to seek the position of r, call the player's Reset function.

You cannot share r by multiple players.

NewPlayer is concurrent-safe.

All the functions of a Player returned by NewPlayer are concurrent-safe.

func (*Context) Resume

func (c *Context) Resume() error

Resume resumes the entire audio play, which was suspended by Suspend.

Resume is concurrent-safe.

func (*Context) Suspend

func (c *Context) Suspend() error

Suspend suspends the entire audio play.

Suspend is concurrent-safe.

type Player

type Player interface {
	// Pause pauses its playing.
	Pause()

	// Play starts its playing if it doesn't play.
	Play()

	// IsPlaying reports whether this player is playing.
	IsPlaying() bool

	// Reset clears the underyling buffer and pauses its playing.
	Reset()

	// Volume returns the current volume in the range of [0, 1].
	// The default volume is 1.
	Volume() float64

	// SetVolume sets the current volume in the range of [0, 1].
	SetVolume(volume float64)

	// UnplayedBufferSize returns the byte size in the underlying buffer that is not played yet.
	UnplayedBufferSize() int

	// Err returns an error if this player has an error.
	Err() error

	io.Closer
}

Player is a PCM (pulse-code modulation) audio player.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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