1 //////////////////////////////////////////////////////////////////////////////
3 /// SoundTouch - main class for tempo/pitch/rate adjusting routines.
6 /// - Initialize the SoundTouch object instance by setting up the sound stream
7 /// parameters with functions 'setSampleRate' and 'setChannels', then set
8 /// desired tempo/pitch/rate settings with the corresponding functions.
10 /// - The SoundTouch class behaves like a first-in-first-out pipeline: The
11 /// samples that are to be processed are fed into one of the pipe by calling
12 /// function 'putSamples', while the ready processed samples can be read
13 /// from the other end of the pipeline with function 'receiveSamples'.
15 /// - The SoundTouch processing classes require certain sized 'batches' of
16 /// samples in order to process the sound. For this reason the classes buffer
17 /// incoming samples until there are enough of samples available for
18 /// processing, then they carry out the processing step and consequently
19 /// make the processed samples available for outputting.
21 /// - For the above reason, the processing routines introduce a certain
22 /// 'latency' between the input and output, so that the samples input to
23 /// SoundTouch may not be immediately available in the output, and neither
24 /// the amount of outputtable samples may not immediately be in direct
25 /// relationship with the amount of previously input samples.
27 /// - The tempo/pitch/rate control parameters can be altered during processing.
28 /// Please notice though that they aren't currently protected by semaphores,
29 /// so in multi-thread application external semaphore protection may be
32 /// - This class utilizes classes 'TDStretch' for tempo change (without modifying
33 /// pitch) and 'RateTransposer' for changing the playback rate (that is, both
34 /// tempo and pitch in the same ratio) of the sound. The third available control
35 /// 'pitch' (change pitch but maintain tempo) is produced by a combination of
36 /// combining the two other controls.
38 /// Author : Copyright (c) Olli Parviainen
39 /// Author e-mail : oparviai @ iki.fi
40 /// SoundTouch WWW: http://www.iki.fi/oparviai/soundtouch
42 ////////////////////////////////////////////////////////////////////////////////
44 // Last changed : $Date$
45 // File revision : $Revision$
49 ////////////////////////////////////////////////////////////////////////////////
53 // SoundTouch audio processing library
54 // Copyright (c) Olli Parviainen
56 // This library is free software; you can redistribute it and/or
57 // modify it under the terms of the GNU Lesser General Public
58 // License as published by the Free Software Foundation; either
59 // version 2.1 of the License, or (at your option) any later version.
61 // This library is distributed in the hope that it will be useful,
62 // but WITHOUT ANY WARRANTY; without even the implied warranty of
63 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
64 // Lesser General Public License for more details.
66 // You should have received a copy of the GNU Lesser General Public
67 // License along with this library; if not, write to the Free Software
68 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
70 ////////////////////////////////////////////////////////////////////////////////
75 #include "FIFOSamplePipe.h"
81 /// Soundtouch library version string
82 #define SOUNDTOUCH_VERSION "1.3.0"
84 /// SoundTouch library version id
85 #define SOUNDTOUCH_VERSION_ID 010300
88 // Available setting IDs for the 'setSetting' & 'get_setting' functions:
90 /// Enable/disable anti-alias filter in pitch transposer (0 = disable)
91 #define SETTING_USE_AA_FILTER 0
93 /// Pitch transposer anti-alias filter length (8 .. 128 taps, default = 32)
94 #define SETTING_AA_FILTER_LENGTH 1
96 /// Enable/disable quick seeking algorithm in tempo changer routine
97 /// (enabling quick seeking lowers CPU utilization but causes a minor sound
98 /// quality compromising)
99 #define SETTING_USE_QUICKSEEK 2
101 /// Time-stretch algorithm single processing sequence length in milliseconds. This determines
102 /// to how long sequences the original sound is chopped in the time-stretch algorithm.
103 /// See "STTypes.h" or README for more information.
104 #define SETTING_SEQUENCE_MS 3
106 /// Time-stretch algorithm seeking window length in milliseconds for algorithm that finds the
107 /// best possible overlapping location. This determines from how wide window the algorithm
108 /// may look for an optimal joining location when mixing the sound sequences back together.
109 /// See "STTypes.h" or README for more information.
110 #define SETTING_SEEKWINDOW_MS 4
112 /// Time-stretch algorithm overlap length in milliseconds. When the chopped sound sequences
113 /// are mixed back together, to form a continuous sound stream, this parameter defines over
114 /// how long period the two consecutive sequences are let to overlap each other.
115 /// See "STTypes.h" or README for more information.
116 #define SETTING_OVERLAP_MS 5
119 class SoundTouch : public FIFOProcessor
122 /// Rate transposer class instance
123 class RateTransposer *pRateTransposer;
125 /// Time-stretch class instance
126 class TDStretch *pTDStretch;
128 /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
131 /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
134 /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
137 /// Flag: Has sample rate been set?
140 /// Calculates effective rate & tempo valuescfrom 'virtualRate', 'virtualTempo' and
141 /// 'virtualPitch' parameters.
142 void calcEffectiveRateAndTempo();
145 /// Number of channels
148 /// Effective 'rate' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
151 /// Effective 'tempo' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
156 virtual ~SoundTouch();
158 /// Get SoundTouch library version string
159 static const char *getVersionString();
161 /// Get SoundTouch library version Id
162 static uint getVersionId();
164 /// Sets new rate control value. Normal rate = 1.0, smaller values
165 /// represent slower rate, larger faster rates.
166 void setRate(float newRate);
168 /// Sets new tempo control value. Normal tempo = 1.0, smaller values
169 /// represent slower tempo, larger faster tempo.
170 void setTempo(float newTempo);
172 /// Sets new rate control value as a difference in percents compared
173 /// to the original rate (-50 .. +100 %)
174 void setRateChange(float newRate);
176 /// Sets new tempo control value as a difference in percents compared
177 /// to the original tempo (-50 .. +100 %)
178 void setTempoChange(float newTempo);
180 /// Sets new pitch control value. Original pitch = 1.0, smaller values
181 /// represent lower pitches, larger values higher pitch.
182 void setPitch(float newPitch);
184 /// Sets pitch change in octaves compared to the original pitch
186 void setPitchOctaves(float newPitch);
188 /// Sets pitch change in semi-tones compared to the original pitch
190 void setPitchSemiTones(int newPitch);
191 void setPitchSemiTones(float newPitch);
193 /// Sets the number of channels, 1 = mono, 2 = stereo
194 void setChannels(uint numChannels);
196 /// Sets sample rate.
197 void setSampleRate(uint srate);
199 /// Flushes the last samples from the processing pipeline to the output.
200 /// Clears also the internal processing buffers.
202 /// Note: This function is meant for extracting the last samples of a sound
203 /// stream. This function may introduce additional blank samples in the end
204 /// of the sound stream, and thus it's not recommended to call this function
205 /// in the middle of a sound stream.
208 /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
209 /// the input of the object. Notice that sample rate _has_to_ be set before
210 /// calling this function, otherwise throws a runtime_error exception.
211 virtual void putSamples(
212 const SAMPLETYPE *samples, ///< Pointer to sample buffer.
213 uint numSamples ///< Number of samples in buffer. Notice
214 ///< that in case of stereo-sound a single sample
215 ///< contains data for both channels.
218 /// Clears all the samples in the object's output and internal processing
220 virtual void clear();
222 /// Changes a setting controlling the processing system behaviour. See the
223 /// 'SETTING_...' defines for available setting ID's.
225 /// \return 'TRUE' if the setting was succesfully changed
226 BOOL setSetting(uint settingId, ///< Setting ID number. see SETTING_... defines.
227 uint value ///< New setting value.
230 /// Reads a setting controlling the processing system behaviour. See the
231 /// 'SETTING_...' defines for available setting ID's.
233 /// \return the setting value.
234 uint getSetting(uint settingId ///< Setting ID number, see SETTING_... defines.
237 /// Returns number of samples currently unprocessed.
238 virtual uint numUnprocessedSamples() const;
241 /// Other handy functions that are implemented in the ancestor classes (see
242 /// classes 'FIFOProcessor' and 'FIFOSamplePipe')
244 /// - receiveSamples() : Use this function to receive 'ready' processed samples from SoundTouch.
245 /// - numSamples() : Get number of 'ready' samples that can be received with
246 /// function 'receiveSamples()'
247 /// - isEmpty() : Returns nonzero if there aren't any 'ready' samples.
248 /// - clear() : Clears all samples from ready/processing buffers.