vuform.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System 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 Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <math.h>
  24. #include <sfsys.h>
  25. #include <string.h>
  26. /* RWD March 2012 changed sndsize to sndsizeEx */
  27. /* RWD Oct 2025 these symbols also now used in Windows*/
  28. #define FFAILED (-1)
  29. #define FSUCCEEDED (0)
  30. static int readfhead(int ifd);
  31. const char* cdp_version = "7.1.0";
  32. int main(int argc, char *argv[])
  33. {
  34. int ifd;
  35. FILE *fp;
  36. int specenvcnt, k, len;
  37. char *filename, out[64], *p, *q;
  38. float *buf;
  39. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  40. fprintf(stdout,"%s\n",cdp_version);
  41. fflush(stdout);
  42. return 0;
  43. }
  44. if(argc!=2) {
  45. fprintf(stdout,"ERROR: Insufficient params:: vuform formantfile.\n");
  46. fflush(stdout);
  47. return(FFAILED);
  48. }
  49. if((ifd = sndopenEx(argv[1],0,CDP_OPEN_RDONLY)) < 0) {
  50. fprintf(stdout,"ERROR: Failure to open file %s for input.\n",argv[1]);
  51. fflush(stdout);
  52. return(FFAILED);
  53. }
  54. len = strlen(argv[1]);
  55. if((filename = (char *)malloc((len+1) * sizeof(char)))==NULL) {
  56. fprintf(stdout,"ERROR: Failure to allocate memory 1.\n");
  57. fflush(stdout);
  58. return(FFAILED);
  59. }
  60. strcpy(filename,argv[1]);
  61. p = filename;
  62. q = filename + len;
  63. while(p < q) {
  64. if(*p == '.')
  65. break;
  66. p++;
  67. }
  68. p--;
  69. *p++ = '1';
  70. *p++ = '.';
  71. *p++ = 't';
  72. *p++ = 'x';
  73. *p++ = 't';
  74. k = sndsizeEx(ifd);
  75. if(k <= 0) {
  76. fprintf(stdout,"ERROR: No data in file\n");
  77. fflush(stdout);
  78. return(FFAILED);
  79. }
  80. if((specenvcnt = readfhead(ifd)) < 0)
  81. return(FFAILED);
  82. specenvcnt *= 3;
  83. if(k < specenvcnt) {
  84. fprintf(stdout,"ERROR: Too little data in file\n");
  85. fflush(stdout);
  86. return(FFAILED);
  87. }
  88. if((buf = (float *)malloc(specenvcnt * sizeof(float)))==NULL) {
  89. fprintf(stdout,"ERROR: Failure to allocate memory 2.\n");
  90. fflush(stdout);
  91. return(FFAILED);
  92. }
  93. if(fgetfbufEx(buf,specenvcnt,ifd,0) < 0) {
  94. fprintf(stdout,"ERROR: Can't read samples from file.\n");
  95. fflush(stdout);
  96. return(FFAILED);
  97. }
  98. sndcloseEx(ifd);
  99. if((fp = fopen(filename,"w"))==NULL) {
  100. fprintf(stdout,"ERROR: Can't open outputfile\n");
  101. fflush(stdout);
  102. return(FFAILED);
  103. }
  104. specenvcnt /= 3;
  105. sprintf(out,"%d\n",specenvcnt);
  106. fputs(out,fp);
  107. for(k = specenvcnt;k < specenvcnt * 3;k++) {
  108. if(k % specenvcnt == 0)
  109. fputs("\n",fp);
  110. sprintf(out,"%.12lf\n",buf[k]);
  111. fputs(out,fp);
  112. }
  113. fclose(fp);
  114. return(FSUCCEEDED);
  115. }
  116. int readfhead(int ifd)
  117. {
  118. int isf;
  119. SFPROPS props = {0};
  120. if(!snd_headread(ifd,&props)) {
  121. fprintf(stdout,"ERROR: Failure to read properties\n");
  122. fflush(stdout);
  123. return(-1);
  124. }
  125. if(sndgetprop(ifd,"is a formant file",(char *) &isf,sizeof(int)) < 0) {
  126. fprintf(stdout,"ERROR: Not a formant file.\n");
  127. fflush(stdout);
  128. return(-1);
  129. }
  130. return props.specenvcnt;
  131. }