annotate win32-mingw/include/rubberband/RubberBandStretcher.h @ 155:54abead6ecce

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