Files
xiaozhi-esp32/main/audio_codecs/no_audio_codec.cc

225 lines
7.7 KiB
C++
Raw Normal View History

2024-11-06 06:18:56 +08:00
#include "no_audio_codec.h"
2024-10-29 00:22:29 +08:00
2024-10-03 06:39:22 +08:00
#include <esp_log.h>
2024-10-15 03:56:35 +08:00
#include <cmath>
2024-10-29 00:22:29 +08:00
2024-11-06 06:18:56 +08:00
#define TAG "NoAudioCodec"
2024-08-31 18:00:23 +08:00
2024-11-06 06:18:56 +08:00
NoAudioCodec::~NoAudioCodec() {
2024-08-31 18:00:23 +08:00
if (rx_handle_ != nullptr) {
ESP_ERROR_CHECK(i2s_channel_disable(rx_handle_));
}
if (tx_handle_ != nullptr) {
ESP_ERROR_CHECK(i2s_channel_disable(tx_handle_));
}
}
2024-11-06 06:18:56 +08:00
NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
2024-08-31 18:00:23 +08:00
duplex_ = true;
2024-11-06 06:18:56 +08:00
input_sample_rate_ = input_sample_rate;
output_sample_rate_ = output_sample_rate;
2024-08-31 18:00:23 +08:00
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
2024-11-29 11:06:05 +08:00
.dma_desc_num = 2,
.dma_frame_num = 240 * 3,
2024-08-31 18:00:23 +08:00
.auto_clear_after_cb = false,
.auto_clear_before_cb = false,
.intr_priority = 0,
};
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
i2s_std_config_t std_cfg = {
.clk_cfg = {
.sample_rate_hz = (uint32_t)output_sample_rate_,
.clk_src = I2S_CLK_SRC_DEFAULT,
.ext_clk_freq_hz = 0,
.mclk_multiple = I2S_MCLK_MULTIPLE_256
},
.slot_cfg = {
.data_bit_width = I2S_DATA_BIT_WIDTH_32BIT,
.slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
.slot_mode = I2S_SLOT_MODE_MONO,
.slot_mask = I2S_STD_SLOT_LEFT,
.ws_width = I2S_DATA_BIT_WIDTH_32BIT,
.ws_pol = false,
.bit_shift = true,
.left_align = true,
.big_endian = false,
.bit_order_lsb = false
},
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
2024-11-06 06:18:56 +08:00
.bclk = bclk,
.ws = ws,
.dout = dout,
.din = din,
2024-08-31 18:00:23 +08:00
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false
}
}
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
ESP_LOGI(TAG, "Duplex channels created");
}
2024-11-06 06:18:56 +08:00
NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, gpio_num_t mic_sck, gpio_num_t mic_ws, gpio_num_t mic_din) {
duplex_ = false;
input_sample_rate_ = input_sample_rate;
output_sample_rate_ = output_sample_rate;
2024-08-31 18:00:23 +08:00
// Create a new channel for speaker
i2s_chan_config_t chan_cfg = {
2024-11-29 11:06:05 +08:00
.id = (i2s_port_t)0,
2024-08-31 18:00:23 +08:00
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
2024-10-31 05:57:13 +08:00
.auto_clear_after_cb = true,
2024-08-31 18:00:23 +08:00
.auto_clear_before_cb = false,
.intr_priority = 0,
};
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, nullptr));
i2s_std_config_t std_cfg = {
.clk_cfg = {
.sample_rate_hz = (uint32_t)output_sample_rate_,
.clk_src = I2S_CLK_SRC_DEFAULT,
.ext_clk_freq_hz = 0,
.mclk_multiple = I2S_MCLK_MULTIPLE_256
},
.slot_cfg = {
.data_bit_width = I2S_DATA_BIT_WIDTH_32BIT,
.slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
.slot_mode = I2S_SLOT_MODE_MONO,
.slot_mask = I2S_STD_SLOT_LEFT,
.ws_width = I2S_DATA_BIT_WIDTH_32BIT,
.ws_pol = false,
.bit_shift = true,
.left_align = true,
.big_endian = false,
.bit_order_lsb = false
},
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
2024-11-06 06:18:56 +08:00
.bclk = spk_bclk,
.ws = spk_ws,
.dout = spk_dout,
2024-08-31 18:00:23 +08:00
.din = I2S_GPIO_UNUSED,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false
}
}
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
// Create a new channel for MIC
2024-11-29 11:06:05 +08:00
chan_cfg.id = (i2s_port_t)1;
2024-08-31 18:00:23 +08:00
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, nullptr, &rx_handle_));
std_cfg.clk_cfg.sample_rate_hz = (uint32_t)input_sample_rate_;
2024-11-06 06:18:56 +08:00
std_cfg.gpio_cfg.bclk = mic_sck;
std_cfg.gpio_cfg.ws = mic_ws;
2024-08-31 18:00:23 +08:00
std_cfg.gpio_cfg.dout = I2S_GPIO_UNUSED;
2024-11-06 06:18:56 +08:00
std_cfg.gpio_cfg.din = mic_din;
2024-08-31 18:00:23 +08:00
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
ESP_LOGI(TAG, "Simplex channels created");
2024-10-24 09:53:08 +08:00
}
2024-08-31 18:00:23 +08:00
2024-12-05 15:40:41 +08:00
NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, gpio_num_t mic_sck, gpio_num_t mic_din) {
duplex_ = false;
input_sample_rate_ = input_sample_rate;
output_sample_rate_ = output_sample_rate;
// Create a new channel for speaker
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_1, I2S_ROLE_MASTER);
tx_chan_cfg.dma_desc_num = 6;
tx_chan_cfg.dma_frame_num = 240;
tx_chan_cfg.auto_clear_after_cb = true;
tx_chan_cfg.auto_clear_before_cb = false;
tx_chan_cfg.intr_priority = 0;
ESP_ERROR_CHECK(i2s_new_channel(&tx_chan_cfg, &tx_handle_, NULL));
i2s_std_config_t tx_std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG((uint32_t)output_sample_rate_),
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO),
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = spk_bclk,
.ws = spk_ws,
.dout = spk_dout,
.din = I2S_GPIO_UNUSED,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false,
},
},
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &tx_std_cfg));
// Create a new channel for MIC in PDM mode
i2s_chan_config_t rx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
ESP_ERROR_CHECK(i2s_new_channel(&rx_chan_cfg, NULL, &rx_handle_));
i2s_pdm_rx_config_t pdm_rx_cfg = {
.clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG((uint32_t)input_sample_rate_),
/* The data bit-width of PDM mode is fixed to 16 */
.slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
.gpio_cfg = {
.clk = mic_sck,
.din = mic_din,
.invert_flags = {
.clk_inv = false,
},
},
};
ESP_ERROR_CHECK(i2s_channel_init_pdm_rx_mode(rx_handle_, &pdm_rx_cfg));
ESP_LOGI(TAG, "Simplex channels created");
}
2024-11-06 06:18:56 +08:00
int NoAudioCodec::Write(const int16_t* data, int samples) {
2024-11-14 23:15:43 +08:00
std::vector<int32_t> buffer(samples);
2024-10-15 03:56:35 +08:00
// output_volume_: 0-100
// volume_factor_: 0-65536
int32_t volume_factor = pow(double(output_volume_) / 100.0, 2) * 65536;
2024-08-31 18:00:23 +08:00
for (int i = 0; i < samples; i++) {
2024-10-25 12:25:22 +08:00
int64_t temp = int64_t(data[i]) * volume_factor; // 使用 int64_t 进行乘法运算
if (temp > INT32_MAX) {
buffer[i] = INT32_MAX;
} else if (temp < INT32_MIN) {
buffer[i] = INT32_MIN;
} else {
buffer[i] = static_cast<int32_t>(temp);
}
2024-08-31 18:00:23 +08:00
}
size_t bytes_written;
2024-11-14 23:15:43 +08:00
ESP_ERROR_CHECK(i2s_channel_write(tx_handle_, buffer.data(), samples * sizeof(int32_t), &bytes_written, portMAX_DELAY));
2024-10-24 09:53:08 +08:00
return bytes_written / sizeof(int32_t);
2024-08-31 18:00:23 +08:00
}
2024-11-06 06:18:56 +08:00
int NoAudioCodec::Read(int16_t* dest, int samples) {
2024-08-31 18:00:23 +08:00
size_t bytes_read;
2024-11-14 23:15:43 +08:00
std::vector<int32_t> bit32_buffer(samples);
if (i2s_channel_read(rx_handle_, bit32_buffer.data(), samples * sizeof(int32_t), &bytes_read, portMAX_DELAY) != ESP_OK) {
2024-08-31 18:00:23 +08:00
ESP_LOGE(TAG, "Read Failed!");
return 0;
}
samples = bytes_read / sizeof(int32_t);
for (int i = 0; i < samples; i++) {
2024-11-01 14:26:02 +08:00
int32_t value = bit32_buffer[i] >> 12;
2024-08-31 18:00:23 +08:00
dest[i] = (value > INT16_MAX) ? INT16_MAX : (value < -INT16_MAX) ? -INT16_MAX : (int16_t)value;
}
return samples;
}