annotate core/RTAudio.cpp @ 280:c55c6f6c233c prerelease

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