From 14010d340e7ecbee0ecce8ac9f2cc980477ed125 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 17 Nov 2017 08:48:19 -0800 Subject: [PATCH] Add SetGain to I2S outputs to scale output volume A new SetGain(float 0.0 ... 4.0) method can scale the output volume of an I2S DAC or SW DAC by scaling the samples. Proper clipping is performed. Adds a 16x16 multiply and a couple shifts/branches to every sample output. --- src/AudioOutput.h | 17 +++++++++++++---- src/AudioOutputI2SDAC.cpp | 3 +++ src/AudioOutputI2SNoDAC.cpp | 7 +++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/AudioOutput.h b/src/AudioOutput.h index 9da878d..9fb4bb3 100644 --- a/src/AudioOutput.h +++ b/src/AudioOutput.h @@ -26,11 +26,12 @@ class AudioOutput { public: - AudioOutput() {}; + AudioOutput() { }; virtual ~AudioOutput() {}; virtual bool SetRate(int hz) { hertz = hz; return true; }; virtual bool SetBitsPerSample(int bits) { bps = bits; return true; }; virtual bool SetChannels(int chan) { channels = chan; return true; }; + virtual bool SetGain(float f) { if (f>4.0) f = 4.0; if (f<0.0) f=0.0; gainF2P6 = (uint8_t)(f*(1<<6)); return true; } virtual bool begin() { return false; }; typedef enum { LEFTCHANNEL=0, RIGHTCHANNEL=1 } SampleIndex; virtual bool ConsumeSample(int16_t sample[2]) { (void)sample; return false; }; @@ -47,10 +48,18 @@ class AudioOutput } }; + inline int16_t Amplify(int16_t s) { + int32_t v = (s * gainF2P6)>>6; + if (v < -32767) return -32767; + else if (v > 32767) return 32767; + else return (int16_t)(v&0xffff); + } + protected: - int hertz; - int bps; - int channels; + uint16_t hertz; + uint8_t bps; + uint8_t channels; + uint8_t gainF2P6; // Fixed point 2.6 }; #endif diff --git a/src/AudioOutputI2SDAC.cpp b/src/AudioOutputI2SDAC.cpp index 6e34995..0cb1be6 100644 --- a/src/AudioOutputI2SDAC.cpp +++ b/src/AudioOutputI2SDAC.cpp @@ -29,6 +29,7 @@ AudioOutputI2SDAC::AudioOutputI2SDAC() if (!i2sOn) i2s_begin(); i2sOn = true; mono = false; + SetGain(1.0); } AudioOutputI2SDAC::~AudioOutputI2SDAC() @@ -74,6 +75,8 @@ bool AudioOutputI2SDAC::begin() bool AudioOutputI2SDAC::ConsumeSample(int16_t sample[2]) { MakeSampleStereo16( sample ); + sample[0] = Amplify(sample[0]); + sample[1] = Amplify(sample[1]); if (this->mono) { // Average the two samples and overwrite diff --git a/src/AudioOutputI2SNoDAC.cpp b/src/AudioOutputI2SNoDAC.cpp index 9ba34f5..c423b51 100644 --- a/src/AudioOutputI2SNoDAC.cpp +++ b/src/AudioOutputI2SNoDAC.cpp @@ -32,6 +32,7 @@ AudioOutputI2SNoDAC::AudioOutputI2SNoDAC() oversample = 32; lastSamp = 0; cumErr = 0; + SetGain(1.0); } AudioOutputI2SNoDAC::~AudioOutputI2SNoDAC() @@ -79,8 +80,10 @@ bool AudioOutputI2SNoDAC::begin() void AudioOutputI2SNoDAC::DeltaSigma(int16_t sample[2], uint32_t dsBuff[4]) { - // Not shift 8 because addition takes care of one mult x 2 - fixed24p8_t newSamp = ( ((int32_t)sample[0]) + ((int32_t)sample[1]) ) << 7; +// // Not shift 8 because addition takes care of one mult x 2 +// fixed24p8_t newSamp = ( ((int32_t)sample[0]) + ((int32_t)sample[1]) ) << 7; + int32_t sum = (((int32_t)sample[0]) + ((int32_t)sample[1])) >> 1; + fixed24p8_t newSamp = ( (int32_t)Amplify(sum) ) << 8; int oversample32 = oversample / 32; // How much the comparison signal changes each oversample step