ESP8266 SinglePinI2SDriver, SPDIF output improvements, FLAC memory optimization (#237)
* Added S/PDIF output using I2S peripheral * Added AudioOutputSPDIF to README and PROGMEM changes * SPDIF/I2S sample rate fixes * Fix SPDIF example file name * Minimal (single pin) I2S driver * AudioOutputSPDIF changes for minimal I2S driver * Documentation changes * FLAC decoding memory optimization
This commit is contained in:
1 parent
a7a8b917d2
commit
377b271edd
13 files changed
+525
-49
No files matched your search
+53
-36
@@ -14,6 +14,14 @@
|
||||
|
||||
Adapted for ESP8266Audio
|
||||
|
||||
NOTE: This module operates I2S at 4x sampling rate, as it needs to
|
||||
send out each bit as two output symbols, packed into
|
||||
32-bit words. Even for mono sound, S/PDIF is specified minimum
|
||||
for 2 channels, each as 32-bits sub-frame. This drains I2S
|
||||
buffers 4x more quickly so you may need 4x bigger output
|
||||
buffers than usual, configurable with 'dma_buf_count'
|
||||
constructor parameter.
|
||||
|
||||
Copyright (C) 2020 Ivan Kostoski
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
@@ -31,10 +39,11 @@
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32)
|
||||
#include "driver/i2s.h"
|
||||
#else
|
||||
#include <i2s.h>
|
||||
#include "soc/rtc.h"
|
||||
#elif defined(ESP8266)
|
||||
#include "driver/SinglePinI2SDriver.h"
|
||||
#endif
|
||||
#include "AudioOutputSPDIF.h"
|
||||
|
||||
@@ -77,7 +86,7 @@ static const uint16_t spdif_bmclookup[256] PROGMEM = {
|
||||
AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count)
|
||||
{
|
||||
this->portNo = port;
|
||||
#ifdef ESP32
|
||||
#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),
|
||||
@@ -87,21 +96,23 @@ AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count)
|
||||
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority
|
||||
.dma_buf_count = dma_buf_count,
|
||||
.dma_buf_len = 256, // 4 x bigger buffers
|
||||
.dma_buf_len = DMA_BUF_SIZE_DEFAULT, // bigger buffers, reduces interrupts
|
||||
.use_apll = true // Audio PLL is needed for low clock jitter
|
||||
};
|
||||
if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_spdif, 0, NULL) != ESP_OK) {
|
||||
audioLogger->println("ERROR: Unable to install I2S drives\n");
|
||||
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);
|
||||
SetPinout(25, 32, dout_pin);
|
||||
rate_multiplier = 2; // For 32bit words
|
||||
#else
|
||||
SetPinout(I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, dout_pin);
|
||||
rate_multiplier = 2; // 2x32bit words
|
||||
#elif defined(ESP8266)
|
||||
(void) dout_pin;
|
||||
(void) dma_buf_count;
|
||||
i2s_begin();
|
||||
rate_multiplier = 4; // For 16bit words
|
||||
if (!I2SDriver.begin(dma_buf_count, DMA_BUF_SIZE_DEFAULT)) {
|
||||
audioLogger->println(F("ERROR: Unable to start I2S driver"));
|
||||
return;
|
||||
}
|
||||
rate_multiplier = 4; // 4x16 bit words
|
||||
#endif
|
||||
i2sOn = true;
|
||||
mono = false;
|
||||
@@ -109,26 +120,27 @@ AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count)
|
||||
channels = 2;
|
||||
frame_num = 0;
|
||||
SetGain(1.0);
|
||||
SetRate(44100); // Default
|
||||
hertz = 0;
|
||||
SetRate(44100);
|
||||
}
|
||||
|
||||
AudioOutputSPDIF::~AudioOutputSPDIF()
|
||||
{
|
||||
#ifdef ESP32
|
||||
#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
|
||||
}
|
||||
#else
|
||||
if (i2sOn) i2s_end();
|
||||
#elif defined(ESP8266)
|
||||
if (i2sOn) I2SDriver.stop();
|
||||
#endif
|
||||
i2sOn = false;
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::SetPinout(int bclk, int wclk, int dout)
|
||||
{
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32)
|
||||
i2s_pin_config_t pins = {
|
||||
.bck_io_num = bclk,
|
||||
.ws_io_num = wclk,
|
||||
@@ -150,15 +162,25 @@ bool AudioOutputSPDIF::SetPinout(int bclk, int wclk, int dout)
|
||||
|
||||
bool AudioOutputSPDIF::SetRate(int hz)
|
||||
{
|
||||
this->hertz = hz;
|
||||
audioLogger->printf("S/PDIF Set Rate: %d\n", AdjustI2SRate(hz));
|
||||
if (!i2sOn) return false;
|
||||
#ifdef ESP32
|
||||
if (i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz)) != ESP_OK) {
|
||||
if (hz < 32000) return false;
|
||||
if (hz == this->hertz) return true;
|
||||
this->hertz = hz;
|
||||
int adjustedHz = AdjustI2SRate(hz);
|
||||
#if defined(ESP32)
|
||||
if (i2s_set_sample_rates((i2s_port_t)portNo, adjustedHz) == ESP_OK) {
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
audioLogger->println("ERROR changing S/PDIF sample rate");
|
||||
}
|
||||
#else
|
||||
i2s_set_rate(AdjustI2SRate(hz));
|
||||
#elif defined(ESP8266)
|
||||
I2SDriver.setRate(adjustedHz);
|
||||
audioLogger->printf_P(PSTR("S/PDIF rate set: %.3f\n"), I2SDriver.getActualRate()/4);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
@@ -193,9 +215,6 @@ bool AudioOutputSPDIF::begin()
|
||||
bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2])
|
||||
{
|
||||
if (!i2sOn) return true; // Sink the data
|
||||
#ifndef ESP32
|
||||
if (i2s_is_full()) return false;
|
||||
#endif
|
||||
int16_t ms[2];
|
||||
uint16_t hi, lo, aux;
|
||||
uint32_t buf[4];
|
||||
@@ -243,18 +262,14 @@ bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2])
|
||||
aux = 0xb333 ^ (((uint32_t)((int16_t)lo)) >> 17);
|
||||
buf[3] = VUCP_PREAMBLE_W | aux;
|
||||
|
||||
// Assume DMA buffers are multiples of 16 bytes. Either we write all bytes or none.
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32)
|
||||
// Assume DMA buffers are multiples of 16 bytes. Either we write all bytes or none.
|
||||
uint32_t bytes_written;
|
||||
esp_err_t ret = i2s_write((i2s_port_t)portNo, (const char*)&buf, 8 * channels, &bytes_written, 0);
|
||||
// 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;
|
||||
#else
|
||||
// NOTE: Order of buffer words is different for ESP8266
|
||||
i2s_write_sample_nb(buf[1]);
|
||||
i2s_write_sample_nb(buf[0]);
|
||||
i2s_write_sample_nb(buf[3]);
|
||||
if (!i2s_write_sample_nb(buf[2])) return false; // Return early and do not increment frame_num
|
||||
#elif defined(ESP8266)
|
||||
if (!I2SDriver.writeInterleaved(buf)) return false;
|
||||
#endif
|
||||
// Increment and rotate frame number
|
||||
if (++frame_num > 191) frame_num = 0;
|
||||
@@ -263,8 +278,10 @@ bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2])
|
||||
|
||||
bool AudioOutputSPDIF::stop()
|
||||
{
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32)
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
#elif defined(ESP8266)
|
||||
I2SDriver.stop();
|
||||
#endif
|
||||
frame_num = 0;
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user