Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | ////////////////////////////////////////////////////////////////////////////// |
michael@0 | 2 | /// |
michael@0 | 3 | /// SoundTouch - main class for tempo/pitch/rate adjusting routines. |
michael@0 | 4 | /// |
michael@0 | 5 | /// Notes: |
michael@0 | 6 | /// - Initialize the SoundTouch object instance by setting up the sound stream |
michael@0 | 7 | /// parameters with functions 'setSampleRate' and 'setChannels', then set |
michael@0 | 8 | /// desired tempo/pitch/rate settings with the corresponding functions. |
michael@0 | 9 | /// |
michael@0 | 10 | /// - The SoundTouch class behaves like a first-in-first-out pipeline: The |
michael@0 | 11 | /// samples that are to be processed are fed into one of the pipe by calling |
michael@0 | 12 | /// function 'putSamples', while the ready processed samples can be read |
michael@0 | 13 | /// from the other end of the pipeline with function 'receiveSamples'. |
michael@0 | 14 | /// |
michael@0 | 15 | /// - The SoundTouch processing classes require certain sized 'batches' of |
michael@0 | 16 | /// samples in order to process the sound. For this reason the classes buffer |
michael@0 | 17 | /// incoming samples until there are enough of samples available for |
michael@0 | 18 | /// processing, then they carry out the processing step and consequently |
michael@0 | 19 | /// make the processed samples available for outputting. |
michael@0 | 20 | /// |
michael@0 | 21 | /// - For the above reason, the processing routines introduce a certain |
michael@0 | 22 | /// 'latency' between the input and output, so that the samples input to |
michael@0 | 23 | /// SoundTouch may not be immediately available in the output, and neither |
michael@0 | 24 | /// the amount of outputtable samples may not immediately be in direct |
michael@0 | 25 | /// relationship with the amount of previously input samples. |
michael@0 | 26 | /// |
michael@0 | 27 | /// - The tempo/pitch/rate control parameters can be altered during processing. |
michael@0 | 28 | /// Please notice though that they aren't currently protected by semaphores, |
michael@0 | 29 | /// so in multi-thread application external semaphore protection may be |
michael@0 | 30 | /// required. |
michael@0 | 31 | /// |
michael@0 | 32 | /// - This class utilizes classes 'TDStretch' for tempo change (without modifying |
michael@0 | 33 | /// pitch) and 'RateTransposer' for changing the playback rate (that is, both |
michael@0 | 34 | /// tempo and pitch in the same ratio) of the sound. The third available control |
michael@0 | 35 | /// 'pitch' (change pitch but maintain tempo) is produced by a combination of |
michael@0 | 36 | /// combining the two other controls. |
michael@0 | 37 | /// |
michael@0 | 38 | /// Author : Copyright (c) Olli Parviainen |
michael@0 | 39 | /// Author e-mail : oparviai 'at' iki.fi |
michael@0 | 40 | /// SoundTouch WWW: http://www.surina.net/soundtouch |
michael@0 | 41 | /// |
michael@0 | 42 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 43 | // |
michael@0 | 44 | // Last changed : $Date: 2014-04-06 10:57:21 -0500 (Sun, 06 Apr 2014) $ |
michael@0 | 45 | // File revision : $Revision: 4 $ |
michael@0 | 46 | // |
michael@0 | 47 | // $Id: SoundTouch.h 195 2014-04-06 15:57:21Z oparviai $ |
michael@0 | 48 | // |
michael@0 | 49 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 50 | // |
michael@0 | 51 | // License : |
michael@0 | 52 | // |
michael@0 | 53 | // SoundTouch audio processing library |
michael@0 | 54 | // Copyright (c) Olli Parviainen |
michael@0 | 55 | // |
michael@0 | 56 | // This library is free software; you can redistribute it and/or |
michael@0 | 57 | // modify it under the terms of the GNU Lesser General Public |
michael@0 | 58 | // License as published by the Free Software Foundation; either |
michael@0 | 59 | // version 2.1 of the License, or (at your option) any later version. |
michael@0 | 60 | // |
michael@0 | 61 | // This library is distributed in the hope that it will be useful, |
michael@0 | 62 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
michael@0 | 63 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
michael@0 | 64 | // Lesser General Public License for more details. |
michael@0 | 65 | // |
michael@0 | 66 | // You should have received a copy of the GNU Lesser General Public |
michael@0 | 67 | // License along with this library; if not, write to the Free Software |
michael@0 | 68 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
michael@0 | 69 | // |
michael@0 | 70 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 71 | |
michael@0 | 72 | #ifndef SoundTouch_H |
michael@0 | 73 | #define SoundTouch_H |
michael@0 | 74 | |
michael@0 | 75 | #include "FIFOSamplePipe.h" |
michael@0 | 76 | #include "STTypes.h" |
michael@0 | 77 | |
michael@0 | 78 | namespace soundtouch |
michael@0 | 79 | { |
michael@0 | 80 | |
michael@0 | 81 | /// Soundtouch library version string |
michael@0 | 82 | #define SOUNDTOUCH_VERSION "1.8.0" |
michael@0 | 83 | |
michael@0 | 84 | /// SoundTouch library version id |
michael@0 | 85 | #define SOUNDTOUCH_VERSION_ID (10800) |
michael@0 | 86 | |
michael@0 | 87 | // |
michael@0 | 88 | // Available setting IDs for the 'setSetting' & 'get_setting' functions: |
michael@0 | 89 | |
michael@0 | 90 | /// Enable/disable anti-alias filter in pitch transposer (0 = disable) |
michael@0 | 91 | #define SETTING_USE_AA_FILTER 0 |
michael@0 | 92 | |
michael@0 | 93 | /// Pitch transposer anti-alias filter length (8 .. 128 taps, default = 32) |
michael@0 | 94 | #define SETTING_AA_FILTER_LENGTH 1 |
michael@0 | 95 | |
michael@0 | 96 | /// Enable/disable quick seeking algorithm in tempo changer routine |
michael@0 | 97 | /// (enabling quick seeking lowers CPU utilization but causes a minor sound |
michael@0 | 98 | /// quality compromising) |
michael@0 | 99 | #define SETTING_USE_QUICKSEEK 2 |
michael@0 | 100 | |
michael@0 | 101 | /// Time-stretch algorithm single processing sequence length in milliseconds. This determines |
michael@0 | 102 | /// to how long sequences the original sound is chopped in the time-stretch algorithm. |
michael@0 | 103 | /// See "STTypes.h" or README for more information. |
michael@0 | 104 | #define SETTING_SEQUENCE_MS 3 |
michael@0 | 105 | |
michael@0 | 106 | /// Time-stretch algorithm seeking window length in milliseconds for algorithm that finds the |
michael@0 | 107 | /// best possible overlapping location. This determines from how wide window the algorithm |
michael@0 | 108 | /// may look for an optimal joining location when mixing the sound sequences back together. |
michael@0 | 109 | /// See "STTypes.h" or README for more information. |
michael@0 | 110 | #define SETTING_SEEKWINDOW_MS 4 |
michael@0 | 111 | |
michael@0 | 112 | /// Time-stretch algorithm overlap length in milliseconds. When the chopped sound sequences |
michael@0 | 113 | /// are mixed back together, to form a continuous sound stream, this parameter defines over |
michael@0 | 114 | /// how long period the two consecutive sequences are let to overlap each other. |
michael@0 | 115 | /// See "STTypes.h" or README for more information. |
michael@0 | 116 | #define SETTING_OVERLAP_MS 5 |
michael@0 | 117 | |
michael@0 | 118 | |
michael@0 | 119 | /// Call "getSetting" with this ID to query nominal average processing sequence |
michael@0 | 120 | /// size in samples. This value tells approcimate value how many input samples |
michael@0 | 121 | /// SoundTouch needs to gather before it does DSP processing run for the sample batch. |
michael@0 | 122 | /// |
michael@0 | 123 | /// Notices: |
michael@0 | 124 | /// - This is read-only parameter, i.e. setSetting ignores this parameter |
michael@0 | 125 | /// - Returned value is approximate average value, exact processing batch |
michael@0 | 126 | /// size may wary from time to time |
michael@0 | 127 | /// - This parameter value is not constant but may change depending on |
michael@0 | 128 | /// tempo/pitch/rate/samplerate settings. |
michael@0 | 129 | #define SETTING_NOMINAL_INPUT_SEQUENCE 6 |
michael@0 | 130 | |
michael@0 | 131 | |
michael@0 | 132 | /// Call "getSetting" with this ID to query nominal average processing output |
michael@0 | 133 | /// size in samples. This value tells approcimate value how many output samples |
michael@0 | 134 | /// SoundTouch outputs once it does DSP processing run for a batch of input samples. |
michael@0 | 135 | /// |
michael@0 | 136 | /// Notices: |
michael@0 | 137 | /// - This is read-only parameter, i.e. setSetting ignores this parameter |
michael@0 | 138 | /// - Returned value is approximate average value, exact processing batch |
michael@0 | 139 | /// size may wary from time to time |
michael@0 | 140 | /// - This parameter value is not constant but may change depending on |
michael@0 | 141 | /// tempo/pitch/rate/samplerate settings. |
michael@0 | 142 | #define SETTING_NOMINAL_OUTPUT_SEQUENCE 7 |
michael@0 | 143 | |
michael@0 | 144 | class EXPORT SoundTouch : public FIFOProcessor |
michael@0 | 145 | { |
michael@0 | 146 | private: |
michael@0 | 147 | /// Rate transposer class instance |
michael@0 | 148 | class RateTransposer *pRateTransposer; |
michael@0 | 149 | |
michael@0 | 150 | /// Time-stretch class instance |
michael@0 | 151 | class TDStretch *pTDStretch; |
michael@0 | 152 | |
michael@0 | 153 | /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters. |
michael@0 | 154 | float virtualRate; |
michael@0 | 155 | |
michael@0 | 156 | /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters. |
michael@0 | 157 | float virtualTempo; |
michael@0 | 158 | |
michael@0 | 159 | /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters. |
michael@0 | 160 | float virtualPitch; |
michael@0 | 161 | |
michael@0 | 162 | /// Flag: Has sample rate been set? |
michael@0 | 163 | bool bSrateSet; |
michael@0 | 164 | |
michael@0 | 165 | /// Calculates effective rate & tempo valuescfrom 'virtualRate', 'virtualTempo' and |
michael@0 | 166 | /// 'virtualPitch' parameters. |
michael@0 | 167 | void calcEffectiveRateAndTempo(); |
michael@0 | 168 | |
michael@0 | 169 | protected : |
michael@0 | 170 | /// Number of channels |
michael@0 | 171 | uint channels; |
michael@0 | 172 | |
michael@0 | 173 | /// Effective 'rate' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch' |
michael@0 | 174 | float rate; |
michael@0 | 175 | |
michael@0 | 176 | /// Effective 'tempo' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch' |
michael@0 | 177 | float tempo; |
michael@0 | 178 | |
michael@0 | 179 | public: |
michael@0 | 180 | SoundTouch(); |
michael@0 | 181 | virtual ~SoundTouch(); |
michael@0 | 182 | |
michael@0 | 183 | /// Get SoundTouch library version string |
michael@0 | 184 | static const char *getVersionString(); |
michael@0 | 185 | |
michael@0 | 186 | /// Get SoundTouch library version Id |
michael@0 | 187 | static uint getVersionId(); |
michael@0 | 188 | |
michael@0 | 189 | /// Sets new rate control value. Normal rate = 1.0, smaller values |
michael@0 | 190 | /// represent slower rate, larger faster rates. |
michael@0 | 191 | void setRate(float newRate); |
michael@0 | 192 | |
michael@0 | 193 | /// Sets new tempo control value. Normal tempo = 1.0, smaller values |
michael@0 | 194 | /// represent slower tempo, larger faster tempo. |
michael@0 | 195 | void setTempo(float newTempo); |
michael@0 | 196 | |
michael@0 | 197 | /// Sets new rate control value as a difference in percents compared |
michael@0 | 198 | /// to the original rate (-50 .. +100 %) |
michael@0 | 199 | void setRateChange(float newRate); |
michael@0 | 200 | |
michael@0 | 201 | /// Sets new tempo control value as a difference in percents compared |
michael@0 | 202 | /// to the original tempo (-50 .. +100 %) |
michael@0 | 203 | void setTempoChange(float newTempo); |
michael@0 | 204 | |
michael@0 | 205 | /// Sets new pitch control value. Original pitch = 1.0, smaller values |
michael@0 | 206 | /// represent lower pitches, larger values higher pitch. |
michael@0 | 207 | void setPitch(float newPitch); |
michael@0 | 208 | |
michael@0 | 209 | /// Sets pitch change in octaves compared to the original pitch |
michael@0 | 210 | /// (-1.00 .. +1.00) |
michael@0 | 211 | void setPitchOctaves(float newPitch); |
michael@0 | 212 | |
michael@0 | 213 | /// Sets pitch change in semi-tones compared to the original pitch |
michael@0 | 214 | /// (-12 .. +12) |
michael@0 | 215 | void setPitchSemiTones(int newPitch); |
michael@0 | 216 | void setPitchSemiTones(float newPitch); |
michael@0 | 217 | |
michael@0 | 218 | /// Sets the number of channels, 1 = mono, 2 = stereo |
michael@0 | 219 | void setChannels(uint numChannels); |
michael@0 | 220 | |
michael@0 | 221 | /// Sets sample rate. |
michael@0 | 222 | void setSampleRate(uint srate); |
michael@0 | 223 | |
michael@0 | 224 | /// Flushes the last samples from the processing pipeline to the output. |
michael@0 | 225 | /// Clears also the internal processing buffers. |
michael@0 | 226 | // |
michael@0 | 227 | /// Note: This function is meant for extracting the last samples of a sound |
michael@0 | 228 | /// stream. This function may introduce additional blank samples in the end |
michael@0 | 229 | /// of the sound stream, and thus it's not recommended to call this function |
michael@0 | 230 | /// in the middle of a sound stream. |
michael@0 | 231 | void flush(); |
michael@0 | 232 | |
michael@0 | 233 | /// Adds 'numSamples' pcs of samples from the 'samples' memory position into |
michael@0 | 234 | /// the input of the object. Notice that sample rate _has_to_ be set before |
michael@0 | 235 | /// calling this function, otherwise throws a runtime_error exception. |
michael@0 | 236 | virtual void putSamples( |
michael@0 | 237 | const SAMPLETYPE *samples, ///< Pointer to sample buffer. |
michael@0 | 238 | uint numSamples ///< Number of samples in buffer. Notice |
michael@0 | 239 | ///< that in case of stereo-sound a single sample |
michael@0 | 240 | ///< contains data for both channels. |
michael@0 | 241 | ); |
michael@0 | 242 | |
michael@0 | 243 | /// Clears all the samples in the object's output and internal processing |
michael@0 | 244 | /// buffers. |
michael@0 | 245 | virtual void clear(); |
michael@0 | 246 | |
michael@0 | 247 | /// Changes a setting controlling the processing system behaviour. See the |
michael@0 | 248 | /// 'SETTING_...' defines for available setting ID's. |
michael@0 | 249 | /// |
michael@0 | 250 | /// \return 'true' if the setting was succesfully changed |
michael@0 | 251 | bool setSetting(int settingId, ///< Setting ID number. see SETTING_... defines. |
michael@0 | 252 | int value ///< New setting value. |
michael@0 | 253 | ); |
michael@0 | 254 | |
michael@0 | 255 | /// Reads a setting controlling the processing system behaviour. See the |
michael@0 | 256 | /// 'SETTING_...' defines for available setting ID's. |
michael@0 | 257 | /// |
michael@0 | 258 | /// \return the setting value. |
michael@0 | 259 | int getSetting(int settingId ///< Setting ID number, see SETTING_... defines. |
michael@0 | 260 | ) const; |
michael@0 | 261 | |
michael@0 | 262 | /// Returns number of samples currently unprocessed. |
michael@0 | 263 | virtual uint numUnprocessedSamples() const; |
michael@0 | 264 | |
michael@0 | 265 | |
michael@0 | 266 | /// Other handy functions that are implemented in the ancestor classes (see |
michael@0 | 267 | /// classes 'FIFOProcessor' and 'FIFOSamplePipe') |
michael@0 | 268 | /// |
michael@0 | 269 | /// - receiveSamples() : Use this function to receive 'ready' processed samples from SoundTouch. |
michael@0 | 270 | /// - numSamples() : Get number of 'ready' samples that can be received with |
michael@0 | 271 | /// function 'receiveSamples()' |
michael@0 | 272 | /// - isEmpty() : Returns nonzero if there aren't any 'ready' samples. |
michael@0 | 273 | /// - clear() : Clears all samples from ready/processing buffers. |
michael@0 | 274 | }; |
michael@0 | 275 | |
michael@0 | 276 | } |
michael@0 | 277 | #endif |