mplayer+科大讯飞离线语音包

mplayer+科大讯飞离线语音包

文章目录

; 【1】mplayer

1.将mplayer放到开发板的bin目录下

system(“mplayer 1.avi &”); // &为后台播放视频

播放音乐

system(“madplay -a -10 xxx.mp3”);//-a 音量控制 范围(-175~+18)

暂停播放

system(“killall -STOP madplay &”);

恢复播放

system(“killall -CONT madplay &”);

停止

system(“killall madplay”);

madplay终止

system(“killall -9 madplay”);

思路 :如何保证将电脑的音频或视频传输给开发板,保持数据完整

1.先发送文件大小
2.再发送数据内容
3.检测发送的数据与接收的数据大小是否一致

【2】科大讯飞离线语音包

链接:https://pan.baidu.com/s/1Dgo8HGs3_Bfw05M0dCbS4A?pwd=vxpz
提取码:vxpz
README文件

README for Linux_aitalk

bin:
|-- msc
    |-- msc.cfg(作用:msc调试、生成msc日志)
    |-- res
        |-- asr(资源文件)
    |-- wav
        |-- ddhghlj.pcm
        |-- ddhgdw.pcm
    |-- call.bnf

doc:
|-- readme.txt

include:调用SDK所需头文件

libs:
|-- x86
    |-- libmsc.so(32位动态库)
|-- x64
    |-- libmsc.so(64位动态库)

samples:
|-- asr_sample
    |-- asr_sample.c
    |-- Makefile
    |-- 32bit_make.sh
    |-- 64bit_make.sh
|-- asr_record_sample
    |-- formats.h
    |-- linuxrec.h
    |-- linuxrec.c
    |-- speech_recognizer.h
    |-- speech_recognizer.c
    |-- asr_record_sample.c
    |-- Makefile
    |-- 32bit_make.sh
    |-- 64bit_make.sh

asr_offline_sample.c

#include
#include
#include
#include
#include

#include "../../include/qisr.h"
#include "../../include/msp_cmn.h"
#include "../../include/msp_errors.h"

#define SAMPLE_RATE_16K     (16000)
#define SAMPLE_RATE_8K      (8000)
#define MAX_GRAMMARID_LEN   (32)
#define MAX_PARAMS_LEN      (1024)

const char * ASR_RES_PATH        = "fo|res/asr/common.jet";
const char * GRM_BUILD_PATH      = "res/asr/GrmBuilld";
const char * GRM_FILE            = "call.bnf";
const char * LEX_NAME            = "contact";

typedef struct _UserData {
    int     build_fini;
    int     update_fini;
    int     errcode;
    char    grammar_id[MAX_GRAMMARID_LEN];
}UserData;

const char *get_audio_file(void);
int build_grammar(UserData *udata);
int update_lexicon(UserData *udata);
int run_asr(UserData *udata);

const char* get_audio_file(void)
{
    char key = 0;
    while(key != 27)
    {
        printf("请选择音频文件:\n");
        printf("1.打电话给丁伟\n");
        printf("2.打电话给黄辣椒\n");
        key = getchar();
        getchar();
        switch(key)
        {
        case '1':
            printf("\n1.打电话给丁伟\n");
            return "wav/ddhgdw.pcm";
        case '2':
            printf("\n2.打电话给黄辣椒\n");
            return "wav/ddhghlj.pcm";
        default:
            continue;
        }
    }
    exit(0);
    return NULL;
}

int build_grm_cb(int ecode, const char *info, void *udata)
{
    UserData *grm_data = (UserData *)udata;

    if (NULL != grm_data) {
        grm_data->build_fini = 1;
        grm_data->errcode = ecode;
    }

    if (MSP_SUCCESS == ecode && NULL != info) {
        printf("构建语法成功! 语法ID:%s\n", info);
        if (NULL != grm_data)
            snprintf(grm_data->grammar_id, MAX_GRAMMARID_LEN - 1, info);
    }
    else
        printf("构建语法失败!%d\n", ecode);

    return 0;
}

int build_grammar(UserData *udata)
{
    FILE *grm_file                           = NULL;
    char *grm_content                        = NULL;
    unsigned int grm_cnt_len                 = 0;
    char grm_build_params[MAX_PARAMS_LEN]    = {NULL};
    int ret                                  = 0;

    grm_file = fopen(GRM_FILE, "rb");
    if(NULL == grm_file) {
        printf("打开\"%s\"文件失败![%s]\n", GRM_FILE, strerror(errno));
        return -1;
    }

    fseek(grm_file, 0, SEEK_END);
    grm_cnt_len = ftell(grm_file);
    fseek(grm_file, 0, SEEK_SET);

    grm_content = (char *)malloc(grm_cnt_len + 1);
    if (NULL == grm_content)
    {
        printf("内存分配失败!\n");
        fclose(grm_file);
        grm_file = NULL;
        return -1;
    }
    fread((void*)grm_content, 1, grm_cnt_len, grm_file);
    grm_content[grm_cnt_len] = '\0';
    fclose(grm_file);
    grm_file = NULL;

    snprintf(grm_build_params, MAX_PARAMS_LEN - 1,
        "engine_type = local, \
        asr_res_path = %s, sample_rate = %d, \
        grm_build_path = %s, ",
        ASR_RES_PATH,
        SAMPLE_RATE_16K,
        GRM_BUILD_PATH
        );
    ret = QISRBuildGrammar("bnf", grm_content, grm_cnt_len, grm_build_params, build_grm_cb, udata);

    free(grm_content);
    grm_content = NULL;

    return ret;
}

int update_lex_cb(int ecode, const char *info, void *udata)
{
    UserData *lex_data = (UserData *)udata;

    if (NULL != lex_data) {
        lex_data->update_fini = 1;
        lex_data->errcode = ecode;
    }

    if (MSP_SUCCESS == ecode)
        printf("更新词典成功!\n");
    else
        printf("更新词典失败!%d\n", ecode);

    return 0;
}

int update_lexicon(UserData *udata)
{
    const char *lex_content                   = "丁伟\n黄辣椒";
    unsigned int lex_cnt_len                  = strlen(lex_content);
    char update_lex_params[MAX_PARAMS_LEN]    = {NULL};

    snprintf(update_lex_params, MAX_PARAMS_LEN - 1,
        "engine_type = local, text_encoding = UTF-8, \
        asr_res_path = %s, sample_rate = %d, \
        grm_build_path = %s, grammar_list = %s, ",
        ASR_RES_PATH,
        SAMPLE_RATE_16K,
        GRM_BUILD_PATH,
        udata->grammar_id);
    return QISRUpdateLexicon(LEX_NAME, lex_content, lex_cnt_len, update_lex_params, update_lex_cb, udata);
}

int run_asr(UserData *udata)
{
    char asr_params[MAX_PARAMS_LEN]    = {NULL};
    const char *rec_rslt               = NULL;
    const char *session_id             = NULL;
    const char *asr_audiof             = NULL;
    FILE *f_pcm                        = NULL;
    char *pcm_data                     = NULL;
    long pcm_count                     = 0;
    long pcm_size                      = 0;
    int last_audio                     = 0;
    int aud_stat                       = MSP_AUDIO_SAMPLE_CONTINUE;
    int ep_status                      = MSP_EP_LOOKING_FOR_SPEECH;
    int rec_status                     = MSP_REC_STATUS_INCOMPLETE;
    int rss_status                     = MSP_REC_STATUS_INCOMPLETE;
    int errcode                        = -1;

    asr_audiof = get_audio_file();
    f_pcm = fopen(asr_audiof, "rb");
    if (NULL == f_pcm) {
        printf("打开\"%s\"失败![%s]\n", f_pcm, strerror(errno));
        goto run_error;
    }
    fseek(f_pcm, 0, SEEK_END);
    pcm_size = ftell(f_pcm);
    fseek(f_pcm, 0, SEEK_SET);
    pcm_data = (char *)malloc(pcm_size);
    if (NULL == pcm_data)
        goto run_error;
    fread((void *)pcm_data, pcm_size, 1, f_pcm);
    fclose(f_pcm);
    f_pcm = NULL;

    snprintf(asr_params, MAX_PARAMS_LEN - 1,
        "engine_type = local, \
        asr_res_path = %s, sample_rate = %d, \
        grm_build_path = %s, local_grammar = %s, \
        result_type = xml, result_encoding = UTF-8, ",
        ASR_RES_PATH,
        SAMPLE_RATE_16K,
        GRM_BUILD_PATH,
        udata->grammar_id
        );
    session_id = QISRSessionBegin(NULL, asr_params, &errcode);
    if (NULL == session_id)
        goto run_error;
    printf("开始识别...\n");

    while (1) {
        unsigned int len = 6400;

        if (pcm_size < 12800) {
            len = pcm_size;
            last_audio = 1;
        }

        aud_stat = MSP_AUDIO_SAMPLE_CONTINUE;

        if (0 == pcm_count)
            aud_stat = MSP_AUDIO_SAMPLE_FIRST;

        if (len  0)
            break;

        printf(">");
        fflush(stdout);
        errcode = QISRAudioWrite(session_id, (const void *)&pcm_data[pcm_count], len, aud_stat, &ep_status, &rec_status);
        if (MSP_SUCCESS != errcode)
            goto run_error;

        pcm_count += (long)len;
        pcm_size -= (long)len;

        if (MSP_EP_AFTER_SPEECH == ep_status)
            break;

        usleep(150 * 1000);
    }

    QISRAudioWrite(session_id, (const void *)NULL, 0, MSP_AUDIO_SAMPLE_LAST, &ep_status, &rec_status);

    free(pcm_data);
    pcm_data = NULL;

    while (MSP_REC_STATUS_COMPLETE != rss_status && MSP_SUCCESS == errcode) {
        rec_rslt = QISRGetResult(session_id, &rss_status, 0, &errcode);
        usleep(150 * 1000);
    }
    printf("\n识别结束:\n");
    printf("=============================================================\n");
    if (NULL != rec_rslt)
        printf("%s\n", rec_rslt);
    else
        printf("没有识别结果!\n");
    printf("=============================================================\n");

    goto run_exit;

run_error:
    if (NULL != pcm_data) {
        free(pcm_data);
        pcm_data = NULL;
    }
    if (NULL != f_pcm) {
        fclose(f_pcm);
        f_pcm = NULL;
    }
run_exit:
    QISRSessionEnd(session_id, NULL);
    return errcode;
}

int main(int argc, char* argv[])
{
    const char *login_config    = "appid = a54c3878";
    UserData asr_data;
    int ret                     = 0 ;
    char c;

    ret = MSPLogin(NULL, NULL, login_config);
    if (MSP_SUCCESS != ret) {
        printf("登录失败:%d\n", ret);
        goto exit;
    }

    memset(&asr_data, 0, sizeof(UserData));
    printf("构建离线识别语法网络...\n");
    ret = build_grammar(&asr_data);
    if (MSP_SUCCESS != ret) {
        printf("构建语法调用失败!\n");
        goto exit;
    }
    while (1 != asr_data.build_fini)
        usleep(300 * 1000);
    if (MSP_SUCCESS != asr_data.errcode)
        goto exit;
    printf("离线识别语法网络构建完成,开始识别...\n");
    ret = run_asr(&asr_data);
    if (MSP_SUCCESS != ret) {
        printf("离线语法识别出错: %d \n", ret);
        goto exit;
    }

    printf("更新离线语法词典...\n");
    ret = update_lexicon(&asr_data);
    if (MSP_SUCCESS != ret) {
        printf("更新词典调用失败!\n");
        goto exit;
    }
    while (1 != asr_data.update_fini)
        usleep(300 * 1000);
    if (MSP_SUCCESS != asr_data.errcode)
        goto exit;
    printf("更新离线语法词典完成,开始识别...\n");
    ret = run_asr(&asr_data);
    if (MSP_SUCCESS != ret) {
        printf("离线语法识别出错: %d \n", ret);
        goto exit;
    }

exit:
    MSPLogout();
    printf("请按任意键退出...\n");
    getchar();
    return 0;
}

Makefile

#common makefile header

DIR_INC = ../../include
DIR_BIN = ../../bin
DIR_LIB = ../../libs

TARGET  = asr_offline_sample
BIN_TARGET = $(DIR_BIN)/$(TARGET)

CROSS_COMPILE =
CFLAGS = -g -Wall -I$(DIR_INC)

ifdef LINUX64
LDFLAGS := -L$(DIR_LIB)/x64
else
LDFLAGS := -L$(DIR_LIB)/x86
endif
LDFLAGS += -lmsc -lrt -ldl -lpthread  -lstdc++

OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c))

$(BIN_TARGET) : $(OBJECTS)
    $(CROSS_COMPILE)gcc $(CFLAGS) $^ -o $@ $(LDFLAGS)

%.o : %.c
    $(CROSS_COMPILE)gcc -c $(CFLAGS) $< -o $@
clean:
    @rm -f *.o $(BIN_TARGET)

.PHONY:clean

#common makefile foot

mplayer+科大讯飞离线语音包
call.bnf

!grammar call;
!slot <want>;
!slot <dialpre>;
!slot <dialsuf>;
!slot <contact>;

!start <callstart>;
<callstart>:[<want>]<dial>;
<want>:我想|我要|请|帮我|我想要|请帮我;
<dial>:<dialpre><contact>[<dialsuf>];
<dialpre>:打电话给!id(10001)|打给!id(10001)|拨打!id(10001)|拨打电话给!id(10001)|呼叫!id(10001)|
打一个电话给!id(10001)|打个电话给!id(10001)|给|拨通!id(10001)|
接通!id(10001)|呼叫!id(10001)|呼叫给!id(10001)|打!id(10001);
<dialsuf>:打电话!id(10001)|打个电话!id(10001)|打一个电话!id(10001)|
拨打电话!id(10001)|拨电话!id(10001)|拨个电话!id(10001)|呼个电话!id(10001)|
的电话!id(10001)|的号码!id(10001)|的手机!id(10001)|
的办公电话!id(10001)|的移动号码!id(10001)|的联通号码!id(10001)|
的电信号码!id(10001)|客服电话!id(10001);
<contact>:丁伟;

Original: https://blog.csdn.net/m0_45463480/article/details/125033058
Author: 红客白帽
Title: mplayer+科大讯飞离线语音包

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/813674/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球