tnlp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*---------------------------------------------------------------------------*\
  2. FILE........: tnlp.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 23/3/93
  5. Test program for non linear pitch estimation functions.
  6. \*---------------------------------------------------------------------------*/
  7. /*
  8. Copyright (C) 2009 David Rowe
  9. All rights reserved.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU Lesser General Public License version 2, as
  12. published by the Free Software Foundation. This program is
  13. distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. License for more details.
  17. You should have received a copy of the GNU Lesser General Public License
  18. along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define N 80 /* frame size */
  21. #define M 320 /* pitch analysis window size */
  22. #define PITCH_MIN 20
  23. #define PITCH_MAX 160
  24. #define TNLP
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <math.h>
  29. #include "defines.h"
  30. #include "dump.h"
  31. #include "sine.h"
  32. #include "nlp.h"
  33. #include "kiss_fft.h"
  34. int frames;
  35. /*---------------------------------------------------------------------------*\
  36. switch_present()
  37. Searches the command line arguments for a "switch". If the switch is
  38. found, returns the command line argument where it ws found, else returns
  39. NULL.
  40. \*---------------------------------------------------------------------------*/
  41. int switch_present(sw,argc,argv)
  42. char sw[]; /* switch in string form */
  43. int argc; /* number of command line arguments */
  44. char *argv[]; /* array of command line arguments in string form */
  45. {
  46. int i; /* loop variable */
  47. for(i=1; i<argc; i++)
  48. if (!strcmp(sw,argv[i]))
  49. return(i);
  50. return 0;
  51. }
  52. /*---------------------------------------------------------------------------*\
  53. MAIN
  54. \*---------------------------------------------------------------------------*/
  55. int main(argc,argv)
  56. int argc;
  57. char *argv[];
  58. {
  59. FILE *fin,*fout;
  60. short buf[N];
  61. float Sn[M]; /* float input speech samples */
  62. kiss_fft_cfg fft_fwd_cfg;
  63. COMP Sw[FFT_ENC]; /* DFT of Sn[] */
  64. float w[M]; /* time domain hamming window */
  65. COMP W[FFT_ENC]; /* DFT of w[] */
  66. float pitch;
  67. int i;
  68. float prev_Wo;
  69. void *nlp_states;
  70. #ifdef DUMP
  71. int dump;
  72. #endif
  73. if (argc < 3) {
  74. printf("\nusage: tnlp InputRawSpeechFile OutputPitchTextFile "
  75. "[--dump DumpFile]\n");
  76. exit(1);
  77. }
  78. /* Input file */
  79. if ((fin = fopen(argv[1],"rb")) == NULL) {
  80. printf("Error opening input speech file: %s\n",argv[1]);
  81. exit(1);
  82. }
  83. /* Output file */
  84. if ((fout = fopen(argv[2],"wt")) == NULL) {
  85. printf("Error opening output text file: %s\n",argv[2]);
  86. exit(1);
  87. }
  88. #ifdef DUMP
  89. dump = switch_present("--dump",argc,argv);
  90. if (dump)
  91. dump_on(argv[dump+1]);
  92. #else
  93. /// TODO
  94. /// #warning "Compile with -DDUMP if you expect to dump anything."
  95. #endif
  96. nlp_states = nlp_create();
  97. fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL);
  98. make_analysis_window(fft_fwd_cfg, w, W);
  99. frames = 0;
  100. prev_Wo = 0;
  101. while(fread(buf,sizeof(short),N,fin)) {
  102. printf("%d\n", frames++);
  103. /* Update input speech buffers */
  104. for(i=0; i<M-N; i++)
  105. Sn[i] = Sn[i+N];
  106. for(i=0; i<N; i++)
  107. Sn[i+M-N] = buf[i];
  108. dft_speech(fft_fwd_cfg, Sw, Sn, w);
  109. #ifdef DUMP
  110. dump_Sn(Sn); dump_Sw(Sw);
  111. #endif
  112. nlp(nlp_states,Sn,N,M,PITCH_MIN,PITCH_MAX,&pitch,Sw,W, &prev_Wo);
  113. prev_Wo = TWO_PI/pitch;
  114. fprintf(fout,"%f\n",pitch);
  115. }
  116. fclose(fin);
  117. fclose(fout);
  118. #ifdef DUMP
  119. if (dump) dump_off();
  120. #endif
  121. nlp_destroy(nlp_states);
  122. return 0;
  123. }