SDL_audiolib 0.0.0
An audio decoding, resampling and mixing library
Aulib::ResamplerSrc Class Reference

SRC (libsamplerate) resampler. More...

#include <ResamplerSrc.h>

Inheritance diagram for Aulib::ResamplerSrc:
Aulib::Resampler

Public Types

enum class  Quality {
  Linear , ZeroOrderHold , SincFastest , SincMedium ,
  SincBest
}
 SRC resampler quality. More...
 

Public Member Functions

 ResamplerSrc (Quality quality=Quality::SincMedium)
 
 ~ResamplerSrc () override
 
auto quality () const noexcept -> Quality
 
- Public Member Functions inherited from Aulib::Resampler
 Resampler ()
 Constructs an audio resampler. More...
 
virtual ~Resampler ()
 
 Resampler (const Resampler &)=delete
 
auto operator= (const Resampler &) -> Resampler &=delete
 
void setDecoder (std::shared_ptr< Decoder > decoder)
 Sets the decoder that is to be used as source. More...
 
auto setSpec (int dstRate, int channels, int chunkSize) -> int
 Sets the target sample rate, channels and chuck size. More...
 
auto currentRate () const -> int
 
auto currentChannels () const -> int
 
auto currentChunkSize () const -> int
 
auto resample (float dst[], int dstLen) -> int
 Fills an output buffer with resampled audio samples. More...
 
void discardPendingSamples ()
 Discards any samples that have not yet been retrieved with resample(). More...
 

Protected Member Functions

void doResampling (float dst[], const float src[], int &dstLen, int &srcLen) override
 
auto adjustForOutputSpec (int dstRate, int srcRate, int channels) -> int override
 Change sample rate and amount of channels. More...
 
void doDiscardPendingSamples () override
 Discard any internally held samples. More...
 
virtual auto adjustForOutputSpec (int dstRate, int srcRate, int channels) -> int=0
 Change sample rate and amount of channels. More...
 
virtual void doResampling (float dst[], const float src[], int &dstLen, int &srcLen)=0
 
virtual void doDiscardPendingSamples ()=0
 Discard any internally held samples. More...
 

Detailed Description

SRC (libsamplerate) resampler.

Member Enumeration Documentation

◆ Quality

enum class Aulib::ResamplerSrc::Quality
strong

SRC resampler quality.

These represent the five SRC resampling methods, with "Linear" and "ZeroOrderHold" being the fastest but lowest quality, and "SincBest" the slowest but highest quality.

From SRC's API documentation:

  • SincBest This is a bandlimited interpolator derived from the mathematical sinc function and this is the highest quality sinc based converter, providing a worst case Signal-to-Noise Ratio (SNR) of 97 decibels (dB) at a bandwidth of 97%. All three Sinc* converters are based on the techniques of Julius O. Smith although this code was developed independantly.
  • SincMedium This is another bandlimited interpolator much like the previous one. It has an SNR of 97dB and a bandwidth of 90%. The speed of the conversion is much faster than the previous one.
  • SincFastest This is the fastest bandlimited interpolator and has an SNR of 97dB and a bandwidth of 80%.
  • ZeroOrderHold A Zero Order Hold converter (interpolated value is equal to the last value). The quality is poor but the conversion speed is blindlingly fast.
  • Linear A linear converter. Again the quality is poor, but the conversion speed is blindingly fast.
Enumerator
Linear 
ZeroOrderHold 
SincFastest 
SincMedium 
SincBest 

Constructor & Destructor Documentation

◆ ResamplerSrc()

Aulib::ResamplerSrc::ResamplerSrc ( Quality  quality = Quality::SincMedium)
explicit
Parameters
qualityResampling quality. Note that the quality can not be changed later on.

◆ ~ResamplerSrc()

Aulib::ResamplerSrc::~ResamplerSrc ( )
override

Member Function Documentation

◆ adjustForOutputSpec()

auto Aulib::ResamplerSrc::adjustForOutputSpec ( int  dstRate,
int  srcRate,
int  channels 
) -> int
overrideprotectedvirtual

Change sample rate and amount of channels.

This function must be implemented when subclassing. It is used to notify subclasses about changes in source and target sample rates, as well as the amount of channels in the audio.

Parameters
dstRateTarget sample rate (rate being resampled to.)
srcRateSource sample rate (rate being resampled from.)
channelsAmount of channels in both the source as well as the target audio buffers.

Implements Aulib::Resampler.

◆ doDiscardPendingSamples()

void Aulib::ResamplerSrc::doDiscardPendingSamples ( )
overrideprotectedvirtual

Discard any internally held samples.

This function must be implemented when subclassing. It should discard any internally held samples. Note that even if you don't actually buffer any samples in your subclass but are using some external resampling library that you delegate resampling to, that external resampler might be holding samples in an internal buffer. Those will need to be discarded as well.

If none of the above applies, this can be implemented as an empty function.

Implements Aulib::Resampler.

◆ doResampling()

void Aulib::ResamplerSrc::doResampling ( float  dst[],
const float  src[],
int &  dstLen,
int &  srcLen 
)
overrideprotectedvirtual

This function must be implemented when subclassing. It must resample the audio contained in 'src' containing 'srcLen' samples, and store the resulting samples in 'dst', which has a capacity of at most 'dstLen' samples.

The 'src' buffer contains audio in either mono or stereo. Stereo is stored in interleaved format.

The source and target sample rates, as well as the amount of channels that are to be used must be those that were specified in the last call to the adjustForOutputSpec() function.

'dstLen' and 'srcLen' are both input as well as output parameters. The function must set 'dstLen' to the amount of samples that were actually stored in 'dst', and 'srcLen' to the amount of samples that were actually used from 'src'. For example, if in the following call:

 dstLen = 200;
 srcLen = 100;
 doResampling(dst, src, dstLen, srcLen);

the function resamples 98 samples from 'src', resulting in 196 samples which are stored in 'dst', the function must set 'srcLen' to 98 and 'dstLen' to 196.

So when implementing this function, you do not need to worry about using up all the available samples in 'src'. Simply resample as much audio from 'src' as you can in order to fill 'dst' as much as possible, and if there's anything left at the end that cannot be resampled, simply ignore it.

Implements Aulib::Resampler.

◆ quality()

auto Aulib::ResamplerSrc::quality ( ) const -> Quality
noexcept