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