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