annotate core/RTAudio.cpp @ 537:bfcbeb437869 API-update

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