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.
This commit is contained in:
1 parent
65c1373300
commit
14010d340e
3 files changed
+21
-6
No files matched your search
+13
-4
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user