|
|
@ -1,186 +1,169 @@
|
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
* 语音合成(Text To Speech,TTS)技术能够自动将任意文字实时转换为连续的
|
|
|
|
#include <stdio.h>
|
|
|
|
* 自然语音,是一种能够在任何时间、任何地点,向任何人提供语音信息服务的
|
|
|
|
#include <unistd.h>
|
|
|
|
* 高效便捷手段,非常符合信息时代海量数据、动态更新和个性化查询的需求。
|
|
|
|
#include <errno.h>
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "../../include/qtts.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "../../include/msp_cmn.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "../../include/msp_errors.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
typedef int SR_DWORD;
|
|
|
|
#include <errno.h>
|
|
|
|
typedef short int SR_WORD ;
|
|
|
|
#include "../../include/qtts.h"
|
|
|
|
|
|
|
|
#include "../../include/msp_cmn.h"
|
|
|
|
/* wav音频头部格式 */
|
|
|
|
#include "../../include/msp_errors.h"
|
|
|
|
typedef struct _wave_pcm_hdr
|
|
|
|
typedef int SR_DWORD;
|
|
|
|
{
|
|
|
|
typedef short int SR_WORD ;
|
|
|
|
char riff[4]; // = "RIFF"
|
|
|
|
|
|
|
|
int size_8; // = FileSize - 8
|
|
|
|
/* wav音频头部格式 */
|
|
|
|
char wave[4]; // = "WAVE"
|
|
|
|
typedef struct _wave_pcm_hdr
|
|
|
|
char fmt[4]; // = "fmt "
|
|
|
|
{
|
|
|
|
int fmt_size; // = 下一个结构体的大小 : 16
|
|
|
|
char riff[4]; // = "RIFF"
|
|
|
|
|
|
|
|
int size_8; // = FileSize - 8
|
|
|
|
short int format_tag; // = PCM : 1
|
|
|
|
char wave[4]; // = "WAVE"
|
|
|
|
short int channels; // = 通道数 : 1
|
|
|
|
char fmt[4]; // = "fmt "
|
|
|
|
int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000
|
|
|
|
int fmt_size; // = 下一个结构体的大小 : 16
|
|
|
|
int avg_bytes_per_sec; // = 每秒字节数 : samples_per_sec * bits_per_sample / 8
|
|
|
|
|
|
|
|
short int block_align; // = 每采样点字节数 : wBitsPerSample / 8
|
|
|
|
short int format_tag; // = PCM : 1
|
|
|
|
short int bits_per_sample; // = 量化比特数: 8 | 16
|
|
|
|
short int channels; // = 通道数 : 1
|
|
|
|
|
|
|
|
int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000
|
|
|
|
char data[4]; // = "data";
|
|
|
|
int avg_bytes_per_sec; // = 每秒字节数 : samples_per_sec * bits_per_sample / 8
|
|
|
|
int data_size; // = 纯数据长度 : FileSize - 44
|
|
|
|
short int block_align; // = 每采样点字节数 : wBitsPerSample / 8
|
|
|
|
} wave_pcm_hdr;
|
|
|
|
short int bits_per_sample; // = 量化比特数: 8 | 16
|
|
|
|
|
|
|
|
|
|
|
|
/* 默认wav音频头部数据 */
|
|
|
|
char data[4]; // = "data";
|
|
|
|
wave_pcm_hdr default_wav_hdr =
|
|
|
|
int data_size; // = 纯数据长度 : FileSize - 44
|
|
|
|
{
|
|
|
|
} wave_pcm_hdr;
|
|
|
|
{ 'R', 'I', 'F', 'F' },
|
|
|
|
|
|
|
|
0,
|
|
|
|
/* 默认wav音频头部数据 */
|
|
|
|
{'W', 'A', 'V', 'E'},
|
|
|
|
wave_pcm_hdr default_wav_hdr =
|
|
|
|
{'f', 'm', 't', ' '},
|
|
|
|
{
|
|
|
|
16,
|
|
|
|
{ 'R', 'I', 'F', 'F' },
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
{'W', 'A', 'V', 'E'},
|
|
|
|
16000,
|
|
|
|
{'f', 'm', 't', ' '},
|
|
|
|
32000,
|
|
|
|
16,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
16,
|
|
|
|
1,
|
|
|
|
{'d', 'a', 't', 'a'},
|
|
|
|
16000,
|
|
|
|
0
|
|
|
|
32000,
|
|
|
|
};
|
|
|
|
2,
|
|
|
|
/* 文本合成 */
|
|
|
|
16,
|
|
|
|
int text_to_speech(const char* src_text, const char* des_path, const char* params)
|
|
|
|
{'d', 'a', 't', 'a'},
|
|
|
|
{
|
|
|
|
0
|
|
|
|
int ret = -1;
|
|
|
|
};
|
|
|
|
FILE* fp = NULL;
|
|
|
|
/* 文本合成 */
|
|
|
|
const char* sessionID = NULL;
|
|
|
|
int text_to_speech(const char* src_text, const char* des_path, const char* params)
|
|
|
|
unsigned int audio_len = 0;
|
|
|
|
{
|
|
|
|
wave_pcm_hdr wav_hdr = default_wav_hdr;
|
|
|
|
int ret = -1;
|
|
|
|
int synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA;
|
|
|
|
FILE* fp = NULL;
|
|
|
|
|
|
|
|
const char* sessionID = NULL;
|
|
|
|
if (NULL == src_text || NULL == des_path)
|
|
|
|
unsigned int audio_len = 0;
|
|
|
|
{
|
|
|
|
wave_pcm_hdr wav_hdr = default_wav_hdr;
|
|
|
|
printf("params is error!\n");
|
|
|
|
int synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA;
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (NULL == src_text || NULL == des_path)
|
|
|
|
fp = fopen(des_path, "wb");
|
|
|
|
{
|
|
|
|
if (NULL == fp)
|
|
|
|
printf("params is error!\n");
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
printf("open %s error.\n", des_path);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
fp = fopen(des_path, "wb");
|
|
|
|
}
|
|
|
|
if (NULL == fp)
|
|
|
|
/* 开始合成 */
|
|
|
|
{
|
|
|
|
sessionID = QTTSSessionBegin(params, &ret);
|
|
|
|
printf("open %s error.\n", des_path);
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
return ret;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
printf("QTTSSessionBegin failed, error code: %d.\n", ret);
|
|
|
|
/* 开始合成 */
|
|
|
|
fclose(fp);
|
|
|
|
sessionID = QTTSSessionBegin(params, &ret);
|
|
|
|
return ret;
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL);
|
|
|
|
printf("QTTSSessionBegin failed, error code: %d.\n", ret);
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
fclose(fp);
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
printf("QTTSTextPut failed, error code: %d.\n",ret);
|
|
|
|
}
|
|
|
|
QTTSSessionEnd(sessionID, "TextPutError");
|
|
|
|
ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL);
|
|
|
|
fclose(fp);
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
return ret;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
printf("QTTSTextPut failed, error code: %d.\n",ret);
|
|
|
|
printf("正在合成 ...\n");
|
|
|
|
QTTSSessionEnd(sessionID, "TextPutError");
|
|
|
|
fwrite(&wav_hdr, sizeof(wav_hdr) ,1, fp); //添加wav音频头,使用采样率为16000
|
|
|
|
fclose(fp);
|
|
|
|
while (1)
|
|
|
|
return ret;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/* 获取合成音频 */
|
|
|
|
printf("正在合成 ...\n");
|
|
|
|
const void* data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
|
|
|
|
fwrite(&wav_hdr, sizeof(wav_hdr) ,1, fp); //添加wav音频头,使用采样率为16000
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
while (1)
|
|
|
|
break;
|
|
|
|
{
|
|
|
|
if (NULL != data)
|
|
|
|
/* 获取合成音频 */
|
|
|
|
{
|
|
|
|
const void* data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
|
|
|
|
fwrite(data, audio_len, 1, fp);
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
wav_hdr.data_size += audio_len; //计算data_size大小
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (NULL != data)
|
|
|
|
if (MSP_TTS_FLAG_DATA_END == synth_status)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
fwrite(data, audio_len, 1, fp);
|
|
|
|
}
|
|
|
|
wav_hdr.data_size += audio_len; //计算data_size大小
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
if (MSP_TTS_FLAG_DATA_END == synth_status)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
printf("QTTSAudioGet failed, error code: %d.\n",ret);
|
|
|
|
}
|
|
|
|
QTTSSessionEnd(sessionID, "AudioGetError");
|
|
|
|
printf("\n");
|
|
|
|
fclose(fp);
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
return ret;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
printf("QTTSAudioGet failed, error code: %d.\n",ret);
|
|
|
|
/* 修正wav文件头数据的大小 */
|
|
|
|
QTTSSessionEnd(sessionID, "AudioGetError");
|
|
|
|
wav_hdr.size_8 += wav_hdr.data_size + (sizeof(wav_hdr) - 8);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
/* 将修正过的数据写回文件头部,音频文件为wav格式 */
|
|
|
|
}
|
|
|
|
fseek(fp, 4, 0);
|
|
|
|
/* 修正wav文件头数据的大小 */
|
|
|
|
fwrite(&wav_hdr.size_8,sizeof(wav_hdr.size_8), 1, fp); //写入size_8的值
|
|
|
|
wav_hdr.size_8 += wav_hdr.data_size + (sizeof(wav_hdr) - 8);
|
|
|
|
fseek(fp, 40, 0); //将文件指针偏移到存储data_size值的位置
|
|
|
|
|
|
|
|
fwrite(&wav_hdr.data_size,sizeof(wav_hdr.data_size), 1, fp); //写入data_size的值
|
|
|
|
/* 将修正过的数据写回文件头部,音频文件为wav格式 */
|
|
|
|
fclose(fp);
|
|
|
|
fseek(fp, 4, 0);
|
|
|
|
fp = NULL;
|
|
|
|
fwrite(&wav_hdr.size_8,sizeof(wav_hdr.size_8), 1, fp); //写入size_8的值
|
|
|
|
/* 合成完毕 */
|
|
|
|
fseek(fp, 40, 0); //将文件指针偏移到存储data_size值的位置
|
|
|
|
ret = QTTSSessionEnd(sessionID, "Normal");
|
|
|
|
fwrite(&wav_hdr.data_size,sizeof(wav_hdr.data_size), 1, fp); //写入data_size的值
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
fclose(fp);
|
|
|
|
{
|
|
|
|
fp = NULL;
|
|
|
|
printf("QTTSSessionEnd failed, error code: %d.\n",ret);
|
|
|
|
/* 合成完毕 */
|
|
|
|
}
|
|
|
|
ret = QTTSSessionEnd(sessionID, "Normal");
|
|
|
|
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
return ret;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
printf("QTTSSessionEnd failed, error code: %d.\n",ret);
|
|
|
|
|
|
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
int ret = MSP_SUCCESS;
|
|
|
|
}
|
|
|
|
const char* login_params = "appid = ee5efae8, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动
|
|
|
|
|
|
|
|
/*
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
* rdn: 合成音频数字发音方式
|
|
|
|
{
|
|
|
|
* volume: 合成音频的音量
|
|
|
|
int ret = MSP_SUCCESS;
|
|
|
|
* pitch: 合成音频的音调
|
|
|
|
const char* login_params = "appid = ee5efae8, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动
|
|
|
|
* speed: 合成音频对应的语速
|
|
|
|
/*
|
|
|
|
* voice_name: 合成发音人
|
|
|
|
* rdn: 合成音频数字发音方式
|
|
|
|
* sample_rate: 合成音频采样率
|
|
|
|
* volume: 合成音频的音量
|
|
|
|
* text_encoding: 合成文本编码格式
|
|
|
|
* pitch: 合成音频的音调
|
|
|
|
*
|
|
|
|
* speed: 合成音频对应的语速
|
|
|
|
*/
|
|
|
|
* voice_name: 合成发音人
|
|
|
|
const char* session_begin_params = "engine_type = local,voice_name=xiaoyan, text_encoding = UTF8, tts_res_path = fo|res/tts/xiaoyan.jet;fo|res/tts/common.jet, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";
|
|
|
|
* sample_rate: 合成音频采样率
|
|
|
|
const char* filename = "/home/pi/Desktop/al/error.wav"; //合成的语音文件名称
|
|
|
|
* text_encoding: 合成文本编码格式
|
|
|
|
const char* text = " 程序发生错误。"; //合成文本
|
|
|
|
*
|
|
|
|
/* 用户登录 */
|
|
|
|
*/
|
|
|
|
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名,第二个参数是密码,第三个参数是登录参数,用户名和密码可在http://www.xfyun.cn注册获取
|
|
|
|
const char* session_begin_params = "engine_type = local,voice_name=xiaoyan, text_encoding = UTF8, tts_res_path = fo|res/tts/xiaoyan.jet;fo|res/tts/common.jet, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
const char* filename = "output.wav"; //合成的语音文件名称
|
|
|
|
{
|
|
|
|
const char* text = "欢迎使用轻便型导盲杖,此产品主要适用于盲人,为盲人出行进行安全的指导,解决盲人出现问题"; //合成文本
|
|
|
|
printf("MSPLogin failed, error code: %d.\n", ret);
|
|
|
|
/* 用户登录 */
|
|
|
|
goto exit ;//登录失败,退出登录
|
|
|
|
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名,第二个参数是密码,第三个参数是登录参数,用户名和密码可在http://www.xfyun.cn注册获取
|
|
|
|
}
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
|
|
|
|
{
|
|
|
|
/* 文本合成 */
|
|
|
|
printf("MSPLogin failed, error code: %d.\n", ret);
|
|
|
|
printf("开始合成 ...\n");
|
|
|
|
goto exit ;//登录失败,退出登录
|
|
|
|
ret = text_to_speech(text, filename, session_begin_params);
|
|
|
|
}
|
|
|
|
if (MSP_SUCCESS != ret)
|
|
|
|
|
|
|
|
{
|
|
|
|
printf("\n###########################################################################\n");
|
|
|
|
printf("text_to_speech failed, error code: %d.\n", ret);
|
|
|
|
printf("## 语音合成(Text To Speech,TTS)技术能够自动将任意文字实时转换为连续的 ##\n");
|
|
|
|
}
|
|
|
|
printf("## 自然语音,是一种能够在任何时间、任何地点,向任何人提供语音信息服务的 ##\n");
|
|
|
|
printf("合成完毕\n");
|
|
|
|
printf("## 高效便捷手段,非常符合信息时代海量数据、动态更新和个性化查询的需求。 ##\n");
|
|
|
|
|
|
|
|
printf("###########################################################################\n\n");
|
|
|
|
exit:
|
|
|
|
|
|
|
|
MSPLogout(); //退出登录
|
|
|
|
/* 文本合成 */
|
|
|
|
|
|
|
|
printf("开始合成 ...\n");
|
|
|
|
return 0;
|
|
|
|
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\n");
|
|
|
|
|
|
|
|
printf("###########################################################################\n\n");
|
|
|
|
|
|
|
|
printf("###########################################################################\n\n");
|
|
|
|
|
|
|
|
printf("###########################################################################\n\n");
|
|
|
|
|
|
|
|
printf("按任意键退出 ...\n");
|
|
|
|
|
|
|
|
getchar();
|
|
|
|
|
|
|
|
MSPLogout(); //退出登录
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|