Skip to content

Commit 4858c83

Browse files
author
Martin Klang
committed
Phasor / ramp oscillator
1 parent 2918c48 commit 4858c83

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

LibSource/Phasor.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef PHASOR_H
2+
#define PHASOR_H
3+
4+
#include "Oscillator.h"
5+
6+
class Phasor : public Oscillator {
7+
private:
8+
float mul;
9+
float phase;
10+
float incr;
11+
public:
12+
Phasor() : phase(0.0f), incr(0.0f) {
13+
setSampleRate(48000);
14+
}
15+
Phasor(float sr) : phase(0.0f), incr(0.0f){
16+
setSampleRate(sr);
17+
}
18+
void setSampleRate(float sr){
19+
mul = 1.0f/sr;
20+
}
21+
void setFrequency(float freq){
22+
incr = freq*mul;
23+
}
24+
void setPhase(float ph){
25+
phase = ph;
26+
while(phase >= 1)
27+
phase -= 1;
28+
}
29+
void reset(){
30+
phase = 0.0f;
31+
}
32+
float getPhase(){
33+
return phase;
34+
}
35+
float getNextSample(){
36+
float sample = phase;
37+
phase += incr;
38+
if(phase >= 1.0f)
39+
phase -= 1.0f;
40+
return sample;
41+
}
42+
float getNextSample(float fm){
43+
float sample = phase;
44+
phase += incr + fm;
45+
if(phase >= 1.0f)
46+
phase -= 1.0f;
47+
return sample;
48+
}
49+
static Phasor* create(float sr){
50+
return new Phasor(sr);
51+
}
52+
static void destroy(Phasor* osc){
53+
delete osc;
54+
}
55+
};
56+
57+
#endif /* PHASOR_H */

0 commit comments

Comments
 (0)