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