annotate include/BeagleRT.h @ 46:eac5d8f40b48 newapi

Fixed file structure to reflect merging include files into BeagleRT.h
author andrewm
date Thu, 28 May 2015 14:38:00 -0400
parents
children 643cbee74eda
rev   line source
andrewm@46 1 /*
andrewm@46 2 * RTAudio.h
andrewm@46 3 *
andrewm@46 4 * Central control code for hard real-time audio on BeagleBone Black
andrewm@46 5 * using PRU and Xenomai Linux extensions. This code began as part
andrewm@46 6 * of the Hackable Instruments project (EPSRC) at Queen Mary University
andrewm@46 7 * of London, 2013-14.
andrewm@46 8 *
andrewm@46 9 * (c) 2014 Victor Zappi and Andrew McPherson
andrewm@46 10 * Queen Mary University of London
andrewm@46 11 */
andrewm@46 12
andrewm@46 13
andrewm@46 14 #ifndef BEAGLERT_H_
andrewm@46 15 #define BEAGLERT_H_
andrewm@46 16
andrewm@46 17 #include <stdint.h>
andrewm@46 18 #include "digital_gpio_mapping.h"
andrewm@46 19
andrewm@46 20 // Useful constants
andrewm@46 21 #define DBOX_CAPE // New custom cape
andrewm@46 22
andrewm@46 23 #ifdef DBOX_CAPE
andrewm@46 24 #define CODEC_I2C_ADDRESS 0x18 // Address of TLV320AIC3104 codec
andrewm@46 25 #else
andrewm@46 26 #define CODEC_I2C_ADDRESS 0x1B // Address of TLV320AIC3106 codec
andrewm@46 27 #endif
andrewm@46 28
andrewm@46 29 // Default volume levels
andrewm@46 30 #define DEFAULT_DAC_LEVEL 0.0
andrewm@46 31 #define DEFAULT_ADC_LEVEL -6.0
andrewm@46 32 #define DEFAULT_HP_LEVEL -6.0
andrewm@46 33
andrewm@46 34 #define MAX_PRU_FILENAME_LENGTH 256
andrewm@46 35 #define MAX_SERVERNAME_LENGTH 256
andrewm@46 36
andrewm@46 37 // Priority at which BeagleRT audio code runs
andrewm@46 38 // Higher numbers preempt the audio; lower numbers are preempted by it
andrewm@46 39 #define BEAGLERT_AUDIO_PRIORITY 95
andrewm@46 40
andrewm@46 41 // Flags for BeagleRTContext data structure
andrewm@46 42 #define BEAGLERT_FLAG_INTERLEAVED (1 << 0) // Set if buffers are interleaved
andrewm@46 43 #define BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST (1 << 1) // Set if analog/digital outputs persist for future buffers
andrewm@46 44
andrewm@46 45 // Mappings from pin numbers on PCB to actual DAC channels
andrewm@46 46 // This gives the DAC and ADC connectors the same effective pinout
andrewm@46 47
andrewm@46 48 #define DAC_PIN0 6
andrewm@46 49 #define DAC_PIN1 4
andrewm@46 50 #define DAC_PIN2 2
andrewm@46 51 #define DAC_PIN3 0
andrewm@46 52 #define DAC_PIN4 1
andrewm@46 53 #define DAC_PIN5 3
andrewm@46 54 #define DAC_PIN6 5
andrewm@46 55 #define DAC_PIN7 7
andrewm@46 56
andrewm@46 57 #define ADC_PIN0 0
andrewm@46 58 #define ADC_PIN1 1
andrewm@46 59 #define ADC_PIN2 2
andrewm@46 60 #define ADC_PIN3 3
andrewm@46 61 #define ADC_PIN4 4
andrewm@46 62 #define ADC_PIN5 5
andrewm@46 63 #define ADC_PIN6 6
andrewm@46 64 #define ADC_PIN7 7
andrewm@46 65
andrewm@46 66 // Structure which contains initialisation parameters for the
andrewm@46 67 // real-time audio system
andrewm@46 68 typedef struct {
andrewm@46 69 // These items might be adjusted by the user:
andrewm@46 70 int periodSize; // Number of (analog) frames per period; audio is twice this
andrewm@46 71 int useAnalog; // Whether to use the analog
andrewm@46 72 int useDigital; // Whether to use the 16 programmable GPIOs
andrewm@46 73 int numAnalogChannels; // How many channels for the ADC and DAC
andrewm@46 74 int numDigitalChannels; // How many channels for the GPIOs
andrewm@46 75
andrewm@46 76 int beginMuted; // Whether to begin with the speakers muted
andrewm@46 77 float dacLevel; // Level for the audio DAC output
andrewm@46 78 float adcLevel; // Level for the audio ADC input
andrewm@46 79 float headphoneLevel; // Level for the headphone output
andrewm@46 80
andrewm@46 81 char pruFilename[MAX_PRU_FILENAME_LENGTH]; // The external .bin file to load. If empty will use PRU code from pru_rtaudio_bin.h
andrewm@46 82 int verbose; // Whether to use verbose logging
andrewm@46 83
andrewm@46 84 // These items are application-dependent but should probably be
andrewm@46 85 // determined by the programmer rather than the user
andrewm@46 86 int interleave; // Whether audio/analog data should be interleaved
andrewm@46 87 int analogOutputsPersist; // Whether analog outputs should persist to future frames
andrewm@46 88 // n.b. digital pins always persist, audio never does
andrewm@46 89
andrewm@46 90 // These items are hardware-dependent and should only be changed
andrewm@46 91 // to run on different hardware
andrewm@46 92 int codecI2CAddress; // Where the codec can be found on the I2C bus
andrewm@46 93 int ampMutePin; // Pin where amplifier mute can be found
andrewm@46 94 int receivePort; // Port where the UDP server will listen
andrewm@46 95 int transmitPort; // Port where the UDP client will transmit
andrewm@46 96 char serverName[MAX_SERVERNAME_LENGTH];
andrewm@46 97 } BeagleRTInitSettings;
andrewm@46 98
andrewm@46 99 // BeagleRTContext data structure
andrewm@46 100 // Holds information passed to the render() function and related calls
andrewm@46 101 // Contains the current audio and sensor settings and pointers to the data buffers
andrewm@46 102
andrewm@46 103 typedef struct {
andrewm@46 104 float *audioIn;
andrewm@46 105 float *audioOut;
andrewm@46 106 float *analogIn;
andrewm@46 107 float *analogOut;
andrewm@46 108 uint32_t *digital;
andrewm@46 109
andrewm@46 110 uint32_t audioFrames;
andrewm@46 111 uint32_t audioChannels;
andrewm@46 112 float audioSampleRate;
andrewm@46 113
andrewm@46 114 uint32_t analogFrames;
andrewm@46 115 uint32_t analogChannels;
andrewm@46 116 float analogSampleRate;
andrewm@46 117
andrewm@46 118 uint32_t digitalFrames;
andrewm@46 119 uint32_t digitalChannels;
andrewm@46 120 float digitalSampleRate;
andrewm@46 121
andrewm@46 122 uint64_t audioSampleCount;
andrewm@46 123 uint32_t flags;
andrewm@46 124 } BeagleRTContext;
andrewm@46 125
andrewm@46 126 enum {
andrewm@46 127 kAmplifierMutePin = 61 // P8-26 controls amplifier mute
andrewm@46 128 };
andrewm@46 129
andrewm@46 130 typedef void* AuxiliaryTask; // Opaque data type to keep track of aux tasks
andrewm@46 131
andrewm@46 132 // Flag that indicates when the audio will stop; can be read or
andrewm@46 133 // set by other components which should end at the same time as the audio
andrewm@46 134 extern bool gShouldStop;
andrewm@46 135
andrewm@46 136 // *** User-defined render functions ***
andrewm@46 137
andrewm@46 138 /**
andrewm@46 139 * \brief User-defined initialisation function which runs before audio rendering begins.
andrewm@46 140 *
andrewm@46 141 * This function runs once at the beginning of the program, after most of the system
andrewm@46 142 * initialisation has begun but before audio rendering starts. Use it to prepare any
andrewm@46 143 * memory or resources that will be needed in render().
andrewm@46 144 *
andrewm@46 145 * \param context Data structure holding information on sample rates, numbers of channels,
andrewm@46 146 * frame sizes and other state. Note: the buffers for audio, analog and digital data will
andrewm@46 147 * \b not yet be available to use. Do not attempt to read or write audio or sensor data
andrewm@46 148 * in initialise_render().
andrewm@46 149 * \param userData An opaque pointer to an optional user-defined data structure. Whatever
andrewm@46 150 * is passed as the second argument to BeagleRT_initAudio() will appear here.
andrewm@46 151 *
andrewm@46 152 * \return true on success, or false if an error occurred. If no initialisation is
andrewm@46 153 * required, initialise_render() should return true.
andrewm@46 154 */
andrewm@46 155 bool initialise_render(BeagleRTContext *context, void *userData);
andrewm@46 156
andrewm@46 157 /**
andrewm@46 158 * \brief User-defined callback function to process audio and sensor data.
andrewm@46 159 *
andrewm@46 160 * This function is called regularly by the system every time there is a new block of
andrewm@46 161 * audio and/or sensor data to process. Your code should process the requested samples
andrewm@46 162 * of data, store the results within \c context, and return.
andrewm@46 163 *
andrewm@46 164 * \param context Data structure holding buffers for audio, analog and digital data. The
andrewm@46 165 * structure also holds information on numbers of channels, frame sizes and sample rates,
andrewm@46 166 * which are guaranteed to remain the same throughout the program and to match what was
andrewm@46 167 * passed to initialise_render().
andrewm@46 168 * \param userData An opaque pointer to an optional user-defined data structure. Will
andrewm@46 169 * be the same as the \c userData parameter passed to initialise_render().
andrewm@46 170 */
andrewm@46 171 void render(BeagleRTContext *context, void *userData);
andrewm@46 172
andrewm@46 173 /**
andrewm@46 174 * \brief User-defined cleanup function which runs when the program finishes.
andrewm@46 175 *
andrewm@46 176 * This function is called by the system once after audio rendering has finished, before the
andrewm@46 177 * program quits. Use it to release any memory allocated in initialise_render() and to perform
andrewm@46 178 * any other required cleanup. If no initialisation is performed in initialise_render(), then
andrewm@46 179 * this function will usually be empty.
andrewm@46 180 *
andrewm@46 181 * \param context Data structure holding information on sample rates, numbers of channels,
andrewm@46 182 * frame sizes and other state. Note: the buffers for audio, analog and digital data will
andrewm@46 183 * no longer be available to use. Do not attempt to read or write audio or sensor data
andrewm@46 184 * in cleanup_render().
andrewm@46 185 * \param userData An opaque pointer to an optional user-defined data structure. Will
andrewm@46 186 * be the same as the \c userData parameter passed to initialise_render() and render().
andrewm@46 187 */
andrewm@46 188 void cleanup_render(BeagleRTContext *context, void *userData);
andrewm@46 189
andrewm@46 190 // *** Command-line settings ***
andrewm@46 191
andrewm@46 192 /**
andrewm@46 193 * \brief Initialise the data structure containing settings for BeagleRT.
andrewm@46 194 *
andrewm@46 195 * This function should be called in main() before parsing any command-line arguments. It
andrewm@46 196 * sets default values in the data structure which specifies the BeagleRT settings, including
andrewm@46 197 * frame sizes, numbers of channels, volume levels and other parameters.
andrewm@46 198 *
andrewm@46 199 * \param settings Structure holding initialisation data for BeagleRT.
andrewm@46 200 */
andrewm@46 201 void BeagleRT_defaultSettings(BeagleRTInitSettings *settings);
andrewm@46 202
andrewm@46 203 /**
andrewm@46 204 * \brief Get long options from command line argument list, including BeagleRT standard options
andrewm@46 205 *
andrewm@46 206 * This function should be used in main() to process command line options, in place of the
andrewm@46 207 * standard library getopt_long(). Internally, it parses standard BeagleRT command-line options,
andrewm@46 208 * storing the results in the settings data structure. Any options which are not part of the
andrewm@46 209 * BeagleRT standard options will be returned, as they would normally be in getopt_long().
andrewm@46 210 *
andrewm@46 211 * \param argc Number of command line options, as passed to main().
andrewm@46 212 * \param argv Array of command line options, as passed to main().
andrewm@46 213 * \param customShortOptions List of short options to be parsed, analogous to getopt_long(). This
andrewm@46 214 * list should not include any characters already parsed as part of the BeagleRT standard options.
andrewm@46 215 * \param customLongOptions List of long options to parsed, analogous to getopt_long(). This
andrewm@46 216 * list should not include any long options already parsed as part of the BeagleRT standard options.
andrewm@46 217 * \param settings Data structure holding initialisation settings for BeagleRT. Any standard options
andrewm@46 218 * parsed will automatically update this data structure.
andrewm@46 219 *
andrewm@46 220 * \return Value of the next option parsed which is not a BeagleRT standard option, or -1 when the
andrewm@46 221 * argument list has been exhausted. Similar to the return value of getopt_long() except that BeagleRT
andrewm@46 222 * standard options are handled internally and not returned.
andrewm@46 223 */
andrewm@46 224 int BeagleRT_getopt_long(int argc, char *argv[], const char *customShortOptions,
andrewm@46 225 const struct option *customLongOptions, BeagleRTInitSettings *settings);
andrewm@46 226
andrewm@46 227 /**
andrewm@46 228 * \brief Print usage information for BeagleRT standard options.
andrewm@46 229 *
andrewm@46 230 * This function should be called from your code wherever you wish to print usage information for the
andrewm@46 231 * user. It will print usage information on BeagleRT standard options, after which you can print usage
andrewm@46 232 * information for your own custom options.
andrewm@46 233 */
andrewm@46 234 void BeagleRT_usage();
andrewm@46 235
andrewm@46 236 /**
andrewm@46 237 * \brief Set level of verbose (debugging) printing.
andrewm@46 238 *
andrewm@46 239 * \param level Verbosity level of the internal BeagleRT system. 0 by default; higher values will
andrewm@46 240 * print more information. Presently all positive numbers produce the same level of printing.
andrewm@46 241 */
andrewm@46 242 void BeagleRT_setVerboseLevel(int level);
andrewm@46 243
andrewm@46 244 // *** Audio control functions ***
andrewm@46 245
andrewm@46 246 /**
andrewm@46 247 * \brief Initialise audio and sensor rendering environment.
andrewm@46 248 *
andrewm@46 249 * This function prepares audio rendering in BeagleRT. It should be called from main() sometime
andrewm@46 250 * after command line option parsing has finished. It will initialise the rendering system, which
andrewm@46 251 * in the process will result in a call to the user-defined initialise_render() function.
andrewm@46 252 *
andrewm@46 253 * \param settings Data structure holding system settings, including numbers of channels, frame sizes,
andrewm@46 254 * volume levels and other information.
andrewm@46 255 * \param userData An opaque pointer to a user-defined data structure which will be passed to
andrewm@46 256 * initialise_render(), render() and cleanup_render(). You can use this to pass custom information
andrewm@46 257 * to the rendering functions, as an alternative to using global variables.
andrewm@46 258 *
andrewm@46 259 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 260 */
andrewm@46 261 int BeagleRT_initAudio(BeagleRTInitSettings *settings, void *userData);
andrewm@46 262
andrewm@46 263 /**
andrewm@46 264 * \brief Begin processing audio and sensor data.
andrewm@46 265 *
andrewm@46 266 * This function will start the BeagleRT audio/sensor system. After this function is called, the
andrewm@46 267 * system will make periodic calls to render() until BeagleRT_stopAudio() is called.
andrewm@46 268 *
andrewm@46 269 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 270 */
andrewm@46 271 int BeagleRT_startAudio();
andrewm@46 272
andrewm@46 273 /**
andrewm@46 274 * \brief Stop processing audio and sensor data.
andrewm@46 275 *
andrewm@46 276 * This function will stop the BeagleRT audio/sensor system. After this function returns, no further
andrewm@46 277 * calls to render() will be issued.
andrewm@46 278 */
andrewm@46 279 void BeagleRT_stopAudio();
andrewm@46 280
andrewm@46 281 /**
andrewm@46 282 * \brief Clean up resources from audio and sensor processing.
andrewm@46 283 *
andrewm@46 284 * This function should only be called after BeagleRT_stopAudio(). It will release any
andrewm@46 285 * internal resources for audio and sensor processing. In the process, it will call the
andrewm@46 286 * user-defined cleanup_render() function.
andrewm@46 287 */
andrewm@46 288 void BeagleRT_cleanupAudio();
andrewm@46 289
andrewm@46 290 // Volume/level controls
andrewm@46 291 // These return 0 on success
andrewm@46 292
andrewm@46 293 // *** Volume and level controls ***
andrewm@46 294
andrewm@46 295 /**
andrewm@46 296 * \brief Set the level of the audio DAC.
andrewm@46 297 *
andrewm@46 298 * This function sets the level of all audio outputs (headphone, line, speaker). It does
andrewm@46 299 * not affect the level of the (non-audio) analog outputs.
andrewm@46 300 *
andrewm@46 301 * \b Important: do not call this function from within render(), as it does not make
andrewm@46 302 * any guarantees on real-time performance.
andrewm@46 303 *
andrewm@46 304 * \param decibels Level of the DAC output. Valid levels range from -63.5 (lowest) to
andrewm@46 305 * 0 (highest) in steps of 0.5dB. Levels between increments of 0.5 will be rounded down.
andrewm@46 306 *
andrewm@46 307 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 308 */
andrewm@46 309 int BeagleRT_setDACLevel(float decibels);
andrewm@46 310
andrewm@46 311 /**
andrewm@46 312 * \brief Set the level of the audio ADC.
andrewm@46 313 *
andrewm@46 314 * This function sets the level of the audio input. It does not affect the level of the
andrewm@46 315 * (non-audio) analog inputs.
andrewm@46 316 *
andrewm@46 317 * \b Important: do not call this function from within render(), as it does not make
andrewm@46 318 * any guarantees on real-time performance.
andrewm@46 319 *
andrewm@46 320 * \param decibels Level of the ADC input. Valid levels range from -12 (lowest) to
andrewm@46 321 * 0 (highest) in steps of 1.5dB. Levels between increments of 1.5 will be rounded down.
andrewm@46 322 *
andrewm@46 323 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 324 */
andrewm@46 325 int BeagleRT_setADCLevel(float decibels);
andrewm@46 326
andrewm@46 327 /**
andrewm@46 328 * \brief Set the level of the onboard headphone amplifier.
andrewm@46 329 *
andrewm@46 330 * This function sets the level of the headphone output only (3-pin connector on the BeagleRT
andrewm@46 331 * cape or the output jack on the BeagleBone Audio Cape). It does not affect the level of the
andrewm@46 332 * speakers or the line out pads on the cape.
andrewm@46 333 *
andrewm@46 334 * \b Important: do not call this function from within render(), as it does not make
andrewm@46 335 * any guarantees on real-time performance.
andrewm@46 336 *
andrewm@46 337 * \param decibels Level of the DAC output. Valid levels range from -63.5 (lowest) to
andrewm@46 338 * 0 (highest) in steps of 0.5dB. Levels between increments of 0.5 will be rounded down.
andrewm@46 339 *
andrewm@46 340 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 341 */
andrewm@46 342 int BeagleRT_setHeadphoneLevel(float decibels);
andrewm@46 343
andrewm@46 344 /**
andrewm@46 345 * \brief Mute or unmute the onboard speaker amplifiers.
andrewm@46 346 *
andrewm@46 347 * This function mutes or unmutes the amplifiers on the BeagleRT cape. Whether the speakers begin
andrewm@46 348 * muted or unmuted depends on the BeagleRTInitSettings structure passed to BeagleRT_initAudio().
andrewm@46 349 *
andrewm@46 350 * \b Important: do not call this function from within render(), as it does not make
andrewm@46 351 * any guarantees on real-time performance.
andrewm@46 352 *
andrewm@46 353 * \param mute 0 to enable the speakers, nonzero to mute the speakers.
andrewm@46 354 *
andrewm@46 355 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 356 */
andrewm@46 357 int BeagleRT_muteSpeakers(int mute);
andrewm@46 358
andrewm@46 359 // *** Functions for creating auxiliary tasks ***
andrewm@46 360
andrewm@46 361 /**
andrewm@46 362 * \brief Create a new auxiliary task.
andrewm@46 363 *
andrewm@46 364 * This function creates a new auxiliary task which, when scheduled, runs the function specified
andrewm@46 365 * in the first argument. Note that the task does not run until scheduleAuxiliaryTask() is called.
andrewm@46 366 * Auxiliary tasks should be created in initialise_render() and never in render() itself.
andrewm@46 367 *
andrewm@46 368 * The second argument specifies the real-time priority. Valid values are between 0
andrewm@46 369 * and 99, and usually should be lower than BEAGLERT_AUDIO_PRIORITY. Tasks with higher priority always
andrewm@46 370 * preempt tasks with lower priority.
andrewm@46 371 *
andrewm@46 372 * \param functionToCall Function which will run each time the auxiliary task is scheduled.
andrewm@46 373 * \param priority Xenomai priority level at which the task should run.
andrewm@46 374 * \param Name for this task, which should be unique system-wide (no other running program should use this name).
andrewm@46 375 */
andrewm@46 376 AuxiliaryTask createAuxiliaryTaskLoop(void (*functionToCall)(void), int priority, const char *name);
andrewm@46 377
andrewm@46 378 /**
andrewm@46 379 * \brief Run an auxiliary task which has previously been created.
andrewm@46 380 *
andrewm@46 381 * This function will schedule an auxiliary task to run. When the task runs, the function in the first
andrewm@46 382 * argument of createAuxiliaryTaskLoop() will be called.
andrewm@46 383 *
andrewm@46 384 * scheduleAuxiliaryTask() is typically called from render() to start a lower-priority task. The function
andrewm@46 385 * will not run immediately, but only once any active higher priority tasks have finished.
andrewm@46 386 *
andrewm@46 387 * \param task Task to schedule for running.
andrewm@46 388 */
andrewm@46 389 void scheduleAuxiliaryTask(AuxiliaryTask task);
andrewm@46 390
andrewm@46 391 #endif /* BEAGLERT_H_ */