annotate core/RTAudio.cpp @ 256:3e93d9793da3 aux_task_args

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