player

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine runs audio playback in a background goroutine.

func NewEngine

func NewEngine(opts Options) *Engine

func (*Engine) Close

func (e *Engine) Close()

func (*Engine) Events

func (e *Engine) Events() <-chan Event

func (*Engine) Play

func (e *Engine) Play(path string)

func (*Engine) SeekTo

func (e *Engine) SeekTo(pos time.Duration) SeekResult

func (*Engine) SetVolume

func (e *Engine) SetVolume(level int)

SetVolume sets the playback volume (0-100).

func (*Engine) Stop

func (e *Engine) Stop()

func (*Engine) TogglePause

func (e *Engine) TogglePause()

type Event

type Event struct {
	Type EventType
	Path string
	Err  error
	Dur  time.Duration
}

type EventType

type EventType string
const (
	EventTrackStarted EventType = "track_started"
	EventTrackEnded   EventType = "track_ended"
	EventTrackError   EventType = "track_error"
	EventTrackPaused  EventType = "track_paused"
	EventTrackResumed EventType = "track_resumed"
	EventTrackStopped EventType = "track_stopped"
)

type OggCodec

type OggCodec interface {
	// SampleRate returns the audio sample rate.
	SampleRate() int

	// Channels returns the number of audio channels.
	Channels() int

	// PreSkip returns samples to skip at stream start (0 for Vorbis).
	PreSkip() int

	// GranuleToSamples converts granule position to sample count.
	GranuleToSamples(granule int64) int64

	// AddHeaderPacket adds a header packet for codecs that need multiple headers.
	// Returns true when all headers are received.
	// For Opus, this is a no-op (single header). For Vorbis, collects 3 headers.
	AddHeaderPacket(packet []byte) (complete bool, err error)

	// Decode decodes a packet into PCM samples.
	// Returns the number of samples per channel decoded.
	Decode(packet []byte, pcm []float32) (samplesPerChannel int, err error)

	// Reset resets decoder state (needed after seeking).
	Reset() error
}

OggCodec handles codec-specific initialization and decoding for Ogg streams.

type OggPage

type OggPage struct {
	GranulePos int64
	Packets    [][]byte
	ByteOffset int64
}

OggPage represents a decoded Ogg page with its audio packets.

type OggReader

type OggReader struct {
	// contains filtered or unexported fields
}

OggReader reads Ogg streams with seeking support. It is codec-agnostic - the caller is responsible for parsing codec headers.

func NewOggReader

func NewOggReader(r io.ReadSeeker, sampleRate, preSkip int) (*OggReader, error)

NewOggReader creates a new OggReader from a seekable stream. sampleRate and preSkip are needed for duration/seeking calculations. The caller is responsible for parsing codec headers before calling this.

func (*OggReader) Duration

func (o *OggReader) Duration() int64

Duration returns the total number of audio samples (excluding pre-skip).

func (*OggReader) PreSkip

func (o *OggReader) PreSkip() int

PreSkip returns the number of samples to skip at the start.

func (*OggReader) ReadPage

func (o *OggReader) ReadPage() (*OggPage, error)

ReadPage reads the next Ogg page from the stream. Handles packets that span multiple pages by joining partial data. Returns io.EOF when no more pages are available.

func (*OggReader) Reset

func (o *OggReader) Reset() error

Reset seeks back to the start of audio data.

func (*OggReader) SampleRate

func (o *OggReader) SampleRate() int

SampleRate returns the sample rate used for duration calculations.

func (*OggReader) ScanLastGranule

func (o *OggReader) ScanLastGranule() error

ScanLastGranule finds the granule position of the last page. This should be called after header parsing to enable duration calculation.

func (*OggReader) SeekToGranule

func (o *OggReader) SeekToGranule(target int64) error

SeekToGranule seeks to the page containing or just before the target granule position. Uses bisection search for efficiency on large files.

func (*OggReader) SetDataStart

func (o *OggReader) SetDataStart(offset int64)

SetDataStart sets the byte offset where audio data begins. Called after header parsing is complete.

type Options

type Options struct {
	SampleRate      int
	ResampleQuality int
	BufferDuration  time.Duration
}

Options configures the audio engine.

type SeekResult

type SeekResult struct {
	Ok  bool
	Pos time.Duration
	Dur time.Duration
}

Jump to

Keyboard shortcuts

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