淘先锋技术网

首页 1 2 3 4 5 6 7

ASR(Automatic Speech Recognition)自动语音识别技术是一种将人的语音转换为文本的技术。

一、freeswitch如何使用asr

freeswitch提供两个app功能detect_speech和play_and_detect_speech给用户调用,detect_speech是异步的,play_and_detect_speech是同步的。

1、detect_speech

1.1语法:detect_speech <mod_name> <gram_name> <name> <dest>

detect_speech stop 

mod_name: 识别模块名称 如ali_asr[:params],其中params是param1=value1,param2=value2类型格式传入到switch_asr_interface_t接口中asr_open接口函数

gram_name:switch_asr_interface_t接口中asr_load_grammar接口函数传入参数

name: switch_asr_interface_t接口中asr_load_grammar接口函数传入参数

dest:switch_asr_interface_t接口中asr_open接口函数传入参数

stop:停止识别

1.2识别事件:设置通道变量fire_asr_events为true。freeswitch会触发SWITCH_EVENT_DETECTED_SPEECH事件返回给事件回调函数。

1.2.1事件头Speech-Type:

begin-speaking:检测到用户说话

detected-speech:识别结果

closed:识别关闭

1.2.2 事件body:识别结果

备注:事件可以用switch_event_bind("mod_asr", SWITCH_EVENT_DETECTED_SPEECH, NULL, my_event_handler, NULL)绑定;

2、play_and_detect_speech

2.1语法 play_and_detect_speech <file> detect:<engine>[:<mrcp_profile>] {param1=val1,param2=val2}<grammar>

file:语音文件或者tts

engine:识别模块

mrcp_profile:mrcp文件

{param1=val1,param2=val2}<grammar> 参数+语法文件

2.2用户可以通过app返回通道变量detect_speech_result获取识别结果

综上所述,异步函数detect_speech可以通过事件SWITCH_EVENT_DETECTED_SPEECH获取识别结果,同步函数 play_and_detect_speech既可以交互语音还能获取识别结果。

二、如何实现一个自定义的asr模块

 众所周知:市面上的asr对接的中文识别引擎有阿里,科大讯飞,百度,想在fs中实现对接有3种方式:

1、mrcp对接,fs提供mod_unimrcp模块通过mrcp对接各类厂家asr服务,前提是厂家支持这种协议。具体细节可以参考官网的例子mod_unimrcp - FreeSWITCH - Confluence

2、通过实现media bug接口来实现

我们可以通过switch_core_media_bug_add(session, "my_detect_speech", key, speech_callback, sth, 0, SMBF_READ_STREAM | SMBF_NO_PAUSE | SMBF_ONE_ONLY, &sth->bug))来绑定识别回调函数,我们只要简单的实现回调函数来获取语音流媒体,把流媒体发送给识别引擎就获取识别结果。

static switch_bool_t speech_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
{
	fastcall_speech_handle_t *sth = (fastcall_speech_handle_t *) user_data;

    uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
	switch_frame_t frame = { 0 };
	switch_asr_flag_t flags = SWITCH_ASR_FLAG_DATA;

	frame.data = data;
	frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;

	switch (type) {
	case SWITCH_ABC_TYPE_INIT:
		{

		}
		break;
	case SWITCH_ABC_TYPE_CLOSE:
		{

		}
		break;
	case SWITCH_ABC_TYPE_READ:

		if (sth->ah) {
			if (switch_core_media_bug_read(bug, &frame, SWITCH_FALSE) != SWITCH_STATUS_FALSE) {

                 //frame.data, frame.datalen是我们需要的语音数据
				}
			}
		}

		break;
	case SWITCH_ABC_TYPE_WRITE:
	default:
		break;
	}

	return SWITCH_TRUE;
}

3、通过fs现有ASR接口方式

asr_interface = (switch_asr_interface_t *)switch_loadable_module_create_interface(*module_interface, SWITCH_ASR_INTERFACE);
	asr_interface->interface_name = "fastcall_asr";
	asr_interface->asr_open = fastcall_asr_open;
	asr_interface->asr_load_grammar = fastcall_asr_load_grammar;
	asr_interface->asr_unload_grammar = fastcall_asr_unload_grammar;
	asr_interface->asr_close = fastcall_asr_close;
	asr_interface->asr_feed = fastcall_asr_feed;
	//asr_interface->asr_feed_dtmf = fastcall_asr_feed_dtmf;
	asr_interface->asr_resume = fastcall_asr_resume;
	asr_interface->asr_pause = fastcall_asr_pause;
	asr_interface->asr_start_input_timers = fastcall_asr_start_input_timers;
	asr_interface->asr_check_results = fastcall_asr_check_results;
	asr_interface->asr_get_results = fastcall_asr_get_results;
	asr_interface->asr_text_param = fastcall_asr_text_param;
	asr_interface->asr_numeric_param = fastcall_asr_numeric_param;
	asr_interface->asr_float_param = fastcall_asr_float_param;

我们只需要实现对应的函数就可以获取到识别结果。

三、软件架构设计

 1、编写mod_AI模块实现Freeswitch的ASR和TTS接口。

 2、编写具有插件功能的ASR_Server系统,生成接口

struct fsl_asr_interface {
	/*! the name of the interface */
	const char *interface_name;
	/*! function to open the asr interface */
	int (*asr_open) (fsl_asr_handle_t *ah, char *param, int realtime);
	/*! function to close the asr interface */
	int (*asr_close) (fsl_asr_handle_t *ah);
	int (*asr_start_session) (fsl_asr_handle_t* ah, int rate, char *param);
	int (*asr_stop_session) (fsl_asr_handle_t* ah);
	/*! function to feed audio to the ASR */
	int (*asr_feed) (fsl_asr_handle_t *ah, void *data, unsigned int len, int audio_state, int* ep_stat, int* rec_stat);
	int (*asr_realtime_feed) (fsl_asr_handle_t* ah, void* data, unsigned int len, char** rst);
	/*! function to read results from the ASR */
	int (*asr_get_result)(fsl_asr_handle_t *ah, char **rst);
	fsl_thread_rwlock_t *rwlock;
	int refs;
	fsl_mutex_t *reflock;
	fsl_loadable_module_interface_t *parent;
	struct fsl_asr_interface *next;
};

给插件使用,方便扩展。

3、分别编写阿里,讯飞,百度模块提供识别引擎功能

4、ASR_Server通过mod_AI传送过来的参数interface_name来调用对应的模块进行识别。

更多探讨请加qq群640880657。