Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ea583ff37 | ||
|
|
3032072561 | ||
|
|
e0143bbf37 | ||
|
|
058e131b26 |
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,47 @@
|
||||
#include <Arduino.h>
|
||||
#include "AudioFileSourcePROGMEM.h"
|
||||
#include "AudioGeneratorMOD.h"
|
||||
#include "AudioOutputPDM.h"
|
||||
|
||||
#if defined(ESP8266) || defined(ARDUINO_ARCH_RP2040)
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.printf("Only for the ESP32\n");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
#else
|
||||
|
||||
// 5_steps.mod sample from the mod archive: https://modarchive.org/
|
||||
#include "5steps.h"
|
||||
|
||||
AudioGeneratorMOD *mod;
|
||||
AudioFileSourcePROGMEM *file;
|
||||
AudioOutputPDM *out;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
audioLogger = &Serial;
|
||||
file = new AudioFileSourcePROGMEM(steps_mod, sizeof(steps_mod));
|
||||
out = new AudioOutputPDM(5 /* change to your PDM output pim*/);
|
||||
mod = new AudioGeneratorMOD();
|
||||
mod->SetBufferSize(3 * 1024);
|
||||
mod->SetSampleRate(44100);
|
||||
mod->SetStereoSeparation(32);
|
||||
mod->begin(file, out);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (mod->isRunning()) {
|
||||
if (!mod->loop()) {
|
||||
mod->stop();
|
||||
}
|
||||
} else {
|
||||
Serial.printf("MOD done\n");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -42,12 +42,8 @@ void setup() {
|
||||
|
||||
audioLogger = &Serial;
|
||||
file = new AudioFileSourceLittleFS("/gs-16b-2c-44100hz.opus");
|
||||
#ifdef ARDUINO_ARCH_RP2040
|
||||
out = new AudioOutputI2S(48000, 26, 22);
|
||||
#else
|
||||
out = new AudioOutputI2S();
|
||||
// out->SetPinout(0, 1, 2); // Set the pinout if needed
|
||||
#endif
|
||||
opus = new AudioGeneratorOpus();
|
||||
opus->begin(file, out);
|
||||
}
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/earlephilhower/ESP8266Audio"
|
||||
},
|
||||
"version": "2.2.0",
|
||||
"version": "2.3.0",
|
||||
"homepage": "https://github.com/earlephilhower/ESP8266Audio",
|
||||
"frameworks": "Arduino",
|
||||
"examples": [
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
name=ESP8266Audio
|
||||
version=2.2.0
|
||||
version=2.3.0
|
||||
author=Earle F. Philhower, III
|
||||
maintainer=Earle F. Philhower, III
|
||||
sentence=Audio file and I2S sound playing routines for ESP8266, ESP32, and Raspberry Pi Pico RP2040
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
+124
-209
@@ -20,7 +20,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#include "driver/i2s.h"
|
||||
#include <driver/i2s_std.h>
|
||||
#elif defined(ARDUINO_ARCH_RP2040) || ARDUINO_ESP8266_MAJOR >= 3
|
||||
#include <I2S.h>
|
||||
#elif ARDUINO_ESP8266_MAJOR < 3
|
||||
@@ -28,44 +28,62 @@
|
||||
#endif
|
||||
#include "AudioOutputI2S.h"
|
||||
|
||||
#if defined(ESP32) || defined(ESP8266)
|
||||
AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int use_apll) {
|
||||
this->portNo = port;
|
||||
this->i2sOn = false;
|
||||
this->dma_buf_count = dma_buf_count;
|
||||
if (output_mode != EXTERNAL_I2S && output_mode != INTERNAL_DAC && output_mode != INTERNAL_PDM) {
|
||||
output_mode = EXTERNAL_I2S;
|
||||
}
|
||||
this->output_mode = output_mode;
|
||||
this->use_apll = use_apll;
|
||||
|
||||
//set defaults
|
||||
AudioOutputI2S::AudioOutputI2S() {
|
||||
i2sOn = false;
|
||||
mono = false;
|
||||
lsb_justified = false;
|
||||
bps = 16;
|
||||
channels = 2;
|
||||
hertz = 0; // Needs to be application-set
|
||||
hertz = 44100; // Needs to be application-set
|
||||
#ifdef ESP32
|
||||
bclkPin = 26;
|
||||
wclkPin = 25;
|
||||
doutPin = 22;
|
||||
mclkPin = 0;
|
||||
use_mclk = false;
|
||||
#else
|
||||
bclkPin = 26;
|
||||
wclkPin = 27;
|
||||
doutPin = 28;
|
||||
#endif
|
||||
mclkPin = -1;
|
||||
SetGain(1.0);
|
||||
SetBuffers(); // Default DMA sizes
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetBuffers(int dmaBufferCount, int dmaBufferBytes) {
|
||||
if (i2sOn || (dmaBufferCount < 3) || (dmaBufferBytes & 3)) {
|
||||
return false;
|
||||
}
|
||||
_buffers = dmaBufferCount;
|
||||
_bufferWords = dmaBufferBytes / 4;
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(ESP32) || defined(ESP8266)
|
||||
AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int use_apll) : AudioOutputI2S() {
|
||||
(void) port;
|
||||
(void) output_mode;
|
||||
(void) use_apll;
|
||||
SetBuffers(dma_buf_count, 128 * 4);
|
||||
#ifdef ESP32
|
||||
_useAPLL = use_apll;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if SOC_CLK_APLL_SUPPORTED
|
||||
bool AudioOutputI2S::SetUseAPLL() {
|
||||
if (i2sOn) {
|
||||
return false;
|
||||
}
|
||||
_useAPLL = true;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
AudioOutputI2S::AudioOutputI2S(long sampleRate, pin_size_t sck, pin_size_t data) {
|
||||
i2sOn = false;
|
||||
mono = false;
|
||||
bps = 16;
|
||||
channels = 2;
|
||||
hertz = sampleRate;
|
||||
AudioOutputI2S::AudioOutputI2S(long sampleRate, pin_size_t sck, pin_size_t data) : AudioOutputI2S() {
|
||||
(void) sampleRate;
|
||||
bclkPin = sck;
|
||||
wclkPin = sck + 1;
|
||||
doutPin = data;
|
||||
mclkPin = 0;
|
||||
use_mclk = false;
|
||||
swap_clocks = false;
|
||||
SetGain(1.0);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -73,81 +91,54 @@ AudioOutputI2S::~AudioOutputI2S() {
|
||||
stop();
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetPinout() {
|
||||
#ifdef ESP32
|
||||
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) {
|
||||
return false; // Not allowed
|
||||
}
|
||||
|
||||
i2s_pin_config_t pins = {
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
|
||||
.mck_io_num = use_mclk ? mclkPin : I2S_PIN_NO_CHANGE,
|
||||
#endif
|
||||
.bck_io_num = bclkPin,
|
||||
.ws_io_num = wclkPin,
|
||||
.data_out_num = doutPin,
|
||||
.data_in_num = I2S_PIN_NO_CHANGE
|
||||
};
|
||||
i2s_set_pin((i2s_port_t)portNo, &pins);
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout) {
|
||||
bclkPin = bclk;
|
||||
wclkPin = wclk;
|
||||
doutPin = dout;
|
||||
use_mclk = false;
|
||||
bool AudioOutputI2S::SwapClocks(bool swap) {
|
||||
if (i2sOn) {
|
||||
return SetPinout();
|
||||
return false;
|
||||
}
|
||||
if (swap) {
|
||||
auto t = bclkPin;
|
||||
bclkPin = wclkPin;
|
||||
wclkPin = t;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout, int mclk) {
|
||||
if (i2sOn) {
|
||||
return false;
|
||||
}
|
||||
bclkPin = bclk;
|
||||
wclkPin = wclk;
|
||||
doutPin = dout;
|
||||
#if defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
|
||||
mclkPin = mclk;
|
||||
use_mclk = true;
|
||||
if (i2sOn) {
|
||||
return SetPinout();
|
||||
}
|
||||
#else
|
||||
(void)mclk;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetRate(int hz) {
|
||||
if (this->hertz == hz) {
|
||||
if (hertz == hz) {
|
||||
return true;
|
||||
}
|
||||
this->hertz = hz;
|
||||
hertz = hz;
|
||||
if (i2sOn) {
|
||||
auto adj = AdjustI2SRate(hz); // Possibly scale for NoDAC
|
||||
#ifdef ESP32
|
||||
i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz));
|
||||
i2s_std_clk_config_t clk_cfg;
|
||||
clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG((uint32_t)adj);
|
||||
#if SOC_CLK_APLL_SUPPORTED
|
||||
clk_cfg.clk_src = _useAPLL ? i2s_clock_src_t::I2S_CLK_SRC_APLL : i2s_clock_src_t::I2S_CLK_SRC_DEFAULT;
|
||||
#endif
|
||||
i2s_channel_disable(_tx_handle);
|
||||
i2s_channel_reconfig_std_clock(_tx_handle, &clk_cfg);
|
||||
i2s_channel_enable(_tx_handle);
|
||||
#elif defined(ESP8266)
|
||||
i2s_set_rate(AdjustI2SRate(hz));
|
||||
i2s_set_rate(adj);
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
i2s.setFrequency(hz);
|
||||
i2s.setFrequency(adj);
|
||||
#endif
|
||||
}
|
||||
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;
|
||||
@@ -162,159 +153,92 @@ bool AudioOutputI2S::SetOutputModeMono(bool mono) {
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetLsbJustified(bool lsbJustified) {
|
||||
if (i2sOn) {
|
||||
return false;
|
||||
}
|
||||
this->lsb_justified = lsbJustified;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SwapClocks(bool swap_clocks) {
|
||||
if (i2sOn) {
|
||||
return false; // Not allowed
|
||||
}
|
||||
this->swap_clocks = swap_clocks;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetMclk(bool enabled) {
|
||||
(void)enabled;
|
||||
bool AudioOutputI2S::begin() {
|
||||
#ifdef ESP32
|
||||
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) {
|
||||
return false; // Not allowed
|
||||
}
|
||||
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
|
||||
chan_cfg.dma_desc_num = _buffers;
|
||||
chan_cfg.dma_frame_num = _bufferWords;
|
||||
assert(ESP_OK == i2s_new_channel(&chan_cfg, &_tx_handle, nullptr));
|
||||
|
||||
use_mclk = enabled;
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
use_mclk = enabled;
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(hertz),
|
||||
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
|
||||
.gpio_cfg = {
|
||||
.mclk = mclkPin < 0 ? I2S_GPIO_UNUSED : (gpio_num_t)mclkPin,
|
||||
.bclk = bclkPin < 0 ? I2S_GPIO_UNUSED : (gpio_num_t)bclkPin,
|
||||
.ws = wclkPin < 0 ? I2S_GPIO_UNUSED : (gpio_num_t)wclkPin,
|
||||
.dout = (gpio_num_t)doutPin,
|
||||
.din = I2S_GPIO_UNUSED,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false,
|
||||
},
|
||||
},
|
||||
};
|
||||
#if SOC_CLK_APLL_SUPPORTED
|
||||
std_cfg.clk_cfg.clk_src = _useAPLL ? i2s_clock_src_t::I2S_CLK_SRC_APLL : i2s_clock_src_t::I2S_CLK_SRC_DEFAULT;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
std_cfg.slot_cfg.bit_shift = !lsb_justified; // I2S = shift, LSBJ = no shift
|
||||
assert(ESP_OK == i2s_channel_init_std_mode(_tx_handle, &std_cfg));
|
||||
|
||||
bool AudioOutputI2S::begin(bool txDAC) {
|
||||
#ifdef ESP32
|
||||
if (!i2sOn) {
|
||||
if (use_apll == APLL_AUTO) {
|
||||
// don't use audio pll on buggy rev0 chips
|
||||
use_apll = APLL_DISABLE;
|
||||
//esp_chip_info_t out_info;
|
||||
//esp_chip_info(&out_info);
|
||||
//if (out_info.revision > 0)
|
||||
{
|
||||
use_apll = APLL_ENABLE;
|
||||
}
|
||||
}
|
||||
// Fill w/0s to start off
|
||||
int16_t a[2] = {0, 0};
|
||||
size_t written = 0;
|
||||
do {
|
||||
i2s_channel_preload_data(_tx_handle, (void*)a, sizeof(a), &written);
|
||||
} while (written);
|
||||
|
||||
i2s_mode_t mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
|
||||
if (output_mode == INTERNAL_DAC) {
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
mode = (i2s_mode_t)(mode | I2S_MODE_DAC_BUILT_IN);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
} else if (output_mode == INTERNAL_PDM) {
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
mode = (i2s_mode_t)(mode | I2S_MODE_PDM);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
i2s_comm_format_t comm_fmt;
|
||||
if (output_mode == INTERNAL_DAC) {
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)
|
||||
comm_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB;
|
||||
#else
|
||||
comm_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_I2S_MSB;
|
||||
#endif
|
||||
} else if (lsb_justified) {
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)
|
||||
comm_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB;
|
||||
#else
|
||||
comm_fmt = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB);
|
||||
#endif
|
||||
} else {
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)
|
||||
comm_fmt = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S);
|
||||
#else
|
||||
comm_fmt = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB);
|
||||
#endif
|
||||
}
|
||||
|
||||
i2s_config_t i2s_config_dac = {
|
||||
.mode = mode,
|
||||
.sample_rate = 44100,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
|
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
|
||||
.communication_format = comm_fmt,
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority
|
||||
.dma_buf_count = dma_buf_count,
|
||||
.dma_buf_len = 128,
|
||||
.use_apll = use_apll, // Use audio PLL
|
||||
.tx_desc_auto_clear = true, // Silence on underflow
|
||||
.fixed_mclk = use_mclk, // Unused
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256, // Unused
|
||||
.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT // Use bits per sample
|
||||
#endif
|
||||
};
|
||||
audioLogger->printf("+%d %p\n", portNo, &i2s_config_dac);
|
||||
if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_dac, 0, NULL) != ESP_OK) {
|
||||
audioLogger->println("ERROR: Unable to install I2S drives\n");
|
||||
}
|
||||
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) {
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
i2s_set_pin((i2s_port_t)portNo, NULL);
|
||||
i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
} else {
|
||||
SetPinout();
|
||||
}
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
}
|
||||
i2sOn = (ESP_OK == i2s_channel_enable(_tx_handle));
|
||||
#elif defined(ESP8266)
|
||||
(void)dma_buf_count;
|
||||
(void)use_apll;
|
||||
if (!i2sOn) {
|
||||
orig_bck = READ_PERI_REG(PERIPHS_IO_MUX_MTDO_U);
|
||||
orig_ws = READ_PERI_REG(PERIPHS_IO_MUX_GPIO2_U);
|
||||
#ifdef I2S_HAS_BEGIN_RXTX_DRIVE_CLOCKS
|
||||
if (!i2s_rxtxdrive_begin(false, true, false, txDAC)) {
|
||||
if (!i2s_rxtxdrive_begin(false, true, false, true)) {
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
if (!i2s_rxtx_begin(false, true)) {
|
||||
return false;
|
||||
}
|
||||
if (!txDAC) {
|
||||
audioLogger->printf_P(PSTR("I2SNoDAC: esp8266 arduino core should be upgraded to avoid conflicts with SPI\n"));
|
||||
}
|
||||
#endif
|
||||
i2sOn = true;
|
||||
}
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
(void)txDAC;
|
||||
if (!i2sOn) {
|
||||
i2s.setSysClk(hertz);
|
||||
i2s.setBCLK(bclkPin);
|
||||
if (wclkPin == bclkPin + 1) {
|
||||
i2s.setBCLK(bclkPin);
|
||||
} else if (wclkPin == bclkPin - 1) {
|
||||
// Swapped!
|
||||
i2s.setBCLK(bclkPin - 1);
|
||||
i2s.swapClocks();
|
||||
} else {
|
||||
audioLogger->printf_P(PSTR("I2S: BCLK and WCLK must be adjacent\n"));
|
||||
return false;
|
||||
}
|
||||
i2s.setDATA(doutPin);
|
||||
if (use_mclk) {
|
||||
if (mclkPin >= 0) {
|
||||
i2s.setMCLK(mclkPin);
|
||||
i2s.setMCLKmult(256);
|
||||
}
|
||||
if (swap_clocks) {
|
||||
i2s.swapClocks();
|
||||
}
|
||||
i2s.setBitsPerSample(bps);
|
||||
i2s.begin(hertz);
|
||||
i2s.setBitsPerSample(16);
|
||||
i2sOn = i2s.begin(hertz);
|
||||
}
|
||||
#endif
|
||||
i2sOn = true;
|
||||
SetRate(hertz ? hertz : 44100); // Default
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::ConsumeSample(int16_t sample[2]) {
|
||||
|
||||
//return if we haven't called ::begin yet
|
||||
if (!i2sOn) {
|
||||
return false;
|
||||
}
|
||||
@@ -332,18 +256,10 @@ bool AudioOutputI2S::ConsumeSample(int16_t sample[2]) {
|
||||
}
|
||||
#ifdef ESP32
|
||||
uint32_t s32;
|
||||
if (output_mode == INTERNAL_DAC) {
|
||||
int16_t l = Amplify(ms[LEFTCHANNEL]) + 0x8000;
|
||||
int16_t r = Amplify(ms[RIGHTCHANNEL]) + 0x8000;
|
||||
s32 = ((r & 0xffff) << 16) | (l & 0xffff);
|
||||
} else {
|
||||
s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
||||
}
|
||||
//"i2s_write_bytes" has been removed in the ESP32 Arduino 2.0.0, use "i2s_write" instead.
|
||||
// return i2s_write_bytes((i2s_port_t)portNo, (const char *)&s32, sizeof(uint32_t), 0);
|
||||
s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
||||
|
||||
size_t i2s_bytes_written;
|
||||
i2s_write((i2s_port_t)portNo, (const char*)&s32, sizeof(uint32_t), &i2s_bytes_written, 0);
|
||||
size_t i2s_bytes_written = sizeof(uint32_t);
|
||||
i2s_channel_write(_tx_handle, (const char*)&s32, sizeof(uint32_t), &i2s_bytes_written, 10);
|
||||
return i2s_bytes_written;
|
||||
#elif defined(ESP8266)
|
||||
uint32_t s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
||||
@@ -357,7 +273,7 @@ bool AudioOutputI2S::ConsumeSample(int16_t sample[2]) {
|
||||
void AudioOutputI2S::flush() {
|
||||
#ifdef ESP32
|
||||
// makes sure that all stored DMA samples are consumed / played
|
||||
int buffersize = 128 * this->dma_buf_count;
|
||||
int buffersize = _buffers * _bufferWords;
|
||||
int16_t samples[2] = {0x0, 0x0};
|
||||
for (int i = 0; i < buffersize; i++) {
|
||||
while (!ConsumeSample(samples)) {
|
||||
@@ -375,9 +291,8 @@ bool AudioOutputI2S::stop() {
|
||||
}
|
||||
|
||||
#ifdef ESP32
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
audioLogger->printf("UNINSTALL I2S\n");
|
||||
i2s_driver_uninstall((i2s_port_t)portNo); //stop & destroy i2s driver
|
||||
i2s_channel_disable(_tx_handle);
|
||||
i2s_del_channel(_tx_handle);
|
||||
#elif defined(ESP8266)
|
||||
i2s_end();
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
|
||||
+47
-27
@@ -25,60 +25,80 @@
|
||||
#if defined(ARDUINO_ARCH_RP2040)
|
||||
#include <Arduino.h>
|
||||
#include <I2S.h>
|
||||
#elif defined(ESP32)
|
||||
#include <driver/i2s_std.h>
|
||||
#endif
|
||||
|
||||
class AudioOutputI2S : public AudioOutput {
|
||||
public:
|
||||
#if defined(ESP32) || defined(ESP8266)
|
||||
AudioOutputI2S(int port = 0, int output_mode = EXTERNAL_I2S, int dma_buf_count = 8, int use_apll = APLL_DISABLE);
|
||||
#ifndef ESP8266
|
||||
[[deprecated("Use AudioOutputI2S(dmaBuffers, dmaBufferBytes) for normal I2S, AudioOutputPDM() for PDM mode, or AudioOutputInternalDAC() for DAC mode")]]
|
||||
#endif
|
||||
AudioOutputI2S(int port, int output_mode = EXTERNAL_I2S, int dma_buf_count = 8, int use_apll = APLL_DISABLE);
|
||||
enum : int { APLL_AUTO = -1, APLL_ENABLE = 1, APLL_DISABLE = 0 };
|
||||
enum : int { EXTERNAL_I2S = 0, INTERNAL_DAC = 1, INTERNAL_PDM = 2 };
|
||||
AudioOutputI2S();
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
AudioOutputI2S(long sampleRate = 44100, pin_size_t sck = 26, pin_size_t data = 28);
|
||||
[[deprecated]] AudioOutputI2S(long sampleRate, pin_size_t sck = 26, pin_size_t data = 28);
|
||||
AudioOutputI2S();
|
||||
#endif
|
||||
bool SetPinout(int bclkPin, int wclkPin, int doutPin);
|
||||
bool SetPinout(int bclkPin, int wclkPin, int doutPin, int mclkPin);
|
||||
#if defined(ESP32) || defined(ESP8266)
|
||||
#define __DMA_BUFF_BYTES (4608 / 2)
|
||||
#else
|
||||
#define __DMA_BUFF_BYTES 4608
|
||||
#endif
|
||||
bool SetBuffers(int dmaBufferCount = 5, int dmaBufferBytes = __DMA_BUFF_BYTES);
|
||||
#undef __DMA_BUFF_BYTES
|
||||
bool SetPinout(int bclkPin, int wclkPin, int doutPin, int mclkPin = -1);
|
||||
|
||||
#if defined(ESP32) && SOC_CLK_APLL_SUPPORTED
|
||||
bool SetUseAPLL();
|
||||
#endif
|
||||
|
||||
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);
|
||||
}
|
||||
virtual bool begin() override;
|
||||
virtual bool ConsumeSample(int16_t sample[2]) override;
|
||||
virtual void flush() override;
|
||||
virtual bool stop() override;
|
||||
|
||||
bool begin(bool txDAC);
|
||||
[[deprecated("Use AudioOutputInternalDAC() for DAC output, begin(void) for normal I2S")]] bool begin(bool txDAC);
|
||||
bool SetOutputModeMono(bool mono); // Force mono output no matter the input
|
||||
bool SetLsbJustified(bool lsbJustified); // Allow supporting non-I2S chips, e.g. PT8211
|
||||
bool SetMclk(bool enabled); // Enable MCLK output (if supported)
|
||||
bool SwapClocks(bool swap_clocks); // Swap BCLK and WCLK
|
||||
[[deprecated("Use SetPinout() with an MCLK pin to enable")]] bool SetMclk(bool enabled) {
|
||||
(void) enabled;
|
||||
return true;
|
||||
}
|
||||
[[deprecated("Use SetPinout() and specify the bclk/wclk to automatically set the clock swapping")]] bool SwapClocks(bool swap_clocks); // Swap BCLK and WCLK
|
||||
|
||||
protected:
|
||||
bool SetPinout();
|
||||
virtual int AdjustI2SRate(int hz) {
|
||||
return hz;
|
||||
}
|
||||
uint8_t portNo;
|
||||
int output_mode;
|
||||
bool mono;
|
||||
int lsb_justified;
|
||||
bool lsb_justified;
|
||||
bool i2sOn;
|
||||
int dma_buf_count;
|
||||
int use_apll;
|
||||
bool use_mclk;
|
||||
bool swap_clocks;
|
||||
|
||||
int8_t bclkPin;
|
||||
int8_t wclkPin;
|
||||
int8_t doutPin;
|
||||
int8_t mclkPin;
|
||||
|
||||
size_t _buffers;
|
||||
size_t _bufferWords;
|
||||
|
||||
#ifdef ESP32
|
||||
bool _useAPLL;
|
||||
// I2S IDF object and info
|
||||
i2s_chan_handle_t _tx_handle;
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
// Normal software-defined I2S
|
||||
I2S i2s;
|
||||
#elif defined(ESP8266)
|
||||
// We can restore the old values and free up these pins when in NoDAC mode
|
||||
uint32_t orig_bck;
|
||||
uint32_t orig_ws;
|
||||
|
||||
uint8_t bclkPin;
|
||||
uint8_t wclkPin;
|
||||
uint8_t doutPin;
|
||||
uint8_t mclkPin;
|
||||
|
||||
#if defined(ARDUINO_ARCH_RP2040)
|
||||
I2S i2s;
|
||||
#endif
|
||||
};
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#include "driver/i2s.h"
|
||||
#include <driver/i2s_std.h>
|
||||
#elif defined(ARDUINO_ARCH_RP2040) || ARDUINO_ESP8266_MAJOR >= 3
|
||||
#include <I2S.h>
|
||||
#elif ARDUINO_ESP8266_MAJOR < 3
|
||||
@@ -39,7 +39,8 @@
|
||||
// So this new constructor adds the ability to pass both port and sck to the underlying class, but
|
||||
// uses the same defaults in the AudioOutputI2S constructor.
|
||||
//
|
||||
AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port, int sck) : AudioOutputI2S(44100, sck, port) {
|
||||
AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port, int sck) : AudioOutputI2S() {
|
||||
SetPinout(sck, sck + 1, port); // TODO - allow RP2040 to disable unused pins like ESP
|
||||
SetOversampling(32);
|
||||
lastSamp = 0;
|
||||
cumErr = 0;
|
||||
@@ -47,7 +48,8 @@ AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port, int sck) : AudioOutputI2S(441
|
||||
|
||||
#else
|
||||
|
||||
AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port) : AudioOutputI2S(port, false) {
|
||||
AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port) : AudioOutputI2S() {
|
||||
SetPinout(-1, -1, port);
|
||||
SetOversampling(32);
|
||||
lastSamp = 0;
|
||||
cumErr = 0;
|
||||
@@ -59,8 +61,8 @@ AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port) : AudioOutputI2S(port, false)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
AudioOutputI2SNoDAC::~AudioOutputI2SNoDAC() {
|
||||
stop();
|
||||
}
|
||||
|
||||
bool AudioOutputI2SNoDAC::SetOversampling(int os) {
|
||||
@@ -120,11 +122,9 @@ bool AudioOutputI2SNoDAC::ConsumeSample(int16_t sample[2]) {
|
||||
|
||||
// Either send complete pulse stream or nothing
|
||||
#ifdef ESP32
|
||||
size_t i2s_bytes_written;
|
||||
i2s_write((i2s_port_t)portNo, (const char *)dsBuff, sizeof(uint32_t) * (oversample / 32), &i2s_bytes_written, 0);
|
||||
if (!i2s_bytes_written) {
|
||||
return false;
|
||||
}
|
||||
size_t i2s_bytes_written = sizeof(uint32_t);
|
||||
i2s_channel_write(_tx_handle, (const char *)dsBuff, sizeof(uint32_t) * (oversample / 32), &i2s_bytes_written, 10);
|
||||
return i2s_bytes_written ? true : false;
|
||||
#elif defined(ESP8266)
|
||||
if (!i2s_write_sample_nb(dsBuff[0])) {
|
||||
return false; // No room at the inn
|
||||
|
||||
@@ -35,9 +35,6 @@ public:
|
||||
#endif
|
||||
|
||||
virtual ~AudioOutputI2SNoDAC() override;
|
||||
virtual bool begin() override {
|
||||
return AudioOutputI2S::begin(false);
|
||||
}
|
||||
virtual bool ConsumeSample(int16_t sample[2]) override;
|
||||
|
||||
bool SetOversampling(int os);
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
|
||||
#pragma once
|
||||
#if defined(ESP32) && SOC_DAC_SUPPORTED
|
||||
#include "AudioOutput.h"
|
||||
#include <driver/dac_continuous.h>
|
||||
|
||||
@@ -7,7 +10,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);
|
||||
@@ -17,7 +20,7 @@ class AudioOutputInternalDAC : public AudioOutput {
|
||||
dac_continuous_config_t dac_config = {
|
||||
.chan_mask = channels == 2 ? DAC_CHANNEL_MASK_ALL : DAC_CHANNEL_MASK_CH0,
|
||||
.desc_num = 2, // we dont need more
|
||||
.buf_size = buffer_sz,
|
||||
.buf_size = (size_t)buffer_sz,
|
||||
.freq_hz = hertz,
|
||||
.offset = 0,
|
||||
.clk_src = DAC_DIGI_CLK_SRC_APLL,
|
||||
@@ -93,11 +96,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 +109,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 +143,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;
|
||||
@@ -167,3 +152,4 @@ public:
|
||||
return true;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
AudioOutputPDM
|
||||
|
||||
Copyright (C) 2025 Earle F. Philhower, III
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if defined(ESP32)
|
||||
|
||||
#include "AudioOutputPDM.h"
|
||||
|
||||
#if SOC_I2S_SUPPORTS_PDM_TX
|
||||
|
||||
AudioOutputPDM::AudioOutputPDM(int pin) : AudioOutputI2S() {
|
||||
i2sOn = false;
|
||||
pdmPin = pin;
|
||||
channels = 2;
|
||||
hertz = 44100; // Needs to be application-set
|
||||
SetGain(1.0);
|
||||
SetBuffers(); // Default DMA sizes
|
||||
}
|
||||
|
||||
AudioOutputPDM::~AudioOutputPDM() {
|
||||
stop();
|
||||
}
|
||||
|
||||
bool AudioOutputPDM::SetRate(int hz) {
|
||||
if (hertz == hz) {
|
||||
return true;
|
||||
}
|
||||
hertz = hz;
|
||||
if (i2sOn) {
|
||||
auto adj = AdjustI2SRate(hz); // Possibly scale for NoDAC
|
||||
i2s_pdm_tx_clk_config_t clk_cfg;
|
||||
clk_cfg = I2S_PDM_TX_CLK_DEFAULT_CONFIG((uint32_t)adj);
|
||||
clk_cfg.up_sample_fp = 960;
|
||||
clk_cfg.up_sample_fs = 480;
|
||||
i2s_channel_disable(_tx_handle);
|
||||
i2s_channel_reconfig_pdm_tx_clock(_tx_handle, &clk_cfg);
|
||||
i2s_channel_enable(_tx_handle);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputPDM::begin() {
|
||||
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
|
||||
chan_cfg.dma_desc_num = _buffers;
|
||||
chan_cfg.dma_frame_num = _bufferWords;
|
||||
assert(ESP_OK == i2s_new_channel(&chan_cfg, &_tx_handle, nullptr));
|
||||
|
||||
i2s_pdm_tx_config_t pdm_cfg = {
|
||||
.clk_cfg = I2S_PDM_TX_CLK_DEFAULT_CONFIG(AdjustI2SRate(hertz)),
|
||||
.slot_cfg = I2S_PDM_TX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
|
||||
.gpio_cfg = {
|
||||
.clk = I2S_GPIO_UNUSED,
|
||||
.dout = (gpio_num_t)pdmPin,
|
||||
.dout2 = I2S_GPIO_UNUSED,
|
||||
.invert_flags = {
|
||||
.clk_inv = false,
|
||||
},
|
||||
},
|
||||
};
|
||||
pdm_cfg.slot_cfg.data_fmt = I2S_PDM_DATA_FMT_PCM;
|
||||
pdm_cfg.clk_cfg.up_sample_fp = 960;
|
||||
pdm_cfg.clk_cfg.up_sample_fs = 480;
|
||||
assert(ESP_OK == i2s_channel_init_pdm_tx_mode(_tx_handle, &pdm_cfg));
|
||||
|
||||
// Fill w/0s to start off
|
||||
int16_t a[2] = {0, 0};
|
||||
size_t written = 0;
|
||||
do {
|
||||
i2s_channel_preload_data(_tx_handle, (void*)a, sizeof(a), &written);
|
||||
} while (written);
|
||||
|
||||
i2sOn = (ESP_OK == i2s_channel_enable(_tx_handle));
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
AudioOutputPDM
|
||||
Base class for a ESP32 PDM output port
|
||||
|
||||
Copyright (C) 20125 Earle F. Philhower, III
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(ESP32)
|
||||
|
||||
#include <driver/i2s_pdm.h>
|
||||
|
||||
#if SOC_I2S_SUPPORTS_PDM_TX
|
||||
|
||||
#include "AudioOutput.h"
|
||||
#include "AudioOutputI2S.h"
|
||||
|
||||
class AudioOutputPDM : public AudioOutputI2S {
|
||||
public:
|
||||
AudioOutputPDM(int pin = 0);
|
||||
|
||||
bool SetPinout(int pdmPin);
|
||||
|
||||
virtual ~AudioOutputPDM() override;
|
||||
virtual bool SetRate(int hz) override;
|
||||
virtual bool begin() override;
|
||||
|
||||
protected:
|
||||
virtual int AdjustI2SRate(int hz) {
|
||||
// TODO - There is some fixed off-by-ratio 1/1.25 in the PDM output clock vs. the PCM input data at IDF 5.5
|
||||
hz *= 8;
|
||||
hz /= 10;
|
||||
return hz;
|
||||
}
|
||||
|
||||
int8_t pdmPin;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
+90
-110
@@ -41,8 +41,8 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#if defined(ESP32)
|
||||
#include "driver/i2s.h"
|
||||
#include "soc/rtc.h"
|
||||
#include <driver/i2s_std.h>
|
||||
#include <soc/rtc.h>
|
||||
#elif defined(ESP8266)
|
||||
#include "driver/SinglePinI2SDriver.h"
|
||||
#endif
|
||||
@@ -84,123 +84,111 @@ static const uint16_t spdif_bmclookup[256] PROGMEM = {
|
||||
0x32aa, 0xb2aa, 0xd2aa, 0x52aa, 0xcaaa, 0x4aaa, 0x2aaa, 0xaaaa
|
||||
};
|
||||
|
||||
AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count) {
|
||||
this->portNo = port;
|
||||
#if defined(ESP32)
|
||||
// Configure ESP32 I2S to roughly compatible to ESP8266 peripheral
|
||||
i2s_config_t i2s_config_spdif = {
|
||||
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
|
||||
.sample_rate = 88200, // 2 x sampling_rate
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, // 32bit words
|
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // Right than left
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)
|
||||
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S),
|
||||
#else
|
||||
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
|
||||
#endif
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority
|
||||
.dma_buf_count = dma_buf_count,
|
||||
.dma_buf_len = DMA_BUF_SIZE_DEFAULT, // bigger buffers, reduces interrupts
|
||||
.use_apll = true, // Audio PLL is needed for low clock jitter
|
||||
.tx_desc_auto_clear = true, // Silence on underflow
|
||||
.fixed_mclk = 0, // Unused
|
||||
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_512, // Unused
|
||||
.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT // Use bits per sample
|
||||
#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT, // Unused
|
||||
.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT // Use bits per sample
|
||||
#endif
|
||||
};
|
||||
if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_spdif, 0, NULL) != ESP_OK) {
|
||||
audioLogger->println(F("ERROR: Unable to install I2S drivers"));
|
||||
return;
|
||||
}
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
SetPinout(I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, dout_pin);
|
||||
|
||||
AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin) {
|
||||
doutPin = dout_pin;
|
||||
#ifdef ESP32
|
||||
rate_multiplier = 2; // 2x32bit words
|
||||
#elif defined(ESP8266)
|
||||
(void) dout_pin;
|
||||
if (!I2SDriver.begin(dma_buf_count, DMA_BUF_SIZE_DEFAULT)) {
|
||||
audioLogger->println(F("ERROR: Unable to start I2S driver"));
|
||||
return;
|
||||
}
|
||||
#else
|
||||
rate_multiplier = 4; // 4x16 bit words
|
||||
#endif
|
||||
i2sOn = true;
|
||||
mono = false;
|
||||
bps = 16;
|
||||
channels = 2;
|
||||
frame_num = 0;
|
||||
SetGain(1.0);
|
||||
hertz = 0;
|
||||
SetRate(44100);
|
||||
frame_num = 0;
|
||||
hertz = 44100;
|
||||
i2sOn = false;
|
||||
SetBuffers(DMA_BUF_COUNT_DEFAULT, DMA_BUF_SIZE_DEFAULT * 4);
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::SetBuffers(int dmaBufferCount, int dmaBufferBytes) {
|
||||
if (i2sOn || (dmaBufferCount < 3) || (dmaBufferBytes & 3)) {
|
||||
return false;
|
||||
}
|
||||
_buffers = dmaBufferCount;
|
||||
_bufferWords = dmaBufferBytes / 4;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count) : AudioOutputSPDIF(dout_pin) {
|
||||
(void) port;
|
||||
(void) dma_buf_count;
|
||||
SetBuffers(dma_buf_count, DMA_BUF_SIZE_DEFAULT * 4);
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::begin() {
|
||||
if (i2sOn) {
|
||||
return false;
|
||||
}
|
||||
#ifdef ESP32
|
||||
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
|
||||
chan_cfg.dma_desc_num = _buffers;
|
||||
chan_cfg.dma_frame_num = _bufferWords;
|
||||
assert(ESP_OK == i2s_new_channel(&chan_cfg, &_tx_handle, nullptr));
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(((uint32_t)hertz) * 2),
|
||||
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO),
|
||||
.gpio_cfg = {
|
||||
.mclk = I2S_GPIO_UNUSED,
|
||||
.bclk = I2S_GPIO_UNUSED,
|
||||
.ws = I2S_GPIO_UNUSED,
|
||||
.dout = (gpio_num_t)doutPin,
|
||||
.din = I2S_GPIO_UNUSED,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false,
|
||||
},
|
||||
},
|
||||
};
|
||||
#if SOC_CLK_APLL_SUPPORTED
|
||||
std_cfg.clk_cfg.clk_src = i2s_clock_src_t::I2S_CLK_SRC_APLL;
|
||||
#endif
|
||||
assert(ESP_OK == i2s_channel_init_std_mode(_tx_handle, &std_cfg));
|
||||
#else
|
||||
if (!I2SDriver.begin(_buffers, _bufferWords)) {
|
||||
audioLogger->println(F("ERROR: Unable to start I2S driver"));
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
i2sOn = true;
|
||||
mono = false;
|
||||
channels = 2;
|
||||
frame_num = 0;
|
||||
SetGain(1.0);
|
||||
return true;
|
||||
}
|
||||
|
||||
AudioOutputSPDIF::~AudioOutputSPDIF() {
|
||||
#if defined(ESP32)
|
||||
if (i2sOn) {
|
||||
i2s_stop((i2s_port_t)this->portNo);
|
||||
audioLogger->printf("UNINSTALL I2S\n");
|
||||
i2s_driver_uninstall((i2s_port_t)this->portNo); //stop & destroy i2s driver
|
||||
}
|
||||
#elif defined(ESP8266)
|
||||
if (i2sOn) {
|
||||
I2SDriver.stop();
|
||||
}
|
||||
#endif
|
||||
i2sOn = false;
|
||||
stop();
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::SetPinout(int bclk, int wclk, int dout) {
|
||||
#if defined(ESP32)
|
||||
i2s_pin_config_t pins = {
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
|
||||
.mck_io_num = 0, // unused
|
||||
#endif
|
||||
.bck_io_num = bclk,
|
||||
.ws_io_num = wclk,
|
||||
.data_out_num = dout,
|
||||
.data_in_num = I2S_PIN_NO_CHANGE
|
||||
};
|
||||
if (i2s_set_pin((i2s_port_t)portNo, &pins) != ESP_OK) {
|
||||
audioLogger->println("ERROR setting up S/PDIF I2S pins\n");
|
||||
bool AudioOutputSPDIF::SetPinout(int dout) {
|
||||
if (i2sOn) {
|
||||
return false;
|
||||
}
|
||||
doutPin = dout;
|
||||
return true;
|
||||
#else
|
||||
(void) bclk;
|
||||
(void) wclk;
|
||||
(void) dout;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::SetRate(int hz) {
|
||||
if (!i2sOn) {
|
||||
return false;
|
||||
}
|
||||
if (hz < 32000) {
|
||||
return false;
|
||||
}
|
||||
if (hz == this->hertz) {
|
||||
if (hz == hertz) {
|
||||
return true;
|
||||
}
|
||||
this->hertz = hz;
|
||||
hertz = hz;
|
||||
int adjustedHz = AdjustI2SRate(hz);
|
||||
#if defined(ESP32)
|
||||
if (i2s_set_sample_rates((i2s_port_t)portNo, adjustedHz) == ESP_OK) {
|
||||
#if defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR < 3)
|
||||
if (adjustedHz == 88200) {
|
||||
// Manually fix the APLL rate for 44100.
|
||||
// See: https://github.com/espressif/esp-idf/issues/2634
|
||||
// sdm0 = 28, sdm1 = 8, sdm2 = 5, odir = 0 -> 88199.977
|
||||
rtc_clk_apll_enable(1, 28, 8, 5, 0);
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
audioLogger->println("ERROR changing S/PDIF sample rate");
|
||||
}
|
||||
i2s_std_clk_config_t clk_cfg;
|
||||
clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG((uint32_t)adjustedHz);
|
||||
i2s_channel_disable(_tx_handle);
|
||||
i2s_channel_reconfig_std_clock(_tx_handle, &clk_cfg);
|
||||
i2s_channel_enable(_tx_handle);
|
||||
#elif defined(ESP8266)
|
||||
I2SDriver.setRate(adjustedHz);
|
||||
audioLogger->printf_P(PSTR("S/PDIF rate set: %.3f\n"), I2SDriver.getActualRate() / 4);
|
||||
@@ -208,14 +196,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;
|
||||
@@ -233,10 +213,6 @@ bool AudioOutputSPDIF::SetOutputModeMono(bool mono) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::begin() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2]) {
|
||||
if (!i2sOn) {
|
||||
return true; // Sink the data
|
||||
@@ -291,7 +267,7 @@ bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2]) {
|
||||
#if defined(ESP32)
|
||||
// Assume DMA buffers are multiples of 16 bytes. Either we write all bytes or none.
|
||||
size_t bytes_written;
|
||||
esp_err_t ret = i2s_write((i2s_port_t)portNo, (const char*)&buf, 8 * channels, &bytes_written, 0);
|
||||
esp_err_t ret = i2s_channel_write(_tx_handle, (const char*)&buf, 8 * channels, &bytes_written, 10);
|
||||
// If we didn't write all bytes, return false early and do not increment frame_num
|
||||
if ((ret != ESP_OK) || (bytes_written != (8 * channels))) {
|
||||
return false;
|
||||
@@ -309,12 +285,16 @@ bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2]) {
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::stop() {
|
||||
if (i2sOn) {
|
||||
i2sOn = false;
|
||||
#if defined(ESP32)
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
i2s_channel_disable(_tx_handle);
|
||||
i2s_del_channel(_tx_handle);
|
||||
#elif defined(ESP8266)
|
||||
I2SDriver.stop();
|
||||
I2SDriver.stop();
|
||||
#endif
|
||||
frame_num = 0;
|
||||
frame_num = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+22
-5
@@ -30,9 +30,14 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if defined(ESP32) || defined(ESP8266)
|
||||
#pragma once
|
||||
|
||||
#if defined(ESP32) || defined(ESP8266)
|
||||
|
||||
#if defined(ESP32)
|
||||
#include <driver/i2s_std.h>
|
||||
#endif
|
||||
|
||||
#include "AudioOutput.h"
|
||||
|
||||
#if defined(ESP32)
|
||||
@@ -47,11 +52,17 @@
|
||||
|
||||
class AudioOutputSPDIF : public AudioOutput {
|
||||
public:
|
||||
AudioOutputSPDIF(int dout_pin = SPDIF_OUT_PIN_DEFAULT, int port = 0, int dma_buf_count = DMA_BUF_COUNT_DEFAULT);
|
||||
AudioOutputSPDIF(int dout_pin = SPDIF_OUT_PIN_DEFAULT);
|
||||
[[deprecated("Use AudioOutputSPDIF() and SetBuffers/SetPinout to configure")]] AudioOutputSPDIF(int dout_pin, int port0, int dma_buf_count = DMA_BUF_COUNT_DEFAULT);
|
||||
virtual ~AudioOutputSPDIF() override;
|
||||
bool SetPinout(int bclkPin, int wclkPin, int doutPin);
|
||||
[[deprecated("Use SetPinout(spdifPin) instead")]] bool SetPinout(int bclkPin, int wclkPin, int doutPin) {
|
||||
(void) bclkPin;
|
||||
(void) wclkPin;
|
||||
return SetPinout(doutPin);
|
||||
}
|
||||
bool SetPinout(int doutPin);
|
||||
bool SetBuffers(int dmaBuffers = DMA_BUF_COUNT_DEFAULT, int dmaBufferBytes = DMA_BUF_SIZE_DEFAULT * 4);
|
||||
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;
|
||||
@@ -67,11 +78,17 @@ protected:
|
||||
virtual inline int AdjustI2SRate(int hz) {
|
||||
return rate_multiplier * hz;
|
||||
}
|
||||
uint8_t portNo;
|
||||
bool mono;
|
||||
bool i2sOn;
|
||||
int8_t doutPin;
|
||||
uint8_t frame_num;
|
||||
uint8_t rate_multiplier;
|
||||
size_t _buffers;
|
||||
size_t _bufferWords;
|
||||
#ifdef ESP32
|
||||
i2s_chan_handle_t _tx_handle;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
#endif // _AUDIOOUTPUTSPDIF_H
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+15
-11
@@ -18,16 +18,14 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32) && SOC_DAC_SUPPORTED
|
||||
|
||||
#include "AudioOutputULP.h"
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
|
||||
#include <esp32/ulp.h>
|
||||
#include <driver/rtc_io.h>
|
||||
#include <soc/rtc_io_reg.h>
|
||||
#include <driver/dac.h>
|
||||
#include <driver/dac_oneshot.h>
|
||||
#include <soc/rtc.h>
|
||||
#include <math.h>
|
||||
|
||||
@@ -70,13 +68,18 @@ bool AudioOutputULP::begin() {
|
||||
unsigned long rtc_fast_freq_hz = 1000000ULL * (1 << RTC_CLK_CAL_FRACT) * 256 / rtc_8md256_period;
|
||||
|
||||
//initialize DACs
|
||||
dac_oneshot_handle_t d0;
|
||||
dac_oneshot_handle_t d1;
|
||||
|
||||
if (activeDACs & 1) {
|
||||
dac_output_enable(DAC_CHANNEL_1);
|
||||
dac_output_voltage(DAC_CHANNEL_1, 128);
|
||||
dac_oneshot_config_t a = { DAC_CHAN_0 };
|
||||
assert(ESP_OK == dac_oneshot_new_channel(&a, &d0));
|
||||
dac_oneshot_output_voltage(d0, 128);
|
||||
}
|
||||
if (activeDACs & 2) {
|
||||
dac_output_enable(DAC_CHANNEL_2);
|
||||
dac_output_voltage(DAC_CHANNEL_2, 128);
|
||||
dac_oneshot_config_t b = { DAC_CHAN_1 };
|
||||
assert(ESP_OK == dac_oneshot_new_channel(&b, &d1));
|
||||
dac_oneshot_output_voltage(d1, 128);
|
||||
}
|
||||
|
||||
int retAddress1 = 9;
|
||||
@@ -263,14 +266,15 @@ bool AudioOutputULP::stop() {
|
||||
ulp_run(0);
|
||||
|
||||
if (activeDACs & 1) {
|
||||
dac_output_voltage(DAC_CHANNEL_1, 128);
|
||||
dac_oneshot_output_voltage(d0, 128);
|
||||
dac_oneshot_del_channel(d0);
|
||||
}
|
||||
if (activeDACs & 2) {
|
||||
dac_output_voltage(DAC_CHANNEL_2, 128);
|
||||
dac_oneshot_output_voltage(d1, 128);
|
||||
dac_oneshot_del_channel(d1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -34,7 +34,9 @@
|
||||
|
||||
#include "AudioOutput.h"
|
||||
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32) && SOC_DAC_SUPPORTED
|
||||
|
||||
#include <driver/dac_oneshot.h>
|
||||
|
||||
class AudioOutputULP : public AudioOutput {
|
||||
public:
|
||||
@@ -54,6 +56,8 @@ private:
|
||||
uint8_t bufferedOddSample = 128;
|
||||
bool waitingOddSample = true; // must be set to false for mono output
|
||||
int activeDACs = 3; // 1:DAC1; 2:DAC2; 3:both;
|
||||
dac_oneshot_handle_t d0;
|
||||
dac_oneshot_handle_t d1;
|
||||
bool stereoOutput = true;
|
||||
const int opcodeCount = 20;
|
||||
const uint32_t dacTableStart1 = 2048 - 512;
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ while [ $(echo $sketches | wc -w) -gt 0 ]; do
|
||||
if [ 0"$werror" -gt 0 ]; then
|
||||
./arduino-cli compile -b "$fqbn" -v --warnings all \
|
||||
--build-property "compiler.c.extra_flags=-Wall -Wextra -Werror -Wno-ignored-qualifiers" \
|
||||
--build-property "compiler.cpp.extra_flags=-Wall -Wextra -Werror -Wno-ignored-qualifiers -Wno-overloaded-virtual" \
|
||||
--build-property "compiler.cpp.extra_flags=-Wall -Wextra -Werror -Wno-ignored-qualifiers -Wno-overloaded-virtual" \
|
||||
--build-path "$outdir" "$sketch" || exit 255
|
||||
else
|
||||
./arduino-cli compile -b "$fqbn" -v --warnings all --build-path "$outdir" "$sketch" || exit 255
|
||||
|
||||
Reference in new issue
Block a user