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 ¶
- func EncodeInterleavedFloat32LE(dst []byte, src []float32)
- func EncodePlanarChannelFloat32LE(dst []byte, interleaved []float32, channel, channels, frames int)
- func OutputClock(curSec, tsContextSec, tsPerfMs, perfNowMs, outputLatencySec float64, ...) (float64, time.Time)
- type Geometry
- type State
- func (s *State) AddWorkletUnderruns(n int)
- func (s *State) Close(nowSec float64, wallNow time.Time) bool
- func (s *State) Flush(nowSec float64, wallNow time.Time) int
- func (s *State) Latency(baseSec, outputSec, nowSec float64) time.Duration
- func (s *State) NeededChunks(nowSec float64) int
- func (s *State) Next(dst []float32, nowSec float64) (frames int, startSec float64, ok bool)
- func (s *State) OnContextState(state string) (bool, error)
- func (s *State) Pause(nowSec float64, wallNow time.Time) int
- func (s *State) Position(nowSec float64, wallNow time.Time) (int64, time.Time)
- func (s *State) PullIntoRing(render func([]float32) int) (frames, produced int, underrun bool)
- func (s *State) Start() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EncodeInterleavedFloat32LE ¶
EncodeInterleavedFloat32LE encodes interleaved Float32 samples into little-endian bytes for bulk transfer to a JS Float32Array via CopyBytesToJS.
func EncodePlanarChannelFloat32LE ¶
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 ¶
NewState is a paused stream of sampleRate and channels, its chunks sized for latency (24 ms when zero).
func (*State) AddWorkletUnderruns ¶
AddWorkletUnderruns counts n underruns an AudioWorklet reported.
func (*State) Flush ¶
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 ¶
Latency is the context's base and output latency plus what is queued at nowSec, zero before the first render.
func (*State) NeededChunks ¶
NeededChunks is how many chunks to render now, at context time nowSec, to keep the latency's worth of audio queued.
func (*State) Next ¶
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 ¶
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 ¶
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 ¶
Position is the frames heard by context time nowSec and the wall time the last of them was heard.
func (*State) PullIntoRing ¶
PullIntoRing invokes render for one chunk, zero-pads any short fill, increments underruns on short fill, and pushes the chunk into the ring buffer.