Skip to content

Commit 80253e9

Browse files
committed
SawTooth
1 parent aeda11b commit 80253e9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/AudioTools/CoreAudio/AudioEffects/SoundGenerator.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,41 @@ template <class T> class FastSineGenerator : public SineWaveGenerator<T> {
283283
}
284284
};
285285

286+
287+
/**
288+
* @brief Sine wave which is based on a fast approximation function.
289+
* @ingroup generator
290+
* @author Vivian Leigh Stewart
291+
* @copyright GPLv3
292+
* @tparam T
293+
*/
294+
template <class T> class SawToothGenerator : public SineWaveGenerator<T> {
295+
public:
296+
SawToothGenerator(float amplitude = 32767.0, float phase = 0.0)
297+
: SineWaveGenerator<T>(amplitude, phase) {
298+
LOGD("SawToothGenerator");
299+
}
300+
301+
virtual T readSample() override {
302+
float angle =
303+
SineWaveGenerator<T>::m_cycles + SineWaveGenerator<T>::m_phase;
304+
T result = SineWaveGenerator<T>::m_amplitude * saw(angle);
305+
SineWaveGenerator<T>::m_cycles +=
306+
SineWaveGenerator<T>::m_frequency * SineWaveGenerator<T>::m_deltaTime;
307+
if (SineWaveGenerator<T>::m_cycles > 1.0) {
308+
SineWaveGenerator<T>::m_cycles -= 1.0;
309+
}
310+
return result;
311+
}
312+
313+
protected:
314+
/// sine approximation.
315+
inline float saw(float t) {
316+
float p = (t - (int)t) - 0.5f; // 0 <= p <= 1
317+
return p;
318+
}
319+
};
320+
286321
/**
287322
* @brief Generates a random noise sound with the help of rand() function.
288323
* @ingroup generator

0 commit comments

Comments
 (0)