100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > ROS语音交互——科大讯飞语音合成TTS(二)

ROS语音交互——科大讯飞语音合成TTS(二)

时间:2021-08-21 16:31:02

相关推荐

ROS语音交互——科大讯飞语音合成TTS(二)

之前我用过科大讯飞的语音包,为了记录一下我重新使用一下

首先注册科大讯飞账号及应用,以后每个下载的在线使用SDK都是以此账户ID登录讯飞语音服务器。

下载科大讯飞在线合成包。

$ unzip Linux_voice_1135_57f49f64\ \(1\).zip

文件目录示意图

.├── bin│ ├── gm_continuous_digit.abnf│ ├── msc│ │ └── msc.cfg│ ├── source.txt│ ├── userwords.txt│ └── wav│├── iflytek01.wav│├── iflytek02.wav│└── weather.pcm├── doc│ ├── Grammar Development Guidelines.chm│ ├── iFlytek MSC Reference Manual│ │ ├── all__0_8js.html│ │ ├── all__1_8js.html│ │ ├── arrowdown.png│ │ ├── arrowright.png│ │ ├── bc_s.png│ │ ├── bdwn.png│ │ ├── closed.png│ │ ├── dir_25539194184bab781b1c7ecd67774cd8.html│ │ ├── dir_9a65024ac0a05d9abfc2bb6c7ff8f818.html│ │ ├── doc.png│ │ ├── doxygen.css│ │ ├── doxygen.png│ │ ├── dynsections_8js.html│ │ ├── dynsections.js│ │ ├── files__0_8js.html│ │ ├── files__1_8js.html│ │ ├── files.html│ │ ├── folderclosed.png│ │ ├── folderopen.png│ │ ├── functions__0_8js.html│ │ ├── functions__1_8js.html│ │ ├── globals_func.html│ │ ├── globals.html│ │ ├── globals_vars.html│ │ ├── index.html│ │ ├── jquery_8js.html│ │ ├── jquery.js│ │ ├── msp__cmn_8h.html│ │ ├── msp__cmn_8h_source.html│ │ ├── nav_f.png│ │ ├── nav_g.png│ │ ├── nav_h.png│ │ ├── open.png│ │ ├── qisr_8h.html│ │ ├── qisr_8h_source.html│ │ ├── qmfv_8h.html│ │ ├── qmfv_8h_source.html│ │ ├── qtts_8h.html│ │ ├── qtts_8h_source.html│ │ ├── search│ │ │ ├── all_0.html│ │ │ ├── all_0.js│ │ │ ├── all_10.html│ │ │ ├── all_10.js│ │ │ ├── all_11.html│ │ │ ├── all_11.js│ │ │ ├── all_1.html│ │ │ ├── all_1.js│ │ │ ├── all_2.html│ │ │ ├── all_2.js│ │ │ ├── all_3.html│ │ │ ├── all_3.js│ │ │ ├── all_4.html│ │ │ ├── all_4.js│ │ │ ├── all_5.html│ │ │ ├── all_5.js│ │ │ ├── all_6.html│ │ │ ├── all_6.js│ │ │ ├── all_7.html│ │ │ ├── all_7.js│ │ │ ├── all_8.html│ │ │ ├── all_8.js│ │ │ ├── all_9.html│ │ │ ├── all_9.js│ │ │ ├── all_a.html│ │ │ ├── all_a.js│ │ │ ├── all_b.html│ │ │ ├── all_b.js│ │ │ ├── all_c.html│ │ │ ├── all_c.js│ │ │ ├── all_d.html│ │ │ ├── all_d.js│ │ │ ├── all_e.html│ │ │ ├── all_e.js│ │ │ ├── all_f.html│ │ │ ├── all_f.js│ │ │ ├── close.png│ │ │ ├── files_0.html│ │ │ ├── files_0.js│ │ │ ├── files_1.html│ │ │ ├── files_1.js│ │ │ ├── files_2.html│ │ │ ├── files_2.js│ │ │ ├── files_3.html│ │ │ ├── files_3.js│ │ │ ├── files_4.html│ │ │ ├── files_4.js│ │ │ ├── files_5.html│ │ │ ├── files_5.js│ │ │ ├── files_6.html│ │ │ ├── files_6.js│ │ │ ├── functions_0.html│ │ │ ├── functions_0.js│ │ │ ├── functions_1.html│ │ │ ├── functions_1.js│ │ │ ├── functions_2.html│ │ │ ├── functions_2.js│ │ │ ├── functions_3.html│ │ │ ├── functions_3.js│ │ │ ├── functions_4.html│ │ │ ├── functions_4.js│ │ │ ├── functions_5.html│ │ │ ├── functions_5.js│ │ │ ├── functions_6.html│ │ │ ├── functions_6.js│ │ │ ├── functions_7.html│ │ │ ├── functions_7.js│ │ │ ├── functions_8.html│ │ │ ├── functions_8.js│ │ │ ├── functions_9.html│ │ │ ├── functions_9.js│ │ │ ├── functions_a.html│ │ │ ├── functions_a.js│ │ │ ├── mag_sel.png│ │ │ ├── nomatches.html│ │ │ ├── search.css│ │ │ ├── searchdata.js│ │ │ ├── search.js│ │ │ ├── search_l.png│ │ │ ├── search_m.png│ │ │ ├── search_r.png│ │ │ ├── variables_0.html│ │ │ ├── variables_0.js│ │ │ ├── variables_1.html│ │ │ ├── variables_1.js│ │ │ ├── variables_2.html│ │ │ ├── variables_2.js│ │ │ ├── variables_3.html│ │ │ ├── variables_3.js│ │ │ ├── variables_4.html│ │ │ ├── variables_4.js│ │ │ ├── variables_5.html│ │ │ ├── variables_5.js│ │ │ ├── variables_6.html│ │ │ ├── variables_6.js│ │ │ ├── variables_7.html│ │ │ └── variables_7.js│ │ ├── search_8js.html│ │ ├── searchdata_8js.html│ │ ├── splitbar.png│ │ ├── sync_off.png│ │ ├── sync_on.png│ │ ├── tab_a.png│ │ ├── tab_b.png│ │ ├── tab_h.png│ │ ├── tabs.css│ │ └── tab_s.png│ ├── MSC Novice Manual for Linux.pdf│ ├── Open Semantic Platform API Documents.pdf│ └── Recording API Documents_.doc├── include│ ├── msp_cmn.h│ ├── msp_errors.h│ ├── msp_types.h│ ├── qisr.h│ └── qtts.h├── libs│ ├── x64│ │ └── libmsc.so│ └── x86│└── libmsc.so├── Linux_voice_1135_57f49f64 (1).zip├── README└── samples├── asr_sample│ ├── 32bit_make.sh│ ├── 64bit_make.sh│ ├── asr_sample.c│ └── Makefile├── iat_record│ ├── 32bit_make.sh│ ├── 64bit_make.sh│ ├── formats.h│ ├── iat_record.c│ ├── linuxrec.c│ ├── linuxrec.h│ ├── Makefile│ ├── speech_recognizer.c│ └── speech_recognizer.h├── iat_sample│ ├── 32bit_make.sh│ ├── 64bit_make.sh│ ├── iat_sample.c│ └── Makefile├── sch_speak│ ├── 32bit_make.sh│ ├── 64bit_make.sh│ ├── Makefile│ └── sch_speak.c├── sch_text│ ├── 32bit_make.sh│ ├── 64bit_make.sh│ ├── Makefile│ └── sch_text.c└── tts_sample├── 32bit_make.sh├── 64bit_make.sh├── Makefile└── tts_sample.c

我们编译使用tts_sample目录下的源文件

编译方法依照讯飞文档

cam@cam-Aspire-V3-572G:~/xunfei/TTS/samples/tts_sample$ source 64bit_make.sh

将64bit_make.sh拷贝到bin目录,执行程序时会用到动态链接库

cam@cam-Aspire-V3-572G:~/xunfei/TTS/samples/tts_sample$ cp 64bit_make.sh ../../bin/

运行程序

cam@cam-Aspire-V3-572G:~/xunfei/TTS/bin$ ./tts_sample

会在当前目录合成一个声音文件。

cam@cam-Aspire-V3-572G:~/xunfei/TTS/bin$ play tts_sample.wav

如果没有安装sox,执行安装命令

sudo apt-get install sox

由于官方的需要选择系统平台,我们都是确定的,就不用那么麻烦的 每次更新环境

修改makfile,删除判断语句

#common makefile headerDIR_INC = ../../includeDIR_BIN = ../../binDIR_LIB = ../../libsTARGET= tts_sampleBIN_TARGET = $(DIR_BIN)/$(TARGET)CROSS_COMPILE = CFLAGS = -g -Wall -I$(DIR_INC)LDFLAGS := -L$(DIR_LIB)/x64LDFLAGS += -lmsc -lrt -ldl -lpthreadOBJECTS := $(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

├── libs │

├── x64

│ │ └── libmsc.so

libmsc.so拷贝到/user/libs下 即可。

以后直接make

源文件

/** 语音合成(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 - 8char wave[4];// = "WAVE"char fmt[4]; // = "fmt "int fmt_size; // = 下一个结构体的大小 : 16short int format_tag; // = PCM : 1short int channels;// = 通道数 : 1int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000int avg_bytes_per_sec;// = 每秒字节数 : samples_per_sec * bits_per_sample / 8short int block_align; // = 每采样点字节数 : wBitsPerSample / 8short int bits_per_sample; // = 量化比特数: 8 | 16char 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){intret= -1;FILE* fp = NULL;const char* sessionID = NULL;unsigned int audio_len = 0;wave_pcm_hdr wav_hdr= default_wav_hdr;intsynth_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音频头,使用采样率为16000while (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 = 57f49f64, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动/** rdn: 合成音频数字发音方式* volume: 合成音频的音量* pitch: 合成音频的音调* speed: 合成音频对应的语速* voice_name: 合成发音人* sample_rate: 合成音频采样率* text_encoding: 合成文本编码格式** 详细参数说明请参阅《讯飞语音云MSC--API文档》*/const char* session_begin_params = "voice_name = xiaowanzi, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 0";const char* filename = "tts_sample.wav"; //合成的语音文件名称const char* text = "我是小丸子,今年126岁了,我的车牌号是12365,今年是12月20号"; //合成文本/* 用户登录 */ret = MSPLogin(NULL, NULL, login_params);//第一个参数是用户名,第二个参数是密码,第三个参数是登录参数,用户名和密码可在注册获取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;}

const char* session_begin_params = "voice_name = xiaowanzi, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 0";

wav文件头的格式

/* wav音频头部格式 */typedef struct _wave_pcm_hdr{char riff[4];// = "RIFF"int size_8; // = FileSize - 8char wave[4];// = "WAVE"char fmt[4]; // = "fmt "int fmt_size; // = 下一个结构体的大小 : 16short int format_tag; // = PCM : 1short int channels;// = 通道数 : 1int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000int avg_bytes_per_sec;// = 每秒字节数 : samples_per_sec * bits_per_sample / 8short int block_align; // = 每采样点字节数 : wBitsPerSample / 8short int bits_per_sample; // = 量化比特数: 8 | 16char 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};

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。