readdata.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. /* floatsam version*/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <float.h>
  25. #include <structures.h>
  26. #include <tkglobals.h>
  27. #include <globcon.h>
  28. #include <processno.h>
  29. #include <special.h>
  30. #include <cdpmain.h>
  31. #include <sfsys.h>
  32. #include <string.h> /*RWD*/
  33. #if defined unix || defined _MSC_VER
  34. #define round(x) lround((x))
  35. #endif
  36. static int get_params(int *cmdlinecnt,char ***cmdline,dataptr dz);
  37. static int get_options(int *cmdlinecnt,char ***cmdline,dataptr dz);
  38. static int get_variants_and_flags(int *cmdlinecnt,char ***cmdline,dataptr dz);
  39. static int get_option_no(char *paramstr,int *option_no,aplptr ap);
  40. static int get_variant_no(char *paramstr,int *variant_no,aplptr ap);
  41. static int get_brkpnt_data_from_file_and_test_it(char *filename,int paramno,dataptr dz);
  42. static int read_param_as_value_or_brkfile_and_check_range(char *str,int paramno,dataptr dz);
  43. static int get_real_paramno(int paramno,dataptr dz);
  44. static void out_of_range(int paramno,double val,double loval,double hival);
  45. /************************ READ_PARAMETERS_AND_FLAGS **********************/
  46. int read_parameters_and_flags(char ***cmdline,int *cmdlinecnt,dataptr dz)
  47. {
  48. int exit_status;
  49. aplptr ap = dz->application;
  50. if(ap->param_cnt) {
  51. if(!sloom) {
  52. if(*cmdlinecnt <= 0) {
  53. sprintf(errstr,"Insufficient parameters on command line.\n");
  54. return(USAGE_ONLY);
  55. }
  56. }
  57. if((exit_status = get_params(cmdlinecnt,cmdline,dz))<0)
  58. return(exit_status);
  59. }
  60. if(ap->option_cnt) {
  61. if((exit_status = get_options(cmdlinecnt,cmdline,dz))<0)
  62. return(exit_status);
  63. }
  64. if(ap->vflag_cnt) {
  65. if((exit_status = get_variants_and_flags(cmdlinecnt,cmdline,dz))<0)
  66. return(exit_status);
  67. }
  68. if(!sloom) {
  69. if(*cmdlinecnt > 0) {
  70. if((*cmdline)[0][0]=='-') {
  71. if(strlen((*cmdline)[0])>1)
  72. sprintf(errstr,"Unknown flag -%c on command line.\n",(*cmdline)[0][1]);
  73. else
  74. sprintf(errstr,"Hanging '-' on command line.\n");
  75. } else
  76. sprintf(errstr,"Too many parameters on command line.\n");
  77. return(USAGE_ONLY);
  78. }
  79. }
  80. return(FINISHED);
  81. }
  82. /************************ GET_PARAMS **********************/
  83. int get_params(int *cmdlinecnt,char ***cmdline,dataptr dz)
  84. {
  85. int exit_status;
  86. int n;
  87. char *paramval;
  88. aplptr ap = dz->application;
  89. for(n=0;n<ap->max_param_cnt;n++) {
  90. if(dz->is_active[n]) {
  91. if(!sloom) {
  92. if(*cmdlinecnt<=0) {
  93. sprintf(errstr,"Insufficient parameters on cmdline.\n");
  94. return(USER_ERROR);
  95. }
  96. }
  97. paramval = (*cmdline)[0];
  98. if((exit_status = read_param_as_value_or_brkfile_and_check_range(paramval,n,dz)) < 0)
  99. return(exit_status);
  100. (*cmdline)++;
  101. (*cmdlinecnt)--;
  102. }
  103. }
  104. return(FINISHED);
  105. }
  106. /************************ GET_OPTIONS **********************/
  107. int get_options(int *cmdlinecnt,char ***cmdline,dataptr dz)
  108. {
  109. int exit_status;
  110. aplptr ap = dz->application;
  111. int n, basecnt = ap->max_param_cnt;
  112. int m = 0;
  113. char *paramstr;
  114. int* options_got;
  115. int option_no = 0, k = 0;
  116. int options_remain = TRUE;
  117. if(!sloom) {
  118. if((options_got = (int *)malloc(ap->option_cnt * sizeof(int)))==NULL) {
  119. sprintf(errstr,"INSUFFICIENT MEMORY for option checking array\n");
  120. return(MEMORY_ERROR);
  121. }
  122. for(n=0;n<ap->option_cnt;n++)
  123. options_got[n]=FALSE;
  124. while(*cmdlinecnt > 0 && options_remain) {
  125. paramstr = (*cmdline)[0];
  126. exit_status = get_option_no(paramstr,&option_no,dz->application);
  127. switch(exit_status) {
  128. case(CONTINUE):
  129. if(options_got[option_no]==TRUE) {
  130. sprintf(errstr,"Duplicate option %c used on command line\n",*(paramstr+1));
  131. return(USAGE_ONLY);
  132. }
  133. options_got[option_no] = TRUE;
  134. paramstr += 2;
  135. k = basecnt + option_no;
  136. if((exit_status = read_param_as_value_or_brkfile_and_check_range(paramstr,k,dz))<0)
  137. return(exit_status);
  138. (*cmdline)++;
  139. (*cmdlinecnt)--;
  140. break;
  141. case(FINISHED):
  142. options_remain = FALSE;
  143. break;
  144. default:
  145. return(exit_status);
  146. }
  147. }
  148. free(options_got);
  149. } else {
  150. for(n=0,m=basecnt;n<ap->option_cnt;n++,m++) {
  151. //TW JULY 2006
  152. if(dz->is_active[m]) {
  153. paramstr = (*cmdline)[0];
  154. if((exit_status = read_param_as_value_or_brkfile_and_check_range(paramstr,m,dz))<0)
  155. return(exit_status);
  156. (*cmdline)++;
  157. (*cmdlinecnt)--;
  158. }
  159. }
  160. }
  161. return(FINISHED);
  162. }
  163. /************************ GET_VARIANTS_AND_FLAGS **********************/
  164. int get_variants_and_flags(int *cmdlinecnt,char ***cmdline,dataptr dz)
  165. {
  166. int exit_status;
  167. aplptr ap = dz->application;
  168. int basecnt = ap->max_param_cnt + ap->option_cnt;
  169. char *paramstr;
  170. int flagno, paramno;
  171. int* flags_got;
  172. int n = 0, rawflags = 0;
  173. if(!sloom) {
  174. if((flags_got = (int *)malloc(ap->vflag_cnt * sizeof(int)))==NULL) {
  175. sprintf(errstr,"INSUFFICIENT MEMORY for variant checking array\n");
  176. return(MEMORY_ERROR);
  177. }
  178. for(flagno=0;flagno<ap->vflag_cnt;flagno++)
  179. flags_got[flagno]=FALSE;
  180. while(*cmdlinecnt > 0) {
  181. paramstr = (*cmdline)[0];
  182. if((exit_status = get_variant_no(paramstr,&flagno,ap))<0)
  183. return(exit_status);
  184. if(flags_got[flagno]==TRUE) {
  185. sprintf(errstr,"Duplicate flag %c used on command line\n",*(paramstr+1));
  186. return(USER_ERROR);
  187. }
  188. flags_got[flagno] = TRUE;
  189. if(flagno < ap->variant_param_cnt) {
  190. paramstr += 2;
  191. paramno = basecnt+flagno;
  192. if((exit_status = read_param_as_value_or_brkfile_and_check_range(paramstr,paramno,dz))<0)
  193. return(exit_status);
  194. if(dz->brksize[paramno] || !flteq(dz->param[paramno],ap->default_val[paramno]))
  195. dz->vflag[flagno] = TRUE;
  196. } else
  197. dz->vflag[flagno] = TRUE;
  198. (*cmdline)++;
  199. (*cmdlinecnt)--;
  200. }
  201. free(flags_got);
  202. } else {
  203. rawflags = ap->vflag_cnt - ap->variant_param_cnt;
  204. for(flagno=0,paramno=basecnt;flagno<ap->variant_param_cnt;flagno++,paramno++) {
  205. paramstr = (*cmdline)[0];
  206. if((exit_status = read_param_as_value_or_brkfile_and_check_range(paramstr,paramno,dz))<0)
  207. return(exit_status);
  208. if(dz->brksize[paramno] || !flteq(dz->param[paramno],ap->default_val[paramno]))
  209. dz->vflag[flagno] = TRUE;
  210. (*cmdline)++;
  211. (*cmdlinecnt)--;
  212. }
  213. if(rawflags > 0) {
  214. basecnt = ap->variant_param_cnt;
  215. for(n=0,flagno = basecnt;n<rawflags;n++,flagno++) {
  216. paramstr = (*cmdline)[0];
  217. /* NEW JUNE 1999 ---> */
  218. if (strlen(paramstr) < 2) {
  219. sprintf(errstr,"Unmarked numeric val for flag %s\n",paramstr); /*RWD 4:12:2003 was $s */
  220. return(DATA_ERROR);
  221. }
  222. paramstr++;
  223. /* <--- NEW JUNE 1999 */
  224. if(!strcmp(paramstr,"0"))
  225. ; /* flag = FALSE, default */
  226. else if(!strcmp(paramstr,"1"))
  227. dz->vflag[flagno] = TRUE;
  228. else {
  229. sprintf(errstr,"Unknown flag value '%s' sent from TK\n",paramstr);
  230. return(DATA_ERROR);
  231. }
  232. (*cmdline)++;
  233. (*cmdlinecnt)--;
  234. }
  235. }
  236. }
  237. return(FINISHED);
  238. }
  239. /************************************* GET_OPTION_NO ****************************************/
  240. int get_option_no(char *paramstr,int *option_no,aplptr ap)
  241. {
  242. char cmdline_flag, *p;
  243. if(*paramstr++!='-') {
  244. sprintf(errstr,"Unknown parameter '%s'\n",--paramstr);
  245. return(USAGE_ONLY);
  246. }
  247. cmdline_flag = *paramstr++;
  248. p = ap->option_flags;
  249. while(*p != ENDOFSTR) {
  250. if(*p == cmdline_flag) {
  251. *option_no = p - ap->option_flags;
  252. //TW JULY 2006
  253. if(ap->option_list[*option_no]=='0') {
  254. fprintf(stdout,"-%c is not a valid flag\n",cmdline_flag);
  255. fflush(stdout);
  256. return(USAGE_ONLY);
  257. }
  258. p++;
  259. if(strlen(paramstr) <=0) {
  260. sprintf(errstr,"option parameter missing with flag -%c\n",cmdline_flag);
  261. return(USAGE_ONLY);
  262. }
  263. return(CONTINUE);
  264. }
  265. p++;
  266. }
  267. return(FINISHED);
  268. }
  269. /************************************ GET_VARIANT_NO ************************************/
  270. int get_variant_no(char *paramstr,int *variant_no,aplptr ap)
  271. {
  272. char cmdline_flag, *p;
  273. if(*paramstr++!='-') {
  274. sprintf(errstr,"Unknown parameter '%s'",--paramstr);
  275. return(USER_ERROR);
  276. }
  277. cmdline_flag = *paramstr++;
  278. p = ap->variant_flags;
  279. while(*p != ENDOFSTR) {
  280. if(*p == cmdline_flag) {
  281. *variant_no = p - ap->variant_flags;
  282. p++;
  283. if(*variant_no < ap->variant_param_cnt && strlen(paramstr) <=0) {
  284. sprintf(errstr,"variant parameter missing with flag -%c\n",cmdline_flag);
  285. return(USER_ERROR);
  286. }
  287. return(CONTINUE);
  288. }
  289. p++;
  290. }
  291. if(ap->option_cnt) {
  292. p = ap->option_flags;
  293. while(*p != ENDOFSTR) {
  294. if(*p == cmdline_flag) {
  295. sprintf(errstr,"option flag -%c out of order on cmdline.\n",cmdline_flag);
  296. return(USER_ERROR);
  297. }
  298. p++;
  299. }
  300. sprintf(errstr,"Unknown variant flag -%c\n",cmdline_flag);
  301. return(USER_ERROR);
  302. } else {
  303. sprintf(errstr,"Unknown flag '-%c'\n",cmdline_flag);
  304. return(USER_ERROR);
  305. }
  306. return(FINISHED);
  307. }
  308. /***************** READ_PARAM_AS_VALUE_OR_BRKFILE_AND_CHECK_RANGE ********************/
  309. int read_param_as_value_or_brkfile_and_check_range(char *str,int paramno,dataptr dz)
  310. {
  311. aplptr ap = dz->application;
  312. int real_paramno;
  313. if(!sloom) { /* CMDLINE convention: filenames can't begin with numbers : */
  314. //TW Soundloom accepts numeric names, and extensions
  315. // 'value_is_numeric' Commandline still looks for numeric value first,
  316. // and if it finds it, assumes it's a number (if user uses filename "123.456" that's the user's problem)
  317. // New 'file_has_invalid_startchar' traps bad directory paths, but permits numeric names
  318. if(!value_is_numeric(str) && file_has_invalid_startchar(str)) {
  319. sprintf(errstr,"Cannot read parameter %d [%s]\n",paramno+1,str);
  320. return(USER_ERROR);
  321. }
  322. if(value_is_numeric(str)) {
  323. if(sscanf(str,"%lf",&(dz->param[paramno]))<1) {
  324. sprintf(errstr,"Cannot read parameter %d [%s].\n",paramno+1,str);
  325. return(USER_ERROR);
  326. }
  327. if(dz->process != ENV_DOVETAILING && dz->process != ENV_CURTAILING) {
  328. if(flteq(dz->param[paramno],ap->lo[paramno]))
  329. dz->param[paramno] = ap->lo[paramno];
  330. if(flteq(dz->param[paramno],ap->hi[paramno]))
  331. dz->param[paramno] = ap->hi[paramno];
  332. if(dz->param[paramno] > ap->hi[paramno] || dz->param[paramno] < ap->lo[paramno]) {
  333. real_paramno = get_real_paramno(paramno,dz);
  334. out_of_range(real_paramno,dz->param[paramno],ap->lo[paramno],ap->hi[paramno]);
  335. return(USER_ERROR);
  336. }
  337. }
  338. if(dz->is_int[paramno])
  339. dz->iparam[paramno] = round(dz->param[paramno]);
  340. } else {
  341. if(dz->no_brk[paramno]) {
  342. sprintf(errstr,"Cannot read parameter %d [%s]: brkpnt_files not permitted.\n",paramno+1,str);
  343. return(USER_ERROR);
  344. } else
  345. return get_brkpnt_data_from_file_and_test_it(str,paramno,dz);
  346. }
  347. } else { /* TK convention, all numeric values are preceded by NUMERICVAL_MARKER */
  348. if(str[0]==NUMERICVAL_MARKER) {
  349. str++;
  350. if(strlen(str)<=0 || sscanf(str,"%lf",&(dz->param[paramno]))!=1) {
  351. sprintf(errstr,"Invalid parameter value encountered.\n");
  352. return(DATA_ERROR);
  353. }
  354. if(dz->process != ENV_DOVETAILING && dz->process != ENV_CURTAILING) {
  355. if(flteq(dz->param[paramno],ap->lo[paramno]))
  356. dz->param[paramno] = ap->lo[paramno];
  357. if(flteq(dz->param[paramno],ap->hi[paramno]))
  358. dz->param[paramno] = ap->hi[paramno];
  359. if(dz->param[paramno] > ap->hi[paramno] || dz->param[paramno] < ap->lo[paramno]) {
  360. real_paramno = get_real_paramno(paramno,dz);
  361. out_of_range(real_paramno,dz->param[paramno],ap->lo[paramno],ap->hi[paramno]);
  362. return(USER_ERROR);
  363. }
  364. }
  365. if(dz->is_int[paramno])
  366. dz->iparam[paramno] = round(dz->param[paramno]);
  367. } else {
  368. if(dz->no_brk[paramno]) {
  369. sprintf(errstr,"Cannot read parameter %d [%s]: brkpnt_files not permitted.\n",paramno+1,str);
  370. return(USER_ERROR);
  371. }
  372. else
  373. return get_brkpnt_data_from_file_and_test_it(str,paramno,dz);
  374. }
  375. }
  376. return(FINISHED);
  377. }
  378. /************************************* GET_REAL_PARAMNO ****************************************/
  379. int get_real_paramno(int paramno,dataptr dz)
  380. {
  381. int m = 0, n;
  382. for(n=0;n <= paramno;n++) {
  383. if(dz->is_active[n])
  384. m++;
  385. }
  386. if(dz->application->special_data)
  387. m++;
  388. return(m);
  389. }
  390. /*************************** GET_BRKPNT_DATA_FROM_FILE_AND_TEST_IT ***********************/
  391. int get_brkpnt_data_from_file_and_test_it(char *filename,int paramno,dataptr dz)
  392. {
  393. FILE *fp;
  394. aplptr ap = dz->application;
  395. double *p, lasttime = 0.0;
  396. int istime = 1;
  397. int arraysize = BIGARRAY;
  398. char temp[200], *q;
  399. int n = 0, dcount;
  400. if((fp = fopen(filename,"r"))==NULL) {
  401. sprintf(errstr, "Can't open brkpntfile %s to read data.\n",filename);
  402. return(DATA_ERROR);
  403. }
  404. if((dz->brk[paramno] = (double *)malloc(arraysize * sizeof(double)))==NULL) {
  405. sprintf(errstr,"INSUFFICIENT MEMORY for brkpnt data in file %s.\n",filename);
  406. return(MEMORY_ERROR);
  407. }
  408. p = dz->brk[paramno];
  409. while(fgets(temp,200,fp)==temp) {
  410. q = temp;
  411. while(get_float_from_within_string(&q,p)) {
  412. if(istime) {
  413. if(p!= dz->brk[paramno] && *p <= lasttime) {
  414. sprintf(errstr,"Times (%lf & %lf) in brkpntfile %s are not in increasing order.\n",
  415. lasttime,*p,filename);
  416. return(DATA_ERROR);
  417. }
  418. lasttime = *p;
  419. } else {
  420. if(flteq(*p,ap->lo[paramno]))
  421. *p = ap->lo[paramno];
  422. else if(flteq(*p,ap->hi[paramno]))
  423. *p = ap->hi[paramno];
  424. if(*p < ap->lo[paramno] || *p > ap->hi[paramno]) {
  425. out_of_range_in_brkfile(filename,*p,ap->lo[paramno],ap->hi[paramno]);
  426. return(DATA_ERROR);
  427. }
  428. }
  429. istime = !istime;
  430. p++;
  431. if(++n >= arraysize) {
  432. arraysize += BIGARRAY;
  433. if((dz->brk[paramno] = (double *)realloc((char *)(dz->brk[paramno]),arraysize * sizeof(double)))==NULL) {
  434. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate brkpnt data from file %s.\n",filename);
  435. return(MEMORY_ERROR);
  436. }
  437. p = dz->brk[paramno] + n;
  438. }
  439. }
  440. }
  441. if(n == 0) {
  442. sprintf(errstr,"No data in brkpnt file %s\n",filename);
  443. return(DATA_ERROR);
  444. }
  445. if((dz->brk[paramno] = (double *)realloc((char *)(dz->brk[paramno]),n * sizeof(double)))==NULL) {
  446. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate brkpnt data from file %s.\n",filename);
  447. return(MEMORY_ERROR);
  448. }
  449. if(((dcount = n/2) * 2) != n) {
  450. sprintf(errstr,"Data not paired correctly in file %s\n",filename);
  451. return(DATA_ERROR);
  452. }
  453. if(fclose(fp)<0) {
  454. fprintf(stdout,"WARNING: Failed to close file %s.\n",filename);
  455. fflush(stdout);
  456. }
  457. dz->brksize[paramno] = dcount;
  458. dz->brkptr[paramno] = dz->brk[paramno]; /* initialise brkptr */
  459. return(FINISHED);
  460. }
  461. /**************************** OUT_OF_RANGE_IN_BRKFILE *************************/
  462. void out_of_range_in_brkfile(char *filename,double val,double loval,double hival)
  463. {
  464. sprintf(errstr,"Value (%lf) out of range (%lf to %lf) in brkpntfile %s.\n",val,loval,hival,filename);
  465. }
  466. /**************************** OUT_OF_RANGE *************************/
  467. void out_of_range(int paramno,double val,double loval,double hival)
  468. {
  469. sprintf(errstr,"Parameter[%d] Value (%lf) out of range (%lf to %lf)\n",paramno,val,loval,hival);
  470. }
  471. /************************* READ_AND_TEST_PITCH_OR_TRANSPOSITION_BRKVALS *****************************/
  472. int read_and_test_pitch_or_transposition_brkvals
  473. (FILE *fp,char *filename,double **brktable,int *brksize,int which_type,double minval,double maxval)
  474. {
  475. int arraysize = BIGARRAY;
  476. double *p, lasttime = 0.0;
  477. int istime = TRUE;
  478. int n = 0,m, final_size;
  479. char temp[200], *q;
  480. if((*brktable = (double *)malloc(arraysize * sizeof(double)))==NULL) {
  481. sprintf(errstr,"INSUFFICIENT MEMORY for pitch-or-transposition brktable.\n");
  482. return(MEMORY_ERROR);
  483. }
  484. p = *brktable;
  485. while(fgets(temp,200,fp)==temp) { /* READ AND TEST BRKPNT VALS */
  486. q = temp;
  487. while(get_float_from_within_string(&q,p)) {
  488. if(istime) {
  489. if(p==*brktable) {
  490. if(*p < 0.0) {
  491. sprintf(errstr,"First timeval(%lf) in brkpntfile %s is less than zero.\n",*p,filename);
  492. return(DATA_ERROR);
  493. }
  494. } else {
  495. if(*p <= lasttime) {
  496. sprintf(errstr,"Times (%lf & %lf) in brkpntfile %s are not in increasing order.\n",
  497. lasttime,*p,filename);
  498. return(DATA_ERROR);
  499. }
  500. }
  501. lasttime = *p;
  502. } else {
  503. switch(which_type) {
  504. case(TRANSPOS_SEMIT_OR_CONSTANT):
  505. *p = *p/12.0; /* semitone to octave */
  506. /* fall thro */
  507. case(TRANSPOS_OCTAVE_OR_CONSTANT):
  508. *p = pow(2.0,*p); /* octave to frqratio */
  509. break;
  510. case(NO_SPECIAL_TYPE):
  511. break;
  512. }
  513. if(flteq(*p,minval))
  514. *p = minval;
  515. else if(flteq(*p,maxval))
  516. *p = maxval;
  517. if(*p < minval || *p > maxval) {
  518. out_of_range_in_brkfile(filename,*p,minval,maxval);
  519. return(DATA_ERROR);
  520. }
  521. }
  522. istime = !istime;
  523. p++;
  524. if(++n >= arraysize) {
  525. arraysize += BIGARRAY;
  526. if((*brktable = (double *)realloc((char *)(*brktable),arraysize * sizeof(double)))==NULL) {
  527. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate pitch-or-transposition brktable.\n");
  528. return(MEMORY_ERROR);
  529. }
  530. p = *brktable + n;
  531. }
  532. }
  533. }
  534. if(n < 2) {
  535. sprintf(errstr,"No data in brkpnt file %s\n",filename);
  536. return(DATA_ERROR);
  537. }
  538. if(ODD(n)) {
  539. sprintf(errstr,"Data not paired correctly in brkpntfile %s\n",filename);
  540. return(DATA_ERROR);
  541. }
  542. final_size = n;
  543. if((*brktable)[0] != 0.0) /* Allow space for a value at time zero, if there isn't one */
  544. final_size = n + 2;
  545. if((*brktable = (double *)realloc((char *)(*brktable),final_size * sizeof(double)))==NULL) {
  546. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate pitch-or-transposition brktable.\n");
  547. return(MEMORY_ERROR);
  548. }
  549. if(final_size != n) { /* Force a value at time zero, if there isn't one */
  550. for(m=n-1;m>=0;m--)
  551. (*brktable)[m+2] = (*brktable)[m];
  552. (*brktable)[0] = 0.0;
  553. (*brktable)[1] = (*brktable)[3];
  554. }
  555. *brksize = final_size/2;
  556. return(FINISHED);
  557. }
  558. /************************* CONVERT_TO_WINDOW_BY_WINDOW_ARRAY *****************************/
  559. int convert_brkpntdata_to_window_by_window_array(double *brktable,int brksize,float **thisarray,int wlen,float timestep)
  560. {
  561. int exit_status;
  562. double *p, val;
  563. int arraysize = BIGARRAY, n = 0;
  564. double ttime = 0.0;
  565. double firstval, lasttime, lastval;
  566. double *endpair, *endoftab;
  567. if((*thisarray = (float *)malloc(arraysize * sizeof(float)))==NULL) {
  568. sprintf(errstr,"INSUFFICIENT MEMORY to create window array.\n");
  569. return(MEMORY_ERROR);
  570. }
  571. p = brktable;
  572. firstval = *(p+1);
  573. endoftab = brktable + (brksize*2);
  574. endpair = endoftab - 2;
  575. lasttime = *endpair;
  576. lastval = *(endpair+1);
  577. while(n < wlen) {
  578. if(ttime <= 0.0) (*thisarray)[n] = (float)firstval;
  579. else if(ttime >= lasttime) (*thisarray)[n] = (float)lastval;
  580. else {
  581. if((exit_status = interp_val(&val,ttime,brktable,endoftab,&p))<0)
  582. return(exit_status);
  583. (*thisarray)[n] = (float)val;
  584. }
  585. if(++n >= arraysize) {
  586. arraysize += BIGARRAY;
  587. if((*thisarray = (float *)realloc((char *)(*thisarray),arraysize*sizeof(float)))==NULL) {
  588. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate window array.\n");
  589. return(MEMORY_ERROR);
  590. }
  591. }
  592. ttime += timestep;
  593. }
  594. if((*thisarray = (float *)realloc((char *)(*thisarray),(wlen+1) * sizeof(float)))==NULL) {
  595. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate window array.\n");
  596. return(MEMORY_ERROR);
  597. }
  598. (*thisarray)[wlen] = 0.0f; /* SAFETY VALUE */
  599. return(FINISHED);
  600. }
  601. /********************** INTERP_VAL *******************/
  602. int interp_val(double *val,double ttime,double *startoftab,double *endoftab,double **p)
  603. {
  604. double hival, hiind, loval, loind, dval;
  605. if(ttime > **p) {
  606. while(**p<ttime) {
  607. if((*p += 2) >= endoftab) {
  608. sprintf(errstr,"Time off end of table :interp_val()\n");
  609. return(PROGRAM_ERROR);
  610. }
  611. }
  612. }
  613. if(*p < startoftab+2) {
  614. sprintf(errstr,"Pointer off start of table: Error in interp_val()\n");
  615. return(PROGRAM_ERROR);
  616. }
  617. hival = *(*p+1);
  618. hiind = **p;
  619. loval = *(*p-1);
  620. loind = *(*p-2);
  621. dval = (ttime - loind)/(hiind - loind);
  622. dval *= (hival - loval);
  623. dval += loval;
  624. *val = dval;
  625. return(FINISHED);
  626. }
  627. /**************************** CONVERT_DB_AT_OR_BELOW_ZERO_TO_GAIN ********************************/
  628. int convert_dB_at_or_below_zero_to_gain(double *val)
  629. {
  630. if (*val>0.0) {
  631. sprintf(errstr,"dB value out of range (> 0dB)\n");
  632. return(DATA_ERROR);
  633. } else if(*val<=MIN_DB_ON_16_BIT)
  634. *val = 0.0;
  635. else if(flteq(*val,0.0))
  636. *val = 1.0;
  637. else /* *val<0.0 */ {
  638. *val = -(*val);
  639. *val /= 20.0;
  640. *val = pow(10.0,*val);
  641. *val = 1.0/(*val);
  642. }
  643. return(FINISHED);
  644. }
  645. /*************************** GET_MAXVALUE_IN_BRKTABLE ************************/
  646. int get_maxvalue_in_brktable(double *brkmax,int paramno,dataptr dz)
  647. {
  648. double *p = dz->brk[paramno];
  649. int n;
  650. *brkmax = -DBL_MAX;
  651. if(dz->brksize[paramno] <= 0) {
  652. sprintf(errstr,"Brktable is empty.\n");
  653. return(DATA_ERROR);
  654. }
  655. p++;
  656. for(n=0;n<dz->brksize[paramno];n++) {
  657. if(*p > *brkmax)
  658. *brkmax = *p;
  659. p+=2;
  660. }
  661. if(*brkmax <= -DBL_MAX) {
  662. sprintf(errstr,"Invalid values in brktable.\n");
  663. return(DATA_ERROR);
  664. }
  665. return(FINISHED);
  666. }
  667. /*************************** GET_MINVALUE_IN_BRKTABLE ************************/
  668. int get_minvalue_in_brktable(double *brkmin,int paramno,dataptr dz)
  669. {
  670. double *p = dz->brk[paramno];
  671. int n;
  672. *brkmin = DBL_MAX;
  673. if(dz->brksize[paramno] <= 0) {
  674. sprintf(errstr,"Brktable is empty.\n");
  675. return(DATA_ERROR);
  676. }
  677. p++;
  678. for(n=0;n<dz->brksize[paramno];n++) {
  679. if(*p < *brkmin)
  680. *brkmin = *p;
  681. p+=2;
  682. }
  683. if(*brkmin >= DBL_MAX) {
  684. sprintf(errstr,"Invalid values in brktable.\n");
  685. return(DATA_ERROR);
  686. }
  687. return(FINISHED);
  688. }
  689. /********************* GET_MAXVALUE *********************/
  690. int get_maxvalue(int paramno,double *maxval,dataptr dz)
  691. {
  692. int exit_status;
  693. if(dz->brksize==NULL) {
  694. sprintf(errstr,"brksize array not initialised: get_maxvalue()\n");
  695. return(PROGRAM_ERROR);
  696. }
  697. if(dz->brksize[paramno]) {
  698. if((exit_status = get_maxvalue_in_brktable(maxval,paramno,dz))<0)
  699. return(exit_status);
  700. } else {
  701. if(dz->is_int[paramno])
  702. *maxval = (double)dz->iparam[paramno];
  703. else
  704. *maxval = dz->param[paramno];
  705. }
  706. return(FINISHED);
  707. }