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.3KB

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