Remove 8-bit handling in output, promote WAV/FLAC to 16 (#782)

Only the WAV and FLAC generators can generate 8-bit values, so it is
more efficient and sane to just have those generators upscale to 16-bit
signed (if necessary) in generation and simplify all the other
Generators and Outputs.
This commit is contained in:
Earle F. Philhower, III authored and GitHub committed 2025-10-13 09:56:39 -07:00
1 parent 724b6d606e
commit 058e131b26
29 files changed
+43 -163

No files matched your search

-4
View File
@@ -211,10 +211,6 @@ bool AudioGeneratorAAC::begin(AudioFileSource *source, AudioOutput *output) {
output->begin();
// AAC always comes out at 16 bits
output->SetBitsPerSample(16);
memset(buff, 0, buffLen);
memset(outSample, 0, 1024 * 2 * sizeof(int16_t));
+12 -2
View File
@@ -107,7 +107,7 @@ bool AudioGeneratorFLAC::loop() {
output->SetChannels(channels = newch);
}
if (newbps != bitsPerSample) {
output->SetBitsPerSample(bitsPerSample = newbps);
bitsPerSample = newbps;
}
}
}
@@ -116,7 +116,17 @@ bool AudioGeneratorFLAC::loop() {
if (buffPtr == buffLen) {
goto done; // At some point the flac better error and we'll return
}
if (bitsPerSample <= 16) {
if (bitsPerSample <= 8) {
lastSample[AudioOutput::LEFTCHANNEL] = buff[0][buffPtr] & 0xffff;
if (channels == 2) {
lastSample[AudioOutput::RIGHTCHANNEL] = buff[1][buffPtr] & 0xffff;
} else {
lastSample[AudioOutput::RIGHTCHANNEL] = lastSample[AudioOutput::LEFTCHANNEL];
}
// Upsample from unsigned 8 bits to signed 16 bits
lastSample[AudioOutput::LEFTCHANNEL] = (((int16_t)(lastSample[AudioOutput::LEFTCHANNEL] & 0xff)) - 128) << 8;
lastSample[AudioOutput::RIGHTCHANNEL] = (((int16_t)(lastSample[AudioOutput::RIGHTCHANNEL] & 0xff)) - 128) << 8;
} else if (bitsPerSample <= 16) {
lastSample[AudioOutput::LEFTCHANNEL] = buff[0][buffPtr] & 0xffff;
if (channels == 2) {
lastSample[AudioOutput::RIGHTCHANNEL] = buff[1][buffPtr] & 0xffff;
-3
View File
@@ -533,9 +533,6 @@ bool AudioGeneratorMIDI::begin(AudioFileSource *src, AudioOutput *out) {
if (!out->SetRate(freq)) {
return false;
}
if (!out->SetBitsPerSample(16)) {
return false;
}
if (!out->SetChannels(1)) {
return false;
}
-3
View File
@@ -133,9 +133,6 @@ bool AudioGeneratorMOD::begin(AudioFileSource *source, AudioOutput *out) {
if (!output->SetRate(sampleRate)) {
return false;
}
if (!output->SetBitsPerSample(16)) {
return false;
}
if (!output->SetChannels(2)) {
return false;
}
-1
View File
@@ -310,7 +310,6 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output) {
// Reset error count from previous file
unrecoverable = 0;
output->SetBitsPerSample(16); // Constant for MP3 decoder
output->SetChannels(2);
if (!output->begin()) {
-3
View File
@@ -163,9 +163,6 @@ bool AudioGeneratorMP3a::begin(AudioFileSource *source, AudioOutput *output) {
output->begin();
// AAC always comes out at 16 bits
output->SetBitsPerSample(16);
running = true;
return true;
-1
View File
@@ -93,7 +93,6 @@ bool AudioGeneratorOpus::begin(AudioFileSource *source, AudioOutput *output) {
// These are fixed by Opus
output->SetRate(48000);
output->SetBitsPerSample(16);
output->SetChannels(2);
running = true;
-3
View File
@@ -357,9 +357,6 @@ bool AudioGeneratorRTTTL::begin(AudioFileSource *source, AudioOutput *output) {
if (!output->SetRate(rate)) {
return false;
}
if (!output->SetBitsPerSample(16)) {
return false;
}
if (!output->SetChannels(2)) {
return false;
}
-3
View File
@@ -73,9 +73,6 @@ bool AudioGeneratorTalkie::begin(AudioFileSource *source, AudioOutput *output) {
}
this->output = output;
if (!output->SetBitsPerSample(16)) {
return false;
}
if (!output->SetChannels(2)) {
return false;
}
+5 -6
View File
@@ -98,8 +98,11 @@ bool AudioGeneratorWAV::loop() {
} else {
r = 0;
}
lastSample[AudioOutput::LEFTCHANNEL] = l;
lastSample[AudioOutput::RIGHTCHANNEL] = r;
// Upsample from unsigned 8 bits to signed 16 bits
lastSample[AudioOutput::LEFTCHANNEL] = (((int16_t)(lastSample[AudioOutput::LEFTCHANNEL] & 0xff)) - 128) << 8;
lastSample[AudioOutput::RIGHTCHANNEL] = (((int16_t)(lastSample[AudioOutput::RIGHTCHANNEL] & 0xff)) - 128) << 8;
} else if (bitsPerSample == 16) {
if (!GetBufferedData(2, &lastSample[AudioOutput::LEFTCHANNEL])) {
stop();
@@ -312,10 +315,6 @@ bool AudioGeneratorWAV::begin(AudioFileSource *source, AudioOutput *output) {
Serial.printf_P(PSTR("AudioGeneratorWAV::begin: failed to SetRate in output\n"));
return false;
}
if (!output->SetBitsPerSample(bitsPerSample)) {
Serial.printf_P(PSTR("AudioGeneratorWAV::begin: failed to SetBitsPerSample in output\n"));
return false;
}
if (!output->SetChannels(channels)) {
Serial.printf_P(PSTR("AudioGeneratorWAV::begin: failed to SetChannels in output\n"));
return false;
-10
View File
@@ -32,10 +32,6 @@ public:
hertz = hz;
return true;
}
virtual bool SetBitsPerSample(int bits) {
bps = bits;
return true;
}
virtual bool SetChannels(int chan) {
channels = chan;
return true;
@@ -90,11 +86,6 @@ protected:
if (channels == 1) {
sample[RIGHTCHANNEL] = sample[LEFTCHANNEL];
}
if (bps == 8) {
// Upsample from unsigned 8 bits to signed 16 bits
sample[LEFTCHANNEL] = (((int16_t)(sample[LEFTCHANNEL] & 0xff)) - 128) << 8;
sample[RIGHTCHANNEL] = (((int16_t)(sample[RIGHTCHANNEL] & 0xff)) - 128) << 8;
}
};
inline int16_t Amplify(int16_t s) {
@@ -110,7 +101,6 @@ protected:
protected:
uint16_t hertz;
uint8_t bps;
uint8_t channels;
uint8_t gainF2P6; // Fixed point 2.6
-4
View File
@@ -39,10 +39,6 @@ bool AudioOutputBuffer::SetRate(int hz) {
return sink->SetRate(hz);
}
bool AudioOutputBuffer::SetBitsPerSample(int bits) {
return sink->SetBitsPerSample(bits);
}
bool AudioOutputBuffer::SetChannels(int channels) {
return sink->SetChannels(channels);
}
-1
View File
@@ -28,7 +28,6 @@ public:
AudioOutputBuffer(int bufferSizeSamples, AudioOutput *dest);
virtual ~AudioOutputBuffer() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override;
virtual bool ConsumeSample(int16_t sample[2]) override;
-4
View File
@@ -47,10 +47,6 @@ bool AudioOutputFilterBiquad::SetRate(int hz) {
return sink->SetRate(hz);
}
bool AudioOutputFilterBiquad::SetBitsPerSample(int bits) {
return sink->SetBitsPerSample(bits);
}
bool AudioOutputFilterBiquad::SetChannels(int channels) {
return sink->SetChannels(channels);
}
-1
View File
@@ -43,7 +43,6 @@ public:
AudioOutputFilterBiquad(int type, float Fc, float Q, float peakGain, AudioOutput *sink);
virtual ~AudioOutputFilterBiquad() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int chan) override;
virtual bool SetGain(float f) override;
virtual bool begin() override;
-4
View File
@@ -53,10 +53,6 @@ bool AudioOutputFilterDecimate::SetRate(int hz) {
return sink->SetRate(hz);
}
bool AudioOutputFilterDecimate::SetBitsPerSample(int bits) {
return sink->SetBitsPerSample(bits);
}
bool AudioOutputFilterDecimate::SetChannels(int channels) {
return sink->SetChannels(channels);
}
-1
View File
@@ -28,7 +28,6 @@ public:
AudioOutputFilterDecimate(uint8_t taps, const int16_t *tap, int num, int den, AudioOutput *sink);
virtual ~AudioOutputFilterDecimate() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int chan) override;
virtual bool SetGain(float f) override;
virtual bool begin() override;
+1 -11
View File
@@ -42,7 +42,6 @@ AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int
//set defaults
mono = false;
lsb_justified = false;
bps = 16;
channels = 2;
hertz = 0; // Needs to be application-set
bclkPin = 26;
@@ -57,7 +56,6 @@ AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int
AudioOutputI2S::AudioOutputI2S(long sampleRate, pin_size_t sck, pin_size_t data) {
i2sOn = false;
mono = false;
bps = 16;
channels = 2;
hertz = sampleRate;
bclkPin = sck;
@@ -140,14 +138,6 @@ bool AudioOutputI2S::SetRate(int hz) {
return true;
}
bool AudioOutputI2S::SetBitsPerSample(int bits) {
if ((bits != 16) && (bits != 8)) {
return false;
}
this->bps = bits;
return true;
}
bool AudioOutputI2S::SetChannels(int channels) {
if ((channels < 1) || (channels > 2)) {
return false;
@@ -303,7 +293,7 @@ bool AudioOutputI2S::begin(bool txDAC) {
if (swap_clocks) {
i2s.swapClocks();
}
i2s.setBitsPerSample(bps);
i2s.setBitsPerSample(16);
i2s.begin(hertz);
}
#endif
-1
View File
@@ -40,7 +40,6 @@ public:
bool SetPinout(int bclkPin, int wclkPin, int doutPin, int mclkPin);
virtual ~AudioOutputI2S() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override {
return begin(true);
+4 -22
View File
@@ -7,7 +7,7 @@ class AudioOutputInternalDAC : public AudioOutput {
dac_continuous_handle_t dac_handle = nullptr;
void dac_init() {
ESP_LOGI("dac_init", "bps=%d, ch=%d, freq=%d", bps, channels, hertz);
ESP_LOGI("dac_init", "bps=%d, ch=%d, freq=%d", 16, channels, hertz);
if (dac_handle) {
dac_continuous_disable(dac_handle);
@@ -93,11 +93,7 @@ public:
auto u8_samples = (uint8_t*)samples;
for (int i = 0 ; i < count ; ++i) {
if (bps == 16) { // int16 to uint8
u8_samples[i] = (samples[i] + 32768) / 257;
} else {
u8_samples[i] = samples[i];
}
u8_samples[i] = (samples[i] + 32768) / 257;
}
dac_write(u8_samples, count);
@@ -110,13 +106,8 @@ public:
}
uint8_t u8_samples[2];
if (bps == 16) { // int16 to uint8
u8_samples[0] = (samples[0] + 32768) / 257;
u8_samples[1] = (samples[1] + 32768) / 257;
} else { // do narrowing conversion
u8_samples[0] = samples[0];
u8_samples[1] = samples[1];
}
u8_samples[0] = (samples[0] + 32768) / 257;
u8_samples[1] = (samples[1] + 32768) / 257;
dac_write(u8_samples, channels);
return true;
@@ -149,15 +140,6 @@ public:
return true;
}
bool SetBitsPerSample(int bits) override {
if (bps != bits) {
bps = bits;
flush();
dac_deinit();
}
return true;
}
bool SetChannels(int ch) override {
if (channels != ch) {
channels = ch;
-15
View File
@@ -38,11 +38,6 @@ bool AudioOutputMixerStub::SetRate(int hz) {
return true;
}
bool AudioOutputMixerStub::SetBitsPerSample(int bits) {
this->bps = bits;
return parent->SetBitsPerSample(16, id); // We're always going to produce 16b samples for sanity's sake
}
bool AudioOutputMixerStub::SetChannels(int channels) {
this->channels = channels;
return parent->SetChannels(channels, id);
@@ -99,11 +94,6 @@ bool AudioOutputMixer::SetRate(int hz) {
return false;
}
bool AudioOutputMixer::SetBitsPerSample(int bits) {
(void) bits;
return false;
}
bool AudioOutputMixer::SetChannels(int channels) {
(void) channels;
return false;
@@ -129,11 +119,6 @@ bool AudioOutputMixer::SetRate(int hz, int id) {
return sink->SetRate(hz);
}
bool AudioOutputMixer::SetBitsPerSample(int bits, int id) {
(void) id;
return sink->SetBitsPerSample(bits);
}
bool AudioOutputMixer::SetChannels(int channels, int id) {
(void) id;
return sink->SetChannels(channels);
-3
View File
@@ -32,7 +32,6 @@ public:
AudioOutputMixerStub(AudioOutputMixer *sink, int id);
virtual ~AudioOutputMixerStub() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override;
virtual bool ConsumeSample(int16_t sample[2]) override;
@@ -51,7 +50,6 @@ public:
AudioOutputMixer(int samples, AudioOutput *sink);
virtual ~AudioOutputMixer() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override;
virtual bool ConsumeSample(int16_t sample[2]) override;
@@ -65,7 +63,6 @@ public:
private:
void RemoveInput(int id);
bool SetRate(int hz, int id);
bool SetBitsPerSample(int bits, int id);
bool SetChannels(int channels, int id);
bool begin(int id);
bool ConsumeSample(int16_t sample[2], int id);
-9
View File
@@ -25,7 +25,6 @@
AudioOutputPWM::AudioOutputPWM(long sampleRate, pin_size_t data) {
pwmOn = false;
mono = false;
bps = 16;
channels = 2;
hertz = sampleRate;
doutPin = data;
@@ -45,14 +44,6 @@ bool AudioOutputPWM::SetRate(int hz) {
return true;
}
bool AudioOutputPWM::SetBitsPerSample(int bits) {
if ((bits != 16) && (bits != 8)) {
return false;
}
this->bps = bits;
return true;
}
bool AudioOutputPWM::SetChannels(int channels) {
if ((channels < 1) || (channels > 2)) {
return false;
-1
View File
@@ -31,7 +31,6 @@ public:
AudioOutputPWM(long sampleRate = 44100, pin_size_t data = 0);
virtual ~AudioOutputPWM() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override;
virtual bool ConsumeSample(int16_t sample[2]) override;
-9
View File
@@ -129,7 +129,6 @@ AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count) {
#endif
i2sOn = true;
mono = false;
bps = 16;
channels = 2;
frame_num = 0;
SetGain(1.0);
@@ -208,14 +207,6 @@ bool AudioOutputSPDIF::SetRate(int hz) {
return true;
}
bool AudioOutputSPDIF::SetBitsPerSample(int bits) {
if ((bits != 16) && (bits != 8)) {
return false;
}
this->bps = bits;
return true;
}
bool AudioOutputSPDIF::SetChannels(int channels) {
if ((channels < 1) || (channels > 2)) {
return false;
-1
View File
@@ -51,7 +51,6 @@ public:
virtual ~AudioOutputSPDIF() override;
bool SetPinout(int bclkPin, int wclkPin, int doutPin);
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override;
virtual bool ConsumeSample(int16_t sample[2]) override;
+7 -12
View File
@@ -65,15 +65,10 @@ bool AudioOutputSPIFFSWAV::begin() {
bool AudioOutputSPIFFSWAV::ConsumeSample(int16_t sample[2]) {
for (int i = 0; i < channels; i++) {
if (bps == 8) {
uint8_t l = sample[i] & 0xff;
f.write(&l, sizeof(l));
} else {
uint8_t l = sample[i] & 0xff;
uint8_t h = (sample[i] >> 8) & 0xff;
f.write(&l, sizeof(l));
f.write(&h, sizeof(h));
}
uint8_t l = sample[i] & 0xff;
uint8_t h = (sample[i] >> 8) & 0xff;
f.write(&l, sizeof(l));
f.write(&h, sizeof(h));
}
return true;
}
@@ -97,14 +92,14 @@ bool AudioOutputSPIFFSWAV::stop() {
wavHeader[25] = (hertz >> 8) & 0xff;
wavHeader[26] = (hertz >> 16) & 0xff;
wavHeader[27] = (hertz >> 24) & 0xff;
int byteRate = hertz * bps * channels / 8;
int byteRate = hertz * 16 * channels / 8;
wavHeader[28] = byteRate & 0xff;
wavHeader[29] = (byteRate >> 8) & 0xff;
wavHeader[30] = (byteRate >> 16) & 0xff;
wavHeader[31] = (byteRate >> 24) & 0xff;
wavHeader[32] = channels * bps / 8;
wavHeader[32] = channels * 16 / 8;
wavHeader[33] = 0;
wavHeader[34] = bps;
wavHeader[34] = 16;
wavHeader[35] = 0;
int datasize = f.size() - sizeof(wavHeader);
+7 -12
View File
@@ -62,15 +62,10 @@ bool AudioOutputSTDIO::ConsumeSample(int16_t sample[2]) {
return false;
}
for (int i = 0; i < channels; i++) {
if (bps == 8) {
uint8_t l = sample[i] & 0xff;
fwrite(&l, sizeof(l), 1, f);
} else {
uint8_t l = sample[i] & 0xff;
uint8_t h = (sample[i] >> 8) & 0xff;
fwrite(&l, sizeof(l), 1, f);
fwrite(&h, sizeof(h), 1, f);
}
uint8_t l = sample[i] & 0xff;
uint8_t h = (sample[i] >> 8) & 0xff;
fwrite(&l, sizeof(l), 1, f);
fwrite(&h, sizeof(h), 1, f);
}
return true;
}
@@ -94,14 +89,14 @@ bool AudioOutputSTDIO::stop() {
wavHeader[25] = (hertz >> 8) & 0xff;
wavHeader[26] = (hertz >> 16) & 0xff;
wavHeader[27] = (hertz >> 24) & 0xff;
int byteRate = hertz * bps * channels / 8;
int byteRate = hertz * 16 * channels / 8;
wavHeader[28] = byteRate & 0xff;
wavHeader[29] = (byteRate >> 8) & 0xff;
wavHeader[30] = (byteRate >> 16) & 0xff;
wavHeader[31] = (byteRate >> 24) & 0xff;
wavHeader[32] = channels * bps / 8;
wavHeader[32] = channels * 16 / 8;
wavHeader[33] = 0;
wavHeader[34] = bps;
wavHeader[34] = 16;
wavHeader[35] = 0;
int datasize = ftell(f) - sizeof(wavHeader);
+7 -13
View File
@@ -35,14 +35,14 @@ bool AudioOutputSerialWAV::begin() {
wavHeader[25] = (hertz >> 8) & 0xff;
wavHeader[26] = (hertz >> 16) & 0xff;
wavHeader[27] = (hertz >> 24) & 0xff;
int byteRate = hertz * bps * channels / 8;
int byteRate = hertz * 16 * channels / 8;
wavHeader[28] = byteRate & 0xff;
wavHeader[29] = (byteRate >> 8) & 0xff;
wavHeader[30] = (byteRate >> 16) & 0xff;
wavHeader[31] = (byteRate >> 24) & 0xff;
wavHeader[32] = channels * bps / 8;
wavHeader[32] = channels * 16 / 8;
wavHeader[33] = 0;
wavHeader[34] = bps;
wavHeader[34] = 16;
wavHeader[35] = 0;
Serial.write(wavHeader, sizeof(wavHeader));
count = 0;
@@ -55,15 +55,10 @@ bool AudioOutputSerialWAV::ConsumeSample(int16_t sample[2]) {
return false;
}
for (int i = 0; i < channels; i++) {
if (bps == 8) {
uint8_t l = sample[i] & 0xff;
Serial.write(l);
} else {
uint8_t l = sample[i] & 0xff;
uint8_t h = (sample[i] >> 8) & 0xff;
Serial.write(l);
Serial.write(h);
}
uint8_t l = sample[i] & 0xff;
uint8_t h = (sample[i] >> 8) & 0xff;
Serial.write(l);
Serial.write(h);
}
return true;
}
@@ -73,4 +68,3 @@ bool AudioOutputSerialWAV::stop() {
audioLogger->printf_P(PSTR("\n\n\nEOF\n\n\n"));
return true;
}