Mercurial > hg > beaglert
comparison core/RTAudio.cpp @ 12:a6beeba3a648
Initial support for higher matrix sample rates by reducing the number of channels. Input not tested yet, and not all examples updated to new format.
author | andrewm |
---|---|
date | Thu, 22 Jan 2015 19:00:22 +0000 |
parents | 09f03ac40fcc |
children | 6adb088196a7 |
comparison
equal
deleted
inserted
replaced
11:517715b23df0 | 12:a6beeba3a648 |
---|---|
65 // initAudio() prepares the infrastructure for running PRU-based real-time | 65 // initAudio() prepares the infrastructure for running PRU-based real-time |
66 // audio, but does not actually start the calculations. | 66 // audio, but does not actually start the calculations. |
67 // periodSize indicates the number of _sensor_ frames per period: the audio period size | 67 // periodSize indicates the number of _sensor_ frames per period: the audio period size |
68 // is twice this value. In total, the audio latency in frames will be 4*periodSize, | 68 // is twice this value. In total, the audio latency in frames will be 4*periodSize, |
69 // plus any latency inherent in the ADCs and DACs themselves. | 69 // plus any latency inherent in the ADCs and DACs themselves. |
70 // useMatrix indicates whether to use the ADC and DAC or just the audio codec. | 70 // useMatrix indicates whether to enable the ADC and DAC or just use the audio codec. |
71 // numMatrixChannels indicates how many ADC and DAC channels to use. | |
71 // userData is an opaque pointer which will be passed through to the initialise_render() | 72 // userData is an opaque pointer which will be passed through to the initialise_render() |
72 // function for application-specific use | 73 // function for application-specific use |
73 // | 74 // |
74 // Returns 0 on success. | 75 // Returns 0 on success. |
75 | 76 |
112 cout << "Couldn't set value on amplifier mute pin\n"; | 113 cout << "Couldn't set value on amplifier mute pin\n"; |
113 return -1; | 114 return -1; |
114 } | 115 } |
115 } | 116 } |
116 | 117 |
118 // Limit the matrix channels to sane values | |
119 if(settings->numMatrixChannels >= 8) | |
120 settings->numMatrixChannels = 8; | |
121 else if(settings->numMatrixChannels >= 4) | |
122 settings->numMatrixChannels = 4; | |
123 else | |
124 settings->numMatrixChannels = 2; | |
125 | |
126 // Sanity check the combination of channels and period size | |
127 if(settings->numMatrixChannels <= 4 && settings->periodSize < 2) { | |
128 cout << "Error: " << settings->numMatrixChannels << " channels and period size of " << settings->periodSize << " not supported.\n"; | |
129 return 1; | |
130 } | |
131 if(settings->numMatrixChannels <= 2 && settings->periodSize < 4) { | |
132 cout << "Error: " << settings->numMatrixChannels << " channels and period size of " << settings->periodSize << " not supported.\n"; | |
133 return 1; | |
134 } | |
135 | |
117 // Use PRU for audio | 136 // Use PRU for audio |
118 gPRU = new PRU(); | 137 gPRU = new PRU(); |
119 gAudioCodec = new I2c_Codec(); | 138 gAudioCodec = new I2c_Codec(); |
120 | 139 |
121 if(gPRU->prepareGPIO(settings->useMatrix, 1, 1)) { | 140 if(gPRU->prepareGPIO(settings->useMatrix, 1, 1)) { |
122 cout << "Error: unable to prepare GPIO for PRU audio\n"; | 141 cout << "Error: unable to prepare GPIO for PRU audio\n"; |
123 return 1; | 142 return 1; |
124 } | 143 } |
125 if(gPRU->initialise(0, settings->periodSize, true)) { | 144 if(gPRU->initialise(0, settings->periodSize, settings->numMatrixChannels, true)) { |
126 cout << "Error: unable to initialise PRU\n"; | 145 cout << "Error: unable to initialise PRU\n"; |
127 return 1; | 146 return 1; |
128 } | 147 } |
129 if(gAudioCodec->initI2C_RW(2, settings->codecI2CAddress, -1)) { | 148 if(gAudioCodec->initI2C_RW(2, settings->codecI2CAddress, -1)) { |
130 cout << "Unable to open codec I2C\n"; | 149 cout << "Unable to open codec I2C\n"; |
138 // Set default volume levels | 157 // Set default volume levels |
139 BeagleRT_setDACLevel(settings->dacLevel); | 158 BeagleRT_setDACLevel(settings->dacLevel); |
140 BeagleRT_setADCLevel(settings->adcLevel); | 159 BeagleRT_setADCLevel(settings->adcLevel); |
141 BeagleRT_setHeadphoneLevel(settings->headphoneLevel); | 160 BeagleRT_setHeadphoneLevel(settings->headphoneLevel); |
142 | 161 |
143 if(!initialise_render(2, settings->useMatrix ? settings->periodSize : 0, settings->periodSize * 2, 22050.0, 44100.0, userData)) { | 162 // Initialise the rendering environment: pass the number of audio and matrix |
163 // channels, the period size for matrix and audio, and the sample rates | |
164 | |
165 int audioPeriodSize = settings->periodSize * 2; | |
166 float audioSampleRate = 44100.0; | |
167 float matrixSampleRate = 22050.0; | |
168 if(settings->useMatrix) { | |
169 audioPeriodSize = settings->periodSize * settings->numMatrixChannels / 4; | |
170 matrixSampleRate = audioSampleRate * 4.0 / (float)settings->numMatrixChannels; | |
171 } | |
172 | |
173 if(!initialise_render(settings->useMatrix ? settings->numMatrixChannels : 0, /* matrix channels */ | |
174 2, /* audio channels */ | |
175 settings->useMatrix ? settings->periodSize : 0, /* matrix period size */ | |
176 audioPeriodSize, | |
177 matrixSampleRate, audioSampleRate, | |
178 userData)) { | |
144 cout << "Couldn't initialise audio rendering\n"; | 179 cout << "Couldn't initialise audio rendering\n"; |
145 return 1; | 180 return 1; |
146 } | 181 } |
147 | 182 |
148 return 0; | 183 return 0; |