Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Files Functions Variables Typedefs Macros Groups
OscillatorBank.h
1 /*
2  ____ _____ _ _
3 | __ )| ____| | / \
4 | _ \| _| | | / _ \
5 | |_) | |___| |___ / ___ \
6 |____/|_____|_____/_/ \_\
7 
8 The platform for ultra-low latency audio and sensor processing
9 
10 http://bela.io
11 
12 A project of the Augmented Instruments Laboratory within the
13 Centre for Digital Music at Queen Mary University of London.
14 http://www.eecs.qmul.ac.uk/~andrewm
15 
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
17  Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
18  Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
19 
20 The Bela software is distributed under the GNU Lesser General Public License
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
22 */
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <ne10/NE10.h>
27 
28 extern "C" {
29  // Function prototype for ARM assembly implementation of oscillator bank
30  void oscillator_bank_neon(int numAudioFrames, float *audioOut,
31  int activePartialNum, int lookupTableSize,
32  float *phases, float *frequencies, float *amplitudes,
33  float *freqDerivatives, float *ampDerivatives,
34  float *lookupTable);
35 }
36 
42 public:
44  sampleRate(1),
45  wavetable(NULL),
46  phases(NULL),
47  frequencies(NULL),
48  amplitudes(NULL),
49  dFrequencies(NULL),
50  dAmplitudes(NULL)
51  {}
52 
53  ~OscillatorBank(){
54  free(wavetable);
55  free(phases);
56  free(frequencies);
57  free(amplitudes);
58  free(dFrequencies);
59  free(dAmplitudes);
60  }
61 
75  int init(int newWavetableLength, int newNumOscillators, float newSampleRate){
76  wavetableLength = newWavetableLength;
77  numOscillators = newNumOscillators;
78  sampleRate = newSampleRate;
79  // Initialise the sine wavetable
80  if(posix_memalign((void **)&wavetable, 8, (wavetableLength + 1) * sizeof(float))) {
81  fprintf(stderr, "Error allocating wavetable\n");
82  return -1;
83  }
84 
85  // Allocate the other buffers
86  if(posix_memalign((void **)&phases, 16, numOscillators * sizeof(float))) {
87  fprintf(stderr, "Error allocating phase buffer\n");
88  return -1;
89  }
90  if(posix_memalign((void **)&frequencies, 16, numOscillators * sizeof(float))) {
91  fprintf(stderr, "Error allocating frequency buffer\n");
92  return -1;
93  }
94  if(posix_memalign((void **)&amplitudes, 16, numOscillators * sizeof(float))) {
95  fprintf(stderr, "Error allocating amplitude buffer\n");
96  return -1;
97  }
98  if(posix_memalign((void **)&dFrequencies, 16, numOscillators * sizeof(float))) {
99  fprintf(stderr, "Error allocating frequency derivative buffer\n");
100  return -1;
101  }
102  if(posix_memalign((void **)&dAmplitudes, 16, numOscillators * sizeof(float))) {
103  fprintf(stderr, "Error allocating amplitude derivative buffer\n");
104  return -1;
105  }
106  clearArrays();
107  return 0;
108  }
109 
118  float* getWavetable(){
119  return wavetable;
120  }
121 
129  return wavetableLength;
130  }
131 
138  return numOscillators;
139  }
140 
147  float* getAmplitudes(){
148  return amplitudes;
149  }
150 
157  float* getPhases(){
158  return phases;
159  }
160 
167  void setAmplitude(int n, float amplitude){
168  amplitudes[n] = amplitude;
169  }
170 
177  void setFrequency(int n, float frequency){
178  // For efficiency, frequency is internally stored in change in wavetable position per sample, not Hz or radians
179  frequencies[n] = frequency * (float)wavetableLength / sampleRate;
180  }
181 
186  void clearArrays(){
187  memset(phases, 0, sizeof(float)*numOscillators);
188  memset(dFrequencies, 0, sizeof(float)*numOscillators);
189  memset(dAmplitudes, 0, sizeof(float)*numOscillators);
190  }
191 
199  void process(int frames, float* output){
200  // Initialise buffer to 0
201  memset(output, 0, frames * sizeof(float));
202  oscillator_bank_neon(frames, output,
203  numOscillators, wavetableLength,
204  phases, frequencies, amplitudes,
205  dFrequencies, dAmplitudes,
206  wavetable);
207  }
208 
209 private:
210  float sampleRate;
211  int numOscillators;
212  int wavetableLength;
213  float *wavetable; // Buffer holding the precalculated sine lookup table
214  float *phases; // Buffer holding the phase of each oscillator
215  float *frequencies; // Buffer holding the frequencies of each oscillator
216  float *amplitudes; // Buffer holding the amplitudes of each oscillator
217  float *dFrequencies; // Buffer holding the derivatives of frequency
218  float *dAmplitudes; // Buffer holding the derivatives of amplitude
219 };
void setFrequency(int n, float frequency)
Definition: OscillatorBank.h:177
float * getAmplitudes()
Definition: OscillatorBank.h:147
float * getWavetable()
Definition: OscillatorBank.h:118
float * getPhases()
Definition: OscillatorBank.h:157
void setAmplitude(int n, float amplitude)
Definition: OscillatorBank.h:167
int getNumOscillators()
Definition: OscillatorBank.h:137
void process(int frames, float *output)
Definition: OscillatorBank.h:199
void clearArrays()
Definition: OscillatorBank.h:186
int init(int newWavetableLength, int newNumOscillators, float newSampleRate)
Definition: OscillatorBank.h:75
Definition: OscillatorBank.h:41
int getWavetableLength()
Definition: OscillatorBank.h:128