Refactor I2S to be cleaner, more extensible
With the multiple I2S modes on the ESP32, it makes sense to use a cleaner class hierarchy for the I2S tools. Make I2SNoDAC into a subclass of I2S (renamed from I2SDAC) and remove about 80% of the code which was identical exept for the per-sample bitstream synthesis.
This commit is contained in:
1 parent
7a7f8bf449
commit
96fc37dce5
12 files changed
+113
-175
No files matched your search
+14
-6
@@ -29,15 +29,23 @@ class AudioOutput
|
||||
public:
|
||||
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 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; };
|
||||
virtual bool stop() { return false; };
|
||||
virtual bool loop() { return true; };
|
||||
virtual bool ConsumeSample(int16_t sample[2]) { (void)sample; return false; }
|
||||
virtual uint16_t ConsumeSamples(int16_t *samples, uint16_t count)
|
||||
{
|
||||
for (uint16_t i=0; i<count; i++) {
|
||||
if (!ConsumeSample(samples)) return i;
|
||||
samples += 2;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
virtual bool stop() { return false; }
|
||||
virtual bool loop() { return true; }
|
||||
|
||||
public:
|
||||
virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
AudioOutputI2SDAC
|
||||
Audio player for an I2S connected DAC, 16bps
|
||||
AudioOutputI2S
|
||||
Base class for I2S interface port
|
||||
|
||||
Copyright (C) 2017 Earle F. Philhower, III
|
||||
|
||||
@@ -24,54 +24,44 @@
|
||||
#else
|
||||
#include <i2s.h>
|
||||
#endif
|
||||
#include "AudioOutputI2SDAC.h"
|
||||
#include "AudioOutputI2S.h"
|
||||
|
||||
bool AudioOutputI2SDAC::i2sOn = false;
|
||||
|
||||
#ifdef ESP32
|
||||
//i2s configuration
|
||||
int i2s_num = 0; // i2s port number
|
||||
i2s_config_t i2s_config_dac = {
|
||||
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
|
||||
.sample_rate = 36000,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
|
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
|
||||
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
|
||||
.dma_buf_count = 8,
|
||||
.dma_buf_len = 64, //Interrupt level 1
|
||||
.use_apll = 1 // Use audio PLL
|
||||
};
|
||||
|
||||
i2s_pin_config_t pin_config_dac = {
|
||||
.bck_io_num = 26, //this is BCK pin
|
||||
.ws_io_num = 25, // this is LRCK pin
|
||||
.data_out_num = 22, // this is DATA output pin
|
||||
.data_in_num = -1 //Not used
|
||||
};
|
||||
#endif
|
||||
|
||||
AudioOutputI2SDAC::AudioOutputI2SDAC()
|
||||
AudioOutputI2S::AudioOutputI2S(int port)
|
||||
{
|
||||
#ifdef ESP32
|
||||
if (!i2sOn) {
|
||||
//initialize i2s with configurations above
|
||||
i2s_driver_install((i2s_port_t)i2s_num, &i2s_config_dac, 0, NULL);
|
||||
i2s_set_pin((i2s_port_t)i2s_num, &pin_config_dac);
|
||||
i2s_config_t i2s_config_dac = {
|
||||
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
|
||||
.sample_rate = 44100,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
|
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
|
||||
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
|
||||
.dma_buf_count = 8,
|
||||
.dma_buf_len = 64, //Interrupt level 1
|
||||
.use_apll = 1 // Use audio PLL
|
||||
};
|
||||
|
||||
i2s_driver_install((i2s_port_t)portNo, &i2s_config_dac, 0, NULL);
|
||||
SetPinout(26, 25, 22);
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
}
|
||||
#else
|
||||
if (!i2sOn) i2s_begin();
|
||||
if (!i2sOn) {
|
||||
i2s_begin();
|
||||
}
|
||||
#endif
|
||||
i2sOn = true;
|
||||
portNo = port;
|
||||
mono = false;
|
||||
SetGain(1.0);
|
||||
}
|
||||
|
||||
AudioOutputI2SDAC::~AudioOutputI2SDAC()
|
||||
AudioOutputI2S::~AudioOutputI2S()
|
||||
{
|
||||
#ifdef ESP32
|
||||
if (!i2sOn) {
|
||||
i2s_driver_uninstall((i2s_port_t)i2s_num); //stop & destroy i2s driver
|
||||
i2s_driver_uninstall((i2s_port_t)portNo); //stop & destroy i2s driver
|
||||
}
|
||||
#else
|
||||
if (i2sOn) i2s_end();
|
||||
@@ -79,45 +69,63 @@ AudioOutputI2SDAC::~AudioOutputI2SDAC()
|
||||
i2sOn = false;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SDAC::SetRate(int hz)
|
||||
bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout)
|
||||
{
|
||||
#ifdef ESP32
|
||||
i2s_pin_config_t pins = {
|
||||
.bck_io_num = bclk,
|
||||
.ws_io_num = wclk,
|
||||
.data_out_num = dout,
|
||||
.data_in_num = I2S_PIN_NO_CHANGE
|
||||
};
|
||||
i2s_set_pin((i2s_port_t)portNo, &pins);
|
||||
return true;
|
||||
#else
|
||||
(void) bclk;
|
||||
(void) wclk;
|
||||
(void) dout;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetRate(int hz)
|
||||
{
|
||||
// TODO - have a list of allowable rates from constructor, check them
|
||||
this->hertz = hz;
|
||||
#ifdef ESP32
|
||||
i2s_set_sample_rates((i2s_port_t)i2s_num, hz);
|
||||
i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz));
|
||||
#else
|
||||
i2s_set_rate(hz);
|
||||
i2s_set_rate(AdjustI2SRate(hz));
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SDAC::SetBitsPerSample(int bits)
|
||||
bool AudioOutputI2S::SetBitsPerSample(int bits)
|
||||
{
|
||||
if ( (bits != 16) && (bits != 8) ) return false;
|
||||
this->bps = bits;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SDAC::SetChannels(int channels)
|
||||
bool AudioOutputI2S::SetChannels(int channels)
|
||||
{
|
||||
if ( (channels < 1) || (channels > 2) ) return false;
|
||||
this->channels = channels;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SDAC::SetOutputModeMono(bool mono)
|
||||
bool AudioOutputI2S::SetOutputModeMono(bool mono)
|
||||
{
|
||||
this->mono = mono;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SDAC::begin()
|
||||
bool AudioOutputI2S::begin()
|
||||
{
|
||||
// Nothing to do here, i2s will start once data comes in
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SDAC::ConsumeSample(int16_t sample[2])
|
||||
bool AudioOutputI2S::ConsumeSample(int16_t sample[2])
|
||||
{
|
||||
MakeSampleStereo16( sample );
|
||||
|
||||
@@ -128,15 +136,17 @@ bool AudioOutputI2SDAC::ConsumeSample(int16_t sample[2])
|
||||
}
|
||||
uint32_t s32 = ((Amplify(sample[RIGHTCHANNEL]))<<16) | (Amplify(sample[LEFTCHANNEL]) & 0xffff);
|
||||
#ifdef ESP32
|
||||
return i2s_write_bytes((i2s_port_t)i2s_num, (const char *)&s32, sizeof(uint32_t), 0);
|
||||
return i2s_push_sample((i2s_port_t)portNo, (const char *)&s32, 0);
|
||||
#else
|
||||
return i2s_write_sample_nb(s32); // If we can't store it, return false. OTW true
|
||||
#endif
|
||||
}
|
||||
|
||||
bool AudioOutputI2SDAC::stop()
|
||||
bool AudioOutputI2S::stop()
|
||||
{
|
||||
// Nothing to do here. Maybe mute control if DAC doesn't do it for you
|
||||
#ifdef ESP32
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
AudioOutputI2SDAC
|
||||
Audio player for an I2S connected DAC, 16bps
|
||||
AudioOutputI2S
|
||||
Base class for an I2S output port
|
||||
|
||||
Copyright (C) 2017 Earle F. Philhower, III
|
||||
|
||||
@@ -18,16 +18,17 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _AUDIOOUTPUTI2SDAC_H
|
||||
#define _AUDIOOUTPUTI2SDAC_H
|
||||
#ifndef _AUDIOOUTPUTI2S_H
|
||||
#define _AUDIOOUTPUTI2S_H
|
||||
|
||||
#include "AudioOutput.h"
|
||||
|
||||
class AudioOutputI2SDAC : public AudioOutput
|
||||
class AudioOutputI2S : public AudioOutput
|
||||
{
|
||||
public:
|
||||
AudioOutputI2SDAC();
|
||||
virtual ~AudioOutputI2SDAC() override;
|
||||
AudioOutputI2S(int port=0);
|
||||
virtual ~AudioOutputI2S() 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;
|
||||
@@ -38,8 +39,10 @@ class AudioOutputI2SDAC : public AudioOutput
|
||||
bool SetOutputModeMono(bool mono); // Force mono output no matter the input
|
||||
|
||||
protected:
|
||||
virtual int AdjustI2SRate(int hz) { return hz; }
|
||||
uint8_t portNo;
|
||||
bool mono;
|
||||
static bool i2sOn; // One per machine, not per instance...
|
||||
bool i2sOn;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -27,70 +27,15 @@
|
||||
#include "AudioOutputI2SNoDAC.h"
|
||||
|
||||
|
||||
#ifdef ESP32
|
||||
extern int i2s_num;
|
||||
extern i2s_config_t i2s_config_dac;
|
||||
extern i2s_pin_config_t pin_config_dac;
|
||||
#endif
|
||||
|
||||
bool AudioOutputI2SNoDAC::i2sOn = false;
|
||||
|
||||
AudioOutputI2SNoDAC::AudioOutputI2SNoDAC()
|
||||
AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port) : AudioOutputI2S(port)
|
||||
{
|
||||
#ifdef ESP32
|
||||
if (!i2sOn) {
|
||||
//initialize i2s with configurations above
|
||||
|
||||
i2s_driver_install((i2s_port_t)i2s_num, &i2s_config_dac, 0, NULL);
|
||||
i2s_set_pin((i2s_port_t)i2s_num, &pin_config_dac);
|
||||
}
|
||||
#else
|
||||
if (!i2sOn) i2s_begin();
|
||||
#endif
|
||||
i2sOn = true;
|
||||
hertz = 44100;
|
||||
oversample = 32;
|
||||
lastSamp = 0;
|
||||
cumErr = 0;
|
||||
SetGain(1.0);
|
||||
}
|
||||
|
||||
AudioOutputI2SNoDAC::~AudioOutputI2SNoDAC()
|
||||
{
|
||||
#ifdef ESP32
|
||||
if (!i2sOn) {
|
||||
i2s_driver_uninstall((i2s_port_t)0); //stop & destroy i2s driver
|
||||
}
|
||||
#else
|
||||
if (i2sOn) i2s_end();
|
||||
#endif
|
||||
i2sOn = false;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SNoDAC::SetRate(int hz)
|
||||
{
|
||||
// TODO - what is the max hz we can request on I2S?
|
||||
this->hertz = hz;
|
||||
#ifdef ESP32
|
||||
i2s_set_sample_rates((i2s_port_t)0, hz * (oversample / 32));
|
||||
#else
|
||||
i2s_set_rate(hz * (oversample / 32));
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SNoDAC::SetBitsPerSample(int bits)
|
||||
{
|
||||
if ( (bits != 16) && (bits != 8) ) return false;
|
||||
this->bps = bits;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SNoDAC::SetChannels(int channels)
|
||||
{
|
||||
if ( (channels < 1) || (channels > 2) ) return false;
|
||||
this->channels = channels;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SNoDAC::SetOversampling(int os) {
|
||||
@@ -102,16 +47,9 @@ bool AudioOutputI2SNoDAC::SetOversampling(int os) {
|
||||
return SetRate(hertz);
|
||||
}
|
||||
|
||||
bool AudioOutputI2SNoDAC::begin()
|
||||
{
|
||||
// Nothing to do here, i2s will start once data comes in
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
int32_t sum = (((int32_t)sample[0]) + ((int32_t)sample[1])) >> 1;
|
||||
fixed24p8_t newSamp = ( (int32_t)Amplify(sum) ) << 8;
|
||||
|
||||
@@ -140,19 +78,6 @@ void AudioOutputI2SNoDAC::DeltaSigma(int16_t sample[2], uint32_t dsBuff[4])
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ESP32
|
||||
/* write sample data to I2S */
|
||||
int i2s_write_sample_nb(uint32_t sample){
|
||||
return i2s_write_bytes((i2s_port_t)0, (const char *)&sample, sizeof(sample), 0);
|
||||
}
|
||||
|
||||
// how to do this blocking??
|
||||
int i2s_write_sample(uint32_t sample){
|
||||
return i2s_write_bytes((i2s_port_t)0, (const char *)&sample, sizeof(sample), portMAX_DELAY);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool AudioOutputI2SNoDAC::ConsumeSample(int16_t sample[2])
|
||||
{
|
||||
MakeSampleStereo16( sample );
|
||||
@@ -160,20 +85,17 @@ bool AudioOutputI2SNoDAC::ConsumeSample(int16_t sample[2])
|
||||
// Make delta-sigma filled buffer
|
||||
uint32_t dsBuff[4];
|
||||
DeltaSigma(sample, dsBuff);
|
||||
|
||||
|
||||
// Either send complete pulse stream or nothing
|
||||
#ifdef ESP32
|
||||
if (!i2s_write_bytes((i2s_port_t)portNo, (const char *)dsBuff, sizeof(uint32_t) * (oversample/32), 0))
|
||||
return false;
|
||||
#else
|
||||
if (!i2s_write_sample_nb(dsBuff[0])) return false; // No room at the inn
|
||||
// At this point we've sent in first of possibly 4 32-bits, need to send
|
||||
// remaining ones even if they block.
|
||||
for (int i = 32; i < oversample; i+=32)
|
||||
i2s_write_sample( dsBuff[i / 32]);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2SNoDAC::stop()
|
||||
{
|
||||
// Nothing to do here. Maybe mute control if DAC doesn't do it for you
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,25 +21,20 @@
|
||||
#ifndef _AUDIOOUTPUTI2SNODAC_H
|
||||
#define _AUDIOOUTPUTI2SNODAC_H
|
||||
|
||||
#include "AudioOutput.h"
|
||||
#include "AudioOutputI2S.h"
|
||||
|
||||
class AudioOutputI2SNoDAC : public AudioOutput
|
||||
class AudioOutputI2SNoDAC : public AudioOutputI2S
|
||||
{
|
||||
public:
|
||||
AudioOutputI2SNoDAC();
|
||||
AudioOutputI2SNoDAC(int port = 0);
|
||||
virtual ~AudioOutputI2SNoDAC() 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;
|
||||
virtual bool stop() override;
|
||||
|
||||
bool SetOversampling(int os);
|
||||
|
||||
protected:
|
||||
static bool i2sOn; // One per machine, not per instance...
|
||||
int oversample;
|
||||
virtual int AdjustI2SRate(int hz) { return hz * oversample/32; }
|
||||
uint8_t oversample;
|
||||
void DeltaSigma(int16_t sample[2], uint32_t dsBuff[4]);
|
||||
typedef int32_t fixed24p8_t;
|
||||
enum {fixedPosValue=0x007fff00}; /* 24.8 of max-signed-int */
|
||||
|
||||
Reference in new issue
Block a user