Rubber Band Library  1.8.1
RubberBand::RubberBandStretcher Class Reference

#include <RubberBandStretcher.h>

Public Types

enum  Option {
  OptionProcessOffline = 0x00000000, OptionProcessRealTime = 0x00000001, OptionStretchElastic = 0x00000000, OptionStretchPrecise = 0x00000010,
  OptionTransientsCrisp = 0x00000000, OptionTransientsMixed = 0x00000100, OptionTransientsSmooth = 0x00000200, OptionDetectorCompound = 0x00000000,
  OptionDetectorPercussive = 0x00000400, OptionDetectorSoft = 0x00000800, OptionPhaseLaminar = 0x00000000, OptionPhaseIndependent = 0x00002000,
  OptionThreadingAuto = 0x00000000, OptionThreadingNever = 0x00010000, OptionThreadingAlways = 0x00020000, OptionWindowStandard = 0x00000000,
  OptionWindowShort = 0x00100000, OptionWindowLong = 0x00200000, OptionSmoothingOff = 0x00000000, OptionSmoothingOn = 0x00800000,
  OptionFormantShifted = 0x00000000, OptionFormantPreserved = 0x01000000, OptionPitchHighSpeed = 0x00000000, OptionPitchHighQuality = 0x02000000,
  OptionPitchHighConsistency = 0x04000000, OptionChannelsApart = 0x00000000, OptionChannelsTogether = 0x10000000
}
 Processing options for the timestretcher. More...
 
enum  PresetOption { DefaultOptions = 0x00000000, PercussiveOptions = 0x00102000 }
 
typedef int Options
 

Public Member Functions

 RubberBandStretcher (size_t sampleRate, size_t channels, Options options=DefaultOptions, double initialTimeRatio=1.0, double initialPitchScale=1.0)
 Construct a time and pitch stretcher object to run at the given sample rate, with the given number of channels. More...
 
 ~RubberBandStretcher ()
 
void reset ()
 Reset the stretcher's internal buffers. More...
 
void setTimeRatio (double ratio)
 Set the time ratio for the stretcher. More...
 
void setPitchScale (double scale)
 Set the pitch scaling ratio for the stretcher. More...
 
double getTimeRatio () const
 Return the last time ratio value that was set (either on construction or with setTimeRatio()). More...
 
double getPitchScale () const
 Return the last pitch scaling ratio value that was set (either on construction or with setPitchScale()). More...
 
size_t getLatency () const
 Return the processing latency of the stretcher. More...
 
void setTransientsOption (Options options)
 Change an OptionTransients configuration setting. More...
 
void setDetectorOption (Options options)
 Change an OptionDetector configuration setting. More...
 
void setPhaseOption (Options options)
 Change an OptionPhase configuration setting. More...
 
void setFormantOption (Options options)
 Change an OptionFormant configuration setting. More...
 
void setPitchOption (Options options)
 Change an OptionPitch configuration setting. More...
 
void setExpectedInputDuration (size_t samples)
 Tell the stretcher exactly how many input samples it will receive. More...
 
void setMaxProcessSize (size_t samples)
 Tell the stretcher the maximum number of sample frames that you will ever be passing in to a single process() call. More...
 
size_t getSamplesRequired () const
 Ask the stretcher how many audio sample frames should be provided as input in order to ensure that some more output becomes available. More...
 
void setKeyFrameMap (const std::map< size_t, size_t > &)
 Provide a set of mappings from "before" to "after" sample numbers so as to enforce a particular stretch profile. More...
 
void study (const float *const *input, size_t samples, bool final)
 Provide a block of "samples" sample frames for the stretcher to study and calculate a stretch profile from. More...
 
void process (const float *const *input, size_t samples, bool final)
 Provide a block of "samples" sample frames for processing. More...
 
int available () const
 Ask the stretcher how many audio sample frames of output data are available for reading (via retrieve()). More...
 
size_t retrieve (float *const *output, size_t samples) const
 Obtain some processed output data from the stretcher. More...
 
float getFrequencyCutoff (int n) const
 Return the value of internal frequency cutoff value n. More...
 
void setFrequencyCutoff (int n, float f)
 Set the value of internal frequency cutoff n to f Hz. More...
 
size_t getInputIncrement () const
 Retrieve the value of the internal input block increment value. More...
 
std::vector< int > getOutputIncrements () const
 In offline mode, retrieve the sequence of internal block increments for output, for the entire audio data, provided the stretch profile has been calculated. More...
 
std::vector< float > getPhaseResetCurve () const
 In offline mode, retrieve the sequence of internal phase reset detection function values, for the entire audio data, provided the stretch profile has been calculated. More...
 
std::vector< int > getExactTimePoints () const
 In offline mode, retrieve the sequence of internal frames for which exact timing has been sought, for the entire audio data, provided the stretch profile has been calculated. More...
 
size_t getChannelCount () const
 Return the number of channels this stretcher was constructed with. More...
 
void calculateStretch ()
 Force the stretcher to calculate a stretch profile. More...
 
void setDebugLevel (int level)
 Set the level of debug output. More...
 

Static Public Member Functions

static void setDefaultDebugLevel (int level)
 Set the default level of debug output for subsequently constructed stretchers. More...
 

Protected Attributes

Impl * m_d
 

Detailed Description

Definition at line 63 of file RubberBandStretcher.h.

Member Typedef Documentation

Definition at line 318 of file RubberBandStretcher.h.

Member Enumeration Documentation

Processing options for the timestretcher.

The preferred options should normally be set in the constructor, as a bitwise OR of the option flags. The default value (DefaultOptions) is intended to give good results in most situations.

  1. Flags prefixed OptionProcess determine how the timestretcher will be invoked. These options may not be changed after construction.
  • OptionProcessOffline - Run the stretcher in offline mode. In this mode the input data needs to be provided twice, once to study(), which calculates a stretch profile for the audio, and once to process(), which stretches it.
  • OptionProcessRealTime - Run the stretcher in real-time mode. In this mode only process() should be called, and the stretcher adjusts dynamically in response to the input audio.

The Process setting is likely to depend on your architecture: non-real-time operation on seekable files: Offline; real-time or streaming operation: RealTime.

  1. Flags prefixed OptionStretch control the profile used for variable timestretching. Rubber Band always adjusts the stretch profile to minimise stretching of busy broadband transient sounds, but the degree to which it does so is adjustable. These options may not be changed after construction.
  • OptionStretchElastic - Only meaningful in offline mode, and the default in that mode. The audio will be stretched at a variable rate, aimed at preserving the quality of transient sounds as much as possible. The timings of low activity regions between transients may be less exact than when the precise flag is set.
  • OptionStretchPrecise - Although still using a variable stretch rate, the audio will be stretched so as to maintain as close as possible to a linear stretch ratio throughout. Timing may be better than when using OptionStretchElastic, at slight cost to the sound quality of transients. This setting is always used when running in real-time mode.
  1. Flags prefixed OptionTransients control the component frequency phase-reset mechanism that may be used at transient points to provide clarity and realism to percussion and other significant transient sounds. These options may be changed after construction when running in real-time mode, but not when running in offline mode.
  • OptionTransientsCrisp - Reset component phases at the peak of each transient (the start of a significant note or percussive event). This, the default setting, usually results in a clear-sounding output; but it is not always consistent, and may cause interruptions in stable sounds present at the same time as transient events. The OptionDetector flags (below) can be used to tune this to some extent.
  • OptionTransientsMixed - Reset component phases at the peak of each transient, outside a frequency range typical of musical fundamental frequencies. The results may be more regular for mixed stable and percussive notes than OptionTransientsCrisp, but with a "phasier" sound. The balance may sound very good for certain types of music and fairly bad for others.
  • OptionTransientsSmooth - Do not reset component phases at any point. The results will be smoother and more regular but may be less clear than with either of the other transients flags.
  1. Flags prefixed OptionDetector control the type of transient detector used. These options may be changed after construction when running in real-time mode, but not when running in offline mode.
  • OptionDetectorCompound - Use a general-purpose transient detector which is likely to be good for most situations. This is the default.
  • OptionDetectorPercussive - Detect percussive transients. Note that this was the default and only option in Rubber Band versions prior to 1.5.
  • OptionDetectorSoft - Use an onset detector with less of a bias toward percussive transients. This may give better results with certain material (e.g. relatively monophonic piano music).
  1. Flags prefixed OptionPhase control the adjustment of component frequency phases from one analysis window to the next during non-transient segments. These options may be changed at any time.
  • OptionPhaseLaminar - Adjust phases when stretching in such a way as to try to retain the continuity of phase relationships between adjacent frequency bins whose phases are behaving in similar ways. This, the default setting, should give good results in most situations.
  • OptionPhaseIndependent - Adjust the phase in each frequency bin independently from its neighbours. This usually results in a slightly softer, phasier sound.
  1. Flags prefixed OptionThreading control the threading model of the stretcher. These options may not be changed after construction.
  • OptionThreadingAuto - Permit the stretcher to determine its own threading model. Usually this means using one processing thread per audio channel in offline mode if the stretcher is able to determine that more than one CPU is available, and one thread only in realtime mode. This is the defafult.
  • OptionThreadingNever - Never use more than one thread.
  • OptionThreadingAlways - Use multiple threads in any situation where OptionThreadingAuto would do so, except omit the check for multiple CPUs and instead assume it to be true.
  1. Flags prefixed OptionWindow control the window size for FFT processing. The window size actually used will depend on many factors, but it can be influenced. These options may not be changed after construction.
  • OptionWindowStandard - Use the default window size. The actual size will vary depending on other parameters. This option is expected to produce better results than the other window options in most situations.
  • OptionWindowShort - Use a shorter window. This may result in crisper sound for audio that depends strongly on its timing qualities.
  • OptionWindowLong - Use a longer window. This is likely to result in a smoother sound at the expense of clarity and timing.
  1. Flags prefixed OptionSmoothing control the use of window-presum FFT and time-domain smoothing. These options may not be changed after construction.
  • OptionSmoothingOff - Do not use time-domain smoothing. This is the default.
  • OptionSmoothingOn - Use time-domain smoothing. This will result in a softer sound with some audible artifacts around sharp transients, but it may be appropriate for longer stretches of some instruments and can mix well with OptionWindowShort.
  1. Flags prefixed OptionFormant control the handling of formant shape (spectral envelope) when pitch-shifting. These options may be changed at any time.
  • OptionFormantShifted - Apply no special formant processing. The spectral envelope will be pitch shifted as normal. This is the default.
  • OptionFormantPreserved - Preserve the spectral envelope of the unshifted signal. This permits shifting the note frequency without so substantially affecting the perceived pitch profile of the voice or instrument.
  1. Flags prefixed OptionPitch control the method used for pitch shifting. These options may be changed at any time. They are only effective in realtime mode; in offline mode, the pitch-shift method is fixed.
  • OptionPitchHighSpeed - Use a method with a CPU cost that is relatively moderate and predictable. This may sound less clear than OptionPitchHighQuality, especially for large pitch shifts. This is the default.
  • OptionPitchHighQuality - Use the highest quality method for pitch shifting. This method has a CPU cost approximately proportional to the required frequency shift.
  • OptionPitchHighConsistency - Use the method that gives greatest consistency when used to create small variations in pitch around the 1.0-ratio level. Unlike the previous two options, this avoids discontinuities when moving across the 1.0 pitch scale in real-time; it also consumes more CPU than the others in the case where the pitch scale is exactly 1.0.
  1. Flags prefixed OptionChannels control the method used for processing two-channel audio. These options may not be changed after construction.
  • OptionChannelsApart - Each channel is processed individually, though timing is synchronised and phases are synchronised at transients (depending on the OptionTransients setting). This gives the highest quality for the individual channels but a relative lack of stereo focus and unrealistic increase in "width". This is the default.
  • OptionChannelsTogether - The first two channels (where two or more are present) are considered to be a stereo pair and are processed in mid-side format; mid and side are processed individually, with timing synchronised and phases synchronised at transients (depending on the OptionTransients setting). This usually leads to better focus in the centre but a loss of stereo space and width. Any channels beyond the first two are processed individually.
Enumerator
OptionProcessOffline 
OptionProcessRealTime 
OptionStretchElastic 
OptionStretchPrecise 
OptionTransientsCrisp 
OptionTransientsMixed 
OptionTransientsSmooth 
OptionDetectorCompound 
OptionDetectorPercussive 
OptionDetectorSoft 
OptionPhaseLaminar 
OptionPhaseIndependent 
OptionThreadingAuto 
OptionThreadingNever 
OptionThreadingAlways 
OptionWindowStandard 
OptionWindowShort 
OptionWindowLong 
OptionSmoothingOff 
OptionSmoothingOn 
OptionFormantShifted 
OptionFormantPreserved 
OptionPitchHighSpeed 
OptionPitchHighQuality 
OptionPitchHighConsistency 
OptionChannelsApart 
OptionChannelsTogether 

Definition at line 275 of file RubberBandStretcher.h.

Enumerator
DefaultOptions 
PercussiveOptions 

Definition at line 320 of file RubberBandStretcher.h.

Constructor & Destructor Documentation

RubberBand::RubberBandStretcher::RubberBandStretcher ( size_t  sampleRate,
size_t  channels,
Options  options = DefaultOptions,
double  initialTimeRatio = 1.0,
double  initialPitchScale = 1.0 
)

Construct a time and pitch stretcher object to run at the given sample rate, with the given number of channels.

Processing options and the time and pitch scaling ratios may be provided. The time and pitch ratios may be changed after construction, but most of the options may not. See the option documentation above for more details.

RubberBand::RubberBandStretcher::~RubberBandStretcher ( )

Member Function Documentation

void RubberBand::RubberBandStretcher::reset ( )

Reset the stretcher's internal buffers.

The stretcher should subsequently behave as if it had just been constructed (although retaining the current time and pitch ratio).

void RubberBand::RubberBandStretcher::setTimeRatio ( double  ratio)

Set the time ratio for the stretcher.

This is the ratio of stretched to unstretched duration – not tempo. For example, a ratio of 2.0 would make the audio twice as long (i.e. halve the tempo); 0.5 would make it half as long (i.e. double the tempo); 1.0 would leave the duration unaffected.

If the stretcher was constructed in Offline mode, the time ratio is fixed throughout operation; this function may be called any number of times between construction (or a call to reset()) and the first call to study() or process(), but may not be called after study() or process() has been called.

If the stretcher was constructed in RealTime mode, the time ratio may be varied during operation; this function may be called at any time, so long as it is not called concurrently with process(). You should either call this function from the same thread as process(), or provide your own mutex or similar mechanism to ensure that setTimeRatio and process() cannot be run at once (there is no internal mutex for this purpose).

void RubberBand::RubberBandStretcher::setPitchScale ( double  scale)

Set the pitch scaling ratio for the stretcher.

This is the ratio of target frequency to source frequency. For example, a ratio of 2.0 would shift up by one octave; 0.5 down by one octave; or 1.0 leave the pitch unaffected.

To put this in musical terms, a pitch scaling ratio corresponding to a shift of S equal-tempered semitones (where S is positive for an upwards shift and negative for downwards) is pow(2.0, S / 12.0).

If the stretcher was constructed in Offline mode, the pitch scaling ratio is fixed throughout operation; this function may be called any number of times between construction (or a call to reset()) and the first call to study() or process(), but may not be called after study() or process() has been called.

If the stretcher was constructed in RealTime mode, the pitch scaling ratio may be varied during operation; this function may be called at any time, so long as it is not called concurrently with process(). You should either call this function from the same thread as process(), or provide your own mutex or similar mechanism to ensure that setPitchScale and process() cannot be run at once (there is no internal mutex for this purpose).

double RubberBand::RubberBandStretcher::getTimeRatio ( ) const

Return the last time ratio value that was set (either on construction or with setTimeRatio()).

double RubberBand::RubberBandStretcher::getPitchScale ( ) const

Return the last pitch scaling ratio value that was set (either on construction or with setPitchScale()).

size_t RubberBand::RubberBandStretcher::getLatency ( ) const

Return the processing latency of the stretcher.

This is the number of audio samples that one would have to discard at the start of the output in order to ensure that the resulting audio aligned with the input audio at the start. In Offline mode, latency is automatically adjusted for and the result is zero. In RealTime mode, the latency may depend on the time and pitch ratio and other options.

void RubberBand::RubberBandStretcher::setTransientsOption ( Options  options)

Change an OptionTransients configuration setting.

This may be called at any time in RealTime mode. It may not be called in Offline mode (for which the transients option is fixed on construction).

void RubberBand::RubberBandStretcher::setDetectorOption ( Options  options)

Change an OptionDetector configuration setting.

This may be called at any time in RealTime mode. It may not be called in Offline mode (for which the detector option is fixed on construction).

void RubberBand::RubberBandStretcher::setPhaseOption ( Options  options)

Change an OptionPhase configuration setting.

This may be called at any time in any mode.

Note that if running multi-threaded in Offline mode, the change may not take effect immediately if processing is already under way when this function is called.

void RubberBand::RubberBandStretcher::setFormantOption ( Options  options)

Change an OptionFormant configuration setting.

This may be called at any time in any mode.

Note that if running multi-threaded in Offline mode, the change may not take effect immediately if processing is already under way when this function is called.

void RubberBand::RubberBandStretcher::setPitchOption ( Options  options)

Change an OptionPitch configuration setting.

This may be called at any time in RealTime mode. It may not be called in Offline mode (for which the transients option is fixed on construction).

void RubberBand::RubberBandStretcher::setExpectedInputDuration ( size_t  samples)

Tell the stretcher exactly how many input samples it will receive.

This is only useful in Offline mode, when it allows the stretcher to ensure that the number of output samples is exactly correct. In RealTime mode no such guarantee is possible and this value is ignored.

void RubberBand::RubberBandStretcher::setMaxProcessSize ( size_t  samples)

Tell the stretcher the maximum number of sample frames that you will ever be passing in to a single process() call.

If you don't call this, the stretcher will assume that you are calling getSamplesRequired() at each cycle and are never passing more samples than are suggested by that function.

If your application has some external constraint that means you prefer a fixed block size, then your normal mode of operation would be to provide that block size to this function; to loop calling process() with that size of block; after each call to process(), test whether output has been generated by calling available(); and, if so, call retrieve() to obtain it. See getSamplesRequired() for a more suitable operating mode for applications without such external constraints.

This function may not be called after the first call to study() or process().

Note that this value is only relevant to process(), not to study() (to which you may pass any number of samples at a time, and from which there is no output).

size_t RubberBand::RubberBandStretcher::getSamplesRequired ( ) const

Ask the stretcher how many audio sample frames should be provided as input in order to ensure that some more output becomes available.

If your application has no particular constraint on processing block size and you are able to provide any block size as input for each cycle, then your normal mode of operation would be to loop querying this function; providing that number of samples to process(); and reading the output using available() and retrieve(). See setMaxProcessSize() for a more suitable operating mode for applications that do have external block size constraints.

Note that this value is only relevant to process(), not to study() (to which you may pass any number of samples at a time, and from which there is no output).

void RubberBand::RubberBandStretcher::setKeyFrameMap ( const std::map< size_t, size_t > &  )

Provide a set of mappings from "before" to "after" sample numbers so as to enforce a particular stretch profile.

The argument is a map from audio sample frame number in the source material, to the corresponding sample frame number in the stretched output. The mapping should be for key frames only, with a "reasonable" gap between mapped samples.

This function cannot be used in RealTime mode.

This function may not be called after the first call to process(). It should be called after the time and pitch ratios have been set; the results of changing the time and pitch ratios after calling this function are undefined. Calling reset() will clear this mapping.

The key frame map only affects points within the material; it does not determine the overall stretch ratio (that is, the ratio between the output material's duration and the source material's duration). You need to provide this ratio separately to setTimeRatio(), otherwise the results may be truncated or extended in unexpected ways regardless of the extent of the frame numbers found in the key frame map.

void RubberBand::RubberBandStretcher::study ( const float *const *  input,
size_t  samples,
bool  final 
)

Provide a block of "samples" sample frames for the stretcher to study and calculate a stretch profile from.

This is only meaningful in Offline mode, and is required if running in that mode. You should pass the entire input through study() before any process() calls are made, as a sequence of blocks in individual study() calls, or as a single large block.

"input" should point to de-interleaved audio data with one float array per channel. "samples" supplies the number of audio sample frames available in "input". If "samples" is zero, "input" may be NULL.

Set "final" to true if this is the last block of data that will be provided to study() before the first process() call.

void RubberBand::RubberBandStretcher::process ( const float *const *  input,
size_t  samples,
bool  final 
)

Provide a block of "samples" sample frames for processing.

See also getSamplesRequired() and setMaxProcessSize().

Set "final" to true if this is the last block of input data.

int RubberBand::RubberBandStretcher::available ( ) const

Ask the stretcher how many audio sample frames of output data are available for reading (via retrieve()).

This function returns 0 if no frames are available: this usually means more input data needs to be provided, but if the stretcher is running in threaded mode it may just mean that not enough data has yet been processed. Call getSamplesRequired() to discover whether more input is needed.

This function returns -1 if all data has been fully processed and all output read, and the stretch process is now finished.

size_t RubberBand::RubberBandStretcher::retrieve ( float *const *  output,
size_t  samples 
) const

Obtain some processed output data from the stretcher.

Up to "samples" samples will be stored in the output arrays (one per channel for de-interleaved audio data) pointed to by "output". The return value is the actual number of sample frames retrieved.

float RubberBand::RubberBandStretcher::getFrequencyCutoff ( int  n) const

Return the value of internal frequency cutoff value n.

This function is not for general use.

void RubberBand::RubberBandStretcher::setFrequencyCutoff ( int  n,
float  f 
)

Set the value of internal frequency cutoff n to f Hz.

This function is not for general use.

size_t RubberBand::RubberBandStretcher::getInputIncrement ( ) const

Retrieve the value of the internal input block increment value.

This function is provided for diagnostic purposes only.

std::vector<int> RubberBand::RubberBandStretcher::getOutputIncrements ( ) const

In offline mode, retrieve the sequence of internal block increments for output, for the entire audio data, provided the stretch profile has been calculated.

In realtime mode, retrieve any output increments that have accumulated since the last call to getOutputIncrements, to a limit of 16.

This function is provided for diagnostic purposes only.

std::vector<float> RubberBand::RubberBandStretcher::getPhaseResetCurve ( ) const

In offline mode, retrieve the sequence of internal phase reset detection function values, for the entire audio data, provided the stretch profile has been calculated.

In realtime mode, retrieve any phase reset points that have accumulated since the last call to getPhaseResetCurve, to a limit of 16.

This function is provided for diagnostic purposes only.

std::vector<int> RubberBand::RubberBandStretcher::getExactTimePoints ( ) const

In offline mode, retrieve the sequence of internal frames for which exact timing has been sought, for the entire audio data, provided the stretch profile has been calculated.

In realtime mode, return an empty sequence.

This function is provided for diagnostic purposes only.

size_t RubberBand::RubberBandStretcher::getChannelCount ( ) const

Return the number of channels this stretcher was constructed with.

void RubberBand::RubberBandStretcher::calculateStretch ( )

Force the stretcher to calculate a stretch profile.

Normally this happens automatically for the first process() call in offline mode.

This function is provided for diagnostic purposes only.

void RubberBand::RubberBandStretcher::setDebugLevel ( int  level)

Set the level of debug output.

The value may be from 0 (errors only) to 3 (very verbose, with audible ticks in the output at phase reset points). The default is whatever has been set using setDefaultDebugLevel, or 0 if that function has not been called.

static void RubberBand::RubberBandStretcher::setDefaultDebugLevel ( int  level)
static

Set the default level of debug output for subsequently constructed stretchers.

See Also
setDebugLevel

Member Data Documentation

Impl* RubberBand::RubberBandStretcher::m_d
protected

Definition at line 681 of file RubberBandStretcher.h.


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