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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <stdio.h>
  22. #include <stdint.h>
  23. #include <espeak-ng/espeak_ng.h>
  24. #include <espeak/speak_lib.h>
  25. #include "speech.h"
  26. #include "phoneme.h"
  27. #include "synthesize.h"
  28. #include "voice.h"
  29. #include "spect.h"
  30. #include <math.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. extern double ConvertFromIeeeExtended(unsigned char *bytes);
  34. extern int PeaksToHarmspect(wavegen_peaks_t *peaks, int pitch, int *htab, int control);
  35. extern unsigned char pk_shape1[];
  36. extern int pk_select;
  37. extern char voice_name[];
  38. static int frame_width;
  39. int pk_select;
  40. static int default_freq[N_PEAKS] =
  41. { 200, 500, 1200, 3000, 3500, 4000, 6900, 7800, 9000 };
  42. static int default_width[N_PEAKS] =
  43. { 750, 500, 550, 550, 600, 700, 700, 700, 700 };
  44. static int default_klt_bw[N_PEAKS] =
  45. { 89, 90, 140, 260, 260, 260, 500, 500, 500 };
  46. static double read_double(FILE *stream)
  47. {
  48. unsigned char bytes[10];
  49. fread(bytes, sizeof(char), 10, stream);
  50. return ConvertFromIeeeExtended(bytes);
  51. }
  52. float polint(float xa[], float ya[], int n, float x)
  53. {
  54. // General polinomial interpolation routine, xa[1...n] ya[1...n]
  55. int i, m, ns = 1;
  56. float den, dif, dift, ho, hp, w;
  57. float y; // result
  58. float c[9], d[9];
  59. dif = fabs(x-xa[1]);
  60. for (i = 1; i <= n; i++) {
  61. if ((dift = fabs(x-xa[i])) < dif) {
  62. ns = i;
  63. dif = dift;
  64. }
  65. c[i] = ya[i];
  66. d[i] = ya[i];
  67. }
  68. y = ya[ns--];
  69. for (m = 1; m < n; m++) {
  70. for (i = 1; i <= n-m; i++) {
  71. ho = xa[i]-x;
  72. hp = xa[i+m]-x;
  73. w = c[i+1]-d[i];
  74. if ((den = ho-hp) == 0.0)
  75. return ya[2]; // two input xa are identical
  76. den = w/den;
  77. d[i] = hp*den;
  78. c[i] = ho*den;
  79. }
  80. y += ((2*ns < (n-m) ? c[ns+1] : d[ns--]));
  81. }
  82. return y;
  83. }
  84. static SpectFrame *SpectFrameCreate()
  85. {
  86. int ix;
  87. SpectFrame *frame;
  88. frame = malloc(sizeof(SpectFrame));
  89. if (!frame)
  90. return NULL;
  91. frame->keyframe = 0;
  92. frame->spect = NULL;
  93. frame->markers = 0;
  94. frame->pitch = 0;
  95. frame->nx = 0;
  96. frame->time = 0;
  97. frame->length = 0;
  98. frame->amp_adjust = 100;
  99. frame->length_adjust = 0;
  100. for (ix = 0; ix < N_PEAKS; ix++) {
  101. frame->formants[ix].freq = 0;
  102. frame->peaks[ix].pkfreq = default_freq[ix];
  103. frame->peaks[ix].pkheight = 0;
  104. frame->peaks[ix].pkwidth = default_width[ix];
  105. frame->peaks[ix].pkright = default_width[ix];
  106. frame->peaks[ix].klt_bw = default_klt_bw[ix];
  107. frame->peaks[ix].klt_ap = 0;
  108. frame->peaks[ix].klt_bp = default_klt_bw[ix];
  109. }
  110. memset(frame->klatt_param, 0, sizeof(frame->klatt_param));
  111. frame->klatt_param[KLATT_AV] = 59;
  112. frame->klatt_param[KLATT_Kopen] = 40;
  113. return frame;
  114. }
  115. static void SpectFrameDestroy(SpectFrame *frame)
  116. {
  117. if (frame->spect != NULL)
  118. free(frame->spect);
  119. free(frame);
  120. }
  121. int LoadFrame(SpectFrame *frame, FILE *stream, int file_format_type)
  122. {
  123. short ix;
  124. short x;
  125. unsigned short *spect_data;
  126. frame->time = read_double(stream);
  127. frame->pitch = read_double(stream);
  128. frame->length = read_double(stream);
  129. frame->dx = read_double(stream);
  130. fread(&frame->nx, sizeof(short), 1, stream);
  131. fread(&frame->markers, sizeof(short), 1, stream);
  132. fread(&frame->amp_adjust, sizeof(short), 1, stream);
  133. if (file_format_type == 2) {
  134. fread(&ix, sizeof(short), 1, stream); // spare
  135. fread(&ix, sizeof(short), 1, stream); // spare
  136. }
  137. for (ix = 0; ix < N_PEAKS; ix++) {
  138. fread(&frame->formants[ix].freq, sizeof(short), 1, stream);
  139. fread(&frame->formants[ix].bandw, sizeof(short), 1, stream);
  140. fread(&frame->peaks[ix].pkfreq, sizeof(short), 1, stream);
  141. fread(&frame->peaks[ix].pkheight, sizeof(short), 1, stream);
  142. fread(&frame->peaks[ix].pkwidth, sizeof(short), 1, stream);
  143. fread(&frame->peaks[ix].pkright, sizeof(short), 1, stream);
  144. if (frame->peaks[ix].pkheight > 0)
  145. frame->keyframe = 1;
  146. if (file_format_type == 2) {
  147. fread(&frame->peaks[ix].klt_bw, sizeof(short), 1, stream);
  148. fread(&frame->peaks[ix].klt_ap, sizeof(short), 1, stream);
  149. fread(&frame->peaks[ix].klt_bp, sizeof(short), 1, stream);
  150. }
  151. }
  152. if (file_format_type > 0) {
  153. for (ix = 0; ix < N_KLATTP2; ix++)
  154. fread(frame->klatt_param + ix, sizeof(short), 1, stream);
  155. }
  156. spect_data = malloc(sizeof(USHORT) * frame->nx);
  157. if (spect_data == NULL) {
  158. fprintf(stderr, "Failed to allocate memory\n");
  159. return 1;
  160. }
  161. frame->max_y = 0;
  162. for (ix = 0; ix < frame->nx; ix++) {
  163. fread(&x, sizeof(short), 1, stream);
  164. spect_data[ix] = x;
  165. if (x > frame->max_y) frame->max_y = x;
  166. }
  167. frame->spect = spect_data;
  168. return 0;
  169. }
  170. double GetFrameRms(SpectFrame *frame, int seq_amplitude)
  171. {
  172. int h;
  173. float total = 0;
  174. int maxh;
  175. int height;
  176. int htab[400];
  177. wavegen_peaks_t wpeaks[9];
  178. for (h = 0; h < 9; h++) {
  179. height = (frame->peaks[h].pkheight * seq_amplitude * frame->amp_adjust)/10000;
  180. wpeaks[h].height = height << 8;
  181. wpeaks[h].freq = frame->peaks[h].pkfreq << 16;
  182. wpeaks[h].left = frame->peaks[h].pkwidth << 16;
  183. wpeaks[h].right = frame->peaks[h].pkright << 16;
  184. }
  185. maxh = PeaksToHarmspect(wpeaks, 90<<16, htab, 0);
  186. for (h = 1; h < maxh; h++)
  187. total += ((htab[h] * htab[h]) >> 10);
  188. frame->rms = sqrt(total) / 7.25;
  189. return frame->rms;
  190. }
  191. SpectSeq *SpectSeqCreate()
  192. {
  193. SpectSeq *spect = malloc(sizeof(SpectSeq));
  194. if (!spect)
  195. return NULL;
  196. spect->numframes = 0;
  197. spect->frames = NULL;
  198. spect->name = NULL;
  199. pk_select = 1;
  200. spect->grid = 1;
  201. spect->duration = 0;
  202. spect->pitch1 = 0;
  203. spect->pitch2 = 0;
  204. spect->bass_reduction = 0;
  205. spect->max_x = 3000;
  206. spect->max_y = 1;
  207. spect->file_format = 0;
  208. return spect;
  209. }
  210. void SpectSeqDestroy(SpectSeq *spect)
  211. {
  212. int ix;
  213. if (spect->frames != NULL) {
  214. for (ix = 0; ix < spect->numframes; ix++) {
  215. if (spect->frames[ix] != NULL)
  216. SpectFrameDestroy(spect->frames[ix]);
  217. }
  218. free(spect->frames);
  219. }
  220. free(spect->name);
  221. free(spect);
  222. }
  223. static float GetFrameLength(SpectSeq *spect, int frame)
  224. {
  225. int ix;
  226. float adjust = 0;
  227. if (frame >= spect->numframes-1) return 0;
  228. for (ix = frame+1; ix < spect->numframes-1; ix++) {
  229. if (spect->frames[ix]->keyframe)
  230. break; // reached next keyframe
  231. adjust += spect->frames[ix]->length_adjust;
  232. }
  233. return (spect->frames[ix]->time - spect->frames[frame]->time) * 1000.0 + adjust;
  234. }
  235. espeak_ng_STATUS LoadSpectSeq(SpectSeq *spect, const char *filename)
  236. {
  237. short n, temp;
  238. int ix;
  239. uint32_t id1, id2, name_len;
  240. int set_max_y = 0;
  241. float time_offset;
  242. FILE *stream = fopen(filename, "rb");
  243. if (stream == NULL) {
  244. fprintf(stderr, "Failed to open: '%s'", filename);
  245. return errno;
  246. }
  247. fread(&id1, sizeof(uint32_t), 1, stream);
  248. fread(&id2, sizeof(uint32_t), 1, stream);
  249. if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEQ))
  250. spect->file_format = 0; // eSpeak formants
  251. else if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEK))
  252. spect->file_format = 1; // formants for Klatt synthesizer
  253. else if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSQ2))
  254. spect->file_format = 2; // formants for Klatt synthesizer
  255. else {
  256. fprintf(stderr, "Unsupported spectral file format.\n");
  257. fclose(stream);
  258. return ENS_UNSUPPORTED_PHON_FORMAT;
  259. }
  260. fread(&name_len, sizeof(uint32_t), 1, stream);
  261. if (name_len > 0) {
  262. spect->name = (char *)malloc(name_len);
  263. fread(spect->name, sizeof(char), name_len, stream);
  264. } else
  265. spect->name = NULL;
  266. fread(&n, sizeof(short), 1, stream);
  267. fread(&spect->amplitude, sizeof(short), 1, stream);
  268. fread(&spect->max_y, sizeof(short), 1, stream);
  269. fread(&temp, sizeof(short), 1, stream); // unused
  270. if (n == 0) {
  271. fclose(stream);
  272. return ENS_NO_SPECT_FRAMES;
  273. }
  274. if (spect->frames != NULL) {
  275. for (ix = 0; ix < spect->numframes; ix++) {
  276. if (spect->frames[ix] != NULL)
  277. SpectFrameDestroy(spect->frames[ix]);
  278. }
  279. free(spect->frames);
  280. }
  281. spect->frames = calloc(n, sizeof(SpectFrame *));
  282. spect->numframes = 0;
  283. spect->max_x = 3000;
  284. if (spect->max_y == 0) {
  285. set_max_y = 1;
  286. spect->max_y = 1;
  287. }
  288. for (ix = 0; ix < n; ix++) {
  289. SpectFrame *frame = SpectFrameCreate();
  290. if (!frame)
  291. return ENOMEM;
  292. if (LoadFrame(frame, stream, spect->file_format) != 0) {
  293. free(frame);
  294. break;
  295. }
  296. spect->frames[spect->numframes++] = frame;
  297. if (set_max_y && (frame->max_y > spect->max_y))
  298. spect->max_y = frame->max_y;
  299. if (frame->nx * frame->dx > spect->max_x) spect->max_x = (int)(frame->nx * frame->dx);
  300. }
  301. spect->max_x = 9000; // disable auto-xscaling
  302. frame_width = (int)((FRAME_WIDTH*spect->max_x)/MAX_DISPLAY_FREQ);
  303. if (frame_width > FRAME_WIDTH) frame_width = FRAME_WIDTH;
  304. // start times from zero
  305. time_offset = spect->frames[0]->time;
  306. for (ix = 0; ix < spect->numframes; ix++)
  307. spect->frames[ix]->time -= time_offset;
  308. spect->pitch1 = spect->pitchenv.pitch1;
  309. spect->pitch2 = spect->pitchenv.pitch2;
  310. spect->duration = (int)(spect->frames[spect->numframes-1]->time * 1000);
  311. if (spect->max_y < 400)
  312. spect->max_y = 200;
  313. else
  314. spect->max_y = 29000; // disable auto height scaling
  315. for (ix = 0; ix < spect->numframes; ix++) {
  316. if (spect->frames[ix]->keyframe)
  317. spect->frames[ix]->length_adjust = spect->frames[ix]->length - GetFrameLength(spect, ix);
  318. }
  319. fclose(stream);
  320. return ENS_OK;
  321. }