algo_stream怎样使用

dengbq
Posts: 38
Joined: Thu Mar 31, 2022 8:45 am

algo_stream怎样使用

Postby dengbq » Tue Oct 11, 2022 7:23 am

录音后降躁处理再输出,结果降躁处理后,音频时间减半,声音也怪怪的。
pipline为i2s->resmaple_filter-->algo-->wav_encoder-->http,如果把resmaple_filter-->algo去掉,录音是正常的,加上后问题就出现了,录音为单通道16K采样16it.只去掉resample_filter,效果也一样。

idf版本:4.4,源码如下,麻烦看一下是什么原因?


#define EXAMPLE_AUDIO_SAMPLE_RATE (16000)
#define EXAMPLE_AUDIO_BITS (16)
#define EXAMPLE_AUDIO_CHANNELS (1)

audio_pipeline_handle_t pipeline_rec;
audio_element_handle_t i2s_stream_reader;
audio_element_handle_t http_stream_writer;


/* The AEC internal buffering mechanism requires that the recording signal
is delayed by around 0 - 10 ms compared to the corresponding reference (playback) signal. */
#define DEFAULT_REF_DELAY_MS 35
#define DEFAULT_REC_DELAY_MS 0


#define CONFIG_SERVER_URI "http://192.168.5.43:8000/upload"

static void oneshot_start_timer_callback(void* arg);
static void oneshot_stop_timer_callback(void* arg);

static void oneshot_start_timer_callback(void* arg)
{
int64_t time_since_boot = esp_timer_get_time();

audio_element_handle_t http_stream_writer = (audio_element_handle_t)arg;
ESP_LOGI(TAG, "One-shot start timer called, time since boot: %lld us,httpwriter:%p", time_since_boot, http_stream_writer);

//record
/*
audio_pipeline_stop(pipeline_rec);
ESP_LOGI(TAG, "oneshot_start_timer_callback 01");
audio_pipeline_wait_for_stop(pipeline_rec);
ESP_LOGI(TAG, "oneshot_start_timer_callback 02");
audio_pipeline_reset_ringbuffer(pipeline_rec);
audio_pipeline_reset_elements(pipeline_rec);
ESP_LOGI(TAG, "oneshot_start_timer_callback 03");
audio_pipeline_terminate(pipeline_rec);
*/
ESP_LOGI(TAG, "oneshot_start_timer_callback 04");

audio_element_set_uri(http_stream_writer, CONFIG_SERVER_URI);
audio_pipeline_run(pipeline_rec);

ESP_LOGI(TAG, "oneshot_start_timer_callback 05");
}


static void oneshot_stop_timer_callback(void* arg)
{
int64_t time_since_boot = esp_timer_get_time();
ESP_LOGI(TAG, "One-shot stop timer called, time since boot: %lld us", time_since_boot);

//stop record
audio_element_set_ringbuf_done(i2s_stream_reader);
}

esp_err_t _http_stream_event_handle(http_stream_event_msg_t *msg)
{
.....
return ESP_OK;
}


void app_main()
{
esp_log_level_set("*", ESP_LOG_DEBUG);
esp_log_level_set(TAG, ESP_LOG_INFO);

EventGroupHandle_t EXIT_FLAG = xEventGroupCreate();

esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0))
ESP_ERROR_CHECK(esp_netif_init());
#else
tcpip_adapter_init();
#endif

ESP_LOGI(TAG, "[ 1 ] Initialize Button Peripheral & Connect to wifi network");
// Initialize peripherals management
esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);

periph_wifi_cfg_t wifi_cfg = {
.ssid = "asdkfds",
.password = "123456789",
};
esp_periph_handle_t wifi_handle = periph_wifi_init(&wifi_cfg);

// Start wifi & button peripheral
esp_periph_start(set, wifi_handle);
periph_wifi_wait_for_connected(wifi_handle, portMAX_DELAY);

ESP_LOGI(TAG, "[2.0] Start codec chip");
audio_board_handle_t board_handle = audio_board_init();
audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
audio_hal_set_volume(board_handle->audio_hal, 70);

ESP_LOGI(TAG, "[3.0] Create audio pipeline_rec for recording");
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
pipeline_rec = audio_pipeline_init(&pipeline_cfg);
mem_assert(pipeline_rec);


ESP_LOGI(TAG, "[3.1] Create i2s stream to read audio data from codec chip");
i2s_stream_cfg_t i2s_rd_cfg = I2S_STREAM_CFG_DEFAULT();
i2s_rd_cfg.type = AUDIO_STREAM_READER;
i2s_rd_cfg.i2s_config.sample_rate = EXAMPLE_AUDIO_SAMPLE_RATE;
i2s_rd_cfg.i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT;
#ifdef CONFIG_ESP_LYRAT_MINI_V1_1_BOARD
i2s_rd_cfg.i2s_port = 0;
#endif
i2s_rd_cfg.task_core = 1;
i2s_stream_reader = i2s_stream_init(&i2s_rd_cfg);

rsp_filter_cfg_t rsp_cfg_r = DEFAULT_RESAMPLE_FILTER_CONFIG();
rsp_cfg_r.src_rate = EXAMPLE_AUDIO_SAMPLE_RATE;
rsp_cfg_r.src_ch = 1;
rsp_cfg_r.dest_rate = EXAMPLE_AUDIO_SAMPLE_RATE;
#ifndef CONFIG_ESP_LYRAT_MINI_V1_1_BOARD
rsp_cfg_r.dest_ch = 1;
#endif
rsp_cfg_r.dest_ch = 2;

rsp_cfg_r.complexity = 5;
rsp_cfg_r.task_core = 1;
rsp_cfg_r.out_rb_size = 10 * 1024;
audio_element_handle_t filter_r = rsp_filter_init(&rsp_cfg_r);

algorithm_stream_cfg_t algo_config = ALGORITHM_STREAM_CFG_DEFAULT();
/*
#ifdef CONFIG_ESP_LYRAT_MINI_V1_1_BOARD
algo_config.input_type = ALGORITHM_STREAM_INPUT_TYPE1;
#else*/
algo_config.input_type = ALGORITHM_STREAM_INPUT_TYPE1;
//algo_config.input_type = ALGORITHM_STREAM_INPUT_TYPE2;
//#endif
algo_config.task_core = 1;
#ifdef DEBUG_ALGO_INPUT
algo_config.debug_input = true;
#endif
//algo_config.algo_mask = ALGORITHM_STREAM_USE_AGC | ALGORITHM_STREAM_USE_NS;

audio_element_handle_t element_algo = algo_stream_init(&algo_config);
audio_element_set_music_info(element_algo, EXAMPLE_AUDIO_SAMPLE_RATE, 1, EXAMPLE_AUDIO_BITS);

ESP_LOGI(TAG, "[3.2] Create wav encoder to encode wav format");
wav_encoder_cfg_t wav_cfg = DEFAULT_WAV_ENCODER_CONFIG();
audio_element_handle_t wav_encoder = wav_encoder_init(&wav_cfg);

ESP_LOGI(TAG, "[3.1] Create http stream to post data to server");
http_stream_cfg_t http_cfg = HTTP_STREAM_CFG_DEFAULT();
http_cfg.type = AUDIO_STREAM_WRITER;
http_cfg.event_handle = _http_stream_event_handle;
http_stream_writer = http_stream_init(&http_cfg);

const esp_timer_create_args_t oneshot_start_timer_args = {
.callback = &oneshot_start_timer_callback,
/* argument specified here will be passed to timer callback function */
.arg = (void *)http_stream_writer,
.name = "one-shot_start"
};
esp_timer_handle_t oneshot_start_timer;
ESP_ERROR_CHECK(esp_timer_create(&oneshot_start_timer_args, &oneshot_start_timer));

const esp_timer_create_args_t oneshot_stop_timer_args = {
.callback = &oneshot_stop_timer_callback,
/* argument specified here will be passed to timer callback function */
//.arg = (void*) periodic_timer,
.name = "one-shot_stop"
};
esp_timer_handle_t oneshot_stop_timer;
ESP_ERROR_CHECK(esp_timer_create(&oneshot_stop_timer_args, &oneshot_stop_timer));

/* Start the timers */
ESP_ERROR_CHECK(esp_timer_start_once(oneshot_start_timer, 3000000));
ESP_ERROR_CHECK(esp_timer_start_once(oneshot_stop_timer, 15000000));
ESP_LOGI(TAG, "Started timers, time since boot: %lld us", esp_timer_get_time());

ESP_LOGI(TAG, "[3.4] Register all elements to audio pipeline_rec");
audio_pipeline_register(pipeline_rec, i2s_stream_reader, "i2s_rd");
audio_pipeline_register(pipeline_rec, filter_r, "filter_r");
audio_pipeline_register(pipeline_rec, element_algo, "algo");
audio_pipeline_register(pipeline_rec, wav_encoder, "wav_encoder");
audio_pipeline_register(pipeline_rec, http_stream_writer, "http");

ESP_LOGI(TAG, "[3.5] Link it together [codec_chip]-->i2s_stream-->filter-->algorithm-->wav_encoder-->[http]");
const char *link_rec[5] = {"i2s_rd", "filter_r", "algo", "wav_encoder", "http"};
audio_pipeline_link(pipeline_rec, &link_rec[0], 5);

/*
ESP_LOGI(TAG, "[3.5] Link it together [codec_chip]-->i2s_stream-->algorithm-->wav_encoder-->[http]");
const char *link_rec[4] = {"i2s_rd", "algo", "wav_encoder", "http"};
audio_pipeline_link(pipeline_rec, &link_rec[0], 4);
*/

/*
ESP_LOGI(TAG, "[3.5] Link it together [codec_chip]-->i2s_stream-->wav_encoder-->[http]");
const char *link_rec[3] = {"i2s_rd", "wav_encoder", "http"};
audio_pipeline_link(pipeline_rec, &link_rec[0], 3);*/


//#ifndef CONFIG_ESP_LYRAT_MINI_V1_1_BOARD
//Please reference the way of ALGORITHM_STREAM_INPUT_TYPE2 in "algorithm_stream.h"
ringbuf_handle_t ringbuf_ref = rb_create(50 * 1024, 1);
audio_element_set_multi_input_ringbuf(element_algo, ringbuf_ref, 0);
audio_element_set_multi_output_ringbuf(http_stream_writer, ringbuf_ref, 0);

/* When the playback signal far ahead of the recording signal,
the playback signal needs to be delayed */
algo_stream_set_delay(http_stream_writer, ringbuf_ref, DEFAULT_REF_DELAY_MS);

/* When the playback signal after the recording signal,
the recording signal needs to be delayed */
algo_stream_set_delay(element_algo, audio_element_get_input_ringbuf(element_algo), DEFAULT_REC_DELAY_MS);
//#endif

i2s_stream_set_clk(i2s_stream_reader, EXAMPLE_AUDIO_SAMPLE_RATE, EXAMPLE_AUDIO_BITS, EXAMPLE_AUDIO_CHANNELS);

ESP_LOGI(TAG, "[5.0] Set up event listener");
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);


ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);

//ESP_LOGI(TAG, "[6.0] Start audio_pipeline");
//audio_pipeline_run(pipeline_rec);

ESP_LOGI(TAG, "[7.0] Listen for all pipeline events");

ESP_LOGI(TAG, "[ 4 ] Press [Rec] button to record, Press [Mode] to exit");
xEventGroupWaitBits(EXIT_FLAG, BIT0, true, false, portMAX_DELAY);

ESP_LOGI(TAG, "[8.0] Stop audio_pipeline");

audio_pipeline_stop(pipeline_rec);
audio_pipeline_wait_for_stop(pipeline_rec);
audio_pipeline_deinit(pipeline_rec);

/* Stop all periph before removing the listener */
esp_periph_set_stop_all(set);
audio_event_iface_remove_listener(esp_periph_set_get_event_iface(set), evt);

/* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
audio_event_iface_destroy(evt);

esp_periph_set_destroy(set);
}

dengbq
Posts: 38
Joined: Thu Mar 31, 2022 8:45 am

Re: algo_stream怎样使用

Postby dengbq » Tue Oct 11, 2022 7:40 am

发错地方的,应该到中文版的,没法删除啊.

Who is online

Users browsing this forum: No registered users and 67 guests