comparison core/RTAudio.cpp @ 16:670be80463a3 matrix_gpio

- analog matrixIn/matrixOut are now mapped as floats from 0 to 1 - use of an external PRU code can be enabled with -P <filename> - 16 channels of programmable GPIO can be accessed straight from render() either writing directly to the matrixGpio[] array or using digitalWrite(), digitalRead(), setDigitalDirection() macros from Utilities.h .
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 27 Apr 2015 13:01:57 +0100
parents 901d205d1a3c
children c98863e63174
comparison
equal deleted inserted replaced
15:901d205d1a3c 16:670be80463a3
54 54
55 // Flag which tells the audio task to stop 55 // Flag which tells the audio task to stop
56 bool gShouldStop = false; 56 bool gShouldStop = false;
57 57
58 // general settings 58 // general settings
59 char *gPRUFilename;//[256] = "pru_rtaudio.bin"; // path to PRU binary file
59 int gRTAudioVerbose = 0; // Verbosity level for debugging 60 int gRTAudioVerbose = 0; // Verbosity level for debugging
60 int gAmplifierMutePin = -1; 61 int gAmplifierMutePin = -1;
61 int gAmplifierShouldBeginMuted = 0; 62 int gAmplifierShouldBeginMuted = 0;
62 63
63 // Number of audio and matrix channels, globally accessible 64 // Number of audio and matrix channels, globally accessible
64 // At least gNumMatrixChannels needs to be global to be used 65 // At least gNumMatrixChannels and gNumMatrixGpioChannels need to be global to be used
65 // by the analogRead() and analogWrite() macros without creating 66 // by the analogRead() and analogWrite() and the digital macros without creating
66 // extra confusion in their use cases by passing this argument 67 // extra confusion in their use cases by passing this argument
67 int gNumAudioChannels = 0; 68 int gNumAudioChannels = 0;
68 int gNumMatrixChannels = 0; 69 int gNumMatrixChannels = 0;
70 int gNumMatrixGpioChannels = 0;
69 71
70 // initAudio() prepares the infrastructure for running PRU-based real-time 72 // initAudio() prepares the infrastructure for running PRU-based real-time
71 // audio, but does not actually start the calculations. 73 // audio, but does not actually start the calculations.
72 // periodSize indicates the number of _sensor_ frames per period: the audio period size 74 // periodSize indicates the number of _sensor_ frames per period: the audio period size
73 // is twice this value. In total, the audio latency in frames will be 4*periodSize, 75 // is twice this value. In total, the audio latency in frames will be 4*periodSize,
81 83
82 int BeagleRT_initAudio(RTAudioSettings *settings, void *userData) 84 int BeagleRT_initAudio(RTAudioSettings *settings, void *userData)
83 { 85 {
84 rt_print_auto_init(1); 86 rt_print_auto_init(1);
85 setVerboseLevel(settings->verbose); 87 setVerboseLevel(settings->verbose);
86 88 gPRUFilename=settings->pruFilename;
87 if(gRTAudioVerbose == 1) 89 if(gRTAudioVerbose == 1)
88 rt_printf("Running with Xenomai\n"); 90 rt_printf("Running with Xenomai\n");
89 91
90 if(gRTAudioVerbose) { 92 if(gRTAudioVerbose) {
91 cout << "Starting with period size " << settings->periodSize << "; "; 93 cout << "Starting with period size " << settings->periodSize << "; ";
104 gAmplifierMutePin = settings->ampMutePin; 106 gAmplifierMutePin = settings->ampMutePin;
105 gAmplifierShouldBeginMuted = settings->beginMuted; 107 gAmplifierShouldBeginMuted = settings->beginMuted;
106 108
107 if(gpio_export(settings->ampMutePin)) { 109 if(gpio_export(settings->ampMutePin)) {
108 if(gRTAudioVerbose) 110 if(gRTAudioVerbose)
109 cout << "Warning: couldn't export amplifier mute pin\n"; 111 cout << "Warning: couldn't export amplifier mute pin " << settings-> ampMutePin << "\n";
110 } 112 }
111 if(gpio_set_dir(settings->ampMutePin, OUTPUT_PIN)) { 113 if(gpio_set_dir(settings->ampMutePin, OUTPUT_PIN)) {
112 if(gRTAudioVerbose) 114 if(gRTAudioVerbose)
113 cout << "Couldn't set direction on amplifier mute pin\n"; 115 cout << "Couldn't set direction on amplifier mute pin\n";
114 return -1; 116 return -1;
140 142
141 // Use PRU for audio 143 // Use PRU for audio
142 gPRU = new PRU(); 144 gPRU = new PRU();
143 gAudioCodec = new I2c_Codec(); 145 gAudioCodec = new I2c_Codec();
144 146
145 if(gPRU->prepareGPIO(settings->useMatrix, 1, 1)) { 147 gNumMatrixGpioChannels = settings->useMatrixGpio ? settings->numMatrixGpioChannels : 0; //this is called here to make sure prepareGPIO initializes the appropriate GPIO pins
148 if(gPRU->prepareGPIO(settings->useMatrix, settings->useMatrixGpio, 1, 1)) {
146 cout << "Error: unable to prepare GPIO for PRU audio\n"; 149 cout << "Error: unable to prepare GPIO for PRU audio\n";
147 return 1; 150 return 1;
148 } 151 }
149 if(gPRU->initialise(0, settings->periodSize, settings->numMatrixChannels, true)) { 152 if(gPRU->initialise(0, settings->periodSize, settings->numMatrixChannels, true)) {
150 cout << "Error: unable to initialise PRU\n"; 153 cout << "Error: unable to initialise PRU\n";
175 matrixSampleRate = audioSampleRate * 4.0 / (float)settings->numMatrixChannels; 178 matrixSampleRate = audioSampleRate * 4.0 / (float)settings->numMatrixChannels;
176 } 179 }
177 180
178 gNumAudioChannels = 2; 181 gNumAudioChannels = 2;
179 gNumMatrixChannels = settings->useMatrix ? settings->numMatrixChannels : 0; 182 gNumMatrixChannels = settings->useMatrix ? settings->numMatrixChannels : 0;
180 183 if(!initialise_render(gNumMatrixChannels, gNumMatrixGpioChannels, gNumAudioChannels,
181 if(!initialise_render(gNumMatrixChannels, gNumAudioChannels,
182 settings->useMatrix ? settings->periodSize : 0, /* matrix period size */ 184 settings->useMatrix ? settings->periodSize : 0, /* matrix period size */
183 audioPeriodSize, 185 audioPeriodSize,
184 matrixSampleRate, audioSampleRate, 186 matrixSampleRate, audioSampleRate,
185 userData)) { 187 userData)) {
186 cout << "Couldn't initialise audio rendering\n"; 188 cout << "Couldn't initialise audio rendering\n";
206 if(gAudioCodec->startAudio(0)) { 208 if(gAudioCodec->startAudio(0)) {
207 rt_printf("Error: unable to start I2C audio codec\n"); 209 rt_printf("Error: unable to start I2C audio codec\n");
208 gShouldStop = 1; 210 gShouldStop = 1;
209 } 211 }
210 else { 212 else {
211 if(gPRU->start()) { 213 if(gPRU->start(gPRUFilename)) {
212 rt_printf("Error: unable to start PRU\n"); 214 rt_printf("Error: unable to start PRU from file %s\n", gPRUFilename);
213 gShouldStop = 1; 215 gShouldStop = 1;
214 } 216 }
215 else { 217 else {
216 // All systems go. Run the loop; it will end when gShouldStop is set to 1 218 // All systems go. Run the loop; it will end when gShouldStop is set to 1
217 219