cannam@130: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@130: cannam@130: /* cannam@130: Rubber Band Library cannam@130: An audio time-stretching and pitch-shifting library. cannam@130: Copyright 2007-2015 Particular Programs Ltd. cannam@130: cannam@130: This program is free software; you can redistribute it and/or cannam@130: modify it under the terms of the GNU General Public License as cannam@130: published by the Free Software Foundation; either version 2 of the cannam@130: License, or (at your option) any later version. See the file cannam@130: COPYING included with this distribution for more information. cannam@130: cannam@130: Alternatively, if you have a valid commercial licence for the cannam@130: Rubber Band Library obtained by agreement with the copyright cannam@130: holders, you may redistribute and/or modify it under the terms cannam@130: described in that licence. cannam@130: cannam@130: If you wish to distribute code using the Rubber Band Library cannam@130: under terms other than those of the GNU General Public License, cannam@130: you must obtain a valid commercial licence before doing so. cannam@130: */ cannam@130: cannam@130: #ifndef _RUBBERBANDSTRETCHER_H_ cannam@130: #define _RUBBERBANDSTRETCHER_H_ cannam@130: cannam@130: #define RUBBERBAND_VERSION "1.8.2" cannam@130: #define RUBBERBAND_API_MAJOR_VERSION 2 cannam@130: #define RUBBERBAND_API_MINOR_VERSION 5 cannam@130: cannam@130: #include cannam@130: #include cannam@130: #include cannam@130: cannam@130: /** cannam@130: * @mainpage RubberBand cannam@130: * cannam@130: * The Rubber Band API is contained in the single class cannam@130: * RubberBand::RubberBandStretcher. cannam@130: * cannam@130: * Threading notes for real-time applications: cannam@130: * cannam@130: * Multiple instances of RubberBandStretcher may be created and used cannam@130: * in separate threads concurrently. However, for any single instance cannam@130: * of RubberBandStretcher, you may not call process() more than once cannam@130: * concurrently, and you may not change the time or pitch ratio while cannam@130: * a process() call is being executed (if the stretcher was created in cannam@130: * "real-time mode"; in "offline mode" you can't change the ratios cannam@130: * during use anyway). cannam@130: * cannam@130: * So you can run process() in its own thread if you like, but if you cannam@130: * want to change ratios dynamically from a different thread, you will cannam@130: * need some form of mutex in your code. Changing the time or pitch cannam@130: * ratio is real-time safe except in extreme circumstances, so for cannam@130: * most applications that may change these dynamically it probably cannam@130: * makes most sense to do so from the same thread as calls process(), cannam@130: * even if that is a real-time thread. cannam@130: */ cannam@130: cannam@130: namespace RubberBand cannam@130: { cannam@130: cannam@130: class RubberBandStretcher cannam@130: { cannam@130: public: cannam@130: /** cannam@130: * Processing options for the timestretcher. The preferred cannam@130: * options should normally be set in the constructor, as a bitwise cannam@130: * OR of the option flags. The default value (DefaultOptions) is cannam@130: * intended to give good results in most situations. cannam@130: * cannam@130: * 1. Flags prefixed \c OptionProcess determine how the timestretcher cannam@130: * will be invoked. These options may not be changed after cannam@130: * construction. cannam@130: * cannam@130: * \li \c OptionProcessOffline - Run the stretcher in offline cannam@130: * mode. In this mode the input data needs to be provided cannam@130: * twice, once to study(), which calculates a stretch profile cannam@130: * for the audio, and once to process(), which stretches it. cannam@130: * cannam@130: * \li \c OptionProcessRealTime - Run the stretcher in real-time cannam@130: * mode. In this mode only process() should be called, and the cannam@130: * stretcher adjusts dynamically in response to the input audio. cannam@130: * cannam@130: * The Process setting is likely to depend on your architecture: cannam@130: * non-real-time operation on seekable files: Offline; real-time cannam@130: * or streaming operation: RealTime. cannam@130: * cannam@130: * 2. Flags prefixed \c OptionStretch control the profile used for cannam@130: * variable timestretching. Rubber Band always adjusts the cannam@130: * stretch profile to minimise stretching of busy broadband cannam@130: * transient sounds, but the degree to which it does so is cannam@130: * adjustable. These options may not be changed after cannam@130: * construction. cannam@130: * cannam@130: * \li \c OptionStretchElastic - Only meaningful in offline cannam@130: * mode, and the default in that mode. The audio will be cannam@130: * stretched at a variable rate, aimed at preserving the quality cannam@130: * of transient sounds as much as possible. The timings of low cannam@130: * activity regions between transients may be less exact than cannam@130: * when the precise flag is set. cannam@130: * cannam@130: * \li \c OptionStretchPrecise - Although still using a variable cannam@130: * stretch rate, the audio will be stretched so as to maintain cannam@130: * as close as possible to a linear stretch ratio throughout. cannam@130: * Timing may be better than when using \c OptionStretchElastic, at cannam@130: * slight cost to the sound quality of transients. This setting cannam@130: * is always used when running in real-time mode. cannam@130: * cannam@130: * 3. Flags prefixed \c OptionTransients control the component cannam@130: * frequency phase-reset mechanism that may be used at transient cannam@130: * points to provide clarity and realism to percussion and other cannam@130: * significant transient sounds. These options may be changed cannam@130: * after construction when running in real-time mode, but not when cannam@130: * running in offline mode. cannam@130: * cannam@130: * \li \c OptionTransientsCrisp - Reset component phases at the cannam@130: * peak of each transient (the start of a significant note or cannam@130: * percussive event). This, the default setting, usually cannam@130: * results in a clear-sounding output; but it is not always cannam@130: * consistent, and may cause interruptions in stable sounds cannam@130: * present at the same time as transient events. The cannam@130: * OptionDetector flags (below) can be used to tune this to some cannam@130: * extent. cannam@130: * cannam@130: * \li \c OptionTransientsMixed - Reset component phases at the cannam@130: * peak of each transient, outside a frequency range typical of cannam@130: * musical fundamental frequencies. The results may be more cannam@130: * regular for mixed stable and percussive notes than cannam@130: * \c OptionTransientsCrisp, but with a "phasier" sound. The cannam@130: * balance may sound very good for certain types of music and cannam@130: * fairly bad for others. cannam@130: * cannam@130: * \li \c OptionTransientsSmooth - Do not reset component phases cannam@130: * at any point. The results will be smoother and more regular cannam@130: * but may be less clear than with either of the other cannam@130: * transients flags. cannam@130: * cannam@130: * 4. Flags prefixed \c OptionDetector control the type of cannam@130: * transient detector used. These options may be changed cannam@130: * after construction when running in real-time mode, but not when cannam@130: * running in offline mode. cannam@130: * cannam@130: * \li \c OptionDetectorCompound - Use a general-purpose cannam@130: * transient detector which is likely to be good for most cannam@130: * situations. This is the default. cannam@130: * cannam@130: * \li \c OptionDetectorPercussive - Detect percussive cannam@130: * transients. Note that this was the default and only option cannam@130: * in Rubber Band versions prior to 1.5. cannam@130: * cannam@130: * \li \c OptionDetectorSoft - Use an onset detector with less cannam@130: * of a bias toward percussive transients. This may give better cannam@130: * results with certain material (e.g. relatively monophonic cannam@130: * piano music). cannam@130: * cannam@130: * 5. Flags prefixed \c OptionPhase control the adjustment of cannam@130: * component frequency phases from one analysis window to the next cannam@130: * during non-transient segments. These options may be changed at cannam@130: * any time. cannam@130: * cannam@130: * \li \c OptionPhaseLaminar - Adjust phases when stretching in cannam@130: * such a way as to try to retain the continuity of phase cannam@130: * relationships between adjacent frequency bins whose phases cannam@130: * are behaving in similar ways. This, the default setting, cannam@130: * should give good results in most situations. cannam@130: * cannam@130: * \li \c OptionPhaseIndependent - Adjust the phase in each cannam@130: * frequency bin independently from its neighbours. This cannam@130: * usually results in a slightly softer, phasier sound. cannam@130: * cannam@130: * 6. Flags prefixed \c OptionThreading control the threading cannam@130: * model of the stretcher. These options may not be changed after cannam@130: * construction. cannam@130: * cannam@130: * \li \c OptionThreadingAuto - Permit the stretcher to cannam@130: * determine its own threading model. Usually this means using cannam@130: * one processing thread per audio channel in offline mode if cannam@130: * the stretcher is able to determine that more than one CPU is cannam@130: * available, and one thread only in realtime mode. This is the cannam@130: * defafult. cannam@130: * cannam@130: * \li \c OptionThreadingNever - Never use more than one thread. cannam@130: * cannam@130: * \li \c OptionThreadingAlways - Use multiple threads in any cannam@130: * situation where \c OptionThreadingAuto would do so, except omit cannam@130: * the check for multiple CPUs and instead assume it to be true. cannam@130: * cannam@130: * 7. Flags prefixed \c OptionWindow control the window size for cannam@130: * FFT processing. The window size actually used will depend on cannam@130: * many factors, but it can be influenced. These options may not cannam@130: * be changed after construction. cannam@130: * cannam@130: * \li \c OptionWindowStandard - Use the default window size. cannam@130: * The actual size will vary depending on other parameters. cannam@130: * This option is expected to produce better results than the cannam@130: * other window options in most situations. cannam@130: * cannam@130: * \li \c OptionWindowShort - Use a shorter window. This may cannam@130: * result in crisper sound for audio that depends strongly on cannam@130: * its timing qualities. cannam@130: * cannam@130: * \li \c OptionWindowLong - Use a longer window. This is cannam@130: * likely to result in a smoother sound at the expense of cannam@130: * clarity and timing. cannam@130: * cannam@130: * 8. Flags prefixed \c OptionSmoothing control the use of cannam@130: * window-presum FFT and time-domain smoothing. These options may cannam@130: * not be changed after construction. cannam@130: * cannam@130: * \li \c OptionSmoothingOff - Do not use time-domain smoothing. cannam@130: * This is the default. cannam@130: * cannam@130: * \li \c OptionSmoothingOn - Use time-domain smoothing. This cannam@130: * will result in a softer sound with some audible artifacts cannam@130: * around sharp transients, but it may be appropriate for longer cannam@130: * stretches of some instruments and can mix well with cannam@130: * OptionWindowShort. cannam@130: * cannam@130: * 9. Flags prefixed \c OptionFormant control the handling of cannam@130: * formant shape (spectral envelope) when pitch-shifting. These cannam@130: * options may be changed at any time. cannam@130: * cannam@130: * \li \c OptionFormantShifted - Apply no special formant cannam@130: * processing. The spectral envelope will be pitch shifted as cannam@130: * normal. This is the default. cannam@130: * cannam@130: * \li \c OptionFormantPreserved - Preserve the spectral cannam@130: * envelope of the unshifted signal. This permits shifting the cannam@130: * note frequency without so substantially affecting the cannam@130: * perceived pitch profile of the voice or instrument. cannam@130: * cannam@130: * 10. Flags prefixed \c OptionPitch control the method used for cannam@130: * pitch shifting. These options may be changed at any time. cannam@130: * They are only effective in realtime mode; in offline mode, the cannam@130: * pitch-shift method is fixed. cannam@130: * cannam@130: * \li \c OptionPitchHighSpeed - Use a method with a CPU cost cannam@130: * that is relatively moderate and predictable. This may cannam@130: * sound less clear than OptionPitchHighQuality, especially cannam@130: * for large pitch shifts. This is the default. cannam@130: cannam@130: * \li \c OptionPitchHighQuality - Use the highest quality cannam@130: * method for pitch shifting. This method has a CPU cost cannam@130: * approximately proportional to the required frequency shift. cannam@130: cannam@130: * \li \c OptionPitchHighConsistency - Use the method that gives cannam@130: * greatest consistency when used to create small variations in cannam@130: * pitch around the 1.0-ratio level. Unlike the previous two cannam@130: * options, this avoids discontinuities when moving across the cannam@130: * 1.0 pitch scale in real-time; it also consumes more CPU than cannam@130: * the others in the case where the pitch scale is exactly 1.0. cannam@130: * cannam@130: * 11. Flags prefixed \c OptionChannels control the method used for cannam@130: * processing two-channel audio. These options may not be changed cannam@130: * after construction. cannam@130: * cannam@130: * \li \c OptionChannelsApart - Each channel is processed cannam@130: * individually, though timing is synchronised and phases are cannam@130: * synchronised at transients (depending on the OptionTransients cannam@130: * setting). This gives the highest quality for the individual cannam@130: * channels but a relative lack of stereo focus and unrealistic cannam@130: * increase in "width". This is the default. cannam@130: * cannam@130: * \li \c OptionChannelsTogether - The first two channels (where cannam@130: * two or more are present) are considered to be a stereo pair cannam@130: * and are processed in mid-side format; mid and side are cannam@130: * processed individually, with timing synchronised and phases cannam@130: * synchronised at transients (depending on the OptionTransients cannam@130: * setting). This usually leads to better focus in the centre cannam@130: * but a loss of stereo space and width. Any channels beyond cannam@130: * the first two are processed individually. cannam@130: */ cannam@130: cannam@130: enum Option { cannam@130: cannam@130: OptionProcessOffline = 0x00000000, cannam@130: OptionProcessRealTime = 0x00000001, cannam@130: cannam@130: OptionStretchElastic = 0x00000000, cannam@130: OptionStretchPrecise = 0x00000010, cannam@130: cannam@130: OptionTransientsCrisp = 0x00000000, cannam@130: OptionTransientsMixed = 0x00000100, cannam@130: OptionTransientsSmooth = 0x00000200, cannam@130: cannam@130: OptionDetectorCompound = 0x00000000, cannam@130: OptionDetectorPercussive = 0x00000400, cannam@130: OptionDetectorSoft = 0x00000800, cannam@130: cannam@130: OptionPhaseLaminar = 0x00000000, cannam@130: OptionPhaseIndependent = 0x00002000, cannam@130: cannam@130: OptionThreadingAuto = 0x00000000, cannam@130: OptionThreadingNever = 0x00010000, cannam@130: OptionThreadingAlways = 0x00020000, cannam@130: cannam@130: OptionWindowStandard = 0x00000000, cannam@130: OptionWindowShort = 0x00100000, cannam@130: OptionWindowLong = 0x00200000, cannam@130: cannam@130: OptionSmoothingOff = 0x00000000, cannam@130: OptionSmoothingOn = 0x00800000, cannam@130: cannam@130: OptionFormantShifted = 0x00000000, cannam@130: OptionFormantPreserved = 0x01000000, cannam@130: cannam@130: OptionPitchHighSpeed = 0x00000000, cannam@130: OptionPitchHighQuality = 0x02000000, cannam@130: OptionPitchHighConsistency = 0x04000000, cannam@130: cannam@130: OptionChannelsApart = 0x00000000, cannam@130: OptionChannelsTogether = 0x10000000, cannam@130: cannam@130: // n.b. Options is int, so we must stop before 0x80000000 cannam@130: }; cannam@130: cannam@130: typedef int Options; cannam@130: cannam@130: enum PresetOption { cannam@130: DefaultOptions = 0x00000000, cannam@130: PercussiveOptions = 0x00102000 cannam@130: }; cannam@130: cannam@130: /** cannam@130: * Construct a time and pitch stretcher object to run at the given cannam@130: * sample rate, with the given number of channels. Processing cannam@130: * options and the time and pitch scaling ratios may be provided. cannam@130: * The time and pitch ratios may be changed after construction, cannam@130: * but most of the options may not. See the option documentation cannam@130: * above for more details. cannam@130: */ cannam@130: RubberBandStretcher(size_t sampleRate, cannam@130: size_t channels, cannam@130: Options options = DefaultOptions, cannam@130: double initialTimeRatio = 1.0, cannam@130: double initialPitchScale = 1.0); cannam@130: ~RubberBandStretcher(); cannam@130: cannam@130: /** cannam@130: * Reset the stretcher's internal buffers. The stretcher should cannam@130: * subsequently behave as if it had just been constructed cannam@130: * (although retaining the current time and pitch ratio). cannam@130: */ cannam@130: void reset(); cannam@130: cannam@130: /** cannam@130: * Set the time ratio for the stretcher. This is the ratio of cannam@130: * stretched to unstretched duration -- not tempo. For example, a cannam@130: * ratio of 2.0 would make the audio twice as long (i.e. halve the cannam@130: * tempo); 0.5 would make it half as long (i.e. double the tempo); cannam@130: * 1.0 would leave the duration unaffected. cannam@130: * cannam@130: * If the stretcher was constructed in Offline mode, the time cannam@130: * ratio is fixed throughout operation; this function may be cannam@130: * called any number of times between construction (or a call to cannam@130: * reset()) and the first call to study() or process(), but may cannam@130: * not be called after study() or process() has been called. cannam@130: * cannam@130: * If the stretcher was constructed in RealTime mode, the time cannam@130: * ratio may be varied during operation; this function may be cannam@130: * called at any time, so long as it is not called concurrently cannam@130: * with process(). You should either call this function from the cannam@130: * same thread as process(), or provide your own mutex or similar cannam@130: * mechanism to ensure that setTimeRatio and process() cannot be cannam@130: * run at once (there is no internal mutex for this purpose). cannam@130: */ cannam@130: void setTimeRatio(double ratio); cannam@130: cannam@130: /** cannam@130: * Set the pitch scaling ratio for the stretcher. This is the cannam@130: * ratio of target frequency to source frequency. For example, a cannam@130: * ratio of 2.0 would shift up by one octave; 0.5 down by one cannam@130: * octave; or 1.0 leave the pitch unaffected. cannam@130: * cannam@130: * To put this in musical terms, a pitch scaling ratio cannam@130: * corresponding to a shift of S equal-tempered semitones (where S cannam@130: * is positive for an upwards shift and negative for downwards) is cannam@130: * pow(2.0, S / 12.0). cannam@130: * cannam@130: * If the stretcher was constructed in Offline mode, the pitch cannam@130: * scaling ratio is fixed throughout operation; this function may cannam@130: * be called any number of times between construction (or a call cannam@130: * to reset()) and the first call to study() or process(), but may cannam@130: * not be called after study() or process() has been called. cannam@130: * cannam@130: * If the stretcher was constructed in RealTime mode, the pitch cannam@130: * scaling ratio may be varied during operation; this function may cannam@130: * be called at any time, so long as it is not called concurrently cannam@130: * with process(). You should either call this function from the cannam@130: * same thread as process(), or provide your own mutex or similar cannam@130: * mechanism to ensure that setPitchScale and process() cannot be cannam@130: * run at once (there is no internal mutex for this purpose). cannam@130: */ cannam@130: void setPitchScale(double scale); cannam@130: cannam@130: /** cannam@130: * Return the last time ratio value that was set (either on cannam@130: * construction or with setTimeRatio()). cannam@130: */ cannam@130: double getTimeRatio() const; cannam@130: cannam@130: /** cannam@130: * Return the last pitch scaling ratio value that was set (either cannam@130: * on construction or with setPitchScale()). cannam@130: */ cannam@130: double getPitchScale() const; cannam@130: cannam@130: /** cannam@130: * Return the processing latency of the stretcher. This is the cannam@130: * number of audio samples that one would have to discard at the cannam@130: * start of the output in order to ensure that the resulting audio cannam@130: * aligned with the input audio at the start. In Offline mode, cannam@130: * latency is automatically adjusted for and the result is zero. cannam@130: * In RealTime mode, the latency may depend on the time and pitch cannam@130: * ratio and other options. cannam@130: */ cannam@130: size_t getLatency() const; cannam@130: cannam@130: /** cannam@130: * Change an OptionTransients configuration setting. This may be cannam@130: * called at any time in RealTime mode. It may not be called in cannam@130: * Offline mode (for which the transients option is fixed on cannam@130: * construction). cannam@130: */ cannam@130: void setTransientsOption(Options options); cannam@130: cannam@130: /** cannam@130: * Change an OptionDetector configuration setting. This may be cannam@130: * called at any time in RealTime mode. It may not be called in cannam@130: * Offline mode (for which the detector option is fixed on cannam@130: * construction). cannam@130: */ cannam@130: void setDetectorOption(Options options); cannam@130: cannam@130: /** cannam@130: * Change an OptionPhase configuration setting. This may be cannam@130: * called at any time in any mode. cannam@130: * cannam@130: * Note that if running multi-threaded in Offline mode, the change cannam@130: * may not take effect immediately if processing is already under cannam@130: * way when this function is called. cannam@130: */ cannam@130: void setPhaseOption(Options options); cannam@130: cannam@130: /** cannam@130: * Change an OptionFormant configuration setting. This may be cannam@130: * called at any time in any mode. cannam@130: * cannam@130: * Note that if running multi-threaded in Offline mode, the change cannam@130: * may not take effect immediately if processing is already under cannam@130: * way when this function is called. cannam@130: */ cannam@130: void setFormantOption(Options options); cannam@130: cannam@130: /** cannam@130: * Change an OptionPitch configuration setting. This may be cannam@130: * called at any time in RealTime mode. It may not be called in cannam@130: * Offline mode (for which the transients option is fixed on cannam@130: * construction). cannam@130: */ cannam@130: void setPitchOption(Options options); cannam@130: cannam@130: /** cannam@130: * Tell the stretcher exactly how many input sample frames it will cannam@130: * receive. This is only useful in Offline mode, when it allows cannam@130: * the stretcher to ensure that the number of output samples is cannam@130: * exactly correct. In RealTime mode no such guarantee is cannam@130: * possible and this value is ignored. cannam@130: * cannam@130: * Note that the value of "samples" refers to the number of audio cannam@130: * sample frames, which may be multi-channel, not the number of cannam@130: * individual samples. (For example, one second of stereo audio cannam@130: * sampled at 44100Hz yields a value of 44100 sample frames, not cannam@130: * 88200.) This rule applies throughout the Rubber Band API. cannam@130: */ cannam@130: void setExpectedInputDuration(size_t samples); cannam@130: cannam@130: /** cannam@130: * Tell the stretcher the maximum number of sample frames that you cannam@130: * will ever be passing in to a single process() call. If you cannam@130: * don't call this, the stretcher will assume that you are calling cannam@130: * getSamplesRequired() at each cycle and are never passing more cannam@130: * samples than are suggested by that function. cannam@130: * cannam@130: * If your application has some external constraint that means you cannam@130: * prefer a fixed block size, then your normal mode of operation cannam@130: * would be to provide that block size to this function; to loop cannam@130: * calling process() with that size of block; after each call to cannam@130: * process(), test whether output has been generated by calling cannam@130: * available(); and, if so, call retrieve() to obtain it. See cannam@130: * getSamplesRequired() for a more suitable operating mode for cannam@130: * applications without such external constraints. cannam@130: * cannam@130: * This function may not be called after the first call to study() cannam@130: * or process(). cannam@130: * cannam@130: * Note that this value is only relevant to process(), not to cannam@130: * study() (to which you may pass any number of samples at a time, cannam@130: * and from which there is no output). cannam@130: * cannam@130: * Note that the value of "samples" refers to the number of audio cannam@130: * sample frames, which may be multi-channel, not the number of cannam@130: * individual samples. (For example, one second of stereo audio cannam@130: * sampled at 44100Hz yields a value of 44100 sample frames, not cannam@130: * 88200.) This rule applies throughout the Rubber Band API. cannam@130: */ cannam@130: void setMaxProcessSize(size_t samples); cannam@130: cannam@130: /** cannam@130: * Ask the stretcher how many audio sample frames should be cannam@130: * provided as input in order to ensure that some more output cannam@130: * becomes available. cannam@130: * cannam@130: * If your application has no particular constraint on processing cannam@130: * block size and you are able to provide any block size as input cannam@130: * for each cycle, then your normal mode of operation would be to cannam@130: * loop querying this function; providing that number of samples cannam@130: * to process(); and reading the output using available() and cannam@130: * retrieve(). See setMaxProcessSize() for a more suitable cannam@130: * operating mode for applications that do have external block cannam@130: * size constraints. cannam@130: * cannam@130: * Note that this value is only relevant to process(), not to cannam@130: * study() (to which you may pass any number of samples at a time, cannam@130: * and from which there is no output). cannam@130: * cannam@130: * Note that the return value refers to the number of audio sample cannam@130: * frames, which may be multi-channel, not the number of cannam@130: * individual samples. (For example, one second of stereo audio cannam@130: * sampled at 44100Hz yields a value of 44100 sample frames, not cannam@130: * 88200.) This rule applies throughout the Rubber Band API. cannam@130: */ cannam@130: size_t getSamplesRequired() const; cannam@130: cannam@130: /** cannam@130: * Provide a set of mappings from "before" to "after" sample cannam@130: * numbers so as to enforce a particular stretch profile. The cannam@130: * argument is a map from audio sample frame number in the source cannam@130: * material, to the corresponding sample frame number in the cannam@130: * stretched output. The mapping should be for key frames only, cannam@130: * with a "reasonable" gap between mapped samples. cannam@130: * cannam@130: * This function cannot be used in RealTime mode. cannam@130: * cannam@130: * This function may not be called after the first call to cannam@130: * process(). It should be called after the time and pitch ratios cannam@130: * have been set; the results of changing the time and pitch cannam@130: * ratios after calling this function are undefined. Calling cannam@130: * reset() will clear this mapping. cannam@130: * cannam@130: * The key frame map only affects points within the material; it cannam@130: * does not determine the overall stretch ratio (that is, the cannam@130: * ratio between the output material's duration and the source cannam@130: * material's duration). You need to provide this ratio cannam@130: * separately to setTimeRatio(), otherwise the results may be cannam@130: * truncated or extended in unexpected ways regardless of the cannam@130: * extent of the frame numbers found in the key frame map. cannam@130: */ cannam@130: void setKeyFrameMap(const std::map &); cannam@130: cannam@130: /** cannam@130: * Provide a block of "samples" sample frames for the stretcher to cannam@130: * study and calculate a stretch profile from. cannam@130: * cannam@130: * This is only meaningful in Offline mode, and is required if cannam@130: * running in that mode. You should pass the entire input through cannam@130: * study() before any process() calls are made, as a sequence of cannam@130: * blocks in individual study() calls, or as a single large block. cannam@130: * cannam@130: * "input" should point to de-interleaved audio data with one cannam@130: * float array per channel. Sample values are conventionally cannam@130: * expected to be in the range -1.0f to +1.0f. "samples" supplies cannam@130: * the number of audio sample frames available in "input". If cannam@130: * "samples" is zero, "input" may be NULL. cannam@130: * cannam@130: * Note that the value of "samples" refers to the number of audio cannam@130: * sample frames, which may be multi-channel, not the number of cannam@130: * individual samples. (For example, one second of stereo audio cannam@130: * sampled at 44100Hz yields a value of 44100 sample frames, not cannam@130: * 88200.) This rule applies throughout the Rubber Band API. cannam@130: * cannam@130: * Set "final" to true if this is the last block of data that will cannam@130: * be provided to study() before the first process() call. cannam@130: */ cannam@130: void study(const float *const *input, size_t samples, bool final); cannam@130: cannam@130: /** cannam@130: * Provide a block of "samples" sample frames for processing. cannam@130: * See also getSamplesRequired() and setMaxProcessSize(). cannam@130: * cannam@130: * "input" should point to de-interleaved audio data with one cannam@130: * float array per channel. Sample values are conventionally cannam@130: * expected to be in the range -1.0f to +1.0f. "samples" supplies cannam@130: * the number of audio sample frames available in "input". cannam@130: * cannam@130: * Note that the value of "samples" refers to the number of audio cannam@130: * sample frames, which may be multi-channel, not the number of cannam@130: * individual samples. (For example, one second of stereo audio cannam@130: * sampled at 44100Hz yields a value of 44100 sample frames, not cannam@130: * 88200.) This rule applies throughout the Rubber Band API. cannam@130: * cannam@130: * Set "final" to true if this is the last block of input data. cannam@130: */ cannam@130: void process(const float *const *input, size_t samples, bool final); cannam@130: cannam@130: /** cannam@130: * Ask the stretcher how many audio sample frames of output data cannam@130: * are available for reading (via retrieve()). cannam@130: * cannam@130: * This function returns 0 if no frames are available: this cannam@130: * usually means more input data needs to be provided, but if the cannam@130: * stretcher is running in threaded mode it may just mean that not cannam@130: * enough data has yet been processed. Call getSamplesRequired() cannam@130: * to discover whether more input is needed. cannam@130: * cannam@130: * Note that the return value refers to the number of audio sample cannam@130: * frames, which may be multi-channel, not the number of cannam@130: * individual samples. (For example, one second of stereo audio cannam@130: * sampled at 44100Hz yields a value of 44100 sample frames, not cannam@130: * 88200.) This rule applies throughout the Rubber Band API. cannam@130: * cannam@130: * This function returns -1 if all data has been fully processed cannam@130: * and all output read, and the stretch process is now finished. cannam@130: */ cannam@130: int available() const; cannam@130: cannam@130: /** cannam@130: * Obtain some processed output data from the stretcher. Up to cannam@130: * "samples" samples will be stored in the output arrays (one per cannam@130: * channel for de-interleaved audio data) pointed to by "output". cannam@130: * The return value is the actual number of sample frames cannam@130: * retrieved. cannam@130: * cannam@130: * Note that the value of "samples" and the return value refer to cannam@130: * the number of audio sample frames, which may be multi-channel, cannam@130: * not the number of individual samples. (For example, one second cannam@130: * of stereo audio sampled at 44100Hz yields a value of 44100 cannam@130: * sample frames, not 88200.) This rule applies throughout the cannam@130: * Rubber Band API. cannam@130: */ cannam@130: size_t retrieve(float *const *output, size_t samples) const; cannam@130: cannam@130: /** cannam@130: * Return the value of internal frequency cutoff value n. cannam@130: * cannam@130: * This function is not for general use. cannam@130: */ cannam@130: float getFrequencyCutoff(int n) const; cannam@130: cannam@130: /** cannam@130: * Set the value of internal frequency cutoff n to f Hz. cannam@130: * cannam@130: * This function is not for general use. cannam@130: */ cannam@130: void setFrequencyCutoff(int n, float f); cannam@130: cannam@130: /** cannam@130: * Retrieve the value of the internal input block increment value. cannam@130: * cannam@130: * This function is provided for diagnostic purposes only. cannam@130: */ cannam@130: size_t getInputIncrement() const; cannam@130: cannam@130: /** cannam@130: * In offline mode, retrieve the sequence of internal block cannam@130: * increments for output, for the entire audio data, provided the cannam@130: * stretch profile has been calculated. In realtime mode, cannam@130: * retrieve any output increments that have accumulated since the cannam@130: * last call to getOutputIncrements, to a limit of 16. cannam@130: * cannam@130: * This function is provided for diagnostic purposes only. cannam@130: */ cannam@130: std::vector getOutputIncrements() const; cannam@130: cannam@130: /** cannam@130: * In offline mode, retrieve the sequence of internal phase reset cannam@130: * detection function values, for the entire audio data, provided cannam@130: * the stretch profile has been calculated. In realtime mode, cannam@130: * retrieve any phase reset points that have accumulated since the cannam@130: * last call to getPhaseResetCurve, to a limit of 16. cannam@130: * cannam@130: * This function is provided for diagnostic purposes only. cannam@130: */ cannam@130: std::vector getPhaseResetCurve() const; cannam@130: cannam@130: /** cannam@130: * In offline mode, retrieve the sequence of internal frames for cannam@130: * which exact timing has been sought, for the entire audio data, cannam@130: * provided the stretch profile has been calculated. In realtime cannam@130: * mode, return an empty sequence. cannam@130: * cannam@130: * This function is provided for diagnostic purposes only. cannam@130: */ cannam@130: std::vector getExactTimePoints() const; cannam@130: cannam@130: /** cannam@130: * Return the number of channels this stretcher was constructed cannam@130: * with. cannam@130: */ cannam@130: size_t getChannelCount() const; cannam@130: cannam@130: /** cannam@130: * Force the stretcher to calculate a stretch profile. Normally cannam@130: * this happens automatically for the first process() call in cannam@130: * offline mode. cannam@130: * cannam@130: * This function is provided for diagnostic purposes only. cannam@130: */ cannam@130: void calculateStretch(); cannam@130: cannam@130: /** cannam@130: * Set the level of debug output. The value may be from 0 (errors cannam@130: * only) to 3 (very verbose, with audible ticks in the output at cannam@130: * phase reset points). The default is whatever has been set cannam@130: * using setDefaultDebugLevel, or 0 if that function has not been cannam@130: * called. cannam@130: */ cannam@130: void setDebugLevel(int level); cannam@130: cannam@130: /** cannam@130: * Set the default level of debug output for subsequently cannam@130: * constructed stretchers. cannam@130: * cannam@130: * @see setDebugLevel cannam@130: */ cannam@130: static void setDefaultDebugLevel(int level); cannam@130: cannam@130: protected: cannam@130: class Impl; cannam@130: Impl *m_d; cannam@130: }; cannam@130: cannam@130: } cannam@130: cannam@130: #endif