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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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, dif, dift, ho, hp, w;
  58. float y; // result
  59. float c[9], d[9];
  60. 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. int ix;
  88. SpectFrame *frame;
  89. frame = malloc(sizeof(SpectFrame));
  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. int 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. if (file_format_type == 2) {
  133. fread(&ix, sizeof(short), 1, stream); // spare
  134. fread(&ix, sizeof(short), 1, stream); // spare
  135. }
  136. for (ix = 0; ix < N_PEAKS; ix++) {
  137. fread(&frame->formants[ix].freq, sizeof(short), 1, stream);
  138. fread(&frame->formants[ix].bandw, sizeof(short), 1, stream);
  139. fread(&frame->peaks[ix].pkfreq, sizeof(short), 1, stream);
  140. fread(&frame->peaks[ix].pkheight, sizeof(short), 1, stream);
  141. fread(&frame->peaks[ix].pkwidth, sizeof(short), 1, stream);
  142. fread(&frame->peaks[ix].pkright, sizeof(short), 1, stream);
  143. if (frame->peaks[ix].pkheight > 0)
  144. frame->keyframe = 1;
  145. if (file_format_type == 2) {
  146. fread(&frame->peaks[ix].klt_bw, sizeof(short), 1, stream);
  147. fread(&frame->peaks[ix].klt_ap, sizeof(short), 1, stream);
  148. fread(&frame->peaks[ix].klt_bp, sizeof(short), 1, stream);
  149. }
  150. }
  151. if (file_format_type > 0) {
  152. for (ix = 0; ix < N_KLATTP2; ix++)
  153. fread(frame->klatt_param + ix, sizeof(short), 1, stream);
  154. }
  155. spect_data = malloc(sizeof(USHORT) * frame->nx);
  156. if (spect_data == NULL) {
  157. fprintf(stderr, "Failed to allocate memory\n");
  158. return 1;
  159. }
  160. frame->max_y = 0;
  161. for (ix = 0; ix < frame->nx; ix++) {
  162. fread(&x, sizeof(short), 1, stream);
  163. spect_data[ix] = x;
  164. if (x > frame->max_y) frame->max_y = x;
  165. }
  166. frame->spect = spect_data;
  167. return 0;
  168. }
  169. double GetFrameRms(SpectFrame *frame, int seq_amplitude)
  170. {
  171. int h;
  172. float total = 0;
  173. int maxh;
  174. int height;
  175. int htab[400];
  176. wavegen_peaks_t wpeaks[9];
  177. for (h = 0; h < 9; h++) {
  178. height = (frame->peaks[h].pkheight * seq_amplitude * frame->amp_adjust)/10000;
  179. wpeaks[h].height = height << 8;
  180. wpeaks[h].freq = frame->peaks[h].pkfreq << 16;
  181. wpeaks[h].left = frame->peaks[h].pkwidth << 16;
  182. wpeaks[h].right = frame->peaks[h].pkright << 16;
  183. }
  184. maxh = PeaksToHarmspect(wpeaks, 90<<16, htab, 0);
  185. for (h = 1; h < maxh; h++)
  186. total += ((htab[h] * htab[h]) >> 10);
  187. frame->rms = sqrt(total) / 7.25;
  188. return frame->rms;
  189. }
  190. SpectSeq *SpectSeqCreate()
  191. {
  192. SpectSeq *spect = malloc(sizeof(SpectSeq));
  193. spect->numframes = 0;
  194. spect->frames = NULL;
  195. spect->name = NULL;
  196. pk_select = 1;
  197. spect->grid = 1;
  198. spect->duration = 0;
  199. spect->pitch1 = 0;
  200. spect->pitch2 = 0;
  201. spect->bass_reduction = 0;
  202. spect->max_x = 3000;
  203. spect->max_y = 1;
  204. spect->file_format = 0;
  205. return spect;
  206. }
  207. void SpectSeqDestroy(SpectSeq *spect)
  208. {
  209. int ix;
  210. if (spect->frames != NULL) {
  211. for (ix = 0; ix < spect->numframes; ix++) {
  212. if (spect->frames[ix] != NULL)
  213. SpectFrameDestroy(spect->frames[ix]);
  214. }
  215. free(spect->frames);
  216. }
  217. free(spect->name);
  218. free(spect);
  219. }
  220. static float GetFrameLength(SpectSeq *spect, int frame)
  221. {
  222. int ix;
  223. float adjust = 0;
  224. if (frame >= spect->numframes-1) return 0;
  225. for (ix = frame+1; ix < spect->numframes-1; ix++) {
  226. if (spect->frames[ix]->keyframe)
  227. break; // reached next keyframe
  228. adjust += spect->frames[ix]->length_adjust;
  229. }
  230. return (spect->frames[ix]->time - spect->frames[frame]->time) * 1000.0 + adjust;
  231. }
  232. int LoadSpectSeq(SpectSeq *spect, const char *filename)
  233. {
  234. short n, temp;
  235. int ix;
  236. uint32_t id1, id2, name_len;
  237. int set_max_y = 0;
  238. float time_offset;
  239. FILE *stream = fopen(filename, "rb");
  240. if (stream == NULL) {
  241. fprintf(stderr, "Failed to open: '%s'", filename);
  242. return 0;
  243. }
  244. fread(&id1, sizeof(uint32_t), 1, stream);
  245. fread(&id2, sizeof(uint32_t), 1, stream);
  246. if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEQ))
  247. spect->file_format = 0; // eSpeak formants
  248. else if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEK))
  249. spect->file_format = 1; // formants for Klatt synthesizer
  250. else if ((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSQ2))
  251. spect->file_format = 2; // formants for Klatt synthesizer
  252. else {
  253. fprintf(stderr, "Unsupported spectral file format.\n");
  254. fclose(stream);
  255. return 1;
  256. }
  257. fread(&name_len, sizeof(uint32_t), 1, stream);
  258. if (name_len > 0) {
  259. spect->name = (char *)malloc(name_len);
  260. fread(spect->name, sizeof(char), name_len, stream);
  261. } else
  262. spect->name = NULL;
  263. fread(&n, sizeof(short), 1, stream);
  264. fread(&spect->amplitude, sizeof(short), 1, stream);
  265. fread(&spect->max_y, sizeof(short), 1, stream);
  266. fread(&temp, sizeof(short), 1, stream); // unused
  267. if (n == 0) {
  268. fclose(stream);
  269. return 0;
  270. }
  271. if (spect->frames != NULL) {
  272. for (ix = 0; ix < spect->numframes; ix++) {
  273. if (spect->frames[ix] != NULL)
  274. SpectFrameDestroy(spect->frames[ix]);
  275. }
  276. free(spect->frames);
  277. }
  278. spect->frames = malloc(sizeof(SpectFrame) * n);
  279. spect->numframes = 0;
  280. spect->max_x = 3000;
  281. if (spect->max_y == 0) {
  282. set_max_y = 1;
  283. spect->max_y = 1;
  284. }
  285. for (ix = 0; ix < n; ix++) {
  286. SpectFrame *frame = SpectFrameCreate();
  287. if (LoadFrame(frame, stream, spect->file_format) != 0) {
  288. free(frame);
  289. break;
  290. }
  291. spect->frames[spect->numframes++] = frame;
  292. if (set_max_y && (frame->max_y > spect->max_y))
  293. spect->max_y = frame->max_y;
  294. if (frame->nx * frame->dx > spect->max_x) spect->max_x = (int)(frame->nx * frame->dx);
  295. }
  296. spect->max_x = 9000; // disable auto-xscaling
  297. frame_width = (int)((FRAME_WIDTH*spect->max_x)/MAX_DISPLAY_FREQ);
  298. if (frame_width > FRAME_WIDTH) frame_width = FRAME_WIDTH;
  299. // start times from zero
  300. time_offset = spect->frames[0]->time;
  301. for (ix = 0; ix < spect->numframes; ix++)
  302. spect->frames[ix]->time -= time_offset;
  303. spect->pitch1 = spect->pitchenv.pitch1;
  304. spect->pitch2 = spect->pitchenv.pitch2;
  305. spect->duration = (int)(spect->frames[spect->numframes-1]->time * 1000);
  306. if (spect->max_y < 400)
  307. spect->max_y = 200;
  308. else
  309. spect->max_y = 29000; // disable auto height scaling
  310. for (ix = 0; ix < spect->numframes; ix++) {
  311. if (spect->frames[ix]->keyframe)
  312. spect->frames[ix]->length_adjust = spect->frames[ix]->length - GetFrameLength(spect, ix);
  313. }
  314. fclose(stream);
  315. return 0;
  316. }