avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
av::AudioResampler Class Reference

Converts audio between sample rates, sample formats and channel layouts. More...

#include <audioresampler.h>

Inheritance diagram for av::AudioResampler:

Public Member Functions

 AudioResampler ()
 Create an uninitialised resampler.
 AudioResampler (uint64_t dstChannelsLayout, int dstRate, SampleFormat dstFormat, uint64_t srcChannelsLayout, int srcRate, SampleFormat srcFormat, OptionalErrorCode ec=throws())
 Create and initialise a resampler.
 AudioResampler (uint64_t dstChannelsLayout, int dstRate, SampleFormat dstFormat, uint64_t srcChannelsLayout, int srcRate, SampleFormat srcFormat, Dictionary &options, OptionalErrorCode ec=throws())
 Create and initialise a resampler with options.
 AudioResampler (uint64_t dstChannelsLayout, int dstRate, SampleFormat dstFormat, uint64_t srcChannelsLayout, int srcRate, SampleFormat srcFormat, Dictionary &&options, OptionalErrorCode ec=throws())
 Create and initialise a resampler with options, moving the dictionary.
 AudioResampler (AudioResampler &&other)
 Move a resampler.
AudioResampler & operator= (AudioResampler &&rhs)
 Move assignment.
void swap (AudioResampler &other)
 Exchange the state of two resamplers.
uint64_t dstChannelLayout () const
 Channel layout the resampler produces.
int dstChannels () const
 Number of channels the resampler produces.
int dstSampleRate () const
 Sample rate the resampler produces.
SampleFormat dstSampleFormat () const
 Sample format the resampler produces.
uint64_t srcChannelLayout () const
 Channel layout the resampler expects.
int srcChannels () const
 Number of channels the resampler expects.
int srcSampleRate () const
 Sample rate the resampler expects.
SampleFormat srcSampleFormat () const
 Sample format the resampler expects.
void push (const AudioSamples &src, OptionalErrorCode ec=throws())
 Push frame to the rescaler context.
bool pop (AudioSamples &dst, bool getall, OptionalErrorCode ec=throws())
 Pop frame from the rescaler context.
AudioSamples pop (size_t samplesCount, OptionalErrorCode ec=throws())
 Pop frame from the rescaler context.
bool isValid () const
 Whether the resampler is initialised.
 operator bool () const
 Whether the resampler is initialised.
int64_t delay () const
 Samples still buffered inside the resampler.
bool init (uint64_t dstChannelsLayout, int dstRate, SampleFormat dstFormat, uint64_t srcChannelsLayout, int srcRate, SampleFormat srcFormat, OptionalErrorCode ec=throws())
 Initialise the resampler.
bool init (uint64_t dstChannelsLayout, int dstRate, SampleFormat dstFormat, uint64_t srcChannelsLayout, int srcRate, SampleFormat srcFormat, Dictionary &options, OptionalErrorCode ec=throws())
 Initialise the resampler with options.
bool init (uint64_t dstChannelsLayout, int dstRate, SampleFormat dstFormat, uint64_t srcChannelsLayout, int srcRate, SampleFormat srcFormat, Dictionary &&options, OptionalErrorCode ec=throws())
 Initialise the resampler with options, moving the dictionary.
Public Member Functions inherited from FFWrapperPtr< SwrContext >
 FFWrapperPtr ()=default
 Create an empty wrapper; isNull() is true.
const SwrContext * raw () const
 Read access to the wrapped object.
void reset (SwrContext *raw=nullptr)
 Replace the stored pointer.
bool isNull () const
 Whether the wrapper is empty.
void _log (int level, const char *fmt) const
 Log through the FFmpeg log system, using this wrapper as the context.

Static Public Member Functions

static bool validate (uint64_t dstChannelsLayout, int dstRate, SampleFormat dstFormat)
 Whether the output parameters are usable before the input is known.

Additional Inherited Members

Protected Member Functions inherited from av::noncopyable
 noncopyable (const noncopyable &)=delete
void operator= (const noncopyable &)=delete
Protected Attributes inherited from FFWrapperPtr< SwrContext >
SwrContext * m_raw
 The wrapped pointer; owned by the derived class, not by this template.

Detailed Description

The typical use is a decode-transform-encode pipeline: the decoder produces whatever the stream contains, the encoder accepts one specific layout, and this is the component in between. Construct it with both layouts, or call init() later; push() feeds samples in and pop() takes them out.

A single push can produce several output buffers, and the resampler holds an internal delay, so popping until it returns nothing is part of the contract.

Examples
api2-decode-encode-audio.cpp, and api2-decode-rasample-audio.cpp.

Constructor & Destructor Documentation

◆ AudioResampler() [1/5]

av::AudioResampler::AudioResampler ( )

Nothing can be pushed until init() succeeds.

◆ AudioResampler() [2/5]

av::AudioResampler::AudioResampler ( uint64_t dstChannelsLayout,
int dstRate,
SampleFormat dstFormat,
uint64_t srcChannelsLayout,
int srcRate,
SampleFormat srcFormat,
OptionalErrorCode ec = throws() )
Parameters
dstChannelsLayoutchannel layout to produce, as an AV_CH_LAYOUT_* mask
dstRatesample rate to produce, in hertz
dstFormatsample format to produce
srcChannelsLayoutchannel layout of the input
srcRatesample rate of the input, in hertz
srcFormatsample format of the input
echolder for the error code

◆ AudioResampler() [3/5]

av::AudioResampler::AudioResampler ( uint64_t dstChannelsLayout,
int dstRate,
SampleFormat dstFormat,
uint64_t srcChannelsLayout,
int srcRate,
SampleFormat srcFormat,
Dictionary & options,
OptionalErrorCode ec = throws() )
Parameters
dstChannelsLayoutchannel layout to produce
dstRatesample rate to produce
dstFormatsample format to produce
srcChannelsLayoutchannel layout of the input
srcRatesample rate of the input
srcFormatsample format of the input
optionsresampler options; the object stays usable, but FFmpeg removes the options it recognized
echolder for the error code

◆ AudioResampler() [4/5]

av::AudioResampler::AudioResampler ( uint64_t dstChannelsLayout,
int dstRate,
SampleFormat dstFormat,
uint64_t srcChannelsLayout,
int srcRate,
SampleFormat srcFormat,
Dictionary && options,
OptionalErrorCode ec = throws() )
Parameters
dstChannelsLayoutchannel layout to produce
dstRatesample rate to produce
dstFormatsample format to produce
srcChannelsLayoutchannel layout of the input
srcRatesample rate of the input
srcFormatsample format of the input
optionsresampler options, moved into the call
echolder for the error code

◆ AudioResampler() [5/5]

av::AudioResampler::AudioResampler ( AudioResampler && other)
Parameters
otherthe resampler to move from; left uninitialised

Member Function Documentation

◆ delay()

int64_t av::AudioResampler::delay ( ) const
Returns
the delay in output samples
Examples
api2-decode-encode-audio.cpp.

◆ dstChannelLayout()

uint64_t av::AudioResampler::dstChannelLayout ( ) const
Returns
an AV_CH_LAYOUT_* mask

◆ dstChannels()

int av::AudioResampler::dstChannels ( ) const
Returns
the channel count

◆ dstSampleFormat()

SampleFormat av::AudioResampler::dstSampleFormat ( ) const
Returns
the format

◆ dstSampleRate()

int av::AudioResampler::dstSampleRate ( ) const
Returns
samples per second

◆ init() [1/3]

bool av::AudioResampler::init ( uint64_t dstChannelsLayout,
int dstRate,
SampleFormat dstFormat,
uint64_t srcChannelsLayout,
int srcRate,
SampleFormat srcFormat,
Dictionary && options,
OptionalErrorCode ec = throws() )
Parameters
dstChannelsLayoutchannel layout to produce
dstRatesample rate to produce
dstFormatsample format to produce
srcChannelsLayoutchannel layout of the input
srcRatesample rate of the input
srcFormatsample format of the input
optionsresampler options, moved into the call
echolder for the error code
Returns
true when the resampler is ready

◆ init() [2/3]

bool av::AudioResampler::init ( uint64_t dstChannelsLayout,
int dstRate,
SampleFormat dstFormat,
uint64_t srcChannelsLayout,
int srcRate,
SampleFormat srcFormat,
Dictionary & options,
OptionalErrorCode ec = throws() )
Parameters
dstChannelsLayoutchannel layout to produce
dstRatesample rate to produce
dstFormatsample format to produce
srcChannelsLayoutchannel layout of the input
srcRatesample rate of the input
srcFormatsample format of the input
optionsresampler options; the object stays usable, but FFmpeg removes the options it recognized
echolder for the error code
Returns
true when the resampler is ready

◆ init() [3/3]

bool av::AudioResampler::init ( uint64_t dstChannelsLayout,
int dstRate,
SampleFormat dstFormat,
uint64_t srcChannelsLayout,
int srcRate,
SampleFormat srcFormat,
OptionalErrorCode ec = throws() )
Parameters
dstChannelsLayoutchannel layout to produce, as an AV_CH_LAYOUT_* mask
dstRatesample rate to produce, in hertz
dstFormatsample format to produce
srcChannelsLayoutchannel layout of the input
srcRatesample rate of the input, in hertz
srcFormatsample format of the input
echolder for the error code
Returns
true when the resampler is ready

◆ isValid()

bool av::AudioResampler::isValid ( ) const
Returns
true after a successful init()

◆ operator bool()

av::AudioResampler::operator bool ( ) const
inline
Returns
true after a successful init()

◆ operator=()

AudioResampler & av::AudioResampler::operator= ( AudioResampler && rhs)
Parameters
rhsthe resampler to move from; left uninitialised
Returns
this resampler

◆ pop() [1/2]

bool av::AudioResampler::pop ( AudioSamples & dst,
bool getall,
OptionalErrorCode ec = throws() )

Note, single push can produce multiple output frames.

If getall sets to true and false returns, this means that no data at all and resampler fully flushed.

Parameters
[out]dstframe to store audio data. Frame must be allocated before call.
[in]getallif true and if avail data less then nb_samples return data as is
[in,out]ecthis represents the error status on exit, if this is pre-initialized to av::throws the function will throw on error instead
Returns
false if no samples avail, true otherwise. On error false.
Examples
api2-decode-encode-audio.cpp, and api2-decode-rasample-audio.cpp.

◆ pop() [2/2]

AudioSamples av::AudioResampler::pop ( size_t samplesCount,
OptionalErrorCode ec = throws() )

Note, single push can produce multiple output frames.

If samplesCount sets to the zero and null frame returns, it means that no data at all and resampler fully flushed.

Parameters
[in]samplesCountsamples count to extract. If zero (0) - extract all delayed (buffered) samples.
[in,out]ecthis represents the error status on exit, if this is pre-initialized to av::throws the function will throw on error instead
Returns
resampled samples or null-frame when no requested samples avail. On error null-frame.

◆ push()

void av::AudioResampler::push ( const AudioSamples & src,
OptionalErrorCode ec = throws() )

Note, signle push can produce multiple frames.

Parameters
[in]srcsource frame
[in,out]ecthis represents the error status on exit, if this is pre-initialized to av::throws the function will throw on error instead
Examples
api2-decode-encode-audio.cpp, and api2-decode-rasample-audio.cpp.

◆ srcChannelLayout()

uint64_t av::AudioResampler::srcChannelLayout ( ) const
Returns
an AV_CH_LAYOUT_* mask

◆ srcChannels()

int av::AudioResampler::srcChannels ( ) const
Returns
the channel count

◆ srcSampleFormat()

SampleFormat av::AudioResampler::srcSampleFormat ( ) const
Returns
the format

◆ srcSampleRate()

int av::AudioResampler::srcSampleRate ( ) const
Returns
samples per second

◆ swap()

void av::AudioResampler::swap ( AudioResampler & other)
Parameters
otherthe resampler to swap with

◆ validate()

bool av::AudioResampler::validate ( uint64_t dstChannelsLayout,
int dstRate,
SampleFormat dstFormat )
static
Parameters
dstChannelsLayoutchannel layout to produce
dstRatesample rate to produce
dstFormatsample format to produce
Returns
true when the resampler initialises and accepts these values

The documentation for this class was generated from the following file: