概述
首先到讯飞上注册一个帐号(感觉讯飞官网和阿里云官网的差不多)
随后创建一个新应用(Windows平台下)
最后我们要的是Appid如下图所示
下面是讯飞SDK(含说明文档)下载地址:
链接:http://pan.baidu.com/s/1i45xEo1 密码:m8il
因为此程序涉及WAV语音知识,未了解WAV的朋友请百度补充,或者在下面这个链接初略的学习下
http://blog.csdn.net/qq78442761/article/details/53385561
下面先来认识下几个API(从文档提取出来)
1.int MSPAPI MSPLogin (const char *usr, const char *pwd, const char *params) //初始化msc,用户登录。
usr[in] 此参数保留,传入NULL即可。
pwd[in] 此参数保留,传入NULL即可。
官方例子
const char* usr = NULL;
const char* pwd = NULL;
const char* lgi_param = "appid = ********";
int ret = MSPLogin(usr, pwd, lgi_param);
if( MSP_SUCCESS != ret )
{
printf( "MSPLogin failed, error code is: %d", ret );
}
2.const char *MSPAPI QTTSSessionBegin (const char *params, int *errorCode) //开始一次语音合成,分配语音合成资源。
(信息有点多,参数具体见文档qtts.h 文件参考)
下面是官方例子
const char * ssb_param = "voice_name = xiaoyan, aue = speex-wb;7, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";
int ret = -1;
const char * sessionID = QTTSSessionBegin( ssb_param, &ret );
if( MSP_SUCCESS != ret )
{
printf( “QTTSSessionBegin failed, error code is: %d”, ret );
}
(信息有点多,参数具体见文档qtts.h 文件参考)
例子
FILE* fp = fopen("tts.pcm", "wb");
while (1)
{
const void * data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
if (NULL != data)
{
fwrite(data, audio_len, 1, fp);
}
if (MSP_TTS_FLAG_DATA_END == synth_status || MSP_SUCCESS != ret)
{
break;
}
}
fclose(fp);
int ret = QTTSSessionEnd ( sessionID, “normal end” );
if( MSP_SUCCESS != ret )
{
printf( “QTTSSessionEnd failed, error code is: %d”, ret );
}
下面是完整项目的代码:
几个个文件1.stdafx.h。2.targetver.h。3.QTTSDemo.cpp。4.stdafx.cpp。
除了QTTSDemo.cpp外,其他文件都是vs2013创建控制台程序自带的,在此只给出QTTSDemo.cpp代码
但注意在stdafx.h中加入#define _CRT_SECURE_NO_WARNINGS
QTTSDemo.cpp
// QTTSDemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <msp_cmn.h>
#include <msp_errors.h>
#include <qtts.h>
#include <string.h>
#include <windows.h>
#pragma comment(lib,"WinMM.lib")
#ifdef _WIN64
#pragma comment(lib,"msc_x64.lib")
#else
#pragma comment(lib,"msc.lib")
#endif
//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|16000
int avg_bytes_per_sec; //=每秒字节数:samples_per_sec*bit_per_sample
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 _tmain(int argc, _TCHAR* argv[])
{
//登录
const char* usr = NULL;
const char* pwd = NULL;
const char* lgi_param = "appid = 583aea17";
int ret = MSPLogin(usr, pwd, lgi_param);
if (MSP_SUCCESS != ret)
{
printf("MSPLogin failed, error code is: %d", ret);
}
//开始合成
const char * ssb_param = "voice_name = xiaorong, aue = speex-wb;7, sample_rate = 16000, speed = 50, volume = 80, pitch = 50, rdn = 2";
ret = -1;
const char * sessionID = QTTSSessionBegin(ssb_param, &ret);
if (MSP_SUCCESS != ret)
{
printf("QTTSSessionBegin failed, error code is : %d", ret);
}
//设置待合成文本
char src_text[1024];
printf("请输入一段文字(中文注意逗号,句话):n");
gets(src_text);
unsigned int text_len = strlen(src_text); //textLen参数为合成文本所占字节数
ret = QTTSTextPut(sessionID, src_text, text_len, NULL);
if (MSP_SUCCESS != ret)
{
printf("QTTSTextPut failed, error code is : %d", ret);
}
//获取合成的音频wav
FILE* fp = fopen("Demo.wav", "wb"); //一定是二进制模式
fwrite(&default_wav_hdr, sizeof(default_wav_hdr), 1, fp);
unsigned int audio_len = 0;
int synth_status = 0;
while (1)
{
const void * data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
if (NULL != data)
{
fwrite(data, audio_len, 1, fp);
default_wav_hdr.data_size += audio_len;
}
if (MSP_TTS_FLAG_DATA_END == synth_status || MSP_SUCCESS != ret)
{
break;
}
}
default_wav_hdr.size_8 += default_wav_hdr.data_size + (sizeof(default_wav_hdr)-8);
fseek(fp, 4, 0);
fwrite(&default_wav_hdr.size_8, sizeof(default_wav_hdr.size_8), 1, fp); //写入size_8的值
fseek(fp, 40, 0); //将文件指针偏移到存储data_size值的位置
fwrite(&default_wav_hdr.data_size, sizeof(default_wav_hdr.data_size), 1, fp);//写入data_size的值
fclose(fp);
PlaySoundA("Demo.wav", NULL, SND_ASYNC);
ret = QTTSSessionEnd(sessionID, "normal end");
if (MSP_SUCCESS != ret)
{
printf("QTTSSessionEnd failed, error code is : %d", ret);
}
//退出
ret = MSPLogout();
if (MSP_SUCCESS != ret)
{
printf("MSPLogout failed, error code is: %d", ret);
}
system("pause");
return 0;
}
最后
以上就是热心魔镜为你收集整理的讯飞C/C++语音合成基础篇的全部内容,希望文章能够帮你解决讯飞C/C++语音合成基础篇所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复