Add ESP32 PDM 1-bit output support (#784)
Borrowed from BackgroundAudio PDM implementation https://github.com/earlephilhower/BackgroundAudio Requires an ESP32 model with PDM support.
This commit is contained in:
1 parent
e0143bbf37
commit
3032072561
4 files changed
+11448
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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user