copysf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (c) 1983-2023 Richard Dobson and Composers Desktop Project Ltd
  3. * http://people.bath.ac.uk/masrwd
  4. * http://www.composersdesktop.com
  5. * This file is part of the CDP System.
  6. * The CDP System is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  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.
  14. * See the 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 02111-1307 USA
  18. *
  19. */
  20. /*
  21. * copysfx: version of copysfx using portsf
  22. */
  23. /* Oct 2009: added cube */
  24. /* Dec 2009 1.9.1. fixed horrible bug failing to convert to WAVE_EX! */
  25. /* March 2012 corrected wave type arg parsing */
  26. /* August 2012 updated portsf to correct SPKRS_MONO */
  27. /* Nov 2013 added MC_SURR_6_1 */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #ifdef _WIN32
  32. #include <malloc.h> //RWD.6.5.99
  33. #endif
  34. #include <string.h>
  35. #include <math.h>
  36. #include <time.h>
  37. #include <sys/types.h>
  38. #include <sys/timeb.h>
  39. #include <portsf.h>
  40. #define VERSION "Revision: 2.1.1 2020 "
  41. #ifdef unix
  42. /* in portsf.lib */
  43. extern int stricmp(const char *a, const char *b);
  44. #endif
  45. enum {OPT_STD_WAVE,OPT_WAVEX_GENERIC,OPT_WAVEX,OPT_WAVEX_LCRS,OPT_WAVEX_SURROUND,OPT_WAVEX_BFORMAT,OPT_WAVEX_5_0,OPT_WAVEX_7_1,OPT_WAVEX_CUBE,OPT_WAVEX_6_1,};
  46. /*
  47. The number of channels defines the order of the soundfield:
  48. 3 channel = h = 1st order horizontal
  49. 4 channel = f = 1st order 3-D
  50. 5 channel = hh = 2nd order horizontal
  51. 6 channel = fh = 2nd order horizontal + 1st order height (formerly
  52. called 2.5 order)
  53. 7 channel = hhh = 3rd order horizontal
  54. 8 channel = fhh = 3rd order horizontal + 1st order height
  55. 9 channel = ff = 2nd order 3-D
  56. 11 channel = ffh = 3rd order horizontal + 2nd order height
  57. 16 channel = fff = 3rd order 3-D
  58. */
  59. #define N_BFORMATS (10)
  60. static const int bformats[N_BFORMATS] = {2,3,4,5,6,7,8,9,11,16};
  61. int get_sampsize(psf_stype type)
  62. {
  63. int size = 0;
  64. switch(type){
  65. case PSF_SAMP_8:
  66. size = 1;
  67. break;
  68. case PSF_SAMP_16:
  69. size = 2;
  70. break;
  71. case PSF_SAMP_24:
  72. size = 3;
  73. break;
  74. case PSF_SAMP_32:
  75. case PSF_SAMP_IEEE_FLOAT:
  76. size = 4;
  77. break;
  78. default:
  79. break;
  80. }
  81. return size;
  82. }
  83. void
  84. usage()
  85. {
  86. fprintf(stderr,"\n\nCDP MCTOOLS: COPYSFX (c) RWD,CDP %s\n",VERSION);
  87. fprintf(stderr,"\tcopy/convert soundfiles\n");
  88. fprintf(stderr,"usage: copysfx [-d][-h][-sN] [-tN] infile outfile\n");
  89. fprintf(stderr,"-d : apply TPDF dither to 16bit outfile.\n"
  90. "-s : force output sample type to type N.\n");
  91. fprintf(stderr," Available sample types:\n"
  92. " 1 : 16bit integers (shorts)\n"
  93. " 2 : 32bit integer (longs)\n"
  94. " 3 : 32bit floating-point\n"
  95. " 4 : 24bit integer 'packed' \n"
  96. " Default: format of infile.\n");
  97. fprintf(stderr,"-h : write mimimum header (no extra data).\n"
  98. " Default: include PEAK data.\n"
  99. );
  100. /* fprintf(stderr,"-i : interpret 32bit files as floats (use with care!)\n"); */
  101. fprintf(stderr,"-tN : write outfile format as type N\n");
  102. fprintf(stderr,"Possible formats:\n"
  103. "0 : standard soundfile (.wav, .aif, .afc, .aifc)\n"
  104. "1 : generic WAVE_EX (no speaker assignments)\n"
  105. "2 : WAVE_EX mono/stereo/quad(LF,RF,LR,RR) - infile nchans must match\n"
  106. "3 : WAVE_EX quad surround (L,C,R,S) - infile must be quad\n"
  107. "4 : WAVE_EX 5.1 format surround - infile must be 6-channel\n"
  108. "5 : WAVE_EX Ambisonic B-format (W,X,Y,Z...) - file extension .amb supported \n"
  109. "6 : WAVE_EX 5.0 Surround - infile must be 5-channel\n"
  110. "7 : WAVE_EX 7.1 Surround - infile must be 8-channel\n"
  111. "8 : WAVE_EX Cube Surround - infile must be 8-channel\n"
  112. "9 : WAVE_EX 6.1 Surround (new in v2.1.0) - infile must be 7-channel\n"
  113. "default in all cases: outfile has format of infile\n"
  114. "NB: types 1 to 9 are for WAV format only\n"
  115. "See also CHXFORMAT: destructively change WAVE_EX GUID and/or speaker mask.\n"
  116. );
  117. }
  118. int
  119. main(int argc, char *argv[])
  120. {
  121. unsigned long size,i;
  122. unsigned long channels,srate;
  123. //RWD.6.5.99
  124. PSF_PROPS inprops,outprops;
  125. int force_wave = 0,rc = 0;
  126. psf_stype force_stype = 0;
  127. int opt_wave_type = -1;
  128. int min_header = 0;
  129. int interpret_floats = 0;
  130. int res;
  131. char *create_msg = NULL;
  132. int do_dither = 0;
  133. psf_format outformat = PSF_FMT_UNKNOWN;
  134. unsigned long update_size;
  135. PSF_CHPEAK *peaks = NULL;
  136. MYLONG peaktime;
  137. char* ext = NULL;
  138. int have_amb_ext = 0;
  139. int ifd; /* the input file */
  140. int ofd; /* the output file */
  141. float *sampleframe = NULL;
  142. double outsize_bytes;
  143. double maxsize = pow(2.0,32.0) - 200.0; /* some safety margin! */
  144. /* CDP version number */
  145. if(argc==2 && (stricmp(argv[1],"--version")==0)){
  146. printf("2.0.3\n");
  147. return 0;
  148. }
  149. if(psf_init()) {
  150. printf("failed to init psfsys\n");
  151. exit(1);
  152. }
  153. if(argc < 3){
  154. usage();
  155. exit(1);
  156. }
  157. printf("\nCDP MCTOOLS: COPYSFX v2.0.3 (c) RWD,CDP 2011\n");
  158. while(argv[1][0]=='-'){
  159. int sflag = 0;
  160. switch(argv[1][1]){
  161. case 'd':
  162. do_dither = 1;
  163. break;
  164. case('t'):
  165. if(argv[1][2]=='\0'){
  166. fprintf(stderr,"-t flag requires parameter\n");
  167. usage();
  168. exit(1);
  169. }
  170. opt_wave_type = atoi(&(argv[1][2]));
  171. if((opt_wave_type < 0) || opt_wave_type > OPT_WAVEX_CUBE){
  172. fprintf(stderr,"wave type out of range\n");
  173. usage();
  174. exit(1);
  175. }
  176. force_wave = 1;
  177. break;
  178. case('s'):
  179. if(argv[1][2]=='\0'){
  180. fprintf(stderr,"-s flag requires parameter\n");
  181. usage();
  182. exit(1);
  183. }
  184. if(force_stype >0){
  185. fprintf(stderr,"copysfx: error: -s flag may only be used once!\n");
  186. usage();
  187. exit(1);
  188. }
  189. sflag = atoi(&(argv[1][2]));
  190. if(sflag < 1 || sflag > 6){
  191. fprintf(stderr,"-s parameter out of range\n");
  192. usage();
  193. exit(1);
  194. }
  195. switch(sflag){
  196. case(1):
  197. force_stype = PSF_SAMP_16;
  198. break;
  199. case(2):
  200. force_stype = PSF_SAMP_32;
  201. break;
  202. case(3):
  203. force_stype = PSF_SAMP_IEEE_FLOAT;
  204. break;
  205. case(4):
  206. force_stype = PSF_SAMP_24;
  207. break;
  208. default:
  209. fprintf(stderr,"internal error: unmatched sampletype parameter!\n");
  210. break;
  211. }
  212. break;
  213. case('h'):
  214. min_header = 1;
  215. break;
  216. #ifdef NOTDEF
  217. case('i'):
  218. interpret_floats = 1;
  219. break;
  220. #endif
  221. default:
  222. fprintf(stderr,"unknown flag option %s\n",argv[1]);
  223. usage();
  224. exit(1);
  225. }
  226. argc--; argv++;
  227. }
  228. if(argc < 3){
  229. usage();
  230. exit(1);
  231. }
  232. if((ifd = psf_sndOpen(argv[1],&inprops,0)) < 0) {
  233. fprintf(stderr, "copysfx: can't open input SFfile %s:\n\t",
  234. argv[1]);
  235. exit(1);
  236. }
  237. size = psf_sndSize(ifd);
  238. channels = inprops.chans;
  239. sampleframe = (float *) malloc(channels * sizeof(float));
  240. if(sampleframe==NULL){
  241. psf_sndClose(ifd);
  242. puts("no memory for sample buffer\n");
  243. exit(1);
  244. }
  245. srate = inprops.srate;
  246. update_size = (unsigned long)((double)srate * 0.5);
  247. outprops = inprops;
  248. /* if -i flag used, infile is floats, therefore we write outfile as floats,
  249. * UNLESS we also have outtype coercion!
  250. */
  251. if(interpret_floats)
  252. outprops.samptype = PSF_SAMP_IEEE_FLOAT;
  253. if(inprops.format== PSF_WAVE_EX){
  254. printf("opened WAVE-EX file:\n");
  255. switch(inprops.chformat){
  256. case(MC_STD):
  257. printf("\tno speaker assignments defined\n");
  258. break;
  259. case(MC_MONO):
  260. printf("\tMONO\n");
  261. break;
  262. case(MC_STEREO):
  263. printf("\tSTEREO\n");
  264. break;
  265. case(MC_QUAD):
  266. printf("\tGeneric quad format: LF-RF-LR-RR\n");
  267. break;
  268. case(MC_LCRS):
  269. printf("\tQuad Surround: L-C-R-S format\n");
  270. break;
  271. case(MC_BFMT):
  272. printf("\tAmbisonic B-Format\n");
  273. break;
  274. case(MC_DOLBY_5_1):
  275. printf("\t5.1 surround\n");
  276. break;
  277. case(MC_SURR_5_0):
  278. printf("\t5.0 surround\n");
  279. break;
  280. case(MC_SURR_6_1):
  281. printf("\t6.1 Surround\n");
  282. break;
  283. case(MC_SURR_7_1):
  284. printf("\t7.1 Surround\n");
  285. break;
  286. case(MC_CUBE):
  287. printf("\tCube Surround\n");
  288. break;
  289. default:
  290. printf("\tunrecognized speaker positions\n");
  291. break;
  292. }
  293. if(min_header==1)
  294. printf("WARNING: minimum header is not recommended for WAVE_EX files\n");
  295. }
  296. peaks = (PSF_CHPEAK *) calloc(channels,sizeof(PSF_CHPEAK));
  297. if(peaks==NULL){
  298. puts("no memory for fpeak data buffer\n");
  299. psf_sndClose(ifd);
  300. exit(1);
  301. }
  302. //read infile peak data, if it exists, and report to user
  303. res = psf_sndReadPeaks(ifd,peaks, &peaktime);
  304. if(res ==0) {
  305. printf("no peak data in this infile\n");
  306. }
  307. else if(res < 0){
  308. fprintf(stderr,"error reading infile peak data\n");
  309. psf_sndClose(ifd);
  310. exit(1);
  311. }
  312. else {
  313. time_t t_peaktime = (time_t) peaktime;
  314. printf("Infile PEAK data:\n\tcreation time: %s\n", ctime(&t_peaktime));
  315. for(i=0;i < channels; i++)
  316. #ifdef CPLONG64
  317. printf("CH %ld: %.4lf at frame %u:\t%.4lf secs\n",
  318. #else
  319. printf("(32) CH %ld: %.4lf at frame %lu:\t%.4lf secs\n",
  320. #endif
  321. i,peaks[i].val,peaks[i].pos,(double) (peaks[i].pos) / (double)srate);
  322. }
  323. if(force_stype > PSF_SAMP_UNKNOWN)
  324. outprops.samptype = force_stype;
  325. outsize_bytes = (double) size * outprops.chans * get_sampsize(outprops.samptype);
  326. if(outsize_bytes > maxsize){
  327. printf("output file size %.0f MB exceeds 4GB: cannot proceed.\n",outsize_bytes / 1048576.0);
  328. exit(1);
  329. }
  330. if(force_wave){
  331. int i,matched;
  332. //check file extension...
  333. switch(opt_wave_type){
  334. case(OPT_STD_WAVE):
  335. outprops.chformat = STDWAVE;
  336. outprops.format = PSF_STDWAVE;
  337. create_msg = "creating standard WAVE file\n";
  338. break;
  339. case(OPT_WAVEX_GENERIC):
  340. inprops.chformat = MC_STD;
  341. outprops.format = PSF_WAVE_EX;
  342. create_msg = "creating STD WAVE_EX file\n";
  343. break;
  344. case(OPT_WAVEX):
  345. switch(inprops.chans){
  346. case(1):
  347. outprops.chformat = MC_MONO;
  348. outprops.format = PSF_WAVE_EX;
  349. create_msg = "creating MONO WAVE_EX file\n";
  350. break;
  351. case(2):
  352. outprops.chformat = MC_STEREO;
  353. outprops.format = PSF_WAVE_EX;
  354. create_msg = "creating STEREO WAVE_EX file\n";
  355. break;
  356. case(4):
  357. outprops.chformat = MC_QUAD;
  358. outprops.format = PSF_WAVE_EX;
  359. create_msg = "creating QUAD WAVE_EX file\n";
  360. break;
  361. default:
  362. fprintf(stderr,"infile nchans incompatible with requested WAVE-EX format\n");
  363. usage();
  364. psf_sndClose(ifd);
  365. if(peaks)
  366. free(peaks);
  367. exit(1);
  368. }
  369. break;
  370. case(OPT_WAVEX_LCRS):
  371. if(inprops.chans != 4){
  372. fprintf(stderr,"infile must have four channels\n");
  373. usage();
  374. psf_sndClose(ifd);
  375. if(peaks)
  376. free(peaks);
  377. exit(1);
  378. }
  379. outprops.chformat = MC_LCRS;
  380. outprops.format = PSF_WAVE_EX;
  381. create_msg = "creating LCRS-surround WAVE_EX file\n";
  382. break;
  383. case(OPT_WAVEX_SURROUND):
  384. if(inprops.chans != 6){
  385. fprintf(stderr,"infile must have six channels\n");
  386. usage();
  387. psf_sndClose(ifd);
  388. if(peaks)
  389. free(peaks);
  390. exit(1);
  391. }
  392. outprops.chformat = MC_DOLBY_5_1;
  393. outprops.format = PSF_WAVE_EX;
  394. create_msg = "creating 5.1 surround WAVE_EX file\n";
  395. break;
  396. case(OPT_WAVEX_BFORMAT):
  397. matched = 0;
  398. for(i=0;i < N_BFORMATS;i++) {
  399. if(inprops.chans == bformats[i]){
  400. matched = 1;
  401. break;
  402. }
  403. }
  404. if(!matched){
  405. printf("WARNING: No Bformat definition for %d-channel file.\n",inprops.chans);
  406. }
  407. outprops.chformat = MC_BFMT;
  408. outprops.format = inprops.format = PSF_WAVE_EX;
  409. create_msg = "creating AMBISONIC B-FORMAT WAVE_EX file\n";
  410. break;
  411. case(OPT_WAVEX_5_0):
  412. if(inprops.chans != 5){
  413. fprintf(stderr,"infile must have five channels\n");
  414. usage();
  415. psf_sndClose(ifd);
  416. if(peaks)
  417. free(peaks);
  418. exit(1);
  419. }
  420. outprops.chformat = MC_SURR_5_0;
  421. outprops.format = PSF_WAVE_EX;
  422. create_msg = "creating 5.0 surround WAVE_EX file\n";
  423. break;
  424. case(OPT_WAVEX_7_1):
  425. if(inprops.chans != 8){
  426. fprintf(stderr,"infile must have eight channels\n");
  427. usage();
  428. psf_sndClose(ifd);
  429. if(peaks)
  430. free(peaks);
  431. exit(1);
  432. }
  433. outprops.chformat = MC_SURR_7_1;
  434. outprops.format = PSF_WAVE_EX;
  435. create_msg = "creating 7.1 surround WAVE_EX file\n";
  436. break;
  437. case OPT_WAVEX_CUBE:
  438. if(inprops.chans != 8){
  439. fprintf(stderr,"infile must have eight channels\n");
  440. usage();
  441. psf_sndClose(ifd);
  442. if(peaks)
  443. free(peaks);
  444. exit(1);
  445. }
  446. outprops.chformat = MC_CUBE;
  447. outprops.format = PSF_WAVE_EX;
  448. create_msg = "creating 5.0 surround WAVE_EX file\n";
  449. break;
  450. case(OPT_WAVEX_6_1):
  451. if(inprops.chans != 7){
  452. fprintf(stderr,"infile must have seven channels\n");
  453. usage();
  454. psf_sndClose(ifd);
  455. if(peaks)
  456. free(peaks);
  457. exit(1);
  458. }
  459. outprops.chformat = MC_SURR_6_1;
  460. outprops.format = PSF_WAVE_EX;
  461. create_msg = "creating 6.1 surround WAVE_EX file\n";
  462. break;
  463. default:
  464. printf("copysfx: Program error: impossible wave_ex type\n");
  465. psf_sndClose(ifd);
  466. if(peaks)
  467. free(peaks);
  468. exit(1);
  469. }
  470. }
  471. //ignore all that if user wants aiff!
  472. //informat = inprops.format;
  473. ext = strrchr(argv[2],'.');
  474. if(ext && stricmp(ext,".amb")==0)
  475. have_amb_ext = 1;
  476. if(have_amb_ext){
  477. if(!(outprops.format == PSF_WAVE_EX && outprops.chformat == MC_BFMT)){
  478. fprintf(stderr,"Error: .amb extension only allowed for WAVE_EX B-Format file.\n");
  479. exit(1);
  480. }
  481. }
  482. outformat = psf_getFormatExt(argv[2]);
  483. if((ofd = psf_sndCreate(argv[2],&outprops,0,min_header,PSF_CREATE_RDWR)) < 0){
  484. fprintf(stderr, "copysfx: can't create output file %s:\n\t",argv[2]);
  485. psf_sndClose(ifd);
  486. fprintf(stderr,"\n");
  487. if(peaks)
  488. free(peaks);
  489. exit(1);
  490. }
  491. if(do_dither){
  492. psf_sndSetDither(ofd,PSF_DITHER_TPDF);
  493. }
  494. if(force_wave){
  495. if(outprops.format==PSF_WAVE_EX){
  496. if(outformat > PSF_WAVE_EX)
  497. printf("WARNING: extended formats require .wav file format:\n\t - creating standard file\n");
  498. else
  499. printf("%s\n",create_msg);
  500. }
  501. }
  502. printf("copying...\n");
  503. for(i=0;i < size;i++){
  504. /* salve to CEP users: need interpret_floats somewhere...? */
  505. if((psf_sndReadFloatFrames(ifd, sampleframe, 1)) != 1){
  506. fprintf(stderr,"copysfx: error reading from infile\n");
  507. psf_sndClose(ifd);
  508. psf_sndClose(ofd);
  509. free(sampleframe);
  510. if(peaks)
  511. free(peaks);
  512. exit(1);
  513. }
  514. if(psf_sndWriteFloatFrames(ofd, sampleframe,1)!=1){
  515. fprintf(stderr,"copysfx: error writing to outfile\n");
  516. psf_sndClose(ifd);
  517. psf_sndClose(ofd);
  518. free(sampleframe);
  519. if(peaks)
  520. free(peaks);
  521. exit(1);
  522. }
  523. if((i / channels) % update_size == 0) {
  524. printf("%.2lf secs\r",(double) (i / channels) / (double) srate);
  525. fflush(stdout);
  526. }
  527. }
  528. printf("%.4lf secs\r",(double) (i / channels) / (double) srate);
  529. if(psf_sndReadPeaks( ofd,peaks,&peaktime)){
  530. printf("Outfile PEAK values:\n");
  531. for(i=0; i < (unsigned long) inprops.chans; i++){
  532. double val, dbval;
  533. val = (double) peaks[i].val;
  534. if(val > 0.0){
  535. dbval = 20.0 * log10(val);
  536. #ifdef CPLONG64
  537. printf("CH %ld: %.6f (%.2lfdB) at frame %u:\t%.4f secs\n",i,
  538. #else
  539. printf("CH %ld: %.6f (%.2lfdB) at frame %lu:\t%.4f secs\n",i,
  540. #endif
  541. val,dbval,peaks[i].pos,(double)peaks[i].pos / (double) inprops.srate);
  542. }
  543. else{
  544. #ifdef CPLONG64
  545. printf("CH %ld: %.6f (-infdB) at frame %u:\t%.4f secs\n",i,
  546. #else
  547. printf("CH %ld: %.6f (-infdB) at frame %lu:\t%.4f secs\n",i,
  548. #endif
  549. val,peaks[i].pos,(double)peaks[i].pos / (double) inprops.srate);
  550. }
  551. }
  552. }
  553. if(psf_sndClose(ifd) < 0) {
  554. rc++;
  555. }
  556. if(psf_sndClose(ofd) < 0) {
  557. rc++;
  558. }
  559. if(sampleframe)
  560. free(sampleframe);
  561. if(peaks)
  562. free(peaks);
  563. psf_finish();
  564. return rc;
  565. }