panorama.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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 <structures.h>
  24. #include <tkglobals.h>
  25. #include <pnames.h>
  26. #include <filetype.h>
  27. #include <processno.h>
  28. #include <modeno.h>
  29. #include <logic.h>
  30. #include <globcon.h>
  31. #include <cdpmain.h>
  32. #include <math.h>
  33. #include <mixxcon.h>
  34. #include <osbind.h>
  35. #include <standalone.h>
  36. #include <ctype.h>
  37. #include <sfsys.h>
  38. #include <string.h>
  39. #include <srates.h>
  40. #define SIGNAL_TO_LEFT (0)
  41. #define SIGNAL_TO_RIGHT (1)
  42. #define ROOT2 (1.4142136)
  43. #ifdef unix
  44. #define round(x) lround((x))
  45. #endif
  46. char errstr[2400];
  47. int anal_infiles = 1;
  48. int sloom = 0;
  49. int sloombatch = 0;
  50. const char* cdp_version = "7.1.0";
  51. //CDP LIB REPLACEMENTS
  52. static int check_panorama_param_validity_and_consistency(dataptr dz);
  53. static int setup_panorama_application(dataptr dz);
  54. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  55. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  56. static int setup_panorama_param_ranges_and_defaults(dataptr dz);
  57. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  58. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  59. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  60. static int establish_application(dataptr dz);
  61. static int initialise_vflags(dataptr dz);
  62. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  63. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  64. static int mark_parameter_types(dataptr dz,aplptr ap);
  65. static int assign_file_data_storage(int infilecnt,dataptr dz);
  66. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  67. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  68. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  69. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  70. static int handle_the_special_data(char *str,dataptr dz);
  71. static int panorama(dataptr dz);
  72. static void pancalc(double position,double *leftgain,double *rightgain);
  73. static int panorama_param_preprocess(dataptr dz);
  74. /**************************************** MAIN *********************************************/
  75. int main(int argc,char *argv[])
  76. {
  77. int exit_status;
  78. dataptr dz = NULL;
  79. char **cmdline;
  80. int cmdlinecnt;
  81. aplptr ap;
  82. int is_launched = FALSE;
  83. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  84. fprintf(stdout,"%s\n",cdp_version);
  85. fflush(stdout);
  86. return 0;
  87. }
  88. /* CHECK FOR SOUNDLOOM */
  89. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  90. sloom = 0;
  91. sloombatch = 1;
  92. }
  93. if(sflinit("cdp")){
  94. sfperror("cdp: initialisation\n");
  95. return(FAILED);
  96. }
  97. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  98. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  99. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  100. return(FAILED);
  101. }
  102. if(!sloom) {
  103. if(argc == 1) {
  104. usage1();
  105. return(FAILED);
  106. } else if(argc == 2) {
  107. usage2(argv[1]);
  108. return(FAILED);
  109. }
  110. }
  111. if(!sloom) {
  112. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  113. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  114. return(FAILED);
  115. }
  116. cmdline = argv;
  117. cmdlinecnt = argc;
  118. if((get_the_process_no(argv[0],dz))<0)
  119. return(FAILED);
  120. cmdline++;
  121. cmdlinecnt--;
  122. dz->maxmode = 2;
  123. if((get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  124. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  125. return(FAILED);
  126. }
  127. cmdline++;
  128. cmdlinecnt--;
  129. // setup_particular_application =
  130. if((exit_status = setup_panorama_application(dz))<0) {
  131. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  132. return(FAILED);
  133. }
  134. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  135. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  136. return(FAILED);
  137. }
  138. } else {
  139. //parse_TK_data() =
  140. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  141. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  142. return(exit_status);
  143. }
  144. }
  145. ap = dz->application;
  146. if(dz->infilecnt < 2) {
  147. sprintf(errstr,"Process only works with more than one input file\n");
  148. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  149. return(exit_status);
  150. }
  151. /* TEST */
  152. fprintf(stderr,"555 dz->infilecnt = %d\n",dz->infilecnt);
  153. /* TEST */
  154. // parse_infile_and_hone_type() =
  155. dz->process = MIXDUMMY;
  156. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  157. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  158. return(FAILED);
  159. }
  160. dz->process = PANORAMA;
  161. // setup_param_ranges_and_defaults() =
  162. if((exit_status = setup_panorama_param_ranges_and_defaults(dz))<0) {
  163. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  164. return(FAILED);
  165. }
  166. // open_first_infile CDP LIB
  167. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  168. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  169. return(FAILED);
  170. }
  171. cmdlinecnt--;
  172. cmdline++;
  173. dz->process = MIXDUMMY;
  174. if((exit_status = handle_extra_infiles(&cmdline,&cmdlinecnt,dz))<0) {
  175. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  176. return(FAILED);
  177. }
  178. dz->process = PANORAMA;
  179. /* TEST */
  180. fprintf(stderr,"999 cmdline[0] = %s\n",cmdline[0]);
  181. /* TEST */
  182. // handle_outfile() =
  183. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  184. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  185. return(FAILED);
  186. }
  187. /* TEST */
  188. fprintf(stderr,"AAA cmdline[0] = %s\n",cmdline[0]);
  189. /* TEST */
  190. // handle_formants() redundant
  191. // handle_formant_quiksearch() redundant
  192. if(dz->mode == 1) {
  193. if((exit_status = handle_the_special_data(cmdline[0],dz))<0) {
  194. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  195. return(FAILED);
  196. }
  197. cmdlinecnt--;
  198. cmdline++;
  199. }
  200. /* TEST */
  201. fprintf(stderr,"BBB\n");
  202. /* TEST */
  203. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  204. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  205. return(FAILED);
  206. }
  207. /* TEST */
  208. fprintf(stderr,"CCC\n");
  209. /* TEST */
  210. // check_param_validity_and_consistency....
  211. if((exit_status = check_panorama_param_validity_and_consistency(dz))<0) {
  212. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  213. return(FAILED);
  214. }
  215. is_launched = TRUE;
  216. if((exit_status = panorama_param_preprocess(dz))<0) {
  217. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  218. return(FAILED);
  219. }
  220. //process_file =
  221. if((exit_status = panorama(dz))<0) {
  222. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  223. return(FAILED);
  224. }
  225. if((exit_status = complete_output(dz))<0) { // CDP LIB
  226. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  227. return(FAILED);
  228. }
  229. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  230. free(dz);
  231. return(SUCCEEDED);
  232. }
  233. /**********************************************
  234. REPLACED CDP LIB FUNCTIONS
  235. **********************************************/
  236. /****************************** SET_PARAM_DATA *********************************/
  237. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  238. {
  239. ap->special_data = (char)special_data;
  240. ap->param_cnt = (char)paramcnt;
  241. ap->max_param_cnt = (char)maxparamcnt;
  242. if(ap->max_param_cnt>0) {
  243. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  244. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  245. return(MEMORY_ERROR);
  246. }
  247. strcpy(ap->param_list,paramlist);
  248. }
  249. return(FINISHED);
  250. }
  251. /****************************** SET_VFLGS *********************************/
  252. int set_vflgs
  253. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  254. {
  255. ap->option_cnt = (char) optcnt; /*RWD added cast */
  256. if(optcnt) {
  257. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  258. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  259. return(MEMORY_ERROR);
  260. }
  261. strcpy(ap->option_list,optlist);
  262. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  263. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  264. return(MEMORY_ERROR);
  265. }
  266. strcpy(ap->option_flags,optflags);
  267. }
  268. ap->vflag_cnt = (char) vflagcnt;
  269. ap->variant_param_cnt = (char) vparamcnt;
  270. if(vflagcnt) {
  271. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  272. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  273. return(MEMORY_ERROR);
  274. }
  275. strcpy(ap->variant_list,varlist);
  276. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  277. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  278. return(MEMORY_ERROR);
  279. }
  280. strcpy(ap->variant_flags,varflags);
  281. }
  282. return(FINISHED);
  283. }
  284. /***************************** APPLICATION_INIT **************************/
  285. int application_init(dataptr dz)
  286. {
  287. int exit_status;
  288. int storage_cnt;
  289. int tipc, brkcnt;
  290. aplptr ap = dz->application;
  291. if(ap->vflag_cnt>0)
  292. initialise_vflags(dz);
  293. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  294. ap->total_input_param_cnt = (char)tipc;
  295. if(tipc>0) {
  296. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  297. return(exit_status);
  298. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  299. return(exit_status);
  300. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  301. return(exit_status);
  302. }
  303. brkcnt = tipc;
  304. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  305. if(brkcnt>0) {
  306. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  307. return(exit_status);
  308. }
  309. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  310. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  311. return(exit_status);
  312. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  313. return(exit_status);
  314. }
  315. if((exit_status = mark_parameter_types(dz,ap))<0)
  316. return(exit_status);
  317. // establish_infile_constants() replaced by
  318. dz->infilecnt = 1;
  319. //establish_bufptrs_and_extra_buffers():
  320. return(FINISHED);
  321. }
  322. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  323. /* RWD mallo changed to calloc; helps debug verison run as release! */
  324. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  325. {
  326. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  327. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  328. return(MEMORY_ERROR);
  329. }
  330. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  331. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  332. return(MEMORY_ERROR);
  333. }
  334. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  335. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  336. return(MEMORY_ERROR);
  337. }
  338. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  339. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  340. return(MEMORY_ERROR);
  341. }
  342. return(FINISHED);
  343. }
  344. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  345. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  346. {
  347. int n;
  348. for(n=0;n<storage_cnt;n++) {
  349. dz->is_int[n] = (char)0;
  350. dz->no_brk[n] = (char)0;
  351. }
  352. return(FINISHED);
  353. }
  354. /***************************** MARK_PARAMETER_TYPES **************************/
  355. int mark_parameter_types(dataptr dz,aplptr ap)
  356. {
  357. int n, m; /* PARAMS */
  358. for(n=0;n<ap->max_param_cnt;n++) {
  359. switch(ap->param_list[n]) {
  360. case('0'): break; /* dz->is_active[n] = 0 is default */
  361. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  362. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  363. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  364. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  365. default:
  366. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  367. return(PROGRAM_ERROR);
  368. }
  369. } /* OPTIONS */
  370. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  371. switch(ap->option_list[n]) {
  372. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  373. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  374. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  375. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  376. default:
  377. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  378. return(PROGRAM_ERROR);
  379. }
  380. } /* VARIANTS */
  381. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  382. switch(ap->variant_list[n]) {
  383. case('0'): break;
  384. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  385. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  386. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  387. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  388. default:
  389. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  390. return(PROGRAM_ERROR);
  391. }
  392. } /* INTERNAL */
  393. for(n=0,
  394. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  395. switch(ap->internal_param_list[n]) {
  396. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  397. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  398. case('d'): dz->no_brk[m] = (char)1; break;
  399. default:
  400. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  401. return(PROGRAM_ERROR);
  402. }
  403. }
  404. return(FINISHED);
  405. }
  406. /************************ HANDLE_THE_OUTFILE *********************/
  407. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  408. {
  409. int exit_status;
  410. char *filename = (*cmdline)[0], *p;
  411. if(filename[0]=='-' && filename[1]=='f') {
  412. dz->floatsam_output = 1;
  413. dz->true_outfile_stype = SAMP_FLOAT;
  414. filename+= 2;
  415. }
  416. if(!sloom) {
  417. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  418. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  419. return(DATA_ERROR);
  420. }
  421. }
  422. p = filename;
  423. while(*p != ENDOFSTR) {
  424. if(*p == '.') {
  425. *p = ENDOFSTR;
  426. break;
  427. }
  428. p++;
  429. }
  430. strcpy(dz->outfilename,filename);
  431. if(!sloom)
  432. strcat(dz->outfilename,".mmx");
  433. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  434. return(exit_status);
  435. (*cmdline)++;
  436. (*cmdlinecnt)--;
  437. return(FINISHED);
  438. }
  439. /***************************** ESTABLISH_APPLICATION **************************/
  440. int establish_application(dataptr dz)
  441. {
  442. aplptr ap;
  443. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  444. sprintf(errstr,"establish_application()\n");
  445. return(MEMORY_ERROR);
  446. }
  447. ap = dz->application;
  448. memset((char *)ap,0,sizeof(struct applic));
  449. return(FINISHED);
  450. }
  451. /************************* INITIALISE_VFLAGS *************************/
  452. int initialise_vflags(dataptr dz)
  453. {
  454. int n;
  455. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  456. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  457. return(MEMORY_ERROR);
  458. }
  459. for(n=0;n<dz->application->vflag_cnt;n++)
  460. dz->vflag[n] = FALSE;
  461. return FINISHED;
  462. }
  463. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  464. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  465. {
  466. int n;
  467. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  468. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  469. return(MEMORY_ERROR);
  470. }
  471. for(n=0;n<tipc;n++)
  472. ap->default_val[n] = 0.0;
  473. return(FINISHED);
  474. }
  475. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  476. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  477. {
  478. int n;
  479. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  480. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  481. return(MEMORY_ERROR);
  482. }
  483. for(n=0;n<tipc;n++)
  484. dz->is_active[n] = (char)0;
  485. return(FINISHED);
  486. }
  487. /************************* SETUP_PANORAMA_APPLICATION *******************/
  488. int setup_panorama_application(dataptr dz)
  489. {
  490. int exit_status;
  491. aplptr ap;
  492. if((exit_status = establish_application(dz))<0) // GLOBAL
  493. return(FAILED);
  494. ap = dz->application;
  495. // SEE parstruct FOR EXPLANATION of next 2 functions
  496. if(dz->mode == 1) {
  497. if((exit_status = set_param_data(ap,PANOLSPKRS,5,3,"00ddi"))<0)
  498. return(FAILED);
  499. } else {
  500. if((exit_status = set_param_data(ap,0,5,5,"idddi"))<0)
  501. return(FAILED);
  502. }
  503. if((exit_status = set_vflgs(ap,"r",1,"d","pq",2,0,"00"))<0)
  504. return(FAILED);
  505. // set_legal_infile_structure -->
  506. dz->has_otherfile = FALSE;
  507. // assign_process_logic -->
  508. dz->input_data_type = MANY_SNDFILES;
  509. dz->process_type = TO_TEXTFILE;
  510. dz->outfiletype = TEXTFILE_OUT;
  511. return application_init(dz); //GLOBAL
  512. }
  513. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  514. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  515. {
  516. int exit_status;
  517. infileptr infile_info;
  518. if(!sloom) {
  519. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  520. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  521. return(MEMORY_ERROR);
  522. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  523. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  524. return(PROGRAM_ERROR);
  525. } else if(infile_info->filetype != SNDFILE) {
  526. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  527. return(DATA_ERROR);
  528. } else if(infile_info->channels != 1) {
  529. sprintf(errstr,"File %s is not of correct type (must be mono)\n",cmdline[0]);
  530. return(DATA_ERROR);
  531. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  532. sprintf(errstr,"Failed to copy file parsing information\n");
  533. return(PROGRAM_ERROR);
  534. }
  535. free(infile_info);
  536. }
  537. dz->all_words = 0;
  538. if((exit_status = store_filename(cmdline[0],dz))<0)
  539. return(exit_status);
  540. return(FINISHED);
  541. }
  542. /************************* SETUP_PANORAMA_PARAM_RANGES_AND_DEFAULTS *******************/
  543. int setup_panorama_param_ranges_and_defaults(dataptr dz)
  544. {
  545. int exit_status;
  546. aplptr ap = dz->application;
  547. // set_param_ranges()
  548. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  549. // NB total_input_param_cnt is > 0 !!!
  550. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  551. return(FAILED);
  552. // get_param_ranges()
  553. if(dz->mode == 0) {
  554. ap->lo[PANO_LCNT] = 3.0;
  555. ap->hi[PANO_LCNT] = 16.0;
  556. ap->default_val[PANO_LCNT] = 8;
  557. ap->lo[PANO_LWID] = 190.0;
  558. ap->hi[PANO_LWID] = 360.0;
  559. ap->default_val[PANO_LWID] = 360;
  560. }
  561. ap->lo[PANO_SPRD] = 0.0;
  562. ap->hi[PANO_SPRD] = 360.0;
  563. ap->default_val[PANO_SPRD] = 360;
  564. ap->lo[PANO_OFST] = -180;
  565. ap->hi[PANO_OFST] = 180;
  566. ap->default_val[PANO_OFST] = 0;
  567. ap->lo[PANO_CNFG] = 1;
  568. ap->hi[PANO_CNFG] = 128;
  569. ap->default_val[PANO_CNFG] = 1;
  570. ap->lo[PANO_RAND] = 0;
  571. ap->hi[PANO_RAND] = 1;
  572. ap->default_val[PANO_RAND] = 0;
  573. dz->maxmode = 2;
  574. if(!sloom)
  575. put_default_vals_in_all_params(dz);
  576. return(FINISHED);
  577. }
  578. /********************************* PARSE_SLOOM_DATA *********************************/
  579. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  580. {
  581. int exit_status;
  582. int cnt = 1, infilecnt;
  583. int filesize, insams, inbrksize;
  584. double dummy;
  585. int true_cnt = 0;
  586. aplptr ap;
  587. while(cnt<=PRE_CMDLINE_DATACNT) {
  588. if(cnt > argc) {
  589. sprintf(errstr,"Insufficient data sent from TK\n");
  590. return(DATA_ERROR);
  591. }
  592. switch(cnt) {
  593. case(1):
  594. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  595. sprintf(errstr,"Cannot read process no. sent from TK\n");
  596. return(DATA_ERROR);
  597. }
  598. break;
  599. case(2):
  600. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  601. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  602. return(DATA_ERROR);
  603. }
  604. if(dz->mode > 0)
  605. dz->mode--;
  606. //setup_particular_application() =
  607. if((exit_status = setup_panorama_application(dz))<0)
  608. return(exit_status);
  609. ap = dz->application;
  610. break;
  611. case(3):
  612. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  613. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  614. return(DATA_ERROR);
  615. }
  616. if(infilecnt < 1) {
  617. true_cnt = cnt + 1;
  618. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  619. }
  620. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  621. return(exit_status);
  622. break;
  623. case(INPUT_FILETYPE+4):
  624. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  625. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  626. return(DATA_ERROR);
  627. }
  628. break;
  629. case(INPUT_FILESIZE+4):
  630. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  631. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  632. return(DATA_ERROR);
  633. }
  634. dz->insams[0] = filesize;
  635. break;
  636. case(INPUT_INSAMS+4):
  637. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  638. sprintf(errstr,"Cannot read insams sent from TK\n");
  639. return(DATA_ERROR);
  640. }
  641. dz->insams[0] = insams;
  642. break;
  643. case(INPUT_SRATE+4):
  644. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  645. sprintf(errstr,"Cannot read srate sent from TK\n");
  646. return(DATA_ERROR);
  647. }
  648. break;
  649. case(INPUT_CHANNELS+4):
  650. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  651. sprintf(errstr,"Cannot read channels sent from TK\n");
  652. return(DATA_ERROR);
  653. }
  654. break;
  655. case(INPUT_STYPE+4):
  656. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  657. sprintf(errstr,"Cannot read stype sent from TK\n");
  658. return(DATA_ERROR);
  659. }
  660. break;
  661. case(INPUT_ORIGSTYPE+4):
  662. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  663. sprintf(errstr,"Cannot read origstype sent from TK\n");
  664. return(DATA_ERROR);
  665. }
  666. break;
  667. case(INPUT_ORIGRATE+4):
  668. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  669. sprintf(errstr,"Cannot read origrate sent from TK\n");
  670. return(DATA_ERROR);
  671. }
  672. break;
  673. case(INPUT_MLEN+4):
  674. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  675. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  676. return(DATA_ERROR);
  677. }
  678. break;
  679. case(INPUT_DFAC+4):
  680. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  681. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  682. return(DATA_ERROR);
  683. }
  684. break;
  685. case(INPUT_ORIGCHANS+4):
  686. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  687. sprintf(errstr,"Cannot read origchans sent from TK\n");
  688. return(DATA_ERROR);
  689. }
  690. break;
  691. case(INPUT_SPECENVCNT+4):
  692. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  693. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  694. return(DATA_ERROR);
  695. }
  696. dz->specenvcnt = dz->infile->specenvcnt;
  697. break;
  698. case(INPUT_WANTED+4):
  699. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  700. sprintf(errstr,"Cannot read wanted sent from TK\n");
  701. return(DATA_ERROR);
  702. }
  703. break;
  704. case(INPUT_WLENGTH+4):
  705. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  706. sprintf(errstr,"Cannot read wlength sent from TK\n");
  707. return(DATA_ERROR);
  708. }
  709. break;
  710. case(INPUT_OUT_CHANS+4):
  711. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  712. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  713. return(DATA_ERROR);
  714. }
  715. break;
  716. /* RWD these chanegs to samps - tk will have to deal with that! */
  717. case(INPUT_DESCRIPTOR_BYTES+4):
  718. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  719. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  720. return(DATA_ERROR);
  721. }
  722. break;
  723. case(INPUT_IS_TRANSPOS+4):
  724. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  725. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  726. return(DATA_ERROR);
  727. }
  728. break;
  729. case(INPUT_COULD_BE_TRANSPOS+4):
  730. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  731. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  732. return(DATA_ERROR);
  733. }
  734. break;
  735. case(INPUT_COULD_BE_PITCH+4):
  736. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  737. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  738. return(DATA_ERROR);
  739. }
  740. break;
  741. case(INPUT_DIFFERENT_SRATES+4):
  742. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  743. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  744. return(DATA_ERROR);
  745. }
  746. break;
  747. case(INPUT_DUPLICATE_SNDS+4):
  748. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  749. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  750. return(DATA_ERROR);
  751. }
  752. break;
  753. case(INPUT_BRKSIZE+4):
  754. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  755. sprintf(errstr,"Cannot read brksize sent from TK\n");
  756. return(DATA_ERROR);
  757. }
  758. if(inbrksize > 0) {
  759. switch(dz->input_data_type) {
  760. case(WORDLIST_ONLY):
  761. break;
  762. case(PITCH_AND_PITCH):
  763. case(PITCH_AND_TRANSPOS):
  764. case(TRANSPOS_AND_TRANSPOS):
  765. dz->tempsize = inbrksize;
  766. break;
  767. case(BRKFILES_ONLY):
  768. case(UNRANGED_BRKFILE_ONLY):
  769. case(DB_BRKFILES_ONLY):
  770. case(ALL_FILES):
  771. case(ANY_NUMBER_OF_ANY_FILES):
  772. if(dz->extrabrkno < 0) {
  773. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  774. return(DATA_ERROR);
  775. }
  776. if(dz->brksize == NULL) {
  777. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  778. return(PROGRAM_ERROR);
  779. }
  780. dz->brksize[dz->extrabrkno] = inbrksize;
  781. break;
  782. default:
  783. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  784. dz->input_data_type);
  785. return(PROGRAM_ERROR);
  786. }
  787. break;
  788. }
  789. break;
  790. case(INPUT_NUMSIZE+4):
  791. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  792. sprintf(errstr,"Cannot read numsize sent from TK\n");
  793. return(DATA_ERROR);
  794. }
  795. break;
  796. case(INPUT_LINECNT+4):
  797. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  798. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  799. return(DATA_ERROR);
  800. }
  801. break;
  802. case(INPUT_ALL_WORDS+4):
  803. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  804. sprintf(errstr,"Cannot read all_words sent from TK\n");
  805. return(DATA_ERROR);
  806. }
  807. break;
  808. case(INPUT_ARATE+4):
  809. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  810. sprintf(errstr,"Cannot read arate sent from TK\n");
  811. return(DATA_ERROR);
  812. }
  813. break;
  814. case(INPUT_FRAMETIME+4):
  815. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  816. sprintf(errstr,"Cannot read frametime sent from TK\n");
  817. return(DATA_ERROR);
  818. }
  819. dz->frametime = (float)dummy;
  820. break;
  821. case(INPUT_WINDOW_SIZE+4):
  822. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  823. sprintf(errstr,"Cannot read window_size sent from TK\n");
  824. return(DATA_ERROR);
  825. }
  826. break;
  827. case(INPUT_NYQUIST+4):
  828. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  829. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  830. return(DATA_ERROR);
  831. }
  832. break;
  833. case(INPUT_DURATION+4):
  834. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  835. sprintf(errstr,"Cannot read duration sent from TK\n");
  836. return(DATA_ERROR);
  837. }
  838. break;
  839. case(INPUT_MINBRK+4):
  840. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  841. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  842. return(DATA_ERROR);
  843. }
  844. break;
  845. case(INPUT_MAXBRK+4):
  846. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  847. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  848. return(DATA_ERROR);
  849. }
  850. break;
  851. case(INPUT_MINNUM+4):
  852. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  853. sprintf(errstr,"Cannot read minnum sent from TK\n");
  854. return(DATA_ERROR);
  855. }
  856. break;
  857. case(INPUT_MAXNUM+4):
  858. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  859. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  860. return(DATA_ERROR);
  861. }
  862. break;
  863. default:
  864. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  865. return(PROGRAM_ERROR);
  866. }
  867. cnt++;
  868. }
  869. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  870. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  871. return(DATA_ERROR);
  872. }
  873. if(true_cnt)
  874. cnt = true_cnt;
  875. *cmdlinecnt = 0;
  876. while(cnt < argc) {
  877. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  878. return(exit_status);
  879. cnt++;
  880. }
  881. return(FINISHED);
  882. }
  883. /********************************* GET_TK_CMDLINE_WORD *********************************/
  884. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  885. {
  886. if(*cmdlinecnt==0) {
  887. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  888. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  889. return(MEMORY_ERROR);
  890. }
  891. } else {
  892. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  893. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  894. return(MEMORY_ERROR);
  895. }
  896. }
  897. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  898. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  899. return(MEMORY_ERROR);
  900. }
  901. strcpy((*cmdline)[*cmdlinecnt],q);
  902. (*cmdlinecnt)++;
  903. return(FINISHED);
  904. }
  905. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  906. int assign_file_data_storage(int infilecnt,dataptr dz)
  907. {
  908. int exit_status;
  909. int no_sndfile_system_files = FALSE;
  910. dz->infilecnt = infilecnt;
  911. if((exit_status = allocate_filespace(dz))<0)
  912. return(exit_status);
  913. if(no_sndfile_system_files)
  914. dz->infilecnt = 0;
  915. return(FINISHED);
  916. }
  917. /************************* redundant functions: to ensure libs compile OK *******************/
  918. int assign_process_logic(dataptr dz)
  919. {
  920. return(FINISHED);
  921. }
  922. void set_legal_infile_structure(dataptr dz)
  923. {}
  924. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  925. {
  926. return(FINISHED);
  927. }
  928. int setup_internal_arrays_and_array_pointers(dataptr dz)
  929. {
  930. return(FINISHED);
  931. }
  932. int establish_bufptrs_and_extra_buffers(dataptr dz)
  933. {
  934. return(FINISHED);
  935. }
  936. int read_special_data(char *str,dataptr dz)
  937. {
  938. return(FINISHED);
  939. }
  940. int inner_loop
  941. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  942. {
  943. return(FINISHED);
  944. }
  945. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  946. {
  947. return(FINISHED);
  948. }
  949. /******************************** USAGE1 ********************************/
  950. int usage1(void)
  951. {
  952. usage2("panorama");
  953. return(USAGE_ONLY);
  954. }
  955. /**************************** CHECK_PANORAMA_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  956. int check_panorama_param_validity_and_consistency(dataptr dz)
  957. {
  958. double *lspkrpos, *pos, rightmost, leftmost, anglestep;
  959. int n;
  960. if(dz->mode == 0) { // Set lspkr positions for mode 0
  961. if((dz->parray = (double **)malloc(2 * sizeof(double *)))==NULL) {
  962. sprintf(errstr,"INSUFFICIENT MEMORY for position storage arrays.\n");
  963. return(MEMORY_ERROR);
  964. }
  965. dz->itemcnt = dz->iparam[PANO_LCNT]; // Array for loudspeaker positions
  966. if((dz->parray[0] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  967. sprintf(errstr,"INSUFFICIENT MEMORY to store loudspeaker positions.\n");
  968. return(MEMORY_ERROR);
  969. } // Array for sound positions
  970. if((dz->parray[1] = (double *)malloc(dz->infilecnt * sizeof(double)))==NULL) {
  971. sprintf(errstr,"INSUFFICIENT MEMORY to store sound positions.\n");
  972. return(MEMORY_ERROR);
  973. } // Array for sorting
  974. if((dz->parray[2] = (double *)malloc(max(dz->infilecnt,dz->itemcnt) * sizeof(double)))==NULL) {
  975. sprintf(errstr,"INSUFFICIENT MEMORY to store sound positions.\n");
  976. return(MEMORY_ERROR);
  977. }
  978. lspkrpos = dz->parray[0];
  979. pos = dz->parray[1];
  980. if(dz->param[PANO_LWID] < 360) { // distribute loudspeaker positions across angular width
  981. rightmost = dz->param[PANO_LWID]/2.0;
  982. leftmost = 360.0 - dz->param[PANO_LWID]/2.0;
  983. anglestep = dz->param[PANO_LWID]/(double)(dz->itemcnt-1);
  984. for(n=dz->itemcnt-1;n>0;n--) {
  985. lspkrpos[n] = rightmost;
  986. if(flteq(lspkrpos[n],0.0))
  987. lspkrpos[n] = 0.0;
  988. else if(lspkrpos[n] < 0.0)
  989. lspkrpos[n] += 360.0;
  990. rightmost -= anglestep;
  991. }
  992. lspkrpos[0] = leftmost;
  993. } else { // distribute loudspeaker positions around circle
  994. anglestep = 360.00/dz->itemcnt;
  995. if(dz->vflag[0]) // Front pair
  996. rightmost = -(anglestep/2.0);
  997. else // Front single
  998. rightmost = 0.0;
  999. for(n = 0;n < dz->itemcnt;n++) {
  1000. lspkrpos[n] = rightmost;
  1001. if(lspkrpos[n] < 0.0)
  1002. lspkrpos[n] += 360.0;
  1003. rightmost += anglestep;
  1004. }
  1005. }
  1006. }
  1007. n = dz->infilecnt/dz->iparam[PANO_CNFG]; // Check configuration parameter
  1008. if(n * dz->iparam[PANO_CNFG] != dz->infilecnt) {
  1009. sprintf(errstr,"Sound configuration value is not a divisor of number of input files.\n");
  1010. return(DATA_ERROR);
  1011. } // Check randomisation param
  1012. if((dz->infilecnt == 2) && (dz->iparam[PANO_SPRD] < 360.0)) {
  1013. if(dz->param[PANO_RAND] > 0.0) {
  1014. sprintf(errstr,"No position randomisation possible with 2 inputs, and sound-spread < 360.\n");
  1015. return(DATA_ERROR);
  1016. }
  1017. }
  1018. return FINISHED;
  1019. }
  1020. /**************************** PANORAMA_PARAM_PREPROCESS *****************************/
  1021. int panorama_param_preprocess(dataptr dz)
  1022. {
  1023. double *lspkrpos, *pos, *sort, halfdiffdn, halfdiffup, diffdn, diffup, randoff, temp;
  1024. double sndleftmost, sndrightmost, sndangstep;
  1025. int n, m, k, up, dn, stepcnt, thisstep;
  1026. lspkrpos = dz->parray[0];
  1027. pos = dz->parray[1];
  1028. sort = dz->parray[2];
  1029. for(n = 0;n <dz->itemcnt-1;n++) { // sort loudspeaker positions into angular ascending order from 0
  1030. for(m=n;m<dz->itemcnt;m++) {
  1031. if(lspkrpos[n] > lspkrpos[m]) {
  1032. temp = lspkrpos[n];
  1033. lspkrpos[n] = lspkrpos[m];
  1034. lspkrpos[m] = temp;
  1035. }
  1036. }
  1037. } // For no spaces in the configuration
  1038. if(dz->iparam[PANO_CNFG] == 1) { // Position sounds AS IF THEY ARE zero-centred.
  1039. sndleftmost = 360 - dz->param[PANO_SPRD]/2.0; // startingSat the rightmost sound, divide snd-spread angle equally
  1040. sndrightmost = dz->param[PANO_SPRD]/2.0; // For 360, snds (X) and steps (--) are equal in number
  1041. stepcnt = dz->infilecnt; // X--X--X--X--X--X--
  1042. if(dz->param[PANO_SPRD] < 360.0) // For < 360, there is 1 less step
  1043. stepcnt--; // X--X--X--X--X--X
  1044. sndangstep = dz->param[PANO_SPRD]/(double)stepcnt;
  1045. if(dz->param[PANO_SPRD] >= 360.0) { // For 360, the last position, oocurs BEFORE the end of the angle-spread
  1046. sndleftmost += sndangstep; // or else 1st and last snds are superimposed:
  1047. if((ODD(dz->infilecnt) && !dz->vflag[1]) // If a sound at 0 degrees required, offset all vals half-a-turn
  1048. || (EVEN(dz->infilecnt) && dz->vflag[1])) { // Starting at 180, with an even number of files, 0 degrees will be used
  1049. sndrightmost -= sndangstep/2.0; // so only offset if pairing-flag is set
  1050. if(flteq(sndrightmost,0.0)) // Starting at 180, with an odd number of files, 0 degrees will not be used
  1051. sndrightmost = 0.0; // so offset if pairing-flag is not used
  1052. else if(sndrightmost < 0)
  1053. sndrightmost += 360.0;
  1054. sndleftmost -= sndangstep/2.0;
  1055. if(flteq(sndleftmost,0.0))
  1056. sndleftmost = 0.0;
  1057. else if(sndleftmost < 0)
  1058. sndleftmost += 360.0;
  1059. }
  1060. }
  1061. for(n=dz->infilecnt - 1;n>0;n--) {
  1062. pos[n] = sndrightmost;
  1063. sndrightmost -= sndangstep;
  1064. if(flteq(sndrightmost,0.0))
  1065. sndrightmost = 0.0;
  1066. else if(sndrightmost < 0)
  1067. sndrightmost += 360.0;
  1068. }
  1069. pos[0] = sndleftmost;
  1070. } else { // Where there are space in the configuration (config > 1)
  1071. sndleftmost = 360 - dz->param[PANO_SPRD]/2.0; // introducing appropriate gaps into sound layout.
  1072. sndrightmost = dz->param[PANO_SPRD]/2.0;
  1073. thisstep = 0;
  1074. if(dz->param[PANO_SPRD] < 360.0) { // 6 positions with config 2, produces pairs with spaces
  1075. stepcnt = dz->infilecnt + (dz->infilecnt/dz->iparam[PANO_CNFG]) - 2; // X--X--o--X--X--o--X--X
  1076. sndangstep = dz->param[PANO_SPRD]/(double)stepcnt; // So there is 1 space for each pair 6/2 = 3
  1077. for(n=dz->infilecnt - 1;n>0;n--) { // Except there is no space associated with last pair = (6/2) - 1
  1078. pos[n] = sndrightmost; // Number of items (poss + spaces) = 6 + (6/2) - 1
  1079. sndrightmost -= sndangstep; // Number of steps between items is 1 less, 6 + (6/2) - 2 = 8
  1080. if(++thisstep == dz->iparam[PANO_CNFG]) { // 6 positions with config 3, produces triplets with spaces
  1081. sndrightmost -= sndangstep; // X--X--X--o--X--X--X
  1082. thisstep = 0; // So there is a space for each triplet 6/3 = 2
  1083. } // Except there is no space associated with last triplet = (6/3) - 1
  1084. if(sndrightmost < 0) // Number of items (poss + spaces) = 6 + (6/3) - 1
  1085. sndrightmost += 360.0; // Number of steps between items is 1 less, 6 + (6/3) - 1 = 7
  1086. }
  1087. pos[0] = sndleftmost;
  1088. } else { // Sound surround situation differs
  1089. stepcnt = dz->infilecnt + (dz->infilecnt/dz->iparam[PANO_CNFG]); // (1) There is a space after EVERY position-set
  1090. sndangstep = dz->param[PANO_SPRD]/(double)stepcnt; // (2) There is a step between EVERY position-or-space
  1091. sndrightmost = 180; // Start at rear, ending on left of space
  1092. sndleftmost = 180 + sndangstep; // So first and last don't overlap, move leftmost by a turn.
  1093. if(EVEN(stepcnt)) { // In order to centre the central-group at 0 degrees
  1094. thisstep = dz->iparam[PANO_CNFG]/2; // If an even number of steps,
  1095. if(EVEN(dz->iparam[PANO_CNFG])) // Start counting spaces ALREADY half-way through a set
  1096. sndrightmost -= (sndangstep * 0.5); // And, if an even set, need to offset the group start by half a turn
  1097. } else { // If an odd number of steps, force a gap at rear, by
  1098. sndrightmost -= sndangstep; // offsetting rightmost by 1 turn
  1099. thisstep = 0;
  1100. }
  1101. for(n=dz->infilecnt - 1;n>=0;n--) {
  1102. pos[n] = sndrightmost;
  1103. sndrightmost -= sndangstep;
  1104. if(++thisstep == dz->iparam[PANO_CNFG]) {
  1105. sndrightmost -= sndangstep;
  1106. thisstep = 0;
  1107. }
  1108. if(sndrightmost < 0)
  1109. sndrightmost += 360.0;
  1110. else if(sndrightmost >= 360.0)
  1111. sndrightmost -= 360.0;
  1112. }
  1113. }
  1114. }
  1115. for(n=0;n<dz->infilecnt;n++) { // Offset the sound-positions
  1116. pos[n] += dz->param[PANO_OFST];
  1117. if(pos[n] > 360.0)
  1118. pos[n] -= 360.0;
  1119. else if(pos[n] < 0)
  1120. pos[n] += 360.0;
  1121. }
  1122. for(n = 0;n <dz->infilecnt-1;n++) { // Sort sound positions into angular ascending order from 0
  1123. for(m=n;m<dz->infilecnt;m++) {
  1124. if(pos[n] > pos[m]) {
  1125. temp = pos[n];
  1126. pos[n] = pos[m];
  1127. pos[m] = temp;
  1128. }
  1129. }
  1130. }
  1131. if(dz->param[PANO_RAND] > 0.0) { // Randomise the sound-positions
  1132. for(n = 0;n <dz->infilecnt;n++) {
  1133. if(pos[n] > 180) { // sort into leftmost to rightmost order (for randomising positions)
  1134. m = n;
  1135. for(k = 0;m < dz->infilecnt;m++,k++)
  1136. sort[k] = pos[m];
  1137. for(m = 0;k < dz->infilecnt;m++,k++)
  1138. sort[k] = pos[m];
  1139. for(m=0;m<dz->infilecnt;m++)
  1140. pos[m] = sort[m];
  1141. break;
  1142. }
  1143. }
  1144. for(n = 0;n < dz->infilecnt;n++) // Store original positions
  1145. sort[n] = pos[n];
  1146. dn = 0;
  1147. up = 2;
  1148. for(n = 1;n < dz->infilecnt-1;n++) { // Randomise all inner positions
  1149. diffdn = (sort[n] - sort[dn]); // X--r--r--r--r--r--X for < 360
  1150. diffup = (sort[up] - sort[n]); //
  1151. if(diffdn < 0)
  1152. diffdn += 360;
  1153. if(diffup < 0)
  1154. diffup += 360;
  1155. halfdiffup = diffup/2.0;
  1156. halfdiffdn = diffdn/2.0;
  1157. randoff = (drand48() * 2.0) - 1.0; // X--r--r--r--r--r--X-- for 360
  1158. randoff *= dz->param[PANO_RAND]; // NB no effect if only 2 infiles
  1159. if(randoff > 0.0) {
  1160. pos[n] = sort[n] + (halfdiffup * randoff);
  1161. if(pos[n] >= 360.0)
  1162. pos[n] -= 360.0;
  1163. } else {
  1164. pos[n] = sort[n] - (halfdiffdn * randoff);
  1165. if(pos[n] < 0.0)
  1166. pos[n] += 360.0;
  1167. }
  1168. up++;
  1169. dn++;
  1170. } // For 360 degree layout
  1171. if(dz->param[PANO_SPRD] >= 360) { // Randomise last outer positions
  1172. if(dz->infilecnt >= 3) { // X--r--r--r--r--r--R--
  1173. n = dz->infilecnt-1;
  1174. dn = dz->infilecnt-2;
  1175. up = 0;
  1176. diffdn = sort[n] - sort[dn];
  1177. diffup = sort[up] - sort[n];
  1178. if(diffdn < 0)
  1179. diffdn += 360;
  1180. if(diffup < 0)
  1181. diffup += 360;
  1182. halfdiffup = diffup/2.0;
  1183. halfdiffdn = diffdn/2.0;
  1184. randoff = (drand48() * 2.0) - 1.0;
  1185. randoff *= dz->param[PANO_RAND];
  1186. if(randoff > 0.0) {
  1187. pos[n] = sort[n] + (halfdiffup * randoff);
  1188. if(pos[n] >= 360.0)
  1189. pos[n] -= 360.0;
  1190. } else {
  1191. pos[n] = sort[n] - (halfdiffdn * randoff);
  1192. if(pos[n] < 0.0)
  1193. pos[n] += 360.0;
  1194. }
  1195. n = 0; // Randomise first outer position
  1196. up = 1; // R--r--r--r--r--r--r--
  1197. dn = dz->infilecnt-1;
  1198. diffdn = sort[n] - sort[dn];
  1199. diffup = sort[up] - sort[n];
  1200. if(diffdn < 0)
  1201. diffdn += 360;
  1202. if(diffup < 0)
  1203. diffup += 360;
  1204. halfdiffup = diffup/2.0;
  1205. halfdiffdn = diffdn/2.0;
  1206. randoff = (drand48() * 2.0) - 1.0;
  1207. randoff *= dz->param[PANO_RAND];
  1208. if(randoff > 0.0) {
  1209. pos[n] = sort[n] + (halfdiffup * randoff);
  1210. if(pos[n] >= 360.0)
  1211. pos[n] -= 360.0;
  1212. } else {
  1213. pos[n] = sort[n] - (halfdiffdn * randoff);
  1214. if(pos[n] < 0.0)
  1215. pos[n] += 360.0;
  1216. }
  1217. } else { // If only 2 infiles, nothing has changed, so
  1218. n = 0; // Randomise first outer position
  1219. up = 1; // R--X
  1220. dn = dz->infilecnt - 1;
  1221. diffup = sort[up] - sort[n];
  1222. if(diffup < 0)
  1223. diffup += 360;
  1224. diffdn = 360.0 - diffup;
  1225. halfdiffup = diffup/2.0;
  1226. halfdiffdn = diffdn/2.0;
  1227. randoff = (drand48() * 2.0) - 1.0;
  1228. randoff *= dz->param[PANO_RAND];
  1229. if(randoff > 0.0) {
  1230. pos[n] = sort[n] + (halfdiffup * randoff);
  1231. if(pos[n] >= 360.0)
  1232. pos[n] -= 360.0;
  1233. } else {
  1234. pos[n] = sort[n] - (halfdiffdn * randoff);
  1235. if(pos[n] < 0.0)
  1236. pos[n] += 360.0;
  1237. } // Randomise last outer position
  1238. n = 1; // r--R
  1239. temp = halfdiffup;
  1240. halfdiffup = halfdiffdn;
  1241. halfdiffdn = temp;
  1242. randoff = (drand48() * 2.0) - 1.0;
  1243. randoff *= dz->param[PANO_RAND];
  1244. if(randoff > 0.0) {
  1245. pos[n] = sort[n] + (halfdiffup * randoff);
  1246. if(pos[n] >= 360.0)
  1247. pos[n] -= 360.0;
  1248. } else {
  1249. pos[n] = sort[n] - (halfdiffdn * randoff);
  1250. if(pos[n] < 0.0)
  1251. pos[n] += 360.0;
  1252. }
  1253. }
  1254. }
  1255. for(n = 0;n <dz->infilecnt-1;n++) { // Resort sound positions into angular ascending order from 0
  1256. for(m=n;m<dz->infilecnt;m++) {
  1257. if(pos[n] > pos[m]) {
  1258. temp = pos[n];
  1259. pos[n] = pos[m];
  1260. pos[m] = temp;
  1261. }
  1262. }
  1263. }
  1264. }
  1265. for(n = 0;n <dz->infilecnt;n++) { // sort into leftmost to rightmost order (for assigning infiles)
  1266. if(pos[n] > 180) {
  1267. m = n;
  1268. for(k = 0;m < dz->infilecnt;m++,k++)
  1269. sort[k] = pos[m];
  1270. for(m = 0;k < dz->infilecnt;m++,k++)
  1271. sort[k] = pos[m];
  1272. for(m=0;m<dz->infilecnt;m++)
  1273. pos[m] = sort[m];
  1274. break;
  1275. }
  1276. }
  1277. return FINISHED;
  1278. }
  1279. /********************************************************************************************/
  1280. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1281. {
  1282. if(!strcmp(prog_identifier_from_cmdline,"panorama")) dz->process = PANORAMA;
  1283. else {
  1284. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  1285. return(USAGE_ONLY);
  1286. }
  1287. return(FINISHED);
  1288. }
  1289. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  1290. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  1291. {
  1292. int n;
  1293. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1294. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  1295. return(MEMORY_ERROR);
  1296. }
  1297. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  1298. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  1299. return(MEMORY_ERROR);
  1300. }
  1301. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1302. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  1303. return(MEMORY_ERROR);
  1304. }
  1305. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1306. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  1307. return(MEMORY_ERROR);
  1308. }
  1309. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1310. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  1311. return(MEMORY_ERROR);
  1312. }
  1313. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  1314. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  1315. return(MEMORY_ERROR);
  1316. }
  1317. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  1318. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  1319. return(MEMORY_ERROR);
  1320. }
  1321. for(n=0;n<brkcnt;n++) {
  1322. dz->brk[n] = NULL;
  1323. dz->brkptr[n] = NULL;
  1324. dz->brkinit[n] = 0;
  1325. dz->brksize[n] = 0;
  1326. }
  1327. return(FINISHED);
  1328. }
  1329. /******************************** USAGE2 ********************************/
  1330. int usage2(char *str)
  1331. {
  1332. if(!strcmp(str,"panorama")) {
  1333. fprintf(stdout,
  1334. "USAGE: panorama panorama 1 infile infile2 [infile3.....] outmixfile\n"
  1335. " lspk_cnt lspk_aw sounds_aw sounds_ao config [-rrand] [-p] [-q]\n"
  1336. "OR: panorama panorama 2 infile infile2 [infile3.....] outmixfile\n"
  1337. " lspk_positions sounds_aw sounds_ao config [-rrand] [-p] [-q]\n"
  1338. "\n"
  1339. "Distribute (>1) mono files in spatial panorama, in multichannel mixfile.\n"
  1340. "Lspkrs assumed to surround listening area from front outwards.\n"
  1341. "Input sounds distributed in order from leftmost to rightmost position.\n"
  1342. "\n"
  1343. "LSPK_CNT Number of lspkrs (mode 1 equally spaced: not ness so in mode 2).\n"
  1344. "LSPK_AW Angular width of loudspeaker array (190 - 360), front centre at 0.\n"
  1345. " Loudspeaker array assumed symmetrical around a centre-line\n"
  1346. " running through front-centre of lspkr array and centre of auditorium.\n"
  1347. " Thus, if front centre is at 0 then \n"
  1348. " 190 spread is from -95 to (+)95; 360 spread is from -180 to (+)180.\n"
  1349. "\n"
  1350. "SOUNDS_AW angular width of output sounds (equal to or less than LSPK_AW).\n"
  1351. "\n"
  1352. "SOUNDS_AO angular offset of output snds. (Only possible if SOUNDS_AW < LSPK_AW)\n"
  1353. " (angle between centre-line of snds and centre-line of lspkrs)\n"
  1354. "\n"
  1355. "CONFIG Distribution of output sounds within output angle\n"
  1356. " config = 1: sounds equally spaced\n"
  1357. " config = 2: 2 sounds equally spaced, followed by gap, etc.\n"
  1358. " config = 3: 3 sounds equally spaced, followed by gap\n"
  1359. " and so on. CONFIG must be a divisor of the number of input sounds.\n"
  1360. "\n"
  1361. "RAND Randomisation of sound positions (0-1).\n"
  1362. "\n"
  1363. "LSPK_POSITIONS (Mode 2) Textfile list of angular positions of (3-16) lspkrs.\n"
  1364. " Positions specified by +ve values from 0 (front centre) clockwise, so\n"
  1365. " vals to right of centre lie between 0 (front)and 180 (rear)\n"
  1366. " vals to left of centre lie between >180 (rear) and 360(=0) (front)\n"
  1367. "\n"
  1368. "-p If angular width of lspkrs (LSPK_AW) < 360 :\n"
  1369. " Odd number of lspkrs gives 1 lspkr at centre front.\n"
  1370. " Even number of lspkrs gives pair of lspkrs centred on front.\n"
  1371. " If LSP_AW = 360, speaker orientation is ambiguous.\n"
  1372. " Default is 1 lspkr at centre front.\n"
  1373. " \"-p\" flag give PAIR of lspkrs at front (IGNORED if LSP_AW < 360 )\n"
  1374. "-q Same logic for SOUND positions ( ignored if SOUNDS_AW < 360 )\n"
  1375. "\n");
  1376. } else
  1377. fprintf(stdout,"Unknown option '%s'\n",str);
  1378. return(USAGE_ONLY);
  1379. }
  1380. int usage3(char *str1,char *str2)
  1381. {
  1382. fprintf(stderr,"Insufficient parameters on command line.\n");
  1383. return(USAGE_ONLY);
  1384. }
  1385. /******************************** PANORAMA ********************************/
  1386. int panorama(dataptr dz)
  1387. {
  1388. double *lspkrpos = dz->parray[0], *pos = dz->parray[1];
  1389. int chans = dz->infile->channels, gotext = 0;
  1390. double thispos, loangle, hiangle = 0.0, lspkr_angle, left_lspkr_offset, stereopos, leftgain, rightgain, lspkrposlo, lspkrposhi;
  1391. int n, lo, hi;
  1392. char temp[4000], temp2[200], *p;
  1393. sprintf(temp,"%i\n",dz->itemcnt);
  1394. if(fputs(temp,dz->fp) < 0) {
  1395. sprintf(errstr,"Error writing to output data file\n");
  1396. return(PROGRAM_ERROR);
  1397. }
  1398. for(n=0; n< dz->infilecnt; n++) {
  1399. loangle = -1;
  1400. thispos = pos[n];
  1401. lo = 0;
  1402. hi = 1;
  1403. while(lo < dz->itemcnt) {
  1404. lspkrposlo = lspkrpos[lo];
  1405. lspkrposhi = lspkrpos[hi];
  1406. if(lspkrposhi < lspkrposlo) {
  1407. if((thispos >= lspkrposlo && thispos <= 360.0)
  1408. || (thispos >= 0.0 && thispos < lspkrposhi)) {
  1409. loangle = lspkrpos[lo];
  1410. hiangle = lspkrpos[hi] + 360;
  1411. break;
  1412. }
  1413. } else if (thispos >= lspkrposlo && thispos < lspkrposhi) {
  1414. loangle = lspkrpos[lo];
  1415. hiangle = lspkrpos[hi];
  1416. break;
  1417. }
  1418. if(++hi >= dz->itemcnt)
  1419. hi = 0;
  1420. lo++;
  1421. }
  1422. if(loangle < 0.0) {
  1423. sprintf(errstr,"Error locating lspkr pair\n");
  1424. return(PROGRAM_ERROR);
  1425. }
  1426. lspkr_angle = hiangle - loangle;
  1427. if(lspkrpos[lo] < thispos)
  1428. left_lspkr_offset = thispos - lspkrpos[lo];
  1429. else
  1430. left_lspkr_offset = thispos - (lspkrpos[lo] - 360.0);
  1431. if(flteq(left_lspkr_offset,360.0))
  1432. left_lspkr_offset = 0.0;
  1433. stereopos = left_lspkr_offset/lspkr_angle; // Range 0 - 1
  1434. stereopos = (stereopos * 2.0) - 1.0; // Range -1 to +1
  1435. pancalc(stereopos,&leftgain,&rightgain);
  1436. lo++; // Change from data-numbering (0 to N-1) to channel-routing numbering (1 to N)
  1437. hi++;
  1438. sprintf(temp2,"%s",dz->wordstor[n]);
  1439. p = temp2;
  1440. gotext = 0;
  1441. while(*p != ENDOFSTR) {
  1442. if(*p == '.') {
  1443. gotext = 1;
  1444. break;
  1445. }
  1446. p++;
  1447. }
  1448. if(!gotext)
  1449. strcat(temp2,".wav");
  1450. strcpy(temp,temp2);
  1451. strcat(temp," 0.0 ");
  1452. sprintf(temp2,"%i",chans);
  1453. strcat(temp,temp2);
  1454. if(!flteq(leftgain,0.0)) {
  1455. sprintf(temp2," 1:%i",lo);
  1456. strcat(temp,temp2);
  1457. strcat(temp," ");
  1458. sprintf(temp2,"%lf",leftgain);
  1459. strcat(temp,temp2);
  1460. }
  1461. if(!flteq(rightgain,0.0)) {
  1462. sprintf(temp2," 1:%i",hi);
  1463. strcat(temp,temp2);
  1464. strcat(temp," ");
  1465. sprintf(temp2,"%lf",rightgain);
  1466. strcat(temp,temp2);
  1467. }
  1468. strcat(temp,"\n");
  1469. if(fputs(temp,dz->fp) < 0) {
  1470. sprintf(errstr,"Error writing to output data file\n");
  1471. return(PROGRAM_ERROR);
  1472. }
  1473. }
  1474. return FINISHED;
  1475. }
  1476. /**************************** HANDLE_THE_SPECIAL_DATA ****************************/
  1477. int handle_the_special_data(char *str,dataptr dz)
  1478. {
  1479. int n, m, k;
  1480. double *lspkrpos, *sort;
  1481. double dummy = 0.0, leftangle, rightangle, thispos, tempval;
  1482. FILE *fp;
  1483. int cnt;
  1484. char temp[200], *p;
  1485. if((dz->parray = (double **)malloc(3 * sizeof(double *)))==NULL) { // Arrays fpr lspkr positions, sound positions and sorting
  1486. sprintf(errstr,"INSUFFICIENT MEMORY for position storage arrays.\n");
  1487. return(MEMORY_ERROR);
  1488. }
  1489. if((fp = fopen(str,"r"))==NULL) {
  1490. sprintf(errstr,"Cannot open file %s to read loudspeaker locations.\n",str);
  1491. return(DATA_ERROR);
  1492. }
  1493. cnt = 0;
  1494. while(fgets(temp,200,fp)!=NULL) {
  1495. p = temp;
  1496. if(*p == ';') // Allow comments in file
  1497. continue;
  1498. while(get_float_from_within_string(&p,&dummy)) {
  1499. if(dummy < 0.0 || dummy > 360) {
  1500. sprintf(errstr,"Angular position (%lf) of loudspeaker %d out of range (0-360).\n",dummy,cnt+1);
  1501. return(DATA_ERROR);
  1502. }
  1503. if(flteq(dummy,360.0))
  1504. dummy = 0.0;
  1505. cnt++;
  1506. }
  1507. }
  1508. if(cnt > 16 || cnt < 3) {
  1509. sprintf(errstr,"Process only works with 3 or more (and no more than 16) loudspeakers. You have entered %d\n",cnt);
  1510. return(DATA_ERROR);
  1511. }
  1512. dz->itemcnt = cnt;
  1513. if((dz->parray[0] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) { // Array for loudspeaker positions
  1514. sprintf(errstr,"INSUFFICIENT MEMORY to store loudspeaker positions.\n");
  1515. return(MEMORY_ERROR);
  1516. } // Array for sound positions
  1517. if((dz->parray[1] = (double *)malloc(dz->infilecnt * sizeof(double)))==NULL) {
  1518. sprintf(errstr,"INSUFFICIENT MEMORY to store sound positions.\n");
  1519. return(MEMORY_ERROR);
  1520. } // Array for sorting lspkr AND snd positions
  1521. if((dz->parray[2] = (double *)malloc(max(dz->infilecnt,dz->itemcnt) * sizeof(double)))==NULL) {
  1522. sprintf(errstr,"INSUFFICIENT MEMORY to store loudspeaker positions sort.\n");
  1523. return(MEMORY_ERROR);
  1524. }
  1525. lspkrpos = dz->parray[0];
  1526. sort = dz->parray[2];
  1527. fseek(fp,0,0);
  1528. cnt = 0;
  1529. while(fgets(temp,200,fp)!=NULL) {
  1530. p = temp;
  1531. if(*p == ';') // Allow comments in file
  1532. continue;
  1533. while(get_float_from_within_string(&p,&dummy)) {
  1534. if(cnt > 0) {
  1535. for(n = 0; n < cnt; n++) {
  1536. if(fabs(lspkrpos[n] - dummy) < 1.0) {
  1537. sprintf(errstr,"Loudspeaker positions at %f and %f are unrealisatically close\n",lspkrpos[n],dummy);
  1538. return(DATA_ERROR);
  1539. }
  1540. }
  1541. }
  1542. lspkrpos[cnt] = dummy;
  1543. cnt++;
  1544. }
  1545. }
  1546. if(fclose(fp)<0) {
  1547. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",str);
  1548. fflush(stdout);
  1549. }
  1550. leftangle = 0.0;
  1551. rightangle = 0.0;
  1552. for(n = 0;n <cnt;n++) { // sort into angular ascending order
  1553. if(lspkrpos[n] > 180) {
  1554. thispos = 360 - lspkrpos[n];
  1555. if(thispos > leftangle)
  1556. leftangle = thispos;
  1557. } else if(lspkrpos[n] > rightangle)
  1558. rightangle = lspkrpos[n];
  1559. }
  1560. if(leftangle + rightangle <= 180) {
  1561. sprintf(errstr,"Loudspeakers do not encircle the listening area.\n");
  1562. return(DATA_ERROR);
  1563. }
  1564. for(n = 0;n <cnt-1;n++) { // sort into angular ascending order
  1565. for(m=n;m<cnt;m++) {
  1566. if(lspkrpos[n] > lspkrpos[m]) {
  1567. tempval = lspkrpos[n];
  1568. lspkrpos[n] = lspkrpos[m];
  1569. lspkrpos[m] = tempval;
  1570. }
  1571. }
  1572. }
  1573. sort = dz->parray[2];
  1574. for(n = 0;n <cnt;n++) { // sort into leftmost to rightmost order
  1575. if(lspkrpos[n] > 180) {
  1576. m = n;
  1577. for(k = 0;m < cnt;m++,k++)
  1578. sort[k] = lspkrpos[m];
  1579. for(m = 0;k < cnt;m++,k++)
  1580. sort[k] = lspkrpos[m];
  1581. for(m=0;m<cnt;m++)
  1582. lspkrpos[m] = sort[m];
  1583. break;
  1584. }
  1585. }
  1586. return(FINISHED);
  1587. }
  1588. /****************************** GET_THE_MODE_FROM_CMDLINE *********************************/
  1589. int get_the_mode_from_cmdline(char *str, dataptr dz)
  1590. {
  1591. if(sscanf(str,"%d",&dz->mode)!=1) {
  1592. sprintf(errstr,"Cannot read mode of program.\n");
  1593. fprintf(stdout,"INFO: %s\n",errstr);
  1594. fflush(stdout);
  1595. return(USAGE_ONLY);
  1596. }
  1597. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1598. sprintf(errstr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1599. fprintf(stdout,"INFO: %s\n",errstr);
  1600. fflush(stdout);
  1601. return(USAGE_ONLY);
  1602. }
  1603. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1604. return(FINISHED);
  1605. }
  1606. /************************************ PANCALC *******************************/
  1607. void pancalc(double position,double *leftgain,double *rightgain)
  1608. {
  1609. int dirflag;
  1610. double temp;
  1611. double relpos;
  1612. double reldist, invsquare;
  1613. if(position < 0.0)
  1614. dirflag = SIGNAL_TO_LEFT; /* signal on left */
  1615. else
  1616. dirflag = SIGNAL_TO_RIGHT;
  1617. if(position < 0)
  1618. relpos = -position;
  1619. else
  1620. relpos = position;
  1621. if(relpos <= 1.0){ /* between the speakers */
  1622. temp = 1.0 + (relpos * relpos);
  1623. reldist = ROOT2 / sqrt(temp);
  1624. temp = (position + 1.0) / 2.0;
  1625. *rightgain = temp * reldist;
  1626. *leftgain = (1.0 - temp ) * reldist;
  1627. } else { /* outside the speakers */
  1628. temp = (relpos * relpos) + 1.0;
  1629. reldist = sqrt(temp) / ROOT2; /* relative distance to source */
  1630. invsquare = 1.0 / (reldist * reldist);
  1631. if(dirflag == SIGNAL_TO_LEFT){
  1632. *leftgain = invsquare;
  1633. *rightgain = 0.0;
  1634. } else { /* SIGNAL_TO_RIGHT */
  1635. *rightgain = invsquare;
  1636. *leftgain = 0;
  1637. }
  1638. }
  1639. }