no_audio_codec.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "no_audio_codec.h"
  2. #include <esp_log.h>
  3. #include <cmath>
  4. #include <cstring>
  5. #define TAG "NoAudioCodec"
  6. NoAudioCodec::~NoAudioCodec() {
  7. if (rx_handle_ != nullptr) {
  8. ESP_ERROR_CHECK(i2s_channel_disable(rx_handle_));
  9. }
  10. if (tx_handle_ != nullptr) {
  11. ESP_ERROR_CHECK(i2s_channel_disable(tx_handle_));
  12. }
  13. }
  14. NoAudioCodecDuplex::NoAudioCodecDuplex(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
  15. duplex_ = true;
  16. input_sample_rate_ = input_sample_rate;
  17. output_sample_rate_ = output_sample_rate;
  18. i2s_chan_config_t chan_cfg = {
  19. .id = I2S_NUM_0,
  20. .role = I2S_ROLE_MASTER,
  21. .dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
  22. .dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
  23. .auto_clear_after_cb = true,
  24. .auto_clear_before_cb = false,
  25. .intr_priority = 0,
  26. };
  27. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
  28. i2s_std_config_t std_cfg = {
  29. .clk_cfg = {
  30. .sample_rate_hz = (uint32_t)output_sample_rate_,
  31. .clk_src = I2S_CLK_SRC_DEFAULT,
  32. .mclk_multiple = I2S_MCLK_MULTIPLE_256,
  33. #ifdef I2S_HW_VERSION_2
  34. .ext_clk_freq_hz = 0,
  35. #endif
  36. },
  37. .slot_cfg = {
  38. .data_bit_width = I2S_DATA_BIT_WIDTH_32BIT,
  39. .slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
  40. .slot_mode = I2S_SLOT_MODE_MONO,
  41. .slot_mask = I2S_STD_SLOT_LEFT,
  42. .ws_width = I2S_DATA_BIT_WIDTH_32BIT,
  43. .ws_pol = false,
  44. .bit_shift = true,
  45. #ifdef I2S_HW_VERSION_2
  46. .left_align = true,
  47. .big_endian = false,
  48. .bit_order_lsb = false
  49. #endif
  50. },
  51. .gpio_cfg = {
  52. .mclk = I2S_GPIO_UNUSED,
  53. .bclk = bclk,
  54. .ws = ws,
  55. .dout = dout,
  56. .din = din,
  57. .invert_flags = {
  58. .mclk_inv = false,
  59. .bclk_inv = false,
  60. .ws_inv = false
  61. }
  62. }
  63. };
  64. ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
  65. ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
  66. ESP_LOGI(TAG, "Duplex channels created");
  67. }
  68. NoAudioCodecSimplex::NoAudioCodecSimplex(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) {
  69. duplex_ = false;
  70. input_sample_rate_ = input_sample_rate;
  71. output_sample_rate_ = output_sample_rate;
  72. // Create a new channel for speaker
  73. i2s_chan_config_t chan_cfg = {
  74. .id = (i2s_port_t)0,
  75. .role = I2S_ROLE_MASTER,
  76. .dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
  77. .dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
  78. .auto_clear_after_cb = true,
  79. .auto_clear_before_cb = false,
  80. .intr_priority = 0,
  81. };
  82. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, nullptr));
  83. i2s_std_config_t std_cfg = {
  84. .clk_cfg = {
  85. .sample_rate_hz = (uint32_t)output_sample_rate_,
  86. .clk_src = I2S_CLK_SRC_DEFAULT,
  87. .mclk_multiple = I2S_MCLK_MULTIPLE_256,
  88. #ifdef I2S_HW_VERSION_2
  89. .ext_clk_freq_hz = 0,
  90. #endif
  91. },
  92. .slot_cfg = {
  93. .data_bit_width = I2S_DATA_BIT_WIDTH_32BIT,
  94. .slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
  95. .slot_mode = I2S_SLOT_MODE_MONO,
  96. .slot_mask = I2S_STD_SLOT_LEFT,
  97. .ws_width = I2S_DATA_BIT_WIDTH_32BIT,
  98. .ws_pol = false,
  99. .bit_shift = true,
  100. #ifdef I2S_HW_VERSION_2
  101. .left_align = true,
  102. .big_endian = false,
  103. .bit_order_lsb = false
  104. #endif
  105. },
  106. .gpio_cfg = {
  107. .mclk = I2S_GPIO_UNUSED,
  108. .bclk = spk_bclk,
  109. .ws = spk_ws,
  110. .dout = spk_dout,
  111. .din = I2S_GPIO_UNUSED,
  112. .invert_flags = {
  113. .mclk_inv = false,
  114. .bclk_inv = false,
  115. .ws_inv = false
  116. }
  117. }
  118. };
  119. ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
  120. // Create a new channel for MIC
  121. chan_cfg.id = (i2s_port_t)1;
  122. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, nullptr, &rx_handle_));
  123. std_cfg.clk_cfg.sample_rate_hz = (uint32_t)input_sample_rate_;
  124. std_cfg.gpio_cfg.bclk = mic_sck;
  125. std_cfg.gpio_cfg.ws = mic_ws;
  126. std_cfg.gpio_cfg.dout = I2S_GPIO_UNUSED;
  127. std_cfg.gpio_cfg.din = mic_din;
  128. ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
  129. ESP_LOGI(TAG, "Simplex channels created");
  130. }
  131. NoAudioCodecSimplex::NoAudioCodecSimplex(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, i2s_std_slot_mask_t spk_slot_mask, gpio_num_t mic_sck, gpio_num_t mic_ws, gpio_num_t mic_din, i2s_std_slot_mask_t mic_slot_mask){
  132. duplex_ = false;
  133. input_sample_rate_ = input_sample_rate;
  134. output_sample_rate_ = output_sample_rate;
  135. // Create a new channel for speaker
  136. i2s_chan_config_t chan_cfg = {
  137. .id = (i2s_port_t)0,
  138. .role = I2S_ROLE_MASTER,
  139. .dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
  140. .dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
  141. .auto_clear_after_cb = true,
  142. .auto_clear_before_cb = false,
  143. .intr_priority = 0,
  144. };
  145. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, nullptr));
  146. i2s_std_config_t std_cfg = {
  147. .clk_cfg = {
  148. .sample_rate_hz = (uint32_t)output_sample_rate_,
  149. .clk_src = I2S_CLK_SRC_DEFAULT,
  150. .mclk_multiple = I2S_MCLK_MULTIPLE_256,
  151. #ifdef I2S_HW_VERSION_2
  152. .ext_clk_freq_hz = 0,
  153. #endif
  154. },
  155. .slot_cfg = {
  156. .data_bit_width = I2S_DATA_BIT_WIDTH_32BIT,
  157. .slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
  158. .slot_mode = I2S_SLOT_MODE_MONO,
  159. .slot_mask = spk_slot_mask,
  160. .ws_width = I2S_DATA_BIT_WIDTH_32BIT,
  161. .ws_pol = false,
  162. .bit_shift = true,
  163. #ifdef I2S_HW_VERSION_2
  164. .left_align = true,
  165. .big_endian = false,
  166. .bit_order_lsb = false
  167. #endif
  168. },
  169. .gpio_cfg = {
  170. .mclk = I2S_GPIO_UNUSED,
  171. .bclk = spk_bclk,
  172. .ws = spk_ws,
  173. .dout = spk_dout,
  174. .din = I2S_GPIO_UNUSED,
  175. .invert_flags = {
  176. .mclk_inv = false,
  177. .bclk_inv = false,
  178. .ws_inv = false
  179. }
  180. }
  181. };
  182. ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
  183. // Create a new channel for MIC
  184. chan_cfg.id = (i2s_port_t)1;
  185. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, nullptr, &rx_handle_));
  186. std_cfg.clk_cfg.sample_rate_hz = (uint32_t)input_sample_rate_;
  187. std_cfg.slot_cfg.slot_mask = mic_slot_mask;
  188. std_cfg.gpio_cfg.bclk = mic_sck;
  189. std_cfg.gpio_cfg.ws = mic_ws;
  190. std_cfg.gpio_cfg.dout = I2S_GPIO_UNUSED;
  191. std_cfg.gpio_cfg.din = mic_din;
  192. ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
  193. ESP_LOGI(TAG, "Simplex channels created");
  194. }
  195. NoAudioCodecSimplexPdm::NoAudioCodecSimplexPdm(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) {
  196. duplex_ = false;
  197. input_sample_rate_ = input_sample_rate;
  198. output_sample_rate_ = output_sample_rate;
  199. // Create a new channel for speaker
  200. i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)1, I2S_ROLE_MASTER);
  201. tx_chan_cfg.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM;
  202. tx_chan_cfg.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM;
  203. tx_chan_cfg.auto_clear_after_cb = true;
  204. tx_chan_cfg.auto_clear_before_cb = false;
  205. tx_chan_cfg.intr_priority = 0;
  206. ESP_ERROR_CHECK(i2s_new_channel(&tx_chan_cfg, &tx_handle_, NULL));
  207. i2s_std_config_t tx_std_cfg = {
  208. .clk_cfg = {
  209. .sample_rate_hz = (uint32_t)output_sample_rate_,
  210. .clk_src = I2S_CLK_SRC_DEFAULT,
  211. .mclk_multiple = I2S_MCLK_MULTIPLE_256,
  212. #ifdef I2S_HW_VERSION_2
  213. .ext_clk_freq_hz = 0,
  214. #endif
  215. },
  216. .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO),
  217. .gpio_cfg = {
  218. .mclk = I2S_GPIO_UNUSED,
  219. .bclk = spk_bclk,
  220. .ws = spk_ws,
  221. .dout = spk_dout,
  222. .din = I2S_GPIO_UNUSED,
  223. .invert_flags = {
  224. .mclk_inv = false,
  225. .bclk_inv = false,
  226. .ws_inv = false,
  227. },
  228. },
  229. };
  230. ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &tx_std_cfg));
  231. #if SOC_I2S_SUPPORTS_PDM_RX
  232. // Create a new channel for MIC in PDM mode
  233. i2s_chan_config_t rx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)0, I2S_ROLE_MASTER);
  234. ESP_ERROR_CHECK(i2s_new_channel(&rx_chan_cfg, NULL, &rx_handle_));
  235. i2s_pdm_rx_config_t pdm_rx_cfg = {
  236. .clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG((uint32_t)input_sample_rate_),
  237. /* The data bit-width of PDM mode is fixed to 16 */
  238. .slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
  239. .gpio_cfg = {
  240. .clk = mic_sck,
  241. .din = mic_din,
  242. .invert_flags = {
  243. .clk_inv = false,
  244. },
  245. },
  246. };
  247. ESP_ERROR_CHECK(i2s_channel_init_pdm_rx_mode(rx_handle_, &pdm_rx_cfg));
  248. #else
  249. ESP_LOGE(TAG, "PDM is not supported");
  250. #endif
  251. ESP_LOGI(TAG, "Simplex channels created");
  252. }
  253. int NoAudioCodec::Write(const int16_t* data, int samples) {
  254. std::vector<int32_t> buffer(samples);
  255. // output_volume_: 0-100
  256. // volume_factor_: 0-65536
  257. int32_t volume_factor = pow(double(output_volume_) / 100.0, 2) * 65536;
  258. for (int i = 0; i < samples; i++) {
  259. int64_t temp = int64_t(data[i]) * volume_factor; // 使用 int64_t 进行乘法运算
  260. if (temp > INT32_MAX) {
  261. buffer[i] = INT32_MAX;
  262. } else if (temp < INT32_MIN) {
  263. buffer[i] = INT32_MIN;
  264. } else {
  265. buffer[i] = static_cast<int32_t>(temp);
  266. }
  267. }
  268. size_t bytes_written;
  269. ESP_ERROR_CHECK(i2s_channel_write(tx_handle_, buffer.data(), samples * sizeof(int32_t), &bytes_written, portMAX_DELAY));
  270. return bytes_written / sizeof(int32_t);
  271. }
  272. int NoAudioCodec::Read(int16_t* dest, int samples) {
  273. size_t bytes_read;
  274. std::vector<int32_t> bit32_buffer(samples);
  275. if (i2s_channel_read(rx_handle_, bit32_buffer.data(), samples * sizeof(int32_t), &bytes_read, portMAX_DELAY) != ESP_OK) {
  276. ESP_LOGE(TAG, "Read Failed!");
  277. return 0;
  278. }
  279. samples = bytes_read / sizeof(int32_t);
  280. for (int i = 0; i < samples; i++) {
  281. int32_t value = bit32_buffer[i] >> 12;
  282. dest[i] = (value > INT16_MAX) ? INT16_MAX : (value < -INT16_MAX) ? -INT16_MAX : (int16_t)value;
  283. }
  284. return samples;
  285. }
  286. int NoAudioCodecSimplexPdm::Read(int16_t* dest, int samples) {
  287. size_t bytes_read;
  288. // PDM 解调后的数据位宽为 16 位,直接读取到目标缓冲区
  289. if (i2s_channel_read(rx_handle_, dest, samples * sizeof(int16_t), &bytes_read, portMAX_DELAY) != ESP_OK) {
  290. ESP_LOGE(TAG, "Read Failed!");
  291. return 0;
  292. }
  293. // 计算实际读取的样本数
  294. return bytes_read / sizeof(int16_t);
  295. }