andrewm@0: /* andrewm@0: * RTAudio.cpp andrewm@0: * andrewm@0: * Central control code for hard real-time audio on BeagleBone Black andrewm@0: * using PRU and Xenomai Linux extensions. This code began as part andrewm@0: * of the Hackable Instruments project (EPSRC) at Queen Mary University andrewm@0: * of London, 2013-14. andrewm@0: * andrewm@0: * (c) 2014 Victor Zappi and Andrew McPherson andrewm@0: * Queen Mary University of London andrewm@0: */ andrewm@0: andrewm@0: andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: andrewm@0: // Xenomai-specific includes andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@45: #include andrewm@0: #include andrewm@0: giuliomoro@301: #include "../include/Bela.h" andrewm@0: #include "../include/PRU.h" andrewm@0: #include "../include/I2c_Codec.h" andrewm@0: #include "../include/GPIOcontrol.h" andrewm@0: andrewm@45: // ARM interrupt number for PRU event EVTOUT7 andrewm@45: #define PRU_RTAUDIO_IRQ 21 andrewm@45: andrewm@0: using namespace std; andrewm@0: andrewm@0: // Data structure to keep track of auxiliary tasks we andrewm@0: // can schedule andrewm@0: typedef struct { andrewm@0: RT_TASK task; l@256: void (*argfunction)(void*); l@256: void (*function)(void); andrewm@0: char *name; andrewm@0: int priority; giuliomoro@174: bool started; l@256: bool hasArgs; l@254: void* args; l@258: bool autoSchedule; andrewm@0: } InternalAuxiliaryTask; andrewm@0: andrewm@0: // Real-time tasks and objects andrewm@0: RT_TASK gRTAudioThread; andrewm@307: const char gRTAudioThreadName[] = "bela-audio"; andrewm@307: andrewm@303: #ifdef BELA_USE_XENOMAI_INTERRUPTS andrewm@45: RT_INTR gRTAudioInterrupt; andrewm@307: const char gRTAudioInterruptName[] = "bela-pru-irq"; andrewm@50: #endif andrewm@307: andrewm@0: PRU *gPRU = 0; andrewm@0: I2c_Codec *gAudioCodec = 0; andrewm@0: giuliomoro@176: vector &getAuxTasks(){ giuliomoro@176: static vector auxTasks; giuliomoro@176: return auxTasks; giuliomoro@176: } andrewm@0: andrewm@0: // Flag which tells the audio task to stop giuliomoro@233: int gShouldStop = false; andrewm@0: andrewm@0: // general settings andrewm@45: char gPRUFilename[MAX_PRU_FILENAME_LENGTH]; // Path to PRU binary file (internal code if empty)_ andrewm@0: int gRTAudioVerbose = 0; // Verbosity level for debugging andrewm@0: int gAmplifierMutePin = -1; andrewm@5: int gAmplifierShouldBeginMuted = 0; andrewm@0: andrewm@45: // Context which holds all the audio/sensor data passed to the render routines andrewm@307: InternalBelaContext gContext; andrewm@45: andrewm@45: // User data passed in from main() andrewm@45: void *gUserData; andrewm@0: andrewm@0: // initAudio() prepares the infrastructure for running PRU-based real-time andrewm@0: // audio, but does not actually start the calculations. giuliomoro@178: // periodSize indicates the number of audio frames per period: the analog period size giuliomoro@178: // will depend on the number of analog channels, in such a way that giuliomoro@178: // analogPeriodSize = 4*periodSize/numAnalogChannels giuliomoro@178: // In total, the audio latency in frames will be 2*periodSize, andrewm@0: // plus any latency inherent in the ADCs and DACs themselves. giuliomoro@19: // useAnalog indicates whether to enable the ADC and DAC or just use the audio codec. giuliomoro@19: // numAnalogChannels indicates how many ADC and DAC channels to use. andrewm@56: // userData is an opaque pointer which will be passed through to the setup() andrewm@0: // function for application-specific use andrewm@0: // andrewm@0: // Returns 0 on success. andrewm@0: giuliomoro@301: int Bela_initAudio(BelaInitSettings *settings, void *userData) andrewm@0: { andrewm@381: // First check if there's a Bela program already running on the board. andrewm@381: // We can't have more than one instance at a time, but we can tell via andrewm@381: // the Xenomai task info. We expect the rt_task_bind call to fail so if it andrewm@381: // doesn't then it means something else is running. andrewm@381: RT_TASK otherBelaTask; andrewm@381: int returnVal = rt_task_bind(&otherBelaTask, gRTAudioThreadName, TM_NONBLOCK); andrewm@381: if(returnVal == 0) { andrewm@381: cout << "Error: Bela is already running in another process. Cannot start.\n"; andrewm@381: rt_task_unbind(&otherBelaTask); andrewm@381: return -1; andrewm@381: } andrewm@381: else if(returnVal != -EWOULDBLOCK && returnVal != -ETIMEDOUT) { andrewm@381: cout << "Error " << returnVal << " occurred determining if another Bela task is running.\n"; andrewm@381: return -1; andrewm@381: } andrewm@381: andrewm@280: // Sanity checks andrewm@280: if(settings->pruNumber < 0 || settings->pruNumber > 1) { andrewm@280: cout << "Invalid PRU number " << settings->pruNumber << endl; andrewm@280: return -1; andrewm@280: } andrewm@280: if(settings->pruNumber != 1 && settings->numMuxChannels != 0) { andrewm@280: cout << "Incompatible settings: multiplexer can only be run using PRU 1\n"; andrewm@280: return -1; andrewm@280: } andrewm@280: andrewm@0: rt_print_auto_init(1); andrewm@45: giuliomoro@301: Bela_setVerboseLevel(settings->verbose); andrewm@45: strncpy(gPRUFilename, settings->pruFilename, MAX_PRU_FILENAME_LENGTH); andrewm@45: gUserData = userData; andrewm@45: andrewm@45: // Initialise context data structure giuliomoro@301: memset(&gContext, 0, sizeof(BelaContext)); andrewm@0: andrewm@5: if(gRTAudioVerbose) { andrewm@5: cout << "Starting with period size " << settings->periodSize << "; "; giuliomoro@19: if(settings->useAnalog) giuliomoro@19: cout << "analog enabled\n"; andrewm@5: else giuliomoro@19: cout << "analog disabled\n"; andrewm@5: cout << "DAC level " << settings->dacLevel << "dB; ADC level " << settings->adcLevel; andrewm@5: cout << "dB; headphone level " << settings->headphoneLevel << "dB\n"; andrewm@5: if(settings->beginMuted) andrewm@5: cout << "Beginning with speaker muted\n"; andrewm@5: } andrewm@0: andrewm@0: // Prepare GPIO pins for amplifier mute and status LED andrewm@5: if(settings->ampMutePin >= 0) { andrewm@5: gAmplifierMutePin = settings->ampMutePin; andrewm@5: gAmplifierShouldBeginMuted = settings->beginMuted; andrewm@0: andrewm@5: if(gpio_export(settings->ampMutePin)) { andrewm@0: if(gRTAudioVerbose) giuliomoro@16: cout << "Warning: couldn't export amplifier mute pin " << settings-> ampMutePin << "\n"; andrewm@0: } andrewm@5: if(gpio_set_dir(settings->ampMutePin, OUTPUT_PIN)) { andrewm@0: if(gRTAudioVerbose) andrewm@0: cout << "Couldn't set direction on amplifier mute pin\n"; andrewm@0: return -1; andrewm@0: } andrewm@5: if(gpio_set_value(settings->ampMutePin, LOW)) { andrewm@0: if(gRTAudioVerbose) andrewm@0: cout << "Couldn't set value on amplifier mute pin\n"; andrewm@0: return -1; andrewm@0: } andrewm@0: } andrewm@0: giuliomoro@537: if(settings->numAnalogInChannels != settings->numAnalogOutChannels){ giuliomoro@537: printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n"); giuliomoro@537: return 1; giuliomoro@537: } giuliomoro@537: unsigned int numAnalogChannels = settings->numAnalogInChannels; giuliomoro@19: // Limit the analog channels to sane values giuliomoro@537: if(numAnalogChannels != 2 giuliomoro@537: && numAnalogChannels != 4 giuliomoro@537: && numAnalogChannels != 8) { giuliomoro@537: cout << "Invalid number of analog channels: " << numAnalogChannels << ". Valid values are 2, 4, 8.\n"; andrewm@373: return -1; andrewm@373: } andrewm@12: andrewm@45: // Initialise the rendering environment: sample rates, frame counts, numbers of channels andrewm@45: gContext.audioSampleRate = 44100.0; giuliomoro@528: giuliomoro@528: // TODO: settings a different number of channels for inputs and outputs is not yet supported giuliomoro@528: gContext.audioInChannels = 2; giuliomoro@528: gContext.audioOutChannels = 2; andrewm@45: andrewm@45: if(settings->useAnalog) { giuliomoro@178: gContext.audioFrames = settings->periodSize; andrewm@45: giuliomoro@537: // TODO: a different number of channels for inputs and outputs is not yet supported giuliomoro@537: gContext.analogFrames = gContext.audioFrames * 4 / settings->numAnalogInChannels; giuliomoro@537: gContext.analogInChannels = settings->numAnalogInChannels; giuliomoro@537: gContext.analogOutChannels = settings->numAnalogOutChannels; giuliomoro@537: unsigned int numAnalogChannelsForSampleRate = settings->numAnalogInChannels; giuliomoro@537: gContext.analogSampleRate = gContext.audioSampleRate * 4.0 / (float)numAnalogChannelsForSampleRate; andrewm@45: } andrewm@45: else { giuliomoro@178: gContext.audioFrames = settings->periodSize; andrewm@45: andrewm@45: gContext.analogFrames = 0; giuliomoro@528: gContext.analogInChannels = 0; giuliomoro@528: gContext.analogOutChannels = 0; andrewm@45: gContext.analogSampleRate = 0; andrewm@45: } andrewm@45: giuliomoro@528: if(gContext.analogInChannels != gContext.analogOutChannels){ giuliomoro@528: printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n"); giuliomoro@528: return -1; giuliomoro@528: } giuliomoro@528: unsigned int analogChannels = gContext.analogInChannels; giuliomoro@178: // Sanity check the combination of channels and period size giuliomoro@528: if( analogChannels != 0 && ((analogChannels <= 4 && gContext.analogFrames < 2) || giuliomoro@528: (analogChannels <= 2 && gContext.analogFrames < 4)) ) giuliomoro@178: { giuliomoro@528: cout << "Error: " << analogChannels << " channels and period size of " << gContext.analogFrames << " not supported.\n"; giuliomoro@178: return 1; giuliomoro@178: } giuliomoro@178: andrewm@45: // For now, digital frame rate is equal to audio frame rate andrewm@45: if(settings->useDigital) { andrewm@45: gContext.digitalFrames = gContext.audioFrames; andrewm@45: gContext.digitalSampleRate = gContext.audioSampleRate; andrewm@45: gContext.digitalChannels = settings->numDigitalChannels; andrewm@45: } andrewm@45: else { andrewm@45: gContext.digitalFrames = 0; andrewm@45: gContext.digitalSampleRate = 0; andrewm@45: gContext.digitalChannels = 0; andrewm@45: } andrewm@45: andrewm@45: // Set flags based on init settings andrewm@45: if(settings->interleave) andrewm@303: gContext.flags |= BELA_FLAG_INTERLEAVED; andrewm@45: if(settings->analogOutputsPersist) andrewm@303: gContext.flags |= BELA_FLAG_ANALOG_OUTPUTS_PERSIST; andrewm@45: andrewm@0: // Use PRU for audio andrewm@45: gPRU = new PRU(&gContext); andrewm@0: gAudioCodec = new I2c_Codec(); andrewm@0: andrewm@45: // Initialise the GPIO pins, including possibly the digital pins in the render routines andrewm@45: if(gPRU->prepareGPIO(1, 1)) { andrewm@0: cout << "Error: unable to prepare GPIO for PRU audio\n"; andrewm@0: return 1; andrewm@0: } andrewm@280: andrewm@45: // Get the PRU memory buffers ready to go giuliomoro@528: if(gContext.analogInChannels != gContext.analogOutChannels){ giuliomoro@528: printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n"); giuliomoro@528: return 1; giuliomoro@528: } giuliomoro@528: giuliomoro@528: if(gPRU->initialise(settings->pruNumber, gContext.analogFrames, analogChannels, andrewm@280: settings->numMuxChannels, true)) { andrewm@0: cout << "Error: unable to initialise PRU\n"; andrewm@0: return 1; andrewm@0: } andrewm@45: andrewm@45: // Prepare the audio codec, which clocks the whole system andrewm@5: if(gAudioCodec->initI2C_RW(2, settings->codecI2CAddress, -1)) { andrewm@0: cout << "Unable to open codec I2C\n"; andrewm@0: return 1; andrewm@0: } andrewm@0: if(gAudioCodec->initCodec()) { andrewm@0: cout << "Error: unable to initialise audio codec\n"; andrewm@0: return 1; andrewm@0: } giuliomoro@172: andrewm@5: // Set default volume levels giuliomoro@301: Bela_setDACLevel(settings->dacLevel); giuliomoro@301: Bela_setADCLevel(settings->adcLevel); giuliomoro@174: // TODO: add more argument checks giuliomoro@171: for(int n = 0; n < 2; n++){ giuliomoro@172: if(settings->pgaGain[n] > 59.5){ giuliomoro@172: std::cerr << "PGA gain out of range [0,59.5]\n"; giuliomoro@172: exit(1); giuliomoro@172: } giuliomoro@301: Bela_setPgaGain(settings->pgaGain[n], n); giuliomoro@171: } giuliomoro@301: Bela_setHeadphoneLevel(settings->headphoneLevel); andrewm@5: andrewm@45: // Call the user-defined initialisation function andrewm@307: if(!setup((BelaContext *)&gContext, userData)) { andrewm@0: cout << "Couldn't initialise audio rendering\n"; andrewm@0: return 1; andrewm@0: } andrewm@0: andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: // audioLoop() is the main function which starts the PRU audio code andrewm@0: // and then transfers control to the PRU object. The PRU object in andrewm@0: // turn will call the audio render() callback function every time andrewm@0: // there is new data to process. andrewm@0: andrewm@0: void audioLoop(void *) andrewm@0: { andrewm@0: if(gRTAudioVerbose==1) andrewm@0: rt_printf("_________________Audio Thread!\n"); andrewm@0: andrewm@0: // PRU audio andrewm@0: assert(gAudioCodec != 0 && gPRU != 0); andrewm@0: andrewm@0: if(gAudioCodec->startAudio(0)) { andrewm@0: rt_printf("Error: unable to start I2C audio codec\n"); andrewm@0: gShouldStop = 1; andrewm@0: } andrewm@0: else { giuliomoro@16: if(gPRU->start(gPRUFilename)) { giuliomoro@16: rt_printf("Error: unable to start PRU from file %s\n", gPRUFilename); andrewm@0: gShouldStop = 1; andrewm@0: } andrewm@0: else { andrewm@0: // All systems go. Run the loop; it will end when gShouldStop is set to 1 andrewm@5: andrewm@5: if(!gAmplifierShouldBeginMuted) { andrewm@5: // First unmute the amplifier giuliomoro@301: if(Bela_muteSpeakers(0)) { andrewm@5: if(gRTAudioVerbose) andrewm@5: rt_printf("Warning: couldn't set value (high) on amplifier mute pin\n"); andrewm@5: } andrewm@0: } andrewm@0: andrewm@303: #ifdef BELA_USE_XENOMAI_INTERRUPTS andrewm@45: gPRU->loop(&gRTAudioInterrupt, gUserData); andrewm@50: #else andrewm@50: gPRU->loop(0, gUserData); andrewm@50: #endif andrewm@0: // Now clean up andrewm@0: // gPRU->waitForFinish(); andrewm@0: gPRU->disable(); andrewm@0: gAudioCodec->stopAudio(); andrewm@0: gPRU->cleanupGPIO(); andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: if(gRTAudioVerbose == 1) andrewm@0: rt_printf("audio thread ended\n"); andrewm@0: } andrewm@0: andrewm@0: // Create a calculation loop which can run independently of the audio, at a different andrewm@303: // (equal or lower) priority. Audio priority is defined in BELA_AUDIO_PRIORITY; andrewm@45: // priority should be generally be less than this. andrewm@0: // Returns an (opaque) pointer to the created task on success; 0 on failure giuliomoro@301: AuxiliaryTask Bela_createAuxiliaryTask(void (*functionToCall)(void* args), int priority, const char *name, void* args, bool autoSchedule) andrewm@0: { andrewm@0: InternalAuxiliaryTask *newTask = (InternalAuxiliaryTask*)malloc(sizeof(InternalAuxiliaryTask)); andrewm@0: andrewm@0: // Attempt to create the task andrewm@0: if(rt_task_create(&(newTask->task), name, 0, priority, T_JOINABLE | T_FPU)) { andrewm@0: cout << "Error: unable to create auxiliary task " << name << endl; andrewm@0: free(newTask); andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: // Populate the rest of the data structure and store it in the vector l@256: newTask->argfunction = functionToCall; andrewm@0: newTask->name = strdup(name); andrewm@0: newTask->priority = priority; giuliomoro@174: newTask->started = false; l@254: newTask->args = args; l@256: newTask->hasArgs = true; l@258: newTask->autoSchedule = autoSchedule; l@258: giuliomoro@176: getAuxTasks().push_back(newTask); andrewm@0: andrewm@0: return (AuxiliaryTask)newTask; andrewm@0: } giuliomoro@301: AuxiliaryTask Bela_createAuxiliaryTask(void (*functionToCall)(void), int priority, const char *name, bool autoSchedule) l@256: { l@256: InternalAuxiliaryTask *newTask = (InternalAuxiliaryTask*)malloc(sizeof(InternalAuxiliaryTask)); l@258: l@256: // Attempt to create the task l@256: if(rt_task_create(&(newTask->task), name, 0, priority, T_JOINABLE | T_FPU)) { l@256: cout << "Error: unable to create auxiliary task " << name << endl; l@256: free(newTask); l@256: return 0; l@256: } l@258: l@256: // Populate the rest of the data structure and store it in the vector l@256: newTask->function = functionToCall; l@256: newTask->name = strdup(name); l@256: newTask->priority = priority; l@256: newTask->started = false; l@256: newTask->hasArgs = false; l@258: newTask->autoSchedule = autoSchedule; l@258: l@256: getAuxTasks().push_back(newTask); l@258: l@256: return (AuxiliaryTask)newTask; l@256: } andrewm@0: giuliomoro@174: // Schedule a previously created (and started) auxiliary task. It will run when the priority rules next andrewm@0: // allow it to be scheduled. giuliomoro@301: void Bela_scheduleAuxiliaryTask(AuxiliaryTask task) andrewm@0: { andrewm@0: InternalAuxiliaryTask *taskToSchedule = (InternalAuxiliaryTask *)task; giuliomoro@174: if(taskToSchedule->started == false){ // Note: this is not the safest method to check if a task giuliomoro@301: Bela_startAuxiliaryTask(task); // is started (or ready to be resumed), but it probably is the fastest. giuliomoro@174: // A safer approach would use rt_task_inquire() giuliomoro@174: } andrewm@0: rt_task_resume(&taskToSchedule->task); andrewm@0: } giuliomoro@301: void Bela_autoScheduleAuxiliaryTasks(){ l@258: vector::iterator it; l@258: for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) { l@258: if ((InternalAuxiliaryTask *)(*it)->autoSchedule){ giuliomoro@301: Bela_scheduleAuxiliaryTask(*it); l@258: } l@258: } l@258: } andrewm@0: andrewm@0: // Calculation loop that can be used for other tasks running at a lower andrewm@0: // priority than the audio thread. Simple wrapper for Xenomai calls. andrewm@0: // Treat the argument as containing the task structure andrewm@0: void auxiliaryTaskLoop(void *taskStruct) andrewm@0: { l@256: InternalAuxiliaryTask *task = ((InternalAuxiliaryTask *)taskStruct); l@256: andrewm@0: // Get function to call from the argument l@256: void (*auxiliary_argfunction)(void* args) = task->argfunction; l@256: void (*auxiliary_function)(void) = task->function; l@256: l@258: // get the task's name l@256: const char *name = task->name; andrewm@0: andrewm@0: // Wait for a notification andrewm@0: rt_task_suspend(NULL); andrewm@0: andrewm@0: while(!gShouldStop) { andrewm@0: // Then run the calculations l@256: if (task->hasArgs) l@256: auxiliary_argfunction(task->args); l@256: else l@256: auxiliary_function(); andrewm@0: andrewm@0: // Wait for a notification andrewm@0: rt_task_suspend(NULL); andrewm@0: } andrewm@0: andrewm@0: if(gRTAudioVerbose == 1) andrewm@0: rt_printf("auxiliary task %s ended\n", name); andrewm@0: } andrewm@0: giuliomoro@174: giuliomoro@301: int Bela_startAuxiliaryTask(AuxiliaryTask task){ giuliomoro@174: InternalAuxiliaryTask *taskStruct; giuliomoro@174: taskStruct = (InternalAuxiliaryTask *)task; giuliomoro@174: if(taskStruct->started == true) giuliomoro@174: return 0; giuliomoro@174: if(rt_task_start(&(taskStruct->task), &auxiliaryTaskLoop, taskStruct)) { giuliomoro@174: cerr << "Error: unable to start Xenomai task " << taskStruct->name << endl; giuliomoro@174: return -1; giuliomoro@174: } giuliomoro@174: taskStruct->started = true; giuliomoro@174: return 0; giuliomoro@174: } giuliomoro@174: andrewm@0: // startAudio() should be called only after initAudio() successfully completes. andrewm@0: // It launches the real-time Xenomai task which runs the audio loop. Returns 0 andrewm@0: // on success. andrewm@0: giuliomoro@301: int Bela_startAudio() andrewm@0: { andrewm@45: // Create audio thread with high Xenomai priority andrewm@303: if(rt_task_create(&gRTAudioThread, gRTAudioThreadName, 0, BELA_AUDIO_PRIORITY, T_JOINABLE | T_FPU)) { andrewm@0: cout << "Error: unable to create Xenomai audio thread" << endl; andrewm@0: return -1; andrewm@0: } andrewm@0: andrewm@303: #ifdef BELA_USE_XENOMAI_INTERRUPTS andrewm@45: // Create an interrupt which the audio thread receives from the PRU andrewm@45: int result = 0; andrewm@45: if((result = rt_intr_create(&gRTAudioInterrupt, gRTAudioInterruptName, PRU_RTAUDIO_IRQ, I_NOAUTOENA)) != 0) { andrewm@45: cout << "Error: unable to create Xenomai interrupt for PRU (error " << result << ")" << endl; andrewm@45: return -1; andrewm@45: } andrewm@50: #endif andrewm@45: andrewm@0: // Start all RT threads andrewm@0: if(rt_task_start(&gRTAudioThread, &audioLoop, 0)) { andrewm@0: cout << "Error: unable to start Xenomai audio thread" << endl; andrewm@0: return -1; andrewm@0: } andrewm@0: andrewm@0: // The user may have created other tasks. Start those also. andrewm@0: vector::iterator it; giuliomoro@176: for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) { giuliomoro@301: int ret = Bela_startAuxiliaryTask(*it); giuliomoro@177: if(ret != 0) giuliomoro@177: return -2; andrewm@0: } andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: // Stop the PRU-based audio from running and wait andrewm@0: // for the tasks to complete before returning. andrewm@0: giuliomoro@301: void Bela_stopAudio() andrewm@0: { andrewm@0: // Tell audio thread to stop (if this hasn't been done already) andrewm@0: gShouldStop = true; andrewm@0: andrewm@5: if(gRTAudioVerbose) andrewm@5: cout << "Stopping audio...\n"; andrewm@5: andrewm@0: // Now wait for threads to respond and actually stop... andrewm@0: rt_task_join(&gRTAudioThread); andrewm@0: andrewm@0: // Stop all the auxiliary threads too andrewm@0: vector::iterator it; giuliomoro@176: for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) { andrewm@0: InternalAuxiliaryTask *taskStruct = *it; andrewm@0: andrewm@0: // Wake up each thread and join it andrewm@0: rt_task_resume(&(taskStruct->task)); andrewm@0: rt_task_join(&(taskStruct->task)); andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: // Free any resources associated with PRU real-time audio giuliomoro@301: void Bela_cleanupAudio() andrewm@0: { andrewm@307: cleanup((BelaContext *)&gContext, gUserData); andrewm@0: andrewm@0: // Clean up the auxiliary tasks andrewm@0: vector::iterator it; giuliomoro@176: for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) { andrewm@0: InternalAuxiliaryTask *taskStruct = *it; andrewm@0: andrewm@45: // Delete the task andrewm@45: rt_task_delete(&taskStruct->task); andrewm@45: andrewm@0: // Free the name string and the struct itself andrewm@0: free(taskStruct->name); andrewm@0: free(taskStruct); andrewm@0: } giuliomoro@176: getAuxTasks().clear(); andrewm@0: andrewm@45: // Delete the audio task and its interrupt andrewm@303: #ifdef BELA_USE_XENOMAI_INTERRUPTS andrewm@45: rt_intr_delete(&gRTAudioInterrupt); andrewm@50: #endif andrewm@45: rt_task_delete(&gRTAudioThread); andrewm@45: andrewm@0: if(gPRU != 0) andrewm@0: delete gPRU; andrewm@0: if(gAudioCodec != 0) andrewm@0: delete gAudioCodec; andrewm@0: andrewm@0: if(gAmplifierMutePin >= 0) andrewm@0: gpio_unexport(gAmplifierMutePin); andrewm@0: gAmplifierMutePin = -1; andrewm@0: } andrewm@0: andrewm@5: // Set the level of the DAC; affects all outputs (headphone, line, speaker) andrewm@5: // 0dB is the maximum, -63.5dB is the minimum; 0.5dB steps giuliomoro@301: int Bela_setDACLevel(float decibels) andrewm@5: { andrewm@5: if(gAudioCodec == 0) andrewm@5: return -1; andrewm@5: return gAudioCodec->setDACVolume((int)floorf(decibels * 2.0 + 0.5)); andrewm@5: } andrewm@5: andrewm@5: // Set the level of the ADC andrewm@5: // 0dB is the maximum, -12dB is the minimum; 1.5dB steps giuliomoro@301: int Bela_setADCLevel(float decibels) andrewm@5: { andrewm@5: if(gAudioCodec == 0) andrewm@5: return -1; andrewm@5: return gAudioCodec->setADCVolume((int)floorf(decibels * 2.0 + 0.5)); andrewm@5: } andrewm@5: giuliomoro@171: // Set the level of the Programmable Gain Amplifier giuliomoro@171: // 59.5dB is maximum, 0dB is minimum; 0.5dB steps giuliomoro@301: int Bela_setPgaGain(float decibels, int channel){ giuliomoro@171: if(gAudioCodec == 0) giuliomoro@171: return -1; giuliomoro@171: return gAudioCodec->setPga(decibels, channel); giuliomoro@171: } giuliomoro@171: andrewm@5: // Set the level of the onboard headphone amplifier; affects headphone andrewm@5: // output only (not line out or speaker) andrewm@5: // 0dB is the maximum, -63.5dB is the minimum; 0.5dB steps giuliomoro@301: int Bela_setHeadphoneLevel(float decibels) andrewm@5: { andrewm@5: if(gAudioCodec == 0) andrewm@5: return -1; andrewm@5: return gAudioCodec->setHPVolume((int)floorf(decibels * 2.0 + 0.5)); andrewm@5: } andrewm@5: andrewm@5: // Mute or unmute the onboard speaker amplifiers andrewm@5: // mute == 0 means unmute; otherwise mute andrewm@5: // Returns 0 on success giuliomoro@301: int Bela_muteSpeakers(int mute) andrewm@5: { andrewm@5: int pinValue = mute ? LOW : HIGH; andrewm@5: andrewm@5: // Check that we have an enabled pin for controlling the mute andrewm@5: if(gAmplifierMutePin < 0) andrewm@5: return -1; andrewm@5: andrewm@5: return gpio_set_value(gAmplifierMutePin, pinValue); andrewm@5: } andrewm@5: andrewm@0: // Set the verbosity level giuliomoro@301: void Bela_setVerboseLevel(int level) andrewm@0: { andrewm@0: gRTAudioVerbose = level; andrewm@0: }