Skip to content

Commit ac17121

Browse files
author
Martin Klang
committed
added block-based AntialiasedSquareWaveOscillator::generate()
1 parent 1f20d66 commit ac17121

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

LibSource/SquareWaveOscillator.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,48 @@ class AntialiasedSquareWaveOscillator : public OscillatorTemplate<AntialiasedSqu
5959
sample -= polyblep(fmod(phase + 1 - pw, 1), incr);
6060
return sample;
6161
}
62+
void generate(FloatArray output){
63+
float sample = phase < pw ? 1 : -1;
64+
if(phase < incr){
65+
float t = phase / incr;
66+
sample += t+t - t*t - 1;
67+
}
68+
size_t len = output.getSize();
69+
float* dest = output.getData();
70+
while(len--){
71+
phase += incr;
72+
if(phase >= pw){
73+
if(phase >= 1){
74+
// wrap phase
75+
phase -= 1;
76+
// correct current sample
77+
float t = (phase - incr) / incr;
78+
sample += t*t + t+t + 1;
79+
*dest++ = sample;
80+
sample = 1;
81+
// correct next sample
82+
t = phase / incr;
83+
sample += t+t - t*t - 1;
84+
}else if(sample == 1){
85+
// correct current sample
86+
float t = (fmod(phase + 1 - pw, 1) - incr) / incr;
87+
sample -= t*t + t+t + 1;
88+
*dest++ = sample;
89+
sample = -1;
90+
// correct next sample
91+
t = fmod(phase + 1 - pw, 1) / incr;
92+
sample -= t+t - t*t - 1;
93+
}else{
94+
*dest++ = sample;
95+
sample = -1;
96+
}
97+
}else{
98+
*dest++ = sample;
99+
sample = 1;
100+
}
101+
}
102+
}
103+
using OscillatorTemplate<AntialiasedSquareWaveOscillator>::generate;
62104
};
63105

64106
#endif /* __SquareWaveOscillator_h */

0 commit comments

Comments
 (0)