Advertisement
Guest User

Untitled

a guest
Jun 10th, 2021
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.96 KB | None | 0 0
  1. #include <string.h>
  2. #include "nvs_flash.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5.  
  6. #include "esp_log.h"
  7. #include "esp_peripherals.h"
  8. #include "periph_touch.h"
  9. #include "periph_adc_button.h"
  10. #include "periph_button.h"
  11. #include "esp_bt_defs.h"
  12. #include "esp_gap_bt_api.h"
  13. #include "esp_hf_client_api.h"
  14.  
  15. #include "esp_bt.h"
  16. #include "esp_bt_main.h"
  17. #include "esp_bt_device.h"
  18. #include "esp_gap_bt_api.h"
  19. #include "esp_console.h"
  20.  
  21. #include "audio_element.h"
  22. #include "audio_pipeline.h"
  23. #include "audio_event_iface.h"
  24. #include "audio_mem.h"
  25.  
  26. #include "i2s_stream.h"
  27. #include "board.h"
  28. #include "bluetooth_service.h"
  29. #include "filter_resample.h"
  30. #include "raw_stream.h"
  31.  
  32. #include "fatfs_stream.h"
  33. #include "wav_encoder.h"
  34. #include "filter_resample.h"
  35. #include "wav_encoder.h"
  36.  
  37. #if __has_include("esp_idf_version.h")
  38. #include "esp_idf_version.h"
  39. #else
  40. #define ESP_IDF_VERSION_VAL(major, minor, patch) 1
  41. #endif
  42.  
  43. //#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0))
  44. #define HFP_RESAMPLE_RATE 16000
  45. //#else
  46. //#define HFP_RESAMPLE_RATE 8000
  47. //#endif
  48.  
  49. static const char *TAG = "BLUETOOTH_EXAMPLE";
  50. static const char *BT_HF_TAG = "BT_HF";
  51.  
  52. static audio_element_handle_t  raw_read, bt_stream_reader, i2s_stream_writer, i2s_stream_reader;
  53. static audio_pipeline_handle_t pipeline_bluetooth_i2sw, pipeline_i2sr_raw;
  54. static audio_element_handle_t el_raw_read, wav_encoder;
  55. static bool is_get_hfp = true;
  56.  
  57. audio_pipeline_handle_t pipeline_for_record;
  58.  
  59. const char *c_hf_evt_str[] = {
  60.     "CONNECTION_STATE_EVT",              /*!< connection state changed event */
  61.     "AUDIO_STATE_EVT",                   /*!< audio connection state change event */
  62.     "VR_STATE_CHANGE_EVT",                /*!< voice recognition state changed */
  63.     "CALL_IND_EVT",                      /*!< call indication event */
  64.     "CALL_SETUP_IND_EVT",                /*!< call setup indication event */
  65.     "CALL_HELD_IND_EVT",                 /*!< call held indicator event */
  66.     "NETWORK_STATE_EVT",                 /*!< network state change event */
  67.     "SIGNAL_STRENGTH_IND_EVT",           /*!< signal strength indication event */
  68.     "ROAMING_STATUS_IND_EVT",            /*!< roaming status indication event */
  69.     "BATTERY_LEVEL_IND_EVT",             /*!< battery level indication event */
  70.     "CURRENT_OPERATOR_EVT",              /*!< current operator name event */
  71.     "RESP_AND_HOLD_EVT",                 /*!< response and hold event */
  72.     "CLIP_EVT",                          /*!< Calling Line Identification notification event */
  73.     "CALL_WAITING_EVT",                  /*!< call waiting notification */
  74.     "CLCC_EVT",                          /*!< listing current calls event */
  75.     "VOLUME_CONTROL_EVT",                /*!< audio volume control event */
  76.     "AT_RESPONSE",                       /*!< audio volume control event */
  77.     "SUBSCRIBER_INFO_EVT",               /*!< subscriber information event */
  78.     "INBAND_RING_TONE_EVT",              /*!< in-band ring tone settings */
  79.     "LAST_VOICE_TAG_NUMBER_EVT",         /*!< requested number from AG event */
  80.     "RING_IND_EVT",                      /*!< ring indication event */
  81. };
  82.  
  83. // esp_hf_client_connection_state_t
  84. const char *c_connection_state_str[] = {
  85.     "disconnected",
  86.     "connecting",
  87.     "connected",
  88.     "slc_connected",
  89.     "disconnecting",
  90. };
  91.  
  92. // esp_hf_client_audio_state_t
  93. const char *c_audio_state_str[] = {
  94.     "disconnected",
  95.     "connecting",
  96.     "connected",
  97.     "connected_msbc",
  98. };
  99.  
  100. /// esp_hf_vr_state_t
  101. const char *c_vr_state_str[] = {
  102.     "disabled",
  103.     "enabled",
  104. };
  105.  
  106. // esp_hf_service_availability_status_t
  107. const char *c_service_availability_status_str[] = {
  108.     "unavailable",
  109.     "available",
  110. };
  111.  
  112. // esp_hf_roaming_status_t
  113. const char *c_roaming_status_str[] = {
  114.     "inactive",
  115.     "active",
  116. };
  117.  
  118. // esp_hf_client_call_state_t
  119. const char *c_call_str[] = {
  120.     "NO call in progress",
  121.     "call in progress",
  122. };
  123.  
  124. // esp_hf_client_callsetup_t
  125. const char *c_call_setup_str[] = {
  126.     "NONE",
  127.     "INCOMING",
  128.     "OUTGOING_DIALING",
  129.     "OUTGOING_ALERTING"
  130. };
  131.  
  132. // esp_hf_client_callheld_t
  133. const char *c_call_held_str[] = {
  134.     "NONE held",
  135.     "Held and Active",
  136.     "Held",
  137. };
  138.  
  139. // esp_hf_response_and_hold_status_t
  140. const char *c_resp_and_hold_str[] = {
  141.     "HELD",
  142.     "HELD ACCEPTED",
  143.     "HELD REJECTED",
  144. };
  145.  
  146. // esp_hf_client_call_direction_t
  147. const char *c_call_dir_str[] = {
  148.     "outgoing",
  149.     "incoming",
  150. };
  151.  
  152. // esp_hf_client_call_state_t
  153. const char *c_call_state_str[] = {
  154.     "active",
  155.     "held",
  156.     "dialing",
  157.     "alerting",
  158.     "incoming",
  159.     "waiting",
  160.     "held_by_resp_hold",
  161. };
  162.  
  163. // esp_hf_current_call_mpty_type_t
  164. const char *c_call_mpty_type_str[] = {
  165.     "single",
  166.     "multi",
  167. };
  168.  
  169. // esp_hf_volume_control_target_t
  170. const char *c_volume_control_target_str[] = {
  171.     "SPEAKER",
  172.     "MICROPHONE"
  173. };
  174.  
  175. // esp_hf_at_response_code_t
  176. const char *c_at_response_code_str[] = {
  177.     "OK",
  178.     "ERROR"
  179.     "ERR_NO_CARRIER",
  180.     "ERR_BUSY",
  181.     "ERR_NO_ANSWER",
  182.     "ERR_DELAYED",
  183.     "ERR_BLACKLILSTED",
  184.     "ERR_CME",
  185. };
  186.  
  187. // esp_hf_subscriber_service_type_t
  188. const char *c_subscriber_service_type_str[] = {
  189.     "unknown",
  190.     "voice",
  191.     "fax",
  192. };
  193.  
  194. // esp_hf_client_in_band_ring_state_t
  195. const char *c_inband_ring_state_str[] = {
  196.     "NOT provided",
  197.     "Provided",
  198. };
  199.  
  200. static void bt_app_hf_client_audio_open(void)
  201. {
  202.     ESP_LOGE(BT_HF_TAG, "bt_app_hf_client_audio_open");
  203.     int sample_rate = HFP_RESAMPLE_RATE;
  204.     audio_element_info_t bt_info = {0};
  205.     audio_element_getinfo(bt_stream_reader, &bt_info);
  206.     bt_info.sample_rates = sample_rate;
  207.     bt_info.channels = 1;
  208.     bt_info.bits = 16;
  209.     audio_element_setinfo(bt_stream_reader, &bt_info);
  210.     audio_element_report_info(bt_stream_reader);
  211. }
  212.  
  213. static void bt_app_hf_client_audio_close(void)
  214. {
  215.     ESP_LOGE(BT_HF_TAG, "bt_app_hf_client_audio_close");
  216.     int sample_rate = periph_bluetooth_get_a2dp_sample_rate();
  217.     audio_element_info_t bt_info = {0};
  218.     audio_element_getinfo(bt_stream_reader, &bt_info);
  219.     bt_info.sample_rates = sample_rate;
  220.     bt_info.channels = 2;
  221.     bt_info.bits = 16;
  222.     audio_element_setinfo(bt_stream_reader, &bt_info);
  223.     audio_element_report_info(bt_stream_reader);
  224. }
  225.  
  226. static uint32_t bt_app_hf_client_outgoing_cb(uint8_t *p_buf, uint32_t sz)
  227. {
  228.     int out_len_bytes = 0;
  229.     char *enc_buffer = (char *)audio_malloc(sz);
  230.     AUDIO_MEM_CHECK(BT_HF_TAG, enc_buffer, return 0);
  231.     if (is_get_hfp) {
  232.         out_len_bytes = raw_stream_read(raw_read, enc_buffer, sz);
  233.     }
  234.  
  235.     if (out_len_bytes == sz) {
  236.         is_get_hfp = false;
  237.         memcpy(p_buf, enc_buffer, out_len_bytes);
  238.         free(enc_buffer);
  239.         return sz;
  240.     } else {
  241.         is_get_hfp = true;
  242.         free(enc_buffer);
  243.         return 0;
  244.     }
  245. }
  246.  
  247. static void bt_app_hf_client_incoming_cb(const uint8_t *buf, uint32_t sz)
  248. {
  249.     if (bt_stream_reader) {
  250.         if (audio_element_get_state(bt_stream_reader) == AEL_STATE_RUNNING) {
  251.             audio_element_output(bt_stream_reader, (char *)buf, sz);
  252.             esp_hf_client_outgoing_data_ready();
  253.         }
  254.     }
  255. }
  256. /* callback for HF_CLIENT */
  257. void bt_hf_client_cb(esp_hf_client_cb_event_t event, esp_hf_client_cb_param_t *param)
  258. {
  259.     if (event <= ESP_HF_CLIENT_RING_IND_EVT) {
  260.         ESP_LOGE(BT_HF_TAG, "APP HFP event: %s", c_hf_evt_str[event]);
  261.     } else {
  262.         ESP_LOGE(BT_HF_TAG, "APP HFP invalid event %d", event);
  263.     }
  264.  
  265.     switch (event) {
  266.     case ESP_HF_CLIENT_CONNECTION_STATE_EVT:
  267.         ESP_LOGE(BT_HF_TAG, "--connection state %s, peer feats 0x%x, chld_feats 0x%x",
  268.                  c_connection_state_str[param->conn_stat.state],
  269.                  param->conn_stat.peer_feat,
  270.                  param->conn_stat.chld_feat);
  271.         break;
  272.     case ESP_HF_CLIENT_AUDIO_STATE_EVT:
  273.         ESP_LOGE(BT_HF_TAG, "--audio state %s",
  274.                  c_audio_state_str[param->audio_stat.state]);
  275.         if ((param->audio_stat.state == ESP_HF_CLIENT_AUDIO_STATE_CONNECTED)
  276.             || (param->audio_stat.state == ESP_HF_CLIENT_AUDIO_STATE_CONNECTED_MSBC)) {
  277.             bt_app_hf_client_audio_open();
  278.             esp_hf_client_register_data_callback(bt_app_hf_client_incoming_cb,
  279.                                                  bt_app_hf_client_outgoing_cb);
  280.         } else if (param->audio_stat.state == ESP_HF_CLIENT_AUDIO_STATE_DISCONNECTED) {
  281.             bt_app_hf_client_audio_close();
  282.         }
  283.         break;
  284.     case ESP_HF_CLIENT_BVRA_EVT:
  285.         ESP_LOGE(BT_HF_TAG, "--VR state %s",
  286.                  c_vr_state_str[param->bvra.value]);
  287.         break;
  288.     case ESP_HF_CLIENT_CIND_SERVICE_AVAILABILITY_EVT:
  289.         ESP_LOGE(BT_HF_TAG, "--NETWORK STATE %s",
  290.                  c_service_availability_status_str[param->service_availability.status]);
  291.         break;
  292.     case ESP_HF_CLIENT_CIND_ROAMING_STATUS_EVT:
  293.         ESP_LOGE(BT_HF_TAG, "--ROAMING: %s",
  294.                  c_roaming_status_str[param->roaming.status]);
  295.         break;
  296.     case ESP_HF_CLIENT_CIND_SIGNAL_STRENGTH_EVT:
  297.         ESP_LOGE(BT_HF_TAG, "-- signal strength: %d",
  298.                  param->signal_strength.value);
  299.         break;
  300.     case ESP_HF_CLIENT_CIND_BATTERY_LEVEL_EVT:
  301.         ESP_LOGE(BT_HF_TAG, "--battery level %d",
  302.                  param->battery_level.value);
  303.         break;
  304.     case ESP_HF_CLIENT_COPS_CURRENT_OPERATOR_EVT:
  305.         ESP_LOGE(BT_HF_TAG, "--operator name: %s",
  306.                  param->cops.name);
  307.         break;
  308.     case ESP_HF_CLIENT_CIND_CALL_EVT:
  309.         ESP_LOGE(BT_HF_TAG, "--Call indicator %s",
  310.                  c_call_str[param->call.status]);
  311.         break;
  312.     case ESP_HF_CLIENT_CIND_CALL_SETUP_EVT:
  313.         ESP_LOGE(BT_HF_TAG, "--Call setup indicator %s",
  314.                  c_call_setup_str[param->call_setup.status]);        
  315.         break;
  316.     case ESP_HF_CLIENT_CIND_CALL_HELD_EVT:
  317.         ESP_LOGE(BT_HF_TAG, "--Call held indicator %s",
  318.                  c_call_held_str[param->call_held.status]);
  319.         break;
  320.     case ESP_HF_CLIENT_BTRH_EVT:
  321.         ESP_LOGE(BT_HF_TAG, "--response and hold %s",
  322.                  c_resp_and_hold_str[param->btrh.status]);
  323.         break;
  324.     case ESP_HF_CLIENT_CLIP_EVT:
  325.         ESP_LOGE(BT_HF_TAG, "--clip number %s",
  326.                  (param->clip.number == NULL) ? "NULL" : (param->clip.number));
  327.         break;
  328.     case ESP_HF_CLIENT_CCWA_EVT:
  329.         ESP_LOGE(BT_HF_TAG, "--call_waiting %s",
  330.                  (param->ccwa.number == NULL) ? "NULL" : (param->ccwa.number));
  331.         break;
  332.     case ESP_HF_CLIENT_CLCC_EVT:
  333.         ESP_LOGE(BT_HF_TAG, "--Current call: idx %d, dir %s, state %s, mpty %s, number %s",
  334.                  param->clcc.idx,
  335.                  c_call_dir_str[param->clcc.dir],
  336.                  c_call_state_str[param->clcc.status],
  337.                  c_call_mpty_type_str[param->clcc.mpty],
  338.                  (param->clcc.number == NULL) ? "NULL" : (param->clcc.number));
  339.         break;
  340.     case ESP_HF_CLIENT_VOLUME_CONTROL_EVT:
  341.         ESP_LOGE(BT_HF_TAG, "--volume_target: %s, volume %d",
  342.                  c_volume_control_target_str[param->volume_control.type],
  343.                  param->volume_control.volume);
  344.         break;
  345.     case ESP_HF_CLIENT_AT_RESPONSE_EVT:
  346.         ESP_LOGE(BT_HF_TAG, "--AT response event, code %d, cme %d",
  347.                  param->at_response.code, param->at_response.cme);
  348.         break;
  349.     case ESP_HF_CLIENT_CNUM_EVT:
  350.         ESP_LOGE(BT_HF_TAG, "--subscriber type %s, number %s",
  351.                  c_subscriber_service_type_str[param->cnum.type],
  352.                  (param->cnum.number == NULL) ? "NULL" : param->cnum.number);
  353.         break;
  354.     case ESP_HF_CLIENT_BSIR_EVT:
  355.         ESP_LOGE(BT_HF_TAG, "--inband ring state %s",
  356.                  c_inband_ring_state_str[param->bsir.state]);
  357.         break;
  358.     case ESP_HF_CLIENT_BINP_EVT:
  359.         ESP_LOGE(BT_HF_TAG, "--last voice tag number: %s",
  360.                  (param->binp.number == NULL) ? "NULL" : param->binp.number);
  361.         break;
  362.     default:
  363.         ESP_LOGE(BT_HF_TAG, "HF_CLIENT EVT: %d", event);
  364.         break;
  365.     }
  366. }
  367.  
  368. void audio_init()
  369. {
  370.     ESP_LOGI(TAG, "[ 2 ] Start codec chip");
  371.     audio_board_handle_t board_handle = audio_board_init();
  372.     audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
  373. }
  374.  
  375. void audio_pipeline_create()
  376. {
  377.     ESP_LOGI(TAG, "[ 3 ] Create audio pipeline for playback");
  378.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  379.     pipeline_bluetooth_i2sw = audio_pipeline_init(&pipeline_cfg);
  380.     pipeline_i2sr_raw = audio_pipeline_init(&pipeline_cfg);
  381. }
  382.  
  383. void audio_i2s_bluetooth_raw_stream_init()
  384. {
  385.     ESP_LOGI(TAG, "[3.1] Create i2s stream to write data to codec chip and read data from codec chip");
  386.     i2s_stream_cfg_t i2s_cfg1 = I2S_STREAM_CFG_DEFAULT();
  387.     i2s_cfg1.type = AUDIO_STREAM_WRITER;
  388.     i2s_stream_writer = i2s_stream_init(&i2s_cfg1);
  389.  
  390.     i2s_stream_cfg_t i2s_cfg2 = I2S_STREAM_CFG_DEFAULT();
  391.     i2s_cfg2.type = AUDIO_STREAM_READER;
  392.     i2s_stream_reader = i2s_stream_init(&i2s_cfg2);
  393.  
  394.     raw_stream_cfg_t raw_cfg = RAW_STREAM_CFG_DEFAULT();
  395.     raw_cfg.type = AUDIO_STREAM_READER;
  396.     raw_read = raw_stream_init(&raw_cfg);
  397.  
  398.     ESP_LOGI(TAG, "[3.2] Create Bluetooth stream");
  399.     bt_stream_reader = bluetooth_service_create_stream();
  400. }
  401.  
  402. void bluetooth_init()
  403. {
  404.     ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
  405.     bluetooth_service_cfg_t bt_cfg = {
  406.         .device_name = "My device",
  407.         .mode = BLUETOOTH_A2DP_SINK,
  408.     };
  409.     bluetooth_service_start(&bt_cfg);
  410. }
  411.  
  412. void handsfree_init()
  413. {
  414.     esp_hf_client_register_callback(bt_hf_client_cb);
  415.     esp_hf_client_init();    
  416. }
  417.  
  418. void avrc_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
  419. {
  420.     ESP_LOGE("AVRC", "Event: %d", (uint8_t)event);
  421.     switch(event)
  422.     {
  423.         case ESP_AVRC_CT_SET_ABSOLUTE_VOLUME_RSP_EVT:
  424.             ESP_LOGE("AVRC", "Volume: %d%%", param->set_volume_rsp.volume);
  425.         default:
  426.             break;
  427.     }
  428. }
  429.  
  430. void avrc_init()
  431. {
  432.     esp_avrc_ct_init();
  433.     esp_avrc_ct_register_callback(avrc_callback);
  434.  
  435.     esp_avrc_rn_evt_cap_mask_t evt_set = {0};
  436.     esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_SET, &evt_set, ESP_AVRC_RN_VOLUME_CHANGE);
  437. }
  438.  
  439. void pipelines_make_link()
  440. {
  441.     ESP_LOGI(TAG, "[3.3] Register all elements to audio pipeline");
  442.     audio_pipeline_register(pipeline_bluetooth_i2sw, bt_stream_reader, "bt");
  443.     audio_pipeline_register(pipeline_bluetooth_i2sw, i2s_stream_writer, "i2s_w");
  444.  
  445.     audio_pipeline_register(pipeline_i2sr_raw, i2s_stream_reader, "i2s_r");
  446.     audio_pipeline_register(pipeline_i2sr_raw, raw_read, "raw");
  447.  
  448.     ESP_LOGI(TAG, "[3.4] Link it together [Bluetooth]-->bt_stream_reader-->i2s_stream_writer-->[codec_chip]");
  449.     const char *link_d[2] = {"bt", "i2s_w"};
  450.     audio_pipeline_link(pipeline_bluetooth_i2sw, &link_d[0], 2);
  451.  
  452.     const char *link_e[2] = {"i2s_r", "raw"};
  453.     audio_pipeline_link(pipeline_i2sr_raw, &link_e[0], 2);  
  454. }
  455.  
  456. void pipelines_add_listener(esp_periph_set_handle_t* set, audio_event_iface_handle_t* evt)
  457. {
  458.     ESP_LOGI(TAG, "[5.1] Listening event from all elements of pipeline");
  459.     audio_pipeline_set_listener(pipeline_bluetooth_i2sw, *evt);
  460.     audio_pipeline_set_listener(pipeline_i2sr_raw, *evt);
  461.  
  462.     ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
  463.     audio_event_iface_set_listener(esp_periph_set_get_event_iface(*set), *evt);
  464.  
  465.     ESP_LOGI(TAG, "[ 6 ] Start audio_pipeline");
  466.     audio_pipeline_run(pipeline_bluetooth_i2sw);
  467.     audio_pipeline_run(pipeline_i2sr_raw);
  468. }
  469.  
  470. void peripherals_init(esp_periph_set_handle_t* set, esp_periph_handle_t* bt_periph)
  471. {
  472.     ESP_LOGI(TAG, "[ 4 ] Initialize peripherals");
  473.     esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
  474.     *set = esp_periph_set_init(&periph_cfg);
  475.  
  476.     ESP_LOGI(TAG, "[4.1] Initialize Touch peripheral");
  477.     audio_board_key_init(*set);
  478.  
  479.     ESP_LOGI(TAG, "[4.2] Create Bluetooth peripheral");
  480.     *bt_periph = bluetooth_service_create_periph();
  481.  
  482.     ESP_LOGI(TAG, "[4.2] Start all peripherals");
  483.     esp_periph_start(*set, *bt_periph);
  484. }
  485.  
  486. void nvs_init()
  487. {
  488.     esp_err_t err = nvs_flash_init();
  489.     if (err == ESP_ERR_NVS_NO_FREE_PAGES)
  490.     {
  491.         ESP_ERROR_CHECK(nvs_flash_erase());
  492.         err = nvs_flash_init();
  493.     }    
  494. }
  495.  
  496. void deinit_all(esp_periph_set_handle_t* set, audio_event_iface_handle_t* evt)
  497. {
  498.     ESP_LOGI(TAG, "[ 8 ] Stop audio_pipeline");
  499.     audio_pipeline_stop(pipeline_bluetooth_i2sw);
  500.     audio_pipeline_wait_for_stop(pipeline_bluetooth_i2sw);
  501.     audio_pipeline_terminate(pipeline_bluetooth_i2sw);
  502.     audio_pipeline_stop(pipeline_i2sr_raw);
  503.     audio_pipeline_wait_for_stop(pipeline_i2sr_raw);
  504.     audio_pipeline_terminate(pipeline_i2sr_raw);
  505.  
  506.     audio_pipeline_unregister(pipeline_bluetooth_i2sw, bt_stream_reader);
  507.     audio_pipeline_unregister(pipeline_bluetooth_i2sw, i2s_stream_writer);
  508.  
  509.     audio_pipeline_unregister(pipeline_i2sr_raw, i2s_stream_reader);
  510.     audio_pipeline_unregister(pipeline_i2sr_raw, raw_read);
  511.  
  512.     /* Terminate the pipeline before removing the listener */
  513.     audio_pipeline_remove_listener(pipeline_bluetooth_i2sw);
  514.  
  515.     /* Stop all peripherals before removing the listener */
  516.     esp_periph_set_stop_all(*set);
  517.     audio_event_iface_remove_listener(esp_periph_set_get_event_iface(*set), *evt);
  518.  
  519.     /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
  520.     audio_event_iface_destroy(*evt);
  521.  
  522.     /* Release all resources */
  523.     audio_pipeline_deinit(pipeline_bluetooth_i2sw);
  524.     audio_element_deinit(bt_stream_reader);
  525.     audio_element_deinit(i2s_stream_writer);
  526.     audio_element_deinit(i2s_stream_reader);
  527.     audio_element_deinit(raw_read);
  528.  
  529.     esp_periph_set_destroy(*set);
  530.     bluetooth_service_destroy();
  531. }
  532.  
  533. audio_element_handle_t fatfs_stream_writer;
  534.  
  535. void record_pipeline_init()
  536. {
  537.     mem_assert(pipeline_for_record);
  538.  
  539.     fatfs_stream_cfg_t fatfs_write_cfg = FATFS_STREAM_CFG_DEFAULT();
  540.     fatfs_write_cfg.type = AUDIO_STREAM_WRITER;
  541.     fatfs_stream_writer = fatfs_stream_init(&fatfs_write_cfg);
  542.  
  543.     raw_stream_cfg_t raw_cfg = RAW_STREAM_CFG_DEFAULT();
  544.     raw_cfg.type = AUDIO_STREAM_WRITER;
  545.     el_raw_read = raw_stream_init(&raw_cfg);
  546.  
  547.     wav_encoder_cfg_t wav_cfg = DEFAULT_WAV_ENCODER_CONFIG();
  548.     wav_encoder = wav_encoder_init(&wav_cfg);
  549.  
  550.     audio_pipeline_cfg_t  pipeline_for_record_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  551.     pipeline_for_record = audio_pipeline_init(&pipeline_for_record_cfg);
  552.  
  553.     audio_element_set_uri(fatfs_stream_writer, "/sdcard/rec2.wav");
  554.        
  555.     audio_pipeline_register(pipeline_for_record, el_raw_read, "raw");
  556.     audio_pipeline_register(pipeline_for_record, wav_encoder, "wav");
  557.     audio_pipeline_register(pipeline_for_record, fatfs_stream_writer, "file");
  558.  
  559.     const char *link_tag[3] = {"raw", "wav", "file"};
  560.     audio_pipeline_link(pipeline_for_record, &link_tag[0], 3);
  561. }
  562.  
  563. void app_main(void)
  564. {
  565.     nvs_init();
  566.  
  567.     esp_log_level_set("*", ESP_LOG_INFO);
  568.     esp_log_level_set(TAG, ESP_LOG_DEBUG);
  569.  
  570.     bluetooth_init();
  571.  
  572.     handsfree_init();
  573.  
  574.     audio_init();
  575.  
  576.     audio_pipeline_create();
  577.  
  578.     audio_i2s_bluetooth_raw_stream_init();
  579.  
  580.     pipelines_make_link();
  581.  
  582.     esp_periph_set_handle_t set;
  583.     esp_periph_handle_t bt_periph;
  584.     peripherals_init(&set, &bt_periph);
  585.  
  586.     ESP_LOGI(TAG, "[ 1.1 ] Initialize sd card");
  587.     audio_board_sdcard_init(set, SD_MODE_1_LINE);
  588.  
  589.     ESP_LOGI(TAG, "[ 5 ] Set up  event listener");
  590.     audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  591.     audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  592.  
  593.     record_pipeline_init();
  594.    
  595.     ringbuf_handle_t rb = audio_element_get_output_ringbuf(el_raw_read);
  596.     audio_element_set_multi_output_ringbuf(bt_stream_reader, rb, 0);    
  597.    
  598.     audio_pipeline_set_listener(pipeline_for_record, evt);
  599.    
  600.     pipelines_add_listener(&set, &evt);
  601.     audio_pipeline_run(pipeline_for_record);
  602.  
  603.     ESP_LOGI(TAG, "[ 7 ] Listen for all pipeline events");
  604.     while (1) {
  605.         audio_event_iface_msg_t msg;
  606.         esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
  607.         if (ret != ESP_OK) {
  608.             ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
  609.             continue;
  610.         }
  611.  
  612.         ESP_LOGE("main", "Data length: %d", msg.data_len);
  613.  
  614.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) bt_stream_reader
  615.             && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
  616.             audio_element_info_t music_info = {0};
  617.             audio_element_getinfo(bt_stream_reader, &music_info);
  618.  
  619.             ESP_LOGI(TAG, "[ * ] Receive music info from Bluetooth, sample_rates=%d, bits=%d, ch=%d",
  620.                      music_info.sample_rates, music_info.bits, music_info.channels);
  621.             audio_element_setinfo(i2s_stream_writer, &music_info);
  622.             i2s_stream_set_clk(i2s_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
  623.  
  624.             continue;
  625.         }
  626.         if ((msg.source_type == PERIPH_ID_TOUCH || msg.source_type == PERIPH_ID_BUTTON || msg.source_type == PERIPH_ID_ADC_BTN)
  627.             && (msg.cmd == PERIPH_TOUCH_TAP || msg.cmd == PERIPH_BUTTON_PRESSED || msg.cmd == PERIPH_ADC_BUTTON_PRESSED)) {
  628.  
  629.             if ((int) msg.data == get_input_play_id()) {
  630.                 ESP_LOGI(TAG, "[ * ] [Play] touch tap event");
  631.                 periph_bluetooth_play(bt_periph);
  632.  
  633.                 break;
  634.             } else if ((int) msg.data == get_input_set_id()) {
  635.                 ESP_LOGI(TAG, "[ * ] [Set] touch tap event");
  636.                 periph_bluetooth_pause(bt_periph);
  637.             } else if ((int) msg.data == get_input_volup_id()) {
  638.                 ESP_LOGI(TAG, "[ * ] [Vol+] touch tap event");
  639.                 periph_bluetooth_next(bt_periph);
  640.             } else if ((int) msg.data == get_input_voldown_id()) {
  641.                 ESP_LOGI(TAG, "[ * ] [Vol-] touch tap event");
  642.                 periph_bluetooth_prev(bt_periph);
  643.             }
  644.         }
  645.  
  646.         /* Stop when the Bluetooth is disconnected or suspended */
  647.         /*if (msg.source_type == PERIPH_ID_BLUETOOTH
  648.             && msg.source == (void *)bt_periph) {
  649.             if (msg.cmd == PERIPH_BLUETOOTH_DISCONNECTED) {
  650.                 ESP_LOGW(TAG, "[ * ] Bluetooth disconnected");
  651.                 break;
  652.             }
  653.         }*/
  654.         /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  655.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
  656.             && msg.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg.data == AEL_STATUS_STATE_STOPPED) {
  657.             ESP_LOGW(TAG, "[ * ] Stop event received");
  658.             break;
  659.         }
  660.     }
  661.  
  662.     audio_pipeline_unregister_more(pipeline_for_record, el_raw_read, fatfs_stream_writer, NULL);
  663.     deinit_all(&set, &evt);
  664.     audio_pipeline_deinit(pipeline_for_record);
  665.     audio_element_deinit(el_raw_read);
  666.     audio_element_deinit(fatfs_stream_writer);
  667. }
  668.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement