audio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "driver/i2s_pdm.h"
  2. #include "esp_log.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "esp_audio_enc.h"
  6. #include "esp_audio_enc_default.h"
  7. #include "esp_audio_enc_reg.h"
  8. #include "esp_g711_enc.h"
  9. #include "peer_connection.h"
  10. #define I2S_CLK_GPIO 42
  11. #define I2S_DATA_GPIO 41
  12. static const char* TAG = "AUDIO";
  13. extern PeerConnection* g_pc;
  14. extern PeerConnectionState eState;
  15. extern int get_timestamp();
  16. i2s_chan_handle_t rx_handle = NULL;
  17. esp_audio_enc_handle_t enc_handle = NULL;
  18. esp_audio_enc_in_frame_t aenc_in_frame = {0};
  19. esp_audio_enc_out_frame_t aenc_out_frame = {0};
  20. esp_g711_enc_config_t g711_cfg;
  21. esp_audio_enc_config_t enc_cfg;
  22. esp_err_t audio_codec_init() {
  23. uint8_t* read_buf = NULL;
  24. uint8_t* write_buf = NULL;
  25. int read_size = 0;
  26. int out_size = 0;
  27. esp_audio_err_t ret = ESP_AUDIO_ERR_OK;
  28. esp_audio_enc_register_default();
  29. g711_cfg.sample_rate = ESP_AUDIO_SAMPLE_RATE_8K;
  30. g711_cfg.channel = ESP_AUDIO_MONO;
  31. g711_cfg.bits_per_sample = ESP_AUDIO_BIT16;
  32. enc_cfg.type = ESP_AUDIO_TYPE_G711A;
  33. enc_cfg.cfg = &g711_cfg;
  34. enc_cfg.cfg_sz = sizeof(g711_cfg);
  35. ret = esp_audio_enc_open(&enc_cfg, &enc_handle);
  36. if (ret != ESP_AUDIO_ERR_OK) {
  37. ESP_LOGE(TAG, "audio encoder open failed");
  38. return ESP_FAIL;
  39. }
  40. int frame_size = (g711_cfg.bits_per_sample * g711_cfg.channel) >> 3;
  41. // Get frame_size
  42. esp_audio_enc_get_frame_size(enc_handle, &read_size, &out_size);
  43. ESP_LOGI(TAG, "audio codec init. frame size: %d, read size: %d, out size: %d", frame_size, read_size, out_size);
  44. // 8000HZ duration 20ms
  45. if (frame_size == read_size) {
  46. read_size *= 8000 / 1000 * 20;
  47. out_size *= 8000 / 1000 * 20;
  48. }
  49. read_buf = malloc(read_size);
  50. write_buf = malloc(out_size);
  51. if (read_buf == NULL || write_buf == NULL) {
  52. return ESP_FAIL;
  53. }
  54. aenc_in_frame.buffer = read_buf;
  55. aenc_in_frame.len = read_size;
  56. aenc_out_frame.buffer = write_buf;
  57. aenc_out_frame.len = out_size;
  58. ESP_LOGI(TAG, "audio codec init done. in buffer size: %d, out buffer size: %d", read_size, out_size);
  59. return 0;
  60. }
  61. esp_err_t audio_init(void) {
  62. i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
  63. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, NULL, &rx_handle));
  64. i2s_pdm_rx_config_t pdm_rx_cfg = {
  65. .clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG(8000),
  66. .slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
  67. .gpio_cfg = {
  68. .clk = I2S_CLK_GPIO,
  69. .din = I2S_DATA_GPIO,
  70. .invert_flags = {
  71. .clk_inv = false,
  72. },
  73. },
  74. };
  75. ESP_ERROR_CHECK(i2s_channel_init_pdm_rx_mode(rx_handle, &pdm_rx_cfg));
  76. ESP_ERROR_CHECK(i2s_channel_enable(rx_handle));
  77. return audio_codec_init();
  78. }
  79. void audio_deinit(void) {
  80. ESP_ERROR_CHECK(i2s_channel_disable(rx_handle));
  81. ESP_ERROR_CHECK(i2s_del_channel(rx_handle));
  82. }
  83. int32_t audio_get_samples(uint8_t* buf, size_t size) {
  84. size_t bytes_read;
  85. if (i2s_channel_read(rx_handle, (char*)buf, size, &bytes_read, 1000) != ESP_OK) {
  86. ESP_LOGE(TAG, "i2s read error");
  87. }
  88. return bytes_read;
  89. }
  90. void audio_task(void* arg) {
  91. int ret;
  92. static int64_t last_time;
  93. int64_t curr_time;
  94. float bytes = 0;
  95. last_time = get_timestamp();
  96. ESP_LOGI(TAG, "audio task started");
  97. for (;;) {
  98. if (eState == PEER_CONNECTION_COMPLETED) {
  99. ret = audio_get_samples(aenc_in_frame.buffer, aenc_in_frame.len);
  100. if (ret == aenc_in_frame.len) {
  101. if (esp_audio_enc_process(enc_handle, &aenc_in_frame, &aenc_out_frame) == ESP_AUDIO_ERR_OK) {
  102. peer_connection_send_audio(g_pc, aenc_out_frame.buffer, aenc_out_frame.encoded_bytes);
  103. bytes += aenc_out_frame.encoded_bytes;
  104. if (bytes > 50000) {
  105. curr_time = get_timestamp();
  106. ESP_LOGI(TAG, "audio bitrate: %.1f bps", 1000.0 * (bytes * 8.0 / (float)(curr_time - last_time)));
  107. last_time = curr_time;
  108. bytes = 0;
  109. }
  110. }
  111. }
  112. vTaskDelay(pdMS_TO_TICKS(5));
  113. } else {
  114. vTaskDelay(pdMS_TO_TICKS(100));
  115. }
  116. }
  117. }