tinterp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*---------------------------------------------------------------------------*\
  2. FILE........: tinterp.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 22/8/10
  5. Tests interpolation functions.
  6. \*---------------------------------------------------------------------------*/
  7. /*
  8. Copyright (C) 2010 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. #include <assert.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include <ctype.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include "defines.h"
  30. #include "sine.h"
  31. #include "interp.h"
  32. void make_amp(MODEL *model, float f0, float cdB, float mdBHz)
  33. {
  34. int i;
  35. float mdBrad = mdBHz*FS/TWO_PI;
  36. model->Wo = f0*TWO_PI/FS;
  37. model->L = PI/model->Wo;
  38. for(i=0; i<=model->L; i++)
  39. model->A[i] = pow(10.0,(cdB + (float)i*model->Wo*mdBrad)/20.0);
  40. model->voiced = 1;
  41. }
  42. void write_amp(char file[], MODEL *model)
  43. {
  44. FILE *f;
  45. int i;
  46. f = fopen(file,"wt");
  47. for(i=1; i<=model->L; i++)
  48. fprintf(f, "%f\t%f\n", model->Wo*i, model->A[i]);
  49. fclose(f);
  50. }
  51. const char *get_next_float(const char *s, float *num)
  52. {
  53. const char *p = s;
  54. char tmp[MAX_STR];
  55. while(*p && !isspace(*p))
  56. p++;
  57. assert((p-s) < (int)(sizeof(tmp)-1));
  58. memcpy(tmp, s, p-s);
  59. tmp[p-s] = 0;
  60. *num = atof(tmp);
  61. return p+1;
  62. }
  63. const char *get_next_int(const char *s, int *num)
  64. {
  65. const char *p = s;
  66. char tmp[MAX_STR];
  67. while(*p && !isspace(*p))
  68. p++;
  69. assert((p-s) < (int)(sizeof(tmp)-1));
  70. memcpy(tmp, s, p-s);
  71. tmp[p-s] = 0;
  72. *num = atoi(tmp);
  73. return p+1;
  74. }
  75. void load_amp(MODEL *model, const char * file, int frame)
  76. {
  77. FILE *f;
  78. int i;
  79. char s[1024];
  80. const char *ps;
  81. f = fopen(file,"rt");
  82. assert(f);
  83. for(i=0; i<frame; i++)
  84. ps = fgets(s, 1023, f);
  85. /// can frame ever be 0? what if fgets fails?
  86. ps = s;
  87. ps = get_next_float(ps, &model->Wo);
  88. ps = get_next_int(ps, &model->L);
  89. for(i=1; i<=model->L; i++)
  90. ps = get_next_float(ps, &model->A[i]);
  91. fclose(f);
  92. }
  93. void load_or_make_amp(MODEL *model,
  94. const char * filename, int frame,
  95. float f0, float cdB, float mdBHz)
  96. {
  97. struct stat buf;
  98. int rc = stat(filename, &buf);
  99. if (rc || !S_ISREG(buf.st_mode) || ((buf.st_mode & S_IRUSR) != S_IRUSR))
  100. {
  101. make_amp(model, f0, cdB, mdBHz);
  102. }
  103. else
  104. {
  105. load_amp(model, filename, frame);
  106. }
  107. }
  108. int main() {
  109. MODEL prev, next, interp;
  110. load_or_make_amp(&prev,
  111. "../src/hts1a_model.txt", 32,
  112. 50.0, 60.0, 6E-3);
  113. load_or_make_amp(&next,
  114. "../src/hts1a_model.txt", 34,
  115. 50.0, 40.0, 6E-3);
  116. interp.voiced = 1;
  117. interpolate(&interp, &prev, &next);
  118. write_amp("tinterp_prev.txt", &prev);
  119. write_amp("tinterp_interp.txt", &interp);
  120. write_amp("tinterp_next.txt", &next);
  121. return 0;
  122. }