Skip to content

Commit 64b371a

Browse files
author
Vivian Stewart
committed
FastSineGenerator class
1 parent 24e2c08 commit 64b371a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/AudioEffects/SoundGenerator.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ class SineWaveGenerator : public SoundGenerator<T>{
211211

212212
/// Provides a single sample
213213
virtual T readSample() override {
214-
float angle = m_cycles + m_phase;
215-
T result = m_amplitude * sine(angle);
214+
float angle = double_Pi * m_cycles + m_phase;
215+
T result = m_amplitude * sinf(angle);
216216
m_cycles += m_frequency * m_deltaTime;
217217
if (m_cycles > 1.0) {
218218
m_cycles -= 1.0;
@@ -261,6 +261,32 @@ class SquareWaveGenerator : public SineWaveGenerator<T> {
261261
}
262262
};
263263

264+
/// sine approximation.
265+
float sine(float t) {
266+
float p = (t - (int)t) - 0.5f; // 0 <= p <= 1
267+
float pp = p * p;
268+
return (p - 6.283211f * pp * p + 9.132843f * pp * pp * p) * -6.221086f;
269+
}
270+
271+
template <class T>
272+
class FastSineGenerator : public SineWaveGenerator<T> {
273+
public:
274+
FastSineGenerator(float amplitude = 32767.0, float phase = 0.0) : SineWaveGenerator<T>(amplitude, phase) {
275+
LOGD("FastSineGenerator");
276+
}
277+
278+
virtual T readSample() override {
279+
float angle = double_Pi * SineWaveGenerator<T>::m_cycles +
280+
SineWaveGenerator<T>::m_phase;
281+
T result = SineWaveGenerator<T>::m_amplitude * sine(angle);
282+
SineWaveGenerator<T>::m_cycles += SineWaveGenerator<T>::m_frequency *
283+
SineWaveGenerator<T>::m_deltaTime;
284+
if (SineWaveGenerator<T>::m_cycles > 1.0) {
285+
SineWaveGenerator<T>::m_cycles -= 1.0;
286+
}
287+
return result;
288+
}
289+
};
264290

265291
/**
266292
* @brief Generates a random noise sound with the help of rand() function.

0 commit comments

Comments
 (0)