eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spect.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (C) 2005 to 2007 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2013-2016 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <errno.h>
  21. #include <math.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <endian.h>
  27. #include <espeak-ng/espeak_ng.h>
  28. #include <espeak-ng/speak_lib.h>
  29. #include "phoneme.h"
  30. #include "voice.h"
  31. #include "synthesize.h"
  32. #include "spect.h"
  33. #include "ieee80.h"
  34. extern unsigned char pk_shape1[];
  35. extern int pk_select;
  36. extern char voice_name[];
  37. static int frame_width;
  38. int pk_select;
  39. static int default_freq[N_PEAKS] =
  40. { 200, 500, 1200, 3000, 3500, 4000, 6900, 7800, 9000 };
  41. static int default_width[N_PEAKS] =
  42. { 750, 500, 550, 550, 600, 700, 700, 700, 700 };
  43. static int default_klt_bw[N_PEAKS] =
  44. { 89, 90, 140, 260, 260, 260, 500, 500, 500 };
  45. static double read_double(FILE *stream)
  46. {
  47. unsigned char bytes[10];
  48. fread(bytes, sizeof(char), 10, stream);
  49. return ConvertFromIeeeExtended((char *)bytes);
  50. }
  51. float polint(float xa[], float ya[], int n, float x)
  52. {
  53. // General polinomial interpolation routine, xa[1...n] ya[1...n]
  54. int i, m, ns = 1;
  55. float den, dif, dift, ho, hp, w;
  56. float y; // result
  57. float c[9], d[9];
  58. dif = fabs(x-xa[1]);
  59. for (i = 1; i <= n; i++) {
  60. if ((dift = fabs(x-xa[i])) < dif) {
  61. ns = i;
  62. dif = dift;
  63. }
  64. c[i] = ya[i];
  65. d[i] = ya[i];
  66. }
  67. y = ya[ns--];
  68. for (m = 1; m < n; m++) {
  69. for (i = 1; i <= n-m; i++) {
  70. ho = xa[i]-x;
  71. hp = xa[i+m]-x;
  72. w = c[i+1]-d[i];
  73. if ((den = ho-hp) == 0.0)
  74. return ya[2]; // two input xa are identical
  75. den = w/den;
  76. d[i] = hp*den;
  77. c[i] = ho*den;
  78. }
  79. y += ((2*ns < (n-m) ? c[ns+1] : d[ns--]));
  80. }
  81. return y;
  82. }
  83. static SpectFrame *SpectFrameCreate()
  84. {
  85. int ix;
  86. SpectFrame *frame;
  87. frame = malloc(sizeof(SpectFrame));
  88. if (!frame)
  89. return NULL;
  90. frame->keyframe = 0;
  91. frame->spect = NULL;
  92. frame->markers = 0;
  93. frame->pitch = 0;
  94. frame->nx = 0;
  95. frame->time = 0;
  96. frame->length = 0;
  97. frame->amp_adjust = 100;
  98. frame->length_adjust = 0;
  99. for (ix = 0; ix < N_PEAKS; ix++) {
  100. frame->formants[ix].freq = 0;
  101. frame->peaks[ix].pkfreq = default_freq[ix];
  102. frame->peaks[ix].pkheight = 0;
  103. frame->peaks[ix].pkwidth = default_width[ix];
  104. frame->peaks[ix].pkright = default_width[ix];
  105. frame->peaks[ix].klt_bw = default_klt_bw[ix];
  106. frame->peaks[ix].klt_ap = 0;
  107. frame->peaks[ix].klt_bp = default_klt_bw[ix];
  108. }
  109. memset(frame->klatt_param, 0, sizeof(frame->klatt_param));
  110. frame->klatt_param[KLATT_AV] = 59;
  111. frame->klatt_param[KLATT_Kopen] = 40;
  112. return frame;
  113. }
  114. static void SpectFrameDestroy(SpectFrame *frame)
  115. {
  116. if (frame->spect != NULL)
  117. free(frame->spect);
  118. free(frame);
  119. }
  120. static espeak_ng_STATUS LoadFrame(SpectFrame *frame, FILE *stream, int file_format_type)
  121. {
  122. short ix;
  123. short x;
  124. unsigned short *spect_data;
  125. frame->time = read_double(stream);
  126. frame->pitch = read_double(stream);
  127. frame->length = read_double(stream);
  128. frame->dx = read_double(stream);
  129. fread(&frame->nx, sizeof(short), 1, stream);
  130. fread(&frame->markers, sizeof(short), 1, stream);
  131. fread(&frame->amp_adjust, sizeof(short), 1, stream);
  132. frame->nx = le16toh(frame->nx);
  133. frame->markers = le16toh(frame->markers);
  134. frame->amp_adjust = le16toh(frame->amp_adjust);
  135. if (file_format_type == 2) {
  136. fread(&ix, sizeof(short), 1, stream); // spare
  137. fread(&ix, sizeof(short), 1, stream); // spare
  138. }
  139. for (ix = 0; ix < N_PEAKS; ix++) {
  140. fread(&frame->formants[ix].freq, sizeof(short), 1, stream);
  141. fread(&frame->formants[ix].bandw, sizeof(short), 1, stream);
  142. fread(&frame->peaks[ix].pkfreq, sizeof(short), 1, stream);
  143. fread(&frame->peaks[ix].pkheight, sizeof(short), 1, stream);
  144. fread(&frame->peaks[ix].pkwidth, sizeof(short), 1, stream);
  145. fread(&frame->peaks[ix].pkright, sizeof(short), 1, stream);
  146. frame->formants[ix].freq = le16toh(frame->formants[ix].freq);
  147. frame->formants[ix].bandw = le16toh(frame->formants[ix].bandw);
  148. frame->peaks[ix].pkfreq = le16toh(frame->peaks[ix].pkfreq);
  149. frame->peaks[ix].pkheight = le16toh(frame->peaks[ix].pkheight);
  150. frame->peaks[ix].pkwidth = le16toh(frame->peaks[ix].pkwidth);
  151. frame->peaks[ix].pkright = le16toh(frame->peaks[ix].pkright);
  152. if (frame->peaks[ix].pkheight > 0)
  153. frame->keyframe = 1;
  154. if (file_format_type == 2) {
  155. fread(&frame->peaks[ix].klt_bw, sizeof(short), 1, stream);
  156. fread(&frame->peaks[ix].klt_ap, sizeof(short), 1, stream);
  157. fread(&frame->peaks[ix].klt_bp, sizeof(short), 1, stream);
  158. frame->peaks[ix].klt_bw = le16toh(frame->peaks[ix].klt_bw);
  159. frame->peaks[ix].klt_ap = le16toh(frame->peaks[ix].klt_ap);
  160. frame->peaks[ix].klt_bp = le16toh(frame->peaks[ix].klt_bp);
  161. }
  162. }
  163. if (file_format_type > 0) {
  164. for (ix = 0; ix < N_KLATTP2; ix++)
  165. {
  166. fread(frame->klatt_param + ix, sizeof(short), 1, stream);
  167. frame->klatt_param[ix] = le16toh(frame->klatt_param[ix]);
  168. }
  169. }
  170. spect_data = malloc(sizeof(unsigned short) * frame->nx);
  171. if (spect_data == NULL)
  172. return ENOMEM;
  173. frame->max_y = 0;
  174. for (ix = 0; ix < frame->nx; ix++) {
  175. fread(&x, sizeof(short), 1, stream);
  176. x = le16toh(x);
  177. spect_data[ix] = x;
  178. if (x > frame->max_y) frame->max_y = x;
  179. }
  180. frame->spect = spect_data;
  181. return ENS_OK;
  182. }
  183. double GetFrameRms(SpectFrame *frame, int seq_amplitude)
  184. {
  185. int h;
  186. float total = 0;
  187. int maxh;
  188. int height;
  189. int htab[400];
  190. wavegen_peaks_t wpeaks[9];
  191. for (h = 0; h < 9; h++) {
  192. height = (frame->peaks[h].pkheight * seq_amplitude * frame->amp_adjust)/10000;
  193. wpeaks[h].height = height << 8;
  194. wpeaks[h].freq = frame->peaks[h].pkfreq << 16;
  195. wpeaks[h].left = frame->peaks[h].pkwidth << 16;
  196. wpeaks[h].right = frame->peaks[h].pkright << 16;
  197. }
  198. maxh = PeaksToHarmspect(wpeaks, 90<<16, htab, 0);
  199. for (h = 1; h < maxh; h++)
  200. total += ((htab[h] * htab[h]) >> 10);
  201. frame->rms = sqrt(total) / 7.25;
  202. return frame->rms;
  203. }
  204. SpectSeq *SpectSeqCreate()
  205. {
  206. SpectSeq *spect = malloc(sizeof(SpectSeq));
  207. if (!spect)
  208. return NULL;
  209. spect->numframes = 0;
  210. spect->frames = NULL;
  211. spect->name = NULL;
  212. pk_select = 1;
  213. spect->grid = 1;
  214. spect->duration = 0;
  215. spect->pitch1 = 0;
  216. spect->pitch2 = 0;
  217. spect->bass_reduction = 0;
  218. spect->max_x = 3000;
  219. spect->max_y = 1;
  220. spect->file_format = 0;
  221. return spect;
  222. }
  223. void SpectSeqDestroy(SpectSeq *spect)
  224. {
  225. int ix;
  226. if (spect->frames != NULL) {
  227. for (ix = 0; ix < spect->numframes; ix++) {
  228. if (spect->frames[ix] != NULL)
  229. SpectFrameDestroy(spect->frames[ix]);
  230. }
  231. free(spect->frames);
  232. }
  233. free(spect->name);
  234. free(spect);
  235. }
  236. static float GetFrameLength(SpectSeq *spect, int frame)
  237. {
  238. int ix;
  239. float adjust = 0;
  240. if (frame >= spect->numframes-1) return 0;
  241. for (ix = frame+1; ix < spect->numframes-1; ix++) {
  242. if (spect->frames[ix]->keyframe)
  243. break; // reached next keyframe
  244. adjust += spect->frames[ix]->length_adjust;
  245. }
  246. return (spect->frames[ix]->time - spect->frames[frame]->time) * 1000.0 + adjust;
  247. }
  248. espeak_ng_STATUS LoadSpectSeq(SpectSeq *spect, const char *filename)
  249. {
  250. short n, temp;
  251. int ix;
  252. uint32_t id1, id2, name_len;
  253. int set_max_y = 0;
  254. float time_offset;
  255. FILE *stream = fopen(filename, "rb");
  256. if (stream == NULL) {
  257. fprintf(stderr, "Failed to open: '%s'", filename);
  258. return errno;
  259. }
  260. fread(&id1, sizeof(uint32_t), 1, stream);
  261. id1 = le32toh(id1);
  262. fread(&id2, sizeof(uint32_t), 1, stream);
  263. id2 = le32toh(id2);
  264. if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEQ))
  265. spect->file_format = 0; // eSpeak formants
  266. else if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEK))
  267. spect->file_format = 1; // formants for Klatt synthesizer
  268. else if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSQ2))
  269. spect->file_format = 2; // formants for Klatt synthesizer
  270. else {
  271. fprintf(stderr, "Unsupported spectral file format.\n");
  272. fclose(stream);
  273. return ENS_UNSUPPORTED_PHON_FORMAT;
  274. }
  275. fread(&name_len, sizeof(uint32_t), 1, stream);
  276. name_len = le32toh(name_len);
  277. if (name_len > 0) {
  278. if ((spect->name = (char *)malloc(name_len)) == NULL) {
  279. fclose(stream);
  280. return ENOMEM;
  281. }
  282. fread(spect->name, sizeof(char), name_len, stream);
  283. } else
  284. spect->name = NULL;
  285. fread(&n, sizeof(short), 1, stream);
  286. fread(&spect->amplitude, sizeof(short), 1, stream);
  287. fread(&spect->max_y, sizeof(short), 1, stream);
  288. fread(&temp, sizeof(short), 1, stream); // unused
  289. n = le16toh(n);
  290. spect->amplitude = le16toh(spect->amplitude);
  291. spect->max_y = le16toh(spect->max_y);
  292. temp = le16toh(temp);
  293. if (n == 0) {
  294. fclose(stream);
  295. return ENS_NO_SPECT_FRAMES;
  296. }
  297. if (spect->frames != NULL) {
  298. for (ix = 0; ix < spect->numframes; ix++) {
  299. if (spect->frames[ix] != NULL)
  300. SpectFrameDestroy(spect->frames[ix]);
  301. }
  302. free(spect->frames);
  303. }
  304. spect->frames = calloc(n, sizeof(SpectFrame *));
  305. spect->numframes = 0;
  306. spect->max_x = 3000;
  307. if (spect->max_y == 0) {
  308. set_max_y = 1;
  309. spect->max_y = 1;
  310. }
  311. for (ix = 0; ix < n; ix++) {
  312. SpectFrame *frame = SpectFrameCreate();
  313. if (!frame) {
  314. fclose(stream);
  315. return ENOMEM;
  316. }
  317. espeak_ng_STATUS status = LoadFrame(frame, stream, spect->file_format);
  318. if (status != ENS_OK) {
  319. free(frame);
  320. fclose(stream);
  321. return status;
  322. }
  323. spect->frames[spect->numframes++] = frame;
  324. if (set_max_y && (frame->max_y > spect->max_y))
  325. spect->max_y = frame->max_y;
  326. if (frame->nx * frame->dx > spect->max_x) spect->max_x = (int)(frame->nx * frame->dx);
  327. }
  328. spect->max_x = 9000; // disable auto-xscaling
  329. frame_width = (int)((FRAME_WIDTH*spect->max_x)/MAX_DISPLAY_FREQ);
  330. if (frame_width > FRAME_WIDTH) frame_width = FRAME_WIDTH;
  331. // start times from zero
  332. time_offset = spect->frames[0]->time;
  333. for (ix = 0; ix < spect->numframes; ix++)
  334. spect->frames[ix]->time -= time_offset;
  335. spect->pitch1 = spect->pitchenv.pitch1;
  336. spect->pitch2 = spect->pitchenv.pitch2;
  337. spect->duration = (int)(spect->frames[spect->numframes-1]->time * 1000);
  338. if (spect->max_y < 400)
  339. spect->max_y = 200;
  340. else
  341. spect->max_y = 29000; // disable auto height scaling
  342. for (ix = 0; ix < spect->numframes; ix++) {
  343. if (spect->frames[ix]->keyframe)
  344. spect->frames[ix]->length_adjust = spect->frames[ix]->length - GetFrameLength(spect, ix);
  345. }
  346. fclose(stream);
  347. return ENS_OK;
  348. }