|
| 1 | +#ifndef __SuperSaw_h__ |
| 2 | +#define __SuperSaw_h__ |
| 3 | + |
| 4 | + |
| 5 | +/** |
| 6 | + * This class is a SuperSaw wave oscillator using multiple free-running non-bandlimited sawtooth oscillators (7 by default). |
| 7 | + * |
| 8 | + * Controls for mix and detune are provided. |
| 9 | + * |
| 10 | + * Note: these controls are linear, unlike in the Roland JP-8000. |
| 11 | + * |
| 12 | + * Main reference is <a href="https://www.nada.kth.se/utbildning/grukth/exjobb/rapportlistor/2010/rapporter10/szabo_adam_10131.pdf">https://www.nada.kth.se/utbildning/grukth/exjobb/rapportlistor/2010/rapporter10/szabo_adam_10131.pdf</a> |
| 13 | + */ |
| 14 | + |
| 15 | +class SuperSaw{ |
| 16 | +private: |
| 17 | + enum lastCalls { |
| 18 | + kNone, |
| 19 | + kSetDetune, |
| 20 | + kSetFrequency, |
| 21 | + kUpdatePhaseIncrements, |
| 22 | + }; |
| 23 | + int lastCall=kNone; |
| 24 | + float samplePeriod; |
| 25 | + float detune; |
| 26 | + float* frequencyRatios; |
| 27 | + float* phaseIncrements; |
| 28 | + float* amplitudes; |
| 29 | + float* phases; |
| 30 | + float* allTheArrays=NULL; |
| 31 | + BiquadFilter *filter=NULL; |
| 32 | + float frequency; |
| 33 | + int numberOfOscillators; |
| 34 | + bool filterBypass; |
| 35 | +void init(){ |
| 36 | + frequency=0; |
| 37 | + numberOfOscillators=7; |
| 38 | + filterBypass=false; |
| 39 | + allocate(); |
| 40 | + setDetune(0); |
| 41 | + setMix(0); |
| 42 | + } |
| 43 | + |
| 44 | + void destroy(){ |
| 45 | + free(allTheArrays); |
| 46 | + if(filter!=NULL){ |
| 47 | + BiquadFilter::destroy(filter); |
| 48 | + filter=NULL; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + void allocate(){ |
| 53 | + destroy(); |
| 54 | + int numArrays=4; |
| 55 | + float size=sizeof(float)*numberOfOscillators*numArrays; |
| 56 | + allTheArrays=(float*)malloc(size); |
| 57 | + memset(allTheArrays,0,size); |
| 58 | + frequencyRatios=allTheArrays; |
| 59 | + phaseIncrements=allTheArrays+numberOfOscillators*1; |
| 60 | + amplitudes=allTheArrays+numberOfOscillators*2; |
| 61 | + phases=allTheArrays+numberOfOscillators*3; |
| 62 | + float filterStages=2; |
| 63 | + filter=BiquadFilter::create(filterStages); |
| 64 | + } |
| 65 | + |
| 66 | + void setFilter(float aFrequency){ |
| 67 | + float cutoff=aFrequency*samplePeriod*0.5; |
| 68 | + filter->setHighPass(cutoff, FilterStage::BUTTERWORTH_Q); |
| 69 | + } |
| 70 | + |
| 71 | + void updatePhaseIncrements(){ |
| 72 | + lastCall=kUpdatePhaseIncrements; |
| 73 | + for(int n=0; n<numberOfOscillators; n++){ |
| 74 | + phaseIncrements[n]=frequencyRatios[n]*frequency*samplePeriod * 2;// the *2 multiplier takes into account that the oscillator range is -1 to 1 |
| 75 | + } |
| 76 | + } |
| 77 | +public: |
| 78 | + /** |
| 79 | + * Default constructor. |
| 80 | + * @remarks Before using the oscillator you will need to initialize the sample rate with setSampleRate(). |
| 81 | + */ |
| 82 | + SuperSaw(){ |
| 83 | + init(); |
| 84 | + } |
| 85 | + /** |
| 86 | + * Constructor. |
| 87 | + * @param[in] aSampleRate the sample rate of the system |
| 88 | + */ |
| 89 | + SuperSaw(float aSampleRate){ |
| 90 | + setSampleRate(aSampleRate); |
| 91 | + init(); |
| 92 | + } |
| 93 | + /** Destructor |
| 94 | + */ |
| 95 | + ~SuperSaw(){ |
| 96 | + destroy(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Set the sample rate |
| 101 | + * @param[in] aSampleRate the sampling rate of the system |
| 102 | + */ |
| 103 | + void setSampleRate(float aSampleRate){ |
| 104 | + samplePeriod=1/aSampleRate; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Set the number of oscillators |
| 109 | + * @param[in] aNumberOfOscillators the number of oscillators. |
| 110 | + * @remark This method allocates memory. You should avoid calling it while processing audio. |
| 111 | + */ |
| 112 | + void setNumOscillators(int aNumberOfOscillators){ |
| 113 | + if(numberOfOscillators==aNumberOfOscillators){ //nothing to do |
| 114 | + return; |
| 115 | + } |
| 116 | + numberOfOscillators=aNumberOfOscillators; |
| 117 | + allocate(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Set the frequency. |
| 122 | + * @param[in] aFrequency The frequency of the oscillator. |
| 123 | + */ |
| 124 | + void setFrequency(float aFrequency){ |
| 125 | + lastCall=kSetFrequency; |
| 126 | + frequency=aFrequency; |
| 127 | + setFilter(aFrequency); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Set the mix. |
| 132 | + * Set the mix parametem that is the mix between the oscillator at the base frequency and the detuned oscillators. |
| 133 | + * @param[in] aMix the mix parameter. Values will be clipped in the range 0-1 |
| 134 | + */ |
| 135 | + void setMix(float aMix){ |
| 136 | + if(aMix>1) { |
| 137 | + aMix=1; |
| 138 | + } else if (aMix<0) { |
| 139 | + aMix=0; |
| 140 | + } |
| 141 | + float norm=1.0f/(numberOfOscillators-1); |
| 142 | + float detunedAmplitude=aMix*norm; |
| 143 | + for(int n=0; n<numberOfOscillators; n++){ |
| 144 | + amplitudes[n]=detunedAmplitude; |
| 145 | + } |
| 146 | + amplitudes[numberOfOscillators/2]=1-aMix; |
| 147 | + debugMessage("detunedamplitude", detunedAmplitude, 1-aMix); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Set the detune. |
| 152 | + * @param[in] aDetune the detune parameter. |
| 153 | + */ |
| 154 | + void setDetune(float aDetune){ |
| 155 | + lastCall=kSetDetune; |
| 156 | + for(int n=0; n<numberOfOscillators; n++){ |
| 157 | + float spread=(n-(int)(numberOfOscillators/2))/(float)numberOfOscillators; //this will be ==0 for the middle value of n, that is the middle oscillator will always be in tune |
| 158 | + float frequencyRatio=1+aDetune*spread; |
| 159 | + frequencyRatios[n]=1/frequencyRatio; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Get the oscillator samples |
| 165 | + * @param[out] output The array where the output is to be written |
| 166 | + */ |
| 167 | + void getSamples(FloatArray& output){ |
| 168 | + getSamples((float*)output, output.getSize()); |
| 169 | + } |
| 170 | + |
| 171 | + /* |
| 172 | + * Bypasses the built-in high pass filter. |
| 173 | + * Sets the state of the built-in high pass filter tuned at the fundamental frequency, which was used |
| 174 | + * in the original Roland design to attenuate aliasing below the fundamental frequency. |
| 175 | + * @param bypass If set to *true* the filter will be bypassed, if set to *false* the filter will be applied (default). |
| 176 | + */ |
| 177 | + void setFilterBypass(bool bypass){ |
| 178 | + filterBypass=bypass; |
| 179 | + } |
| 180 | + /** |
| 181 | + * Get the oscillator samples |
| 182 | + * @param[out] output Pointer to a location in memory where the output is to be written |
| 183 | + * @param[in] size Number of samples to be written. |
| 184 | + */ |
| 185 | + void getSamples(float* output, int size){ |
| 186 | + if(lastCall!=kUpdatePhaseIncrements){ //if the frequencies have not been updated after setDetune or setFrequency |
| 187 | + updatePhaseIncrements(); //this ensures that updatePhaseIncrements gets called at most once when frequency and/or detune are set |
| 188 | + //and regardless of the order in which they have been called. |
| 189 | + } |
| 190 | + for (int i=0; i<size; i++){ |
| 191 | + output[i]=0; |
| 192 | + for(int n=0; n<numberOfOscillators; n++){ |
| 193 | + output[i]+=phases[n]*amplitudes[n]; |
| 194 | + phases[n]+=phaseIncrements[n]; |
| 195 | + if(phases[n]>=1) |
| 196 | + phases[n]-=2; // 2 reflects the fact that the oscillator range is -1 to 1 |
| 197 | + } |
| 198 | + } |
| 199 | + if(filterBypass==false){ |
| 200 | + filter->process(output, output, size); //highpass filter |
| 201 | + } |
| 202 | + } |
| 203 | +}; |
| 204 | +#endif // __SuperSaw_h__ |
0 commit comments