comparison include/BeagleRT.h @ 47:643cbee74eda newapi

First draft of Doxygen documentation
author andrewm
date Thu, 28 May 2015 17:06:03 -0400
parents eac5d8f40b48
children 42a683058b6a
comparison
equal deleted inserted replaced
46:eac5d8f40b48 47:643cbee74eda
1 /* 1 /**
2 * RTAudio.h 2 * @file
3 * @brief Main BeagleRT public API
3 * 4 *
4 * Central control code for hard real-time audio on BeagleBone Black 5 * Central control code for hard real-time audio on BeagleBone Black
5 * using PRU and Xenomai Linux extensions. This code began as part 6 * using PRU and Xenomai Linux extensions. This code began as part
6 * of the Hackable Instruments project (EPSRC) at Queen Mary University 7 * of the Hackable Instruments project (EPSRC) at Queen Mary University
7 * of London, 2013-14. 8 * of London, 2013-14.
8 * 9 *
9 * (c) 2014 Victor Zappi and Andrew McPherson 10 * (c) 2014-15 Andrew McPherson, Victor Zappi and Giulio Moro,
10 * Queen Mary University of London 11 * Queen Mary University of London
11 */ 12 */
12 13
13 14
14 #ifndef BEAGLERT_H_ 15 #ifndef BEAGLERT_H_
16 17
17 #include <stdint.h> 18 #include <stdint.h>
18 #include "digital_gpio_mapping.h" 19 #include "digital_gpio_mapping.h"
19 20
20 // Useful constants 21 // Useful constants
22
23 /** \cond PRIVATE */
21 #define DBOX_CAPE // New custom cape 24 #define DBOX_CAPE // New custom cape
22
23 #ifdef DBOX_CAPE 25 #ifdef DBOX_CAPE
24 #define CODEC_I2C_ADDRESS 0x18 // Address of TLV320AIC3104 codec 26 #define CODEC_I2C_ADDRESS 0x18 // Address of TLV320AIC3104 codec
25 #else 27 #else
26 #define CODEC_I2C_ADDRESS 0x1B // Address of TLV320AIC3106 codec 28 #define CODEC_I2C_ADDRESS 0x1B // Address of TLV320AIC3106 codec
27 #endif 29 #endif
28 30
29 // Default volume levels
30 #define DEFAULT_DAC_LEVEL 0.0
31 #define DEFAULT_ADC_LEVEL -6.0
32 #define DEFAULT_HP_LEVEL -6.0
33
34 #define MAX_PRU_FILENAME_LENGTH 256 31 #define MAX_PRU_FILENAME_LENGTH 256
35 #define MAX_SERVERNAME_LENGTH 256 32 #define MAX_SERVERNAME_LENGTH 256
36 33 /** \endcond */
37 // Priority at which BeagleRT audio code runs 34
38 // Higher numbers preempt the audio; lower numbers are preempted by it 35 /**
36 * \ingroup auxtask
37 *
38 * Xenomai priority level for audio processing. Maximum possible priority is 99.
39 * In general, all auxiliary tasks should have a level lower than this unless for\
40 * special purposes where the task needs to interrupt audio processing.
41 */
39 #define BEAGLERT_AUDIO_PRIORITY 95 42 #define BEAGLERT_AUDIO_PRIORITY 95
40 43
41 // Flags for BeagleRTContext data structure 44 // Default volume levels
45
46 /**
47 * \addtogroup levels
48 *
49 * @{
50 */
51
52 /**
53 * Default level of the audio DAC in decibels. See BeagleRT_setDACLevel().
54 */
55 #define DEFAULT_DAC_LEVEL 0.0
56
57 /**
58 * Default level of the audio ADC in decibels. See BeagleRT_setADCLevel().
59 */
60 #define DEFAULT_ADC_LEVEL -6.0
61
62 /**
63 * Default level of the headphone output in decibels. See BeagleRT_setHeadphoneLevel().
64 */
65 #define DEFAULT_HP_LEVEL -6.0
66 /** @} */
67
68 /**
69 * Flag for BeagleRTContext. If set, indicates the audio and analog buffers are interleaved.
70 */
42 #define BEAGLERT_FLAG_INTERLEAVED (1 << 0) // Set if buffers are interleaved 71 #define BEAGLERT_FLAG_INTERLEAVED (1 << 0) // Set if buffers are interleaved
72 /**
73 * Flag for BeagleRTContext. If set, indicates analog outputs persist for future frames.
74 */
43 #define BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST (1 << 1) // Set if analog/digital outputs persist for future buffers 75 #define BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST (1 << 1) // Set if analog/digital outputs persist for future buffers
44 76
45 // Mappings from pin numbers on PCB to actual DAC channels 77 // Mappings from pin numbers on PCB to actual DAC channels
46 // This gives the DAC and ADC connectors the same effective pinout 78 // This gives the DAC and ADC connectors the same effective pinout
47
48 #define DAC_PIN0 6 79 #define DAC_PIN0 6
49 #define DAC_PIN1 4 80 #define DAC_PIN1 4
50 #define DAC_PIN2 2 81 #define DAC_PIN2 2
51 #define DAC_PIN3 0 82 #define DAC_PIN3 0
52 #define DAC_PIN4 1 83 #define DAC_PIN4 1
61 #define ADC_PIN4 4 92 #define ADC_PIN4 4
62 #define ADC_PIN5 5 93 #define ADC_PIN5 5
63 #define ADC_PIN6 6 94 #define ADC_PIN6 6
64 #define ADC_PIN7 7 95 #define ADC_PIN7 7
65 96
66 // Structure which contains initialisation parameters for the 97 /**
67 // real-time audio system 98 * \ingroup control
99 * \brief Structure containing initialisation parameters for the real-time
100 * audio control system.
101 *
102 * This structure is initialised using BeagleRT_defaultSettings(). Its contents
103 * are used up through the point of calling
104 * BeagleRT_initAudio() at which point it is no longer needed.
105 */
68 typedef struct { 106 typedef struct {
69 // These items might be adjusted by the user: 107 // These items might be adjusted by the user:
70 int periodSize; // Number of (analog) frames per period; audio is twice this 108
71 int useAnalog; // Whether to use the analog 109 /// \brief Number of (analog) frames per period.
72 int useDigital; // Whether to use the 16 programmable GPIOs 110 ///
73 int numAnalogChannels; // How many channels for the ADC and DAC 111 /// Number of audio frames depends on relative sample rates of the two. By default,
74 int numDigitalChannels; // How many channels for the GPIOs 112 /// audio is twice the sample rate, so has twice the period size.
75 113 int periodSize;
76 int beginMuted; // Whether to begin with the speakers muted 114 /// Whether to use the analog input and output
77 float dacLevel; // Level for the audio DAC output 115 int useAnalog;
78 float adcLevel; // Level for the audio ADC input 116 /// Whether to use the 16 programmable GPIOs
79 float headphoneLevel; // Level for the headphone output 117 int useDigital;
80 118 /// How many channels for the ADC and DAC
81 char pruFilename[MAX_PRU_FILENAME_LENGTH]; // The external .bin file to load. If empty will use PRU code from pru_rtaudio_bin.h 119 int numAnalogChannels;
82 int verbose; // Whether to use verbose logging 120 /// How many channels for the GPIOs
121 int numDigitalChannels;
122
123 /// Whether to begin with the speakers muted
124 int beginMuted;
125 /// Level for the audio DAC output
126 float dacLevel;
127 /// Level for the audio ADC input
128 float adcLevel;
129 /// Level for the headphone output
130 float headphoneLevel;
131
132 /// The external .bin file to load. If empty will use PRU code from pru_rtaudio_bin.h
133 char pruFilename[MAX_PRU_FILENAME_LENGTH];
134 /// Whether to use verbose logging
135 int verbose;
83 136
84 // These items are application-dependent but should probably be 137 // These items are application-dependent but should probably be
85 // determined by the programmer rather than the user 138 // determined by the programmer rather than the user
86 int interleave; // Whether audio/analog data should be interleaved 139
87 int analogOutputsPersist; // Whether analog outputs should persist to future frames 140 /// Whether audio/analog data should be interleaved
88 // n.b. digital pins always persist, audio never does 141 int interleave;
142 /// \brief Whether analog outputs should persist to future frames.
143 ///
144 /// n.b. digital pins always persist, audio never does
145 int analogOutputsPersist;
89 146
90 // These items are hardware-dependent and should only be changed 147 // These items are hardware-dependent and should only be changed
91 // to run on different hardware 148 // to run on different hardware
92 int codecI2CAddress; // Where the codec can be found on the I2C bus 149
93 int ampMutePin; // Pin where amplifier mute can be found 150 /// Where the codec can be found on the I2C bus
94 int receivePort; // Port where the UDP server will listen 151 int codecI2CAddress;
95 int transmitPort; // Port where the UDP client will transmit 152 /// Pin where amplifier mute can be found
153 int ampMutePin;
154 /// Port where the UDP server will listen
155 int receivePort;
156 /// Port where the UDP client will transmit
157 int transmitPort;
96 char serverName[MAX_SERVERNAME_LENGTH]; 158 char serverName[MAX_SERVERNAME_LENGTH];
97 } BeagleRTInitSettings; 159 } BeagleRTInitSettings;
98 160
99 // BeagleRTContext data structure 161
100 // Holds information passed to the render() function and related calls 162 /**
101 // Contains the current audio and sensor settings and pointers to the data buffers 163 * \ingroup render
102 164 * \brief Structure holding current audio and sensor settings and pointers to data buffers.
165 *
166 * This structure is passed to initialise_render(), render() and cleanup_render(). It is
167 * initialised in BeagleRT_initAudio() based on the contents of the BeagleRTInitSettings
168 * structure.
169 */
103 typedef struct { 170 typedef struct {
171 /// \brief Buffer holding audio input samples
172 ///
173 /// This buffer may be in either interleaved or non-interleaved format,
174 /// depending on the contents of the BeagleRTInitSettings structure.
175 /// \b Note: this element is available in render() only.
104 float *audioIn; 176 float *audioIn;
177
178 /// \brief Buffer holding audio output samples
179 ///
180 /// This buffer may be in either interleaved or non-interleaved format,
181 /// depending on the contents of the BeagleRTInitSettings structure.
182 /// \b Note: this element is available in render() only.
105 float *audioOut; 183 float *audioOut;
184
185 /// \brief Buffer holding analog input samples
186 ///
187 /// This buffer may be in either interleaved or non-interleaved format,
188 /// depending on the contents of the BeagleRTInitSettings structure.
189 /// \b Note: this element is available in render() only.
106 float *analogIn; 190 float *analogIn;
191
192 /// \brief Buffer holding analog output samples
193 ///
194 /// This buffer may be in either interleaved or non-interleaved format,
195 /// depending on the contents of the BeagleRTInitSettings structure.
196 /// \b Note: this element is available in render() only.
107 float *analogOut; 197 float *analogOut;
198
199 /// \brief Buffer holding digital input/output samples
200 ///
201 /// \b Note: this element is available in render() only.
108 uint32_t *digital; 202 uint32_t *digital;
109 203
204 /// Number of audio frames per period
110 uint32_t audioFrames; 205 uint32_t audioFrames;
206 /// Number of audio channels (currently always 2)
111 uint32_t audioChannels; 207 uint32_t audioChannels;
208 /// Audio sample rate in Hz (currently always 44100.0)
112 float audioSampleRate; 209 float audioSampleRate;
113 210
211 /// \brief Number of analog frames per period
212 ///
213 /// This will be 0 if analog I/O is disabled.
114 uint32_t analogFrames; 214 uint32_t analogFrames;
215
216 /// \brief Number of analog channels
217 ///
218 /// This could take a value of 8, 4 or 2. This will be 0 if analog I/O is disabled.
115 uint32_t analogChannels; 219 uint32_t analogChannels;
220
221 /// \brief Analog sample rate in Hz
222 ///
223 /// The analog sample rate depends on the number of analog channels used. If
224 /// 8 channels are used, the sample rate is 22050. If 4 channels are used, the sample
225 /// rate is 44100. If 2 channels are used, the sample rate is 88200. If analog I/O
226 /// is disabled, the sample rate is 0.
116 float analogSampleRate; 227 float analogSampleRate;
117 228
229 /// Number of digital frames per period
118 uint32_t digitalFrames; 230 uint32_t digitalFrames;
231 /// \brief Number of digital channels
232 ///
233 /// Currently this will always be 16, unless digital I/O is disabled, in which case it will be 0.
119 uint32_t digitalChannels; 234 uint32_t digitalChannels;
235 /// Digital sample rate in Hz (currently always 44100.0)
120 float digitalSampleRate; 236 float digitalSampleRate;
121 237
238 /// \brief Number of elapsed audio samples since the start of rendering.
239 ///
240 /// This holds the total number of audio samples as of the beginning of the current period. To
241 /// find the current number of analog or digital samples elapsed, multiply by the ratio of the
242 /// sample rates (e.g. half the number of analog samples will have elapsed if the analog sample
243 /// rate is 22050).
122 uint64_t audioSampleCount; 244 uint64_t audioSampleCount;
245
246 /// \brief Other audio/sensor settings
247 ///
248 /// Binary combination of flags including:
249 ///
250 /// BEAGLERT_FLAG_INTERLEAVED: indicates the audio and analog buffers are interleaved
251 ///
252 /// BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST: indicates that writes to the analog outputs will
253 /// persist for future frames. If not set, writes affect one frame only.
123 uint32_t flags; 254 uint32_t flags;
124 } BeagleRTContext; 255 } BeagleRTContext;
125 256
126 enum { 257 /** \ingroup auxtask
127 kAmplifierMutePin = 61 // P8-26 controls amplifier mute 258 *
128 }; 259 * Auxiliary task variable. Auxiliary tasks are created using createAuxiliaryTask() and
129 260 * automatically cleaned up after cleanup_render() finishes.
261 */
130 typedef void* AuxiliaryTask; // Opaque data type to keep track of aux tasks 262 typedef void* AuxiliaryTask; // Opaque data type to keep track of aux tasks
131 263
132 // Flag that indicates when the audio will stop; can be read or 264 /** \ingroup render
133 // set by other components which should end at the same time as the audio 265 *
266 * Flag that indicates when the audio will stop. Threads can poll this variable to indicate when
267 * they should stop. Additionally, a program can set this to \c true
268 * to indicate that audio processing should terminate. Calling BeagleRT_stopAudio()
269 * has the effect of setting this to \c true.
270 */
134 extern bool gShouldStop; 271 extern bool gShouldStop;
135 272
136 // *** User-defined render functions *** 273 // *** User-defined render functions ***
274
275 /**
276 * \defgroup render User-defined render functions
277 *
278 * These three functions must be implemented by the developer in every BeagleRT program.
279 * Typically they appear in their own .cpp source file.
280 *
281 * @{
282 */
137 283
138 /** 284 /**
139 * \brief User-defined initialisation function which runs before audio rendering begins. 285 * \brief User-defined initialisation function which runs before audio rendering begins.
140 * 286 *
141 * This function runs once at the beginning of the program, after most of the system 287 * This function runs once at the beginning of the program, after most of the system
184 * in cleanup_render(). 330 * in cleanup_render().
185 * \param userData An opaque pointer to an optional user-defined data structure. Will 331 * \param userData An opaque pointer to an optional user-defined data structure. Will
186 * be the same as the \c userData parameter passed to initialise_render() and render(). 332 * be the same as the \c userData parameter passed to initialise_render() and render().
187 */ 333 */
188 void cleanup_render(BeagleRTContext *context, void *userData); 334 void cleanup_render(BeagleRTContext *context, void *userData);
335
336 /** @} */
337
338 /**
339 * \defgroup control Control and command line functions
340 *
341 * These functions are used to initialise the BeagleRT settings, process arguments
342 * from the command line, and start/stop the audio and sensor system.
343 *
344 * @{
345 */
189 346
190 // *** Command-line settings *** 347 // *** Command-line settings ***
191 348
192 /** 349 /**
193 * \brief Initialise the data structure containing settings for BeagleRT. 350 * \brief Initialise the data structure containing settings for BeagleRT.
239 * \param level Verbosity level of the internal BeagleRT system. 0 by default; higher values will 396 * \param level Verbosity level of the internal BeagleRT system. 0 by default; higher values will
240 * print more information. Presently all positive numbers produce the same level of printing. 397 * print more information. Presently all positive numbers produce the same level of printing.
241 */ 398 */
242 void BeagleRT_setVerboseLevel(int level); 399 void BeagleRT_setVerboseLevel(int level);
243 400
401
244 // *** Audio control functions *** 402 // *** Audio control functions ***
245 403
246 /** 404 /**
247 * \brief Initialise audio and sensor rendering environment. 405 * \brief Initialise audio and sensor rendering environment.
248 * 406 *
285 * internal resources for audio and sensor processing. In the process, it will call the 443 * internal resources for audio and sensor processing. In the process, it will call the
286 * user-defined cleanup_render() function. 444 * user-defined cleanup_render() function.
287 */ 445 */
288 void BeagleRT_cleanupAudio(); 446 void BeagleRT_cleanupAudio();
289 447
290 // Volume/level controls 448 /** @} */
291 // These return 0 on success 449
450 /**
451 * \defgroup levels Audio level controls
452 *
453 * These functions control the input and output levels for the audio codec. If a BeagleRT program
454 * does not call these functions, sensible default levels will be used.
455 *
456 * @{
457 */
292 458
293 // *** Volume and level controls *** 459 // *** Volume and level controls ***
294 460
295 /** 461 /**
296 * \brief Set the level of the audio DAC. 462 * \brief Set the level of the audio DAC.
354 * 520 *
355 * \return 0 on success, or nonzero if an error occurred. 521 * \return 0 on success, or nonzero if an error occurred.
356 */ 522 */
357 int BeagleRT_muteSpeakers(int mute); 523 int BeagleRT_muteSpeakers(int mute);
358 524
525 /** @} */
526
527 /**
528 * \defgroup auxtask Auxiliary task support
529 *
530 * These functions are used to create separate real-time tasks (threads) which run at lower
531 * priority than the audio processing. They can be used, for example, for large time-consuming
532 * calculations which would take more than one audio frame length to process, or they could be
533 * used to communicate with external hardware when that communication might block or be delayed.
534 *
535 * All auxiliary tasks used by the program should be created in initialise_render(). The tasks
536 * can then be scheduled at will within the render() function.
537 *
538 * @{
539 */
540
359 // *** Functions for creating auxiliary tasks *** 541 // *** Functions for creating auxiliary tasks ***
360 542
361 /** 543 /**
362 * \brief Create a new auxiliary task. 544 * \brief Create a new auxiliary task.
363 * 545 *
364 * This function creates a new auxiliary task which, when scheduled, runs the function specified 546 * This function creates a new auxiliary task which, when scheduled, runs the function specified
365 * in the first argument. Note that the task does not run until scheduleAuxiliaryTask() is called. 547 * in the first argument. Note that the task does not run until scheduleAuxiliaryTask() is called.
366 * Auxiliary tasks should be created in initialise_render() and never in render() itself. 548 * Auxiliary tasks should be created in initialise_render() and never in render() itself.
367 * 549 *
368 * The second argument specifies the real-time priority. Valid values are between 0 550 * The second argument specifies the real-time priority. Valid values are between 0
369 * and 99, and usually should be lower than BEAGLERT_AUDIO_PRIORITY. Tasks with higher priority always 551 * and 99, and usually should be lower than \ref BEAGLERT_AUDIO_PRIORITY. Tasks with higher priority always
370 * preempt tasks with lower priority. 552 * preempt tasks with lower priority.
371 * 553 *
372 * \param functionToCall Function which will run each time the auxiliary task is scheduled. 554 * \param functionToCall Function which will run each time the auxiliary task is scheduled.
373 * \param priority Xenomai priority level at which the task should run. 555 * \param priority Xenomai priority level at which the task should run.
374 * \param Name for this task, which should be unique system-wide (no other running program should use this name). 556 * \param name Name for this task, which should be unique system-wide (no other running program should use this name).
375 */ 557 */
376 AuxiliaryTask createAuxiliaryTaskLoop(void (*functionToCall)(void), int priority, const char *name); 558 AuxiliaryTask BeagleRT_createAuxiliaryTask(void (*functionToCall)(void), int priority, const char *name);
377 559
378 /** 560 /**
379 * \brief Run an auxiliary task which has previously been created. 561 * \brief Run an auxiliary task which has previously been created.
380 * 562 *
381 * This function will schedule an auxiliary task to run. When the task runs, the function in the first 563 * This function will schedule an auxiliary task to run. When the task runs, the function in the first
384 * scheduleAuxiliaryTask() is typically called from render() to start a lower-priority task. The function 566 * scheduleAuxiliaryTask() is typically called from render() to start a lower-priority task. The function
385 * will not run immediately, but only once any active higher priority tasks have finished. 567 * will not run immediately, but only once any active higher priority tasks have finished.
386 * 568 *
387 * \param task Task to schedule for running. 569 * \param task Task to schedule for running.
388 */ 570 */
389 void scheduleAuxiliaryTask(AuxiliaryTask task); 571 void BeagleRT_scheduleAuxiliaryTask(AuxiliaryTask task);
572
573 /** @} */
390 574
391 #endif /* BEAGLERT_H_ */ 575 #endif /* BEAGLERT_H_ */