diff 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
line wrap: on
line diff
--- a/core/RTAudio.cpp	Thu Nov 13 16:02:59 2014 +0100
+++ b/core/RTAudio.cpp	Thu Jan 22 19:00:22 2015 +0000
@@ -67,7 +67,8 @@
 // periodSize indicates the number of _sensor_ frames per period: the audio period size
 // is twice this value. In total, the audio latency in frames will be 4*periodSize,
 // plus any latency inherent in the ADCs and DACs themselves.
-// useMatrix indicates whether to use the ADC and DAC or just the audio codec.
+// useMatrix indicates whether to enable the ADC and DAC or just use the audio codec.
+// numMatrixChannels indicates how many ADC and DAC channels to use.
 // userData is an opaque pointer which will be passed through to the initialise_render()
 // function for application-specific use
 //
@@ -114,6 +115,24 @@
 		}
 	}
 
+	// Limit the matrix channels to sane values
+	if(settings->numMatrixChannels >= 8)
+		settings->numMatrixChannels = 8;
+	else if(settings->numMatrixChannels >= 4)
+		settings->numMatrixChannels = 4;
+	else
+		settings->numMatrixChannels = 2;
+
+	// Sanity check the combination of channels and period size
+	if(settings->numMatrixChannels <= 4 && settings->periodSize < 2) {
+		cout << "Error: " << settings->numMatrixChannels << " channels and period size of " << settings->periodSize << " not supported.\n";
+		return 1;
+	}
+	if(settings->numMatrixChannels <= 2 && settings->periodSize < 4) {
+		cout << "Error: " << settings->numMatrixChannels << " channels and period size of " << settings->periodSize << " not supported.\n";
+		return 1;
+	}
+
 	// Use PRU for audio
 	gPRU = new PRU();
 	gAudioCodec = new I2c_Codec();
@@ -122,7 +141,7 @@
 		cout << "Error: unable to prepare GPIO for PRU audio\n";
 		return 1;
 	}
-	if(gPRU->initialise(0, settings->periodSize, true)) {
+	if(gPRU->initialise(0, settings->periodSize, settings->numMatrixChannels, true)) {
 		cout << "Error: unable to initialise PRU\n";
 		return 1;
 	}
@@ -140,7 +159,23 @@
 	BeagleRT_setADCLevel(settings->adcLevel);
 	BeagleRT_setHeadphoneLevel(settings->headphoneLevel);
 
-	if(!initialise_render(2, settings->useMatrix ? settings->periodSize : 0, settings->periodSize * 2, 22050.0, 44100.0, userData)) {
+	// Initialise the rendering environment: pass the number of audio and matrix
+	// channels, the period size for matrix and audio, and the sample rates
+
+	int audioPeriodSize = settings->periodSize * 2;
+	float audioSampleRate = 44100.0;
+	float matrixSampleRate = 22050.0;
+	if(settings->useMatrix) {
+		audioPeriodSize = settings->periodSize * settings->numMatrixChannels / 4;
+		matrixSampleRate = audioSampleRate * 4.0 / (float)settings->numMatrixChannels;
+	}
+
+	if(!initialise_render(settings->useMatrix ? settings->numMatrixChannels : 0, /* matrix channels */
+						  2, /* audio channels */
+				          settings->useMatrix ? settings->periodSize : 0, /* matrix period size */
+				          audioPeriodSize,
+				          matrixSampleRate, audioSampleRate,
+				          userData)) {
 		cout << "Couldn't initialise audio rendering\n";
 		return 1;
 	}