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