espeak-sg.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <espeak-ng/espeak_ng.h>
  2. #define PLAYBACK_MODE (ENOUTPUT_MODE_SYNCHRONOUS | ENOUTPUT_MODE_SPEAK_AUDIO)
  3. #include <vector>
  4. #include <string>
  5. #include <sndfile.h>
  6. #include <math.h>
  7. #include <string.h>
  8. #include <fstream>
  9. #include <boost/lexical_cast.hpp>
  10. #include <boost/algorithm/string/classification.hpp>
  11. #include <boost/algorithm/string/split.hpp>
  12. struct segment
  13. {
  14. char type;
  15. int start;
  16. int length;
  17. };
  18. struct pho_event
  19. {
  20. std::string code;
  21. int type;
  22. int start;
  23. };
  24. struct output_data
  25. {
  26. // for each run
  27. std::vector<short int> samples;
  28. std::vector<segment> segments;
  29. std::vector<pho_event> pho;
  30. char segment_type = 0;
  31. int segment_pos = 0;
  32. float length;
  33. };
  34. struct output_data* current;
  35. struct output_data* last = nullptr;
  36. std::vector<output_data*> runs;
  37. std::map<std::string,std::string> mbrolaMap;
  38. void init()
  39. {
  40. espeak_ng_InitializePath(NULL);
  41. espeak_ng_ERROR_CONTEXT context = NULL;
  42. espeak_ng_Initialize(&context);
  43. espeak_ng_InitializeOutput(ENOUTPUT_MODE_SYNCHRONOUS, 0, NULL); //FIX https://notabug.org/isengaara/sekai/issues/4
  44. }
  45. void segment_event(char type)
  46. {
  47. if(current->segment_type!=type)
  48. {
  49. if(current->segment_type!=0)
  50. {
  51. segment s;
  52. s.type = current->segment_type;
  53. s.start = current->segment_pos;
  54. s.length = current->samples.size()-current->segment_pos;
  55. current->segments.push_back(s);
  56. }
  57. current->segment_type = type;
  58. current->segment_pos = current->samples.size();
  59. }
  60. }
  61. void outputPhoSymbol(char* pho_code,int pho_type)
  62. {
  63. pho_event p;
  64. p.code = pho_code;
  65. p.type = pho_type;
  66. p.start = current->samples.size();
  67. current->pho.push_back(p);
  68. }
  69. void outputSilence(short int sample)
  70. {
  71. segment_event('S');
  72. current->samples.push_back(sample);
  73. }
  74. void outputUnvoiced(short int sample)
  75. {
  76. segment_event('U');
  77. current->samples.push_back(sample);
  78. }
  79. void outputVoiced(short int sample)
  80. {
  81. segment_event('V');
  82. current->samples.push_back(sample);
  83. }
  84. void flush()
  85. {
  86. outputPhoSymbol((char*)"#",0);
  87. segment_event(0);
  88. }
  89. void write_zstring(FILE* f,std::string s)
  90. {
  91. fwrite(s.c_str(),1,s.length()+1,f);
  92. }
  93. void write_int(FILE* f,int n)
  94. {
  95. fwrite(&n,1,sizeof(int),f);
  96. }
  97. void write_short(FILE* f,short n)
  98. {
  99. fwrite(&n,1,sizeof(short),f);
  100. }
  101. void write_char(FILE* f,char n)
  102. {
  103. fwrite(&n,1,sizeof(char),f);
  104. }
  105. void write_float(FILE* f,float n)
  106. {
  107. fwrite(&n,1,sizeof(float),f);
  108. }
  109. void do_synth(int rate,int f0,char* lyric)
  110. {
  111. // synth with param
  112. espeak_SetParameter(espeakRATE, rate, 0);
  113. //synth
  114. espeak_ng_SetConstF0(f0);
  115. espeak_ng_Synthesize(lyric, 0, 0, POS_CHARACTER, 0, 0, NULL, NULL);
  116. flush();
  117. }
  118. void show_length(float fs)
  119. {
  120. float count = current->samples.size();
  121. for (uint i = 0; i<current->pho.size()-1; i++)
  122. {
  123. auto pho = current->pho[i].code;
  124. auto pos = current->pho[i].start;
  125. //printf("%s %i\n",pho.c_str(),pos);
  126. if(pho[0]=='_')
  127. {
  128. float length = pos/fs;
  129. current->length = length;
  130. return;
  131. }
  132. }
  133. float length = count/fs;
  134. current->length = length;
  135. }
  136. void find_best_one(float note_length)
  137. {
  138. bool found = false;
  139. for(size_t i=0; i<runs.size()-1; i++)
  140. {
  141. float delta0 = fabs(runs[i]->length - note_length);
  142. float delta1 = fabs(runs[i+1]->length - note_length);
  143. //printf("find best one %i %f %f",(int)i,delta0,delta1);
  144. if(!found && delta1 > delta0)
  145. {
  146. //printf(" found");
  147. current = runs[i];
  148. found = true;
  149. }
  150. //printf("\n");
  151. }
  152. }
  153. void load_mbrola_table(std::string fileName) {
  154. std::ifstream infile(fileName);
  155. std::string line;
  156. while (std::getline(infile, line)) {
  157. std::vector<std::string> spl;
  158. boost::split(spl, line, boost::is_any_of("\t "),
  159. boost::token_compress_on);
  160. mbrolaMap[spl[0]] = spl[1];
  161. }
  162. }
  163. int main(int argc,char** argv)
  164. {
  165. if(argc<6)
  166. {
  167. printf("usage: espeak-sg voice f0 lyric rate filename [optargs..]\n");
  168. return 0;
  169. }
  170. char* voice = argv[1];
  171. //printf("espeak::voice=%s\n",voice);
  172. int f0 = atoi(argv[2]);
  173. char* lyric = argv[3];
  174. int rate = atoi(argv[4]);
  175. char* filename = argv[5];
  176. bool have_note_length = false;
  177. float note_length = 0;
  178. bool mbrola = false;
  179. for(int i=6;i<argc;i++)
  180. {
  181. char* optarg = argv[i];
  182. if(strlen(optarg)>=4)
  183. {
  184. if(optarg[0]=='n' && optarg[1]=='l' && optarg[2]=='=')
  185. {
  186. note_length = boost::lexical_cast<float>(optarg+3);
  187. have_note_length = true;
  188. }
  189. if(optarg[0]=='m' && optarg[1]=='b' && optarg[2]=='=')
  190. {
  191. mbrola=true;
  192. load_mbrola_table(optarg+3);
  193. }
  194. }
  195. }
  196. init();
  197. espeak_ng_SetVoiceByName(voice);
  198. espeak_ng_OUTPUT_HOOKS hooks;
  199. hooks.outputPhoSymbol = outputPhoSymbol;
  200. hooks.outputSilence = outputSilence;
  201. hooks.outputUnvoiced = outputUnvoiced;
  202. hooks.outputVoiced = outputVoiced;
  203. espeak_ng_SetOutputHooks(&hooks);
  204. float samplerate = (float)espeak_ng_GetSampleRate();
  205. if(rate!=0)
  206. {
  207. current = new output_data;
  208. do_synth(rate,f0,lyric);
  209. }
  210. else
  211. {
  212. if(!have_note_length)
  213. {
  214. fprintf(stderr,"note length required\n");
  215. return 1;
  216. }
  217. #if 0
  218. SF_INFO info;
  219. info.samplerate = espeak_ng_GetSampleRate();
  220. info.channels = 1;
  221. info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
  222. info.sections = 0;
  223. info.frames = 0;
  224. info.seekable = 0;
  225. SNDFILE* sf = sf_open("/tmp/debug.wav",SFM_WRITE,&info);
  226. #endif
  227. for(int current_rate = 80; current_rate < 450; current_rate += 5)
  228. {
  229. //printf("set rate %i\n",current_rate);
  230. current = new output_data;
  231. do_synth(current_rate,f0,lyric);
  232. show_length(samplerate);
  233. runs.push_back(current);
  234. last = current;
  235. }
  236. find_best_one(note_length);
  237. #if 0
  238. sf_write_short(sf,current->samples.data(),current->samples.size());
  239. sf_close(sf);
  240. #endif
  241. }
  242. if(mbrola)
  243. {
  244. FILE* f = fopen(filename,"w");
  245. ///if(have_note_length) fprintf(f,"#nl=%f\n",note_length);
  246. for (uint i = 0; i<current->pho.size()-1; i++)
  247. {
  248. auto pho = current->pho[i].code;
  249. if(pho[0]=='_') break;
  250. if (mbrolaMap.count(pho))
  251. {
  252. pho = mbrolaMap[pho];
  253. }
  254. float t = (current->pho[i+1].start + current->pho[i].start) * 1.0 / samplerate;
  255. char* ototypes[]= {"PAUSE","STRESS","VOWEL","LIQUID","STOP","VSTOP","FRICATIVE","VFRICATIVE","NASAL","VIRTUAL","DELETED","INVALID"};
  256. fprintf(f,"%s %s %f",pho.c_str(),ototypes[current->pho[i].type],t);
  257. if(last)
  258. {
  259. float t_min = (last->pho[i+1].start + last->pho[i].start) * 1.0 / samplerate;
  260. fprintf(f," %f",t_min);
  261. }
  262. fprintf(f,"\n");
  263. }
  264. fclose(f);
  265. return 0;
  266. }
  267. FILE* f = fopen(filename,"w");
  268. write_zstring(f,"espeak-sg");
  269. write_int(f,0); //version of the file format
  270. write_int(f,samplerate);
  271. write_int(f,f0);
  272. write_int(f,current->pho.size());
  273. for (auto i : current->pho)
  274. {
  275. write_zstring(f,i.code);
  276. write_int(f,i.type);
  277. write_int(f,i.start);
  278. }
  279. write_int(f,current->segments.size());
  280. for (auto i : current->segments)
  281. {
  282. write_char(f, i.type);
  283. write_int(f, i.start);
  284. write_int(f, i.length);
  285. }
  286. write_int(f,current->samples.size());
  287. fwrite(current->samples.data(),current->samples.size(),sizeof(short),f);
  288. fclose(f);
  289. return 0;
  290. }