Documentation ¶
Index ¶
- Constants
- Variables
- func Init(d Driver) error
- func InitDefault() error
- func Play(ctx context.Context, dst Writer, src Reader, options ...PlayOption) error
- func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)
- func ReadFull(r Reader, buf []byte) (n int, err error)
- type BytesReader
- type Driver
- type Format
- type Formatted
- type IOReader
- type LoopingReader
- type PlayOption
- type PlayOptions
- type Reader
- type Writer
Constants ¶
const WriterBufferLengthInSeconds = 1
WriterBufferLengthInSeconds defines how much data os-level writers provided by this package will rotate through in a theoretical circular buffer.
Variables ¶
var ErrMismatchedPCMFormat = fmt.Errorf("source and destination have differing PCM formats")
ErrMismatchedPCMFormat will be returned by operations streaming from Readers to Writers where the PCM formats of those Readers and Writers are not equivalent.
Functions ¶
func InitDefault ¶
func InitDefault() error
InitDefault calls Init with the following value by OS: windows: DriverDirectSound linux,osx: DriverPulse
func Play ¶
Play will copy data from the provided src to the provided dst until ctx is cancelled. This copy is not constant. The copy will occur in two phases: first, an initial population of the writer to give distance between the read cursor and write cursor; immediately upon this write, the writer should begin playback. Following this setup, a sub-second amount of data will streamed from src to dst after waiting that same duration. These wait times can be configured via PlayOptions.
func ReadAtLeast ¶
ReadAtLeast acts like io.ReadAtLeast with a pcm Reader. It will read until at least min bytes have been read into the provided buffer.
Types ¶
type BytesReader ¶
A BytesReader acts like a bytes.Buffer for converting raw []bytes into pcm Readers.
type Driver ¶
type Driver int
A Driver defines the underlying interface that should be used for initializing PCM audio writers by this package.
type Format ¶
Format is a PCM format. Equivalent to klang.Format.
func (Format) BytesPerSecond ¶
BytesPerSecond returns how many bytes this format would be encoded into per second in an audio stream.
type Formatted ¶
type Formatted interface { // PCMFormat will return the Format used by an encoded audio or expected by an audio consumer. // Implementations can embed a Format struct to simplify this. PCMFormat() Format }
The Formatted interface represents types that are aware of a PCM Format they expect or provide.
type LoopingReader ¶
type LoopingReader struct { Reader // contains filtered or unexported fields }
A LoopingReader will read from Reader continually, even after it has been fully consumed. The data read from the reader will be cached after read within the LoopingReader structure, potentially inflating memory if provided a large stream.
type PlayOption ¶
type PlayOption func(*PlayOptions)
A PlayOption sets some value on a PlayOptions struct.
type PlayOptions ¶
type PlayOptions struct { // The span of data that should be copied from reader to writer // at a time. If too low, may lose accuracy on windows. If too high, // may require manual resets when changing audio sources. // Defaults to 125 Milliseconds. CopyIncrement time.Duration // How many increments should make up the time between our read and write // cursors-- i.e. the audio will be playing at X and we will be writing to // X + ChaseIncrements * CopyIncrement. // This must be at least 2 to avoid the read and write buffers clipping. // Defaults to 2. ChaseIncrements int // If AllowMismatchedFormats is false, Play will error when a reader's PCM format // disagrees with a writer's expected PCM format. Defaults to false. AllowMismatchedFormats bool }
PlayOptions define ways to configure how playback of some audio proceeds
type Reader ¶
A Reader mimics io.Reader for pcm data.
func LoopReader ¶
LoopReader will cache read bytes as they are read and resend them after the reader returns EOF.
type Writer ¶
type Writer interface { io.Closer Formatted // WritePCM expects PCM bytes matching the format this speaker was initialized with. // WritePCM will block until all of the bytes are consumed. WritePCM([]byte) (n int, err error) // Reset must clear out any written data from buffers, without stopping playback // TODO: do we need this? Reset() error }
A Writer can have PCM formatted audio data written to it. It mimics io.Writer.