annotate core/RTAudio.cpp @ 258:88cf310417cd aux_task_args

Add a parameter 'autoSchedule' to createAuxiliaryTask() which when true causes the task to be automatically scheduled after every render function call, without the user needing to call scheduleAuxiliaryTask()
author Liam Donovan <l.b.donovan@qmul.ac.uk>
date Sat, 07 May 2016 13:23:15 +0100
parents 3e93d9793da3
children c55c6f6c233c
rev   line source
andrewm@0 1 /*
andrewm@0 2 * RTAudio.cpp
andrewm@0 3 *
andrewm@0 4 * Central control code for hard real-time audio on BeagleBone Black
andrewm@0 5 * using PRU and Xenomai Linux extensions. This code began as part
andrewm@0 6 * of the Hackable Instruments project (EPSRC) at Queen Mary University
andrewm@0 7 * of London, 2013-14.
andrewm@0 8 *
andrewm@0 9 * (c) 2014 Victor Zappi and Andrew McPherson
andrewm@0 10 * Queen Mary University of London
andrewm@0 11 */
andrewm@0 12
andrewm@0 13
andrewm@0 14 #include <stdio.h>
andrewm@0 15 #include <stdlib.h>
andrewm@0 16 #include <string.h>
andrewm@0 17 #include <strings.h>
andrewm@0 18 #include <math.h>
andrewm@0 19 #include <iostream>
andrewm@0 20 #include <assert.h>
andrewm@0 21 #include <vector>
andrewm@0 22
andrewm@0 23 // Xenomai-specific includes
andrewm@0 24 #include <sys/mman.h>
andrewm@0 25 #include <native/task.h>
andrewm@0 26 #include <native/timer.h>
andrewm@45 27 #include <native/intr.h>
andrewm@0 28 #include <rtdk.h>
andrewm@0 29
andrewm@45 30 #include "../include/BeagleRT.h"
andrewm@0 31 #include "../include/PRU.h"
andrewm@0 32 #include "../include/I2c_Codec.h"
andrewm@0 33 #include "../include/GPIOcontrol.h"
andrewm@0 34
andrewm@45 35 // ARM interrupt number for PRU event EVTOUT7
andrewm@45 36 #define PRU_RTAUDIO_IRQ 21
andrewm@45 37
andrewm@0 38 using namespace std;
andrewm@0 39
andrewm@0 40 // Data structure to keep track of auxiliary tasks we
andrewm@0 41 // can schedule
andrewm@0 42 typedef struct {
andrewm@0 43 RT_TASK task;
l@256 44 void (*argfunction)(void*);
l@256 45 void (*function)(void);
andrewm@0 46 char *name;
andrewm@0 47 int priority;
giuliomoro@174 48 bool started;
l@256 49 bool hasArgs;
l@254 50 void* args;
l@258 51 bool autoSchedule;
andrewm@0 52 } InternalAuxiliaryTask;
andrewm@0 53
andrewm@0 54 const char gRTAudioThreadName[] = "beaglert-audio";
andrewm@45 55 const char gRTAudioInterruptName[] = "beaglert-pru-irq";
andrewm@0 56
andrewm@0 57 // Real-time tasks and objects
andrewm@0 58 RT_TASK gRTAudioThread;
andrewm@50 59 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
andrewm@45 60 RT_INTR gRTAudioInterrupt;
andrewm@50 61 #endif
andrewm@0 62 PRU *gPRU = 0;
andrewm@0 63 I2c_Codec *gAudioCodec = 0;
andrewm@0 64
giuliomoro@176 65 vector<InternalAuxiliaryTask*> &getAuxTasks(){
giuliomoro@176 66 static vector<InternalAuxiliaryTask*> auxTasks;
giuliomoro@176 67 return auxTasks;
giuliomoro@176 68 }
andrewm@0 69
andrewm@0 70 // Flag which tells the audio task to stop
giuliomoro@233 71 int gShouldStop = false;
andrewm@0 72
andrewm@0 73 // general settings
andrewm@45 74 char gPRUFilename[MAX_PRU_FILENAME_LENGTH]; // Path to PRU binary file (internal code if empty)_
andrewm@0 75 int gRTAudioVerbose = 0; // Verbosity level for debugging
andrewm@0 76 int gAmplifierMutePin = -1;
andrewm@5 77 int gAmplifierShouldBeginMuted = 0;
andrewm@0 78
andrewm@45 79 // Context which holds all the audio/sensor data passed to the render routines
andrewm@45 80 BeagleRTContext gContext;
andrewm@45 81
andrewm@45 82 // User data passed in from main()
andrewm@45 83 void *gUserData;
andrewm@0 84
andrewm@0 85 // initAudio() prepares the infrastructure for running PRU-based real-time
andrewm@0 86 // audio, but does not actually start the calculations.
giuliomoro@178 87 // periodSize indicates the number of audio frames per period: the analog period size
giuliomoro@178 88 // will depend on the number of analog channels, in such a way that
giuliomoro@178 89 // analogPeriodSize = 4*periodSize/numAnalogChannels
giuliomoro@178 90 // In total, the audio latency in frames will be 2*periodSize,
andrewm@0 91 // plus any latency inherent in the ADCs and DACs themselves.
giuliomoro@19 92 // useAnalog indicates whether to enable the ADC and DAC or just use the audio codec.
giuliomoro@19 93 // numAnalogChannels indicates how many ADC and DAC channels to use.
andrewm@56 94 // userData is an opaque pointer which will be passed through to the setup()
andrewm@0 95 // function for application-specific use
andrewm@0 96 //
andrewm@0 97 // Returns 0 on success.
andrewm@0 98
andrewm@45 99 int BeagleRT_initAudio(BeagleRTInitSettings *settings, void *userData)
andrewm@0 100 {
andrewm@0 101 rt_print_auto_init(1);
andrewm@45 102
andrewm@45 103 BeagleRT_setVerboseLevel(settings->verbose);
andrewm@45 104 strncpy(gPRUFilename, settings->pruFilename, MAX_PRU_FILENAME_LENGTH);
andrewm@45 105 gUserData = userData;
andrewm@45 106
andrewm@45 107 // Initialise context data structure
andrewm@45 108 memset(&gContext, 0, sizeof(BeagleRTContext));
andrewm@0 109
andrewm@5 110 if(gRTAudioVerbose) {
andrewm@5 111 cout << "Starting with period size " << settings->periodSize << "; ";
giuliomoro@19 112 if(settings->useAnalog)
giuliomoro@19 113 cout << "analog enabled\n";
andrewm@5 114 else
giuliomoro@19 115 cout << "analog disabled\n";
andrewm@5 116 cout << "DAC level " << settings->dacLevel << "dB; ADC level " << settings->adcLevel;
andrewm@5 117 cout << "dB; headphone level " << settings->headphoneLevel << "dB\n";
andrewm@5 118 if(settings->beginMuted)
andrewm@5 119 cout << "Beginning with speaker muted\n";
andrewm@5 120 }
andrewm@0 121
andrewm@0 122 // Prepare GPIO pins for amplifier mute and status LED
andrewm@5 123 if(settings->ampMutePin >= 0) {
andrewm@5 124 gAmplifierMutePin = settings->ampMutePin;
andrewm@5 125 gAmplifierShouldBeginMuted = settings->beginMuted;
andrewm@0 126
andrewm@5 127 if(gpio_export(settings->ampMutePin)) {
andrewm@0 128 if(gRTAudioVerbose)
giuliomoro@16 129 cout << "Warning: couldn't export amplifier mute pin " << settings-> ampMutePin << "\n";
andrewm@0 130 }
andrewm@5 131 if(gpio_set_dir(settings->ampMutePin, OUTPUT_PIN)) {
andrewm@0 132 if(gRTAudioVerbose)
andrewm@0 133 cout << "Couldn't set direction on amplifier mute pin\n";
andrewm@0 134 return -1;
andrewm@0 135 }
andrewm@5 136 if(gpio_set_value(settings->ampMutePin, LOW)) {
andrewm@0 137 if(gRTAudioVerbose)
andrewm@0 138 cout << "Couldn't set value on amplifier mute pin\n";
andrewm@0 139 return -1;
andrewm@0 140 }
andrewm@0 141 }
andrewm@0 142
giuliomoro@19 143 // Limit the analog channels to sane values
giuliomoro@19 144 if(settings->numAnalogChannels >= 8)
giuliomoro@19 145 settings->numAnalogChannels = 8;
giuliomoro@19 146 else if(settings->numAnalogChannels >= 4)
giuliomoro@19 147 settings->numAnalogChannels = 4;
andrewm@12 148 else
giuliomoro@19 149 settings->numAnalogChannels = 2;
andrewm@12 150
andrewm@45 151 // Initialise the rendering environment: sample rates, frame counts, numbers of channels
andrewm@45 152 gContext.audioSampleRate = 44100.0;
andrewm@45 153 gContext.audioChannels = 2;
andrewm@45 154
andrewm@45 155 if(settings->useAnalog) {
giuliomoro@178 156 gContext.audioFrames = settings->periodSize;
andrewm@45 157
giuliomoro@178 158 gContext.analogFrames = gContext.audioFrames * 4 / settings->numAnalogChannels;
andrewm@45 159 gContext.analogChannels = settings->numAnalogChannels;
andrewm@45 160 gContext.analogSampleRate = gContext.audioSampleRate * 4.0 / (float)settings->numAnalogChannels;
andrewm@45 161 }
andrewm@45 162 else {
giuliomoro@178 163 gContext.audioFrames = settings->periodSize;
andrewm@45 164
andrewm@45 165 gContext.analogFrames = 0;
andrewm@45 166 gContext.analogChannels = 0;
andrewm@45 167 gContext.analogSampleRate = 0;
andrewm@45 168 }
andrewm@45 169
giuliomoro@178 170 // Sanity check the combination of channels and period size
giuliomoro@210 171 if( gContext.analogChannels != 0 && ((gContext.analogChannels <= 4 && gContext.analogFrames < 2) ||
giuliomoro@210 172 (gContext.analogChannels <= 2 && gContext.analogFrames < 4)) )
giuliomoro@178 173 {
giuliomoro@178 174 cout << "Error: " << gContext.analogChannels << " channels and period size of " << gContext.analogFrames << " not supported.\n";
giuliomoro@178 175 return 1;
giuliomoro@178 176 }
giuliomoro@178 177
andrewm@45 178 // For now, digital frame rate is equal to audio frame rate
andrewm@45 179 if(settings->useDigital) {
andrewm@45 180 gContext.digitalFrames = gContext.audioFrames;
andrewm@45 181 gContext.digitalSampleRate = gContext.audioSampleRate;
andrewm@45 182 gContext.digitalChannels = settings->numDigitalChannels;
andrewm@45 183 }
andrewm@45 184 else {
andrewm@45 185 gContext.digitalFrames = 0;
andrewm@45 186 gContext.digitalSampleRate = 0;
andrewm@45 187 gContext.digitalChannels = 0;
andrewm@45 188 }
andrewm@45 189
andrewm@45 190 // Set flags based on init settings
andrewm@45 191 if(settings->interleave)
andrewm@45 192 gContext.flags |= BEAGLERT_FLAG_INTERLEAVED;
andrewm@45 193 if(settings->analogOutputsPersist)
andrewm@45 194 gContext.flags |= BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST;
andrewm@45 195
andrewm@0 196 // Use PRU for audio
andrewm@45 197 gPRU = new PRU(&gContext);
andrewm@0 198 gAudioCodec = new I2c_Codec();
andrewm@0 199
andrewm@45 200 // Initialise the GPIO pins, including possibly the digital pins in the render routines
andrewm@45 201 if(gPRU->prepareGPIO(1, 1)) {
andrewm@0 202 cout << "Error: unable to prepare GPIO for PRU audio\n";
andrewm@0 203 return 1;
andrewm@0 204 }
andrewm@45 205
andrewm@45 206 // Get the PRU memory buffers ready to go
giuliomoro@178 207 if(gPRU->initialise(0, gContext.analogFrames, gContext.analogChannels, true)) {
andrewm@0 208 cout << "Error: unable to initialise PRU\n";
andrewm@0 209 return 1;
andrewm@0 210 }
andrewm@45 211
andrewm@45 212 // Prepare the audio codec, which clocks the whole system
andrewm@5 213 if(gAudioCodec->initI2C_RW(2, settings->codecI2CAddress, -1)) {
andrewm@0 214 cout << "Unable to open codec I2C\n";
andrewm@0 215 return 1;
andrewm@0 216 }
andrewm@0 217 if(gAudioCodec->initCodec()) {
andrewm@0 218 cout << "Error: unable to initialise audio codec\n";
andrewm@0 219 return 1;
andrewm@0 220 }
giuliomoro@172 221
andrewm@5 222 // Set default volume levels
andrewm@5 223 BeagleRT_setDACLevel(settings->dacLevel);
andrewm@5 224 BeagleRT_setADCLevel(settings->adcLevel);
giuliomoro@174 225 // TODO: add more argument checks
giuliomoro@171 226 for(int n = 0; n < 2; n++){
giuliomoro@172 227 if(settings->pgaGain[n] > 59.5){
giuliomoro@172 228 std::cerr << "PGA gain out of range [0,59.5]\n";
giuliomoro@172 229 exit(1);
giuliomoro@172 230 }
giuliomoro@171 231 BeagleRT_setPgaGain(settings->pgaGain[n], n);
giuliomoro@171 232 }
andrewm@5 233 BeagleRT_setHeadphoneLevel(settings->headphoneLevel);
andrewm@5 234
andrewm@45 235 // Call the user-defined initialisation function
andrewm@56 236 if(!setup(&gContext, userData)) {
andrewm@0 237 cout << "Couldn't initialise audio rendering\n";
andrewm@0 238 return 1;
andrewm@0 239 }
andrewm@0 240
andrewm@0 241 return 0;
andrewm@0 242 }
andrewm@0 243
andrewm@0 244 // audioLoop() is the main function which starts the PRU audio code
andrewm@0 245 // and then transfers control to the PRU object. The PRU object in
andrewm@0 246 // turn will call the audio render() callback function every time
andrewm@0 247 // there is new data to process.
andrewm@0 248
andrewm@0 249 void audioLoop(void *)
andrewm@0 250 {
andrewm@0 251 if(gRTAudioVerbose==1)
andrewm@0 252 rt_printf("_________________Audio Thread!\n");
andrewm@0 253
andrewm@0 254 // PRU audio
andrewm@0 255 assert(gAudioCodec != 0 && gPRU != 0);
andrewm@0 256
andrewm@0 257 if(gAudioCodec->startAudio(0)) {
andrewm@0 258 rt_printf("Error: unable to start I2C audio codec\n");
andrewm@0 259 gShouldStop = 1;
andrewm@0 260 }
andrewm@0 261 else {
giuliomoro@16 262 if(gPRU->start(gPRUFilename)) {
giuliomoro@16 263 rt_printf("Error: unable to start PRU from file %s\n", gPRUFilename);
andrewm@0 264 gShouldStop = 1;
andrewm@0 265 }
andrewm@0 266 else {
andrewm@0 267 // All systems go. Run the loop; it will end when gShouldStop is set to 1
andrewm@5 268
andrewm@5 269 if(!gAmplifierShouldBeginMuted) {
andrewm@5 270 // First unmute the amplifier
andrewm@5 271 if(BeagleRT_muteSpeakers(0)) {
andrewm@5 272 if(gRTAudioVerbose)
andrewm@5 273 rt_printf("Warning: couldn't set value (high) on amplifier mute pin\n");
andrewm@5 274 }
andrewm@0 275 }
andrewm@0 276
andrewm@50 277 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
andrewm@45 278 gPRU->loop(&gRTAudioInterrupt, gUserData);
andrewm@50 279 #else
andrewm@50 280 gPRU->loop(0, gUserData);
andrewm@50 281 #endif
andrewm@0 282 // Now clean up
andrewm@0 283 // gPRU->waitForFinish();
andrewm@0 284 gPRU->disable();
andrewm@0 285 gAudioCodec->stopAudio();
andrewm@0 286 gPRU->cleanupGPIO();
andrewm@0 287 }
andrewm@0 288 }
andrewm@0 289
andrewm@0 290 if(gRTAudioVerbose == 1)
andrewm@0 291 rt_printf("audio thread ended\n");
andrewm@0 292 }
andrewm@0 293
andrewm@0 294 // Create a calculation loop which can run independently of the audio, at a different
andrewm@45 295 // (equal or lower) priority. Audio priority is defined in BEAGLERT_AUDIO_PRIORITY;
andrewm@45 296 // priority should be generally be less than this.
andrewm@0 297 // Returns an (opaque) pointer to the created task on success; 0 on failure
l@258 298 AuxiliaryTask BeagleRT_createAuxiliaryTask(void (*functionToCall)(void* args), int priority, const char *name, void* args, bool autoSchedule)
andrewm@0 299 {
andrewm@0 300 InternalAuxiliaryTask *newTask = (InternalAuxiliaryTask*)malloc(sizeof(InternalAuxiliaryTask));
andrewm@0 301
andrewm@0 302 // Attempt to create the task
andrewm@0 303 if(rt_task_create(&(newTask->task), name, 0, priority, T_JOINABLE | T_FPU)) {
andrewm@0 304 cout << "Error: unable to create auxiliary task " << name << endl;
andrewm@0 305 free(newTask);
andrewm@0 306 return 0;
andrewm@0 307 }
andrewm@0 308
andrewm@0 309 // Populate the rest of the data structure and store it in the vector
l@256 310 newTask->argfunction = functionToCall;
andrewm@0 311 newTask->name = strdup(name);
andrewm@0 312 newTask->priority = priority;
giuliomoro@174 313 newTask->started = false;
l@254 314 newTask->args = args;
l@256 315 newTask->hasArgs = true;
l@258 316 newTask->autoSchedule = autoSchedule;
l@258 317
giuliomoro@176 318 getAuxTasks().push_back(newTask);
andrewm@0 319
andrewm@0 320 return (AuxiliaryTask)newTask;
andrewm@0 321 }
l@258 322 AuxiliaryTask BeagleRT_createAuxiliaryTask(void (*functionToCall)(void), int priority, const char *name, bool autoSchedule)
l@256 323 {
l@256 324 InternalAuxiliaryTask *newTask = (InternalAuxiliaryTask*)malloc(sizeof(InternalAuxiliaryTask));
l@258 325
l@256 326 // Attempt to create the task
l@256 327 if(rt_task_create(&(newTask->task), name, 0, priority, T_JOINABLE | T_FPU)) {
l@256 328 cout << "Error: unable to create auxiliary task " << name << endl;
l@256 329 free(newTask);
l@256 330 return 0;
l@256 331 }
l@258 332
l@256 333 // Populate the rest of the data structure and store it in the vector
l@256 334 newTask->function = functionToCall;
l@256 335 newTask->name = strdup(name);
l@256 336 newTask->priority = priority;
l@256 337 newTask->started = false;
l@256 338 newTask->hasArgs = false;
l@258 339 newTask->autoSchedule = autoSchedule;
l@258 340
l@256 341 getAuxTasks().push_back(newTask);
l@258 342
l@256 343 return (AuxiliaryTask)newTask;
l@256 344 }
andrewm@0 345
giuliomoro@174 346 // Schedule a previously created (and started) auxiliary task. It will run when the priority rules next
andrewm@0 347 // allow it to be scheduled.
andrewm@47 348 void BeagleRT_scheduleAuxiliaryTask(AuxiliaryTask task)
andrewm@0 349 {
andrewm@0 350 InternalAuxiliaryTask *taskToSchedule = (InternalAuxiliaryTask *)task;
giuliomoro@174 351 if(taskToSchedule->started == false){ // Note: this is not the safest method to check if a task
giuliomoro@174 352 BeagleRT_startAuxiliaryTask(task); // is started (or ready to be resumed), but it probably is the fastest.
giuliomoro@174 353 // A safer approach would use rt_task_inquire()
giuliomoro@174 354 }
andrewm@0 355 rt_task_resume(&taskToSchedule->task);
andrewm@0 356 }
l@258 357 void BeagleRT_autoScheduleAuxiliaryTasks(){
l@258 358 vector<InternalAuxiliaryTask*>::iterator it;
l@258 359 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
l@258 360 if ((InternalAuxiliaryTask *)(*it)->autoSchedule){
l@258 361 BeagleRT_scheduleAuxiliaryTask(*it);
l@258 362 }
l@258 363 }
l@258 364 }
andrewm@0 365
andrewm@0 366 // Calculation loop that can be used for other tasks running at a lower
andrewm@0 367 // priority than the audio thread. Simple wrapper for Xenomai calls.
andrewm@0 368 // Treat the argument as containing the task structure
andrewm@0 369 void auxiliaryTaskLoop(void *taskStruct)
andrewm@0 370 {
l@256 371 InternalAuxiliaryTask *task = ((InternalAuxiliaryTask *)taskStruct);
l@256 372
andrewm@0 373 // Get function to call from the argument
l@256 374 void (*auxiliary_argfunction)(void* args) = task->argfunction;
l@256 375 void (*auxiliary_function)(void) = task->function;
l@256 376
l@258 377 // get the task's name
l@256 378 const char *name = task->name;
andrewm@0 379
andrewm@0 380 // Wait for a notification
andrewm@0 381 rt_task_suspend(NULL);
andrewm@0 382
andrewm@0 383 while(!gShouldStop) {
andrewm@0 384 // Then run the calculations
l@256 385 if (task->hasArgs)
l@256 386 auxiliary_argfunction(task->args);
l@256 387 else
l@256 388 auxiliary_function();
andrewm@0 389
andrewm@0 390 // Wait for a notification
andrewm@0 391 rt_task_suspend(NULL);
andrewm@0 392 }
andrewm@0 393
andrewm@0 394 if(gRTAudioVerbose == 1)
andrewm@0 395 rt_printf("auxiliary task %s ended\n", name);
andrewm@0 396 }
andrewm@0 397
giuliomoro@174 398
giuliomoro@174 399 int BeagleRT_startAuxiliaryTask(AuxiliaryTask task){
giuliomoro@174 400 InternalAuxiliaryTask *taskStruct;
giuliomoro@174 401 taskStruct = (InternalAuxiliaryTask *)task;
giuliomoro@174 402 if(taskStruct->started == true)
giuliomoro@174 403 return 0;
giuliomoro@174 404 if(rt_task_start(&(taskStruct->task), &auxiliaryTaskLoop, taskStruct)) {
giuliomoro@174 405 cerr << "Error: unable to start Xenomai task " << taskStruct->name << endl;
giuliomoro@174 406 return -1;
giuliomoro@174 407 }
giuliomoro@174 408 taskStruct->started = true;
giuliomoro@174 409 return 0;
giuliomoro@174 410 }
giuliomoro@174 411
andrewm@0 412 // startAudio() should be called only after initAudio() successfully completes.
andrewm@0 413 // It launches the real-time Xenomai task which runs the audio loop. Returns 0
andrewm@0 414 // on success.
andrewm@0 415
andrewm@5 416 int BeagleRT_startAudio()
andrewm@0 417 {
andrewm@45 418 // Create audio thread with high Xenomai priority
andrewm@45 419 if(rt_task_create(&gRTAudioThread, gRTAudioThreadName, 0, BEAGLERT_AUDIO_PRIORITY, T_JOINABLE | T_FPU)) {
andrewm@0 420 cout << "Error: unable to create Xenomai audio thread" << endl;
andrewm@0 421 return -1;
andrewm@0 422 }
andrewm@0 423
andrewm@50 424 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
andrewm@45 425 // Create an interrupt which the audio thread receives from the PRU
andrewm@45 426 int result = 0;
andrewm@45 427 if((result = rt_intr_create(&gRTAudioInterrupt, gRTAudioInterruptName, PRU_RTAUDIO_IRQ, I_NOAUTOENA)) != 0) {
andrewm@45 428 cout << "Error: unable to create Xenomai interrupt for PRU (error " << result << ")" << endl;
andrewm@45 429 return -1;
andrewm@45 430 }
andrewm@50 431 #endif
andrewm@45 432
andrewm@0 433 // Start all RT threads
andrewm@0 434 if(rt_task_start(&gRTAudioThread, &audioLoop, 0)) {
andrewm@0 435 cout << "Error: unable to start Xenomai audio thread" << endl;
andrewm@0 436 return -1;
andrewm@0 437 }
andrewm@0 438
andrewm@0 439 // The user may have created other tasks. Start those also.
andrewm@0 440 vector<InternalAuxiliaryTask*>::iterator it;
giuliomoro@176 441 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
giuliomoro@177 442 int ret = BeagleRT_startAuxiliaryTask(*it);
giuliomoro@177 443 if(ret != 0)
giuliomoro@177 444 return -2;
andrewm@0 445 }
andrewm@0 446 return 0;
andrewm@0 447 }
andrewm@0 448
andrewm@0 449 // Stop the PRU-based audio from running and wait
andrewm@0 450 // for the tasks to complete before returning.
andrewm@0 451
andrewm@5 452 void BeagleRT_stopAudio()
andrewm@0 453 {
andrewm@0 454 // Tell audio thread to stop (if this hasn't been done already)
andrewm@0 455 gShouldStop = true;
andrewm@0 456
andrewm@5 457 if(gRTAudioVerbose)
andrewm@5 458 cout << "Stopping audio...\n";
andrewm@5 459
andrewm@0 460 // Now wait for threads to respond and actually stop...
andrewm@0 461 rt_task_join(&gRTAudioThread);
andrewm@0 462
andrewm@0 463 // Stop all the auxiliary threads too
andrewm@0 464 vector<InternalAuxiliaryTask*>::iterator it;
giuliomoro@176 465 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
andrewm@0 466 InternalAuxiliaryTask *taskStruct = *it;
andrewm@0 467
andrewm@0 468 // Wake up each thread and join it
andrewm@0 469 rt_task_resume(&(taskStruct->task));
andrewm@0 470 rt_task_join(&(taskStruct->task));
andrewm@0 471 }
andrewm@0 472 }
andrewm@0 473
andrewm@0 474 // Free any resources associated with PRU real-time audio
andrewm@5 475 void BeagleRT_cleanupAudio()
andrewm@0 476 {
andrewm@56 477 cleanup(&gContext, gUserData);
andrewm@0 478
andrewm@0 479 // Clean up the auxiliary tasks
andrewm@0 480 vector<InternalAuxiliaryTask*>::iterator it;
giuliomoro@176 481 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
andrewm@0 482 InternalAuxiliaryTask *taskStruct = *it;
andrewm@0 483
andrewm@45 484 // Delete the task
andrewm@45 485 rt_task_delete(&taskStruct->task);
andrewm@45 486
andrewm@0 487 // Free the name string and the struct itself
andrewm@0 488 free(taskStruct->name);
andrewm@0 489 free(taskStruct);
andrewm@0 490 }
giuliomoro@176 491 getAuxTasks().clear();
andrewm@0 492
andrewm@45 493 // Delete the audio task and its interrupt
andrewm@50 494 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
andrewm@45 495 rt_intr_delete(&gRTAudioInterrupt);
andrewm@50 496 #endif
andrewm@45 497 rt_task_delete(&gRTAudioThread);
andrewm@45 498
andrewm@0 499 if(gPRU != 0)
andrewm@0 500 delete gPRU;
andrewm@0 501 if(gAudioCodec != 0)
andrewm@0 502 delete gAudioCodec;
andrewm@0 503
andrewm@0 504 if(gAmplifierMutePin >= 0)
andrewm@0 505 gpio_unexport(gAmplifierMutePin);
andrewm@0 506 gAmplifierMutePin = -1;
andrewm@0 507 }
andrewm@0 508
andrewm@5 509 // Set the level of the DAC; affects all outputs (headphone, line, speaker)
andrewm@5 510 // 0dB is the maximum, -63.5dB is the minimum; 0.5dB steps
andrewm@5 511 int BeagleRT_setDACLevel(float decibels)
andrewm@5 512 {
andrewm@5 513 if(gAudioCodec == 0)
andrewm@5 514 return -1;
andrewm@5 515 return gAudioCodec->setDACVolume((int)floorf(decibels * 2.0 + 0.5));
andrewm@5 516 }
andrewm@5 517
andrewm@5 518 // Set the level of the ADC
andrewm@5 519 // 0dB is the maximum, -12dB is the minimum; 1.5dB steps
andrewm@5 520 int BeagleRT_setADCLevel(float decibels)
andrewm@5 521 {
andrewm@5 522 if(gAudioCodec == 0)
andrewm@5 523 return -1;
andrewm@5 524 return gAudioCodec->setADCVolume((int)floorf(decibels * 2.0 + 0.5));
andrewm@5 525 }
andrewm@5 526
giuliomoro@171 527 // Set the level of the Programmable Gain Amplifier
giuliomoro@171 528 // 59.5dB is maximum, 0dB is minimum; 0.5dB steps
giuliomoro@171 529 int BeagleRT_setPgaGain(float decibels, int channel){
giuliomoro@171 530 if(gAudioCodec == 0)
giuliomoro@171 531 return -1;
giuliomoro@171 532 return gAudioCodec->setPga(decibels, channel);
giuliomoro@171 533 }
giuliomoro@171 534
andrewm@5 535 // Set the level of the onboard headphone amplifier; affects headphone
andrewm@5 536 // output only (not line out or speaker)
andrewm@5 537 // 0dB is the maximum, -63.5dB is the minimum; 0.5dB steps
andrewm@5 538 int BeagleRT_setHeadphoneLevel(float decibels)
andrewm@5 539 {
andrewm@5 540 if(gAudioCodec == 0)
andrewm@5 541 return -1;
andrewm@5 542 return gAudioCodec->setHPVolume((int)floorf(decibels * 2.0 + 0.5));
andrewm@5 543 }
andrewm@5 544
andrewm@5 545 // Mute or unmute the onboard speaker amplifiers
andrewm@5 546 // mute == 0 means unmute; otherwise mute
andrewm@5 547 // Returns 0 on success
andrewm@5 548 int BeagleRT_muteSpeakers(int mute)
andrewm@5 549 {
andrewm@5 550 int pinValue = mute ? LOW : HIGH;
andrewm@5 551
andrewm@5 552 // Check that we have an enabled pin for controlling the mute
andrewm@5 553 if(gAmplifierMutePin < 0)
andrewm@5 554 return -1;
andrewm@5 555
andrewm@5 556 return gpio_set_value(gAmplifierMutePin, pinValue);
andrewm@5 557 }
andrewm@5 558
andrewm@0 559 // Set the verbosity level
andrewm@45 560 void BeagleRT_setVerboseLevel(int level)
andrewm@0 561 {
andrewm@0 562 gRTAudioVerbose = level;
andrewm@0 563 }