For example the following code will setup conversion from planar float sample format to interleaved signed 16-bit integer, downsampling from 48kHz to 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing matrix):

Once the context is initialized, it must be opened with avresample_open(). If you need to change the conversion parameters, you must close the context with avresample_close(), change the parameters as described above, then reopen it again.

The conversion itself is done by repeatedly calling avresample_convert(). Note that the samples may get buffered in two places in lavr. The first one is the output FIFO, where the samples end up if the output buffer is not large enough. The data stored in there may be retrieved at any time with avresample_read(). The second place is the resampling delay buffer, applicable only when resampling is done. The samples in it require more input before they can be processed. Their current amount is returned by avresample_get_delay(). At the end of conversion the resampling buffer can be flushed by calling avresample_convert() with NULL input.

The following code demonstrates the conversion loop assuming the parameters from above and caller-defined functions get_input() and handle_output():

This closes the context, but it does not change the parameters. The context can be reopened with avresample_open(). It does, however, clear the output FIFO and any remaining leftover samples in the resampling delay buffer. If there was a custom matrix being used, that is also cleared.

Allows for setting a custom mixing matrix, overriding the default matrix generated internally during avresample_open(). This function can be called anytime on an allocated context, either before or after calling avresample_open(), as long as the channel layouts have been set. avresample_convert() always uses the current matrix. Calling avresample_close() on the context will clear the current matrix.

The map for each input channel specifies the channel index in the source to use for that particular channel, or -1 to mute the channel. Source channels can be duplicated by using the same index for multiple input channels.

This can be called anytime after avresample_open(). If resampling is not automatically enabled because of a sample rate conversion, the "force_resampling" option must have been set to 1 when opening the context in order to use resampling compensation.

The output data can be NULL or have fewer allocated samples than required. In this case, any remaining samples not written to the output will be added to an internal FIFO buffer, to be returned at the next call to this function or to avresample_read().

If converting sample rate, there may be data remaining in the internal resampling delay buffer. avresample_get_delay() tells the number of remaining samples. To get this data as output, call avresample_convert() with NULL input.

Return the number of samples currently in the resampling delay buffer.

When resampling, there may be a delay between the input and output. Any unconverted samples in each call are stored internally in a delay buffer. This function allows the user to determine the current number of samples in the delay buffer, which can be useful for synchronization.

During conversion, if the user does not specify an output buffer or specifies an output buffer that is smaller than what is needed, remaining samples that are not written to the output are stored to an internal FIFO buffer. The samples in the FIFO can be read with avresample_read() or avresample_convert().

During conversion, if the user does not specify an output buffer or specifies an output buffer that is smaller than what is needed, remaining samples that are not written to the output are stored to an internal FIFO buffer. This function can be used to read samples from that internal FIFO.

The output AVFrame can be NULL or have fewer allocated samples than required. In this case, any remaining samples not written to the output will be added to an internal FIFO buffer, to be returned at the next call to this function or to avresample_convert() or to avresample_read().

If converting sample rate, there may be data remaining in the internal resampling delay buffer. avresample_get_delay() tells the number of remaining samples. To get this data as output, call this function or avresample_convert() with NULL input.

If the AVAudioResampleContext configuration does not match the output and input AVFrame settings the conversion does not take place and depending on which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED or AVERROR_OUTPUT_CHANGED|AVERROR_INPUT_CHANGED is returned.