diff options
| author | jacqueline <me@jacqueline.id.au> | 2022-11-15 13:25:58 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2022-11-15 13:25:58 +1100 |
| commit | 530fd15e66a2c89c0dcd6edd1b2a318958c349a4 (patch) | |
| tree | 2b4b75a644587440a6a5cf6620f1b6e36f81c2d6 /src/drivers/i2s_audio_output.cpp | |
| parent | 37041b810fbd10aab0834a33ae1dbd9edbb8bcb9 (diff) | |
| download | tangara-fw-530fd15e66a2c89c0dcd6edd1b2a318958c349a4.tar.gz | |
WIP audio play and pause
Diffstat (limited to 'src/drivers/i2s_audio_output.cpp')
| -rw-r--r-- | src/drivers/i2s_audio_output.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/drivers/i2s_audio_output.cpp b/src/drivers/i2s_audio_output.cpp new file mode 100644 index 00000000..6b231f0e --- /dev/null +++ b/src/drivers/i2s_audio_output.cpp @@ -0,0 +1,92 @@ +#include "i2s_audio_output.hpp" +#include <algorithm> +#include "audio_output.hpp" +#include "gpio-expander.hpp" + +static const i2s_port_t kI2SPort = I2S_NUM_0; + +namespace drivers { + +auto I2SAudioOutput::create(GpioExpander *expander) + -> cpp::result<std::unique_ptr<I2SAudioOutput>, Error> { + + // First, we need to perform initial configuration of the DAC chip. + auto dac_result = drivers::AudioDac::create(expander); + if (dac_result.has_error()) { + ESP_LOGE(TAG, "failed to init dac: %d", dac_result.error()); + return cpp::fail(DAC_CONFIG); + } + std::unique_ptr<AudioDac> dac = std::move(dac_result.value()); + + // Soft mute immediately, in order to minimise any clicks and pops caused by + // the initial output element and pipeline configuration. + dac->WriteVolume(255); + + i2s_stream_cfg_t i2s_stream_config = i2s_stream_cfg_t{ + .type = AUDIO_STREAM_WRITER, + .i2s_config = + { + // static_cast bc esp-adf uses enums incorrectly + .mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX), + .sample_rate = 44100, + .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, + .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, + .communication_format = I2S_COMM_FORMAT_STAND_I2S, + .intr_alloc_flags = ESP_INTR_FLAG_LOWMED, + .dma_buf_count = 8, + .dma_buf_len = 64, + .use_apll = false, + .tx_desc_auto_clear = false, + .fixed_mclk = 0, + .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT, + .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT, + }, + .i2s_port = kI2SPort, + .use_alc = false, + .volume = 0, // Does nothing; use AudioDac to change this. + .out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, + .task_stack = I2S_STREAM_TASK_STACK, + .task_core = I2S_STREAM_TASK_CORE, + .task_prio = I2S_STREAM_TASK_PRIO, + .stack_in_ext = false, + .multi_out_num = 0, + .uninstall_drv = true, + .need_expand = false, + .expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, + }; + i2s_stream_writer = i2s_stream_init(&i2s_stream_config); + if (i2s_stream_writer == NULL) { + return cpp::fail(Error::STREAM_INIT); + } + + // NOTE: i2s_stream_init does some additional setup that hardcodes MCK as + // GPIO0. This happens to work fine for us, but be careful if changing. + i2s_pin_config_t pin_config = {.mck_io_num = GPIO_NUM_0, + .bck_io_num = GPIO_NUM_26, + .ws_io_num = GPIO_NUM_27, + .data_out_num = GPIO_NUM_5, + .data_in_num = I2S_PIN_NO_CHANGE}; + if (esp_err_t err = i2s_set_pin(kI2SPort, &pin_config) != ESP_OK) { + ESP_LOGE(kTag, "failed to configure i2s pins %x", err); + return cpp::fail(Error::I2S_CONFIG); + } + + return std::make_unique<I2SAudioOutput>(dac, i2s_stream_writer); +} + +I2SAudioOutput(std::unique<AudioDac> dac, audio_element_handle_t element) : IAudioOutput(element), dac_(dac) {} +~I2SAudioOutput() { + // TODO: power down the DAC. +} + +auto I2SAudioOutput::SetVolume(uint8_t volume) -> void { + dac_->WriteVolume(255); +} + +auto I2SAudioOutput::Configure(audio_element_info_t info) -> void { + audio_element_setinfo(element_, &music_info); + i2s_stream_set_clk(element_, music_info.sample_rates, + music_info.bits, music_info.channels); +} + +} |
