-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsine.cpp
51 lines (41 loc) · 1.1 KB
/
sine.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
// ECS7012P Music and Audio Programming
// School of Electronic Engineering and Computer Science
// Queen Mary University of London
// Spring 2020
// sine.cpp: file for implementing the sine oscillator class
#include <cmath>
#include "sine.h"
// Default constructor: call the specific constructor with a default value
Sine::Sine() : Sine(44100.0) {}
// Constructor taking a sample rate
// Can also use initialisation lists instead of setting
// variables inside the function
Sine::Sine(float sampleRate) {
setSampleRate(sampleRate);
frequency_ = 440.0;
phase_ = 0;
}
// Set the sample rate
void Sine::setSampleRate(float rate) {
sampleRate_ = rate;
}
// Set the oscillator frequency
void Sine::setFrequency(float f) {
frequency_ = f;
}
// Get the oscillator frequency
float Sine::frequency() {
return frequency_;
}
// Get the next sample and update the phase
float Sine::nextSample() {
// Increment and wrap the phase
phase_ += 2.0 * M_PI * frequency_ / sampleRate_;
while(phase_ >= 2.0 * M_PI)
phase_ -= 2.0 * M_PI;
return sinf(phase_);
}
// Destructor
Sine::~Sine() {
// Nothing to do here
}