Bela.h
Go to the documentation of this file.
1 
39 #ifndef BELA_H_
40 #define BELA_H_
41 
42 #include <stdint.h>
43 #include <unistd.h>
44 #include <rtdk.h>
45 #include "digital_gpio_mapping.h"
46 #include <GPIOcontrol.h>
47 
48 // Useful constants
49 
51 #define CODEC_I2C_ADDRESS 0x18 // Address of TLV320AIC3104 codec
52 
53 #define MAX_PRU_FILENAME_LENGTH 256
54 #define MAX_SERVERNAME_LENGTH 256
55 
64 #define BELA_AUDIO_PRIORITY 95
65 
66 // Default volume levels
67 
77 #define DEFAULT_DAC_LEVEL 0.0
78 
82 #define DEFAULT_ADC_LEVEL -6.0
83 
84 
88 #define DEFAULT_PGA_GAIN 16
89 
93 #define DEFAULT_HP_LEVEL -6.0
94 
99 #define BELA_FLAG_INTERLEAVED (1 << 0) // Set if buffers are interleaved
100 
103 #define BELA_FLAG_ANALOG_OUTPUTS_PERSIST (1 << 1) // Set if analog/digital outputs persist for future buffers
104 
114 typedef struct {
115  // These items might be adjusted by the user:
116 
136 
140  float dacLevel;
142  float adcLevel;
144  float pgaGain[2];
149 
151  int pruNumber;
153  char pruFilename[MAX_PRU_FILENAME_LENGTH];
155  int verbose;
156 
157  // These items are application-dependent but should probably be
158  // determined by the programmer rather than the user
159 
166 
167  // These items are hardware-dependent and should only be changed
168  // to run on different hardware
169 
178  char serverName[MAX_SERVERNAME_LENGTH];
180 
181 
190 typedef struct {
196  const float * const audioIn;
197 
203  float * const audioOut;
204 
210  const float * const analogIn;
211 
217  float * const analogOut;
218 
222  uint32_t * const digital;
223 
225  const uint32_t audioFrames;
227  const uint32_t audioInChannels;
229  const uint32_t audioOutChannels;
231  const float audioSampleRate;
232 
236  const uint32_t analogFrames;
237 
241  const uint32_t analogInChannels;
242 
246  const uint32_t analogOutChannels;
247 
254  const float analogSampleRate;
255 
257  const uint32_t digitalFrames;
261  const uint32_t digitalChannels;
263  const float digitalSampleRate;
264 
271  const uint64_t audioFramesElapsed;
272 
281  const uint32_t flags;
282 } BelaContext;
283 
289 typedef void* AuxiliaryTask; // Opaque data type to keep track of aux tasks
290 
298 extern int gShouldStop;
299 
300 // *** User-defined render functions ***
301 
328 bool setup(BelaContext *context, void *userData);
329 
344 void render(BelaContext *context, void *userData);
345 
361 void cleanup(BelaContext *context, void *userData);
362 
374 // *** Command-line settings ***
375 
385 void Bela_defaultSettings(BelaInitSettings *settings);
386 
408 int Bela_getopt_long(int argc, char *argv[], const char *customShortOptions,
409  const struct option *customLongOptions, BelaInitSettings *settings);
410 
418 void Bela_usage();
419 
426 void Bela_setVerboseLevel(int level);
427 
428 
429 // *** Audio control functions ***
430 
446 int Bela_initAudio(BelaInitSettings *settings, void *userData);
447 
456 int Bela_startAudio();
457 
465 void Bela_stopAudio();
466 
474 void Bela_cleanupAudio();
475 
487 // *** Volume and level controls ***
488 
503 int Bela_setDACLevel(float decibels);
504 
519 int Bela_setADCLevel(float decibels);
520 
521 
538 int Bela_setPgaGain(float decibels, int channel);
539 
555 int Bela_setHeadphoneLevel(float decibels);
556 
570 int Bela_muteSpeakers(int mute);
571 
588 // *** Functions for creating auxiliary tasks ***
589 
605 AuxiliaryTask Bela_createAuxiliaryTask(void (*functionToCall)(void*), int priority, const char *name, void* args, bool autoSchedule = false);
606 AuxiliaryTask Bela_createAuxiliaryTask(void (*functionToCall)(void), int priority, const char *name, bool autoSchedule = false);
607 
635 void Bela_autoScheduleAuxiliaryTasks();
636 
638 #include <Utilities.h>
639 
640 #endif /* BELA_H_ */
int Bela_getopt_long(int argc, char *argv[], const char *customShortOptions, const struct option *customLongOptions, BelaInitSettings *settings)
Get long options from command line argument list, including Bela standard options.
const float *const analogIn
Buffer holding analog input samples.
Definition: Bela.h:210
float adcLevel
Level for the audio ADC input.
Definition: Bela.h:142
int ampMutePin
Pin where amplifier mute can be found.
Definition: Bela.h:173
const uint32_t audioInChannels
Number of input audio channels.
Definition: Bela.h:227
const float digitalSampleRate
Digital sample rate in Hz (currently always 44100.0)
Definition: Bela.h:263
const uint32_t analogInChannels
Number of input analog channels.
Definition: Bela.h:241
void Bela_usage()
Print usage information for Bela standard options.
int receivePort
Port where the UDP server will listen.
Definition: Bela.h:175
float headphoneLevel
Level for the headphone output.
Definition: Bela.h:146
const uint32_t analogOutChannels
Number of output analog channels.
Definition: Bela.h:246
void Bela_setVerboseLevel(int level)
Set level of verbose (debugging) printing.
int Bela_startAuxiliaryTask(AuxiliaryTask it)
Stop processing audio and sensor data.
const uint32_t flags
Other audio/sensor settings.
Definition: Bela.h:281
int Bela_setDACLevel(float decibels)
Set the level of the audio DAC.
int transmitPort
Port where the UDP client will transmit.
Definition: Bela.h:177
int verbose
Whether to use verbose logging.
Definition: Bela.h:155
AuxiliaryTask Bela_createAuxiliaryTask(void(*functionToCall)(void *), int priority, const char *name, void *args, bool autoSchedule=false)
Create a new auxiliary task.
const uint32_t digitalChannels
Number of digital channels.
Definition: Bela.h:261
const uint32_t digitalFrames
Number of digital frames per period.
Definition: Bela.h:257
int numAudioInChannels
How many audio input channels.
Definition: Bela.h:127
int Bela_setHeadphoneLevel(float decibels)
Set the level of the onboard headphone amplifier.
void Bela_scheduleAuxiliaryTask(AuxiliaryTask task)
Run an auxiliary task which has previously been created.
int pruNumber
Which PRU (0 or 1) the code should run on.
Definition: Bela.h:151
void cleanup(BelaContext *context, void *userData)
User-defined cleanup function which runs when the program finishes.
Definition: 01-Basics/minimal/render.cpp:51
Structure containing initialisation parameters for the real-time audio control system.
Definition: Bela.h:114
float *const audioOut
Buffer holding audio output samples.
Definition: Bela.h:203
int numAudioOutChannels
How many audio out channels.
Definition: Bela.h:129
int interleave
Whether audio/analog data should be interleaved.
Definition: Bela.h:161
const uint32_t audioFrames
Number of audio frames per period.
Definition: Bela.h:225
int Bela_muteSpeakers(int mute)
Mute or unmute the onboard speaker amplifiers.
const float analogSampleRate
Analog sample rate in Hz.
Definition: Bela.h:254
Structure holding current audio and sensor settings and pointers to data buffers. ...
Definition: Bela.h:190
bool setup(BelaContext *context, void *userData)
User-defined initialisation function which runs before audio rendering begins.
Definition: 01-Basics/minimal/render.cpp:35
int numMuxChannels
How many channels to use on the multiplexer capelet, if enabled.
Definition: Bela.h:148
int Bela_initAudio(BelaInitSettings *settings, void *userData)
Initialise audio and sensor rendering environment.
int numAnalogOutChannels
How many analog output channels.
Definition: Bela.h:133
float *const analogOut
Buffer holding analog output samples.
Definition: Bela.h:217
const uint32_t audioOutChannels
Number of output audio channels.
Definition: Bela.h:229
int Bela_setPgaGain(float decibels, int channel)
Set the gain of the audio preamplifier.
int beginMuted
Whether to begin with the speakers muted.
Definition: Bela.h:138
int numAnalogInChannels
How many analog input channels.
Definition: Bela.h:131
int gShouldStop
Wiring-inspired utility functions and macros.
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition: 01-Basics/minimal/render.cpp:44
uint32_t *const digital
Buffer holding digital input/output samples.
Definition: Bela.h:222
const uint64_t audioFramesElapsed
Number of elapsed audio frames since the start of rendering.
Definition: Bela.h:271
int codecI2CAddress
Where the codec can be found on the I2C bus.
Definition: Bela.h:171
float dacLevel
Level for the audio DAC output.
Definition: Bela.h:140
int Bela_setADCLevel(float decibels)
Set the level of the audio ADC.
const uint32_t analogFrames
Number of analog frames per period.
Definition: Bela.h:236
int numDigitalChannels
How many channels for the GPIOs.
Definition: Bela.h:135
void * AuxiliaryTask
Definition: Bela.h:289
int useAnalog
Whether to use the analog input and output.
Definition: Bela.h:123
const float audioSampleRate
Audio sample rate in Hz (currently always 44100.0)
Definition: Bela.h:231
int Bela_startAudio()
Begin processing audio and sensor data.
int periodSize
Number of (analog) frames per period.
Definition: Bela.h:121
int analogOutputsPersist
Whether analog outputs should persist to future frames.
Definition: Bela.h:165
const float *const audioIn
Buffer holding audio input samples.
Definition: Bela.h:196
int useDigital
Whether to use the 16 programmable GPIOs.
Definition: Bela.h:125
void Bela_defaultSettings(BelaInitSettings *settings)
Initialise the data structure containing settings for Bela.
void Bela_cleanupAudio()
Clean up resources from audio and sensor processing.