Skip to content

Commit 9e356eb

Browse files
committed
SPDIFStream additional checks
1 parent 847d553 commit 9e356eb

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

src/AudioExperiments/AudioSPDIF.h

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -182,37 +182,32 @@ class SPDFOutI2SESP32 : public SPDIFOut {
182182

183183
// initialize I2S for S/PDIF transmission
184184
void begin(SPDIFConfig &cfg) {
185-
if (cfg.sample_rate != 0) {
186-
// uninstall and reinstall I2S driver for avoiding I2S bug
187-
int sample_rate = cfg.sample_rate * BMC_BITS_FACTOR;
188-
int bclk = sample_rate * I2S_BITS_PER_SAMPLE * I2S_CHANNELS;
189-
int mclk =
190-
(I2S_BUG_MAGIC / bclk) * bclk; // use mclk for avoiding I2S bug
191-
192-
i2s_config_t i2s_config = {
193-
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
194-
.sample_rate = sample_rate,
195-
.bits_per_sample = (i2s_bits_per_sample_t)I2S_BITS_PER_SAMPLE,
196-
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
197-
.communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_I2S,
198-
.intr_alloc_flags = 0,
199-
.dma_buf_count = DMA_BUF_COUNT,
200-
.dma_buf_len = DMA_BUF_LEN,
201-
.use_apll = true,
202-
.tx_desc_auto_clear = true,
203-
.fixed_mclk = mclk, // avoiding I2S bug
204-
};
205-
i2s_pin_config_t pin_config = {
206-
.bck_io_num = -1,
207-
.ws_io_num = -1,
208-
.data_out_num = cfg.pin_data,
209-
.data_in_num = -1,
210-
};
211-
ESP_ERROR_CHECK(i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL));
212-
ESP_ERROR_CHECK(i2s_set_pin(I2S_NUM, &pin_config));
213-
} else {
214-
LOGE("sample_rate must not be null")
215-
}
185+
// uninstall and reinstall I2S driver for avoiding I2S bug
186+
int sample_rate = cfg.sample_rate * BMC_BITS_FACTOR;
187+
int bclk = sample_rate * I2S_BITS_PER_SAMPLE * I2S_CHANNELS;
188+
int mclk = (I2S_BUG_MAGIC / bclk) * bclk; // use mclk for avoiding I2S bug
189+
190+
i2s_config_t i2s_config = {
191+
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
192+
.sample_rate = sample_rate,
193+
.bits_per_sample = (i2s_bits_per_sample_t)I2S_BITS_PER_SAMPLE,
194+
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
195+
.communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_I2S,
196+
.intr_alloc_flags = 0,
197+
.dma_buf_count = DMA_BUF_COUNT,
198+
.dma_buf_len = DMA_BUF_LEN,
199+
.use_apll = true,
200+
.tx_desc_auto_clear = true,
201+
.fixed_mclk = mclk, // avoiding I2S bug
202+
};
203+
i2s_pin_config_t pin_config = {
204+
.bck_io_num = -1,
205+
.ws_io_num = -1,
206+
.data_out_num = cfg.pin_data,
207+
.data_in_num = -1,
208+
};
209+
ESP_ERROR_CHECK(i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL));
210+
ESP_ERROR_CHECK(i2s_set_pin(I2S_NUM, &pin_config));
216211
}
217212

218213
bool end() { return i2s_driver_uninstall(I2S_NUM) == ESP_OK; }
@@ -388,6 +383,14 @@ class SPDIFStream : public AudioStreamX {
388383
/// start SPDIF with the indicated configuration
389384
bool begin(SPDIFConfig cfg) {
390385
this->cfg = cfg;
386+
if (cfg.sample_rate == 0) {
387+
LOGE("sample_rate must not be 0")
388+
return false;
389+
}
390+
if (cfg.channels == 0) {
391+
LOGE("channels must not be 0")
392+
return false;
393+
}
391394
// Define output class if not yet defined
392395
if (spdif_out == nullptr) {
393396
#if USE_ESP32_I2S == 1

0 commit comments

Comments
 (0)