Module Swresample.Make

Converters that read values of type I.t and return values of type O.t.

Parameters

module I : AudioData
module O : AudioData

Signature

type t = (I.t, O.t) ctx
val create : ?options:options list -> Avutil.Channel_layout.t -> ?in_sample_format:Avutil.Sample_format.t -> int -> Avutil.Channel_layout.t -> ?out_sample_format:Avutil.Sample_format.t -> int -> t

create ?options in_layout ?in_sample_format in_rate out_layout ?out_sample_format out_rate creates a converter. The positional arguments are, in order: the input channel layout, the input sample rate in Hz, the output channel layout and the output sample rate in Hz.

The sample format of each side comes from its data module. When the module fixes a format, as S16Bytes or FloatArray do, the optional argument is omitted, or equal to that format. When the module leaves the format open, as Bytes and Frame do, the optional argument is required. With Bytes the format is a packed (interleaved) one.

Both layouts are copied. Every other setting of the resampler keeps FFmpeg's default. The call releases the OCaml runtime lock while the resampler is initialised.

  • parameter options

    Settings applied in list order, none by default. A later element of the same type overrides an earlier one.

  • parameter in_sample_format

    The sample format of the input values.

  • parameter out_sample_format

    The sample format of the values returned.

  • raises Avutil.Error

    with `Failure _ when a sample format is missing, differs from the one the data module fixes or is planar for Bytes, and when a sample rate is below 1; with the FFmpeg error when FFmpeg rejects a setting or fails to initialise the resampler.

val from_codec : ?options:options list -> Avutil.audio Avcodec.params -> Avutil.Channel_layout.t -> ?out_sample_format:Avutil.Sample_format.t -> int -> t

from_codec ?options params out_layout ?out_sample_format out_rate is create with the input channel layout, sample format and sample rate read from the codec parameters params. The format read is given as in_sample_format: creation fails when I fixes another format.

val to_codec : ?options:options list -> Avutil.Channel_layout.t -> ?in_sample_format:Avutil.Sample_format.t -> int -> Avutil.audio Avcodec.params -> t

to_codec ?options in_layout ?in_sample_format in_rate params is create with the output channel layout, sample format and sample rate read from the codec parameters params. The format read is given as out_sample_format: creation fails when O fixes another format.

val from_codec_to_codec : ?options:options list -> Avutil.audio Avcodec.params -> Avutil.audio Avcodec.params -> t

from_codec_to_codec ?options in_params out_params is create with the channel layout, sample format and sample rate of the input read from in_params and those of the output read from out_params. Creation fails when a data module fixes a format other than the one read.

val convert : ?offset:int -> ?length:int -> t -> I.t -> O.t

convert ?offset ?length converter input converts samples of input and returns the samples the resampler produces, as a new value.

The result holds exactly the samples produced. Their number is known only after the conversion: the resampler keeps samples buffered between calls, and flush returns the ones it still holds. It can be 0, and the result is then an empty value of the same shape: empty planes, or a frame of zero samples. A frame returned has the output channel layout, sample format and sample rate, and no timestamp.

input is checked against the input side of the converter. A planar value has one plane per channel, all of the same length. A frame has the channel count and the sample format of the converter; its sample rate and the arrangement of its channel layout are the caller's to match. A trailing partial sample group of an interleaved value is ignored.

Bytes and float arrays are copied in and out. A NaN read from a float array, and a NaN written to one, becomes 0. Bigarrays and frames are read in place while the OCaml runtime lock is released, without any NaN replacement: what another thread writes to them during the call is visible to the conversion.

The call excludes every other operation on the converter: a concurrent one raises Error (`Failure "Object in use!") at once. After a failure the converter stays usable.

  • parameter offset

    The first sample converted, counted in samples per channel. 0 by default.

  • parameter length

    The number of samples per channel converted. By default, everything after offset.

  • raises Avutil.Error

    with `Failure _ when offset or length is negative, when offset + length exceeds the samples per channel input holds and when input has not the shape described above; with the FFmpeg error when the conversion fails.

val flush : t -> O.t

flush converter returns the samples the resampler still holds: the tail a rate conversion keeps back until it knows that no input follows. After it, the samples returned by all the calls since creation are everything the input produces.

A second flush with no conversion in between returns an empty value. The converter stays usable: convert may be called again. The call releases the OCaml runtime lock and excludes other operations on the converter, as convert does.