comparison core/RTAudio.cpp @ 178:a156a694864d

-p flag now indicates the buffer size in audio samples. Closes #1520
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 02 Jan 2016 13:08:39 +0100
parents 3b364fb8c4ee
children e23c304d264f
comparison
equal deleted inserted replaced
177:3b364fb8c4ee 178:a156a694864d
78 // User data passed in from main() 78 // User data passed in from main()
79 void *gUserData; 79 void *gUserData;
80 80
81 // initAudio() prepares the infrastructure for running PRU-based real-time 81 // initAudio() prepares the infrastructure for running PRU-based real-time
82 // audio, but does not actually start the calculations. 82 // audio, but does not actually start the calculations.
83 // periodSize indicates the number of _sensor_ frames per period: the audio period size 83 // periodSize indicates the number of audio frames per period: the analog period size
84 // is twice this value. In total, the audio latency in frames will be 4*periodSize, 84 // will depend on the number of analog channels, in such a way that
85 // analogPeriodSize = 4*periodSize/numAnalogChannels
86 // In total, the audio latency in frames will be 2*periodSize,
85 // plus any latency inherent in the ADCs and DACs themselves. 87 // plus any latency inherent in the ADCs and DACs themselves.
86 // useAnalog indicates whether to enable the ADC and DAC or just use the audio codec. 88 // useAnalog indicates whether to enable the ADC and DAC or just use the audio codec.
87 // numAnalogChannels indicates how many ADC and DAC channels to use. 89 // numAnalogChannels indicates how many ADC and DAC channels to use.
88 // userData is an opaque pointer which will be passed through to the setup() 90 // userData is an opaque pointer which will be passed through to the setup()
89 // function for application-specific use 91 // function for application-specific use
140 else if(settings->numAnalogChannels >= 4) 142 else if(settings->numAnalogChannels >= 4)
141 settings->numAnalogChannels = 4; 143 settings->numAnalogChannels = 4;
142 else 144 else
143 settings->numAnalogChannels = 2; 145 settings->numAnalogChannels = 2;
144 146
145 // Sanity check the combination of channels and period size
146 if(settings->numAnalogChannels <= 4 && settings->periodSize < 2) {
147 cout << "Error: " << settings->numAnalogChannels << " channels and period size of " << settings->periodSize << " not supported.\n";
148 return 1;
149 }
150 if(settings->numAnalogChannels <= 2 && settings->periodSize < 4) {
151 cout << "Error: " << settings->numAnalogChannels << " channels and period size of " << settings->periodSize << " not supported.\n";
152 return 1;
153 }
154
155 // Initialise the rendering environment: sample rates, frame counts, numbers of channels 147 // Initialise the rendering environment: sample rates, frame counts, numbers of channels
156 gContext.audioSampleRate = 44100.0; 148 gContext.audioSampleRate = 44100.0;
157 gContext.audioChannels = 2; 149 gContext.audioChannels = 2;
158 150
159 if(settings->useAnalog) { 151 if(settings->useAnalog) {
160 gContext.audioFrames = settings->periodSize * settings->numAnalogChannels / 4; 152 gContext.audioFrames = settings->periodSize;
161 153
162 gContext.analogFrames = settings->periodSize; 154 gContext.analogFrames = gContext.audioFrames * 4 / settings->numAnalogChannels;
163 gContext.analogChannels = settings->numAnalogChannels; 155 gContext.analogChannels = settings->numAnalogChannels;
164 gContext.analogSampleRate = gContext.audioSampleRate * 4.0 / (float)settings->numAnalogChannels; 156 gContext.analogSampleRate = gContext.audioSampleRate * 4.0 / (float)settings->numAnalogChannels;
165 } 157 }
166 else { 158 else {
167 gContext.audioFrames = settings->periodSize * 2; 159 gContext.audioFrames = settings->periodSize;
168 160
169 gContext.analogFrames = 0; 161 gContext.analogFrames = 0;
170 gContext.analogChannels = 0; 162 gContext.analogChannels = 0;
171 gContext.analogSampleRate = 0; 163 gContext.analogSampleRate = 0;
164 }
165
166 // Sanity check the combination of channels and period size
167 if( (gContext.analogChannels <= 4 && gContext.analogFrames < 2) ||
168 (gContext.analogChannels <= 2 && gContext.analogFrames < 4))
169 {
170 cout << "Error: " << gContext.analogChannels << " channels and period size of " << gContext.analogFrames << " not supported.\n";
171 return 1;
172 } 172 }
173 173
174 // For now, digital frame rate is equal to audio frame rate 174 // For now, digital frame rate is equal to audio frame rate
175 if(settings->useDigital) { 175 if(settings->useDigital) {
176 gContext.digitalFrames = gContext.audioFrames; 176 gContext.digitalFrames = gContext.audioFrames;
198 cout << "Error: unable to prepare GPIO for PRU audio\n"; 198 cout << "Error: unable to prepare GPIO for PRU audio\n";
199 return 1; 199 return 1;
200 } 200 }
201 201
202 // Get the PRU memory buffers ready to go 202 // Get the PRU memory buffers ready to go
203 if(gPRU->initialise(0, settings->periodSize, settings->numAnalogChannels, true)) { 203 if(gPRU->initialise(0, gContext.analogFrames, gContext.analogChannels, true)) {
204 cout << "Error: unable to initialise PRU\n"; 204 cout << "Error: unable to initialise PRU\n";
205 return 1; 205 return 1;
206 } 206 }
207 207
208 // Prepare the audio codec, which clocks the whole system 208 // Prepare the audio codec, which clocks the whole system