修改ReadMeFirst,同时删除opensrc中没有用到的代码
This commit is contained in:
parent
0fbf5b0c9f
commit
07ad4c0b33
|
@ -4,18 +4,26 @@ This is a brief introduce of our software program.
|
|||
|---->幼儿实时监视预警系统-汇报PPT:课程实践过程中产生的PPT文件
|
||||
|---->幼儿实时监视预警系统-项目简介.pptx: 项目初期的一个简单介绍的PPT文件
|
||||
|---->幼儿实时监视预警系统 -第一次迭代汇报.pptx:第一学期结课时汇报使用的PPT文件
|
||||
|---->幼儿实时监视预警系统-项目进度报告.pptx
|
||||
|---->幼儿实时监视预警系统-项目进度报告.pptx: 第二学期开学时进度汇报的PPT文件
|
||||
|---->幼儿实时监视预警系统-课程实践汇报.pptx:课程实践基本完成使用的汇报PPT文件
|
||||
|---->幼儿实时监视预警系统宣传材料
|
||||
|---->幼儿实时监视预警系统-实践自评表.xlsx
|
||||
|---->幼儿实时监视预警系统-软件需求构想与描述.docx
|
||||
|---->幼儿实时监视预警系统-软件需求规格说明书.doc
|
||||
|---->幼儿实时监视预警系统.png: 项目的宣传彩页
|
||||
|---->幼儿实时监视预警系统-实践自评表.xlsx: 项目的实践自评表
|
||||
|---->幼儿实时监视预警系统-软件需求构想与描述.docx: 软件需求与构想文档
|
||||
|---->幼儿实时监视预警系统-软件需求规格说明书.doc: 软件的需求规格说明书
|
||||
|
||||
model: 软件设计中产生的模型
|
||||
|---->幼儿实时监视预警系统 - 类设计模型.docx
|
||||
|---->幼儿实时监视预警系统- 数据库设计模型.docx
|
||||
|---->幼儿实时监视预警系统- 用例设计模型.vsdx
|
||||
|---->幼儿实时监视预警系统- 界面类图.pdf
|
||||
|---->幼儿实时监视预警系统- 类设计模型.vsdx
|
||||
|---->幼儿实时监视预警系统- 软件体系结构图.docx
|
||||
|
||||
src: 存储软件设计过程中的相关代码
|
||||
|---->dangerDetect: 通过图像识别的方式检测幼儿是否可能受到危险
|
||||
|---->EBaby: 手机端的代码
|
||||
|---->Socket: 手机和上网本通信部分代码
|
||||
|
||||
openSrc:使用的开源软件代码
|
||||
|
||||
draft: 文档撰写过程中的原材料,不一一枚举
|
||||
|---->cryDetect: 哭泣检测部分相关代码
|
||||
|---->openSrc:使用的开源软件代码
|
||||
|---->ReadMeFirst.txt: 对于代码的说明
|
|
@ -1 +1,6 @@
|
|||
当前代码为软件原型的实现代码,还没有做精化
|
||||
dangerDetect: 通过图像识别的方式检测幼儿是否可能受到危险
|
||||
EBaby: 手机端的代码
|
||||
Socket: 手机和上网本通信部分代码
|
||||
cryDetect: 哭泣检测部分相关代码
|
||||
openSrc:使用的开源软件代码
|
||||
说明:openSrc为项目引入的开源代码,其余部分为自己编写的代码
|
|
@ -1,272 +0,0 @@
|
|||
/*
|
||||
* 语音识别(Automatic Speech Recognition)技术能够从语音中识别出特定的命令词或语句模式。
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "qisr.h"
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
|
||||
#define BUFFER_SIZE 2048
|
||||
#define HINTS_SIZE 100
|
||||
#define GRAMID_LEN 128
|
||||
#define FRAME_LEN 640
|
||||
|
||||
int get_grammar_id(char* grammar_id, unsigned int id_len)
|
||||
{
|
||||
FILE* fp = NULL;
|
||||
char* grammar = NULL;
|
||||
unsigned int grammar_len = 0;
|
||||
unsigned int read_len = 0;
|
||||
const char* ret_id = NULL;
|
||||
unsigned int ret_id_len = 0;
|
||||
int ret = -1;
|
||||
|
||||
if (NULL == grammar_id)
|
||||
goto grammar_exit;
|
||||
|
||||
fp = fopen("gm_continuous_digit.abnf", "rb");
|
||||
if (NULL == fp)
|
||||
{
|
||||
printf("\nopen grammar file failed!\n");
|
||||
goto grammar_exit;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
grammar_len = ftell(fp); //获取语法文件大小
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
grammar = (char*)malloc(grammar_len + 1);
|
||||
if (NULL == grammar)
|
||||
{
|
||||
printf("\nout of memory!\n");
|
||||
goto grammar_exit;
|
||||
}
|
||||
|
||||
read_len = fread((void *)grammar, 1, grammar_len, fp); //读取语法内容
|
||||
if (read_len != grammar_len)
|
||||
{
|
||||
printf("\nread grammar error!\n");
|
||||
goto grammar_exit;
|
||||
}
|
||||
grammar[grammar_len] = '\0';
|
||||
|
||||
ret_id = MSPUploadData("usergram", grammar, grammar_len, "dtt = abnf, sub = asr", &ret); //上传语法
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("\nMSPUploadData failed, error code: %d.\n", ret);
|
||||
goto grammar_exit;
|
||||
}
|
||||
|
||||
ret_id_len = strlen(ret_id);
|
||||
if (ret_id_len >= id_len)
|
||||
{
|
||||
printf("\nno enough buffer for grammar_id!\n");
|
||||
goto grammar_exit;
|
||||
}
|
||||
strncpy(grammar_id, ret_id, ret_id_len);
|
||||
printf("grammar_id: \"%s\" \n", grammar_id); //下次可以直接使用该ID,不必重复上传语法。
|
||||
|
||||
grammar_exit:
|
||||
if (NULL != fp)
|
||||
{
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
if (NULL!= grammar)
|
||||
{
|
||||
free(grammar);
|
||||
grammar = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void run_asr(const char* audio_file, const char* params, char* grammar_id)
|
||||
{
|
||||
const char* session_id = NULL;
|
||||
char rec_result[BUFFER_SIZE] = {'\0'};
|
||||
char hints[HINTS_SIZE] = {'\0'}; //hints为结束本次会话的原因描述,由用户自定义
|
||||
unsigned int total_len = 0;
|
||||
int aud_stat = MSP_AUDIO_SAMPLE_CONTINUE; //音频状态
|
||||
int ep_stat = MSP_EP_LOOKING_FOR_SPEECH; //端点检测
|
||||
int rec_stat = MSP_REC_STATUS_SUCCESS; //识别状态
|
||||
int errcode = MSP_SUCCESS;
|
||||
|
||||
FILE* f_pcm = NULL;
|
||||
char* p_pcm = NULL;
|
||||
long pcm_count = 0;
|
||||
long pcm_size = 0;
|
||||
long read_size = 0;
|
||||
|
||||
if (NULL == audio_file)
|
||||
goto asr_exit;
|
||||
|
||||
f_pcm = fopen(audio_file, "rb");
|
||||
if (NULL == f_pcm)
|
||||
{
|
||||
printf("\nopen [%s] failed!\n", audio_file);
|
||||
goto asr_exit;
|
||||
}
|
||||
|
||||
fseek(f_pcm, 0, SEEK_END);
|
||||
pcm_size = ftell(f_pcm); //获取音频文件大小
|
||||
fseek(f_pcm, 0, SEEK_SET);
|
||||
|
||||
p_pcm = (char*)malloc(pcm_size);
|
||||
if (NULL == p_pcm)
|
||||
{
|
||||
printf("\nout of memory!\n");
|
||||
goto asr_exit;
|
||||
}
|
||||
|
||||
read_size = fread((void *)p_pcm, 1, pcm_size, f_pcm); //读取音频文件内容
|
||||
if (read_size != pcm_size)
|
||||
{
|
||||
printf("\nread [%s] failed!\n", audio_file);
|
||||
goto asr_exit;
|
||||
}
|
||||
|
||||
printf("\n开始语音识别 ...\n");
|
||||
session_id = QISRSessionBegin(grammar_id, params, &errcode);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRSessionBegin failed, error code:%d\n", errcode);
|
||||
goto asr_exit;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
unsigned int len = 10 * FRAME_LEN; // 每次写入200ms音频(16k,16bit):1帧音频20ms,10帧=200ms。16k采样率的16位音频,一帧的大小为640Byte
|
||||
int ret = 0;
|
||||
|
||||
if (pcm_size < 2 * len)
|
||||
len = pcm_size;
|
||||
if (len <= 0)
|
||||
break;
|
||||
|
||||
aud_stat = MSP_AUDIO_SAMPLE_CONTINUE;
|
||||
if (0 == pcm_count)
|
||||
aud_stat = MSP_AUDIO_SAMPLE_FIRST;
|
||||
|
||||
printf(">");
|
||||
ret = QISRAudioWrite(session_id, (const void *)&p_pcm[pcm_count], len, aud_stat, &ep_stat, &rec_stat);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("\nQISRAudioWrite failed, error code:%d\n",ret);
|
||||
goto asr_exit;
|
||||
}
|
||||
|
||||
pcm_count += (long)len;
|
||||
pcm_size -= (long)len;
|
||||
|
||||
if (MSP_EP_AFTER_SPEECH == ep_stat)
|
||||
break;
|
||||
usleep(200*1000); //模拟人说话时间间隙,10帧的音频长度为200ms
|
||||
}
|
||||
errcode = QISRAudioWrite(session_id, NULL, 0, MSP_AUDIO_SAMPLE_LAST, &ep_stat, &rec_stat);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRAudioWrite failed, error code:%d\n",errcode);
|
||||
goto asr_exit;
|
||||
}
|
||||
|
||||
while (MSP_REC_STATUS_COMPLETE != rec_stat)
|
||||
{
|
||||
const char *rslt = QISRGetResult(session_id, &rec_stat, 0, &errcode);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRGetResult failed, error code: %d\n", errcode);
|
||||
goto asr_exit;
|
||||
}
|
||||
if (NULL != rslt)
|
||||
{
|
||||
unsigned int rslt_len = strlen(rslt);
|
||||
total_len += rslt_len;
|
||||
if (total_len >= BUFFER_SIZE)
|
||||
{
|
||||
printf("\nno enough buffer for rec_result !\n");
|
||||
goto asr_exit;
|
||||
}
|
||||
strncat(rec_result, rslt, rslt_len);
|
||||
}
|
||||
usleep(150*1000); //防止频繁占用CPU
|
||||
}
|
||||
printf("\n语音识别结束\n");
|
||||
printf("=============================================================\n");
|
||||
printf("%s",rec_result);
|
||||
printf("=============================================================\n");
|
||||
|
||||
asr_exit:
|
||||
if (NULL != f_pcm)
|
||||
{
|
||||
fclose(f_pcm);
|
||||
f_pcm = NULL;
|
||||
}
|
||||
if (NULL != p_pcm)
|
||||
{
|
||||
free(p_pcm);
|
||||
p_pcm = NULL;
|
||||
}
|
||||
|
||||
QISRSessionEnd(session_id, hints);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ret = MSP_SUCCESS;
|
||||
const char* login_params = "appid = 5afda937, work_dir = ."; //登录参数,appid与msc库绑定,请勿随意改动
|
||||
/*
|
||||
* sub: 请求业务类型
|
||||
* result_type: 识别结果格式
|
||||
* result_encoding: 结果编码格式
|
||||
*
|
||||
* 详细参数说明请参阅《讯飞语音云MSC--API文档》
|
||||
*/
|
||||
const char* session_begin_params = "sub = asr, result_type = plain, result_encoding = utf8";
|
||||
char* grammar_id = NULL;
|
||||
|
||||
/* 用户登录 */
|
||||
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名,第二个参数是密码,均传NULL即可,第三个参数是登录参数
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("MSPLogin failed, error code: %d.\n",ret);
|
||||
goto exit; //登录失败,退出登录
|
||||
}
|
||||
|
||||
printf("\n##################################################\n");
|
||||
printf("## 语音识别(Automatic Speech Recognition)技术 ##\n");
|
||||
printf("## 能够从语音中识别出特定的命令词或语句模式。 ##\n");
|
||||
printf("##################################################\n\n");
|
||||
|
||||
grammar_id = (char*)malloc(GRAMID_LEN);
|
||||
if (NULL == grammar_id)
|
||||
{
|
||||
printf("out of memory !\n");
|
||||
goto exit;
|
||||
}
|
||||
memset(grammar_id, 0, GRAMID_LEN);
|
||||
|
||||
printf("上传语法 ...\n");
|
||||
ret = get_grammar_id(grammar_id, GRAMID_LEN);
|
||||
if (MSP_SUCCESS != ret)
|
||||
goto exit;
|
||||
printf("上传语法成功\n");
|
||||
|
||||
run_asr("wav/iflytek01.wav", session_begin_params, grammar_id); //iflytek01对应的音频内容:“18012345678”
|
||||
|
||||
exit:
|
||||
if (NULL != grammar_id)
|
||||
{
|
||||
free(grammar_id);
|
||||
grammar_id = NULL;
|
||||
}
|
||||
printf("按任意键退出 ...\n");
|
||||
getchar();
|
||||
MSPLogout(); //退出登录
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,344 +0,0 @@
|
|||
/*
|
||||
* 语音听写(iFly Auto Transform)技术能够实时地将语音转换成对应的文字。
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "qisr.h"
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
#include "speech_recognizer.h"
|
||||
#include <iconv.h>
|
||||
|
||||
#define FRAME_LEN 640
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
/* Upload User words */
|
||||
static int upload_userwords()
|
||||
{
|
||||
char* userwords = NULL;
|
||||
size_t len = 0;
|
||||
size_t read_len = 0;
|
||||
FILE* fp = NULL;
|
||||
int ret = -1;
|
||||
|
||||
fp = fopen("userwords.txt", "rb");
|
||||
if (NULL == fp)
|
||||
{
|
||||
printf("\nopen [userwords.txt] failed! \n");
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
len = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
userwords = (char*)malloc(len + 1);
|
||||
if (NULL == userwords)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
read_len = fread((void*)userwords, 1, len, fp);
|
||||
if (read_len != len)
|
||||
{
|
||||
printf("\nread [userwords.txt] failed!\n");
|
||||
goto upload_exit;
|
||||
}
|
||||
userwords[len] = '\0';
|
||||
|
||||
MSPUploadData("userwords", userwords, len, "sub = uup, dtt = userword", &ret); //ÉÏ´«Óû§´Ê±í
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("\nMSPUploadData failed ! errorCode: %d \n", ret);
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
upload_exit:
|
||||
if (NULL != fp)
|
||||
{
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
if (NULL != userwords)
|
||||
{
|
||||
free(userwords);
|
||||
userwords = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void alert()
|
||||
{
|
||||
//gnome-terminal -x bash -c "rostopic pub /chatter std_msgs/String "alert" ; exec bash\"
|
||||
system("gnome-terminal -x bash -c \"rostopic pub /chatter std_msgs/String \"alert\" ; exec bash\"");
|
||||
system("play ~/alert.mp3");
|
||||
}
|
||||
|
||||
static void show_result(char *string, char is_over)
|
||||
{
|
||||
|
||||
printf("\rResult:[ %s ]", string);
|
||||
int strIndex = 0; //字符串指针的位置
|
||||
int countTimes = 0;//记录出现关键字的次数
|
||||
int len = strlen(string);
|
||||
for(strIndex = 0; strIndex < len -1;strIndex++)
|
||||
{
|
||||
if((string[strIndex] == 'h'&& string[strIndex+1] == 'a') || (string[strIndex] == 'w'&& string[strIndex+1] == 'a'))
|
||||
{
|
||||
countTimes ++;
|
||||
if(countTimes == 5)
|
||||
{
|
||||
alert();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(is_over)
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
static char *g_result = NULL;
|
||||
static unsigned int g_buffersize = BUFFER_SIZE;
|
||||
|
||||
void on_result(const char *result, char is_last)
|
||||
{
|
||||
if (result) {
|
||||
size_t left = g_buffersize - 1 - strlen(g_result);
|
||||
size_t size = strlen(result);
|
||||
if (left < size) {
|
||||
g_result = (char*)realloc(g_result, g_buffersize + BUFFER_SIZE);
|
||||
if (g_result)
|
||||
g_buffersize += BUFFER_SIZE;
|
||||
else {
|
||||
printf("mem alloc failed\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
strncat(g_result, result, size);
|
||||
show_result(g_result, is_last);
|
||||
}
|
||||
}
|
||||
void on_speech_begin()
|
||||
{
|
||||
if (g_result)
|
||||
{
|
||||
free(g_result);
|
||||
}
|
||||
g_result = (char*)malloc(BUFFER_SIZE);
|
||||
g_buffersize = BUFFER_SIZE;
|
||||
memset(g_result, 0, g_buffersize);
|
||||
|
||||
printf("Start Listening...\n");
|
||||
}
|
||||
void on_speech_end(int reason)
|
||||
{
|
||||
if (reason == END_REASON_VAD_DETECT)
|
||||
printf("\nSpeaking done \n");
|
||||
else
|
||||
printf("\nRecognizer error %d\n", reason);
|
||||
}
|
||||
|
||||
/* demo send audio data from a file */
|
||||
static void demo_file(const char* audio_file, const char* session_begin_params)
|
||||
{
|
||||
int errcode = 0;
|
||||
FILE* f_pcm = NULL;
|
||||
char* p_pcm = NULL;
|
||||
unsigned long pcm_count = 0;
|
||||
unsigned long pcm_size = 0;
|
||||
unsigned long read_size = 0;
|
||||
struct speech_rec iat;
|
||||
struct speech_rec_notifier recnotifier = {
|
||||
on_result,
|
||||
on_speech_begin,
|
||||
on_speech_end
|
||||
};
|
||||
|
||||
if (NULL == audio_file)
|
||||
goto iat_exit;
|
||||
|
||||
f_pcm = fopen(audio_file, "rb");
|
||||
if (NULL == f_pcm)
|
||||
{
|
||||
printf("\nopen [%s] failed! \n", audio_file);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
fseek(f_pcm, 0, SEEK_END);
|
||||
pcm_size = ftell(f_pcm);
|
||||
fseek(f_pcm, 0, SEEK_SET);
|
||||
|
||||
p_pcm = (char *)malloc(pcm_size);
|
||||
if (NULL == p_pcm)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
read_size = fread((void *)p_pcm, 1, pcm_size, f_pcm);
|
||||
if (read_size != pcm_size)
|
||||
{
|
||||
printf("\nread [%s] error!\n", audio_file);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
errcode = sr_init(&iat, session_begin_params, SR_USER, &recnotifier);
|
||||
if (errcode) {
|
||||
printf("speech recognizer init failed : %d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
errcode = sr_start_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("\nsr_start_listening failed! error code:%d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
unsigned int len = 10 * FRAME_LEN; /* 200ms audio */
|
||||
int ret = 0;
|
||||
|
||||
if (pcm_size < 2 * len)
|
||||
len = pcm_size;
|
||||
if (len <= 0)
|
||||
break;
|
||||
|
||||
ret = sr_write_audio_data(&iat, &p_pcm[pcm_count], len);
|
||||
|
||||
if (0 != ret)
|
||||
{
|
||||
printf("\nwrite audio data failed! error code:%d\n", ret);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
pcm_count += (long)len;
|
||||
pcm_size -= (long)len;
|
||||
}
|
||||
|
||||
errcode = sr_stop_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("\nsr_stop_listening failed! error code:%d \n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
iat_exit:
|
||||
if (NULL != f_pcm)
|
||||
{
|
||||
fclose(f_pcm);
|
||||
f_pcm = NULL;
|
||||
}
|
||||
if (NULL != p_pcm)
|
||||
{
|
||||
free(p_pcm);
|
||||
p_pcm = NULL;
|
||||
}
|
||||
|
||||
sr_stop_listening(&iat);
|
||||
sr_uninit(&iat);
|
||||
}
|
||||
|
||||
/* demo recognize the audio from microphone */
|
||||
static void demo_mic(const char* session_begin_params)
|
||||
{
|
||||
int errcode;
|
||||
int i = 0;
|
||||
|
||||
struct speech_rec iat;
|
||||
|
||||
struct speech_rec_notifier recnotifier = {
|
||||
on_result,
|
||||
on_speech_begin,
|
||||
on_speech_end
|
||||
};
|
||||
|
||||
errcode = sr_init(&iat, session_begin_params, SR_MIC, &recnotifier);
|
||||
if (errcode) {
|
||||
printf("speech recognizer init failed\n");
|
||||
return;
|
||||
}
|
||||
errcode = sr_start_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("start listen failed %d\n", errcode);
|
||||
}
|
||||
/* demo 15 seconds recording */
|
||||
while(i++ < 15)
|
||||
sleep(1);
|
||||
errcode = sr_stop_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("stop listening failed %d\n", errcode);
|
||||
}
|
||||
|
||||
sr_uninit(&iat);
|
||||
}
|
||||
|
||||
|
||||
/* main thread: start/stop record ; query the result of recgonization.
|
||||
* record thread: record callback(data write)
|
||||
* helper thread: ui(keystroke detection)
|
||||
*/
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
//FILE *fp = fopen("result.txt","w");
|
||||
int ret = MSP_SUCCESS;
|
||||
int upload_on = 1; /* whether upload the user word */
|
||||
/* login params, please do keep the appid correct */
|
||||
const char* login_params = "appid = 5afd8086, work_dir = .";
|
||||
int aud_src = 0; /* from mic or file */
|
||||
|
||||
/*
|
||||
* See "iFlytek MSC Reference Manual" "sub = iat, domain = iat, language = zh_cn, "
|
||||
*/
|
||||
const char* session_begin_params =
|
||||
"sub = iat, domain = iat, language = en_us, "
|
||||
"accent = mandarin, sample_rate = 16000, "
|
||||
"result_type = plain, result_encoding = utf8";
|
||||
|
||||
/* Login first. the 1st arg is username, the 2nd arg is password
|
||||
* just set them as NULL. the 3rd arg is login paramertes
|
||||
* */
|
||||
ret = MSPLogin(NULL, NULL, login_params);
|
||||
if (MSP_SUCCESS != ret) {
|
||||
printf("MSPLogin failed , Error code %d.\n",ret);
|
||||
goto exit; // login fail, exit the program
|
||||
}
|
||||
|
||||
printf("Want to upload the user words ? \n0: No.\n1: Yes\n");
|
||||
scanf("%d", &upload_on);
|
||||
if (upload_on)
|
||||
{
|
||||
printf("Uploading the user words ...\n");
|
||||
ret = upload_userwords();
|
||||
if (MSP_SUCCESS != ret)
|
||||
goto exit;
|
||||
printf("Uploaded successfully\n");
|
||||
}
|
||||
|
||||
printf("Where the audio comes from?\n"
|
||||
"0: From a audio file.\n1: From microphone.\n");
|
||||
scanf("%d", &aud_src);
|
||||
if(aud_src != 0) {
|
||||
printf("Demo recognizing the speech from microphone\n");
|
||||
printf("Speak in 15 seconds\n");
|
||||
|
||||
demo_mic(session_begin_params);
|
||||
|
||||
printf("15 sec passed\n");
|
||||
}
|
||||
else {
|
||||
//printf("Please type the route of the source audio:\n");
|
||||
|
||||
printf("Demo recgonizing the speech from a recorded audio file\n");
|
||||
demo_file("wav/iflytek02.wav", session_begin_params);
|
||||
}
|
||||
exit:
|
||||
MSPLogout(); // Logout...
|
||||
//fclose(fp);
|
||||
return 0;
|
||||
}
|
|
@ -1,253 +0,0 @@
|
|||
/*
|
||||
* 语音听写(iFly Auto Transform)技术能够实时地将语音转换成对应的文字。
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "qisr.h"
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
#include "speech_recognizer.h"
|
||||
#include <iconv.h>
|
||||
|
||||
#include "ros/ros.h"
|
||||
#include "std_msgs/String.h"
|
||||
|
||||
#define FRAME_LEN 640
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
int flag = 0 ;
|
||||
int flag_ok = 0 ;
|
||||
int flag_no = 0 ;
|
||||
|
||||
|
||||
/* Upload User words */
|
||||
static int upload_userwords()
|
||||
{
|
||||
char* userwords = NULL;
|
||||
size_t len = 0;
|
||||
size_t read_len = 0;
|
||||
FILE* fp = NULL;
|
||||
int ret = -1;
|
||||
|
||||
fp = fopen("userwords.txt", "rb");
|
||||
if (NULL == fp)
|
||||
{
|
||||
printf("\nopen [userwords.txt] failed! \n");
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
len = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
userwords = (char*)malloc(len + 1);
|
||||
if (NULL == userwords)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
read_len = fread((void*)userwords, 1, len, fp);
|
||||
if (read_len != len)
|
||||
{
|
||||
printf("\nread [userwords.txt] failed!\n");
|
||||
goto upload_exit;
|
||||
}
|
||||
userwords[len] = '\0';
|
||||
|
||||
MSPUploadData("userwords", userwords, len, "sub = uup, dtt = userword", &ret); //ÉÏ´«Óû§´Ê±í
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("\nMSPUploadData failed ! errorCode: %d \n", ret);
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
upload_exit:
|
||||
if (NULL != fp)
|
||||
{
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
if (NULL != userwords)
|
||||
{
|
||||
free(userwords);
|
||||
userwords = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static void show_result(char *string, char is_over)
|
||||
{
|
||||
flag_ok=1;
|
||||
printf("\rResult:[ %s ]", string);
|
||||
if(is_over)
|
||||
putchar('\n');
|
||||
|
||||
}
|
||||
|
||||
static char *g_result = NULL;
|
||||
static unsigned int g_buffersize = BUFFER_SIZE;
|
||||
|
||||
void on_result(const char *result, char is_last)
|
||||
{
|
||||
if (result) {
|
||||
size_t left = g_buffersize - 1 - strlen(g_result);
|
||||
size_t size = strlen(result);
|
||||
if (left < size) {
|
||||
g_result = (char*)realloc(g_result, g_buffersize + BUFFER_SIZE);
|
||||
if (g_result)
|
||||
g_buffersize += BUFFER_SIZE;
|
||||
else {
|
||||
printf("mem alloc failed\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
strncat(g_result, result, size);
|
||||
show_result(g_result, is_last);
|
||||
}
|
||||
}
|
||||
void on_speech_begin()
|
||||
{
|
||||
if (g_result)
|
||||
{
|
||||
free(g_result);
|
||||
}
|
||||
g_result = (char*)malloc(BUFFER_SIZE);
|
||||
g_buffersize = BUFFER_SIZE;
|
||||
memset(g_result, 0, g_buffersize);
|
||||
|
||||
printf("Start Listening...\n");
|
||||
}
|
||||
void on_speech_end(int reason)
|
||||
{
|
||||
if (reason == END_REASON_VAD_DETECT)
|
||||
printf("\nSpeaking done \n");
|
||||
else
|
||||
printf("\nRecognizer error %d\n", reason);
|
||||
}
|
||||
|
||||
|
||||
/* demo recognize the audio from microphone */
|
||||
static void demo_mic(const char* session_begin_params)
|
||||
{
|
||||
int errcode;
|
||||
int i = 0;
|
||||
|
||||
struct speech_rec iat;
|
||||
|
||||
struct speech_rec_notifier recnotifier = {
|
||||
on_result,
|
||||
on_speech_begin,
|
||||
on_speech_end
|
||||
};
|
||||
|
||||
errcode = sr_init(&iat, session_begin_params, SR_MIC, &recnotifier);
|
||||
if (errcode) {
|
||||
flag_no=1;
|
||||
printf("speech recognizer init failed\n");
|
||||
return;
|
||||
}
|
||||
errcode = sr_start_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("start listen failed %d\n", errcode);
|
||||
}
|
||||
/* demo 15 seconds recording */
|
||||
while(i++ < 15)
|
||||
sleep(1);
|
||||
errcode = sr_stop_listening(&iat);
|
||||
if (errcode) {
|
||||
flag_no=1;
|
||||
printf("stop listening failed %d\n", errcode);
|
||||
}
|
||||
|
||||
sr_uninit(&iat);
|
||||
}
|
||||
|
||||
|
||||
/* main thread: start/stop record ; query the result of recgonization.
|
||||
* record thread: record callback(data write)
|
||||
* helper thread: ui(keystroke detection)
|
||||
*/
|
||||
|
||||
void WakeUp(const std_msgs::String::ConstPtr& msg)
|
||||
{
|
||||
printf("waking up\r\n");
|
||||
// printf("%s", *msg->data.c_str());
|
||||
usleep(700*1000);
|
||||
flag=1;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
ros::init(argc, argv, "xfspeech");
|
||||
ros::NodeHandle n;
|
||||
ros::Publisher pub = n.advertise<std_msgs::String>("xfspeech", 1000);
|
||||
ros::Rate loop_rate(10);
|
||||
|
||||
ros::Subscriber sbu = n.subscribe("xfwakeup", 1000, WakeUp);
|
||||
ros::Publisher pub1 = n.advertise<std_msgs::String>("xfwords", 1000);
|
||||
ros::Publisher pub2 = n.advertise<std_msgs::String>("xfspeech", 1000);
|
||||
|
||||
int count=0;
|
||||
while(ros::ok())
|
||||
{
|
||||
|
||||
if (flag){
|
||||
|
||||
int ret = MSP_SUCCESS;
|
||||
const char* login_params = "appid = 5afda937, work_dir = .";
|
||||
|
||||
const char* session_begin_params =
|
||||
"sub = iat, domain = iat, language = zh_cn, "
|
||||
"accent = mandarin, sample_rate = 16000, "
|
||||
"result_type = plain, result_encoding = utf8";
|
||||
|
||||
ret = MSPLogin(NULL, NULL, login_params);
|
||||
if(MSP_SUCCESS != ret){
|
||||
MSPLogout();
|
||||
printf("MSPLogin failed , Error code %d.\n",ret);
|
||||
}
|
||||
|
||||
printf("Demo recognizing the speech from microphone\n");
|
||||
printf("Speak in 15 seconds\n");
|
||||
|
||||
demo_mic(session_begin_params);
|
||||
|
||||
printf("15 sec passed\n");
|
||||
|
||||
flag=0;
|
||||
MSPLogout();
|
||||
}
|
||||
|
||||
if(flag_ok){
|
||||
flag_ok=0;
|
||||
std_msgs::String msg;
|
||||
msg.data = g_result;
|
||||
pub2.publish(msg);
|
||||
}
|
||||
|
||||
if(flag_no){
|
||||
flag_no=0;
|
||||
std_msgs::String msg;
|
||||
msg.data = "Sorry Please do it again";
|
||||
pub1.publish(msg);
|
||||
}
|
||||
|
||||
ros::spinOnce();
|
||||
loop_rate.sleep();
|
||||
count++;
|
||||
printf("c:%d \r\n", count);
|
||||
}
|
||||
|
||||
exit:
|
||||
MSPLogout(); // Logout...
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,341 +0,0 @@
|
|||
/*
|
||||
* 语音听写(iFly Auto Transform)技术能够实时地将语音转换成对应的文字。
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "qisr.h"
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
#include "speech_recognizer.h"
|
||||
#include <iconv.h>
|
||||
|
||||
#define FRAME_LEN 640
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
/* Upload User words */
|
||||
static int upload_userwords()
|
||||
{
|
||||
char* userwords = NULL;
|
||||
size_t len = 0;
|
||||
size_t read_len = 0;
|
||||
FILE* fp = NULL;
|
||||
int ret = -1;
|
||||
|
||||
fp = fopen("userwords.txt", "rb");
|
||||
if (NULL == fp)
|
||||
{
|
||||
printf("\nopen [userwords.txt] failed! \n");
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
len = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
userwords = (char*)malloc(len + 1);
|
||||
if (NULL == userwords)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
read_len = fread((void*)userwords, 1, len, fp);
|
||||
if (read_len != len)
|
||||
{
|
||||
printf("\nread [userwords.txt] failed!\n");
|
||||
goto upload_exit;
|
||||
}
|
||||
userwords[len] = '\0';
|
||||
|
||||
MSPUploadData("userwords", userwords, len, "sub = uup, dtt = userword", &ret); //ÉÏ´«Óû§´Ê±í
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("\nMSPUploadData failed ! errorCode: %d \n", ret);
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
upload_exit:
|
||||
if (NULL != fp)
|
||||
{
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
if (NULL != userwords)
|
||||
{
|
||||
free(userwords);
|
||||
userwords = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static void show_result(char *string, char is_over)
|
||||
{
|
||||
char buf[100] = {0};
|
||||
FILE* fd = fopen("result.txt", "w");
|
||||
|
||||
printf("\rResult:[ %s ]", string);
|
||||
fprintf(fd,"%s\n", string);
|
||||
int strPos = 0;
|
||||
int countTimes = 0;
|
||||
for(strPos = 0; strPos < strlen(string) -1;strPos++)
|
||||
{
|
||||
if((string[strPos] == 'h'&& string[strPos+1] == 'a')
|
||||
|| (string[strPos] == 'w'&& string[strPos+1] == 'a'))
|
||||
countTimes ++;
|
||||
if(countTimes == 4)
|
||||
{
|
||||
//system("play ~/alert.mp3");
|
||||
system("rostopic pub /chatter std_msgs/String \"alert\" ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(is_over)
|
||||
putchar('\n');
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
static char *g_result = NULL;
|
||||
static unsigned int g_buffersize = BUFFER_SIZE;
|
||||
|
||||
void on_result(const char *result, char is_last)
|
||||
{
|
||||
if (result) {
|
||||
size_t left = g_buffersize - 1 - strlen(g_result);
|
||||
size_t size = strlen(result);
|
||||
if (left < size) {
|
||||
g_result = (char*)realloc(g_result, g_buffersize + BUFFER_SIZE);
|
||||
if (g_result)
|
||||
g_buffersize += BUFFER_SIZE;
|
||||
else {
|
||||
printf("mem alloc failed\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
strncat(g_result, result, size);
|
||||
show_result(g_result, is_last);
|
||||
}
|
||||
}
|
||||
void on_speech_begin()
|
||||
{
|
||||
if (g_result)
|
||||
{
|
||||
free(g_result);
|
||||
}
|
||||
g_result = (char*)malloc(BUFFER_SIZE);
|
||||
g_buffersize = BUFFER_SIZE;
|
||||
memset(g_result, 0, g_buffersize);
|
||||
|
||||
printf("Start Listening...\n");
|
||||
}
|
||||
void on_speech_end(int reason)
|
||||
{
|
||||
if (reason == END_REASON_VAD_DETECT)
|
||||
printf("\nSpeaking done \n");
|
||||
else
|
||||
printf("\nRecognizer error %d\n", reason);
|
||||
}
|
||||
|
||||
/* demo send audio data from a file */
|
||||
static void demo_file(const char* audio_file, const char* session_begin_params)
|
||||
{
|
||||
int errcode = 0;
|
||||
FILE* f_pcm = NULL;
|
||||
char* p_pcm = NULL;
|
||||
unsigned long pcm_count = 0;
|
||||
unsigned long pcm_size = 0;
|
||||
unsigned long read_size = 0;
|
||||
struct speech_rec iat;
|
||||
struct speech_rec_notifier recnotifier = {
|
||||
on_result,
|
||||
on_speech_begin,
|
||||
on_speech_end
|
||||
};
|
||||
|
||||
if (NULL == audio_file)
|
||||
goto iat_exit;
|
||||
|
||||
f_pcm = fopen(audio_file, "rb");
|
||||
if (NULL == f_pcm)
|
||||
{
|
||||
printf("\nopen [%s] failed! \n", audio_file);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
fseek(f_pcm, 0, SEEK_END);
|
||||
pcm_size = ftell(f_pcm);
|
||||
fseek(f_pcm, 0, SEEK_SET);
|
||||
|
||||
p_pcm = (char *)malloc(pcm_size);
|
||||
if (NULL == p_pcm)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
read_size = fread((void *)p_pcm, 1, pcm_size, f_pcm);
|
||||
if (read_size != pcm_size)
|
||||
{
|
||||
printf("\nread [%s] error!\n", audio_file);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
errcode = sr_init(&iat, session_begin_params, SR_USER, &recnotifier);
|
||||
if (errcode) {
|
||||
printf("speech recognizer init failed : %d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
errcode = sr_start_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("\nsr_start_listening failed! error code:%d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
unsigned int len = 10 * FRAME_LEN; /* 200ms audio */
|
||||
int ret = 0;
|
||||
|
||||
if (pcm_size < 2 * len)
|
||||
len = pcm_size;
|
||||
if (len <= 0)
|
||||
break;
|
||||
|
||||
ret = sr_write_audio_data(&iat, &p_pcm[pcm_count], len);
|
||||
|
||||
if (0 != ret)
|
||||
{
|
||||
printf("\nwrite audio data failed! error code:%d\n", ret);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
pcm_count += (long)len;
|
||||
pcm_size -= (long)len;
|
||||
}
|
||||
|
||||
errcode = sr_stop_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("\nsr_stop_listening failed! error code:%d \n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
iat_exit:
|
||||
if (NULL != f_pcm)
|
||||
{
|
||||
fclose(f_pcm);
|
||||
f_pcm = NULL;
|
||||
}
|
||||
if (NULL != p_pcm)
|
||||
{
|
||||
free(p_pcm);
|
||||
p_pcm = NULL;
|
||||
}
|
||||
|
||||
sr_stop_listening(&iat);
|
||||
sr_uninit(&iat);
|
||||
}
|
||||
|
||||
/* demo recognize the audio from microphone */
|
||||
static void demo_mic(const char* session_begin_params)
|
||||
{
|
||||
int errcode;
|
||||
int i = 0;
|
||||
|
||||
struct speech_rec iat;
|
||||
|
||||
struct speech_rec_notifier recnotifier = {
|
||||
on_result,
|
||||
on_speech_begin,
|
||||
on_speech_end
|
||||
};
|
||||
|
||||
errcode = sr_init(&iat, session_begin_params, SR_MIC, &recnotifier);
|
||||
if (errcode) {
|
||||
printf("speech recognizer init failed\n");
|
||||
return;
|
||||
}
|
||||
errcode = sr_start_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("start listen failed %d\n", errcode);
|
||||
}
|
||||
/* demo 15 seconds recording */
|
||||
while(i++ < 15)
|
||||
sleep(1);
|
||||
errcode = sr_stop_listening(&iat);
|
||||
if (errcode) {
|
||||
printf("stop listening failed %d\n", errcode);
|
||||
}
|
||||
|
||||
sr_uninit(&iat);
|
||||
}
|
||||
|
||||
|
||||
/* main thread: start/stop record ; query the result of recgonization.
|
||||
* record thread: record callback(data write)
|
||||
* helper thread: ui(keystroke detection)
|
||||
*/
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
//FILE *fp = fopen("result.txt","w");
|
||||
int ret = MSP_SUCCESS;
|
||||
int upload_on = 1; /* whether upload the user word */
|
||||
/* login params, please do keep the appid correct */
|
||||
const char* login_params = "appid = 5afd8086, work_dir = .";
|
||||
int aud_src = 0; /* from mic or file */
|
||||
|
||||
/*
|
||||
* See "iFlytek MSC Reference Manual" "sub = iat, domain = iat, language = zh_cn, "
|
||||
*/
|
||||
const char* session_begin_params =
|
||||
"sub = iat, domain = iat, language = en_us, "
|
||||
"accent = mandarin, sample_rate = 16000, "
|
||||
"result_type = plain, result_encoding = utf8";
|
||||
|
||||
/* Login first. the 1st arg is username, the 2nd arg is password
|
||||
* just set them as NULL. the 3rd arg is login paramertes
|
||||
* */
|
||||
ret = MSPLogin(NULL, NULL, login_params);
|
||||
if (MSP_SUCCESS != ret) {
|
||||
printf("MSPLogin failed , Error code %d.\n",ret);
|
||||
goto exit; // login fail, exit the program
|
||||
}
|
||||
|
||||
printf("Want to upload the user words ? \n0: No.\n1: Yes\n");
|
||||
scanf("%d", &upload_on);
|
||||
if (upload_on)
|
||||
{
|
||||
printf("Uploading the user words ...\n");
|
||||
ret = upload_userwords();
|
||||
if (MSP_SUCCESS != ret)
|
||||
goto exit;
|
||||
printf("Uploaded successfully\n");
|
||||
}
|
||||
|
||||
printf("Where the audio comes from?\n"
|
||||
"0: From a audio file.\n1: From microphone.\n");
|
||||
scanf("%d", &aud_src);
|
||||
if(aud_src != 0) {
|
||||
printf("Demo recognizing the speech from microphone\n");
|
||||
printf("Speak in 15 seconds\n");
|
||||
|
||||
demo_mic(session_begin_params);
|
||||
|
||||
printf("15 sec passed\n");
|
||||
}
|
||||
else {
|
||||
//printf("Please type the route of the source audio:\n");
|
||||
|
||||
printf("Demo recgonizing the speech from a recorded audio file\n");
|
||||
demo_file("wav/iflytek02.wav", session_begin_params);
|
||||
}
|
||||
exit:
|
||||
MSPLogout(); // Logout...
|
||||
//fclose(fp);
|
||||
return 0;
|
||||
}
|
|
@ -1,275 +0,0 @@
|
|||
/*
|
||||
* 语音听写(iFly Auto Transform)技术能够实时地将语音转换成对应的文字。
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "qisr.h"
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
#define FRAME_LEN 640
|
||||
#define HINTS_SIZE 100
|
||||
|
||||
/* 上传用户词表 */
|
||||
int upload_userwords()
|
||||
{
|
||||
char* userwords = NULL;
|
||||
unsigned int len = 0;
|
||||
unsigned int read_len = 0;
|
||||
FILE* fp = NULL;
|
||||
int ret = -1;
|
||||
|
||||
fp = fopen("userwords.txt", "rb");
|
||||
if (NULL == fp)
|
||||
{
|
||||
printf("\nopen [userwords.txt] failed! \n");
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
len = ftell(fp); //获取音频文件大小
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
userwords = (char*)malloc(len + 1);
|
||||
if (NULL == userwords)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
read_len = fread((void*)userwords, 1, len, fp); //读取用户词表内容
|
||||
if (read_len != len)
|
||||
{
|
||||
printf("\nread [userwords.txt] failed!\n");
|
||||
goto upload_exit;
|
||||
}
|
||||
userwords[len] = '\0';
|
||||
|
||||
MSPUploadData("userwords", userwords, len, "sub = uup, dtt = userword", &ret); //上传用户词表
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("\nMSPUploadData failed ! errorCode: %d \n", ret);
|
||||
goto upload_exit;
|
||||
}
|
||||
|
||||
upload_exit:
|
||||
if (NULL != fp)
|
||||
{
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
if (NULL != userwords)
|
||||
{
|
||||
free(userwords);
|
||||
userwords = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void run_iat(const char* audio_file, const char* session_begin_params)
|
||||
{
|
||||
const char* session_id = NULL;
|
||||
char rec_result[BUFFER_SIZE] = {NULL};
|
||||
char hints[HINTS_SIZE] = {NULL}; //hints为结束本次会话的原因描述,由用户自定义
|
||||
unsigned int total_len = 0;
|
||||
int aud_stat = MSP_AUDIO_SAMPLE_CONTINUE ; //音频状态
|
||||
int ep_stat = MSP_EP_LOOKING_FOR_SPEECH; //端点检测
|
||||
int rec_stat = MSP_REC_STATUS_SUCCESS ; //识别状态
|
||||
int errcode = MSP_SUCCESS ;
|
||||
|
||||
FILE* f_pcm = NULL;
|
||||
char* p_pcm = NULL;
|
||||
long pcm_count = 0;
|
||||
long pcm_size = 0;
|
||||
long read_size = 0;
|
||||
|
||||
|
||||
if (NULL == audio_file)
|
||||
goto iat_exit;
|
||||
|
||||
f_pcm = fopen(audio_file, "rb");
|
||||
if (NULL == f_pcm)
|
||||
{
|
||||
printf("\nopen [%s] failed! \n", audio_file);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
fseek(f_pcm, 0, SEEK_END);
|
||||
pcm_size = ftell(f_pcm); //获取音频文件大小
|
||||
fseek(f_pcm, 0, SEEK_SET);
|
||||
|
||||
p_pcm = (char *)malloc(pcm_size);
|
||||
if (NULL == p_pcm)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
read_size = fread((void *)p_pcm, 1, pcm_size, f_pcm); //读取音频文件内容
|
||||
if (read_size != pcm_size)
|
||||
{
|
||||
printf("\nread [%s] error!\n", audio_file);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
printf("\n开始语音听写 ...\n");
|
||||
session_id = QISRSessionBegin(NULL, session_begin_params, &errcode); //听写不需要语法,第一个参数为NULL
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRSessionBegin failed! error code:%d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
unsigned int len = 10 * FRAME_LEN; // 每次写入200ms音频(16k,16bit):1帧音频20ms,10帧=200ms。16k采样率的16位音频,一帧的大小为640Byte
|
||||
int ret = 0;
|
||||
|
||||
if (pcm_size < 2 * len)
|
||||
len = pcm_size;
|
||||
if (len <= 0)
|
||||
break;
|
||||
|
||||
aud_stat = MSP_AUDIO_SAMPLE_CONTINUE;
|
||||
if (0 == pcm_count)
|
||||
aud_stat = MSP_AUDIO_SAMPLE_FIRST;
|
||||
|
||||
printf(">");
|
||||
ret = QISRAudioWrite(session_id, (const void *)&p_pcm[pcm_count], len, aud_stat, &ep_stat, &rec_stat);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("\nQISRAudioWrite failed! error code:%d\n", ret);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
pcm_count += (long)len;
|
||||
pcm_size -= (long)len;
|
||||
|
||||
if (MSP_REC_STATUS_SUCCESS == rec_stat) //已经有部分听写结果
|
||||
{
|
||||
const char *rslt = QISRGetResult(session_id, &rec_stat, 0, &errcode);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRGetResult failed! error code: %d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
if (NULL != rslt)
|
||||
{
|
||||
unsigned int rslt_len = strlen(rslt);
|
||||
total_len += rslt_len;
|
||||
if (total_len >= BUFFER_SIZE)
|
||||
{
|
||||
printf("\nno enough buffer for rec_result !\n");
|
||||
goto iat_exit;
|
||||
}
|
||||
strncat(rec_result, rslt, rslt_len);
|
||||
}
|
||||
}
|
||||
|
||||
if (MSP_EP_AFTER_SPEECH == ep_stat)
|
||||
break;
|
||||
usleep(200*1000); //模拟人说话时间间隙。200ms对应10帧的音频
|
||||
}
|
||||
errcode = QISRAudioWrite(session_id, NULL, 0, MSP_AUDIO_SAMPLE_LAST, &ep_stat, &rec_stat);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRAudioWrite failed! error code:%d \n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
while (MSP_REC_STATUS_COMPLETE != rec_stat)
|
||||
{
|
||||
const char *rslt = QISRGetResult(session_id, &rec_stat, 0, &errcode);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRGetResult failed, error code: %d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
if (NULL != rslt)
|
||||
{
|
||||
unsigned int rslt_len = strlen(rslt);
|
||||
total_len += rslt_len;
|
||||
if (total_len >= BUFFER_SIZE)
|
||||
{
|
||||
printf("\nno enough buffer for rec_result !\n");
|
||||
goto iat_exit;
|
||||
}
|
||||
strncat(rec_result, rslt, rslt_len);
|
||||
}
|
||||
usleep(150*1000); //防止频繁占用CPU
|
||||
}
|
||||
printf("\n语音听写结束\n");
|
||||
printf("=============================================================\n");
|
||||
printf("%s\n",rec_result);
|
||||
printf("=============================================================\n");
|
||||
|
||||
iat_exit:
|
||||
if (NULL != f_pcm)
|
||||
{
|
||||
fclose(f_pcm);
|
||||
f_pcm = NULL;
|
||||
}
|
||||
if (NULL != p_pcm)
|
||||
{ free(p_pcm);
|
||||
p_pcm = NULL;
|
||||
}
|
||||
|
||||
QISRSessionEnd(session_id, hints);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ret = MSP_SUCCESS;
|
||||
int upload_on = 1; //是否上传用户词表
|
||||
const char* login_params = "appid = 5afda937, work_dir = ."; // 登录参数,appid与msc库绑定,请勿随意改动
|
||||
|
||||
/*
|
||||
* sub: 请求业务类型
|
||||
* domain: 领域
|
||||
* language: 语言
|
||||
* accent: 方言
|
||||
* sample_rate: 音频采样率
|
||||
* result_type: 识别结果格式
|
||||
* result_encoding: 结果编码格式
|
||||
*
|
||||
* 详细参数说明请参阅《讯飞语音云MSC--API文档》
|
||||
*/
|
||||
const char* session_begin_params = "sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding = utf8";
|
||||
|
||||
/* 用户登录 */
|
||||
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名,第二个参数是密码,均传NULL即可,第三个参数是登录参数
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("MSPLogin failed , Error code %d.\n",ret);
|
||||
goto exit; //登录失败,退出登录
|
||||
}
|
||||
|
||||
printf("\n########################################################################\n");
|
||||
printf("## 语音听写(iFly Auto Transform)技术能够实时地将语音转换成对应的文字。##\n");
|
||||
printf("########################################################################\n\n");
|
||||
printf("演示示例选择:是否上传用户词表?\n0:不使用\n1:使用\n");
|
||||
|
||||
scanf("%d", &upload_on);
|
||||
if (upload_on)
|
||||
{
|
||||
printf("上传用户词表 ...\n");
|
||||
ret = upload_userwords();
|
||||
if (MSP_SUCCESS != ret)
|
||||
goto exit;
|
||||
printf("上传用户词表成功\n");
|
||||
}
|
||||
run_iat("wav/iflytek02.wav", session_begin_params); //iflytek02音频内容为“中美数控”;如果上传了用户词表,识别结果为:“中美速控”。
|
||||
exit:
|
||||
printf("按任意键退出 ...\n");
|
||||
getchar();
|
||||
MSPLogout(); //退出登录
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,204 +0,0 @@
|
|||
/*
|
||||
* 语音语义技术能够将语音听写业务中的内容进行语义解析。
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "qisr.h"
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
#define FRAME_LEN 640
|
||||
#define HINTS_SIZE 100
|
||||
|
||||
void run_iat(const char* audio_file, const char* session_begin_params)
|
||||
{
|
||||
const char* session_id = NULL;
|
||||
char rec_result[BUFFER_SIZE] = {NULL};
|
||||
char hints[HINTS_SIZE] = {NULL}; //hints为结束本次会话的原因描述,由用户自定义
|
||||
unsigned int total_len = 0;
|
||||
int aud_stat = MSP_AUDIO_SAMPLE_CONTINUE ; //音频状态
|
||||
int ep_stat = MSP_EP_LOOKING_FOR_SPEECH; //端点检测
|
||||
int rec_stat = MSP_REC_STATUS_SUCCESS ; //识别状态
|
||||
int errcode = MSP_SUCCESS ;
|
||||
|
||||
FILE* f_pcm = NULL;
|
||||
char* p_pcm = NULL;
|
||||
long pcm_count = 0;
|
||||
long pcm_size = 0;
|
||||
long read_size = 0;
|
||||
|
||||
|
||||
if (NULL == audio_file)
|
||||
goto iat_exit;
|
||||
|
||||
f_pcm = fopen(audio_file, "rb");
|
||||
if (NULL == f_pcm)
|
||||
{
|
||||
printf("\nopen [%s] failed! \n", audio_file);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
fseek(f_pcm, 0, SEEK_END);
|
||||
pcm_size = ftell(f_pcm); //获取音频文件大小
|
||||
fseek(f_pcm, 0, SEEK_SET);
|
||||
|
||||
p_pcm = (char *)malloc(pcm_size);
|
||||
if (NULL == p_pcm)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
read_size = fread((void *)p_pcm, 1, pcm_size, f_pcm); //读取音频文件内容
|
||||
if (read_size != pcm_size)
|
||||
{
|
||||
printf("\nread [%s] error!\n", audio_file);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
session_id = QISRSessionBegin(NULL, session_begin_params, &errcode); //听写不需要语法,第一个参数为NULL
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRSessionBegin failed! error code:%d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
unsigned int len = 10 * FRAME_LEN; // 每次写入200ms音频(16k,16bit):1帧音频20ms,10帧=200ms。16k采样率的16位音频,一帧的大小为640Byte
|
||||
int ret = 0;
|
||||
|
||||
if (pcm_size < 2 * len)
|
||||
len = pcm_size;
|
||||
if (len <= 0)
|
||||
break;
|
||||
|
||||
aud_stat = MSP_AUDIO_SAMPLE_CONTINUE;
|
||||
if (0 == pcm_count)
|
||||
aud_stat = MSP_AUDIO_SAMPLE_FIRST;
|
||||
|
||||
printf(">");
|
||||
ret = QISRAudioWrite(session_id, (const void *)&p_pcm[pcm_count], len, aud_stat, &ep_stat, &rec_stat);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("\nQISRAudioWrite failed! error code:%d\n", ret);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
pcm_count += (long)len;
|
||||
pcm_size -= (long)len;
|
||||
|
||||
if (MSP_REC_STATUS_SUCCESS == rec_stat) //已经有部分听写结果
|
||||
{
|
||||
const char *rslt = QISRGetResult(session_id, &rec_stat, 0, &errcode);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRGetResult failed! error code: %d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
if (NULL != rslt)
|
||||
{
|
||||
unsigned int rslt_len = strlen(rslt);
|
||||
total_len += rslt_len;
|
||||
if (total_len >= BUFFER_SIZE)
|
||||
{
|
||||
printf("\nno enough buffer for rec_result !\n");
|
||||
goto iat_exit;
|
||||
}
|
||||
strncat(rec_result, rslt, rslt_len);
|
||||
}
|
||||
}
|
||||
|
||||
if (MSP_EP_AFTER_SPEECH == ep_stat)
|
||||
break;
|
||||
usleep(200*1000); //模拟人说话时间间隙。200ms对应10帧的音频
|
||||
}
|
||||
errcode = QISRAudioWrite(session_id, NULL, 0, MSP_AUDIO_SAMPLE_LAST, &ep_stat, &rec_stat);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRAudioWrite failed! error code:%d \n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
|
||||
while (MSP_REC_STATUS_COMPLETE != rec_stat)
|
||||
{
|
||||
const char *rslt = QISRGetResult(session_id, &rec_stat, 0, &errcode);
|
||||
if (MSP_SUCCESS != errcode)
|
||||
{
|
||||
printf("\nQISRGetResult failed, error code: %d\n", errcode);
|
||||
goto iat_exit;
|
||||
}
|
||||
if (NULL != rslt)
|
||||
{
|
||||
unsigned int rslt_len = strlen(rslt);
|
||||
total_len += rslt_len;
|
||||
if (total_len >= BUFFER_SIZE)
|
||||
{
|
||||
printf("\nno enough buffer for rec_result !\n");
|
||||
goto iat_exit;
|
||||
}
|
||||
strncat(rec_result, rslt, rslt_len);
|
||||
}
|
||||
usleep(150*1000); //防止频繁占用CPU
|
||||
}
|
||||
printf("\n语音语义解析结束\n");
|
||||
printf("=============================================================\n");
|
||||
printf("%s\n",rec_result);
|
||||
printf("=============================================================\n");
|
||||
|
||||
iat_exit:
|
||||
if (NULL != f_pcm)
|
||||
{
|
||||
fclose(f_pcm);
|
||||
f_pcm = NULL;
|
||||
}
|
||||
if (NULL != p_pcm)
|
||||
{ free(p_pcm);
|
||||
p_pcm = NULL;
|
||||
}
|
||||
|
||||
QISRSessionEnd(session_id, hints);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ret = MSP_SUCCESS;
|
||||
int upload_on = 1; //是否上传用户词表
|
||||
const char* login_params = "appid = 5afda937"; // 登录参数,appid与msc库绑定,请勿随意改动
|
||||
|
||||
|
||||
/*
|
||||
* sub: 请求业务类型
|
||||
* domain: 领域
|
||||
* language: 语言
|
||||
* accent: 方言
|
||||
* sample_rate: 音频采样率
|
||||
* result_type: 识别结果格式
|
||||
* result_encoding: 结果编码格式
|
||||
*
|
||||
* nlp_version: 语义版本
|
||||
* sch: 是否使用语义标识
|
||||
* 详细参数说明请参阅《iFlytek MSC Reference Manual》
|
||||
*/
|
||||
const char* session_begin_params ="nlp_version =2.0,sch=1,sub=iat,domain = iat, language = zh_cn, accent = mandarin,aue = ico, sample_rate = 16000, result_type = plain, result_encoding = gb2312";
|
||||
|
||||
/* 用户登录 */
|
||||
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名,第二个参数是密码,均传NULL即可,第三个参数是登录参数
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("MSPLogin failed , Error code %d.\n",ret);
|
||||
goto exit; //登录失败,退出登录
|
||||
}
|
||||
run_iat("wav/weather.pcm", session_begin_params);
|
||||
exit:
|
||||
printf("按任意键退出 ...\n");
|
||||
getchar();
|
||||
MSPLogout(); //退出登录
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,104 +0,0 @@
|
|||
/*
|
||||
* 文本语义技术能将文本内容进行语义解析。
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
|
||||
#define SOURCETEXT "source.txt" //语义文本资源
|
||||
#define RESULTTEXT "result.txt" //语义结果文本
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const char* login_params = "appid = 5afda937"; // 登录参数,appid与msc库绑定,请勿随意改动
|
||||
const char* rec_text = NULL;
|
||||
unsigned int str_len = 0;
|
||||
int ret = MSP_SUCCESS;
|
||||
FILE* fw = NULL;
|
||||
FILE* fr = NULL;
|
||||
long txtSize = 0;
|
||||
long read_size = 0;
|
||||
char* text = NULL;
|
||||
/* 用户登录 */
|
||||
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名,第二个参数是密码,均传NULL即可,第三个参数是登录参数
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("MSPLogin failed , Error code %d.\n",ret);
|
||||
goto exit; //登录失败,退出登录
|
||||
}
|
||||
fr=fopen(SOURCETEXT,"rb");
|
||||
if(NULL == fr)
|
||||
{
|
||||
printf("\nopen [%s] failed! \n",SOURCETEXT);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fseek(fr, 0, SEEK_END);
|
||||
txtSize = ftell(fr);
|
||||
fseek(fr, 0, SEEK_SET);
|
||||
|
||||
text = (char *)malloc(txtSize+1);
|
||||
if (NULL == text)
|
||||
{
|
||||
printf("\nout of memory! \n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
read_size = fread((void *)text,1, txtSize, fr);
|
||||
if (read_size != txtSize)
|
||||
{
|
||||
printf("\nread [%s] error!\n", SOURCETEXT);
|
||||
goto exit;
|
||||
}
|
||||
text[txtSize]='\0';
|
||||
str_len = strlen(text);
|
||||
|
||||
printf("\n开始语义解析...\n");
|
||||
rec_text = MSPSearch("nlp_version=2.0",text,&str_len,&ret);
|
||||
if(MSP_SUCCESS !=ret)
|
||||
{
|
||||
printf("MSPSearch failed ,error code is:%d\n",ret);
|
||||
goto exit;
|
||||
}
|
||||
printf("\n语义解析完成!\n");
|
||||
fw=fopen(RESULTTEXT,"wb");
|
||||
if(NULL == fw)
|
||||
{
|
||||
printf("\nopen [%s] failed! \n",RESULTTEXT);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
read_size = fwrite(rec_text,1,str_len,fw);
|
||||
if(read_size != str_len)
|
||||
{
|
||||
printf("\nwrite [%s] error!\n", RESULTTEXT);
|
||||
goto exit;
|
||||
}
|
||||
printf("\n语义解析结果已写入%s文件\n",RESULTTEXT);
|
||||
exit:
|
||||
if (NULL != fr)
|
||||
{
|
||||
fclose(fr);
|
||||
fr = NULL;
|
||||
}
|
||||
if (NULL != fw)
|
||||
{
|
||||
fclose(fw);
|
||||
fw = NULL;
|
||||
}
|
||||
if (NULL != text)
|
||||
{
|
||||
free(text);
|
||||
text = NULL;
|
||||
}
|
||||
printf("\n按任意键退出 ...\n");
|
||||
getchar();
|
||||
MSPLogout(); //退出登录
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,183 +0,0 @@
|
|||
/*
|
||||
* 语音合成(Text To Speech,TTS)技术能够自动将任意文字实时转换为连续的
|
||||
* 自然语音,是一种能够在任何时间、任何地点,向任何人提供语音信息服务的
|
||||
* 高效便捷手段,非常符合信息时代海量数据、动态更新和个性化查询的需求。
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "qtts.h"
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
|
||||
/* wav音频头部格式 */
|
||||
typedef struct _wave_pcm_hdr
|
||||
{
|
||||
char riff[4]; // = "RIFF"
|
||||
int size_8; // = FileSize - 8
|
||||
char wave[4]; // = "WAVE"
|
||||
char fmt[4]; // = "fmt "
|
||||
int fmt_size; // = 下一个结构体的大小 : 16
|
||||
|
||||
short int format_tag; // = PCM : 1
|
||||
short int channels; // = 通道数 : 1
|
||||
int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000
|
||||
int avg_bytes_per_sec; // = 每秒字节数 : samples_per_sec * bits_per_sample / 8
|
||||
short int block_align; // = 每采样点字节数 : wBitsPerSample / 8
|
||||
short int bits_per_sample; // = 量化比特数: 8 | 16
|
||||
|
||||
char data[4]; // = "data";
|
||||
int data_size; // = 纯数据长度 : FileSize - 44
|
||||
} wave_pcm_hdr;
|
||||
|
||||
/* 默认wav音频头部数据 */
|
||||
wave_pcm_hdr default_wav_hdr =
|
||||
{
|
||||
{ 'R', 'I', 'F', 'F' },
|
||||
0,
|
||||
{'W', 'A', 'V', 'E'},
|
||||
{'f', 'm', 't', ' '},
|
||||
16,
|
||||
1,
|
||||
1,
|
||||
16000,
|
||||
32000,
|
||||
2,
|
||||
16,
|
||||
{'d', 'a', 't', 'a'},
|
||||
0
|
||||
};
|
||||
/* 文本合成 */
|
||||
int text_to_speech(const char* src_text, const char* des_path, const char* params)
|
||||
{
|
||||
int ret = -1;
|
||||
FILE* fp = NULL;
|
||||
const char* sessionID = NULL;
|
||||
unsigned int audio_len = 0;
|
||||
wave_pcm_hdr wav_hdr = default_wav_hdr;
|
||||
int synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA;
|
||||
|
||||
if (NULL == src_text || NULL == des_path)
|
||||
{
|
||||
printf("params is error!\n");
|
||||
return ret;
|
||||
}
|
||||
fp = fopen(des_path, "wb");
|
||||
if (NULL == fp)
|
||||
{
|
||||
printf("open %s error.\n", des_path);
|
||||
return ret;
|
||||
}
|
||||
/* 开始合成 */
|
||||
sessionID = QTTSSessionBegin(params, &ret);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("QTTSSessionBegin failed, error code: %d.\n", ret);
|
||||
fclose(fp);
|
||||
return ret;
|
||||
}
|
||||
ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("QTTSTextPut failed, error code: %d.\n",ret);
|
||||
QTTSSessionEnd(sessionID, "TextPutError");
|
||||
fclose(fp);
|
||||
return ret;
|
||||
}
|
||||
printf("正在合成 ...\n");
|
||||
fwrite(&wav_hdr, sizeof(wav_hdr) ,1, fp); //添加wav音频头,使用采样率为16000
|
||||
while (1)
|
||||
{
|
||||
/* 获取合成音频 */
|
||||
const void* data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
|
||||
if (MSP_SUCCESS != ret)
|
||||
break;
|
||||
if (NULL != data)
|
||||
{
|
||||
fwrite(data, audio_len, 1, fp);
|
||||
wav_hdr.data_size += audio_len; //计算data_size大小
|
||||
}
|
||||
if (MSP_TTS_FLAG_DATA_END == synth_status)
|
||||
break;
|
||||
printf(">");
|
||||
usleep(150*1000); //防止频繁占用CPU
|
||||
}//合成状态synth_status取值请参阅《讯飞语音云API文档》
|
||||
printf("\n");
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("QTTSAudioGet failed, error code: %d.\n",ret);
|
||||
QTTSSessionEnd(sessionID, "AudioGetError");
|
||||
fclose(fp);
|
||||
return ret;
|
||||
}
|
||||
/* 修正wav文件头数据的大小 */
|
||||
wav_hdr.size_8 += wav_hdr.data_size + (sizeof(wav_hdr) - 8);
|
||||
|
||||
/* 将修正过的数据写回文件头部,音频文件为wav格式 */
|
||||
fseek(fp, 4, 0);
|
||||
fwrite(&wav_hdr.size_8,sizeof(wav_hdr.size_8), 1, fp); //写入size_8的值
|
||||
fseek(fp, 40, 0); //将文件指针偏移到存储data_size值的位置
|
||||
fwrite(&wav_hdr.data_size,sizeof(wav_hdr.data_size), 1, fp); //写入data_size的值
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
/* 合成完毕 */
|
||||
ret = QTTSSessionEnd(sessionID, "Normal");
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("QTTSSessionEnd failed, error code: %d.\n",ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ret = MSP_SUCCESS;
|
||||
const char* login_params = "appid = 5afda937, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动
|
||||
/*
|
||||
* rdn: 合成音频数字发音方式
|
||||
* volume: 合成音频的音量
|
||||
* pitch: 合成音频的音调
|
||||
* speed: 合成音频对应的语速
|
||||
* voice_name: 合成发音人
|
||||
* sample_rate: 合成音频采样率
|
||||
* text_encoding: 合成文本编码格式
|
||||
*
|
||||
* 详细参数说明请参阅《讯飞语音云MSC--API文档》
|
||||
*/
|
||||
const char* session_begin_params = "voice_name = xiaoyan, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";
|
||||
const char* filename = "tts_sample.wav"; //合成的语音文件名称
|
||||
const char* text = "亲爱的用户,您好,这是一个语音合成示例,感谢您对科大讯飞语音技术的支持!科大讯飞是亚太地区最大的语音上市公司,股票代码:002230"; //合成文本
|
||||
|
||||
/* 用户登录 */
|
||||
ret = MSPLogin(NULL, NULL, login_params);//第一个参数是用户名,第二个参数是密码,第三个参数是登录参数,用户名和密码可在http://open.voicecloud.cn注册获取
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("MSPLogin failed, error code: %d.\n", ret);
|
||||
goto exit ;//登录失败,退出登录
|
||||
}
|
||||
printf("\n###########################################################################\n");
|
||||
printf("## 语音合成(Text To Speech,TTS)技术能够自动将任意文字实时转换为连续的 ##\n");
|
||||
printf("## 自然语音,是一种能够在任何时间、任何地点,向任何人提供语音信息服务的 ##\n");
|
||||
printf("## 高效便捷手段,非常符合信息时代海量数据、动态更新和个性化查询的需求。 ##\n");
|
||||
printf("###########################################################################\n\n");
|
||||
/* 文本合成 */
|
||||
printf("开始合成 ...\n");
|
||||
ret = text_to_speech(text, filename, session_begin_params);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("text_to_speech failed, error code: %d.\n", ret);
|
||||
}
|
||||
printf("合成完毕\n");
|
||||
|
||||
exit:
|
||||
printf("按任意键退出 ...\n");
|
||||
getchar();
|
||||
MSPLogout(); //退出登录
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,234 +0,0 @@
|
|||
/*
|
||||
* 语音合成(Text To Speech,TTS)技术能够自动将任意文字实时转换为连续的
|
||||
* 自然语音,是一种能够在任何时间、任何地点,向任何人提供语音信息服务的
|
||||
* 高效便捷手段,非常符合信息时代海量数据、动态更新和个性化查询的需求。
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
#include "qtts.h"
|
||||
#include "msp_cmn.h"
|
||||
#include "msp_errors.h"
|
||||
|
||||
#include "ros/ros.h"
|
||||
#include "std_msgs/String.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
/* wav音频头部格式 */
|
||||
typedef struct _wave_pcm_hdr
|
||||
{
|
||||
char riff[4]; // = "RIFF"
|
||||
int size_8; // = FileSize - 8
|
||||
char wave[4]; // = "WAVE"
|
||||
char fmt[4]; // = "fmt "
|
||||
int fmt_size; // = 下一个结构体的大小 : 16
|
||||
|
||||
short int format_tag; // = PCM : 1
|
||||
short int channels; // = 通道数 : 1
|
||||
int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000
|
||||
int avg_bytes_per_sec; // = 每秒字节数 : samples_per_sec * bits_per_sample / 8
|
||||
short int block_align; // = 每采样点字节数 : wBitsPerSample / 8
|
||||
short int bits_per_sample; // = 量化比特数: 8 | 16
|
||||
|
||||
char data[4]; // = "data";
|
||||
int data_size; // = 纯数据长度 : FileSize - 44
|
||||
} wave_pcm_hdr;
|
||||
|
||||
/* 默认wav音频头部数据 */
|
||||
wave_pcm_hdr default_wav_hdr =
|
||||
{
|
||||
{ 'R', 'I', 'F', 'F' },
|
||||
0,
|
||||
{'W', 'A', 'V', 'E'},
|
||||
{'f', 'm', 't', ' '},
|
||||
16,
|
||||
1,
|
||||
1,
|
||||
16000,
|
||||
32000,
|
||||
2,
|
||||
16,
|
||||
{'d', 'a', 't', 'a'},
|
||||
0
|
||||
};
|
||||
/* 文本合成 */
|
||||
int text_to_speech(const char* src_text, const char* des_path, const char* params)
|
||||
{
|
||||
int ret = -1;
|
||||
FILE* fp = NULL;
|
||||
const char* sessionID = NULL;
|
||||
unsigned int audio_len = 0;
|
||||
wave_pcm_hdr wav_hdr = default_wav_hdr;
|
||||
int synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA;
|
||||
|
||||
if (NULL == src_text || NULL == des_path)
|
||||
{
|
||||
printf("params is error!\n");
|
||||
return ret;
|
||||
}
|
||||
fp = fopen(des_path, "wb");
|
||||
if (NULL == fp)
|
||||
{
|
||||
printf("open %s error.\n", des_path);
|
||||
return ret;
|
||||
}
|
||||
/* 开始合成 */
|
||||
sessionID = QTTSSessionBegin(params, &ret);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("QTTSSessionBegin failed, error code: %d.\n", ret);
|
||||
fclose(fp);
|
||||
return ret;
|
||||
}
|
||||
ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("QTTSTextPut failed, error code: %d.\n",ret);
|
||||
QTTSSessionEnd(sessionID, "TextPutError");
|
||||
fclose(fp);
|
||||
return ret;
|
||||
}
|
||||
printf("正在合成 ...\n");
|
||||
fwrite(&wav_hdr, sizeof(wav_hdr) ,1, fp); //添加wav音频头,使用采样率为16000
|
||||
while (1)
|
||||
{
|
||||
/* 获取合成音频 */
|
||||
const void* data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
|
||||
if (MSP_SUCCESS != ret)
|
||||
break;
|
||||
if (NULL != data)
|
||||
{
|
||||
fwrite(data, audio_len, 1, fp);
|
||||
wav_hdr.data_size += audio_len; //计算data_size大小
|
||||
}
|
||||
if (MSP_TTS_FLAG_DATA_END == synth_status)
|
||||
break;
|
||||
printf(">");
|
||||
usleep(150*1000); //防止频繁占用CPU
|
||||
}//合成状态synth_status取值请参阅《讯飞语音云API文档》
|
||||
printf("\n");
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("QTTSAudioGet failed, error code: %d.\n",ret);
|
||||
QTTSSessionEnd(sessionID, "AudioGetError");
|
||||
fclose(fp);
|
||||
return ret;
|
||||
}
|
||||
/* 修正wav文件头数据的大小 */
|
||||
wav_hdr.size_8 += wav_hdr.data_size + (sizeof(wav_hdr) - 8);
|
||||
|
||||
/* 将修正过的数据写回文件头部,音频文件为wav格式 */
|
||||
fseek(fp, 4, 0);
|
||||
fwrite(&wav_hdr.size_8,sizeof(wav_hdr.size_8), 1, fp); //写入size_8的值
|
||||
fseek(fp, 40, 0); //将文件指针偏移到存储data_size值的位置
|
||||
fwrite(&wav_hdr.data_size,sizeof(wav_hdr.data_size), 1, fp); //写入data_size的值
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
/* 合成完毕 */
|
||||
ret = QTTSSessionEnd(sessionID, "Normal");
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("QTTSSessionEnd failed, error code: %d.\n",ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void xfcallback(const std_msgs::String::ConstPtr& msg)
|
||||
{
|
||||
char cmd[2000];
|
||||
const char* text;
|
||||
int ret = MSP_SUCCESS;
|
||||
const char* session_begin_params = "voice_name = xiaoyan, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";
|
||||
const char* filename = "tts_sample.wav"; //合成的语音文件名称
|
||||
|
||||
|
||||
std::cout<<"I heard :"<<msg->data.c_str()<<std::endl;
|
||||
text = msg->data.c_str();
|
||||
/* 文本合成 */
|
||||
printf("开始合成 ...\n");
|
||||
ret = text_to_speech(text, filename, session_begin_params);
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("text_to_speech failed, error code: %d.\n", ret);
|
||||
}
|
||||
printf("合成完毕\n");
|
||||
|
||||
|
||||
unlink("/tmp/cmd");
|
||||
mkfifo("/tmp/cmd", 0777);
|
||||
popen("mplayer -quiet -slave -input file=/tmp/cmd 'tts_sample.wav'","r");
|
||||
sleep(30);
|
||||
printf("Mplayer Run Success\n");
|
||||
|
||||
}
|
||||
|
||||
void toPlay()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void toExit()
|
||||
{
|
||||
printf("按任意键退出 ...\n");
|
||||
getchar();
|
||||
MSPLogout(); //退出登录
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ret = MSP_SUCCESS;
|
||||
const char* login_params = "appid = 5afda937, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动
|
||||
/*
|
||||
* rdn: 合成音频数字发音方式
|
||||
* volume: 合成音频的音量
|
||||
* pitch: 合成音频的音调
|
||||
* speed: 合成音频对应的语速
|
||||
* voice_name: 合成发音人
|
||||
* sample_rate: 合成音频采样率
|
||||
* text_encoding: 合成文本编码格式
|
||||
*
|
||||
* 详细参数说明请参阅《讯飞语音云MSC--API文档》
|
||||
*/
|
||||
|
||||
/* 用户登录 */
|
||||
ret = MSPLogin(NULL, NULL, login_params);//第一个参数是用户名,第二个参数是密码,第三个参数是登录参数,用户名和密码可在http://open.voicecloud.cn注册获取
|
||||
if (MSP_SUCCESS != ret)
|
||||
{
|
||||
printf("MSPLogin failed, error code: %d.\n", ret);
|
||||
/*goto exit ;*///登录失败,退出登录
|
||||
toExit();
|
||||
}
|
||||
printf("\n###########################################################################\n");
|
||||
printf("## 语音合成(Text To Speech,TTS)技术能够自动将任意文字实时转换为连续的 ##\n");
|
||||
printf("## 自然语音,是一种能够在任何时间、任何地点,向任何人提供语音信息服务的 ##\n");
|
||||
printf("## 高效便捷手段,非常符合信息时代海量数据、动态更新和个性化查询的需求。 ##\n");
|
||||
printf("###########################################################################\n\n");
|
||||
|
||||
|
||||
ros::init(argc,argv,"xf_tts");
|
||||
ros::NodeHandle n;
|
||||
ros::Subscriber sub =n.subscribe("xfwords",1000,xfcallback);
|
||||
ros::spin();
|
||||
|
||||
|
||||
|
||||
exit:
|
||||
printf("按任意键退出 ...\n");
|
||||
getchar();
|
||||
MSPLogout(); //退出登录
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue