1717#include "esp_gmf_cap.h"
1818#include "esp_gmf_caps_def.h"
1919#include "esp_gmf_audio_element.h"
20+ #ifdef MIXER_DEBUG
21+ #include "esp_timer.h"
22+ #endif
2023
21- #define MIXER_DEFAULT_PROC_TIME_MS (10)
24+ #define MIXER_DEFAULT_PROC_TIME_MS (10)
25+ #define MIXER_GET_FRAME_BYTE_SIZE (info ) (MIXER_DEFAULT_PROC_TIME_MS * info->sample_rate * info->channel * info->bits_per_sample / 8000)
2226
2327typedef struct {
2428 esp_gmf_audio_element_t parent ; /*!< The GMF mixer handle */
2529 esp_ae_mixer_handle_t mixer_hd ; /*!< The audio effects mixer handle */
2630 uint8_t bytes_per_sample ; /*!< Bytes number of per sampling point */
2731 uint32_t process_num ; /*!< The data number of mixer processing */
32+ uint32_t frame_time ; /*!< The time of one frame, unit: ms */
2833 esp_gmf_payload_t * * in_load ; /*!< The array of input payload */
2934 esp_gmf_payload_t * out_load ; /*!< The output payload */
3035 uint8_t * * in_arr ; /*!< The input buffer pointer array of mixer */
3136 esp_ae_mixer_mode_t * mode ; /*!< The mixer mode */
37+ uint8_t src_num ; /*!< The number of source */
3238 bool need_reopen : 1 ; /*!< Whether need to reopen.
3339 True: Execute the close function first, then execute the open function
3440 False: Do nothing */
@@ -102,9 +108,15 @@ static esp_gmf_job_err_t esp_gmf_mixer_open(esp_gmf_element_handle_t self, void
102108 esp_ae_mixer_cfg_t * mixer_info = (esp_ae_mixer_cfg_t * )OBJ_GET_CFG (self );
103109 ESP_GMF_NULL_CHECK (TAG , mixer_info , {return ESP_GMF_JOB_ERR_FAIL ;})
104110 mixer -> bytes_per_sample = (mixer_info -> bits_per_sample >> 3 ) * mixer_info -> channel ;
105- mixer -> process_num = MIXER_DEFAULT_PROC_TIME_MS * mixer_info -> sample_rate * mixer -> bytes_per_sample / 1000 ;
106111 esp_ae_mixer_open (mixer_info , & mixer -> mixer_hd );
107112 ESP_GMF_CHECK (TAG , mixer -> mixer_hd , {return ESP_GMF_JOB_ERR_FAIL ;}, "Failed to create mixer handle" );
113+ for (int i = 0 ; i < mixer_info -> src_num ; i ++ ) {
114+ esp_ae_mixer_set_mode (mixer -> mixer_hd , i , mixer -> mode [i ]);
115+ }
116+ uint32_t process_num = MIXER_GET_FRAME_BYTE_SIZE (mixer_info );
117+ mixer -> process_num = (ESP_GMF_ELEMENT_GET (mixer )-> in_attr .data_size == 0 ) ? (process_num ) : (ESP_GMF_ELEMENT_GET (mixer )-> in_attr .data_size );
118+ mixer -> frame_time = GMF_AUDIO_CALC_PTS (mixer -> process_num , mixer_info -> sample_rate , mixer_info -> channel , mixer_info -> bits_per_sample );
119+ mixer -> src_num = mixer_info -> src_num ;
108120 mixer -> in_load = esp_gmf_oal_calloc (1 , sizeof (esp_gmf_payload_t * ) * mixer_info -> src_num );
109121 ESP_GMF_MEM_VERIFY (TAG , mixer -> in_load , {return ESP_GMF_JOB_ERR_FAIL ;},
110122 "in load" , sizeof (esp_gmf_payload_t * ) * mixer_info -> src_num );
@@ -156,16 +168,27 @@ static esp_gmf_job_err_t esp_gmf_mixer_process(esp_gmf_element_handle_t self, vo
156168 esp_gmf_port_handle_t in = ESP_GMF_ELEMENT_GET (self )-> in ;
157169 esp_gmf_port_handle_t in_port = in ;
158170 esp_gmf_port_handle_t out_port = ESP_GMF_ELEMENT_GET (self )-> out ;
159- esp_ae_mixer_cfg_t * mixer_info = (esp_ae_mixer_cfg_t * )OBJ_GET_CFG (self );
160- ESP_GMF_NULL_CHECK (TAG , mixer_info , return ESP_GMF_JOB_ERR_FAIL );
161- int index = mixer_info -> src_num ;
171+ int index = mixer -> src_num ;
162172 memset (mixer -> in_load , 0 , sizeof (esp_gmf_payload_t * ) * index );
163173 mixer -> out_load = NULL ;
164174 int i = 0 ;
165- int wait_time = 0 ;
175+ #ifdef MIXER_DEBUG
176+ uint64_t start = 0 ;
177+ uint64_t end = 0 ;
178+ uint64_t acquire_in_time = 0 ;
179+ uint32_t frame_time = mixer -> frame_time ;
180+ #endif
166181 while (in_port != NULL ) {
167- wait_time = i == 0 ? ESP_GMF_MAX_DELAY : 0 ;
168- ret = esp_gmf_port_acquire_in (in_port , & (mixer -> in_load [i ]), mixer -> process_num , wait_time );
182+ #ifdef MIXER_DEBUG
183+ start = esp_timer_get_time ();
184+ #endif
185+ int wait_ticks = ((in_port -> wait_ticks < (mixer -> frame_time / index )) ? (mixer -> frame_time / index ) : (in_port -> wait_ticks ));
186+ ret = esp_gmf_port_acquire_in (in_port , & (mixer -> in_load [i ]), mixer -> process_num , wait_ticks );
187+ #ifdef MIXER_DEBUG
188+ end = esp_timer_get_time ();
189+ acquire_in_time += ((end - start ) / 1000 );
190+ ESP_LOGD (TAG , "Port %d acquire in time: %lld ms, wait ticks: %d" , i , (end - start ) / 1000 , wait_ticks );
191+ #endif
169192 if (ret == ESP_GMF_IO_FAIL || mixer -> in_load [i ]-> buf == NULL ) {
170193 ESP_LOGE (TAG , "Acquire in failed, idx:%d, ret: %d" , i , ret );
171194 out_len = ESP_GMF_JOB_ERR_FAIL ;
@@ -180,11 +203,16 @@ static esp_gmf_job_err_t esp_gmf_mixer_process(esp_gmf_element_handle_t self, vo
180203 memset (mixer -> in_arr [i ] + read_len , 0 , mixer -> process_num - read_len );
181204 }
182205 in_port = in_port -> next ;
183- i ++ ;
184206 ESP_LOGV (TAG , "IN: idx: %d load: %p, buf: %p, valid size: %d, buf length: %d, done: %d" ,
185207 i , mixer -> in_load [i ], mixer -> in_load [i ]-> buf , mixer -> in_load [i ]-> valid_size ,
186208 mixer -> in_load [i ]-> buf_length , mixer -> in_load [i ]-> is_done );
209+ i ++ ;
210+ }
211+ #ifdef MIXER_DEBUG
212+ if (acquire_in_time > frame_time ) {
213+ ESP_LOGW (TAG , "Total acquire in time: %lld ms, frame time: %ld ms" , acquire_in_time , frame_time );
187214 }
215+ #endif
188216 ret = esp_gmf_port_acquire_out (out_port , & mixer -> out_load , mixer -> process_num , ESP_GMF_MAX_DELAY );
189217 ESP_GMF_PORT_ACQUIRE_OUT_CHECK (TAG , ret , out_len , { goto __mixer_release ;});
190218 // Down-mixer never stop in gmf, only user can set to stop
@@ -380,13 +408,17 @@ esp_gmf_err_t esp_gmf_mixer_init(esp_ae_mixer_cfg_t *config, esp_gmf_element_han
380408 esp_gmf_obj_set_config (obj , cfg , sizeof (esp_ae_mixer_cfg_t ));
381409 mixer -> mode = esp_gmf_oal_calloc (cfg -> src_num , sizeof (esp_ae_mixer_mode_t ));
382410 ESP_GMF_MEM_VERIFY (TAG , mixer -> mode , {ret = ESP_GMF_ERR_MEMORY_LACK ; goto MIXER_INIT_FAIL ;}, "Allocate(%d) failed" , cfg -> src_num * sizeof (esp_ae_mixer_mode_t ));
411+ for (int i = 0 ; i < cfg -> src_num ; i ++ ) {
412+ mixer -> mode [i ] = ESP_AE_MIXER_MODE_FADE_UPWARD ;
413+ }
383414 ret = esp_gmf_obj_set_tag (obj , "aud_mixer" );
384415 ESP_GMF_RET_ON_NOT_OK (TAG , ret , goto MIXER_INIT_FAIL , "Failed to set obj tag" );
385416 esp_gmf_element_cfg_t el_cfg = {0 };
417+ mixer -> process_num = MIXER_GET_FRAME_BYTE_SIZE (cfg );
386418 ESP_GMF_ELEMENT_IN_PORT_ATTR_SET (el_cfg .in_attr , ESP_GMF_EL_PORT_CAP_MULTI , 0 , 0 ,
387- ESP_GMF_PORT_TYPE_BLOCK | ESP_GMF_PORT_TYPE_BYTE , ESP_GMF_ELEMENT_PORT_DATA_SIZE_DEFAULT );
419+ ESP_GMF_PORT_TYPE_BLOCK | ESP_GMF_PORT_TYPE_BYTE , mixer -> process_num );
388420 ESP_GMF_ELEMENT_IN_PORT_ATTR_SET (el_cfg .out_attr , ESP_GMF_EL_PORT_CAP_SINGLE , 0 , 0 ,
389- ESP_GMF_PORT_TYPE_BLOCK | ESP_GMF_PORT_TYPE_BYTE , ESP_GMF_ELEMENT_PORT_DATA_SIZE_DEFAULT );
421+ ESP_GMF_PORT_TYPE_BLOCK | ESP_GMF_PORT_TYPE_BYTE , mixer -> process_num );
390422 el_cfg .dependency = true;
391423 ret = esp_gmf_audio_el_init (mixer , & el_cfg );
392424 ESP_GMF_RET_ON_NOT_OK (TAG , ret , goto MIXER_INIT_FAIL , "Failed to initialize mixer element" );
0 commit comments