comparison projects/d-box/OscillatorBank.h @ 0:8a575ba3ab52

Initial commit.
author andrewm
date Fri, 31 Oct 2014 19:10:17 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8a575ba3ab52
1 /*
2 * OscillatorBank.h
3 *
4 * Created on: May 23, 2014
5 * Author: Victor Zappi and Andrew McPherson
6 */
7
8 #ifndef OSCILLATORBANK_H_
9 #define OSCILLATORBANK_H_
10
11
12 #include <string>
13
14 #include "spear_parser.h"
15 #include "ADSR.h"
16 #include "config.h"
17
18 using namespace std;
19
20 enum OscBankstates {bank_stopped, bank_playing, bank_toreset};
21
22 class OscillatorBank
23 {
24 public:
25 OscillatorBank();
26 OscillatorBank(string filename, int hopsize=-1, int samplerate=44100);
27 OscillatorBank(char *filename, int hopsize=-1, int samplerate=44100);
28 ~OscillatorBank();
29 float *oscillatorPhases;
30 float *oscillatorNormFrequencies;
31 float *oscillatorNormFreqDerivatives;
32 float *oscillatorAmplitudes;
33 float *oscillatorAmplitudeDerivatives;
34 float *oscStatNormFrequenciesMean;
35 float *oscStatNumHops;
36 OscBankstates state;
37 bool note;
38 int actPartNum;
39 unsigned int *actPart;
40 int hopCounter;
41 int lookupTableSize;
42 float *lookupTable;
43 float ampTh;
44 int hopNumTh;
45 float pitchMultiplier;
46 float freqMovement;
47 int filterNum;
48 float filterFreqs[5];
49 float filterQ[5];
50 float filterMaxF;
51 float filterAmpMinF;
52 float filterAmpMaxF;
53 float filterAmpMul;
54
55 bool loadFile(string filename, int hopsize=-1, int samplerate=44100);
56 bool loadFile(char *filename, int hopsize=-1, int samplerate=44100);
57 bool initBank(int oversamp=1);
58 void resetOscillators();
59 int getHopSize() { return hopSize; }
60 void nextHop();
61 void setLoopHops(int start, int end);
62 void play(float vel);
63 void stop();
64 void afterTouch(float vel);
65 int getEnvelopeState();
66 float getFrequencyScaler();
67 void setSpeed(float sp);
68 float getSpeed();
69 float getMaxSpeed();
70 float getMinSpeed();
71 void setJumpHop(int hop);
72 int getLastHop();
73 int getCurrentHop() { return currentHop; }
74
75 private:
76
77 bool loaded;
78 int numOfPartials;
79 int numOfOscillators;
80 int partialsHopSize;
81 int overSampling;
82 int hopSize;
83 int hopSizeReminder;
84 int oscBankHopSize;
85 float frequencyScaler;
86 float nyqNorm;
87 int lastHop;
88 int currentHop;
89 int loopDir;
90 int loopDirShift;
91 int loopStartHop;
92 int loopEndHop;
93 int *indicesMapping;
94 float *phaseCopies;
95 float *oscillatorNextNormFreq;
96 float *oscillatorNextAmp;
97 float *nextNormFreqCopies;
98 float *nextAmpCopies;
99 float *freqFixedDeltas;
100 float *ampFixedDeltas;
101 bool *nyquistCut;
102 Spear_parser parser;
103 Partials *partials;
104 ADSR adsr;
105 float minAttackTime;
106 float deltaAttackTime;
107 float minReleaseTime;
108 float deltaReleaseTime;
109 int envState;
110 int rate;
111 float speed;
112 float nextSpeed;
113 float maxSpeed;
114 float minSpeed;
115 int jumpHop;
116 float adsrVal;
117 float prevAdsrVal;
118 float prevAmpTh;
119 int prevHopNumTh;
120 float prevPitchMultiplier;
121 float prevFreqMovement;
122 int prevFilterNum;
123 float prevFilterFreqs[5];
124 float prevFilterQ[5];
125
126 bool loader(char *filename, int hopsize=-1, int samplerate=44100);
127 void addFakeOsc();
128 void nextOscBankHop();
129 void nextPartialHop();
130 int jumpToHop();
131 void setDirection(int dir);
132 int nextEnvState();
133 void checkDirection();
134 void checkSpeed();
135 int checkJump();
136 bool checkOversampling();
137 void updatePrevControls();
138 float calculateParDamping(int parIndex, int hopNTh, float adsrVl, float nextFreq,
139 int filNum, float *filFreq, float *filQ);
140 };
141
142 inline bool OscillatorBank::loadFile(string filename, int hopsize, int samplerate)
143 {
144 return loader((char *)filename.c_str(), hopsize, samplerate);
145 }
146
147 inline bool OscillatorBank::loadFile(char *filename, int hopsize, int samplerate)
148 {
149 return loader(filename, hopsize, samplerate);
150 }
151
152 inline void OscillatorBank::setLoopHops(int start, int end)
153 {
154 if(start > end)
155 end = start;
156
157 if(start<0)
158 start = 0;
159 else if(start>lastHop)
160 start = 0;
161 if(end < 1)
162 end = 1;
163 end = (end<=lastHop) ? end : lastHop;
164
165 // set it, take into consideration hop oversampling
166 loopStartHop = start*overSampling;
167 loopEndHop = end*overSampling;
168 }
169
170 inline void OscillatorBank::stop()
171 {
172 note = false;
173 adsr.gate(0);
174 }
175
176 inline float OscillatorBank::getFrequencyScaler()
177 {
178 return frequencyScaler;
179 }
180
181 inline void OscillatorBank::afterTouch(float vel)
182 {
183 hopNumTh = log((1-vel)+1)/log(2)*20000;
184 if(adsr.getState()==env_attack)
185 adsr.setAttackRate( (minAttackTime + ( (1-vel)*deltaAttackTime )) * rate );
186 adsr.setReleaseRate( (minReleaseTime+(1-vel)*deltaReleaseTime)* rate );
187 }
188
189 inline int OscillatorBank::getEnvelopeState()
190 {
191 return envState;
192 }
193
194 inline void OscillatorBank::setSpeed(float sp)
195 {
196 nextSpeed = sp;
197 }
198
199 inline float OscillatorBank::getSpeed()
200 {
201 return speed;
202 }
203
204 inline float OscillatorBank::getMaxSpeed()
205 {
206 return maxSpeed;
207 }
208
209 inline float OscillatorBank::getMinSpeed()
210 {
211 return minSpeed;
212 }
213
214 inline void OscillatorBank::setJumpHop(int hop)
215 {
216 if(hop<0)
217 return;
218 hop = (hop<=lastHop) ? hop : lastHop;
219 jumpHop = hop;
220 }
221
222 inline void OscillatorBank::setDirection(int dir)
223 {
224 if(dir>=0)
225 {
226 loopDir = 1;
227 loopDirShift = 0;
228 }
229 else
230 {
231 loopDir = -1;
232 loopDirShift = 1;
233 }
234 }
235
236 inline int OscillatorBank::getLastHop()
237 {
238 return lastHop;
239 }
240 #endif /* OSCILLATORBANK_H_ */