ap_focus.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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 <cdpmain.h>
  25. #include <tkglobals.h>
  26. #include <pnames.h>
  27. #include <focus.h>
  28. #include <processno.h>
  29. #include <modeno.h>
  30. #include <globcon.h>
  31. #include <logic.h>
  32. #include <filetype.h>
  33. #include <mixxcon.h>
  34. #include <flags.h>
  35. #include <speccon.h>
  36. #include <arrays.h>
  37. #include <special.h>
  38. #include <formants.h>
  39. #include <sfsys.h>
  40. #include <osbind.h>
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <math.h>
  44. #include <srates.h>
  45. //#ifdef unix
  46. #define round(x) lround((x))
  47. //#endif
  48. /********************************************************************************************/
  49. /********************************** FORMERLY IN pconsistency.c ******************************/
  50. /********************************************************************************************/
  51. static int check_consistency_of_frq_limits_for_focus(dataptr dz);
  52. /********************************************************************************************/
  53. /********************************** FORMERLY IN preprocess.c ********************************/
  54. /********************************************************************************************/
  55. static int scale_accu_parameters(dataptr dz);
  56. static int convert_params_for_exag(dataptr dz);
  57. static int setup_internal_arrays_and_params_for_focus(dataptr dz);
  58. static int generate_focu_filter_params(dataptr dz);
  59. static int establish_internal_arrays_for_focus(dataptr dz);
  60. static int fold_preprocess(dataptr dz);
  61. static int get_frztimes(dataptr dz);
  62. static int rationalise_frztimes(dataptr dz);
  63. static int convert_params_for_step(dataptr dz);
  64. /********************************************************************************************/
  65. /********************************** FORMERLY IN specialin.c *********************************/
  66. /********************************************************************************************/
  67. static int read_specfreeze_data(char *filename,dataptr dz);
  68. static int read_specfreeze2_data(char *filename,dataptr dz);
  69. static int set_timetype(char **first_char_of_string,int strno,int *is_direction_info,dataptr dz);
  70. static int read_time(char **p,double *dtime,int strno,int linecnt, char *filename);
  71. static int insert_values_at_time_zero(int *n,dataptr dz);
  72. static int check_time_sequencing(int n,char *filename,dataptr dz);
  73. /***************************************************************************************/
  74. /****************************** FORMERLY IN aplinit.c **********************************/
  75. /***************************************************************************************/
  76. /***************************** ESTABLISH_BUFPTRS_AND_EXTRA_BUFFERS **************************/
  77. int establish_bufptrs_and_extra_buffers(dataptr dz)
  78. {
  79. int exit_status;
  80. int is_spec = FALSE;
  81. dz->extra_bufcnt = -1; /* ENSURE EVERY CASE HAS A PAIR OF ENTRIES !! */
  82. dz->bptrcnt = 0;
  83. dz->bufcnt = 0;
  84. switch(dz->process) {
  85. case(ACCU): dz->extra_bufcnt = 1; dz->bptrcnt = 1; is_spec = TRUE; break;
  86. case(EXAG): dz->extra_bufcnt = 0; dz->bptrcnt = 1; is_spec = TRUE; break;
  87. case(FOCUS): dz->extra_bufcnt = 0; dz->bptrcnt = 4; is_spec = TRUE; break;
  88. case(FOLD): dz->extra_bufcnt = 0; dz->bptrcnt = 1; is_spec = TRUE; break;
  89. case(FREEZE): dz->extra_bufcnt = 1; dz->bptrcnt = 4; is_spec = TRUE; break;
  90. case(FREEZE2): dz->extra_bufcnt = 0; dz->bptrcnt = 4; is_spec = TRUE; break;
  91. case(STEP): dz->extra_bufcnt = 1; dz->bptrcnt = 2; is_spec = TRUE; break;
  92. default:
  93. sprintf(errstr,"Unknown program type [%d] in establish_bufptrs_and_extra_buffers()\n",dz->process);
  94. return(PROGRAM_ERROR);
  95. }
  96. if(dz->extra_bufcnt < 0) {
  97. sprintf(errstr,"bufcnts have not been set: establish_bufptrs_and_extra_buffers()\n");
  98. return(PROGRAM_ERROR);
  99. }
  100. if(is_spec)
  101. return establish_spec_bufptrs_and_extra_buffers(dz);
  102. else if((dz->process==HOUSE_SPEC && dz->mode==HOUSE_CONVERT) || dz->process==INFO_DIFF) {
  103. if((exit_status = establish_spec_bufptrs_and_extra_buffers(dz))<0)
  104. return(exit_status);
  105. }
  106. return establish_groucho_bufptrs_and_extra_buffers(dz);
  107. }
  108. /***************************** SETUP_INTERNAL_ARRAYS_AND_ARRAY_POINTERS **************************/
  109. int setup_internal_arrays_and_array_pointers(dataptr dz)
  110. {
  111. int n;
  112. dz->ptr_cnt = -1; /* base constructor...process */
  113. dz->array_cnt = -1;
  114. dz->iarray_cnt = -1;
  115. dz->larray_cnt = -1;
  116. switch(dz->process) {
  117. case(ACCU): dz->array_cnt = 0; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  118. case(EXAG): dz->array_cnt = 0; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  119. case(FOCUS): dz->array_cnt = 0; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  120. case(FOLD): dz->array_cnt = 0; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  121. case(FREEZE): dz->array_cnt = 0; dz->iarray_cnt =1; dz->larray_cnt =2; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  122. case(FREEZE2): dz->array_cnt = 0; dz->iarray_cnt =1; dz->larray_cnt =2; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  123. case(STEP): dz->array_cnt = 0; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  124. }
  125. /*** WARNING ***
  126. ANY APPLICATION DEALING WITH A NUMLIST INPUT: MUST establish AT LEAST 1 double array: i.e. dz->array_cnt = at least 1
  127. **** WARNING ***/
  128. if(dz->array_cnt < 0 || dz->iarray_cnt < 0 || dz->larray_cnt < 0 || dz->ptr_cnt < 0 || dz->fptr_cnt < 0) {
  129. sprintf(errstr,"array_cnt not set in setup_internal_arrays_and_array_pointers()\n");
  130. return(PROGRAM_ERROR);
  131. }
  132. if(dz->array_cnt > 0) {
  133. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  134. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  135. return(MEMORY_ERROR);
  136. }
  137. for(n=0;n<dz->array_cnt;n++)
  138. dz->parray[n] = NULL;
  139. }
  140. if(dz->iarray_cnt > 0) {
  141. if((dz->iparray = (int **)malloc(dz->iarray_cnt * sizeof(int *)))==NULL) {
  142. sprintf(errstr,"INSUFFICIENT MEMORY for internal int arrays.\n");
  143. return(MEMORY_ERROR);
  144. }
  145. for(n=0;n<dz->iarray_cnt;n++)
  146. dz->iparray[n] = NULL;
  147. }
  148. if(dz->larray_cnt > 0) {
  149. if((dz->lparray = (int **)malloc(dz->larray_cnt * sizeof(int *)))==NULL) {
  150. sprintf(errstr,"INSUFFICIENT MEMORY for internal long arrays.\n");
  151. return(MEMORY_ERROR);
  152. }
  153. for(n=0;n<dz->larray_cnt;n++)
  154. dz->lparray[n] = NULL;
  155. }
  156. if(dz->ptr_cnt > 0) {
  157. if((dz->ptr = (double **)malloc(dz->ptr_cnt * sizeof(double *)))==NULL) {
  158. sprintf(errstr,"INSUFFICIENT MEMORY for internal pointer arrays.\n");
  159. return(MEMORY_ERROR);
  160. }
  161. for(n=0;n<dz->ptr_cnt;n++)
  162. dz->ptr[n] = NULL;
  163. }
  164. if(dz->fptr_cnt > 0) {
  165. if((dz->fptr = (float **)malloc(dz->fptr_cnt * sizeof(float *)))==NULL) {
  166. sprintf(errstr,"INSUFFICIENT MEMORY for internal float-pointer arrays.\n");
  167. return(MEMORY_ERROR);
  168. }
  169. for(n=0;n<dz->fptr_cnt;n++)
  170. dz->fptr[n] = NULL;
  171. }
  172. return(FINISHED);
  173. }
  174. /****************************** ASSIGN_PROCESS_LOGIC *********************************/
  175. int assign_process_logic(dataptr dz)
  176. {
  177. switch(dz->process) {
  178. case(ACCU): setup_process_logic(ANALFILE_ONLY, EQUAL_ANALFILE, ANALFILE_OUT, dz); break;
  179. case(EXAG): setup_process_logic(ANALFILE_ONLY, EQUAL_ANALFILE, ANALFILE_OUT, dz); break;
  180. case(FOCUS): setup_process_logic(ANALFILE_ONLY, EQUAL_ANALFILE, ANALFILE_OUT, dz); break;
  181. case(FOLD): setup_process_logic(ANALFILE_ONLY, EQUAL_ANALFILE, ANALFILE_OUT, dz); break;
  182. case(FREEZE): setup_process_logic(ANALFILE_ONLY, EQUAL_ANALFILE, ANALFILE_OUT, dz); break;
  183. case(FREEZE2): setup_process_logic(ANALFILE_ONLY, BIG_ANALFILE, ANALFILE_OUT, dz); break;
  184. case(STEP): setup_process_logic(ANALFILE_ONLY, EQUAL_ANALFILE, ANALFILE_OUT, dz); break;
  185. default:
  186. sprintf(errstr,"Unknown process: assign_process_logic()\n");
  187. return(PROGRAM_ERROR);
  188. break;
  189. }
  190. if(dz->has_otherfile) {
  191. switch(dz->input_data_type) {
  192. case(ALL_FILES):
  193. case(TWO_SNDFILES):
  194. case(SNDFILE_AND_ENVFILE):
  195. case(SNDFILE_AND_BRKFILE):
  196. case(SNDFILE_AND_UNRANGED_BRKFILE):
  197. case(SNDFILE_AND_DB_BRKFILE):
  198. break;
  199. case(MANY_SNDFILES):
  200. if(dz->process==INFO_TIMELIST)
  201. break;
  202. /* fall thro */
  203. default:
  204. sprintf(errstr,"Most processes accepting files with different properties\n"
  205. "can only take 2 sound infiles.\n");
  206. return(PROGRAM_ERROR);
  207. }
  208. }
  209. return(FINISHED);
  210. }
  211. /***************************** SET_LEGAL_INFILE_STRUCTURE **************************
  212. *
  213. * Allows 2nd infile to have different props to first infile.
  214. */
  215. void set_legal_infile_structure(dataptr dz)
  216. {
  217. switch(dz->process) {
  218. default:
  219. dz->has_otherfile = FALSE;
  220. break;
  221. }
  222. }
  223. /***************************************************************************************/
  224. /****************************** FORMERLY IN internal.c *********************************/
  225. /***************************************************************************************/
  226. /****************************** SET_LEGAL_INTERNALPARAM_STRUCTURE *********************************/
  227. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  228. {
  229. int exit_status = FINISHED;
  230. mode = 0;
  231. switch(process) {
  232. case(ACCU): return(FINISHED);
  233. case(EXAG): return(FINISHED);
  234. case(FOCUS): exit_status = set_internalparam_data("ddi",ap); break;
  235. case(FOLD): exit_status = set_internalparam_data("iidd",ap); break;
  236. case(FREEZE): return(FINISHED);
  237. case(FREEZE2): return(FINISHED);
  238. case(STEP): return(FINISHED);
  239. default:
  240. sprintf(errstr,"Unknown process in set_legal_internalparam_structure()\n");
  241. return(PROGRAM_ERROR);
  242. }
  243. return(exit_status);
  244. }
  245. /********************************************************************************************/
  246. /********************************** FORMERLY IN specialin.c *********************************/
  247. /********************************************************************************************/
  248. /********************** READ_SPECIAL_DATA ************************/
  249. int read_special_data(char *str,dataptr dz)
  250. {
  251. // int exit_status = FINISHED;
  252. aplptr ap = dz->application;
  253. switch(ap->special_data) {
  254. case(FREEZE_DATA): return read_specfreeze_data(str,dz);
  255. case(FREEZE2_DATA): return read_specfreeze2_data(str,dz);
  256. default:
  257. sprintf(errstr,"Unknown special_data type: read_special_data()\n");
  258. return(PROGRAM_ERROR);
  259. }
  260. return(FINISHED); /* NOTREACHED */
  261. }
  262. /************************** READ_SPECFREEZE_DATA ********************************/
  263. int read_specfreeze_data(char *filename,dataptr dz)
  264. {
  265. int exit_status;
  266. int n = 0, OK = 1, is_direction_info = FALSE, linecnt = 0;
  267. FILE *fp;
  268. char temp[200], *p;
  269. double dtime;
  270. int arraysize = BIGARRAY;
  271. if((dz->iparray[FRZ_TIMETYPE] = (int *)malloc(arraysize * sizeof(int)))==NULL) {
  272. sprintf(errstr,"INSUFFICIENT MEMORY for array of freeze-timetypes.\n");
  273. return(MEMORY_ERROR);
  274. }
  275. if((dz->lparray[FRZ_SEGTIME] = (int *)malloc(arraysize * sizeof(int)))==NULL) {
  276. sprintf(errstr,"INSUFFICIENT MEMORY for array of freeze sample times.\n");
  277. return(MEMORY_ERROR);
  278. }
  279. if((fp = fopen(filename,"r"))==NULL) {
  280. sprintf(errstr,"nCannot open file %s to read data.\n",filename);
  281. return(DATA_ERROR);
  282. }
  283. while(OK && fgets(temp,200,fp)) {
  284. linecnt++;
  285. p = temp;
  286. for(;;) {
  287. dz->iparray[FRZ_TIMETYPE][n] = FRZ_LIMIT;
  288. while(isspace(*p))
  289. p++;
  290. if(*p == '\n' || *p == '\0')
  291. break;
  292. if((exit_status = set_timetype(&p,n,&is_direction_info,dz))<0)
  293. return(exit_status);
  294. if((exit_status = read_time(&p,&dtime,n,linecnt,filename))<0)
  295. return(exit_status);
  296. dz->lparray[FRZ_SEGTIME][n] = round(dtime/dz->frametime);
  297. if(n==0) {
  298. if(dz->lparray[FRZ_SEGTIME][0]!=0) {
  299. if((exit_status = insert_values_at_time_zero(&n,dz))<0)
  300. return(exit_status);
  301. }
  302. } else if((exit_status = check_time_sequencing(n,filename,dz))<0)
  303. return(exit_status);
  304. if(dz->lparray[FRZ_SEGTIME][n] > dz->wlength) {
  305. dz->lparray[FRZ_SEGTIME][n] = dz->wlength;
  306. n++;
  307. OK = 0;
  308. break;
  309. }
  310. if(++n>=arraysize) {
  311. arraysize += BIGARRAY;
  312. if((dz->lparray[FRZ_SEGTIME] =
  313. (int *)realloc((char *)dz->lparray[FRZ_SEGTIME], arraysize * sizeof(int)))==NULL) {
  314. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate freeze sample times.\n");
  315. return(MEMORY_ERROR);
  316. }
  317. if((dz->iparray[FRZ_TIMETYPE] =
  318. (int *)realloc((char *)dz->iparray[FRZ_TIMETYPE],arraysize * sizeof(int)))==NULL) {
  319. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate freeze timetypes.\n");
  320. return(MEMORY_ERROR);
  321. }
  322. }
  323. }
  324. }
  325. if(!is_direction_info) {
  326. sprintf(errstr,"No flags given in freeze data\n");
  327. return(DATA_ERROR);
  328. }
  329. if(dz->lparray[FRZ_SEGTIME][n-1]!=dz->wlength) {
  330. if(++n>=arraysize) {
  331. if((dz->lparray[FRZ_SEGTIME] = (int *)realloc((char *)dz->lparray[FRZ_SEGTIME], n * sizeof(int)))==NULL) {
  332. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate freeze sample times.\n");
  333. return(MEMORY_ERROR);
  334. }
  335. if((dz->iparray[FRZ_TIMETYPE] = (int *)realloc((char *)dz->iparray[FRZ_TIMETYPE],n * sizeof(int)))==NULL) {
  336. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate freeze timetypes.\n");
  337. return(MEMORY_ERROR);
  338. }
  339. }
  340. dz->lparray[FRZ_SEGTIME][n-1] = dz->wlength;
  341. dz->iparray[FRZ_TIMETYPE][n-1] = FRZ_LIMIT;
  342. } else {
  343. if((dz->lparray[FRZ_SEGTIME] = (int *)realloc((char *)dz->lparray[FRZ_SEGTIME], n * sizeof(int)))==NULL) {
  344. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate freeze sample times.\n");
  345. return(MEMORY_ERROR);
  346. }
  347. if((dz->iparray[FRZ_TIMETYPE] = (int *)realloc((char *)dz->iparray[FRZ_TIMETYPE],n * sizeof(int)))==NULL) {
  348. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate freeze timetypes.\n");
  349. return(MEMORY_ERROR);
  350. }
  351. }
  352. dz->itemcnt = n;
  353. return(FINISHED);
  354. }
  355. /************************** READ_SPECFREEZE2_DATA ********************************/
  356. int read_specfreeze2_data(char *filename,dataptr dz)
  357. {
  358. // int n = 0, OK = 1, is_location = TRUE;
  359. int is_location = TRUE;
  360. FILE *fp;
  361. char temp[200], *p;
  362. double dtime;
  363. int arraysize = BIGARRAY;
  364. dz->itemcnt = 0;
  365. if((dz->lparray[FRZ_FRZTIME] = (int *)malloc(arraysize * sizeof(int)))==NULL) {
  366. sprintf(errstr,"INSUFFICIENT MEMORY for array of freeze times.\n");
  367. return(MEMORY_ERROR);
  368. }
  369. if((dz->lparray[FRZ_SEGTIME] = (int *)malloc(arraysize * sizeof(int)))==NULL) {
  370. sprintf(errstr,"INSUFFICIENT MEMORY for array of freeze lengths.\n");
  371. return(MEMORY_ERROR);
  372. }
  373. if((fp = fopen(filename,"r"))==NULL) {
  374. sprintf(errstr,"nCannot open file %s to read data.\n",filename);
  375. return(DATA_ERROR);
  376. }
  377. while(fgets(temp,200,fp)) {
  378. p = temp;
  379. while(isspace(*p))
  380. p++;
  381. if(*p==ENDOFSTR)
  382. continue;
  383. dz->scalefact = dz->duration/dz->frametime;
  384. while(get_float_from_within_string(&p,&dtime)) {
  385. dtime /= dz->frametime;
  386. dz->scalefact += (dtime - 1);
  387. if(is_location) {
  388. dz->lparray[FRZ_FRZTIME][dz->itemcnt] = round(dtime);
  389. if(dz->lparray[FRZ_FRZTIME][dz->itemcnt] < 0
  390. || dz->lparray[FRZ_FRZTIME][dz->itemcnt] >= dz->wlength) {
  391. sprintf(errstr,"Invalid freeze location : %lf\n",dtime);
  392. return(DATA_ERROR);
  393. }
  394. } else {
  395. dz->lparray[FRZ_SEGTIME][dz->itemcnt] = round(dtime);
  396. if(dz->lparray[FRZ_SEGTIME][dz->itemcnt] < 0) {
  397. sprintf(errstr,"Invalid freeze duration : %lf\n",dtime);
  398. return(DATA_ERROR);
  399. }
  400. }
  401. is_location = !is_location;
  402. if(is_location) {
  403. if(++dz->itemcnt >= arraysize) {
  404. arraysize += BIGARRAY;
  405. if((dz->lparray[FRZ_FRZTIME] =
  406. (int *)realloc((char *)dz->lparray[FRZ_FRZTIME],arraysize * sizeof(int)))==NULL) {
  407. sprintf(errstr,"INSUFFICIENT MEMORY to realloc array of freeze times.\n");
  408. return(MEMORY_ERROR);
  409. }
  410. if((dz->lparray[FRZ_SEGTIME] =
  411. (int *)realloc((char *)dz->lparray[FRZ_SEGTIME],arraysize * sizeof(int)))==NULL) {
  412. sprintf(errstr,"INSUFFICIENT MEMORY to realloc array of freeze lengths.\n");
  413. return(MEMORY_ERROR);
  414. }
  415. }
  416. }
  417. }
  418. }
  419. if(!is_location) {
  420. sprintf(errstr,"Freeze times not paired correctly.\n");
  421. return(DATA_ERROR);
  422. }
  423. if((dz->lparray[FRZ_FRZTIME] =
  424. (int *)realloc((char *)dz->lparray[FRZ_FRZTIME],dz->itemcnt * sizeof(int)))==NULL) {
  425. sprintf(errstr,"INSUFFICIENT MEMORY to realloc array of freeze times.\n");
  426. return(MEMORY_ERROR);
  427. }
  428. if((dz->lparray[FRZ_SEGTIME] =
  429. (int *)realloc((char *)dz->lparray[FRZ_SEGTIME],dz->itemcnt * sizeof(int)))==NULL) {
  430. sprintf(errstr,"INSUFFICIENT MEMORY to realloc array of freeze lengths.\n");
  431. return(MEMORY_ERROR);
  432. }
  433. return(FINISHED);
  434. }
  435. /************************** SET_TIMETYPE ********************************/
  436. int set_timetype(char **first_char_of_string,int strno,int *is_direction_info,dataptr dz)
  437. {
  438. char *p = *first_char_of_string;
  439. switch(*p) {
  440. case('b'):
  441. dz->iparray[FRZ_TIMETYPE][strno] = FRZ_BEFORE;
  442. *is_direction_info = TRUE;
  443. p++;
  444. break;
  445. case('a'):
  446. dz->iparray[FRZ_TIMETYPE][strno] = FRZ_AFTER;
  447. *is_direction_info = TRUE;
  448. p++;
  449. break;
  450. }
  451. *first_char_of_string = p;
  452. return(FINISHED);
  453. }
  454. /************************** READ_TIME ********************************/
  455. int read_time(char **p,double *dtime,int strno,int linecnt, char *filename)
  456. {
  457. if(**p!='.' && **p!='-' && !isdigit(**p)) {
  458. sprintf(errstr,"Unknown time flag '%c' on line %d of file %s\n",**p, linecnt,filename);
  459. return(DATA_ERROR);
  460. }
  461. if(sscanf(*p,"%lf",dtime)!=1) {
  462. sprintf(errstr,"Cannot read time value %d on line %d of file %s.\n",strno+1, linecnt,filename);
  463. return(DATA_ERROR);
  464. }
  465. while(!isspace(**p) && **p!='\n' && **p != '\0')
  466. (*p)++;
  467. return(FINISHED);
  468. }
  469. /************************** INSERT_VALUES_AT_TIME_ZERO ********************************/
  470. int insert_values_at_time_zero(int *n,dataptr dz)
  471. {
  472. if(dz->lparray[FRZ_SEGTIME][0] < 0) {
  473. dz->lparray[FRZ_SEGTIME][0] = 0;
  474. *n = 0;
  475. } else {
  476. dz->lparray[FRZ_SEGTIME][1] = dz->lparray[FRZ_SEGTIME][0];
  477. dz->iparray[FRZ_TIMETYPE][1] = dz->iparray[FRZ_TIMETYPE][0];
  478. dz->lparray[FRZ_SEGTIME][0] = 0;
  479. dz->iparray[FRZ_TIMETYPE][0] = FRZ_LIMIT;
  480. *n = 1;
  481. }
  482. return(FINISHED);
  483. }
  484. /************************** CHECK_TIME_SEQUENCING ********************************/
  485. int check_time_sequencing(int n,char *filename,dataptr dz)
  486. {
  487. if(dz->lparray[FRZ_SEGTIME][n] <= dz->lparray[FRZ_SEGTIME][n-1]) {
  488. sprintf(errstr,"Time values out of sequence in file %s.\n",filename);
  489. return(DATA_ERROR);
  490. }
  491. return(FINISHED);
  492. }
  493. /********************************************************************************************/
  494. /********************************** FORMERLY IN preprocess.c ********************************/
  495. /********************************************************************************************/
  496. /****************************** PARAM_PREPROCESS *********************************/
  497. int param_preprocess(dataptr dz)
  498. {
  499. // int exit_status = FINISHED;
  500. switch(dz->process) {
  501. case(ACCU): return scale_accu_parameters(dz);
  502. case(EXAG): return convert_params_for_exag(dz);
  503. case(FOCUS): return setup_internal_arrays_and_params_for_focus(dz);
  504. case(FOLD): return fold_preprocess(dz);
  505. case(FREEZE): return get_frztimes(dz);
  506. case(FREEZE2): return(FINISHED);
  507. case(STEP): return convert_params_for_step(dz);
  508. default:
  509. sprintf(errstr,"PROGRAMMING PROBLEM: Unknown process in param_preprocess()\n");
  510. return(PROGRAM_ERROR);
  511. }
  512. return(FINISHED); /* NOTREACHED */
  513. }
  514. /************************************* SCALE_ACCU_PARAMETERS *******************/
  515. int scale_accu_parameters(dataptr dz)
  516. {
  517. int n;
  518. double *p;
  519. /* decayratio per sec -> decayratio per window */
  520. if(dz->brksize[ACCU_DINDEX]) {
  521. p = dz->brk[ACCU_DINDEX] + 1;
  522. for(n=0;n<dz->brksize[ACCU_DINDEX];n++) {
  523. *p = exp(log(*p) * dz->frametime);
  524. p += 2;
  525. }
  526. } else
  527. dz->param[ACCU_DINDEX] = exp(log(dz->param[ACCU_DINDEX]) * dz->frametime);
  528. /* glis in 8va per sec -> frqratio per window */
  529. if(dz->brksize[ACCU_GINDEX]) {
  530. p = dz->brk[ACCU_GINDEX] + 1;
  531. for(n=0;n<dz->brksize[ACCU_GINDEX];n++) {
  532. *p = pow(2.0,*p * dz->frametime);
  533. p += 2;
  534. }
  535. } else
  536. dz->param[ACCU_GINDEX] = pow(2.0,dz->param[ACCU_GINDEX] * dz->frametime);
  537. return(FINISHED);
  538. }
  539. /************ CONVERT_PARAMS_FOR_EXAG *************/
  540. int convert_params_for_exag(dataptr dz)
  541. {
  542. double *p;
  543. int n;
  544. if(dz->brksize[EXAG_EXAG]) { /* convert to reciprocal of value */
  545. p = dz->brk[EXAG_EXAG]+1;
  546. for(n=0;n<dz->brksize[EXAG_EXAG];n++) {
  547. *p = 1.0/(*p);
  548. p += 2;
  549. }
  550. } else
  551. dz->param[EXAG_EXAG] = 1.0/dz->param[EXAG_EXAG];
  552. return(FINISHED);
  553. }
  554. /************ SETUP_INTERNAL_ARRAYS_AND_PARAMS_FOR_FOCUS *************/
  555. int setup_internal_arrays_and_params_for_focus(dataptr dz)
  556. {
  557. int exit_status;
  558. dz->itemcnt = dz->iparam[FOCU_PKCNT];
  559. if(dz->brksize[FOCU_BW]==0) {
  560. if((exit_status = generate_focu_filter_params(dz))<0)
  561. return(exit_status);
  562. }
  563. if((exit_status = establish_internal_arrays_for_focus(dz))<0)
  564. return(exit_status);
  565. if(dz->vflag[FOCUS_STABLE]) {
  566. if((exit_status = setup_stability_arrays_and_constants(FOCU_STABL,FOCU_SL1,dz))<0)
  567. return(exit_status);
  568. }
  569. return(FINISHED);
  570. }
  571. /****************************** GENERATE_FOCU_FILTER_PARAMS ***************************/
  572. int generate_focu_filter_params(dataptr dz)
  573. {
  574. double halfbwidth = dz->param[FOCU_BW]/2.0;
  575. dz->scalefact = PI/halfbwidth; /* Convert 8va offset from flt centre-frq to a fraction of the bandwidth X PI */
  576. dz->param[FOCU_BRATIO_UP] = pow(2.0,halfbwidth); /* upward transpratio to top of filter band */
  577. dz->param[FOCU_BRATIO_DN] = 1.0/dz->param[FOCU_BRATIO_UP]; /* Downward transpratio to bottom of filter band */
  578. return(FINISHED);
  579. }
  580. /********************** ESTABLISH_INTERNAL_ARRAYS_FOR_FOCUS **********************/
  581. int establish_internal_arrays_for_focus(dataptr dz)
  582. {
  583. if((dz->filtpeak = (double *)malloc((dz->itemcnt+1) * sizeof(double)))==NULL) {
  584. sprintf(errstr,"INSUFFICIENT MEMORY for focus peaks array.\n");
  585. return(MEMORY_ERROR); /* stores amps of filter peaks */
  586. }
  587. if((dz->fbandtop = (double *)malloc((dz->itemcnt+1) * sizeof(double)))==NULL) {
  588. sprintf(errstr,"INSUFFICIENT MEMORY for focus band tops array.\n");
  589. return(MEMORY_ERROR); /* stores top of fiter bands */
  590. }
  591. if((dz->fbandbot = (double *)malloc((dz->itemcnt+1) * sizeof(double)))==NULL) {
  592. sprintf(errstr,"INSUFFICIENT MEMORY for focus band bottoms array.\n");
  593. return(MEMORY_ERROR); /* stores bots of filtbands */
  594. }
  595. if((dz->fsampbuf = (float *)malloc((dz->clength+1) * sizeof(float)))==NULL) {
  596. sprintf(errstr,"INSUFFICIENT MEMORY for filter contour array.\n");
  597. return(MEMORY_ERROR); /* stores filter contour */
  598. }
  599. if((dz->peakno = (int *)malloc((dz->itemcnt+1) * sizeof(int)))==NULL) {
  600. sprintf(errstr,"INSUFFICIENT MEMORY for focus peak location array.\n");
  601. return(MEMORY_ERROR); /* specenv chans where peaks are */
  602. }
  603. memset(dz->peakno,0,dz->itemcnt * sizeof(int));
  604. /* set_array_safety */
  605. dz->filtpeak[dz->itemcnt] = 0.0;
  606. dz->fbandtop[dz->itemcnt] = 0.0; /* stores top of fiter bands */
  607. dz->fbandbot[dz->itemcnt] = 0.0; /* stores bottoms of filtbands */
  608. dz->fsampbuf[dz->clength] = 0.0f; /* stores filter contour */
  609. dz->peakno[dz->itemcnt] = 0; /* points to specenv chans where peaks are */
  610. return(FINISHED);
  611. }
  612. /************************** FOLD_PREPROCESS ******************************/
  613. int fold_preprocess(dataptr dz)
  614. {
  615. if(dz->brksize[FOLD_HIFRQ]==0) {
  616. dz->iparam[FOLD_HICHAN] = (int)((dz->param[FOLD_HIFRQ] + dz->halfchwidth)/dz->chwidth); /* TRUNCATE */
  617. dz->param[FOLD_HICHANTOP] = ((double)dz->iparam[FOLD_HICHAN] * dz->chwidth) + dz->halfchwidth;
  618. if(dz->iparam[FOLD_HICHAN]!=0)
  619. dz->iparam[FOLD_HICHAN] <<= 1;
  620. }
  621. if(dz->brksize[FOLD_LOFRQ]==0) {
  622. dz->iparam[FOLD_LOCHAN] = (int)((dz->param[FOLD_LOFRQ] + dz->halfchwidth)/dz->chwidth); /* TRUNCATE */
  623. if(dz->iparam[FOLD_LOCHAN]>0)
  624. dz->param[FOLD_LOCHANBOT] = ((double)(dz->iparam[FOLD_LOCHAN]-1) * dz->chwidth) + dz->halfchwidth;
  625. else
  626. dz->param[FOLD_LOCHANBOT] = 0.0;
  627. if(dz->iparam[FOLD_LOCHAN]!=0)
  628. dz->iparam[FOLD_LOCHAN] <<= 1;
  629. }
  630. return(FINISHED);
  631. }
  632. /************************* GET_FRZTIMES ****************************/
  633. int get_frztimes(dataptr dz)
  634. {
  635. int n;
  636. if((dz->lparray[FRZ_FRZTIME] = (int *)malloc((dz->itemcnt-1) * sizeof(int)))==NULL) {
  637. sprintf(errstr,"INSUFFICIENT MEMORY for freezetimes array.\n");
  638. return(MEMORY_ERROR);
  639. }
  640. for(n=0;n<dz->itemcnt-1;n++) {
  641. if(dz->iparray[FRZ_TIMETYPE][n+1]<FRZ_BEFORE
  642. || dz->iparray[FRZ_TIMETYPE][n+1]>FRZ_AFTER) {
  643. sprintf(errstr,"Invalid timetype flag in get_frztimes()\n");
  644. return(PROGRAM_ERROR);
  645. }
  646. switch(dz->iparray[FRZ_TIMETYPE][n]) {
  647. case(FRZ_BEFORE):
  648. case(FRZ_LIMIT):
  649. switch(dz->iparray[FRZ_TIMETYPE][n+1]) {
  650. case(FRZ_LIMIT):
  651. case(FRZ_AFTER): dz->lparray[FRZ_FRZTIME][n] = -1;
  652. break;
  653. case(FRZ_BEFORE): dz->lparray[FRZ_FRZTIME][n] = dz->lparray[FRZ_SEGTIME][n+1];
  654. break;
  655. }
  656. break;
  657. case(FRZ_AFTER):
  658. switch(dz->iparray[FRZ_TIMETYPE][n+1]) {
  659. case(FRZ_LIMIT):
  660. case(FRZ_AFTER): dz->lparray[FRZ_FRZTIME][n] = dz->lparray[FRZ_SEGTIME][n];
  661. break;
  662. case(FRZ_BEFORE):
  663. sprintf(errstr,"Impossible time sequence in freeze file.\n");
  664. return(USER_ERROR);
  665. }
  666. break;
  667. default:
  668. if(n==0) {
  669. sprintf(errstr,"Invalid timetype flag in get_frztimes()\n");
  670. return(PROGRAM_ERROR);
  671. }
  672. }
  673. }
  674. return rationalise_frztimes(dz);
  675. }
  676. /************************** RATIONALISE_FRZTIMES **************************
  677. *
  678. * Bear in mind that there are scnt times, icluding the time at the
  679. * end of the file. This means there are (scnt-1) time-segments to
  680. * operate on and therefore (scnt-1) frztimes.
  681. *
  682. * (1) If two consecutive time-segments are flagged (frztime<0) to
  683. * have no action on them, then we can eliminate the time (and
  684. * the related frztime info) of the 2nd window.
  685. * (2) Do this by shuffling existing values above this time[n] downwards,
  686. * overwriting the existing value (ditto for 'frztime' vals), up as far
  687. * end of frztime array,and therefore 1 below top of time array.
  688. * (3) Reduce value of time_seg counter.
  689. * (4) Move the uppermost time_seg value down.
  690. * (5) Replace the absolute values of the time vals, to differences,
  691. * which will be the successive numbers of windows to process when
  692. * the processing begins.
  693. * (6) Hence reduce by 1 the count of time vals stored.
  694. * (7) Realloc memory space for time and frztime values.
  695. */
  696. int rationalise_frztimes(dataptr dz)
  697. {
  698. int n, m;
  699. int orig_itemcnt = dz->itemcnt;
  700. for(n=1;n<dz->itemcnt-1;n++) {
  701. if(dz->lparray[FRZ_FRZTIME][n-1]<0 && dz->lparray[FRZ_FRZTIME][n]<0) {/* 1 */
  702. for(m=n;m<dz->itemcnt-1;m++) {
  703. dz->lparray[FRZ_SEGTIME][n] = dz->lparray[FRZ_SEGTIME][n+1];/* 2 */
  704. dz->lparray[FRZ_FRZTIME][n] = dz->lparray[FRZ_FRZTIME][n+1];
  705. }
  706. dz->itemcnt--; /* 3 */
  707. dz->lparray[FRZ_SEGTIME][dz->itemcnt-1]
  708. = dz->lparray[FRZ_SEGTIME][dz->itemcnt];/* 4 */
  709. }
  710. }
  711. if(dz->itemcnt != orig_itemcnt) {
  712. if((dz->lparray[FRZ_SEGTIME] =
  713. (int *)realloc((char *)dz->lparray[FRZ_SEGTIME],dz->itemcnt * sizeof(int)))==NULL) {
  714. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate freeze segtime array.\n");
  715. return(MEMORY_ERROR);
  716. }
  717. if((dz->lparray[FRZ_FRZTIME] =
  718. (int *)realloc((char *)dz->lparray[FRZ_FRZTIME],(dz->itemcnt-1) * sizeof(int)))==NULL) {
  719. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate freezetimes array.\n");
  720. return(MEMORY_ERROR);
  721. }
  722. }
  723. return(FINISHED);
  724. }
  725. /************ CONVERT_PARAMS_FOR_STEP *************/
  726. int convert_params_for_step(dataptr dz)
  727. {
  728. double *p;
  729. int n;
  730. if(dz->brksize[STEP_STEP]==0)
  731. dz->iparam[STEP_STEP] = round(dz->param[STEP_STEP]/dz->frametime);
  732. else {
  733. p = dz->brk[STEP_STEP] + 1;
  734. for(n=0;n < dz->brksize[STEP_STEP];n++) {
  735. *p /= dz->frametime;
  736. p += 2;
  737. }
  738. dz->is_int[STEP_STEP] = 1;
  739. }
  740. return(FINISHED);
  741. }
  742. /********************************************************************************************/
  743. /********************************** FORMERLY IN procspec.c **********************************/
  744. /********************************************************************************************/
  745. /**************************** SPEC_PROCESS_FILE ****************************/
  746. int spec_process_file(dataptr dz)
  747. {
  748. dz->total_windows = 0;
  749. if(dz->process == FREEZE2)
  750. dz->tempsize = (int)round(dz->scalefact) * dz->wanted;
  751. display_virtual_time(0L,dz);
  752. switch(dz->process) {
  753. case(ACCU): return outer_loop(dz);
  754. case(EXAG): return outer_loop(dz);
  755. case(FOLD): return outer_loop(dz);
  756. case(FOCUS): return outer_focu_loop(dz);
  757. case(FREEZE): return specfreeze(dz);
  758. case(FREEZE2): return specfreeze2(dz);
  759. case(STEP): return specstep(dz);
  760. default:
  761. sprintf(errstr,"Unknown process in spec_process_file()\n");
  762. return(PROGRAM_ERROR);
  763. }
  764. return(FINISHED); /* NOTREACHED */
  765. }
  766. /**************************** INNER_LOOP ****************************/
  767. int inner_loop
  768. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  769. {
  770. int exit_status;
  771. int local_zero_set = FALSE;
  772. int wc;
  773. for(wc=0; wc<windows_in_buf; wc++) {
  774. if(dz->total_windows==0) {
  775. if((exit_status = skip_or_special_operation_on_window_zero(dz))<0)
  776. return(exit_status);
  777. if(exit_status==TRUE) {
  778. dz->flbufptr[0] += dz->wanted;
  779. dz->total_windows++;
  780. dz->time = (float)(dz->time + dz->frametime);
  781. continue;
  782. }
  783. }
  784. if((exit_status = read_values_from_all_existing_brktables((double)dz->time,dz))<0)
  785. return(exit_status);
  786. switch(dz->process) {
  787. case(FOCUS): exit_status = specfocus(peakscore,descnt,least,dz); break;
  788. case(FOLD): exit_status = specfold(dz); break;
  789. case(ACCU): exit_status = specaccu(dz); break;
  790. case(EXAG): exit_status = specexag(&local_zero_set,dz); break;
  791. default:
  792. sprintf(errstr,"unknown process in inner_loop()\n");
  793. return(PROGRAM_ERROR);
  794. }
  795. if(exit_status<0)
  796. return(exit_status);
  797. dz->flbufptr[0] += dz->wanted;
  798. dz->total_windows++;
  799. dz->time = (float)(dz->time + dz->frametime);
  800. }
  801. if(!dz->zeroset && local_zero_set==TRUE) {
  802. fprintf(stdout,"WARNING: Zero-amp spectral window(s) encountered: orig window(s) substituted.\n");
  803. fflush(stdout);
  804. dz->zeroset = TRUE;
  805. }
  806. return(FINISHED);
  807. }
  808. /***************** SKIP_OR_SPECIAL_OPERATION_ON_WINDOW_ZERO ************/
  809. int skip_or_special_operation_on_window_zero(dataptr dz)
  810. {
  811. int exit_status = FINISHED;
  812. int vc;
  813. switch(dz->process) {
  814. case(EXAG): case(FOCUS): case(ACCU):
  815. switch(dz->process) {
  816. case(ACCU):
  817. for(vc = 0; vc < dz->wanted; vc += 2)
  818. dz->windowbuf[0][AMPP] = dz->flbufptr[0][AMPP];
  819. break;
  820. case(FOCUS):
  821. if(!dz->vflag[FOCUS_STABLE])
  822. break;
  823. rectify_window(dz->flbufptr[0],dz);
  824. if((exit_status = extract_specenv(0,0,dz))<0)
  825. return(exit_status);
  826. /* put 1st specenv in end of stability store && copy 1st buf to end of bufstore */
  827. memmove((char *)dz->stable->spec[dz->iparam[FOCU_SL1]],
  828. //TW ERROR IN ORIGINAL CODE
  829. // (char *)dz->specenvamp,dz->infile->specenvcnt * sizeof(int));
  830. (char *)dz->specenvamp,dz->infile->specenvcnt * sizeof(float));
  831. memmove((char *)dz->stable->sbuf[dz->iparam[FOCU_SL1]],
  832. (char *)dz->flbufptr[0],(size_t)dz->wanted * sizeof(float));
  833. dz->stable->total_pkcnt[dz->iparam[FOCU_SL1]] = 0; /* 1st window has 0 amp, hence 0 peaks */
  834. break;
  835. }
  836. return(TRUE);
  837. }
  838. return(FALSE);
  839. }
  840. /****************************** CONSTRUCT_FILTER_ENVELOPE ***************************
  841. *
  842. * If we reach the top of the channels, and there is another peak to work on,
  843. * we must be backtracking to a lower channel. If we are NOT, there is a problem
  844. * in my logic ... no peak can have its lowest point higher than the frq in the highest
  845. * channel!! That's why we have used CHECKLOGIC to check this out!!!
  846. */
  847. int construct_filter_envelope(int pkcnt_here,float *fbuf,dataptr dz)
  848. {
  849. #define OVERSHOOT (8) /* No of channels to test beyond current ch, for frqs BELOW current frq */
  850. /* Guesstimate */
  851. int exit_status;
  852. double thisfrq, thisamp, filt_centre_frq;
  853. int n, m;
  854. int baktrak = -1, activate_baktrak = 0, checklogic = 0;
  855. int beyond_top, band_incremented;
  856. int cc , vc;
  857. for(cc=0; cc<dz->clength; cc++) /* PRESET FILTER ENVELOPE TO ZERO */
  858. dz->fsampbuf[cc] = 0.0f;
  859. n = 0; /* COUNTER FOR CURRENT PEAK */
  860. m = 1; /* COUNTER FOR NEXT PEAK */
  861. cc = 0;
  862. vc = 0;
  863. beyond_top = 0;
  864. while(cc < dz->clength) {
  865. switch(dz->process) {
  866. case(FOCUS): filt_centre_frq = dz->specenvfrq[dz->peakno[n]]; break;
  867. default:
  868. sprintf(errstr,"Invalid application in construct_filter_envelope()\n");
  869. return(PROGRAM_ERROR);
  870. }
  871. band_incremented = 0;
  872. thisfrq = fabs(fbuf[FREQ]);
  873. if(baktrak<0 && (m < pkcnt_here) && (thisfrq > dz->fbandbot[m]))
  874. baktrak = cc; /* IF A FRQ IS ENCOUNTERED WHICH IS IN NEXT BAND
  875. AND FOR THE FIRST TIME : mark this as channel to baktrak to */
  876. if(thisfrq < dz->fbandbot[n]) {
  877. if(beyond_top) /* IF FRQ BELOW BAND: IGNORE IT */
  878. beyond_top++; /* If beyond possible band_top, count channels beyond */
  879. }
  880. else if(thisfrq < filt_centre_frq) { /* IF FRQ IN LOWER HALFBAND: generate filter envel */
  881. if((exit_status = gen_amplitude_in_lo_half_filterband(&thisamp,thisfrq,filt_centre_frq,dz))<0)
  882. return(exit_status);
  883. dz->fsampbuf[cc] = (float)max(dz->fsampbuf[cc],thisamp);
  884. if(beyond_top)
  885. beyond_top++; /* If beyond possible band_top, count channels beyond */
  886. }
  887. else if(thisfrq < dz->fbandtop[n]) { /* IF FRQ IN HIGHER HALFBAND: generate filter envel */
  888. if((exit_status = gen_amplitude_in_hi_half_filterband(&thisamp,thisfrq,filt_centre_frq,dz))<0)
  889. return(exit_status);
  890. dz->fsampbuf[cc] = (float)max(dz->fsampbuf[cc],thisamp);
  891. if(beyond_top)
  892. beyond_top++; /* If beyond possible band_top, count channels beyond */
  893. }
  894. else { /* FREQ is ABOVE TOP OF BAND */
  895. if(!beyond_top) { /* IF only just reached possible bandtop among channel frqs */
  896. if(baktrak < 0) /* If channel to baktrak to has not been set, set it here */
  897. baktrak = cc;
  898. }
  899. if(++beyond_top>OVERSHOOT){/* IF GONE FAR ENOUGH BEYOND POSSIBLE BAND TOP */
  900. beyond_top = 0; /* switch off band_top flag */
  901. if(++n >= pkcnt_here) /* MOVE ON TO NEXT BAND */
  902. break; /* IF REACHED END OF ALL BANDS: stop */
  903. m++;
  904. band_incremented = 1; /* Note that band has been incremented */
  905. activate_baktrak = 1; /* activate the baktraking mechanism */
  906. }
  907. }
  908. if(cc == dz->clength-1) { /* IF REACHED TOP OF CHANNELS ON THIS PASS */
  909. checklogic = 1;
  910. if(!band_incremented) { /* IF BAND HAS NOT ALREADY BEEN INCREMENTED */
  911. beyond_top = 0; /* Ensure band_top flag is switched off */
  912. if(++n >= pkcnt_here) /* MOVE ON TO NEXT BAND */
  913. break; /* IF REACHED END OF ALL BANDS: stop */
  914. m++;
  915. activate_baktrak = 1; /* activate the baktraking mechanism */
  916. }
  917. }
  918. if(activate_baktrak && baktrak>=0) {/* If necessary, baktrak to channel just inside bottom of next band */
  919. if(checklogic)
  920. checklogic++;
  921. cc = baktrak;
  922. vc = cc * 2;
  923. baktrak = -1; /* Cancel baktrak value */
  924. activate_baktrak = 0; /* Deactivate baktraking mechanism */
  925. continue;
  926. }
  927. if(checklogic==1) {
  928. sprintf(errstr,"Problem with peak-scanning logic. construct_filter_envelope()\n");
  929. return(PROGRAM_ERROR);
  930. }
  931. checklogic = 0;
  932. cc++; /* Otherwise, move on to next channel */
  933. vc += 2;
  934. }
  935. return filter_band_test(dz);
  936. }
  937. /********************************************************************************************/
  938. /********************************** FORMERLY IN pconsistency.c ******************************/
  939. /********************************************************************************************/
  940. /****************************** CHECK_PARAM_VALIDITY_AND_CONSISTENCY *********************************/
  941. int check_param_validity_and_consistency(dataptr dz)
  942. {
  943. // int exit_status = FINISHED;
  944. handle_pitch_zeros(dz);
  945. switch(dz->process) {
  946. case(FOCUS): return check_consistency_of_frq_limits_for_focus(dz);
  947. }
  948. return(FINISHED);
  949. }
  950. /********************** CHECK_CONSISTENCY_OF_FRQ_LIMITS_FOR_FOCUS **********************/
  951. int check_consistency_of_frq_limits_for_focus(dataptr dz)
  952. {
  953. if(dz->brksize[FOCU_LOFRQ]==0 && dz->brksize[FOCU_HIFRQ]==0) {
  954. if(flteq(dz->param[FOCU_LOFRQ],dz->param[FOCU_HIFRQ])) {
  955. sprintf(errstr,"Frequency limits define a zero-width band.\n");
  956. return(USER_ERROR);
  957. }
  958. if(dz->param[FOCU_LOFRQ] > dz->param[FOCU_HIFRQ])
  959. swap(&dz->param[FOCU_LOFRQ],&dz->param[FOCU_HIFRQ]);
  960. }
  961. return(FINISHED);
  962. }
  963. /********************************************************************************************/
  964. /********************************** FORMERLY IN buffers.c ***********************************/
  965. /********************************************************************************************/
  966. /**************************** ALLOCATE_LARGE_BUFFERS ******************************/
  967. int allocate_large_buffers(dataptr dz)
  968. {
  969. switch(dz->process) {
  970. case(ACCU): case(EXAG): case(FOLD):
  971. case(STEP):
  972. return allocate_single_buffer(dz);
  973. case(FOCUS):
  974. if(dz->vflag[FOCUS_STABLE])
  975. return allocate_double_buffer(dz);
  976. return allocate_single_buffer(dz);
  977. case(FREEZE):
  978. case(FREEZE2):
  979. return allocate_double_buffer(dz);
  980. default:
  981. sprintf(errstr,"Unknown program no. in allocate_large_buffers()\n");
  982. return(PROGRAM_ERROR);
  983. }
  984. return(FINISHED); /* NOTREACHED */
  985. }
  986. /********************************************************************************************/
  987. /********************************** FORMERLY IN cmdline.c ***********************************/
  988. /********************************************************************************************/
  989. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  990. {
  991. if (!strcmp(prog_identifier_from_cmdline,"accu")) dz->process = ACCU;
  992. else if(!strcmp(prog_identifier_from_cmdline,"exag")) dz->process = EXAG;
  993. else if(!strcmp(prog_identifier_from_cmdline,"focus")) dz->process = FOCUS;
  994. else if(!strcmp(prog_identifier_from_cmdline,"fold")) dz->process = FOLD;
  995. else if(!strcmp(prog_identifier_from_cmdline,"freeze")) dz->process = FREEZE;
  996. else if(!strcmp(prog_identifier_from_cmdline,"hold")) dz->process = FREEZE2;
  997. else if(!strcmp(prog_identifier_from_cmdline,"step")) dz->process = STEP;
  998. else {
  999. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  1000. return(USAGE_ONLY);
  1001. }
  1002. //TW UPDATE
  1003. return(FINISHED);
  1004. }
  1005. /********************************************************************************************/
  1006. /********************************** FORMERLY IN usage.c *************************************/
  1007. /********************************************************************************************/
  1008. /******************************** USAGE1 ********************************/
  1009. int usage1(void)
  1010. {
  1011. sprintf(errstr,
  1012. "\nFOCUSING OPERATIONS ON A SPECTRAL FILE\n\n"
  1013. "USAGE: focus NAME (mode) infile outfile parameters: \n"
  1014. "\n"
  1015. "where NAME can be any one of\n"
  1016. "\n"
  1017. "accu exag focus fold freeze hold step\n\n"
  1018. "Type 'focus accu' for more info on focus accu..ETC.\n");
  1019. return(USAGE_ONLY);
  1020. }
  1021. /******************************** USAGE2 ********************************/
  1022. int usage2(char *str)
  1023. {
  1024. if(!strcmp(str,"accu")) {
  1025. fprintf(stdout,
  1026. "focus accu infile outfile [-ddecay] [-gglis]\n"
  1027. "\n"
  1028. "SUSTAIN EACH SPECTRAL BAND, UNTIL LOUDER DATA APPEARS IN THAT BAND\n"
  1029. "\n"
  1030. "-d sutained channel data decays by factor DECAY per sec.\n"
  1031. " (Possible Range : %lf to 1.0 : Default 1.0)\n"
  1032. " (Suggested Effective Range : %lf to 0.5)\n"
  1033. "-g sutained channel data glisses at GLIS 8vas per sec.\n"
  1034. " (Approx Range : -11.7 to 11.7 : Default 0)\n"
  1035. "\n",ACCU_MIN_DECAY,ACCU_MIN_DECAY);
  1036. } else if(!strcmp(str,"exag")) {
  1037. fprintf(stdout,
  1038. "focus exag infile outfile exaggeration\n"
  1039. "\n"
  1040. "EXAGGERATE SPECTRAL CONTOUR\n"
  1041. "\n"
  1042. "exaggeration >0 will widen troughs: <0 will widen peaks.\n"
  1043. "\n"
  1044. "exaggeration may vary over time.\n"
  1045. "\n");
  1046. } else if(!strcmp(str,"focus")) {
  1047. fprintf(stdout,
  1048. "focus focus infile outfile -fN|-pN [-i] pk bw [-bbt] [-ttp] [-sval]\n"
  1049. "\n"
  1050. "FOCUS SPECTRAL ENERGY ONTO PEAKS IN SPECTRUM\n"
  1051. "\n"
  1052. "-f extract formant envelope linear frqwise,\n"
  1053. " using 1 point for every N equally-spaced frequency-channels.\n"
  1054. "-p extract formant envelope linear pitchwise,\n"
  1055. " using N equally-spaced pitch-bands per octave.\n"
  1056. "-i quicksearch for formants (less accurate).\n"
  1057. "pk (max) number of peaks to find : Range 1 - %d\n"
  1058. "bw bandwidth of peak-centred filters, in octaves.\n"
  1059. "-b BT is bottom frequency to start peak search.\n"
  1060. "-t TP is top frequency to end peak search.\n"
  1061. "-s Attempt to retain only peaks which are STABLE over time.\n"
  1062. " Range 2 - %d : default %d : is no. of windows over which peaks averaged.\n"
  1063. "\n"
  1064. "bandwidth,bottom frequency & top frequency may vary over time.\n"
  1065. "\n",MAXPKCNT,MAXSTABIL,DEFAULT_STABILITY);
  1066. } else if(!strcmp(str,"fold")) {
  1067. fprintf(stdout,
  1068. "focus fold infile outfile lofrq hifrq [-x]\n"
  1069. "\n"
  1070. "OCTAVE-TRANSPOSE SPECTRAL COMPONENTS INTO SPECIFIED RANGE\n"
  1071. "\n"
  1072. "lofrq & hifrq specify range into which spectrum is folded,\n"
  1073. "-x Fuller spectrum.\n"
  1074. "\n"
  1075. "lofrq & hifrq may vary over time. \n");
  1076. } else if(!strcmp(str,"freeze")) {
  1077. fprintf(stdout,
  1078. "focus freeze mode infile outfile datafile\n\n"
  1079. "FREEZE SPECTRAL CHARACTERISTICS IN SOUND, AT GIVEN TIMES \n\n"
  1080. "Datafile contains times at which spectrum is frozen.\n"
  1081. "These times may be preceded by character MARKERS....\n\n"
  1082. "a use window here as freezewindow for spectrum AFTER this time.\n"
  1083. "b use window here as freezewindow for spectrum BEFORE this time.\n\n"
  1084. "Otherwise, times are end\\start of freeze established at one of these markers.\n\n"
  1085. "MODES\n"
  1086. "1 freeze channel amplitudes\n"
  1087. "2 freeze channel frequencies\n"
  1088. "3 freeze channel amplitudes & frequencies\n"
  1089. "\n");
  1090. } else if(!strcmp(str,"hold")) {
  1091. fprintf(stdout,
  1092. //TW UPDATE
  1093. // "focus hold mode infile outfile datafile\n\n"
  1094. "focus hold infile outfile datafile\n\n"
  1095. "HOLD SOUND SPECTRUM, AT GIVEN TIMES \n\n"
  1096. "Datafile contains times at which spectrum is held, & hold-duration.\n"
  1097. "These data items must be paired correctly.\n"
  1098. "The process expands each hold window to the duration given,\n"
  1099. "before proceeding to the next window. The output file is therefore longer\n"
  1100. "than the input file.\n"
  1101. "\n");
  1102. } else if(!strcmp(str,"step")) {
  1103. fprintf(stdout,
  1104. "focus step infile outfile timestep\n"
  1105. "\n"
  1106. "STEP-FRAME THROUGH SOUND BY FREEZING SPECTRUM AT REGULAR TIME INTERVALS\n"
  1107. "\n"
  1108. "Once a freeze window is reached, the following windows are given the spectrum\n"
  1109. "of that window, until the freeze end is reached. The output is the same duration\n"
  1110. "as the input.\n"
  1111. "\n"
  1112. "timestep duration of steps. Must be >= duration of 2 analysis frames.\n"
  1113. " (Rounded internally to a multiple of analysis-frame time.)\n");
  1114. } else
  1115. fprintf(stdout,"Unknown option '%s'\n",str);
  1116. return(USAGE_ONLY);
  1117. }
  1118. /******************************** USAGE3 ********************************/
  1119. int usage3(char *str1,char *str2)
  1120. {
  1121. sprintf(errstr,"Insufficient parameters on command line.\n");
  1122. return(USAGE_ONLY);
  1123. }