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.

Sound_JSD.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Sound_JSD.c
  2. * A modification to praat (www.praat.org)
  3. * to analyse a speech sample to produce a data file "spectrum.dat" which can
  4. * be loaded into my espeakedit program to show a sequence of time-slice spectra.
  5. *
  6. * Copyright (C) 2005 Jonathan Duddington
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. * See the GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "Sound_to_PointProcess.h" // JSD
  23. #include "Sound_to_Pitch.h"
  24. #include "Pitch_to_PointProcess.h"
  25. #include "Formant.h"
  26. #include "Sound_to_Formant.h"
  27. #include "Sound_and_Spectrogram.h"
  28. #include "NUM2.h"
  29. Spectrogram Sound_to_Espeak (Sound me, double fmin, double fmax, double maxfreq, double minFreqStep1, double maxformant, double phase)
  30. // Added by JSD - testing
  31. {
  32. long pt;
  33. long i, j;
  34. long n_samples, leftSample, rightSample;
  35. int rept;
  36. int count;
  37. int best;
  38. int start, end;
  39. FILE *fout;
  40. int ny=1;
  41. double cyclen;
  42. double f0;
  43. double value;
  44. double bandw;
  45. double time;
  46. double timenext;
  47. double total;
  48. double max;
  49. double average;
  50. Spectrogram thee = NULL;
  51. Spectrogram spect = NULL;
  52. Sound sound = NULL;
  53. PointProcess points = NULL;
  54. Formant formants = NULL;
  55. Pitch pitch = Sound_to_Pitch (me, 0.8 / fmin, fmin, fmax);
  56. if (! pitch) goto error;
  57. if (! (points = Sound_Pitch_to_PointProcess_cc (me, pitch))) goto error;
  58. formants = Sound_to_Formant_burg (me, 0.00625, 5, maxformant, 0.025, 50);
  59. // make temporary sound object, 0.1 sec, for doing analysis on
  60. sound = Sound_create(1, 0.0, 0.1, 0.1/my dx, my dx, my dx/2);
  61. fout = fopen("spectrum.dat","w");
  62. if(fout != NULL)
  63. {
  64. fprintf(fout,"%d\n",points->nt-1); // num of frames
  65. }
  66. // analyse each interval in the pitch point process
  67. for(pt=1; pt < points->nt; pt++)
  68. {
  69. time = points->t[pt];
  70. if(pt < points->nt-1)
  71. timenext = points->t[pt+1];
  72. else
  73. timenext = time + cyclen; // end of pitch points, reuse last length
  74. leftSample = Sampled_xToLowIndex (me, time);
  75. rightSample = Sampled_xToLowIndex (me, timenext);
  76. n_samples = rightSample - leftSample;
  77. cyclen = timenext - time;
  78. // make sequential copies of the cycle
  79. j=1;
  80. for(rept=0; rept<5; rept++)
  81. {
  82. for(i=1; i<=n_samples && j<=sound->nx; i++)
  83. {
  84. sound->z[1][j++] = me->z[1][i+leftSample];
  85. }
  86. }
  87. if(spect != NULL) forget (spect);
  88. spect = Sound_to_Spectrogram(sound,cyclen,maxfreq,cyclen/16,
  89. minFreqStep1,kSound_to_Spectrogram_windowShape_GAUSSIAN,
  90. 8.0,8.0);
  91. if(thee == NULL)
  92. {
  93. ny = spect->ny;
  94. thee = Spectrogram_create(1,points->nt-1,points->nt-1,1,0.5,
  95. 0.0,maxfreq,ny+1,spect->dy,
  96. 0.5*spect->dy);
  97. }
  98. f0 = Pitch_getValueAtTime(pitch,time,0,1);
  99. while(f0==NUMundefined && pt<points->nt-1)
  100. {
  101. f0 = Pitch_getValueAtTime(pitch,points->t[pt+1],0,1); // ????
  102. pt++;
  103. }
  104. // extract spectrum from spectrogram
  105. if(fout != NULL)
  106. {
  107. if(spect == NULL) break;
  108. fprintf(fout,"%f %.2f %d %.2f\n",
  109. time,f0,spect->ny,spect->dy);
  110. for(count=1; count<=5; count++)
  111. {
  112. value = 0;
  113. if(formants != NULL)
  114. {
  115. value =
  116. Formant_getValueAtTime(formants,count,time,0);
  117. if(value == NUMundefined)
  118. value = 0.0;
  119. bandw =Formant_getBandwidthAtTime(formants,count,time,0);
  120. if(bandw == NUMundefined)
  121. bandw = 0.0;
  122. }
  123. fprintf(fout,"%.1f %.1f ",value,bandw);
  124. }
  125. fputc('\n',fout);
  126. // find the 'best' looking slice - largest standard deviation
  127. max = 0.0;
  128. best = 24;
  129. start = 1;
  130. if(spect->dy > 0)
  131. start = (int)(phase / spect->dy) + 1; // only use higher freq than 'phase'
  132. // end = spect->ny;
  133. end = (int)(maxformant/spect->dy)+1; // and lower than 'end'
  134. for(count=32; count<48; count++)
  135. {
  136. total = 0.0;
  137. for(i=start; i<=end; i++)
  138. total += spect->z[i][count];
  139. if(spect->ny <= start) break;
  140. average = total / (spect->ny + 1 - start);
  141. total = 0.0;
  142. for(i=start; i<=end; i++)
  143. {
  144. value = spect->z[i][count] - average;
  145. total += (value * value);
  146. }
  147. if(total > max)
  148. {
  149. max = total;
  150. best = count;
  151. }
  152. }
  153. //printf("%d: fstart=%d-%d max=%f best=%d\n",pt,start,end,max,best);
  154. for(i=1; i <= spect->ny; i++)
  155. {
  156. value = spect->z[i][best];
  157. if(value < 0) value = 0;
  158. fprintf(fout,"%f%c",sqrt(value),
  159. (i % 8)==0?'\n':' ');
  160. }
  161. fprintf(fout,"\n\n");
  162. }
  163. for(i=1; i <= ny; i++)
  164. {
  165. if(i <= spect->ny)
  166. thee->z[i][pt] = spect->z[i][best];
  167. else
  168. thee->z[i][pt] = 0;
  169. }
  170. }
  171. if(fout != NULL) fclose(fout);
  172. forget (spect);
  173. forget (pitch);
  174. forget (points);
  175. forget (sound);
  176. return thee;
  177. error:
  178. forget (pitch);
  179. return Melder_errorp ("(Sound_to_PointProcess:) Not performed.");
  180. }