progmach.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. /******** This program is called from TK/TCL to check file compatibility ******
  22. ***** and hence determine which programs will run with current files ******
  23. see progmach.tk
  24. INPUT PARAMETERS TO THIS PROGRAM ARE
  25. (a) #THESE ACCUMULATING FLAGS
  26. 1) Count of input files
  27. 2) Filetype of 1st infile
  28. 3) Filetype of current infile
  29. 4) shared- filetypes
  30. 5) Bitflag re file compatibility (16+8+4+2)
  31. # 16 = ALL INFILES OF SAME TYPE
  32. # 8 = ALL INFILES OF SAME SRATE
  33. # 4 = ALL INFILES OF SAME CHANNEL COUNT
  34. # 2 = ALL INFILES HAVE ALL OTHER PROPERTIES COMPATIBLE
  35. # 1 = AT LEAST 1 OF INFILES IS A BINARY FILE
  36. #PROPS OF 1ST FILE (ONCE ASCERTAINED)
  37. 6) srate
  38. 7) chans
  39. 8) arate
  40. 9) stype
  41. 10) origstype
  42. 11) origrate
  43. 12) Mlen
  44. 13) Dfac
  45. 14) origchans
  46. 15) specenvcnt
  47. 16) descriptor_samps
  48. b) #THE FOLLOWING PROPERTIES OF THE CURRENT INPUT FILE
  49. 17) filetype
  50. 18) srate
  51. 19) channels
  52. 20) arate
  53. 21) stype
  54. 22) origstype
  55. 23) origrate
  56. 24) Mlen
  57. 25) Dfac
  58. 26) origchans
  59. 27) specenvcnt
  60. 28) descriptor_samps
  61. ***********/
  62. #include <stdlib.h>
  63. //TW UPDATE
  64. #include <osbind.h>
  65. #include <stdio.h>
  66. #include <ctype.h>
  67. #include "filetype.h"
  68. #include <string.h>
  69. #define SAME_FILETYPE_BIT (16)
  70. #define SAME_SRATE_BIT (8)
  71. #define SAME_CHANNELS_BIT (4)
  72. #define SAME_PROPS_BIT (2)
  73. #define ONE_BINFILE_BIT (1)
  74. /* convert filetype to filetype-bit N -> 2^(N-1) */
  75. #define BRKFILE_BIT (1) /* #define BRKFILE (1) */
  76. #define DB_BRKFILE_BIT (2) /* #define DB_BRKFILE (2) */
  77. #define UNRANGED_BRKFILE_BIT (4) /* #define UNRANGED_BRKFILE (3) */
  78. #define NUMLIST_BIT (8) /* #define NUMLIST (4) */
  79. #define SNDLIST_BIT (16) /* #define SNDLIST (5) */
  80. #define SYNCLIST_BIT (32) /* #define SYNCLIST (6) */
  81. #define MIXFILE_BIT (64) /* #define MIXFILE (7) */
  82. #define LINELIST_BIT (128) /* #define LINELIST (8) */
  83. #define TEXTFILE_BIT (256) /* #define TEXTFILE (9) */
  84. #define SNDFILE_BIT (512) /* #define SNDFILE (10) */
  85. #define ANALFILE_BIT (1024) /* #define ANALFILE (11) */
  86. #define PITCHFILE_BIT (2048) /* #define PITCHFILE (12) */
  87. #define TRANSPOSFILE_BIT (4096) /* #define TRANSPOSFILE (13) */
  88. #define FORMANTFILE_BIT (8192) /* #define FORMANTFILE (14) */
  89. #define ENVFILE_BIT (16384) /* #define ENVFILE (15) */
  90. #define SPECTRAL_BITS (15460) /* All spectral type files */
  91. #define POSTANAL_BITS (14336) /* pitch, transpos & formant files */
  92. #define BINFILE_BITS (32256) /* All sndsys files: i.e. snd + spectral + envel */
  93. char errstr[400];
  94. int convert_filetype_to_bit(int filetype);
  95. int flteq(double f1,double f2);
  96. unsigned int hz1000(void);
  97. const char* cdp_version = "7.1.0";
  98. int main(int argc, char *argv[])
  99. {
  100. int filecnt, filetype, bitflag, channels;
  101. int stype, origstype, Mlen, Dfac, origchans, specenvcnt, descriptor_samps;
  102. int srate, origrate, S_filetype;
  103. double arate;
  104. //TW UPDATE
  105. unsigned int start = hz1000();
  106. int N_filetype, N_channels, N_stype, N_origstype, N_Mlen, N_Dfac;
  107. int N_origchans, N_specenvcnt, N_descriptor_samps;
  108. int N_srate, N_origrate;
  109. double N_arate;
  110. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  111. fprintf(stdout,"%s\n",cdp_version);
  112. fflush(stdout);
  113. return 0;
  114. }
  115. if(argc!=29) {
  116. fprintf(stdout,"ERROR: Bad number of arguments to progmach.\n");
  117. //TW UPDATE, as are all the following similar timer-insertions
  118. while (!(hz1000() - start))
  119. ;
  120. return -1; /* is this correct way to return an error to TK ?? Ditto below!! */
  121. }
  122. if(sscanf(argv[1],"%d",&filecnt)!=1) {
  123. fprintf(stdout,"ERROR: Bad filecnt argument to progmach.\n");
  124. while (!(hz1000() - start))
  125. ;
  126. return -1;
  127. }
  128. if(sscanf(argv[5],"%d",&bitflag)!=1) {
  129. fprintf(stdout,"ERROR: Bad bitflag argument to progmach.\n");
  130. while (!(hz1000() - start))
  131. ;
  132. return -1;
  133. }
  134. if(filecnt < 0) {
  135. fprintf(stdout,"ERROR: Bad value (%d) for filecnt in progmach.\n",filecnt);
  136. while (!(hz1000() - start))
  137. ;
  138. return -1;
  139. } else if(filecnt==0) {
  140. /* GET PARAMETERS FROM THE INFILE */
  141. if(sscanf(argv[17],"%d", &filetype)!=1) {
  142. fprintf(stdout,"ERROR: Bad filetype argument to progmach from file %d.\n",filecnt+1);
  143. while (!(hz1000() - start))
  144. ;
  145. return -1;
  146. }
  147. if(sscanf(argv[18],"%d",&srate)!=1) {
  148. fprintf(stdout,"ERROR: Bad srate argument to progmach from file %d.\n",filecnt+1);
  149. while (!(hz1000() - start))
  150. ;
  151. return -1;
  152. }
  153. if(sscanf(argv[19],"%d", &channels)!=1) {
  154. fprintf(stdout,"ERROR: Bad channels argument to progmach from file %d.\n",filecnt+1);
  155. while (!(hz1000() - start))
  156. ;
  157. return -1;
  158. }
  159. if(sscanf(argv[20],"%lf",&arate)!=1) {
  160. fprintf(stdout,"ERROR: Bad arate argument to progmach from file %d.\n",filecnt+1);
  161. while (!(hz1000() - start))
  162. ;
  163. return -1;
  164. }
  165. if(sscanf(argv[21],"%d", &stype)!=1) {
  166. fprintf(stdout,"ERROR: Bad stype argument to progmach from file %d.\n",filecnt+1);
  167. while (!(hz1000() - start))
  168. ;
  169. return -1;
  170. }
  171. if(sscanf(argv[22],"%d", &origstype)!=1) {
  172. fprintf(stdout,"ERROR: Bad origstype argument to progmach from file %d.\n",filecnt+1);
  173. while (!(hz1000() - start))
  174. ;
  175. return -1;
  176. }
  177. if(sscanf(argv[23],"%d",&origrate)!=1) {
  178. fprintf(stdout,"ERROR: Bad origrate argument to progmach from file %d.\n",filecnt+1);
  179. while (!(hz1000() - start))
  180. ;
  181. return -1;
  182. }
  183. if(sscanf(argv[24],"%d", &Mlen)!=1) {
  184. fprintf(stdout,"ERROR: Bad Mlen argument to progmach from file %d.\n",filecnt+1);
  185. while (!(hz1000() - start))
  186. ;
  187. return -1;
  188. }
  189. if(sscanf(argv[25],"%d", &Dfac)!=1) {
  190. fprintf(stdout,"ERROR: Bad Dfac argument to progmach from file %d.\n",filecnt+1);
  191. while (!(hz1000() - start))
  192. ;
  193. return -1;
  194. }
  195. if(sscanf(argv[26],"%d", &origchans)!=1) {
  196. fprintf(stdout,"ERROR: Bad origchans argument to progmach from file %d.\n",filecnt+1);
  197. while (!(hz1000() - start))
  198. ;
  199. return -1;
  200. }
  201. if(sscanf(argv[27],"%d", &specenvcnt)!=1) {
  202. fprintf(stdout,"ERROR: Bad specenvcnt argument to progmach from file %d.\n",filecnt+1);
  203. while (!(hz1000() - start))
  204. ;
  205. return -1;
  206. }
  207. if(sscanf(argv[28],"%d", &descriptor_samps)!=1) {
  208. fprintf(stdout,"ERROR: Bad descriptor_samps argument to progmach from file %d.\n",filecnt+1);
  209. while (!(hz1000() - start))
  210. ;
  211. return -1;
  212. }
  213. N_filetype = filetype; /* current filetype = first filetype */
  214. S_filetype = filetype; /* shared filetype = first filetype */
  215. } else { /* NOT THE FIRST FILE */
  216. /* GET PARAMETERS FROM THE CUMULATIVE FLAG */
  217. if(sscanf(argv[2],"%d",&filetype)!=1) {
  218. fprintf(stdout,"ERROR: Bad file-0-type argument to progmach.\n");
  219. while (!(hz1000() - start))
  220. ;
  221. return -1;
  222. }
  223. if(sscanf(argv[4],"%d",&S_filetype)!=1) {
  224. fprintf(stdout,"ERROR: Bad shared-filetypes argument to progmach.\n");
  225. while (!(hz1000() - start))
  226. ;
  227. return -1;
  228. }
  229. if(sscanf(argv[6], "%d",&srate)!=1) {
  230. fprintf(stdout,"ERROR: Bad file_0 sample-rate argument to progmach.\n");
  231. while (!(hz1000() - start))
  232. ;
  233. return -1;
  234. }
  235. if(sscanf(argv[7], "%d",&channels)!=1) {
  236. fprintf(stdout,"ERROR: Bad file_0 channels argument to progmach.\n");
  237. while (!(hz1000() - start))
  238. ;
  239. return -1;
  240. }
  241. if(sscanf(argv[8], "%lf",&arate)!=1) {
  242. fprintf(stdout,"ERROR: Bad file_0 analysis-rate argument to progmach.\n");
  243. while (!(hz1000() - start))
  244. ;
  245. return -1;
  246. }
  247. if(sscanf(argv[9], "%d",&stype)!=1) {
  248. fprintf(stdout,"ERROR: Bad file_0 sample-type argument to progmach.\n");
  249. while (!(hz1000() - start))
  250. ;
  251. return -1;
  252. }
  253. if(sscanf(argv[10], "%d",&origstype)!=1) {
  254. fprintf(stdout,"ERROR: Bad file_0 original-sample-type argument to progmach.\n");
  255. while (!(hz1000() - start))
  256. ;
  257. return -1;
  258. }
  259. if(sscanf(argv[11],"%d",&origrate)!=1) {
  260. fprintf(stdout,"ERROR: Bad file_0 original-sample-rate argument to progmach.\n");
  261. while (!(hz1000() - start))
  262. ;
  263. return -1;
  264. }
  265. if(sscanf(argv[12],"%d",&Mlen)!=1) {
  266. fprintf(stdout,"ERROR: Bad file_0 Mlen argument to progmach.\n");
  267. while (!(hz1000() - start))
  268. ;
  269. return -1;
  270. }
  271. if(sscanf(argv[13],"%d",&Dfac)!=1) {
  272. fprintf(stdout,"ERROR: Bad file_0 decimation-factor argument to progmach.\n");
  273. while (!(hz1000() - start))
  274. ;
  275. return -1;
  276. }
  277. if(sscanf(argv[14],"%d",&origchans)!=1) {
  278. fprintf(stdout,"ERROR: Bad file_0 original-channels argument to progmach.\n");
  279. while (!(hz1000() - start))
  280. ;
  281. return -1;
  282. }
  283. if(sscanf(argv[15],"%d",&specenvcnt)!=1) {
  284. fprintf(stdout,"ERROR: Bad file_0 specenvcnt argument to progmach.\n");
  285. while (!(hz1000() - start))
  286. ;
  287. return -1;
  288. }
  289. if(sscanf(argv[16],"%d",&descriptor_samps)!=1) {
  290. fprintf(stdout,"ERROR: Bad file_0 descriptor_samps argument to progmach.\n");
  291. while (!(hz1000() - start))
  292. ;
  293. return -1;
  294. }
  295. /* THEN GET PARAMETERS FROM THE INFILE */
  296. if(sscanf(argv[17],"%d", &N_filetype)!=1) {
  297. fprintf(stdout,"ERROR: Bad file %d filetype argument to progmach.\n",filecnt+1);
  298. while (!(hz1000() - start))
  299. ;
  300. return -1;
  301. }
  302. /**
  303. N_filetype = convert_filetype_to_bit(N_filetype);
  304. **/
  305. if(sscanf(argv[18],"%d",&N_srate)!=1) {
  306. fprintf(stdout,"ERROR: Bad file %d sample-rate argument to progmach.\n",filecnt+1);
  307. while (!(hz1000() - start))
  308. ;
  309. return -1;
  310. }
  311. if(sscanf(argv[19],"%d", &N_channels)!=1) {
  312. fprintf(stdout,"ERROR: Bad file %d channels argument to progmach.\n",filecnt+1);
  313. while (!(hz1000() - start))
  314. ;
  315. return -1;
  316. }
  317. if(sscanf(argv[20],"%lf",&N_arate)!=1) {
  318. fprintf(stdout,"ERROR: Bad file %d analysis-rate argument to progmach.\n",filecnt+1);
  319. while (!(hz1000() - start))
  320. ;
  321. return -1;
  322. }
  323. if(sscanf(argv[21],"%d", &N_stype)!=1) {
  324. fprintf(stdout,"ERROR: Bad file %d sample-type argument to progmach.\n",filecnt+1);
  325. while (!(hz1000() - start))
  326. ;
  327. return -1;
  328. }
  329. if(sscanf(argv[22],"%d", &N_origstype)!=1) {
  330. fprintf(stdout,"ERROR: Bad file %d original-sample-type argument to progmach.\n",filecnt+1);
  331. while (!(hz1000() - start))
  332. ;
  333. return -1;
  334. }
  335. if(sscanf(argv[23],"%d",&N_origrate)!=1) {
  336. fprintf(stdout,"ERROR: Bad file %d original-sample-rate argument to progmach.\n",filecnt+1);
  337. while (!(hz1000() - start))
  338. ;
  339. return -1;
  340. }
  341. if(sscanf(argv[24],"%d", &N_Mlen)!=1) {
  342. fprintf(stdout,"ERROR: Bad file %d Mlen argument to progmach.\n",filecnt+1);
  343. while (!(hz1000() - start))
  344. ;
  345. return -1;
  346. }
  347. if(sscanf(argv[25],"%d", &N_Dfac)!=1) {
  348. fprintf(stdout,"ERROR: Bad file %d decimation-factor argument to progmach.\n",filecnt+1);
  349. while (!(hz1000() - start))
  350. ;
  351. return -1;
  352. }
  353. if(sscanf(argv[26],"%d", &N_origchans)!=1) {
  354. fprintf(stdout,"ERROR: Bad file %d original-channels argument to progmach.\n",filecnt+1);
  355. while (!(hz1000() - start))
  356. ;
  357. return -1;
  358. }
  359. if(sscanf(argv[27],"%d", &N_specenvcnt)!=1) {
  360. fprintf(stdout,"ERROR: Bad file %d specenvcnt argument to progmach.\n",filecnt+1);
  361. while (!(hz1000() - start))
  362. ;
  363. return -1;
  364. }
  365. if(sscanf(argv[28],"%d", &N_descriptor_samps)!=1) {
  366. fprintf(stdout,"ERROR: Bad file %d descriptor_samps argument to progmach.\n",filecnt+1);
  367. while (!(hz1000() - start))
  368. ;
  369. return -1;
  370. }
  371. /* TEST THE NEW FILE AGAINST THE FIRST FILE */
  372. if(bitflag & SAME_FILETYPE_BIT) { /* if not same filetype */
  373. if(N_filetype & IS_A_SNDSYSTEM_FILE) {
  374. if(N_filetype!=filetype) {
  375. bitflag &= (~SAME_FILETYPE_BIT); /* unset SAME_FILETYPE_BIT */
  376. }
  377. } else if((S_filetype &= N_filetype)==0) /* generate shared-filetypes */
  378. bitflag &= (~SAME_FILETYPE_BIT); /* unset SAME_FILETYPE_BIT */
  379. }
  380. /* if not same srate */
  381. if((bitflag & SAME_SRATE_BIT) && N_srate!=srate)
  382. bitflag &= (~SAME_SRATE_BIT); /* unset SAME_SRATE_BIT */
  383. if(bitflag & SAME_CHANNELS_BIT) {
  384. /* if 1 file is analfile, and other is anal-derived */
  385. /* compare channels with origchans: if not equal */
  386. if((filetype == IS_AN_ANALFILE) && ((N_filetype & IS_A_PTF_BINFILE)>ANALFILE)) {
  387. if(channels != N_origchans)
  388. bitflag &= (~SAME_CHANNELS_BIT); /* unset SAME_CHANNELS_BIT */
  389. } else if(((filetype & IS_A_PTF_BINFILE) > ANALFILE) && (N_filetype == IS_AN_ANALFILE)) {
  390. if(N_channels != origchans)
  391. bitflag &= (~SAME_CHANNELS_BIT); /* simil */
  392. } else if(channels != N_channels) /* else simply compare channel-count, & if not equal */
  393. bitflag &= (~SAME_CHANNELS_BIT); /* unset SAME_CHANNELS_BIT */
  394. }
  395. if(bitflag & SAME_PROPS_BIT) {
  396. /* If NOT (an analfile + a derived-from-anal file) */
  397. if(!((filetype == IS_AN_ANALFILE) && ((N_filetype & IS_A_PTF_BINFILE)>ANALFILE))
  398. && !(((filetype & IS_A_PTF_BINFILE)>ANALFILE) && (N_filetype == IS_AN_ANALFILE))
  399. && origchans != N_origchans) /* compare origchans, and if not equal.. */
  400. bitflag &= (~SAME_PROPS_BIT); /* unset SAME_PROPS_BIT */
  401. /* If both are formants files */
  402. /* & formant properties don't tally...*/
  403. if((filetype == IS_A_FORMANTFILE) && (N_filetype == IS_A_FORMANTFILE)
  404. &&((specenvcnt != N_specenvcnt) || (descriptor_samps != N_descriptor_samps)))
  405. bitflag &= (~SAME_PROPS_BIT); /* unset SAME_PROPS_BIT */
  406. if(filetype != IS_A_SNDFILE) {
  407. if((!flteq(arate,N_arate)) /* compare all other properties */
  408. //TW JUNE 2004: stype compatibility is no longer relevant: all internal calcs are floating point!!
  409. /* || (origstype != N_origstype) */
  410. || (origrate != N_origrate)
  411. || (Mlen != N_Mlen)
  412. || (Dfac != N_Dfac)
  413. //TW JUNE 2004: stype compatibility is no longer relevant: all internal calcs are floating point!!
  414. /* || (stype != N_stype) */)
  415. bitflag &= (~SAME_PROPS_BIT); /* and if they don't tally. unset SAME_PROPS_BIT */
  416. }
  417. }
  418. }
  419. /* ARE THERE ANY BINARY P or T FILES, SO FAR */
  420. /* Strictly speaking I'm being overlax in allowing binary Formant files here */
  421. /* but as there are no non-binary formant-files, no need to test for them!! */
  422. if(!(bitflag & ONE_BINFILE_BIT)) {
  423. if((N_filetype & IS_A_PTF_BINFILE)>ANALFILE) /* IF current file is a binary filetype */
  424. bitflag |= ONE_BINFILE_BIT; /* set (at-least-one-binary-file)-BIT */
  425. }
  426. /* INCREMENT FILECNT */
  427. filecnt++;
  428. /* PRINT OUT THE CUMULATIVE FLAG */
  429. fprintf(stdout,"%d %d %d %d %d %d %d %lf %d %d %d %d %d %d %d %d\n",
  430. filecnt,filetype,N_filetype,S_filetype,bitflag,srate,channels,arate,stype,
  431. origstype,origrate,Mlen,Dfac,origchans,specenvcnt,descriptor_samps);
  432. return 0;
  433. }
  434. /**************************** FLTEQ *******************************/
  435. #define FLTERR (0.000002)
  436. int flteq(double f1,double f2)
  437. {
  438. double upperbnd, lowerbnd;
  439. upperbnd = f2 + FLTERR;
  440. lowerbnd = f2 - FLTERR;
  441. if((f1>upperbnd) || (f1<lowerbnd))
  442. return(0);
  443. return(1);
  444. }