tcontphase.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*---------------------------------------------------------------------------*\
  2. FILE........: tcontphase.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 11/9/09
  5. Test program for developing continuous phase track synthesis algorithm.
  6. However while developing this it was discovered that synthesis_mixed()
  7. worked just as well.
  8. \*---------------------------------------------------------------------------*/
  9. /*
  10. Copyright (C) 2009 David Rowe
  11. All rights reserved.
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU Lesser General Public License version 2, as
  14. published by the Free Software Foundation. This program is
  15. distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. License for more details.
  19. You should have received a copy of the GNU Lesser General Public License
  20. along with this program; if not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #define N 80 /* frame size */
  23. #define F 160 /* frames to synthesis */
  24. #define P 10 /* LPC order */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <math.h>
  29. #include "sine.h"
  30. #include "dump.h"
  31. #include "synth.h"
  32. #include "phase.h"
  33. int frames;
  34. float ak[] = {
  35. 1.000000,
  36. -1.455836,
  37. 1.361841,
  38. -0.879267,
  39. 0.915985,
  40. -1.002202,
  41. 0.944103,
  42. -0.743094,
  43. 1.053356,
  44. -0.817491,
  45. 0.431222
  46. };
  47. /*---------------------------------------------------------------------------*\
  48. switch_present()
  49. Searches the command line arguments for a "switch". If the switch is
  50. found, returns the command line argument where it ws found, else returns
  51. NULL.
  52. \*---------------------------------------------------------------------------*/
  53. int switch_present(sw,argc,argv)
  54. char sw[]; /* switch in string form */
  55. int argc; /* number of command line arguments */
  56. char *argv[]; /* array of command line arguments in string form */
  57. {
  58. int i; /* loop variable */
  59. for(i=1; i<argc; i++)
  60. if (!strcmp(sw,argv[i]))
  61. return(i);
  62. return 0;
  63. }
  64. /*---------------------------------------------------------------------------*\
  65. MAIN
  66. \*---------------------------------------------------------------------------*/
  67. int main(argc,argv)
  68. int argc;
  69. char *argv[];
  70. {
  71. FILE *fout;
  72. short buf[N];
  73. int i,j;
  74. int dump;
  75. float phi_prev[MAX_AMP];
  76. float Wo_prev, ex_phase, G;
  77. //float ak[P+1];
  78. COMP H[MAX_AMP];
  79. float f0;
  80. if (argc < 3) {
  81. printf("\nusage: %s OutputRawSpeechFile F0\n", argv[0]);
  82. exit(1);
  83. }
  84. /* Output file */
  85. if ((fout = fopen(argv[1],"wb")) == NULL) {
  86. printf("Error opening output speech file: %s\n",argv[1]);
  87. exit(1);
  88. }
  89. f0 = atof(argv[2]);
  90. dump = switch_present("--dump",argc,argv);
  91. if (dump)
  92. dump_on(argv[dump+1]);
  93. init_decoder();
  94. for(i=0; i<MAX_AMP; i++)
  95. phi_prev[i] = 0.0;
  96. Wo_prev = 0.0;
  97. model.Wo = PI*(f0/4000.0);
  98. G = 1000.0;
  99. model.L = floor(PI/model.Wo);
  100. //aks_to_H(&model, ak, G , H, P);
  101. //for(i=1; i<=model.L; i++)
  102. model.A[i] = sqrt(H[i].real*H[i].real + H[i].imag*H[i].imag);
  103. //printf("L = %d\n", model.L);
  104. //model.L = 10;
  105. for(i=1; i<=model.L; i++) {
  106. model.A[i] = 1000/model.L;
  107. model.phi[i] = 0;
  108. H[i].real = 1.0; H[i].imag = 0.0;
  109. }
  110. //ak[0] = 1.0;
  111. //for(i=1; i<=P; i++)
  112. // ak[i] = 0.0;
  113. frames = 0;
  114. for(j=0; j<F; j++) {
  115. frames++;
  116. #ifdef SWAP
  117. /* lets make phases bounce around from frame to frame. This
  118. could happen if H[m] is varying, for example due to frame
  119. to frame Wo variations, or non-stationary speech.
  120. Continous model generally results in smooth phase track
  121. under these circumstances. */
  122. if (j%2){
  123. H[1].real = 1.0; H[1].imag = 0.0;
  124. model.phi[1] = 0.0;
  125. }
  126. else {
  127. H[1].real = 0.0; H[1].imag = 1.0;
  128. model.phi[1] = PI/2;
  129. }
  130. #endif
  131. //#define CONT
  132. #ifdef CONT
  133. synthesise_continuous_phase(Pn, &model, Sn_, 1, &Wo_prev, phi_prev);
  134. #else
  135. phase_synth_zero_order(5.0, H, &Wo_prev, &ex_phase);
  136. synthesise_mixed(Pn,&model,Sn_,1);
  137. #endif
  138. for(i=0; i<N; i++)
  139. buf[i] = Sn_[i];
  140. fwrite(buf,sizeof(short),N,fout);
  141. }
  142. fclose(fout);
  143. if (dump) dump_off();
  144. return 0;
  145. }