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