annotate include/Bela.h @ 556:ce391098f321 prerelease tip

THIS PROJECT HAS MOVED TO https://github.com/BelaPlatform/bela
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 25 Jun 2016 20:21:00 +0100
parents bfcbeb437869
children
rev   line source
andrewm@47 1 /**
andrewm@47 2 * @file
giuliomoro@301 3 * @brief Main Bela public API
andrewm@46 4 *
andrewm@46 5 * Central control code for hard real-time audio on BeagleBone Black
andrewm@46 6 * using PRU and Xenomai Linux extensions. This code began as part
andrewm@46 7 * of the Hackable Instruments project (EPSRC) at Queen Mary University
andrewm@46 8 * of London, 2013-14.
andrewm@46 9 *
andrewm@47 10 * (c) 2014-15 Andrew McPherson, Victor Zappi and Giulio Moro,
andrewm@46 11 * Queen Mary University of London
andrewm@46 12 */
andrewm@46 13
andrewm@48 14 /**
andrewm@48 15 * \mainpage
andrewm@48 16 *
giuliomoro@556 17 *
giuliomoro@556 18 * =========== WARNING==============
giuliomoro@556 19 * =================================
giuliomoro@556 20 * This project has now moved to
giuliomoro@556 21 * =================================
giuliomoro@556 22 * https://github.com/BelaPlatform/bela
giuliomoro@556 23 * =================================
giuliomoro@556 24 * _________________________________
giuliomoro@556 25 * =================================
giuliomoro@556 26 *
giuliomoro@556 27 *
giuliomoro@301 28 * Bela is a hard-real-time, ultra-low latency audio and sensor environment for
giuliomoro@301 29 * BeagleBone Black, which works with the BeagleBone Audio Cape or a custom "Bela Cape"
andrewm@48 30 * which incorporates stereo audio with 8x, 16-bit analog inputs and outputs.
andrewm@48 31 *
giuliomoro@301 32 * Bela is based on the Xenomai real-time Linux extensions (http://xenomai.org) and
andrewm@48 33 * uses the BeagleBone %PRU subsystem to address the audio and sensor hardware.
andrewm@48 34 *
andrewm@303 35 * Further information can be found at http://bela.io
andrewm@48 36 */
andrewm@48 37
andrewm@46 38
andrewm@303 39 #ifndef BELA_H_
andrewm@303 40 #define BELA_H_
andrewm@46 41
andrewm@46 42 #include <stdint.h>
giuliomoro@173 43 #include <unistd.h>
andrewm@55 44 #include <rtdk.h>
andrewm@46 45 #include "digital_gpio_mapping.h"
giuliomoro@237 46 #include <GPIOcontrol.h>
andrewm@46 47
andrewm@46 48 // Useful constants
andrewm@47 49
andrewm@47 50 /** \cond PRIVATE */
andrewm@46 51 #define CODEC_I2C_ADDRESS 0x18 // Address of TLV320AIC3104 codec
andrewm@46 52
andrewm@46 53 #define MAX_PRU_FILENAME_LENGTH 256
andrewm@46 54 #define MAX_SERVERNAME_LENGTH 256
andrewm@47 55 /** \endcond */
andrewm@46 56
andrewm@47 57 /**
andrewm@47 58 * \ingroup auxtask
andrewm@47 59 *
andrewm@47 60 * Xenomai priority level for audio processing. Maximum possible priority is 99.
andrewm@47 61 * In general, all auxiliary tasks should have a level lower than this unless for\
andrewm@47 62 * special purposes where the task needs to interrupt audio processing.
andrewm@47 63 */
andrewm@303 64 #define BELA_AUDIO_PRIORITY 95
andrewm@46 65
andrewm@47 66 // Default volume levels
andrewm@47 67
andrewm@47 68 /**
andrewm@47 69 * \addtogroup levels
andrewm@47 70 *
andrewm@47 71 * @{
andrewm@47 72 */
andrewm@47 73
andrewm@47 74 /**
giuliomoro@301 75 * Default level of the audio DAC in decibels. See Bela_setDACLevel().
andrewm@47 76 */
andrewm@47 77 #define DEFAULT_DAC_LEVEL 0.0
andrewm@47 78
andrewm@47 79 /**
giuliomoro@301 80 * Default level of the audio ADC in decibels. See Bela_setADCLevel().
andrewm@47 81 */
andrewm@47 82 #define DEFAULT_ADC_LEVEL -6.0
andrewm@47 83
giuliomoro@171 84
giuliomoro@171 85 /**
giuliomoro@171 86 * Default level of the Programmable Gain Amplifier in decibels.
giuliomoro@171 87 */
giuliomoro@171 88 #define DEFAULT_PGA_GAIN 16
giuliomoro@171 89
andrewm@47 90 /**
giuliomoro@301 91 * Default level of the headphone output in decibels. See Bela_setHeadphoneLevel().
andrewm@47 92 */
andrewm@47 93 #define DEFAULT_HP_LEVEL -6.0
andrewm@47 94 /** @} */
andrewm@47 95
andrewm@47 96 /**
giuliomoro@301 97 * Flag for BelaContext. If set, indicates the audio and analog buffers are interleaved.
andrewm@47 98 */
andrewm@303 99 #define BELA_FLAG_INTERLEAVED (1 << 0) // Set if buffers are interleaved
andrewm@47 100 /**
giuliomoro@301 101 * Flag for BelaContext. If set, indicates analog outputs persist for future frames.
andrewm@47 102 */
andrewm@303 103 #define BELA_FLAG_ANALOG_OUTPUTS_PERSIST (1 << 1) // Set if analog/digital outputs persist for future buffers
andrewm@46 104
andrewm@47 105 /**
andrewm@47 106 * \ingroup control
andrewm@47 107 * \brief Structure containing initialisation parameters for the real-time
andrewm@47 108 * audio control system.
andrewm@47 109 *
giuliomoro@301 110 * This structure is initialised using Bela_defaultSettings(). Its contents
andrewm@47 111 * are used up through the point of calling
giuliomoro@301 112 * Bela_initAudio() at which point it is no longer needed.
andrewm@47 113 */
andrewm@46 114 typedef struct {
andrewm@46 115 // These items might be adjusted by the user:
andrewm@46 116
andrewm@47 117 /// \brief Number of (analog) frames per period.
andrewm@47 118 ///
andrewm@47 119 /// Number of audio frames depends on relative sample rates of the two. By default,
andrewm@47 120 /// audio is twice the sample rate, so has twice the period size.
andrewm@47 121 int periodSize;
andrewm@47 122 /// Whether to use the analog input and output
andrewm@47 123 int useAnalog;
andrewm@47 124 /// Whether to use the 16 programmable GPIOs
andrewm@47 125 int useDigital;
giuliomoro@537 126 /// How many audio input channels
giuliomoro@537 127 int numAudioInChannels;
giuliomoro@537 128 /// How many audio out channels
giuliomoro@537 129 int numAudioOutChannels;
giuliomoro@537 130 /// How many analog input channels
giuliomoro@537 131 int numAnalogInChannels;
giuliomoro@537 132 /// How many analog output channels
giuliomoro@537 133 int numAnalogOutChannels;
andrewm@47 134 /// How many channels for the GPIOs
andrewm@47 135 int numDigitalChannels;
andrewm@46 136
andrewm@47 137 /// Whether to begin with the speakers muted
andrewm@47 138 int beginMuted;
andrewm@47 139 /// Level for the audio DAC output
andrewm@47 140 float dacLevel;
andrewm@47 141 /// Level for the audio ADC input
andrewm@47 142 float adcLevel;
giuliomoro@171 143 /// Gains for the PGA, left and right channels
giuliomoro@171 144 float pgaGain[2];
andrewm@47 145 /// Level for the headphone output
andrewm@47 146 float headphoneLevel;
andrewm@280 147 /// How many channels to use on the multiplexer capelet, if enabled
andrewm@280 148 int numMuxChannels;
andrewm@47 149
andrewm@280 150 /// Which PRU (0 or 1) the code should run on
andrewm@280 151 int pruNumber;
andrewm@47 152 /// The external .bin file to load. If empty will use PRU code from pru_rtaudio_bin.h
andrewm@47 153 char pruFilename[MAX_PRU_FILENAME_LENGTH];
andrewm@47 154 /// Whether to use verbose logging
andrewm@47 155 int verbose;
andrewm@46 156
andrewm@46 157 // These items are application-dependent but should probably be
andrewm@46 158 // determined by the programmer rather than the user
andrewm@47 159
andrewm@47 160 /// Whether audio/analog data should be interleaved
andrewm@47 161 int interleave;
andrewm@47 162 /// \brief Whether analog outputs should persist to future frames.
andrewm@47 163 ///
andrewm@47 164 /// n.b. digital pins always persist, audio never does
andrewm@47 165 int analogOutputsPersist;
andrewm@46 166
andrewm@46 167 // These items are hardware-dependent and should only be changed
andrewm@46 168 // to run on different hardware
andrewm@47 169
andrewm@47 170 /// Where the codec can be found on the I2C bus
andrewm@47 171 int codecI2CAddress;
andrewm@47 172 /// Pin where amplifier mute can be found
andrewm@47 173 int ampMutePin;
andrewm@47 174 /// Port where the UDP server will listen
andrewm@47 175 int receivePort;
andrewm@47 176 /// Port where the UDP client will transmit
andrewm@47 177 int transmitPort;
andrewm@46 178 char serverName[MAX_SERVERNAME_LENGTH];
giuliomoro@301 179 } BelaInitSettings;
andrewm@46 180
andrewm@46 181
andrewm@47 182 /**
andrewm@47 183 * \ingroup render
andrewm@47 184 * \brief Structure holding current audio and sensor settings and pointers to data buffers.
andrewm@47 185 *
andrewm@56 186 * This structure is passed to setup(), render() and cleanup(). It is
giuliomoro@301 187 * initialised in Bela_initAudio() based on the contents of the BelaInitSettings
andrewm@47 188 * structure.
andrewm@47 189 */
andrewm@46 190 typedef struct {
andrewm@47 191 /// \brief Buffer holding audio input samples
andrewm@47 192 ///
andrewm@47 193 /// This buffer may be in either interleaved or non-interleaved format,
giuliomoro@301 194 /// depending on the contents of the BelaInitSettings structure.
andrewm@47 195 /// \b Note: this element is available in render() only.
andrewm@307 196 const float * const audioIn;
andrewm@47 197
andrewm@47 198 /// \brief Buffer holding audio output samples
andrewm@47 199 ///
andrewm@47 200 /// This buffer may be in either interleaved or non-interleaved format,
giuliomoro@301 201 /// depending on the contents of the BelaInitSettings structure.
andrewm@47 202 /// \b Note: this element is available in render() only.
andrewm@307 203 float * const audioOut;
andrewm@47 204
andrewm@47 205 /// \brief Buffer holding analog input samples
andrewm@47 206 ///
andrewm@47 207 /// This buffer may be in either interleaved or non-interleaved format,
giuliomoro@301 208 /// depending on the contents of the BelaInitSettings structure.
andrewm@47 209 /// \b Note: this element is available in render() only.
andrewm@307 210 const float * const analogIn;
andrewm@47 211
andrewm@47 212 /// \brief Buffer holding analog output samples
andrewm@47 213 ///
andrewm@47 214 /// This buffer may be in either interleaved or non-interleaved format,
giuliomoro@301 215 /// depending on the contents of the BelaInitSettings structure.
andrewm@47 216 /// \b Note: this element is available in render() only.
andrewm@307 217 float * const analogOut;
andrewm@47 218
andrewm@47 219 /// \brief Buffer holding digital input/output samples
andrewm@47 220 ///
andrewm@47 221 /// \b Note: this element is available in render() only.
andrewm@307 222 uint32_t * const digital;
andrewm@46 223
andrewm@47 224 /// Number of audio frames per period
andrewm@307 225 const uint32_t audioFrames;
giuliomoro@528 226 /// Number of input audio channels
giuliomoro@528 227 const uint32_t audioInChannels;
giuliomoro@528 228 /// Number of output audio channels
giuliomoro@528 229 const uint32_t audioOutChannels;
andrewm@47 230 /// Audio sample rate in Hz (currently always 44100.0)
andrewm@307 231 const float audioSampleRate;
andrewm@46 232
andrewm@47 233 /// \brief Number of analog frames per period
andrewm@47 234 ///
andrewm@47 235 /// This will be 0 if analog I/O is disabled.
andrewm@307 236 const uint32_t analogFrames;
andrewm@47 237
giuliomoro@528 238 /// \brief Number of input analog channels
andrewm@47 239 ///
giuliomoro@528 240 /// This will be 0 if analog I/O is disabled.
giuliomoro@528 241 const uint32_t analogInChannels;
giuliomoro@528 242
giuliomoro@528 243 /// \brief Number of output analog channels
giuliomoro@528 244 ///
giuliomoro@528 245 /// This will be 0 if analog I/O is disabled.
giuliomoro@528 246 const uint32_t analogOutChannels;
andrewm@47 247
andrewm@47 248 /// \brief Analog sample rate in Hz
andrewm@47 249 ///
andrewm@47 250 /// The analog sample rate depends on the number of analog channels used. If
andrewm@47 251 /// 8 channels are used, the sample rate is 22050. If 4 channels are used, the sample
andrewm@47 252 /// rate is 44100. If 2 channels are used, the sample rate is 88200. If analog I/O
andrewm@47 253 /// is disabled, the sample rate is 0.
andrewm@307 254 const float analogSampleRate;
andrewm@46 255
andrewm@47 256 /// Number of digital frames per period
andrewm@307 257 const uint32_t digitalFrames;
andrewm@47 258 /// \brief Number of digital channels
andrewm@47 259 ///
andrewm@47 260 /// Currently this will always be 16, unless digital I/O is disabled, in which case it will be 0.
andrewm@307 261 const uint32_t digitalChannels;
andrewm@47 262 /// Digital sample rate in Hz (currently always 44100.0)
andrewm@307 263 const float digitalSampleRate;
andrewm@46 264
andrewm@311 265 /// \brief Number of elapsed audio frames since the start of rendering.
andrewm@47 266 ///
andrewm@311 267 /// This holds the total number of audio frames as of the beginning of the current period. To
andrewm@311 268 /// find the current number of analog or digital frames elapsed, multiply by the ratio of the
andrewm@311 269 /// sample rates (e.g. half the number of analog frames will have elapsed if the analog sample
andrewm@47 270 /// rate is 22050).
andrewm@311 271 const uint64_t audioFramesElapsed;
andrewm@47 272
andrewm@47 273 /// \brief Other audio/sensor settings
andrewm@47 274 ///
andrewm@47 275 /// Binary combination of flags including:
andrewm@47 276 ///
andrewm@303 277 /// BELA_FLAG_INTERLEAVED: indicates the audio and analog buffers are interleaved
andrewm@47 278 ///
andrewm@303 279 /// BELA_FLAG_ANALOG_OUTPUTS_PERSIST: indicates that writes to the analog outputs will
andrewm@47 280 /// persist for future frames. If not set, writes affect one frame only.
andrewm@307 281 const uint32_t flags;
giuliomoro@301 282 } BelaContext;
andrewm@46 283
andrewm@47 284 /** \ingroup auxtask
andrewm@47 285 *
andrewm@47 286 * Auxiliary task variable. Auxiliary tasks are created using createAuxiliaryTask() and
andrewm@56 287 * automatically cleaned up after cleanup() finishes.
andrewm@47 288 */
andrewm@46 289 typedef void* AuxiliaryTask; // Opaque data type to keep track of aux tasks
andrewm@46 290
andrewm@47 291 /** \ingroup render
andrewm@47 292 *
andrewm@47 293 * Flag that indicates when the audio will stop. Threads can poll this variable to indicate when
andrewm@47 294 * they should stop. Additionally, a program can set this to \c true
giuliomoro@301 295 * to indicate that audio processing should terminate. Calling Bela_stopAudio()
andrewm@47 296 * has the effect of setting this to \c true.
andrewm@47 297 */
giuliomoro@233 298 extern int gShouldStop;
andrewm@46 299
andrewm@46 300 // *** User-defined render functions ***
andrewm@46 301
andrewm@46 302 /**
andrewm@47 303 * \defgroup render User-defined render functions
andrewm@47 304 *
giuliomoro@301 305 * These three functions must be implemented by the developer in every Bela program.
andrewm@47 306 * Typically they appear in their own .cpp source file.
andrewm@47 307 *
andrewm@47 308 * @{
andrewm@47 309 */
andrewm@47 310
andrewm@47 311 /**
andrewm@46 312 * \brief User-defined initialisation function which runs before audio rendering begins.
andrewm@46 313 *
andrewm@46 314 * This function runs once at the beginning of the program, after most of the system
andrewm@46 315 * initialisation has begun but before audio rendering starts. Use it to prepare any
andrewm@46 316 * memory or resources that will be needed in render().
andrewm@46 317 *
andrewm@46 318 * \param context Data structure holding information on sample rates, numbers of channels,
andrewm@46 319 * frame sizes and other state. Note: the buffers for audio, analog and digital data will
andrewm@46 320 * \b not yet be available to use. Do not attempt to read or write audio or sensor data
andrewm@56 321 * in setup().
andrewm@46 322 * \param userData An opaque pointer to an optional user-defined data structure. Whatever
giuliomoro@301 323 * is passed as the second argument to Bela_initAudio() will appear here.
andrewm@46 324 *
andrewm@46 325 * \return true on success, or false if an error occurred. If no initialisation is
andrewm@56 326 * required, setup() should return true.
andrewm@46 327 */
giuliomoro@301 328 bool setup(BelaContext *context, void *userData);
andrewm@46 329
andrewm@46 330 /**
andrewm@46 331 * \brief User-defined callback function to process audio and sensor data.
andrewm@46 332 *
andrewm@46 333 * This function is called regularly by the system every time there is a new block of
andrewm@46 334 * audio and/or sensor data to process. Your code should process the requested samples
andrewm@46 335 * of data, store the results within \c context, and return.
andrewm@46 336 *
andrewm@46 337 * \param context Data structure holding buffers for audio, analog and digital data. The
andrewm@46 338 * structure also holds information on numbers of channels, frame sizes and sample rates,
andrewm@46 339 * which are guaranteed to remain the same throughout the program and to match what was
andrewm@56 340 * passed to setup().
andrewm@46 341 * \param userData An opaque pointer to an optional user-defined data structure. Will
andrewm@56 342 * be the same as the \c userData parameter passed to setup().
andrewm@46 343 */
giuliomoro@301 344 void render(BelaContext *context, void *userData);
andrewm@46 345
andrewm@46 346 /**
andrewm@46 347 * \brief User-defined cleanup function which runs when the program finishes.
andrewm@46 348 *
andrewm@46 349 * This function is called by the system once after audio rendering has finished, before the
andrewm@56 350 * program quits. Use it to release any memory allocated in setup() and to perform
andrewm@56 351 * any other required cleanup. If no initialisation is performed in setup(), then
andrewm@46 352 * this function will usually be empty.
andrewm@46 353 *
andrewm@46 354 * \param context Data structure holding information on sample rates, numbers of channels,
andrewm@46 355 * frame sizes and other state. Note: the buffers for audio, analog and digital data will
andrewm@46 356 * no longer be available to use. Do not attempt to read or write audio or sensor data
andrewm@56 357 * in cleanup().
andrewm@46 358 * \param userData An opaque pointer to an optional user-defined data structure. Will
andrewm@56 359 * be the same as the \c userData parameter passed to setup() and render().
andrewm@46 360 */
giuliomoro@301 361 void cleanup(BelaContext *context, void *userData);
andrewm@46 362
andrewm@47 363 /** @} */
andrewm@47 364
andrewm@47 365 /**
andrewm@47 366 * \defgroup control Control and command line functions
andrewm@47 367 *
giuliomoro@301 368 * These functions are used to initialise the Bela settings, process arguments
andrewm@47 369 * from the command line, and start/stop the audio and sensor system.
andrewm@47 370 *
andrewm@47 371 * @{
andrewm@47 372 */
andrewm@47 373
andrewm@46 374 // *** Command-line settings ***
andrewm@46 375
andrewm@46 376 /**
giuliomoro@301 377 * \brief Initialise the data structure containing settings for Bela.
andrewm@46 378 *
andrewm@46 379 * This function should be called in main() before parsing any command-line arguments. It
giuliomoro@301 380 * sets default values in the data structure which specifies the Bela settings, including
andrewm@46 381 * frame sizes, numbers of channels, volume levels and other parameters.
andrewm@46 382 *
giuliomoro@301 383 * \param settings Structure holding initialisation data for Bela.
andrewm@46 384 */
giuliomoro@301 385 void Bela_defaultSettings(BelaInitSettings *settings);
andrewm@46 386
andrewm@46 387 /**
giuliomoro@301 388 * \brief Get long options from command line argument list, including Bela standard options
andrewm@46 389 *
andrewm@46 390 * This function should be used in main() to process command line options, in place of the
giuliomoro@301 391 * standard library getopt_long(). Internally, it parses standard Bela command-line options,
andrewm@46 392 * storing the results in the settings data structure. Any options which are not part of the
giuliomoro@301 393 * Bela standard options will be returned, as they would normally be in getopt_long().
andrewm@46 394 *
andrewm@46 395 * \param argc Number of command line options, as passed to main().
andrewm@46 396 * \param argv Array of command line options, as passed to main().
andrewm@46 397 * \param customShortOptions List of short options to be parsed, analogous to getopt_long(). This
giuliomoro@301 398 * list should not include any characters already parsed as part of the Bela standard options.
andrewm@46 399 * \param customLongOptions List of long options to parsed, analogous to getopt_long(). This
giuliomoro@301 400 * list should not include any long options already parsed as part of the Bela standard options.
giuliomoro@301 401 * \param settings Data structure holding initialisation settings for Bela. Any standard options
andrewm@46 402 * parsed will automatically update this data structure.
andrewm@46 403 *
giuliomoro@301 404 * \return Value of the next option parsed which is not a Bela standard option, or -1 when the
giuliomoro@301 405 * argument list has been exhausted. Similar to the return value of getopt_long() except that Bela
andrewm@46 406 * standard options are handled internally and not returned.
andrewm@46 407 */
giuliomoro@301 408 int Bela_getopt_long(int argc, char *argv[], const char *customShortOptions,
giuliomoro@301 409 const struct option *customLongOptions, BelaInitSettings *settings);
andrewm@46 410
andrewm@46 411 /**
giuliomoro@301 412 * \brief Print usage information for Bela standard options.
andrewm@46 413 *
andrewm@46 414 * This function should be called from your code wherever you wish to print usage information for the
giuliomoro@301 415 * user. It will print usage information on Bela standard options, after which you can print usage
andrewm@46 416 * information for your own custom options.
andrewm@46 417 */
giuliomoro@301 418 void Bela_usage();
andrewm@46 419
andrewm@46 420 /**
andrewm@46 421 * \brief Set level of verbose (debugging) printing.
andrewm@46 422 *
giuliomoro@301 423 * \param level Verbosity level of the internal Bela system. 0 by default; higher values will
andrewm@46 424 * print more information. Presently all positive numbers produce the same level of printing.
andrewm@46 425 */
giuliomoro@301 426 void Bela_setVerboseLevel(int level);
andrewm@46 427
andrewm@47 428
andrewm@46 429 // *** Audio control functions ***
andrewm@46 430
andrewm@46 431 /**
andrewm@46 432 * \brief Initialise audio and sensor rendering environment.
andrewm@46 433 *
giuliomoro@301 434 * This function prepares audio rendering in Bela. It should be called from main() sometime
andrewm@46 435 * after command line option parsing has finished. It will initialise the rendering system, which
andrewm@56 436 * in the process will result in a call to the user-defined setup() function.
andrewm@46 437 *
andrewm@46 438 * \param settings Data structure holding system settings, including numbers of channels, frame sizes,
andrewm@46 439 * volume levels and other information.
andrewm@46 440 * \param userData An opaque pointer to a user-defined data structure which will be passed to
andrewm@56 441 * setup(), render() and cleanup(). You can use this to pass custom information
andrewm@46 442 * to the rendering functions, as an alternative to using global variables.
andrewm@46 443 *
andrewm@46 444 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 445 */
giuliomoro@301 446 int Bela_initAudio(BelaInitSettings *settings, void *userData);
andrewm@46 447
andrewm@46 448 /**
andrewm@46 449 * \brief Begin processing audio and sensor data.
andrewm@46 450 *
giuliomoro@301 451 * This function will start the Bela audio/sensor system. After this function is called, the
giuliomoro@301 452 * system will make periodic calls to render() until Bela_stopAudio() is called.
andrewm@46 453 *
andrewm@46 454 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 455 */
giuliomoro@301 456 int Bela_startAudio();
andrewm@46 457
andrewm@46 458 /**
andrewm@46 459 * \brief Stop processing audio and sensor data.
andrewm@46 460 *
giuliomoro@301 461 * This function will stop the Bela audio/sensor system. After this function returns, no further
andrewm@46 462 * calls to render() will be issued.
andrewm@46 463 */
giuliomoro@301 464 int Bela_startAuxiliaryTask(AuxiliaryTask it);
giuliomoro@301 465 void Bela_stopAudio();
andrewm@46 466
andrewm@46 467 /**
andrewm@46 468 * \brief Clean up resources from audio and sensor processing.
andrewm@46 469 *
giuliomoro@301 470 * This function should only be called after Bela_stopAudio(). It will release any
andrewm@46 471 * internal resources for audio and sensor processing. In the process, it will call the
andrewm@56 472 * user-defined cleanup() function.
andrewm@46 473 */
giuliomoro@301 474 void Bela_cleanupAudio();
andrewm@46 475
andrewm@47 476 /** @} */
andrewm@47 477
andrewm@47 478 /**
andrewm@47 479 * \defgroup levels Audio level controls
andrewm@47 480 *
giuliomoro@301 481 * These functions control the input and output levels for the audio codec. If a Bela program
andrewm@47 482 * does not call these functions, sensible default levels will be used.
andrewm@47 483 *
andrewm@47 484 * @{
andrewm@47 485 */
andrewm@46 486
andrewm@46 487 // *** Volume and level controls ***
andrewm@46 488
andrewm@46 489 /**
andrewm@46 490 * \brief Set the level of the audio DAC.
andrewm@46 491 *
andrewm@46 492 * This function sets the level of all audio outputs (headphone, line, speaker). It does
andrewm@46 493 * not affect the level of the (non-audio) analog outputs.
andrewm@46 494 *
andrewm@46 495 * \b Important: do not call this function from within render(), as it does not make
andrewm@46 496 * any guarantees on real-time performance.
andrewm@46 497 *
andrewm@46 498 * \param decibels Level of the DAC output. Valid levels range from -63.5 (lowest) to
andrewm@46 499 * 0 (highest) in steps of 0.5dB. Levels between increments of 0.5 will be rounded down.
andrewm@46 500 *
andrewm@46 501 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 502 */
giuliomoro@301 503 int Bela_setDACLevel(float decibels);
andrewm@46 504
andrewm@46 505 /**
andrewm@46 506 * \brief Set the level of the audio ADC.
andrewm@46 507 *
andrewm@46 508 * This function sets the level of the audio input. It does not affect the level of the
andrewm@46 509 * (non-audio) analog inputs.
andrewm@46 510 *
andrewm@46 511 * \b Important: do not call this function from within render(), as it does not make
andrewm@46 512 * any guarantees on real-time performance.
andrewm@46 513 *
andrewm@46 514 * \param decibels Level of the ADC input. Valid levels range from -12 (lowest) to
andrewm@46 515 * 0 (highest) in steps of 1.5dB. Levels between increments of 1.5 will be rounded down.
andrewm@46 516 *
andrewm@46 517 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 518 */
giuliomoro@301 519 int Bela_setADCLevel(float decibels);
andrewm@46 520
giuliomoro@171 521
giuliomoro@171 522 /**
giuliomoro@171 523 * \brief Set the gain of the audio preamplifier.
giuliomoro@171 524 *
giuliomoro@171 525 * This function sets the level of the Programmable Gain Amplifier(PGA), which
giuliomoro@171 526 * amplifies the signal before the ADC.
giuliomoro@171 527 *
giuliomoro@171 528 * \b Important: do not call this function from within render(), as it does not make
giuliomoro@171 529 * any guarantees on real-time performance.
giuliomoro@171 530 *
giuliomoro@171 531 * \param decibels Level of the PGA Valid levels range from 0 (lowest) to
giuliomoro@171 532 * 59.5 (highest) in steps of 0.5dB. Levels between increments of 0.5 will be rounded.
giuliomoro@171 533 * \param channel Specifies which channel to apply the gain to. Channel 0 is left,
giuliomoro@171 534 * channel 1 is right
giuliomoro@171 535 *
giuliomoro@171 536 * \return 0 on success, or nonzero if an error occurred.
giuliomoro@171 537 */
giuliomoro@301 538 int Bela_setPgaGain(float decibels, int channel);
giuliomoro@171 539
andrewm@46 540 /**
andrewm@46 541 * \brief Set the level of the onboard headphone amplifier.
andrewm@46 542 *
giuliomoro@301 543 * This function sets the level of the headphone output only (3-pin connector on the Bela
andrewm@46 544 * cape or the output jack on the BeagleBone Audio Cape). It does not affect the level of the
andrewm@46 545 * speakers or the line out pads on the cape.
andrewm@46 546 *
andrewm@46 547 * \b Important: do not call this function from within render(), as it does not make
andrewm@46 548 * any guarantees on real-time performance.
andrewm@46 549 *
andrewm@46 550 * \param decibels Level of the DAC output. Valid levels range from -63.5 (lowest) to
andrewm@46 551 * 0 (highest) in steps of 0.5dB. Levels between increments of 0.5 will be rounded down.
andrewm@46 552 *
andrewm@46 553 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 554 */
giuliomoro@301 555 int Bela_setHeadphoneLevel(float decibels);
andrewm@46 556
andrewm@46 557 /**
andrewm@46 558 * \brief Mute or unmute the onboard speaker amplifiers.
andrewm@46 559 *
giuliomoro@301 560 * This function mutes or unmutes the amplifiers on the Bela cape. Whether the speakers begin
giuliomoro@301 561 * muted or unmuted depends on the BelaInitSettings structure passed to Bela_initAudio().
andrewm@46 562 *
andrewm@46 563 * \b Important: do not call this function from within render(), as it does not make
andrewm@46 564 * any guarantees on real-time performance.
andrewm@46 565 *
andrewm@46 566 * \param mute 0 to enable the speakers, nonzero to mute the speakers.
andrewm@46 567 *
andrewm@46 568 * \return 0 on success, or nonzero if an error occurred.
andrewm@46 569 */
giuliomoro@301 570 int Bela_muteSpeakers(int mute);
andrewm@46 571
andrewm@47 572 /** @} */
andrewm@47 573
andrewm@47 574 /**
andrewm@47 575 * \defgroup auxtask Auxiliary task support
andrewm@47 576 *
andrewm@47 577 * These functions are used to create separate real-time tasks (threads) which run at lower
andrewm@47 578 * priority than the audio processing. They can be used, for example, for large time-consuming
andrewm@47 579 * calculations which would take more than one audio frame length to process, or they could be
andrewm@47 580 * used to communicate with external hardware when that communication might block or be delayed.
andrewm@47 581 *
andrewm@56 582 * All auxiliary tasks used by the program should be created in setup(). The tasks
andrewm@47 583 * can then be scheduled at will within the render() function.
andrewm@47 584 *
andrewm@47 585 * @{
andrewm@47 586 */
andrewm@47 587
andrewm@46 588 // *** Functions for creating auxiliary tasks ***
andrewm@46 589
andrewm@46 590 /**
andrewm@46 591 * \brief Create a new auxiliary task.
andrewm@46 592 *
andrewm@46 593 * This function creates a new auxiliary task which, when scheduled, runs the function specified
andrewm@46 594 * in the first argument. Note that the task does not run until scheduleAuxiliaryTask() is called.
andrewm@56 595 * Auxiliary tasks should be created in setup() and never in render() itself.
andrewm@46 596 *
andrewm@46 597 * The second argument specifies the real-time priority. Valid values are between 0
andrewm@303 598 * and 99, and usually should be lower than \ref BELA_AUDIO_PRIORITY. Tasks with higher priority always
andrewm@46 599 * preempt tasks with lower priority.
andrewm@46 600 *
andrewm@46 601 * \param functionToCall Function which will run each time the auxiliary task is scheduled.
andrewm@46 602 * \param priority Xenomai priority level at which the task should run.
andrewm@47 603 * \param name Name for this task, which should be unique system-wide (no other running program should use this name).
andrewm@46 604 */
giuliomoro@301 605 AuxiliaryTask Bela_createAuxiliaryTask(void (*functionToCall)(void*), int priority, const char *name, void* args, bool autoSchedule = false);
giuliomoro@301 606 AuxiliaryTask Bela_createAuxiliaryTask(void (*functionToCall)(void), int priority, const char *name, bool autoSchedule = false);
andrewm@46 607
andrewm@46 608 /**
giuliomoro@174 609 * \brief Start an auxiliary task so that it can be run.
giuliomoro@174 610 *
giuliomoro@174 611 * This function will start an auxiliary task but will NOT schedule it.
giuliomoro@174 612 * It will also set a flag in the associate InternalAuxiliaryTask to flag the
giuliomoro@174 613 * task as "started", so that successive calls to the same function for a given AuxiliaryTask
giuliomoro@174 614 * have no effect.
giuliomoro@174 615 * The user should never be required to call this function directly, as it is called
giuliomoro@301 616 * by Bela_scheduleAuxiliaryTask if needed (e.g.: if a task is scheduled in setup() )
giuliomoro@174 617 * or immediately after starting the audio thread.
giuliomoro@174 618 *
giuliomoro@174 619 * \param task Task to start.
giuliomoro@174 620 */
giuliomoro@174 621
giuliomoro@301 622 int Bela_startAuxiliaryTask(AuxiliaryTask task);
giuliomoro@174 623 /**
andrewm@46 624 * \brief Run an auxiliary task which has previously been created.
andrewm@46 625 *
andrewm@46 626 * This function will schedule an auxiliary task to run. When the task runs, the function in the first
andrewm@46 627 * argument of createAuxiliaryTaskLoop() will be called.
andrewm@46 628 *
andrewm@46 629 * scheduleAuxiliaryTask() is typically called from render() to start a lower-priority task. The function
andrewm@46 630 * will not run immediately, but only once any active higher priority tasks have finished.
andrewm@46 631 *
andrewm@46 632 * \param task Task to schedule for running.
andrewm@46 633 */
giuliomoro@301 634 void Bela_scheduleAuxiliaryTask(AuxiliaryTask task);
giuliomoro@301 635 void Bela_autoScheduleAuxiliaryTasks();
andrewm@47 636
andrewm@47 637 /** @} */
giuliomoro@187 638 #include <Utilities.h>
andrewm@46 639
andrewm@303 640 #endif /* BELA_H_ */