annotate src/rubberband-1.8.1/rubberband/RubberBandStretcher.h @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 89f5e221ed7b
children
rev   line source
cannam@95 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@95 2
cannam@95 3 /*
cannam@95 4 Rubber Band Library
cannam@95 5 An audio time-stretching and pitch-shifting library.
cannam@95 6 Copyright 2007-2012 Particular Programs Ltd.
cannam@95 7
cannam@95 8 This program is free software; you can redistribute it and/or
cannam@95 9 modify it under the terms of the GNU General Public License as
cannam@95 10 published by the Free Software Foundation; either version 2 of the
cannam@95 11 License, or (at your option) any later version. See the file
cannam@95 12 COPYING included with this distribution for more information.
cannam@95 13
cannam@95 14 Alternatively, if you have a valid commercial licence for the
cannam@95 15 Rubber Band Library obtained by agreement with the copyright
cannam@95 16 holders, you may redistribute and/or modify it under the terms
cannam@95 17 described in that licence.
cannam@95 18
cannam@95 19 If you wish to distribute code using the Rubber Band Library
cannam@95 20 under terms other than those of the GNU General Public License,
cannam@95 21 you must obtain a valid commercial licence before doing so.
cannam@95 22 */
cannam@95 23
cannam@95 24 #ifndef _RUBBERBANDSTRETCHER_H_
cannam@95 25 #define _RUBBERBANDSTRETCHER_H_
cannam@95 26
cannam@95 27 #define RUBBERBAND_VERSION "1.8.1"
cannam@95 28 #define RUBBERBAND_API_MAJOR_VERSION 2
cannam@95 29 #define RUBBERBAND_API_MINOR_VERSION 5
cannam@95 30
cannam@95 31 #include <vector>
cannam@95 32 #include <map>
cannam@95 33 #include <cstddef>
cannam@95 34
cannam@95 35 /**
cannam@95 36 * @mainpage RubberBand
cannam@95 37 *
cannam@95 38 * The Rubber Band API is contained in the single class
cannam@95 39 * RubberBand::RubberBandStretcher.
cannam@95 40 *
cannam@95 41 * Threading notes for real-time applications:
cannam@95 42 *
cannam@95 43 * Multiple instances of RubberBandStretcher may be created and used
cannam@95 44 * in separate threads concurrently. However, for any single instance
cannam@95 45 * of RubberBandStretcher, you may not call process() more than once
cannam@95 46 * concurrently, and you may not change the time or pitch ratio while
cannam@95 47 * a process() call is being executed (if the stretcher was created in
cannam@95 48 * "real-time mode"; in "offline mode" you can't change the ratios
cannam@95 49 * during use anyway).
cannam@95 50 *
cannam@95 51 * So you can run process() in its own thread if you like, but if you
cannam@95 52 * want to change ratios dynamically from a different thread, you will
cannam@95 53 * need some form of mutex in your code. Changing the time or pitch
cannam@95 54 * ratio is real-time safe except in extreme circumstances, so for
cannam@95 55 * most applications that may change these dynamically it probably
cannam@95 56 * makes most sense to do so from the same thread as calls process(),
cannam@95 57 * even if that is a real-time thread.
cannam@95 58 */
cannam@95 59
cannam@95 60 namespace RubberBand
cannam@95 61 {
cannam@95 62
cannam@95 63 class RubberBandStretcher
cannam@95 64 {
cannam@95 65 public:
cannam@95 66 /**
cannam@95 67 * Processing options for the timestretcher. The preferred
cannam@95 68 * options should normally be set in the constructor, as a bitwise
cannam@95 69 * OR of the option flags. The default value (DefaultOptions) is
cannam@95 70 * intended to give good results in most situations.
cannam@95 71 *
cannam@95 72 * 1. Flags prefixed \c OptionProcess determine how the timestretcher
cannam@95 73 * will be invoked. These options may not be changed after
cannam@95 74 * construction.
cannam@95 75 *
cannam@95 76 * \li \c OptionProcessOffline - Run the stretcher in offline
cannam@95 77 * mode. In this mode the input data needs to be provided
cannam@95 78 * twice, once to study(), which calculates a stretch profile
cannam@95 79 * for the audio, and once to process(), which stretches it.
cannam@95 80 *
cannam@95 81 * \li \c OptionProcessRealTime - Run the stretcher in real-time
cannam@95 82 * mode. In this mode only process() should be called, and the
cannam@95 83 * stretcher adjusts dynamically in response to the input audio.
cannam@95 84 *
cannam@95 85 * The Process setting is likely to depend on your architecture:
cannam@95 86 * non-real-time operation on seekable files: Offline; real-time
cannam@95 87 * or streaming operation: RealTime.
cannam@95 88 *
cannam@95 89 * 2. Flags prefixed \c OptionStretch control the profile used for
cannam@95 90 * variable timestretching. Rubber Band always adjusts the
cannam@95 91 * stretch profile to minimise stretching of busy broadband
cannam@95 92 * transient sounds, but the degree to which it does so is
cannam@95 93 * adjustable. These options may not be changed after
cannam@95 94 * construction.
cannam@95 95 *
cannam@95 96 * \li \c OptionStretchElastic - Only meaningful in offline
cannam@95 97 * mode, and the default in that mode. The audio will be
cannam@95 98 * stretched at a variable rate, aimed at preserving the quality
cannam@95 99 * of transient sounds as much as possible. The timings of low
cannam@95 100 * activity regions between transients may be less exact than
cannam@95 101 * when the precise flag is set.
cannam@95 102 *
cannam@95 103 * \li \c OptionStretchPrecise - Although still using a variable
cannam@95 104 * stretch rate, the audio will be stretched so as to maintain
cannam@95 105 * as close as possible to a linear stretch ratio throughout.
cannam@95 106 * Timing may be better than when using \c OptionStretchElastic, at
cannam@95 107 * slight cost to the sound quality of transients. This setting
cannam@95 108 * is always used when running in real-time mode.
cannam@95 109 *
cannam@95 110 * 3. Flags prefixed \c OptionTransients control the component
cannam@95 111 * frequency phase-reset mechanism that may be used at transient
cannam@95 112 * points to provide clarity and realism to percussion and other
cannam@95 113 * significant transient sounds. These options may be changed
cannam@95 114 * after construction when running in real-time mode, but not when
cannam@95 115 * running in offline mode.
cannam@95 116 *
cannam@95 117 * \li \c OptionTransientsCrisp - Reset component phases at the
cannam@95 118 * peak of each transient (the start of a significant note or
cannam@95 119 * percussive event). This, the default setting, usually
cannam@95 120 * results in a clear-sounding output; but it is not always
cannam@95 121 * consistent, and may cause interruptions in stable sounds
cannam@95 122 * present at the same time as transient events. The
cannam@95 123 * OptionDetector flags (below) can be used to tune this to some
cannam@95 124 * extent.
cannam@95 125 *
cannam@95 126 * \li \c OptionTransientsMixed - Reset component phases at the
cannam@95 127 * peak of each transient, outside a frequency range typical of
cannam@95 128 * musical fundamental frequencies. The results may be more
cannam@95 129 * regular for mixed stable and percussive notes than
cannam@95 130 * \c OptionTransientsCrisp, but with a "phasier" sound. The
cannam@95 131 * balance may sound very good for certain types of music and
cannam@95 132 * fairly bad for others.
cannam@95 133 *
cannam@95 134 * \li \c OptionTransientsSmooth - Do not reset component phases
cannam@95 135 * at any point. The results will be smoother and more regular
cannam@95 136 * but may be less clear than with either of the other
cannam@95 137 * transients flags.
cannam@95 138 *
cannam@95 139 * 4. Flags prefixed \c OptionDetector control the type of
cannam@95 140 * transient detector used. These options may be changed
cannam@95 141 * after construction when running in real-time mode, but not when
cannam@95 142 * running in offline mode.
cannam@95 143 *
cannam@95 144 * \li \c OptionDetectorCompound - Use a general-purpose
cannam@95 145 * transient detector which is likely to be good for most
cannam@95 146 * situations. This is the default.
cannam@95 147 *
cannam@95 148 * \li \c OptionDetectorPercussive - Detect percussive
cannam@95 149 * transients. Note that this was the default and only option
cannam@95 150 * in Rubber Band versions prior to 1.5.
cannam@95 151 *
cannam@95 152 * \li \c OptionDetectorSoft - Use an onset detector with less
cannam@95 153 * of a bias toward percussive transients. This may give better
cannam@95 154 * results with certain material (e.g. relatively monophonic
cannam@95 155 * piano music).
cannam@95 156 *
cannam@95 157 * 5. Flags prefixed \c OptionPhase control the adjustment of
cannam@95 158 * component frequency phases from one analysis window to the next
cannam@95 159 * during non-transient segments. These options may be changed at
cannam@95 160 * any time.
cannam@95 161 *
cannam@95 162 * \li \c OptionPhaseLaminar - Adjust phases when stretching in
cannam@95 163 * such a way as to try to retain the continuity of phase
cannam@95 164 * relationships between adjacent frequency bins whose phases
cannam@95 165 * are behaving in similar ways. This, the default setting,
cannam@95 166 * should give good results in most situations.
cannam@95 167 *
cannam@95 168 * \li \c OptionPhaseIndependent - Adjust the phase in each
cannam@95 169 * frequency bin independently from its neighbours. This
cannam@95 170 * usually results in a slightly softer, phasier sound.
cannam@95 171 *
cannam@95 172 * 6. Flags prefixed \c OptionThreading control the threading
cannam@95 173 * model of the stretcher. These options may not be changed after
cannam@95 174 * construction.
cannam@95 175 *
cannam@95 176 * \li \c OptionThreadingAuto - Permit the stretcher to
cannam@95 177 * determine its own threading model. Usually this means using
cannam@95 178 * one processing thread per audio channel in offline mode if
cannam@95 179 * the stretcher is able to determine that more than one CPU is
cannam@95 180 * available, and one thread only in realtime mode. This is the
cannam@95 181 * defafult.
cannam@95 182 *
cannam@95 183 * \li \c OptionThreadingNever - Never use more than one thread.
cannam@95 184 *
cannam@95 185 * \li \c OptionThreadingAlways - Use multiple threads in any
cannam@95 186 * situation where \c OptionThreadingAuto would do so, except omit
cannam@95 187 * the check for multiple CPUs and instead assume it to be true.
cannam@95 188 *
cannam@95 189 * 7. Flags prefixed \c OptionWindow control the window size for
cannam@95 190 * FFT processing. The window size actually used will depend on
cannam@95 191 * many factors, but it can be influenced. These options may not
cannam@95 192 * be changed after construction.
cannam@95 193 *
cannam@95 194 * \li \c OptionWindowStandard - Use the default window size.
cannam@95 195 * The actual size will vary depending on other parameters.
cannam@95 196 * This option is expected to produce better results than the
cannam@95 197 * other window options in most situations.
cannam@95 198 *
cannam@95 199 * \li \c OptionWindowShort - Use a shorter window. This may
cannam@95 200 * result in crisper sound for audio that depends strongly on
cannam@95 201 * its timing qualities.
cannam@95 202 *
cannam@95 203 * \li \c OptionWindowLong - Use a longer window. This is
cannam@95 204 * likely to result in a smoother sound at the expense of
cannam@95 205 * clarity and timing.
cannam@95 206 *
cannam@95 207 * 8. Flags prefixed \c OptionSmoothing control the use of
cannam@95 208 * window-presum FFT and time-domain smoothing. These options may
cannam@95 209 * not be changed after construction.
cannam@95 210 *
cannam@95 211 * \li \c OptionSmoothingOff - Do not use time-domain smoothing.
cannam@95 212 * This is the default.
cannam@95 213 *
cannam@95 214 * \li \c OptionSmoothingOn - Use time-domain smoothing. This
cannam@95 215 * will result in a softer sound with some audible artifacts
cannam@95 216 * around sharp transients, but it may be appropriate for longer
cannam@95 217 * stretches of some instruments and can mix well with
cannam@95 218 * OptionWindowShort.
cannam@95 219 *
cannam@95 220 * 9. Flags prefixed \c OptionFormant control the handling of
cannam@95 221 * formant shape (spectral envelope) when pitch-shifting. These
cannam@95 222 * options may be changed at any time.
cannam@95 223 *
cannam@95 224 * \li \c OptionFormantShifted - Apply no special formant
cannam@95 225 * processing. The spectral envelope will be pitch shifted as
cannam@95 226 * normal. This is the default.
cannam@95 227 *
cannam@95 228 * \li \c OptionFormantPreserved - Preserve the spectral
cannam@95 229 * envelope of the unshifted signal. This permits shifting the
cannam@95 230 * note frequency without so substantially affecting the
cannam@95 231 * perceived pitch profile of the voice or instrument.
cannam@95 232 *
cannam@95 233 * 10. Flags prefixed \c OptionPitch control the method used for
cannam@95 234 * pitch shifting. These options may be changed at any time.
cannam@95 235 * They are only effective in realtime mode; in offline mode, the
cannam@95 236 * pitch-shift method is fixed.
cannam@95 237 *
cannam@95 238 * \li \c OptionPitchHighSpeed - Use a method with a CPU cost
cannam@95 239 * that is relatively moderate and predictable. This may
cannam@95 240 * sound less clear than OptionPitchHighQuality, especially
cannam@95 241 * for large pitch shifts. This is the default.
cannam@95 242
cannam@95 243 * \li \c OptionPitchHighQuality - Use the highest quality
cannam@95 244 * method for pitch shifting. This method has a CPU cost
cannam@95 245 * approximately proportional to the required frequency shift.
cannam@95 246
cannam@95 247 * \li \c OptionPitchHighConsistency - Use the method that gives
cannam@95 248 * greatest consistency when used to create small variations in
cannam@95 249 * pitch around the 1.0-ratio level. Unlike the previous two
cannam@95 250 * options, this avoids discontinuities when moving across the
cannam@95 251 * 1.0 pitch scale in real-time; it also consumes more CPU than
cannam@95 252 * the others in the case where the pitch scale is exactly 1.0.
cannam@95 253 *
cannam@95 254 * 11. Flags prefixed \c OptionChannels control the method used for
cannam@95 255 * processing two-channel audio. These options may not be changed
cannam@95 256 * after construction.
cannam@95 257 *
cannam@95 258 * \li \c OptionChannelsApart - Each channel is processed
cannam@95 259 * individually, though timing is synchronised and phases are
cannam@95 260 * synchronised at transients (depending on the OptionTransients
cannam@95 261 * setting). This gives the highest quality for the individual
cannam@95 262 * channels but a relative lack of stereo focus and unrealistic
cannam@95 263 * increase in "width". This is the default.
cannam@95 264 *
cannam@95 265 * \li \c OptionChannelsTogether - The first two channels (where
cannam@95 266 * two or more are present) are considered to be a stereo pair
cannam@95 267 * and are processed in mid-side format; mid and side are
cannam@95 268 * processed individually, with timing synchronised and phases
cannam@95 269 * synchronised at transients (depending on the OptionTransients
cannam@95 270 * setting). This usually leads to better focus in the centre
cannam@95 271 * but a loss of stereo space and width. Any channels beyond
cannam@95 272 * the first two are processed individually.
cannam@95 273 */
cannam@95 274
cannam@95 275 enum Option {
cannam@95 276
cannam@95 277 OptionProcessOffline = 0x00000000,
cannam@95 278 OptionProcessRealTime = 0x00000001,
cannam@95 279
cannam@95 280 OptionStretchElastic = 0x00000000,
cannam@95 281 OptionStretchPrecise = 0x00000010,
cannam@95 282
cannam@95 283 OptionTransientsCrisp = 0x00000000,
cannam@95 284 OptionTransientsMixed = 0x00000100,
cannam@95 285 OptionTransientsSmooth = 0x00000200,
cannam@95 286
cannam@95 287 OptionDetectorCompound = 0x00000000,
cannam@95 288 OptionDetectorPercussive = 0x00000400,
cannam@95 289 OptionDetectorSoft = 0x00000800,
cannam@95 290
cannam@95 291 OptionPhaseLaminar = 0x00000000,
cannam@95 292 OptionPhaseIndependent = 0x00002000,
cannam@95 293
cannam@95 294 OptionThreadingAuto = 0x00000000,
cannam@95 295 OptionThreadingNever = 0x00010000,
cannam@95 296 OptionThreadingAlways = 0x00020000,
cannam@95 297
cannam@95 298 OptionWindowStandard = 0x00000000,
cannam@95 299 OptionWindowShort = 0x00100000,
cannam@95 300 OptionWindowLong = 0x00200000,
cannam@95 301
cannam@95 302 OptionSmoothingOff = 0x00000000,
cannam@95 303 OptionSmoothingOn = 0x00800000,
cannam@95 304
cannam@95 305 OptionFormantShifted = 0x00000000,
cannam@95 306 OptionFormantPreserved = 0x01000000,
cannam@95 307
cannam@95 308 OptionPitchHighSpeed = 0x00000000,
cannam@95 309 OptionPitchHighQuality = 0x02000000,
cannam@95 310 OptionPitchHighConsistency = 0x04000000,
cannam@95 311
cannam@95 312 OptionChannelsApart = 0x00000000,
cannam@95 313 OptionChannelsTogether = 0x10000000,
cannam@95 314
cannam@95 315 // n.b. Options is int, so we must stop before 0x80000000
cannam@95 316 };
cannam@95 317
cannam@95 318 typedef int Options;
cannam@95 319
cannam@95 320 enum PresetOption {
cannam@95 321 DefaultOptions = 0x00000000,
cannam@95 322 PercussiveOptions = 0x00102000
cannam@95 323 };
cannam@95 324
cannam@95 325 /**
cannam@95 326 * Construct a time and pitch stretcher object to run at the given
cannam@95 327 * sample rate, with the given number of channels. Processing
cannam@95 328 * options and the time and pitch scaling ratios may be provided.
cannam@95 329 * The time and pitch ratios may be changed after construction,
cannam@95 330 * but most of the options may not. See the option documentation
cannam@95 331 * above for more details.
cannam@95 332 */
cannam@95 333 RubberBandStretcher(size_t sampleRate,
cannam@95 334 size_t channels,
cannam@95 335 Options options = DefaultOptions,
cannam@95 336 double initialTimeRatio = 1.0,
cannam@95 337 double initialPitchScale = 1.0);
cannam@95 338 ~RubberBandStretcher();
cannam@95 339
cannam@95 340 /**
cannam@95 341 * Reset the stretcher's internal buffers. The stretcher should
cannam@95 342 * subsequently behave as if it had just been constructed
cannam@95 343 * (although retaining the current time and pitch ratio).
cannam@95 344 */
cannam@95 345 void reset();
cannam@95 346
cannam@95 347 /**
cannam@95 348 * Set the time ratio for the stretcher. This is the ratio of
cannam@95 349 * stretched to unstretched duration -- not tempo. For example, a
cannam@95 350 * ratio of 2.0 would make the audio twice as long (i.e. halve the
cannam@95 351 * tempo); 0.5 would make it half as long (i.e. double the tempo);
cannam@95 352 * 1.0 would leave the duration unaffected.
cannam@95 353 *
cannam@95 354 * If the stretcher was constructed in Offline mode, the time
cannam@95 355 * ratio is fixed throughout operation; this function may be
cannam@95 356 * called any number of times between construction (or a call to
cannam@95 357 * reset()) and the first call to study() or process(), but may
cannam@95 358 * not be called after study() or process() has been called.
cannam@95 359 *
cannam@95 360 * If the stretcher was constructed in RealTime mode, the time
cannam@95 361 * ratio may be varied during operation; this function may be
cannam@95 362 * called at any time, so long as it is not called concurrently
cannam@95 363 * with process(). You should either call this function from the
cannam@95 364 * same thread as process(), or provide your own mutex or similar
cannam@95 365 * mechanism to ensure that setTimeRatio and process() cannot be
cannam@95 366 * run at once (there is no internal mutex for this purpose).
cannam@95 367 */
cannam@95 368 void setTimeRatio(double ratio);
cannam@95 369
cannam@95 370 /**
cannam@95 371 * Set the pitch scaling ratio for the stretcher. This is the
cannam@95 372 * ratio of target frequency to source frequency. For example, a
cannam@95 373 * ratio of 2.0 would shift up by one octave; 0.5 down by one
cannam@95 374 * octave; or 1.0 leave the pitch unaffected.
cannam@95 375 *
cannam@95 376 * To put this in musical terms, a pitch scaling ratio
cannam@95 377 * corresponding to a shift of S equal-tempered semitones (where S
cannam@95 378 * is positive for an upwards shift and negative for downwards) is
cannam@95 379 * pow(2.0, S / 12.0).
cannam@95 380 *
cannam@95 381 * If the stretcher was constructed in Offline mode, the pitch
cannam@95 382 * scaling ratio is fixed throughout operation; this function may
cannam@95 383 * be called any number of times between construction (or a call
cannam@95 384 * to reset()) and the first call to study() or process(), but may
cannam@95 385 * not be called after study() or process() has been called.
cannam@95 386 *
cannam@95 387 * If the stretcher was constructed in RealTime mode, the pitch
cannam@95 388 * scaling ratio may be varied during operation; this function may
cannam@95 389 * be called at any time, so long as it is not called concurrently
cannam@95 390 * with process(). You should either call this function from the
cannam@95 391 * same thread as process(), or provide your own mutex or similar
cannam@95 392 * mechanism to ensure that setPitchScale and process() cannot be
cannam@95 393 * run at once (there is no internal mutex for this purpose).
cannam@95 394 */
cannam@95 395 void setPitchScale(double scale);
cannam@95 396
cannam@95 397 /**
cannam@95 398 * Return the last time ratio value that was set (either on
cannam@95 399 * construction or with setTimeRatio()).
cannam@95 400 */
cannam@95 401 double getTimeRatio() const;
cannam@95 402
cannam@95 403 /**
cannam@95 404 * Return the last pitch scaling ratio value that was set (either
cannam@95 405 * on construction or with setPitchScale()).
cannam@95 406 */
cannam@95 407 double getPitchScale() const;
cannam@95 408
cannam@95 409 /**
cannam@95 410 * Return the processing latency of the stretcher. This is the
cannam@95 411 * number of audio samples that one would have to discard at the
cannam@95 412 * start of the output in order to ensure that the resulting audio
cannam@95 413 * aligned with the input audio at the start. In Offline mode,
cannam@95 414 * latency is automatically adjusted for and the result is zero.
cannam@95 415 * In RealTime mode, the latency may depend on the time and pitch
cannam@95 416 * ratio and other options.
cannam@95 417 */
cannam@95 418 size_t getLatency() const;
cannam@95 419
cannam@95 420 /**
cannam@95 421 * Change an OptionTransients configuration setting. This may be
cannam@95 422 * called at any time in RealTime mode. It may not be called in
cannam@95 423 * Offline mode (for which the transients option is fixed on
cannam@95 424 * construction).
cannam@95 425 */
cannam@95 426 void setTransientsOption(Options options);
cannam@95 427
cannam@95 428 /**
cannam@95 429 * Change an OptionDetector configuration setting. This may be
cannam@95 430 * called at any time in RealTime mode. It may not be called in
cannam@95 431 * Offline mode (for which the detector option is fixed on
cannam@95 432 * construction).
cannam@95 433 */
cannam@95 434 void setDetectorOption(Options options);
cannam@95 435
cannam@95 436 /**
cannam@95 437 * Change an OptionPhase configuration setting. This may be
cannam@95 438 * called at any time in any mode.
cannam@95 439 *
cannam@95 440 * Note that if running multi-threaded in Offline mode, the change
cannam@95 441 * may not take effect immediately if processing is already under
cannam@95 442 * way when this function is called.
cannam@95 443 */
cannam@95 444 void setPhaseOption(Options options);
cannam@95 445
cannam@95 446 /**
cannam@95 447 * Change an OptionFormant configuration setting. This may be
cannam@95 448 * called at any time in any mode.
cannam@95 449 *
cannam@95 450 * Note that if running multi-threaded in Offline mode, the change
cannam@95 451 * may not take effect immediately if processing is already under
cannam@95 452 * way when this function is called.
cannam@95 453 */
cannam@95 454 void setFormantOption(Options options);
cannam@95 455
cannam@95 456 /**
cannam@95 457 * Change an OptionPitch configuration setting. This may be
cannam@95 458 * called at any time in RealTime mode. It may not be called in
cannam@95 459 * Offline mode (for which the transients option is fixed on
cannam@95 460 * construction).
cannam@95 461 */
cannam@95 462 void setPitchOption(Options options);
cannam@95 463
cannam@95 464 /**
cannam@95 465 * Tell the stretcher exactly how many input samples it will
cannam@95 466 * receive. This is only useful in Offline mode, when it allows
cannam@95 467 * the stretcher to ensure that the number of output samples is
cannam@95 468 * exactly correct. In RealTime mode no such guarantee is
cannam@95 469 * possible and this value is ignored.
cannam@95 470 */
cannam@95 471 void setExpectedInputDuration(size_t samples);
cannam@95 472
cannam@95 473 /**
cannam@95 474 * Tell the stretcher the maximum number of sample frames that you
cannam@95 475 * will ever be passing in to a single process() call. If you
cannam@95 476 * don't call this, the stretcher will assume that you are calling
cannam@95 477 * getSamplesRequired() at each cycle and are never passing more
cannam@95 478 * samples than are suggested by that function.
cannam@95 479 *
cannam@95 480 * If your application has some external constraint that means you
cannam@95 481 * prefer a fixed block size, then your normal mode of operation
cannam@95 482 * would be to provide that block size to this function; to loop
cannam@95 483 * calling process() with that size of block; after each call to
cannam@95 484 * process(), test whether output has been generated by calling
cannam@95 485 * available(); and, if so, call retrieve() to obtain it. See
cannam@95 486 * getSamplesRequired() for a more suitable operating mode for
cannam@95 487 * applications without such external constraints.
cannam@95 488 *
cannam@95 489 * This function may not be called after the first call to study()
cannam@95 490 * or process().
cannam@95 491 *
cannam@95 492 * Note that this value is only relevant to process(), not to
cannam@95 493 * study() (to which you may pass any number of samples at a time,
cannam@95 494 * and from which there is no output).
cannam@95 495 */
cannam@95 496 void setMaxProcessSize(size_t samples);
cannam@95 497
cannam@95 498 /**
cannam@95 499 * Ask the stretcher how many audio sample frames should be
cannam@95 500 * provided as input in order to ensure that some more output
cannam@95 501 * becomes available.
cannam@95 502 *
cannam@95 503 * If your application has no particular constraint on processing
cannam@95 504 * block size and you are able to provide any block size as input
cannam@95 505 * for each cycle, then your normal mode of operation would be to
cannam@95 506 * loop querying this function; providing that number of samples
cannam@95 507 * to process(); and reading the output using available() and
cannam@95 508 * retrieve(). See setMaxProcessSize() for a more suitable
cannam@95 509 * operating mode for applications that do have external block
cannam@95 510 * size constraints.
cannam@95 511 *
cannam@95 512 * Note that this value is only relevant to process(), not to
cannam@95 513 * study() (to which you may pass any number of samples at a time,
cannam@95 514 * and from which there is no output).
cannam@95 515 */
cannam@95 516 size_t getSamplesRequired() const;
cannam@95 517
cannam@95 518 /**
cannam@95 519 * Provide a set of mappings from "before" to "after" sample
cannam@95 520 * numbers so as to enforce a particular stretch profile. The
cannam@95 521 * argument is a map from audio sample frame number in the source
cannam@95 522 * material, to the corresponding sample frame number in the
cannam@95 523 * stretched output. The mapping should be for key frames only,
cannam@95 524 * with a "reasonable" gap between mapped samples.
cannam@95 525 *
cannam@95 526 * This function cannot be used in RealTime mode.
cannam@95 527 *
cannam@95 528 * This function may not be called after the first call to
cannam@95 529 * process(). It should be called after the time and pitch ratios
cannam@95 530 * have been set; the results of changing the time and pitch
cannam@95 531 * ratios after calling this function are undefined. Calling
cannam@95 532 * reset() will clear this mapping.
cannam@95 533 *
cannam@95 534 * The key frame map only affects points within the material; it
cannam@95 535 * does not determine the overall stretch ratio (that is, the
cannam@95 536 * ratio between the output material's duration and the source
cannam@95 537 * material's duration). You need to provide this ratio
cannam@95 538 * separately to setTimeRatio(), otherwise the results may be
cannam@95 539 * truncated or extended in unexpected ways regardless of the
cannam@95 540 * extent of the frame numbers found in the key frame map.
cannam@95 541 */
cannam@95 542 void setKeyFrameMap(const std::map<size_t, size_t> &);
cannam@95 543
cannam@95 544 /**
cannam@95 545 * Provide a block of "samples" sample frames for the stretcher to
cannam@95 546 * study and calculate a stretch profile from.
cannam@95 547 *
cannam@95 548 * This is only meaningful in Offline mode, and is required if
cannam@95 549 * running in that mode. You should pass the entire input through
cannam@95 550 * study() before any process() calls are made, as a sequence of
cannam@95 551 * blocks in individual study() calls, or as a single large block.
cannam@95 552 *
cannam@95 553 * "input" should point to de-interleaved audio data with one
cannam@95 554 * float array per channel. "samples" supplies the number of
cannam@95 555 * audio sample frames available in "input". If "samples" is
cannam@95 556 * zero, "input" may be NULL.
cannam@95 557 *
cannam@95 558 * Set "final" to true if this is the last block of data that will
cannam@95 559 * be provided to study() before the first process() call.
cannam@95 560 */
cannam@95 561 void study(const float *const *input, size_t samples, bool final);
cannam@95 562
cannam@95 563 /**
cannam@95 564 * Provide a block of "samples" sample frames for processing.
cannam@95 565 * See also getSamplesRequired() and setMaxProcessSize().
cannam@95 566 *
cannam@95 567 * Set "final" to true if this is the last block of input data.
cannam@95 568 */
cannam@95 569 void process(const float *const *input, size_t samples, bool final);
cannam@95 570
cannam@95 571 /**
cannam@95 572 * Ask the stretcher how many audio sample frames of output data
cannam@95 573 * are available for reading (via retrieve()).
cannam@95 574 *
cannam@95 575 * This function returns 0 if no frames are available: this
cannam@95 576 * usually means more input data needs to be provided, but if the
cannam@95 577 * stretcher is running in threaded mode it may just mean that not
cannam@95 578 * enough data has yet been processed. Call getSamplesRequired()
cannam@95 579 * to discover whether more input is needed.
cannam@95 580 *
cannam@95 581 * This function returns -1 if all data has been fully processed
cannam@95 582 * and all output read, and the stretch process is now finished.
cannam@95 583 */
cannam@95 584 int available() const;
cannam@95 585
cannam@95 586 /**
cannam@95 587 * Obtain some processed output data from the stretcher. Up to
cannam@95 588 * "samples" samples will be stored in the output arrays (one per
cannam@95 589 * channel for de-interleaved audio data) pointed to by "output".
cannam@95 590 * The return value is the actual number of sample frames
cannam@95 591 * retrieved.
cannam@95 592 */
cannam@95 593 size_t retrieve(float *const *output, size_t samples) const;
cannam@95 594
cannam@95 595 /**
cannam@95 596 * Return the value of internal frequency cutoff value n.
cannam@95 597 *
cannam@95 598 * This function is not for general use.
cannam@95 599 */
cannam@95 600 float getFrequencyCutoff(int n) const;
cannam@95 601
cannam@95 602 /**
cannam@95 603 * Set the value of internal frequency cutoff n to f Hz.
cannam@95 604 *
cannam@95 605 * This function is not for general use.
cannam@95 606 */
cannam@95 607 void setFrequencyCutoff(int n, float f);
cannam@95 608
cannam@95 609 /**
cannam@95 610 * Retrieve the value of the internal input block increment value.
cannam@95 611 *
cannam@95 612 * This function is provided for diagnostic purposes only.
cannam@95 613 */
cannam@95 614 size_t getInputIncrement() const;
cannam@95 615
cannam@95 616 /**
cannam@95 617 * In offline mode, retrieve the sequence of internal block
cannam@95 618 * increments for output, for the entire audio data, provided the
cannam@95 619 * stretch profile has been calculated. In realtime mode,
cannam@95 620 * retrieve any output increments that have accumulated since the
cannam@95 621 * last call to getOutputIncrements, to a limit of 16.
cannam@95 622 *
cannam@95 623 * This function is provided for diagnostic purposes only.
cannam@95 624 */
cannam@95 625 std::vector<int> getOutputIncrements() const;
cannam@95 626
cannam@95 627 /**
cannam@95 628 * In offline mode, retrieve the sequence of internal phase reset
cannam@95 629 * detection function values, for the entire audio data, provided
cannam@95 630 * the stretch profile has been calculated. In realtime mode,
cannam@95 631 * retrieve any phase reset points that have accumulated since the
cannam@95 632 * last call to getPhaseResetCurve, to a limit of 16.
cannam@95 633 *
cannam@95 634 * This function is provided for diagnostic purposes only.
cannam@95 635 */
cannam@95 636 std::vector<float> getPhaseResetCurve() const;
cannam@95 637
cannam@95 638 /**
cannam@95 639 * In offline mode, retrieve the sequence of internal frames for
cannam@95 640 * which exact timing has been sought, for the entire audio data,
cannam@95 641 * provided the stretch profile has been calculated. In realtime
cannam@95 642 * mode, return an empty sequence.
cannam@95 643 *
cannam@95 644 * This function is provided for diagnostic purposes only.
cannam@95 645 */
cannam@95 646 std::vector<int> getExactTimePoints() const;
cannam@95 647
cannam@95 648 /**
cannam@95 649 * Return the number of channels this stretcher was constructed
cannam@95 650 * with.
cannam@95 651 */
cannam@95 652 size_t getChannelCount() const;
cannam@95 653
cannam@95 654 /**
cannam@95 655 * Force the stretcher to calculate a stretch profile. Normally
cannam@95 656 * this happens automatically for the first process() call in
cannam@95 657 * offline mode.
cannam@95 658 *
cannam@95 659 * This function is provided for diagnostic purposes only.
cannam@95 660 */
cannam@95 661 void calculateStretch();
cannam@95 662
cannam@95 663 /**
cannam@95 664 * Set the level of debug output. The value may be from 0 (errors
cannam@95 665 * only) to 3 (very verbose, with audible ticks in the output at
cannam@95 666 * phase reset points). The default is whatever has been set
cannam@95 667 * using setDefaultDebugLevel, or 0 if that function has not been
cannam@95 668 * called.
cannam@95 669 */
cannam@95 670 void setDebugLevel(int level);
cannam@95 671
cannam@95 672 /**
cannam@95 673 * Set the default level of debug output for subsequently
cannam@95 674 * constructed stretchers.
cannam@95 675 *
cannam@95 676 * @see setDebugLevel
cannam@95 677 */
cannam@95 678 static void setDefaultDebugLevel(int level);
cannam@95 679
cannam@95 680 protected:
cannam@95 681 class Impl;
cannam@95 682 Impl *m_d;
cannam@95 683 };
cannam@95 684
cannam@95 685 }
cannam@95 686
cannam@95 687 #endif