WhisperCpp.Pcm (whisper_cpp v0.2.0)

Copy Markdown View Source

Helpers for slicing little-endian f32 PCM buffers without re-decoding the source audio.

slice/4 does the byte math (4 bytes per sample at sample_rate samples/second) and bounds-checks against the buffer size.

Summary

Functions

Returns the audio duration of an f32-PCM buffer in seconds.

Returns the f32-PCM bytes for [start_s, start_s + duration_s).

Functions

duration_s(samples, sample_rate)

@spec duration_s(binary(), pos_integer()) :: float()

Returns the audio duration of an f32-PCM buffer in seconds.

slice(samples, sample_rate, start_s, duration_s)

@spec slice(binary(), pos_integer(), number(), number()) ::
  {:ok, binary()} | {:error, WhisperCpp.Error.t()}

Returns the f32-PCM bytes for [start_s, start_s + duration_s).

samples is a binary of little-endian f32 samples at sample_rate samples/second. Both start_s and duration_s are seconds; either integer or float.

Returns {:error, %WhisperCpp.Error{reason: :invalid_request}} when:

  • start_s < 0;
  • duration_s is zero or negative, or rounds to zero samples;
  • the requested window is not fully contained in the buffer.

The range is not clamped - slicing past the end is a caller bug.