comparison examples/10-Instruments/d-box/FeedbackOscillator.cpp @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children
comparison
equal deleted inserted replaced
463:c47709e8b5c9 464:8fcfbfb32aa0
1 /*
2 * FeedbackOscillator.cpp
3 *
4 * Recursive phase-shift oscillator implemented
5 * on the matrix
6 *
7 * Andrew McPherson 2014
8 */
9
10 #include "FeedbackOscillator.h"
11 #include <cstdlib>
12 #include <cmath>
13
14 #define COEFF_B0 0
15 #define COEFF_B1 1
16 #define COEFF_A1 2
17
18 FeedbackOscillator::FeedbackOscillator()
19 : wavetable1(0), wavetable2(0)
20 {
21
22 }
23
24 FeedbackOscillator::~FeedbackOscillator() {
25 if(wavetable1 != 0)
26 free(wavetable1);
27 if(wavetable2 != 0)
28 free(wavetable2);
29
30 }
31
32 // Initialise the settings for the feedback oscillator
33 void FeedbackOscillator::initialise(int maxTableSize, float hpfCutoffFrequency, float matrixSampleRate) {
34 wavetableMaxLength = maxTableSize;
35 if(wavetable1 != 0)
36 free(wavetable1);
37 if(wavetable2 != 0)
38 free(wavetable2);
39
40 wavetable1 = (float *)malloc(maxTableSize * sizeof(float));
41 wavetable2 = (float *)malloc(maxTableSize * sizeof(float));
42
43 float omega = tan(M_PI * hpfCutoffFrequency / matrixSampleRate);
44 float n = 1.0f / (1.0f + omega);
45
46 coeffs[COEFF_A1] = (omega - 1.0f) * n;
47 coeffs[COEFF_B0] = n;
48 coeffs[COEFF_B1] = -n;
49
50 for(int n = 0; n < maxTableSize; n++)
51 wavetable1[n] = wavetable2[n] = 0;
52
53 wavetableRead = wavetable1;
54 wavetableWrite = wavetable2;
55 wavetableWritePointer = 0;
56 sampleCount = lastTriggerCount = 0;
57 }
58
59 // Process one sample and store the output value
60 // Returns true if the wavetable needs rendering
61 int FeedbackOscillator::process(float input, float *output) {
62 float outFloat = coeffs[COEFF_B0] * input + coeffs[COEFF_B1] * lastInput - coeffs[COEFF_A1] * lastOutput;
63 int requestRenderLength = 0;
64
65 if(outFloat < -0.5)
66 *output = 0;
67 else if(outFloat > 0.5)
68 *output = 1;
69 else
70 *output = outFloat + 0.5;
71
72 if(canTrigger && outFloat > 0 && lastOutput <= 0) {
73 triggered = true;
74 requestRenderLength = wavetableWritePointer; // How many samples stored thus far?
75 if(requestRenderLength < 4)
76 requestRenderLength = 0; // Ignore anything with fewer than 4 points
77
78 lastTriggerCount = sampleCount;
79 canTrigger = false;
80 wavetableWritePointer = 0;
81
82 // Swap buffers
83 float *temp = wavetableWrite;
84 wavetableWrite = wavetableRead;
85 wavetableRead = temp;
86 }
87
88 if(triggered) {
89 wavetableWrite[wavetableWritePointer] = outFloat;
90 if(++wavetableWritePointer >= wavetableMaxLength) {
91 triggered = false;
92 wavetableWritePointer = 0;
93 }
94 }
95
96 if(sampleCount - lastTriggerCount > 40)
97 canTrigger = true;
98
99 sampleCount++;
100
101 lastOutput = outFloat;
102 lastInput = input;
103
104 return requestRenderLength;
105 }