2323
2424typedef struct {
2525 esp_capture_audio_src_if_t base ;
26+ const char * mic_layout ;
2627 uint8_t channel ;
2728 uint8_t channel_mask ;
2829 esp_codec_dev_handle_t handle ;
@@ -37,8 +38,9 @@ typedef struct {
3738 uint8_t open : 1 ;
3839 uint8_t in_quit : 1 ;
3940 uint8_t stopping : 1 ;
40- const esp_afe_sr_iface_t * aec_if ;
41- esp_afe_sr_data_t * aec_data ;
41+ const esp_afe_sr_iface_t * afe_handle ;
42+ esp_afe_sr_data_t * afe_data ;
43+ srmodel_list_t * models ;
4244} audio_aec_src_t ;
4345
4446static int cal_frame_length (esp_capture_audio_info_t * info )
@@ -49,23 +51,23 @@ static int cal_frame_length(esp_capture_audio_info_t *info)
4951
5052static esp_capture_err_t open_afe (audio_aec_src_t * src )
5153{
52- afe_config_t afe_config = AFE_CONFIG_DEFAULT ( );
53- afe_config . vad_init = false;
54- afe_config . wakenet_init = false ;
55- afe_config . afe_perferred_core = 1 ;
56- afe_config . afe_perferred_priority = 20 ;
57- // afe_config.memory_alloc_mode = AFE_MEMORY_ALLOC_MORE_INTERNAL ;
58- afe_config . pcm_config . mic_num = 1 ;
59- afe_config . pcm_config . ref_num = 1 ;
60- afe_config . pcm_config . total_ch_num = 1 + 1 ;
61- afe_config . aec_init = true ;
62- afe_config . se_init = false ;
63- afe_config . pcm_config . sample_rate = src -> info . sample_rate ;
64- afe_config . voice_communication_init = true;
65- src -> aec_if = & ESP_AFE_VC_HANDLE ;
66- src -> aec_data = src -> aec_if -> create_from_config ( & afe_config );
67- if ( src -> aec_data == NULL ) {
68- return ESP_CAPTURE_ERR_NO_MEM ;
54+ src -> models = esp_srmodel_init ( "model" );
55+ if ( src -> models == NULL ) {
56+ ESP_LOGW ( TAG , "No model to load" ) ;
57+ }
58+
59+ afe_config_t * afe_config = afe_config_init ( src -> mic_layout , src -> models , AFE_TYPE_SR , AFE_MODE_LOW_COST ) ;
60+
61+ src -> afe_handle = esp_afe_handle_from_config ( afe_config ) ;
62+ if ( src -> afe_handle == NULL ) {
63+ ESP_LOGE ( TAG , "Failed to create AFE handle" ) ;
64+ return ESP_CAPTURE_ERR_NOT_SUPPORTED ;
65+ }
66+
67+ src -> afe_data = src -> afe_handle -> create_from_config ( afe_config ) ;
68+ if ( src -> afe_data == NULL ) {
69+ ESP_LOGE ( TAG , "Failed to create AFE data" );
70+ return ESP_CAPTURE_ERR_NOT_SUPPORTED ;
6971 }
7072 return ESP_CAPTURE_ERR_OK ;
7173}
@@ -120,7 +122,7 @@ static void audio_aec_src_buffer_in_thread(void *arg)
120122 ESP_LOGE (TAG , "Fail to read data %d" , ret );
121123 break ;
122124 }
123- ret = src -> aec_if -> feed (src -> aec_data , (int16_t * )feed_data );
125+ ret = src -> afe_handle -> feed (src -> afe_data , (int16_t * )feed_data );
124126 if (ret < 0 ) {
125127 ESP_LOGE (TAG , "Fail to feed data %d" , ret );
126128 break ;
@@ -154,7 +156,7 @@ static esp_capture_err_t audio_aec_src_start(esp_capture_audio_src_if_t *h)
154156 ESP_LOGE (TAG , "Failed to open AFE" );
155157 return ESP_CAPTURE_ERR_NOT_SUPPORTED ;
156158 }
157- int audio_chunksize = src -> aec_if -> get_feed_chunksize (src -> aec_data );
159+ int audio_chunksize = src -> afe_handle -> get_feed_chunksize (src -> afe_data );
158160 src -> cache_size = audio_chunksize * (16 / 8 );
159161
160162 src -> cached_frame = capture_calloc (1 , src -> cache_size * 2 );
@@ -200,7 +202,7 @@ static esp_capture_err_t audio_aec_src_read_frame(esp_capture_audio_src_if_t *h,
200202 }
201203 src -> cache_fill = 0 ;
202204 src -> cached_read_pos = 0 ;
203- afe_fetch_result_t * res = src -> aec_if -> fetch (src -> aec_data );
205+ afe_fetch_result_t * res = src -> afe_handle -> fetch (src -> afe_data );
204206 if (res -> ret_value != ESP_OK ) {
205207 ESP_LOGE (TAG , "Fail to read from AEC" );
206208 return ESP_CAPTURE_ERR_INTERNAL ;
@@ -221,15 +223,20 @@ static esp_capture_err_t audio_aec_src_stop(esp_capture_audio_src_if_t *h)
221223 audio_aec_src_t * src = (audio_aec_src_t * )h ;
222224 if (src -> in_quit == false) {
223225 // fetch once
224- src -> aec_if -> fetch (src -> aec_data );
226+ src -> afe_handle -> fetch (src -> afe_data );
225227 src -> stopping = true;
226228 while (src -> in_quit == false) {
227229 capture_sleep (10 );
228230 }
229231 }
230- if (src -> aec_data ) {
231- src -> aec_if -> destroy (src -> aec_data );
232- src -> aec_data = NULL ;
232+
233+ if (src -> models ) {
234+ esp_srmodel_deinit (src -> models );
235+ src -> models = NULL ;
236+ }
237+ if (src -> afe_data ) {
238+ src -> afe_handle -> destroy (src -> afe_data );
239+ src -> afe_data = NULL ;
233240 }
234241 if (src -> cached_frame ) {
235242 capture_free (src -> cached_frame );
@@ -263,6 +270,11 @@ esp_capture_audio_src_if_t *esp_capture_new_audio_aec_src(esp_capture_audio_aec_
263270 src -> handle = cfg -> record_handle ;
264271 src -> channel = cfg -> channel ? cfg -> channel : 2 ;
265272 src -> channel_mask = cfg -> channel_mask ;
273+ if (cfg -> mic_layout == NULL ) {
274+ cfg -> mic_layout = "MR" ;
275+ } else {
276+ src -> mic_layout = cfg -> mic_layout ;
277+ }
266278 return & src -> base ;
267279}
268280
0 commit comments