Skip to content

Commit

Permalink
control_3206: add ability to set headphone amp volume (and clarify se…
Browse files Browse the repository at this point in the history
…tting DAC volume)
  • Loading branch information
chipaudette committed Sep 20, 2024
1 parent b4cb455 commit e9ec78f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
40 changes: 36 additions & 4 deletions src/control_aic3206.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,44 @@ float AudioControlAIC3206::volume_dB(float orig_vol_dB, int Ichan) { // 0 = Lef
return vol_dB;
}
float AudioControlAIC3206::volume_dB(float vol_left_dB, float vol_right_dB) {
volume_dB(vol_right_dB, 1); //set right channel
return volume_dB(vol_left_dB, 0); //set left channel
volume_dB(vol_right_dB, (int)1); //set right channel
return volume_dB(vol_left_dB, (int)0); //set left channel
}
float AudioControlAIC3206::volume_dB(float vol_dB) {
vol_dB = volume_dB(vol_dB, 1); //set right channel
return volume_dB(vol_dB, 0); //set left channel
vol_dB = volume_dB(vol_dB, (int)1); //set right channel
return volume_dB(vol_dB, (int)0); //set left channel
}

float AudioControlAIC3206::setHeadphoneGain_dB(float gain_left_dB, float gain_right_dB) {
unsigned int buff = 0;
int8_t left_dB_u8 = 0;
int8_t right_dB_u8 = 0;

// round to nearest dB and clamp limits
left_dB_u8 = (int)(gain_left_dB + 0.5);
right_dB_u8 = (int)(gain_right_dB + 0.5);

left_dB_u8 = constrain(left_dB_u8, AIC3206_HP_VOLUME_MIN, AIC3206_HP_VOLUME_MAX);
right_dB_u8 = constrain(gain_right_dB, AIC3206_HP_VOLUME_MIN, AIC3206_HP_VOLUME_MAX);

//check to see if they want it muted
if (gain_left_dB < -90.0) left_dB_u8 = 0b00111001; //mute it
if (gain_right_dB < -90.0) right_dB_u8 = 0b00111001; //mute it

// Set Left Volume
//aic_goToBook(0);
buff = aic_readPage(AIC3206_HP_VOLUME_PAGE, AIC3206_HPL_VOLUME_REG); //read existing register value
buff = (buff & (~AIC3206_HP_VOLUME_MASK)) | (left_dB_u8 & AIC3206_HP_VOLUME_MASK);

aic_writePage( AIC3206_HP_VOLUME_PAGE, AIC3206_HPL_VOLUME_REG, uint8_t(buff) );

// Set Right Volume
buff = aic_readPage(AIC3206_HP_VOLUME_PAGE, AIC3206_HPR_VOLUME_REG); //read existing register value
buff = (buff & (~AIC3206_HP_VOLUME_MASK)) | (right_dB_u8 & AIC3206_HP_VOLUME_MASK);

aic_writePage( AIC3206_HP_VOLUME_PAGE, AIC3206_HPR_VOLUME_REG, uint8_t(buff) );

return left_dB_u8;
}

int AudioControlAIC3206::unmuteDAC(int chan) {
Expand Down
28 changes: 21 additions & 7 deletions src/control_aic3206.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
#define TYMPAN_LEFT_JACK_AS_LINEIN_RIGHT_ON_BOARD_MIC (AudioControlAIC3206::IN3_IN2) //use pink jack for left and on-board mic for right
#define TYMPAN_LEFT_JACK_AS_MIC_RIGHT_ON_BOARD_MIC (AudioControlAIC3206::IN3_wBIAS_IN2) //use pink jack (with bias) for left and on-board mic for right


//convenience names to use with outputSelect()
#define TYMPAN_OUTPUT_HEADPHONE_JACK_OUT 1 //DAC left and right to headphone left and right
#define TYMPAN_OUTPUT_LINE_OUT 2 //DAC left and right to lineout left and right
#define TYMPAN_OUTPUT_HEADPHONE_AND_LINE_OUT 3 //DAC left and right to both headphone and line out
#define TYMPAN_OUTPUT_LEFT2DIFFHP_AND_R2DIFFLO 4 //DAC left to differential headphone, DAC right to line out

//names to use with setMicBias() to set the amount of bias voltage to use
#define TYMPAN_MIC_BIAS_OFF 0
#define TYMPAN_MIC_BIAS_1_25 1
Expand All @@ -39,10 +32,27 @@
#define TYMPAN_MIC_BIAS_VSUPPLY 4
#define TYMPAN_DEFAULT_MIC_BIAS TYMPAN_MIC_BIAS_2_5

//convenience names to use with outputSelect()
#define TYMPAN_OUTPUT_HEADPHONE_JACK_OUT 1 //DAC left and right to headphone left and right
#define TYMPAN_OUTPUT_LINE_OUT 2 //DAC left and right to lineout left and right
#define TYMPAN_OUTPUT_HEADPHONE_AND_LINE_OUT 3 //DAC left and right to both headphone and line out
#define TYMPAN_OUTPUT_LEFT2DIFFHP_AND_R2DIFFLO 4 //DAC left to differential headphone, DAC right to line out

// Headphone Volume
#define AIC3206_HP_VOLUME_PAGE 0x01
#define AIC3206_HPL_VOLUME_REG 0x10 //decimal 16
#define AIC3206_HPR_VOLUME_REG 0x11 //decimal 17
#define AIC3206_HP_VOLUME_MASK 0b00111111
#define AIC3206_HP_VOLUME_MIN (int8_t)-6
#define AIC3206_HP_VOLUME_MAX (int8_t)14


#define BOTH_CHAN 0
#define LEFT_CHAN 1
#define RIGHT_CHAN 2



//define AIC3206_DEFAULT_I2C_BUS 0 //bus zero is &Wire
#define AIC3206_DEFAULT_RESET_PIN 42

Expand Down Expand Up @@ -82,6 +92,10 @@ class AudioControlAIC3206: public TeensyAudioControl
float volume_dB(float vol_dB); //set both channels to the same volume (via the DAC output volume)
float volume_dB(float vol_left_dB, float vol_right_dB); //set both channels (via the DAC output volume), but to their own values
float volume_dB(float vol_left_dB, int chan); //set each channel seperately (0 = left; 1 = right)
float setDacGain_dB(float gain_dB) { return setDacGain_dB(gain_dB, gain_dB); }
float setDacGain_dB(float gain_left_dB, float gain_right_dB) { return volume_dB(gain_left_dB, gain_right_dB); }
float setHeadphoneGain_dB(float gain_dB) { return setHeadphoneGain_dB(gain_dB, gain_dB); }
float setHeadphoneGain_dB(float vol_left_dB, float vol_right_dB); // set HP volume
int muteDAC(int chan = BOTH_CHAN); //mutes the output DAC
int unmuteDAC(int chan = BOTH_CHAN); //unmutes the output DAC
int muteHeadphone(int chan = BOTH_CHAN); //mutes the headphone driver
Expand Down

0 comments on commit e9ec78f

Please sign in to comment.