pmodify.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /* PMODIFY
  22. *
  23. * Open pitch text data file and write to pitch binary data file
  24. * Close file.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <osbind.h>
  30. #include <math.h>
  31. #include <float.h>
  32. #include <float.h>
  33. #include <sfsys.h>
  34. #include <cdplib.h>
  35. static int headwrite(int ofd,int origchans,int origstype,int origrate,float arate,int Mlen,int Dfac);
  36. const char* cdp_version = "7.1.0";
  37. int main(int argc,char *argv[])
  38. {
  39. int samps_written;
  40. size_t bufsize, buflen, n;
  41. float *bigfbuf;
  42. int ofd;
  43. int origchans, origstype, origrate;
  44. float arate;
  45. int Mlen, Dfac;
  46. int srate, chans;
  47. double val;
  48. FILE *fp;
  49. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  50. fprintf(stdout,"%s\n",cdp_version);
  51. fflush(stdout);
  52. return 0;
  53. }
  54. if(argc != 11) {
  55. fprintf(stdout,"ERROR: Incorrect call to program which writes the pitch data.\n");
  56. fflush(stdout);
  57. return 1;
  58. }
  59. /* initialise SFSYS */
  60. if( sflinit("pdisplay") < 0 ) {
  61. fprintf(stdout,"ERROR: Cannot initialise soundfile system.\n");
  62. fflush(stdout);
  63. return 1;
  64. }
  65. /* open input file */
  66. if((fp = fopen(argv[1],"r"))==NULL) {
  67. fprintf(stdout,"INFO: Cannot open temporary data file: %s\n",argv[1]);
  68. fflush(stdout);
  69. // sffinish();
  70. }
  71. //TW output is analysis file
  72. // if((ofd = sndcreat(argv[2], -1, SAMP_FLOAT)) < 0) {
  73. // fprintf(stdout,"ERROR: Can't create output file %s (It may already exist)\n", argv[2]);
  74. // fflush(stdout);
  75. // sffinish();
  76. // return 1;
  77. // }
  78. if(sscanf(argv[3],"%d",&origchans)!=1) {
  79. fprintf(stdout,"ERROR: Cannot read original-channels data,\n");
  80. fflush(stdout);
  81. // sffinish();
  82. return 1;
  83. }
  84. if(sscanf(argv[4],"%d",&origstype)!=1) {
  85. fprintf(stdout,"ERROR: Cannot read original-sample-type data,\n");
  86. fflush(stdout);
  87. // sffinish();
  88. return 1;
  89. }
  90. if(sscanf(argv[5],"%d",&origrate)!=1) {
  91. fprintf(stdout,"ERROR: Cannot read original-sample-rate data,\n");
  92. fflush(stdout);
  93. // sffinish();
  94. return 1;
  95. }
  96. if(sscanf(argv[6],"%f",&arate)!=1) {
  97. fprintf(stdout,"ERROR: Cannot read analysis-rate data,\n");
  98. fflush(stdout);
  99. // sffinish();
  100. return 1;
  101. }
  102. if(sscanf(argv[7],"%d",&Mlen)!=1) {
  103. fprintf(stdout,"ERROR: Cannot read Mlen data,\n");
  104. fflush(stdout);
  105. // sffinish();
  106. return 1;
  107. }
  108. if(sscanf(argv[8],"%d",&Dfac)!=1) {
  109. fprintf(stdout,"ERROR: Cannot read Decimation Factor data,\n");
  110. fflush(stdout);
  111. // sffinish();
  112. return 1;
  113. }
  114. if(sscanf(argv[9],"%d",&srate)!=1) {
  115. fprintf(stdout,"ERROR: Cannot read sample-rate data,\n");
  116. fflush(stdout);
  117. // sffinish();
  118. return 1;
  119. }
  120. if(sscanf(argv[10],"%d",&chans)!=1) {
  121. fprintf(stdout,"ERROR: Cannot read channel data,\n");
  122. fflush(stdout);
  123. // sffinish();
  124. return 1;
  125. }
  126. if((ofd = sndcreat_formatted(argv[2], -1, SAMP_FLOAT,
  127. chans,srate,CDP_CREATE_NORMAL)) < 0) {
  128. fprintf(stdout,"ERROR: Cannot open output file\n");
  129. fflush(stdout);
  130. // sffinish();
  131. return 1;
  132. }
  133. bufsize = (size_t) Malloc(-1);
  134. if((bigfbuf=(float*)Malloc(bufsize+sizeof(float))) == NULL) {
  135. fprintf(stdout,"ERROR: Failed to allocate float buffer.\n");
  136. fflush(stdout);
  137. // sffinish();
  138. return 1;
  139. }
  140. n = ((size_t)bigfbuf+sizeof(float)-1)/sizeof(float) * sizeof(float); /* align bigfbuf to word boundary */
  141. bigfbuf = (float*)n;
  142. buflen = bufsize/sizeof(float);
  143. n = 0;
  144. while (fscanf(fp,"%lf",&val)==1) {
  145. bigfbuf[n++] = (float)val;
  146. if(n>=buflen) {
  147. if((samps_written = fputfbufEx(bigfbuf,(int) buflen,ofd))<0) {
  148. fprintf(stdout,"ERROR: Can't write to output soundfile: (is hard-disk full?).\n");
  149. fflush(stdout);
  150. Mfree(bigfbuf);
  151. sndcloseEx(ofd);
  152. return 1;
  153. }
  154. n = 0;
  155. }
  156. }
  157. if(n > 0) {
  158. if((samps_written = fputfbufEx(bigfbuf,(int)n,ofd))<0) {
  159. fprintf(stdout,"ERROR: Can't write to output soundfile: (is hard-disk full?).\n");
  160. fflush(stdout);
  161. Mfree(bigfbuf);
  162. sndcloseEx(ofd);
  163. return 1;
  164. }
  165. }
  166. fclose(fp);
  167. if (!headwrite(ofd,origchans,origstype,origrate,arate,Mlen,Dfac)) {
  168. fprintf(stdout,"ERROR: Failed to write valid header to output file.\n");
  169. fflush(stdout);
  170. sndunlink(ofd);
  171. }
  172. Mfree(bigfbuf);
  173. sndcloseEx(ofd);
  174. // sffinish();
  175. return 1;
  176. }
  177. /***************************** HEADWRITE *******************************/
  178. int headwrite(int ofd,int origchans,int origstype,int origrate,float arate,int Mlen,int Dfac)
  179. {
  180. int property_marker = 1;
  181. if(sndputprop(ofd,"orig channels", (char *)&origchans, sizeof(int)) < 0){
  182. fprintf(stdout,"ERROR: Failure to write original channel data: headwrite()\n");
  183. fflush(stdout);
  184. return(0);
  185. }
  186. if(sndputprop(ofd,"original sampsize", (char *)&origstype, sizeof(int)) < 0){
  187. fprintf(stdout,"ERROR: Failure to write original sample size. headwrite()\n");
  188. fflush(stdout);
  189. return(0);
  190. }
  191. if(sndputprop(ofd,"original sample rate", (char *)&origrate, sizeof(int)) < 0){
  192. fprintf(stdout,"ERROR: Failure to write original sample rate. headwrite()\n");
  193. fflush(stdout);
  194. return(0);
  195. }
  196. if(sndputprop(ofd,"arate",(char *)&arate,sizeof(float)) < 0){
  197. fprintf(stdout,"ERROR: Failed to write analysis sample rate. headwrite()\n");
  198. fflush(stdout);
  199. return(0);
  200. }
  201. if(sndputprop(ofd,"analwinlen",(char *)&Mlen,sizeof(int)) < 0){
  202. fprintf(stdout,"ERROR: Failure to write analysis window length. headwrite()\n");
  203. fflush(stdout);
  204. return(0);
  205. }
  206. if(sndputprop(ofd,"decfactor",(char *)&Dfac,sizeof(int)) < 0){
  207. fprintf(stdout,"ERROR: Failure to write decimation factor. headwrite()\n");
  208. fflush(stdout);
  209. return(0);
  210. }
  211. if(sndputprop(ofd,"is a pitch file", (char *)&property_marker, sizeof(int)) < 0){
  212. fprintf(stdout,"ERROR: Failure to write pitch property: headwrite()\n");
  213. fflush(stdout);
  214. return(0);
  215. }
  216. return(1);
  217. }