Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
OscillatorBank.h
1 #pragma once
2 #include <string.h>
3 #include <stdio.h>
4 
5 extern "C" {
6  // Function prototype for ARM assembly implementation of oscillator bank
7  void oscillator_bank_neon(int numAudioFrames, float *audioOut,
8  int activePartialNum, int lookupTableSize,
9  float *phases, float *frequencies, float *amplitudes,
10  float *freqDerivatives, float *ampDerivatives,
11  float *lookupTable);
12 }
13 
21 public:
23  sampleRate(1),
24  wavetable(NULL),
25  phases(NULL),
26  frequencies(NULL),
27  amplitudes(NULL),
28  dFrequencies(NULL),
29  dAmplitudes(NULL)
30  {}
31 
32  ~OscillatorBank(){
33  free(wavetable);
34  free(phases);
35  free(frequencies);
36  free(amplitudes);
37  free(dFrequencies);
38  free(dAmplitudes);
39  }
40 
54  int setup(float newSampleRate, unsigned int newWavetableLength, unsigned int newNumOscillators){
55  wavetableLength = newWavetableLength;
56  numOscillators = newNumOscillators;
57  sampleRate = newSampleRate;
58  // Initialise the sine wavetable
59  if(posix_memalign((void **)&wavetable, 16, (wavetableLength + 1) * sizeof(float))) {
60  fprintf(stderr, "Error allocating wavetable\n");
61  return -1;
62  }
63 
64  // Allocate the other buffers
65  if(posix_memalign((void **)&phases, 16, numOscillators * sizeof(float))) {
66  fprintf(stderr, "Error allocating phase buffer\n");
67  return -1;
68  }
69  if(posix_memalign((void **)&frequencies, 16, numOscillators * sizeof(float))) {
70  fprintf(stderr, "Error allocating frequency buffer\n");
71  return -1;
72  }
73  if(posix_memalign((void **)&amplitudes, 16, numOscillators * sizeof(float))) {
74  fprintf(stderr, "Error allocating amplitude buffer\n");
75  return -1;
76  }
77  if(posix_memalign((void **)&dFrequencies, 16, numOscillators * sizeof(float))) {
78  fprintf(stderr, "Error allocating frequency derivative buffer\n");
79  return -1;
80  }
81  if(posix_memalign((void **)&dAmplitudes, 16, numOscillators * sizeof(float))) {
82  fprintf(stderr, "Error allocating amplitude derivative buffer\n");
83  return -1;
84  }
85  clearArrays();
86  return 0;
87  }
88 
97  float* getWavetable(){
98  return wavetable;
99  }
100 
108  return wavetableLength;
109  }
110 
117  return numOscillators;
118  }
119 
126  float* getAmplitudes(){
127  return amplitudes;
128  }
129 
136  float* getPhases(){
137  return phases;
138  }
139 
146  void setAmplitude(int n, float amplitude){
147  amplitudes[n] = amplitude;
148  }
149 
156  void setFrequency(int n, float frequency){
157  // For efficiency, frequency is internally stored in change in wavetable position per sample, not Hz or radians
158  frequencies[n] = frequency * (float)wavetableLength / sampleRate;
159  }
160 
165  void clearArrays(){
166  memset(phases, 0, sizeof(float)*numOscillators);
167  memset(dFrequencies, 0, sizeof(float)*numOscillators);
168  memset(dAmplitudes, 0, sizeof(float)*numOscillators);
169  }
170 
178  void process(unsigned int frames, float* output){
179  // Initialise buffer to 0
180  memset(output, 0, frames * sizeof(float));
181  oscillator_bank_neon(frames, output,
182  numOscillators, wavetableLength,
183  phases, frequencies, amplitudes,
184  dFrequencies, dAmplitudes,
185  wavetable);
186  }
187 
188 private:
189  float sampleRate;
190  int numOscillators;
191  int wavetableLength;
192  float *wavetable; // Buffer holding the precalculated sine lookup table
193  float *phases; // Buffer holding the phase of each oscillator
194  float *frequencies; // Buffer holding the frequencies of each oscillator
195  float *amplitudes; // Buffer holding the amplitudes of each oscillator
196  float *dFrequencies; // Buffer holding the derivatives of frequency
197  float *dAmplitudes; // Buffer holding the derivatives of amplitude
198 };
void setFrequency(int n, float frequency)
Definition: OscillatorBank.h:156
float * getAmplitudes()
Definition: OscillatorBank.h:126
float * getWavetable()
Definition: OscillatorBank.h:97
float * getPhases()
Definition: OscillatorBank.h:136
void setAmplitude(int n, float amplitude)
Definition: OscillatorBank.h:146
void process(unsigned int frames, float *output)
Definition: OscillatorBank.h:178
int setup(float newSampleRate, unsigned int newWavetableLength, unsigned int newNumOscillators)
Definition: OscillatorBank.h:54
int getNumOscillators()
Definition: OscillatorBank.h:116
void clearArrays()
Definition: OscillatorBank.h:165
Definition: OscillatorBank.h:20
int getWavetableLength()
Definition: OscillatorBank.h:107