-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathoscillator.cpp
More file actions
38 lines (31 loc) · 895 Bytes
/
oscillator.cpp
File metadata and controls
38 lines (31 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <fea/audio/oscillator.hpp>
#include <limits>
#include <cmath>
const double pi = 3.14159;
namespace fea
{
Oscillator::Oscillator() :
mFrequency(440.0f),
mTheta(0.0)
{
setStreamProperties(1, 48000);
reset();
}
void Oscillator::fillBufferData(size_t sampleIndex, std::vector<int16_t>& toFill)
{
size_t desiredSampleAmount = toFill.size();
double deltaTheta = 2.0 * pi * ((double)mFrequency / (double)getSampleRate());
for(size_t i = 0; i < desiredSampleAmount; i++)
{
toFill[i] = (int16_t) (std::numeric_limits<int16_t>::max() * sin(mTheta));
mTheta += deltaTheta;
if(mTheta > 2.0 * pi)
mTheta = mTheta - 2.0 * pi;
}
}
void Oscillator::setFrequency(float frequency)
{
mFrequency = frequency;
reset();
}
}