changeset 171:e63563507edd

Added command-line options for the PGA
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 28 Dec 2015 03:53:36 +0100
parents e80340fe527a
children 5f4408705eed
files core/I2c_Codec.cpp core/RTAudio.cpp core/RTAudioCommandLine.cpp include/BeagleRT.h
diffstat 4 files changed, 61 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/core/I2c_Codec.cpp	Mon Dec 28 03:19:59 2015 +0100
+++ b/core/I2c_Codec.cpp	Mon Dec 28 03:53:36 2015 +0100
@@ -90,10 +90,11 @@
 		return 1;
 	if(writeRegister(0x0E, 0x00))	// Headset / button press register B: disabled
 		return 1;
-	if(setPga(16, 0))   // Left ADC PGA gain control: not muted; 16dB
-		return 1;
-	if(setPga(16, 1))	// Right ADC PGA gain control: not muted; 16dB
-		return 1;
+	//TODO: why are the next four lines, if uncommented, executed AFTER this method has returned?
+//	if(setPga(16, 0))   // Left ADC PGA gain control: not muted; 16dB
+//		return 1;
+//	if(setPga(16, 1))	// Right ADC PGA gain control: not muted; 16dB
+//		return 1;
 //	if(writeRegister(0x0F, 0b01000000))	// Left ADC PGA gain control: not muted; 0x20 = 16dB
 //		return 1;
 //	if(writeRegister(0x10, 0b0))	// Right ADC PGA gain control: not muted; 0x20 = 16dB
--- a/core/RTAudio.cpp	Mon Dec 28 03:19:59 2015 +0100
+++ b/core/RTAudio.cpp	Mon Dec 28 03:53:36 2015 +0100
@@ -210,10 +210,13 @@
 		cout << "Error: unable to initialise audio codec\n";
 		return 1;
 	}
-
+	cout << "Initialized codec\n";
 	// Set default volume levels
 	BeagleRT_setDACLevel(settings->dacLevel);
 	BeagleRT_setADCLevel(settings->adcLevel);
+	for(int n = 0; n < 2; n++){
+		BeagleRT_setPgaGain(settings->pgaGain[n], n);
+	}
 	BeagleRT_setHeadphoneLevel(settings->headphoneLevel);
 
 	// Call the user-defined initialisation function
@@ -452,6 +455,15 @@
 	return gAudioCodec->setADCVolume((int)floorf(decibels * 2.0 + 0.5));
 }
 
+// Set the level of the Programmable Gain Amplifier
+// 59.5dB is maximum, 0dB is minimum; 0.5dB steps
+int BeagleRT_setPgaGain(float decibels, int channel){
+	if(gAudioCodec == 0)
+		return -1;
+	printf("calling\n");
+	return gAudioCodec->setPga(decibels, channel);
+}
+
 // Set the level of the onboard headphone amplifier; affects headphone
 // output only (not line out or speaker)
 // 0dB is the maximum, -63.5dB is the minimum; 0.5dB steps
--- a/core/RTAudioCommandLine.cpp	Mon Dec 28 03:19:59 2015 +0100
+++ b/core/RTAudioCommandLine.cpp	Mon Dec 28 03:53:36 2015 +0100
@@ -11,9 +11,10 @@
 #include <getopt.h>
 #include "../include/BeagleRT.h"
 
-#ifndef OPT_PRU_FILE
-#define OPT_PRU_FILE 176 // this is an extended ASCII code
-#endif
+#define OPT_PRU_FILE 1000
+#define OPT_PGA_GAIN_LEFT 1001
+#define OPT_PGA_GAIN_RIGHT 1002
+
 
 enum {
 	kAmplifierMutePin = 61	// P8-26 controls amplifier mute
@@ -31,6 +32,8 @@
 	{"mute-speaker", 1, NULL, 'M'},
 	{"dac-level", 1, NULL, 'D'},
 	{"adc-level", 1, NULL, 'A'},
+	{"pga-gain-left", 1, NULL, OPT_PGA_GAIN_LEFT},
+	{"pga-gain-right", 1, NULL, OPT_PGA_GAIN_RIGHT},
 	{"hp-level", 1, NULL, 'H'},
 	{"receive-port", 1, NULL, 'R'},
 	{"transmit-port", 1, NULL, 'T'},
@@ -54,6 +57,8 @@
 	settings->beginMuted = 0;
 	settings->dacLevel = DEFAULT_DAC_LEVEL;
 	settings->adcLevel = DEFAULT_ADC_LEVEL;
+	for(int n = 0; n < 2; n++)
+		settings->pgaGain[n] = DEFAULT_PGA_GAIN;
 	settings->headphoneLevel = DEFAULT_HP_LEVEL;
 
 	settings->verbose = 0;
@@ -207,6 +212,12 @@
 			else
 				std::cerr << "Warning: filename for the PRU code is too long (>" << MAX_PRU_FILENAME_LENGTH << " characters). Using embedded PRU code instead\n";
 			break;
+		case OPT_PGA_GAIN_LEFT:
+			settings->pgaGain[0] = atof(optarg);
+			break;
+		case OPT_PGA_GAIN_RIGHT:
+			settings->pgaGain[1] = atof(optarg);
+			break;
 		case '?':
 		default:
 			return c;
@@ -221,6 +232,8 @@
 	std::cerr << "   --period [-p] period:            Set the hardware period (buffer) size in analog samples\n";
 	std::cerr << "   --dac-level [-D] dBs:            Set the DAC output level (0dB max; -63.5dB min)\n";
 	std::cerr << "   --adc-level [-A] dBs:            Set the ADC input level (0dB max; -12dB min)\n";
+	std::cerr << "   --pga-gain-left dBs:             Set the Programmable Gain Amplifier for the left audio channel (0dBmin; 59.5dB max; default: 16dB)\n";
+	std::cerr << "   --pga-gain-right dBs:            Set the Programmable Gain Amplifier for the right audio channel (0dBmin; 59.5dB max; default: 16dB)\n";
 	std::cerr << "   --hp-level [-H] dBs:             Set the headphone output level (0dB max; -63.5dB min)\n";
 	std::cerr << "   --mute-speaker [-M] val:         Set whether to mute the speaker initially (default: no)\n";
 	std::cerr << "   --use-analog [-N] val:           Set whether to use ADC/DAC analog (default: yes)\n";
--- a/include/BeagleRT.h	Mon Dec 28 03:19:59 2015 +0100
+++ b/include/BeagleRT.h	Mon Dec 28 03:53:36 2015 +0100
@@ -73,6 +73,12 @@
  */
 #define DEFAULT_ADC_LEVEL	-6.0
 
+
+/**
+ * Default level of the Programmable Gain Amplifier in decibels.
+ */
+#define DEFAULT_PGA_GAIN 16
+
 /**
  * Default level of the headphone output in decibels. See BeagleRT_setHeadphoneLevel().
  */
@@ -120,6 +126,8 @@
 	float dacLevel;
 	/// Level for the audio ADC input
 	float adcLevel;
+	/// Gains for the PGA, left and right channels
+	float pgaGain[2];
 	/// Level for the headphone output
 	float headphoneLevel;
 
@@ -484,6 +492,25 @@
  */
 int BeagleRT_setADCLevel(float decibels);
 
+
+/**
+ * \brief Set the gain of the audio preamplifier.
+ *
+ * This function sets the level of the Programmable Gain Amplifier(PGA), which
+ * amplifies the signal before the ADC.
+ *
+ * \b Important: do not call this function from within render(), as it does not make
+ * any guarantees on real-time performance.
+ *
+ * \param decibels Level of the PGA Valid levels range from 0 (lowest) to
+ * 59.5 (highest) in steps of 0.5dB. Levels between increments of 0.5 will be rounded.
+ * \param channel Specifies which channel to apply the gain to. Channel 0 is left,
+ * channel 1 is right
+ *
+ * \return 0 on success, or nonzero if an error occurred.
+ */
+int BeagleRT_setPgaGain(float decibels, int channel);
+
 /**
  * \brief Set the level of the onboard headphone amplifier.
  *