7 Commits
Author SHA1 Message Date
Earle F. Philhower, III 74fc1f09bb Fix C23 compilation errors (#814) 2026-07-21 08:38:24 -07:00
Wojtek Kaniewski 05f2fb0045 Fix memory corruption in AudioGeneratorAAC on non-ESP8266 platforms (#807)
The description of `AACDecode()` function states that the output buffer
must be big enough for 1024 samples per channel _or_ 2048 if SBR is
enabled. By default SBR is enabled on non-ESP8266 platforms, yet the
buffer in `AudioGeneratorAAC` is always 1024 samples per channel long.
This causes memory corruption on ESP32 and possibly Pi Pico. This change
makes the output buffer correct.
2026-03-18 07:30:39 -07:00
Александр 70defdb6c6 Fix u8 upsampling and mono mode for AudioGeneratorWAV (#802)
For AudioGeneratorWAV, the upsample from u8 to i16 bits has been fixed. Previously, playback of wav files with u8 encoding resulted in silence. The data was read, but not used in calculations. Additionally, in mono mode, I removed the unnecessary resolution increase for the second channel and implemented simple duplication. Now, when playing mono wav files, the audio will be in both the left and right channels.
2025-12-26 18:00:46 -08:00
Earle F. Philhower, III 3450935a2a Add new project link 2025-12-09 14:50:39 -08:00
NeoCat 94c68ecbaa Call i2s_channel_enable(_tx_handle) in AudioOutputSPDIF::begin() (#799)
i2s_channel_enable(_tx_handle) was only called within
AudioOutputSPDIF::SetRate(), which caused i2s_channel_write to fail
if the rate was the same with the default 44100Hz.
This fixes the issue by calling it in begin().
2025-11-12 15:29:27 -08:00
Earle F. Philhower, III cd98586a2c Suggest using latest pioarduino 2025-10-31 08:37:22 -07:00
Earle F. Philhower, III 89c60ad19b ESP32 Platform.IO information
Added section on ESP32 and Platform.IO integration issues and solutions.
2025-10-27 09:36:25 -07:00
9 changed files with 45 additions and 21 deletions

No files matched your search

+19 -1
View File
@@ -26,7 +26,8 @@ If you have a neat use for this library, [I'd love to hear about it](mailto:earl
* A neat MQTT-driven ESP8266 light-and-sound device (alarm? toy? who can say!) was built by @CosmicMac, available at https://github.com/CosmicMac/ESParkle
* A very interesting "linear clock" with a stepper motor, NTP time keeping, and configurable recorded chimes with schematics, 3D printer plans, and source code, is now available https://janderogee.com/projects/linear_clock/linear_clock.htm
* Source and instructions for a gorgeous wooden MP3-playing clock, FM radio and a walkie-talkie using the ESP8266 and AVR microcontrollers is available https://github.com/zduka/mp3-player
* Automate your negativity with a speaking, automatic rejection project at https://www.hackster.io/abouhatab/getting-rejected-by-wemos-d1-mini-f8be9c
## Prerequisites
First, make sure you are running the 2.6.3/later or GIT head version of the Arduino libraries for ESP8266, or the latest ESP32 SDK from Espressif.
@@ -51,6 +52,23 @@ Create an AudioInputXXX source pointing to your input file, an AudioOutputXXX si
After creation, you need to call the AudioGeneratorXXX::loop() routine from inside your own main loop() one or more times. This will automatically read as much of the file as needed and fill up the I2S buffers and immediately return. Since this is not interrupt driven, if you have large delay()s in your code, you may end up with hiccups in playback. Either break large delays into very small ones with calls to AudioGenerator::loop(), or reduce the sampling rate to require fewer samples per second.
## ESP32 and Platform.IO
It seems that Espressif discontinued official Platform.IO integration of their Arduino several
releases ago. The `platformio/framework-arduinoespressif32` package available is much older
than the latest Espressif Arduino core and at IDF 4.x. This library needs IDF 5.x and the new
I2S APIs to function, so if you attempt to build using ``platformio/framework-arduinoespressif32``
you will get errors like, `cannot open source file "driver/i2s_std.h"`.
The solution is to move to @Jason2866's community
[pioarduino](https://github.com/pioarduino/platform-espressif32) core which is built from the
current Espressif Arduino with IDF 5.x. Simply change `platform` in your `platform.ini` as below:
```ini
; Pioarduino Arduino-ESP32 Latest
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
```
## Example
See the examples directory for some simple examples, but the following snippet can play an MP3 file over the simulated I2S DAC:
```cpp
+3 -3
View File
@@ -31,7 +31,7 @@ AudioGeneratorAAC::AudioGeneratorAAC() {
output = NULL;
buff = (uint8_t*)malloc(buffLen);
outSample = (int16_t*)malloc(1024 * 2 * sizeof(uint16_t));
outSample = (int16_t*)malloc(outSampleLen * sizeof(uint16_t));
if (!buff || !outSample) {
audioLogger->printf_P(PSTR("ERROR: Out of memory in AAC\n"));
Serial.flush();
@@ -63,7 +63,7 @@ AudioGeneratorAAC::AudioGeneratorAAC(void *preallocateData, int preallocateSz) {
buff = (uint8_t*) p;
p += (buffLen + 7) & ~7;
outSample = (int16_t*) p;
p += (1024 * 2 * sizeof(int16_t) + 7) & ~7;
p += (outSampleLen * sizeof(int16_t) + 7) & ~7;
int used = p - (uint8_t*)preallocateSpace;
int availSpace = preallocateSize - used;
if (availSpace < 0) {
@@ -212,7 +212,7 @@ bool AudioGeneratorAAC::begin(AudioFileSource *source, AudioOutput *output) {
output->begin();
memset(buff, 0, buffLen);
memset(outSample, 0, 1024 * 2 * sizeof(int16_t));
memset(outSample, 0, outSampleLen * sizeof(int16_t));
running = true;
+6 -1
View File
@@ -49,7 +49,12 @@ protected:
bool FillBufferWithValidFrame(); // Read until we get a valid syncword and min(feof, 2048) butes in the buffer
// Output buffering
int16_t *outSample; //[1024 * 2]; // Interleaved L/R
#ifdef ESP8266
const int outSampleLen = 1024 * 2; // SBR disabled
#else
const int outSampleLen = 2048 * 2; // SBR enabled
#endif
int16_t *outSample; //[1024 * 2] or [2048 * 2]; // Interleaved L/R
int16_t validSamples;
int16_t curSample;
+9 -10
View File
@@ -87,22 +87,21 @@ bool AudioGeneratorWAV::loop() {
// Try and stuff the buffer one sample at a time
do {
if (bitsPerSample == 8) {
uint8_t l, r;
if (!GetBufferedData(1, &l)) {
uint8_t u8s; // For read u8 sample
if (!GetBufferedData(1, &u8s)) {
stop();
}
// Upsample from unsigned 8 bits to signed 16 bits
lastSample[AudioOutput::LEFTCHANNEL] = ((int16_t)u8s - 128) << 8;
if (channels == 2) {
if (!GetBufferedData(1, &r)) {
if (!GetBufferedData(1, &u8s)) {
stop();
}
// Upsample from unsigned 8 bits to signed 16 bits
lastSample[AudioOutput::RIGHTCHANNEL] = ((int16_t)u8s - 128) << 8;
} else {
r = 0;
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) {
if (!GetBufferedData(2, &lastSample[AudioOutput::LEFTCHANNEL])) {
stop();
@@ -112,7 +111,7 @@ bool AudioGeneratorWAV::loop() {
stop();
}
} else {
lastSample[AudioOutput::RIGHTCHANNEL] = 0;
lastSample[AudioOutput::RIGHTCHANNEL] = lastSample[AudioOutput::LEFTCHANNEL];
}
}
} while (running && output->ConsumeSample(lastSample));
+1
View File
@@ -148,6 +148,7 @@ bool AudioOutputSPDIF::begin() {
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));
assert(ESP_OK == i2s_channel_enable(_tx_handle));
#else
if (!I2SDriver.begin(_buffers, _bufferWords)) {
audioLogger->println(F("ERROR: Unable to start I2S driver"));
+1
View File
@@ -276,6 +276,7 @@ int PNS(AACDecInfo *aacDecInfo, int ch) {
const /*short*/ int *sfbTab;
unsigned char *sfbCodeBook;
short *scaleFactors;
(void) scaleFactors;
int msMaskOffset, checkCorr, genNew;
unsigned char msMask;
unsigned char *msMaskPtr;
+2 -2
View File
@@ -873,7 +873,7 @@ enum mad_flow mad_synth_frame(struct mad_synth *synth, struct mad_frame const *f
//void mad_synth_frame(struct mad_synth *synth, struct mad_frame const *frame)
{
unsigned int nch, ns;
enum mad_flow(*synth_frame)(struct mad_synth *, struct mad_frame const *, unsigned int, unsigned int, unsigned int, enum mad_flow(*output_func)(), void *);
enum mad_flow(*synth_frame)(struct mad_synth *, struct mad_frame const *, unsigned int, unsigned int, unsigned int, enum mad_flow(*output_func)(void *, const struct mad_header *, struct mad_pcm *), void *);
nch = MAD_NCHANNELS(&frame->header);
ns = MAD_NSBSAMPLES(&frame->header);
@@ -902,7 +902,7 @@ enum mad_flow mad_synth_frame(struct mad_synth *synth, struct mad_frame const *f
// Up to caller to increment synth->phase, only call proper # of ns
enum mad_flow mad_synth_frame_onens(struct mad_synth *synth, struct mad_frame const *frame, unsigned int ns) {
unsigned int nch; //, ns;
enum mad_flow(*synth_frame)(struct mad_synth *, struct mad_frame const *, unsigned int, unsigned int, unsigned int, enum mad_flow(*output_func)(), void *);
enum mad_flow(*synth_frame)(struct mad_synth *, struct mad_frame const *, unsigned int, unsigned int, unsigned int, enum mad_flow(*output_func)(void *, const struct mad_header *, struct mad_pcm *), void *);
nch = MAD_NCHANNELS(&frame->header);
// ns = MAD_NSBSAMPLES(&frame->header);
+2 -2
View File
@@ -20,8 +20,8 @@ while [ $(echo $sketches | wc -w) -gt 0 ]; do
echo "::group::Compiling $(basename $sketch) for $fqbn into $outdir"
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.c.extra_flags=-Wall -Wextra -Werror -Wno-ignored-qualifiers -Wno-unused-parameter" \
--build-property "compiler.cpp.extra_flags=-Wall -Wextra -Werror -Wno-ignored-qualifiers -Wno-overloaded-virtual -Wno-unused-parameter" \
--build-path "$outdir" "$sketch" || exit 255
else
./arduino-cli compile -b "$fqbn" -v --warnings all --build-path "$outdir" "$sketch" || exit 255
+2 -2
View File
@@ -12,8 +12,8 @@ int main(int argc, char **argv)
AudioFileSourceSTDIO *in = new AudioFileSourceSTDIO(AAC);
AudioOutputSTDIO *out = new AudioOutputSTDIO();
out->SetFilename("out.aac.wav");
void *space = malloc(28000+60000);
AudioGeneratorAAC *aac = new AudioGeneratorAAC(space, 28000+60000);
void *space = malloc(28000+60000+2048);
AudioGeneratorAAC *aac = new AudioGeneratorAAC(space, 28000+60000+2048);
aac->begin(in, out);
while (aac->loop()) { /*noop*/ }