6 #include "alsa/asoundlib.h" 14 basePath =
"/home/chenying/catkin_ws/src/xbot_talker";
29 std::cout <<
"TextToSpeech::TextToSpeech(string basePath, const char* " 30 "src_text, string audioFile) is called." 32 std::cout << basePath << std::endl;
41 map<string, string> text_audio_tempmap;
42 return text_audio_tempmap;
52 return basePath +
"/" + std::to_string(current_map.size()) +
".wav";
65 const char* sessionID = NULL;
66 unsigned int audio_len = 0;
73 map<string, string>::iterator filter =
text_audio_map.find(src_text);
78 std::cout <<
"Playing the file cached done. " << std::endl;
82 std::cout <<
"需要调用xunfei tts函数生成相应音频文件并保存" << std::endl;
85 std::cout << tmp_file << std::endl;
86 const char* tts_begin_params =
"engine_type = local,voice_name=xiaoyan, text_encoding = UTF8, " 87 "tts_res_path = fo|res/tts/xiaoyan.jet;fo|res/tts/common.jet, " 88 "sample_rate = 44100, speed = 50, volume = 50, pitch = 50, rdn = 2";
91 std::cout <<
"param is error!" << std::endl;
94 std::ofstream outfile(tmp_file.c_str(), std::ios::out | std::ios::binary);
95 if (!outfile.is_open())
97 std::cout <<
"open file path error " << tmp_file << std::endl;
109 std::cout <<
"QTTSSessionBegin failed, error code: " << ret << std::endl;
121 ret =
QTTSTextPut(sessionID, src_text, (
unsigned int)strlen(src_text), NULL);
124 std::cout <<
"QTTSTextPut failed, error code:" << ret << std::endl;
131 outfile.write(reinterpret_cast<char*>(&wav_hdr),
sizeof(wav_hdr));
142 const void* data =
QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
147 outfile.write(static_cast<const char*>(const_cast<void*>(data)), audio_len);
157 std::cout <<
"QTTSAudioGet failed, error code: " << ret << std::endl;
166 outfile.seekp(4, std::ios::beg);
168 outfile.write(reinterpret_cast<char*>(&wav_hdr.
size_8),
sizeof(wav_hdr.
size_8));
170 outfile.seekp(40, std::ios::beg);
172 outfile.write(reinterpret_cast<char*>(&wav_hdr.
data_size),
sizeof(wav_hdr.
data_size));
178 std::cout <<
"QTTSSessionEnd failed, error code: " << ret << std::endl;
180 std::cout <<
"Synthesize completed." << std::endl;
185 std::cout <<
"Start talking....... " << tmp_file << std::endl;
187 std::cout <<
"Playing done. " << std::endl;
198 snd_pcm_hw_params_t* params;
201 snd_pcm_uframes_t frames;
203 int channels = wav_header->
channels;
208 rc = snd_pcm_open(&handle,
"default", SND_PCM_STREAM_PLAYBACK, 0);
211 perror(
"\nopen PCM device failed:");
215 snd_pcm_hw_params_alloca(¶ms);
218 perror(
"\nsnd_pcm_hw_params_alloca:");
222 rc = snd_pcm_hw_params_any(handle, params);
225 perror(
"\nsnd_pcm_hw_params_any:");
228 rc = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
231 perror(
"\nsed_pcm_hw_set_access:");
239 snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_U8);
242 snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
245 snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S24_LE);
248 rc = snd_pcm_hw_params_set_channels(handle, params, channels);
251 perror(
"\nsnd_pcm_hw_params_set_channels:");
255 rc = snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
258 perror(
"\nsnd_pcm_hw_params_set_rate_near:");
262 rc = snd_pcm_hw_params(handle, params);
265 perror(
"\nsnd_pcm_hw_params: ");
269 rc = snd_pcm_hw_params_get_period_size(params, &frames, &dir);
272 perror(
"\nsnd_pcm_hw_params_get_period_size:");
276 size = frames * datablock;
278 buffer = (
char*)malloc(size);
281 std::cout <<
"\n Malloc failed in tts module::buffer !!!\n";
284 fseek(fp, 58, SEEK_SET);
288 memset(buffer, 0,
sizeof(buffer));
290 ret = fread(buffer, 1, size, fp);
295 else if (ret != size)
300 snd_pcm_writei(handle, buffer, frames);
305 fprintf(stderr,
"underrun occurred\n");
307 snd_pcm_prepare(handle);
311 fprintf(stderr,
"error from writei: %s\n", snd_strerror(ret));
316 snd_pcm_drain(handle);
317 snd_pcm_close(handle);
326 fp = fopen(file_path.c_str(),
"rb");
331 int nread = fread(&wav_header, 1,
sizeof(wav_header), fp);
const char *MSPAPI QTTSSessionBegin(const char *params, int *errorCode)
int set_pcm_play(FILE *fp, wave_pcm_hdr *wav_header)
static map< string, string > text_audio_map
static map< string, string > createMap()
const void *MSPAPI QTTSAudioGet(const char *sessionID, unsigned int *audioLen, int *synthStatus, int *errorCode)
short int bits_per_sample
static wave_pcm_hdr default_wav_hdr
int MSPAPI QTTSSessionEnd(const char *sessionID, const char *hints)
string getAudioFile(map< string, string > current_map)
bool audioConverter(const std::string base_path, const char *src_text)
const std::string base_path
int play_wav(const string &file_path)
int MSPAPI QTTSTextPut(const char *sessionID, const char *textString, unsigned int textLen, const char *params)