webpcm

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 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 webpcm is the part of a pull PCM output over Web Audio that touches no JavaScript: the chunk geometry for a latency, the ring of rendered chunks, the timeline of chunks scheduled on an AudioContext clock, and the state machine of starting, pausing, flushing and closing. The web backend (internal/platform/web) and the mini game backend (internal/platform/miniprogram, tagged nsgominigame) drive it with the JavaScript their hosts have.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeInterleavedFloat32LE

func EncodeInterleavedFloat32LE(dst []byte, src []float32)

EncodeInterleavedFloat32LE encodes interleaved Float32 samples into little-endian bytes for bulk transfer to a JS Float32Array via CopyBytesToJS.

func EncodePlanarChannelFloat32LE

func EncodePlanarChannelFloat32LE(dst []byte, interleaved []float32, channel, channels, frames int)

EncodePlanarChannelFloat32LE extracts one channel from interleaved Float32 samples into little-endian bytes for AudioBuffer.copyToChannel in the AudioBufferSourceNode fallback path.

func OutputClock

func OutputClock(curSec, tsContextSec, tsPerfMs, perfNowMs, outputLatencySec float64, wallNow time.Time) (float64, time.Time)

OutputClock resolves the audio presentation clock (in AudioContext seconds) and corresponding wall-clock reference from AudioContext.currentTime and AudioContext.getOutputTimestamp(). When getOutputTimestamp reports a valid positive contextTime it is preferred; otherwise currentTime minus outputLatency is used.

Types

type Geometry

type Geometry struct {
	ChunkFrames int

	RingCapFrames  int
	ReportFrames   int
	PumpIntervalMs int
	// contains filtered or unexported fields
}

Geometry holds the derived frame counts and scheduling cadence for a Web pull PCM stream.

type State

type State struct {
	// Geom is the stream's chunk size, ring capacity, progress cadence and pump interval.
	Geom Geometry

	// Active says the stream was started and not paused since; Pausing that it was paused, or opened paused, on purpose, so a suspended context is no interruption; Closed that it is closed for good; SeenRunning that its context ran once.
	Active      bool
	Pausing     bool
	Closed      bool
	SeenRunning bool

	// Underruns counts short renders and the underruns a worklet reported.
	Underruns int
	// contains filtered or unexported fields
}

State encapsulates the platform-independent state machine for Web pull PCM output: ring buffering, short-fill silence padding, underrun accounting, monotonic position tracking, flush/pause/close transitions, latency reporting and AudioContext statechange interruption classification.

func NewState

func NewState(sampleRate, channels int, latency time.Duration) *State

NewState is a paused stream of sampleRate and channels, its chunks sized for latency (24 ms when zero).

func (*State) AddWorkletUnderruns

func (s *State) AddWorkletUnderruns(n int)

AddWorkletUnderruns counts n underruns an AudioWorklet reported.

func (*State) Close

func (s *State) Close(nowSec float64, wallNow time.Time) bool

Close pauses the stream for good, reporting false when it was already closed.

func (*State) Flush

func (s *State) Flush(nowSec float64, wallNow time.Time) int

Flush drops what was rendered but not yet heard, returning the client frames dropped, and keeps the stream as active as it was.

func (*State) Latency

func (s *State) Latency(baseSec, outputSec, nowSec float64) time.Duration

Latency is the context's base and output latency plus what is queued at nowSec, zero before the first render.

func (*State) NeededChunks

func (s *State) NeededChunks(nowSec float64) int

NeededChunks is how many chunks to render now, at context time nowSec, to keep the latency's worth of audio queued.

func (*State) Next

func (s *State) Next(dst []float32, nowSec float64) (frames int, startSec float64, ok bool)

Next takes the oldest rendered chunk from the ring into dst and schedules it on the timeline, no earlier than nowSec on the context's clock. It reports the chunk's frames and the context time it starts at, and false when the ring holds no chunk or dst cannot hold it.

func (*State) OnContextState

func (s *State) OnContextState(state string) (bool, error)

OnContextState updates statechange tracking for an AudioContext state name ("running", "suspended", "interrupted", "closed") and reports whether the transition is an unexpected interruption that should pause the stream and notify OnInterrupt.

func (*State) Pause

func (s *State) Pause(nowSec float64, wallNow time.Time) int

Pause freezes the position at the context time nowSec and drops what was rendered but not yet heard, returning the client frames dropped.

func (*State) Position

func (s *State) Position(nowSec float64, wallNow time.Time) (int64, time.Time)

Position is the frames heard by context time nowSec and the wall time the last of them was heard.

func (*State) PullIntoRing

func (s *State) PullIntoRing(render func([]float32) int) (frames, produced int, underrun bool)

PullIntoRing invokes render for one chunk, zero-pads any short fill, increments underruns on short fill, and pushes the chunk into the ring buffer.

func (*State) Start

func (s *State) Start() error

Start makes the stream active, failing once it is closed.

Jump to

Keyboard shortcuts

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