annotate examples/04-Audio/oscillator-bank/render.cpp @ 507:1cec96845a23 prerelease

Explanted explantation
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 22 Jun 2016 01:51:17 +0100
parents b935f890e512
children cdabbaf3a252
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #include <Bela.h>
robert@464 26 #include <rtdk.h>
robert@464 27 #include <cstdlib>
robert@464 28 #include <cmath>
robert@464 29 #include <cstring>
robert@464 30 #include <time.h>
robert@464 31
robert@464 32 const float kMinimumFrequency = 20.0f;
robert@464 33 const float kMaximumFrequency = 8000.0f;
robert@464 34
robert@464 35 float *gWavetable; // Buffer holding the precalculated sine lookup table
robert@464 36 float *gPhases; // Buffer holding the phase of each oscillator
robert@464 37 float *gFrequencies; // Buffer holding the frequencies of each oscillator
robert@464 38 float *gAmplitudes; // Buffer holding the amplitudes of each oscillator
robert@464 39 float *gDFrequencies; // Buffer holding the derivatives of frequency
robert@464 40 float *gDAmplitudes; // Buffer holding the derivatives of amplitude
robert@464 41
robert@464 42 float gAudioSampleRate;
robert@464 43 int gSampleCount; // Sample counter for indicating when to update frequencies
robert@464 44 float gNewMinFrequency;
robert@464 45 float gNewMaxFrequency;
robert@464 46
robert@464 47 // Task for handling the update of the frequencies using the matrix
robert@464 48 AuxiliaryTask gFrequencyUpdateTask;
robert@464 49
robert@464 50 // These settings are carried over from main.cpp
robert@464 51 // Setting global variables is an alternative approach
robert@464 52 // to passing a structure to userData in setup()
robert@464 53
robert@464 54 extern int gNumOscillators;
robert@464 55 extern int gWavetableLength;
robert@464 56
robert@464 57 void recalculate_frequencies();
robert@464 58
robert@464 59 extern "C" {
robert@464 60 // Function prototype for ARM assembly implementation of oscillator bank
robert@464 61 void oscillator_bank_neon(int numAudioFrames, float *audioOut,
robert@464 62 int activePartialNum, int lookupTableSize,
robert@464 63 float *phases, float *frequencies, float *amplitudes,
robert@464 64 float *freqDerivatives, float *ampDerivatives,
robert@464 65 float *lookupTable);
robert@464 66 }
robert@464 67
robert@464 68 // setup() is called once before the audio rendering starts.
robert@464 69 // Use it to perform any initialisation and allocation which is dependent
robert@464 70 // on the period size or sample rate.
robert@464 71 //
robert@464 72 // userData holds an opaque pointer to a data structure that was passed
robert@464 73 // in from the call to initAudio().
robert@464 74 //
robert@464 75 // Return true on success; returning false halts the program.
robert@464 76 bool setup(BelaContext *context, void *userData)
robert@464 77 {
robert@464 78 srandom(time(NULL));
robert@464 79
robert@464 80 if(context->audioChannels != 2) {
robert@464 81 rt_printf("Error: this example needs stereo audio enabled\n");
robert@464 82 return false;
robert@464 83 }
robert@464 84
robert@464 85 // Initialise the sine wavetable
robert@464 86 if(posix_memalign((void **)&gWavetable, 8, (gWavetableLength + 1) * sizeof(float))) {
robert@464 87 rt_printf("Error allocating wavetable\n");
robert@464 88 return false;
robert@464 89 }
robert@464 90 for(int n = 0; n < gWavetableLength + 1; n++)
robert@464 91 gWavetable[n] = sinf(2.0 * M_PI * (float)n / (float)gWavetableLength);
robert@464 92
robert@464 93 // Allocate the other buffers
robert@464 94 if(posix_memalign((void **)&gPhases, 16, gNumOscillators * sizeof(float))) {
robert@464 95 rt_printf("Error allocating phase buffer\n");
robert@464 96 return false;
robert@464 97 }
robert@464 98 if(posix_memalign((void **)&gFrequencies, 16, gNumOscillators * sizeof(float))) {
robert@464 99 rt_printf("Error allocating frequency buffer\n");
robert@464 100 return false;
robert@464 101 }
robert@464 102 if(posix_memalign((void **)&gAmplitudes, 16, gNumOscillators * sizeof(float))) {
robert@464 103 rt_printf("Error allocating amplitude buffer\n");
robert@464 104 return false;
robert@464 105 }
robert@464 106 if(posix_memalign((void **)&gDFrequencies, 16, gNumOscillators * sizeof(float))) {
robert@464 107 rt_printf("Error allocating frequency derivative buffer\n");
robert@464 108 return false;
robert@464 109 }
robert@464 110 if(posix_memalign((void **)&gDAmplitudes, 16, gNumOscillators * sizeof(float))) {
robert@464 111 rt_printf("Error allocating amplitude derivative buffer\n");
robert@464 112 return false;
robert@464 113 }
robert@464 114
robert@464 115 // Initialise buffer contents
robert@464 116
robert@464 117 float freq = kMinimumFrequency;
robert@464 118 float increment = (kMaximumFrequency - kMinimumFrequency) / (float)gNumOscillators;
robert@464 119
robert@464 120 for(int n = 0; n < gNumOscillators; n++) {
robert@464 121 gPhases[n] = 0.0;
robert@464 122
robert@464 123 if(context->analogFrames == 0) {
robert@464 124 // Random frequencies when used without matrix
robert@464 125 gFrequencies[n] = kMinimumFrequency + (kMaximumFrequency - kMinimumFrequency) * ((float)random() / (float)RAND_MAX);
robert@464 126 }
robert@464 127 else {
robert@464 128 // Constant spread of frequencies when used with matrix
robert@464 129 gFrequencies[n] = freq;
robert@464 130 freq += increment;
robert@464 131 }
robert@464 132
robert@464 133 // For efficiency, frequency is expressed in change in wavetable position per sample, not Hz or radians
robert@464 134 gFrequencies[n] *= (float)gWavetableLength / context->audioSampleRate;
robert@464 135 gAmplitudes[n] = ((float)random() / (float)RAND_MAX) / (float)gNumOscillators;
robert@464 136 gDFrequencies[n] = gDAmplitudes[n] = 0.0;
robert@464 137 }
robert@464 138
robert@464 139 increment = 0;
robert@464 140 freq = 440.0;
robert@464 141
robert@464 142 for(int n = 0; n < gNumOscillators; n++) {
robert@464 143 // Update the frequencies to a regular spread, plus a small amount of randomness
robert@464 144 // to avoid weird phase effects
robert@464 145 float randScale = 0.99 + .02 * (float)random() / (float)RAND_MAX;
robert@464 146 float newFreq = freq * randScale;
robert@464 147
robert@464 148 // For efficiency, frequency is expressed in change in wavetable position per sample, not Hz or radians
robert@464 149 gFrequencies[n] = newFreq * (float)gWavetableLength / context->audioSampleRate;
robert@464 150
robert@464 151 freq += increment;
robert@464 152 }
robert@464 153
robert@464 154 // Initialise auxiliary tasks
robert@464 155 if((gFrequencyUpdateTask = Bela_createAuxiliaryTask(&recalculate_frequencies, 85, "bela-update-frequencies")) == 0)
robert@464 156 return false;
robert@464 157
robert@464 158 //for(int n = 0; n < gNumOscillators; n++)
robert@464 159 // rt_printf("%f\n", gFrequencies[n]);
robert@464 160
robert@464 161 gAudioSampleRate = context->audioSampleRate;
robert@464 162 gSampleCount = 0;
robert@464 163
robert@464 164 return true;
robert@464 165 }
robert@464 166
robert@464 167 // render() is called regularly at the highest priority by the audio engine.
robert@464 168 // Input and output are given from the audio hardware and the other
robert@464 169 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
robert@464 170 // will be 0.
robert@464 171
robert@464 172 void render(BelaContext *context, void *userData)
robert@464 173 {
robert@464 174 // Initialise buffer to 0
robert@464 175 memset(context->audioOut, 0, 2 * context->audioFrames * sizeof(float));
robert@464 176
robert@464 177 // Render audio frames
robert@464 178 oscillator_bank_neon(context->audioFrames, context->audioOut,
robert@464 179 gNumOscillators, gWavetableLength,
robert@464 180 gPhases, gFrequencies, gAmplitudes,
robert@464 181 gDFrequencies, gDAmplitudes,
robert@464 182 gWavetable);
robert@464 183
robert@464 184 if(context->analogFrames != 0 && (gSampleCount += context->audioFrames) >= 128) {
robert@464 185 gSampleCount = 0;
robert@464 186 gNewMinFrequency = map(context->analogIn[0], 0, 1.0, 1000.0f, 8000.0f);
robert@464 187 gNewMaxFrequency = map(context->analogIn[1], 0, 1.0, 1000.0f, 8000.0f);
robert@464 188
robert@464 189 // Make sure max >= min
robert@464 190 if(gNewMaxFrequency < gNewMinFrequency) {
robert@464 191 float temp = gNewMaxFrequency;
robert@464 192 gNewMaxFrequency = gNewMinFrequency;
robert@464 193 gNewMinFrequency = temp;
robert@464 194 }
robert@464 195
robert@464 196 // Request that the lower-priority task run at next opportunity
robert@464 197 //Bela_scheduleAuxiliaryTask(gFrequencyUpdateTask);
robert@464 198 }
robert@464 199 }
robert@464 200
robert@464 201 // This is a lower-priority call to update the frequencies which will happen
robert@464 202 // periodically when the matrix is enabled. By placing it at a lower priority,
robert@464 203 // it has minimal effect on the audio performance but it will take longer to
robert@464 204 // complete if the system is under heavy audio load.
robert@464 205
robert@464 206 void recalculate_frequencies()
robert@464 207 {
robert@464 208 float freq = gNewMinFrequency;
robert@464 209 float increment = (gNewMaxFrequency - gNewMinFrequency) / (float)gNumOscillators;
robert@464 210
robert@464 211 for(int n = 0; n < gNumOscillators; n++) {
robert@464 212 // Update the frequencies to a regular spread, plus a small amount of randomness
robert@464 213 // to avoid weird phase effects
robert@464 214 float randScale = 0.99 + .02 * (float)random() / (float)RAND_MAX;
robert@464 215 float newFreq = freq * randScale;
robert@464 216
robert@464 217 // For efficiency, frequency is expressed in change in wavetable position per sample, not Hz or radians
robert@464 218 gFrequencies[n] = newFreq * (float)gWavetableLength / gAudioSampleRate;
robert@464 219
robert@464 220 freq += increment;
robert@464 221 }
robert@464 222 }
robert@464 223
robert@464 224
robert@464 225 // cleanup() is called once at the end, after the audio has stopped.
robert@464 226 // Release any resources that were allocated in setup().
robert@464 227
robert@464 228 void cleanup(BelaContext *context, void *userData)
robert@464 229 {
robert@464 230 free(gWavetable);
robert@464 231 free(gPhases);
robert@464 232 free(gFrequencies);
robert@464 233 free(gAmplitudes);
robert@464 234 free(gDFrequencies);
robert@464 235 free(gDAmplitudes);
robert@464 236 }
robert@464 237
robert@464 238
robert@464 239 /**
robert@500 240 \example oscillator-bank/render.cpp
robert@464 241
robert@464 242 Oscillator Bank
robert@464 243 ----------------------
robert@464 244
robert@464 245 These files demonstrate an oscillator bank implemented in assembly code
robert@464 246 that is used as part of the d-box project.
robert@464 247 */