spectstr.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. // This algorithm finds the pitch (if any) in each window,
  22. // marking no-pitch-found with a -1 value.
  23. // It discards any windows falling below a threshold amplitude.
  24. // (It can ,optionally, smooth the output data, and remove "blips"
  25. // i.e. "pitches lasting for no more than 1 window".
  26. // but this is not essential, as the statistical survey to follow
  27. // should normally eliminate these anomalies.
  28. //
  29. // It then allots the found pitches to 1/8th semitone bins,
  30. // and finds the bin with the most entries.
  31. // This can be done by counting the windows that fall into the bin,
  32. // or by summing the total-loudnesses of the windows that fall into the bin.
  33. // The bin with the highest score/weight is taken to be the most prominent pitch.
  34. //
  35. // Note that, if the pitch is wrong but falls on a prominent harmonic,
  36. // and the tuning is to a harmonic field with a large number of pitches to tune to,
  37. // such an error may still lead to an appropriate transposition,
  38. // as the real root with be transposed along with the prominent harmonic.
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <structures.h>
  42. #include <tkglobals.h>
  43. #include <pnames.h>
  44. #include <filetype.h>
  45. #include <processno.h>
  46. #include <modeno.h>
  47. #include <arrays.h>
  48. #include <flags.h>
  49. #include <logic.h>
  50. #include <fmnts.h>
  51. #include <formants.h>
  52. #include <globcon.h>
  53. #include <cdpmain.h>
  54. #include <math.h>
  55. #include <mixxcon.h>
  56. #include <osbind.h>
  57. #include <standalone.h>
  58. #include <speccon.h>
  59. #include <ctype.h>
  60. #include <sfsys.h>
  61. #include <string.h>
  62. #include <srates.h>
  63. #ifdef unix
  64. #define round lround
  65. #endif
  66. //static double st_intun, st_noise;
  67. //static int st_wndws;
  68. #define do_decohere is_rectified
  69. #define POSMIN 200
  70. #define DISC_RATIO 1 // Proportion of channels to discohere
  71. #define DISC_RAND 2 // Randomisation of frequency in discohered channels
  72. char errstr[2400];
  73. int anal_infiles = 1;
  74. int sloom = 0;
  75. int sloombatch = 0;
  76. const char* cdp_version = "7.1.0";
  77. /* CDP LIBRARY FUNCTIONS TRANSFERRED HERE */
  78. static int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist);
  79. static int set_vflgs(aplptr ap,char *optflags,int optcnt,char *optlist,
  80. char *varflags,int vflagcnt, int vparamcnt,char *varlist);
  81. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  82. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  83. static int mark_parameter_types(dataptr dz,aplptr ap);
  84. static int establish_application(dataptr dz);
  85. static int application_init(dataptr dz);
  86. static int initialise_vflags(dataptr dz);
  87. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  88. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  89. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  90. static int assign_file_data_storage(int infilecnt,dataptr dz);
  91. /* CDP LIB FUNCTION MODIFIED TO AVOID CALLING setup_particular_application() */
  92. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  93. /* SIMPLIFICATION OF LIB FUNC TO APPLY TO JUST THIS FUNCTION */
  94. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  95. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz);
  96. static int setup_the_application(dataptr dz);
  97. static int setup_the_param_ranges_and_defaults(dataptr dz);
  98. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  99. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  100. //static int get_the_mode_no(char *str, dataptr dz);
  101. /* BYPASS LIBRARY GLOBAL FUNCTION TO GO DIRECTLY TO SPECIFIC APPLIC FUNCTIONS */
  102. extern int allocate_tstretch_buffer(dataptr dz);
  103. extern int check_for_enough_tstretch_brkpnt_vals(dataptr dz);
  104. extern int setup_internal_params_for_tstretch(dataptr dz);
  105. extern int retrograde_sequence_of_time_intervals(int endtime,int count,double *startptr,dataptr dz);
  106. extern double calculate_position(int x,double param0,double param1,double param2);
  107. extern int calc_position_output_wdws_relative_to_input_wdws_for_increasing_stretch
  108. (int *thatwindow,int startwindow,int count,double param0,double param1,double param2,dataptr dz);
  109. extern int calc_position_output_wdws_relative_to_input_wdws_for_decreasing_stretch
  110. (int *thatwindow,int startwindow,int count,int totaldur,double param0,double param1,double param2,dataptr dz);
  111. extern double calculate_number_of_output_windows(double startwdur,double endwdur,int totaldur);
  112. extern int advance_along_input_windows(int wdw_to_advance,int atend,dataptr dz);
  113. extern int timestretch_this_segment(int *thatwindow,int startwindow,double thiswdur,double nextwdur,int totaldur,dataptr dz);
  114. extern int get_both_vals_from_brktable(double *thistime,double *thisstretch,int brktab_no,dataptr dz);
  115. extern int divide_time_into_equal_increments(int *thatwindow,int startwindow,double dur,int count,dataptr dz);
  116. extern int do_timestretching(int *thatwindow,int count,dataptr dz);
  117. extern int do_timevariable_timestretch(int *thatwindow,dataptr dz);
  118. extern int do_constant_timestretch(int *thatwindow,dataptr dz);
  119. extern int spectstretch(dataptr dz);
  120. extern int decohere(float *fbuf,dataptr dz);
  121. /**************************************** MAIN *********************************************/
  122. int main(int argc,char *argv[])
  123. {
  124. int exit_status;
  125. dataptr dz = NULL;
  126. char **cmdline;
  127. int cmdlinecnt;
  128. aplptr ap;
  129. int is_launched = FALSE;
  130. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  131. fprintf(stdout,"%s\n",cdp_version);
  132. fflush(stdout);
  133. return 0;
  134. }
  135. /* CHECK FOR SOUNDLOOM */
  136. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  137. sloom = 0;
  138. sloombatch = 1;
  139. }
  140. if(sflinit("cdp")){
  141. sfperror("cdp: initialisation\n");
  142. return(FAILED);
  143. }
  144. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  145. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  146. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  147. return(FAILED);
  148. }
  149. if(!sloom) {
  150. if(argc == 1) {
  151. usage1();
  152. return(FAILED);
  153. } else if(argc == 2) {
  154. usage2(argv[1]);
  155. return(FAILED);
  156. }
  157. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  158. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  159. return(FAILED);
  160. }
  161. cmdline = argv;
  162. cmdlinecnt = argc;
  163. if((get_the_process_no(argv[0],dz))<0)
  164. return(FAILED);
  165. cmdline++;
  166. cmdlinecnt--;
  167. dz->maxmode = 0;
  168. if((exit_status = setup_the_application(dz))<0) {
  169. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  170. return(FAILED);
  171. }
  172. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  173. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  174. return(FAILED);
  175. }
  176. } else {
  177. //parse_TK_data() =
  178. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  179. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  180. return(exit_status);
  181. }
  182. }
  183. ap = dz->application;
  184. // parse_infile_and_hone_type() =
  185. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  186. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  187. return(FAILED);
  188. }
  189. // setup_param_ranges_and_defaults() =
  190. if((exit_status = setup_the_param_ranges_and_defaults(dz))<0) {
  191. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  192. return(FAILED);
  193. }
  194. if((exit_status = set_internalparam_data("di",ap))<0) { //RWD removed extra 'exit_status'
  195. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  196. return(FAILED);
  197. }
  198. // open_first_infile CDP LIB
  199. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  200. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  201. return(FAILED);
  202. }
  203. cmdlinecnt--;
  204. cmdline++;
  205. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,is_launched,dz))<0) {
  206. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  207. return(FAILED);
  208. }
  209. // handle_formants() redundant
  210. // handle_special_data() redundant except
  211. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  212. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  213. return(FAILED);
  214. }
  215. //check_param_validity_and_consistency() ....
  216. if((exit_status = check_for_enough_tstretch_brkpnt_vals(dz))<0) {
  217. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  218. return(MEMORY_ERROR);
  219. }
  220. if((exit_status = setup_internal_params_for_tstretch(dz))<0) {
  221. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  222. return(FAILED);
  223. }
  224. is_launched = TRUE;
  225. if((exit_status = allocate_tstretch_buffer(dz))<0) {
  226. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  227. return(MEMORY_ERROR);
  228. }
  229. if((dz->iparray[0] = (int *)malloc(dz->clength * sizeof(int)))==NULL) {
  230. sprintf(errstr,"INSUFFICIENT MEMORY for channel sorting array.\n");
  231. return(MEMORY_ERROR);
  232. }
  233. display_virtual_time(0L,dz);
  234. //spec_process_file =
  235. if((exit_status = spectstretch(dz)) < 0) {
  236. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  237. return(FAILED);
  238. }
  239. if((exit_status = complete_output(dz))<0) { // CDP LIB
  240. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  241. return(FAILED);
  242. }
  243. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  244. free(dz);
  245. return(SUCCEEDED);
  246. }
  247. /**********************************************
  248. REPLACED CDP LIB FUNCTIONS
  249. **********************************************/
  250. /****************************** SET_PARAM_DATA *********************************/
  251. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  252. {
  253. ap->special_data = (char)special_data;
  254. ap->param_cnt = (char)paramcnt;
  255. ap->max_param_cnt = (char)maxparamcnt;
  256. if(ap->max_param_cnt>0) {
  257. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  258. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  259. return(MEMORY_ERROR);
  260. }
  261. strcpy(ap->param_list,paramlist);
  262. }
  263. return(FINISHED);
  264. }
  265. /****************************** SET_VFLGS *********************************/
  266. int set_vflgs
  267. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  268. {
  269. ap->option_cnt = (char) optcnt; /*RWD added cast */
  270. if(optcnt) {
  271. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  272. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  273. return(MEMORY_ERROR);
  274. }
  275. strcpy(ap->option_list,optlist);
  276. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  277. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  278. return(MEMORY_ERROR);
  279. }
  280. strcpy(ap->option_flags,optflags);
  281. }
  282. ap->vflag_cnt = (char) vflagcnt;
  283. ap->variant_param_cnt = (char) vparamcnt;
  284. if(vflagcnt) {
  285. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  286. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  287. return(MEMORY_ERROR);
  288. }
  289. strcpy(ap->variant_list,varlist);
  290. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  291. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  292. return(MEMORY_ERROR);
  293. }
  294. strcpy(ap->variant_flags,varflags);
  295. }
  296. return(FINISHED);
  297. }
  298. /***************************** APPLICATION_INIT **************************/
  299. int application_init(dataptr dz)
  300. {
  301. int exit_status, n;
  302. int storage_cnt;
  303. int tipc, brkcnt;
  304. aplptr ap = dz->application;
  305. if(ap->vflag_cnt>0)
  306. initialise_vflags(dz);
  307. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  308. ap->total_input_param_cnt = (char)tipc;
  309. if(tipc>0) {
  310. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  311. return(exit_status);
  312. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  313. return(exit_status);
  314. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  315. return(exit_status);
  316. }
  317. brkcnt = tipc;
  318. if(brkcnt>0) {
  319. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  320. return(exit_status);
  321. }
  322. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  323. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  324. return(exit_status);
  325. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  326. return(exit_status);
  327. }
  328. if((exit_status = mark_parameter_types(dz,ap))<0)
  329. return(exit_status);
  330. // establish_infile_constants() replaced by
  331. dz->infilecnt = 1;
  332. dz->iarray_cnt = 1;
  333. dz->array_cnt = 2;
  334. dz->ptr_cnt = 4;
  335. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  336. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  337. return(MEMORY_ERROR);
  338. }
  339. for(n=0;n<dz->array_cnt;n++)
  340. dz->parray[n] = NULL;
  341. if((dz->iparray = (int **)malloc(dz->iarray_cnt * sizeof(int *)))==NULL) {
  342. sprintf(errstr,"INSUFFICIENT MEMORY for internal integer arrays.\n");
  343. return(MEMORY_ERROR);
  344. }
  345. for(n=0;n<dz->iarray_cnt;n++)
  346. dz->iparray[n] = NULL;
  347. if((dz->ptr = (double **)malloc(dz->ptr_cnt * sizeof(double *)))==NULL) {
  348. sprintf(errstr,"INSUFFICIENT MEMORY for internal pointer arrays.\n");
  349. return(MEMORY_ERROR);
  350. }
  351. for(n=0;n<dz->ptr_cnt;n++)
  352. dz->ptr[n] = NULL;
  353. return(FINISHED);
  354. }
  355. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  356. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  357. {
  358. int n;
  359. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  360. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  361. return(MEMORY_ERROR);
  362. }
  363. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  364. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  365. return(MEMORY_ERROR);
  366. }
  367. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  368. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  369. return(MEMORY_ERROR);
  370. }
  371. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  372. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  373. return(MEMORY_ERROR);
  374. }
  375. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  376. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  377. return(MEMORY_ERROR);
  378. }
  379. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  380. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  381. return(MEMORY_ERROR);
  382. }
  383. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  384. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  385. return(MEMORY_ERROR);
  386. }
  387. for(n=0;n<brkcnt;n++) {
  388. dz->brk[n] = NULL;
  389. dz->brkptr[n] = NULL;
  390. dz->brkinit[n] = 0;
  391. dz->brksize[n] = 0;
  392. }
  393. return(FINISHED);
  394. }
  395. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  396. /* RWD mallo changed to calloc; helps debug verison run as release! */
  397. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  398. {
  399. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  400. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  401. return(MEMORY_ERROR);
  402. }
  403. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  404. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  405. return(MEMORY_ERROR);
  406. }
  407. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  408. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  409. return(MEMORY_ERROR);
  410. }
  411. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  412. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  413. return(MEMORY_ERROR);
  414. }
  415. return(FINISHED);
  416. }
  417. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  418. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  419. {
  420. int n;
  421. for(n=0;n<storage_cnt;n++) {
  422. dz->is_int[n] = (char)0;
  423. dz->no_brk[n] = (char)0;
  424. }
  425. return(FINISHED);
  426. }
  427. /***************************** MARK_PARAMETER_TYPES **************************/
  428. int mark_parameter_types(dataptr dz,aplptr ap)
  429. {
  430. int n, m; /* PARAMS */
  431. for(n=0;n<ap->max_param_cnt;n++) {
  432. switch(ap->param_list[n]) {
  433. case('0'): break; /* dz->is_active[n] = 0 is default */
  434. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  435. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  436. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  437. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  438. default:
  439. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  440. return(PROGRAM_ERROR);
  441. }
  442. } /* OPTIONS */
  443. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  444. switch(ap->option_list[n]) {
  445. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  446. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  447. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  448. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  449. default:
  450. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  451. return(PROGRAM_ERROR);
  452. }
  453. } /* VARIANTS */
  454. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  455. switch(ap->variant_list[n]) {
  456. case('0'): break;
  457. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  458. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  459. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  460. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  461. default:
  462. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  463. return(PROGRAM_ERROR);
  464. }
  465. } /* INTERNAL */
  466. for(n=0,
  467. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  468. switch(ap->internal_param_list[n]) {
  469. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  470. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  471. case('d'): dz->no_brk[m] = (char)1; break;
  472. default:
  473. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  474. return(PROGRAM_ERROR);
  475. }
  476. }
  477. return(FINISHED);
  478. }
  479. /***************************** HANDLE_THE_OUTFILE **************************/
  480. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz)
  481. {
  482. int exit_status;
  483. char *filename = NULL;
  484. filename = (*cmdline)[0];
  485. strcpy(dz->outfilename,filename);
  486. if((exit_status = create_sized_outfile(filename,dz))<0)
  487. return(exit_status);
  488. (*cmdline)++;
  489. (*cmdlinecnt)--;
  490. return(FINISHED);
  491. }
  492. /***************************** ESTABLISH_APPLICATION **************************/
  493. int establish_application(dataptr dz)
  494. {
  495. aplptr ap;
  496. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  497. sprintf(errstr,"establish_application()\n");
  498. return(MEMORY_ERROR);
  499. }
  500. ap = dz->application;
  501. memset((char *)ap,0,sizeof(struct applic));
  502. return(FINISHED);
  503. }
  504. /************************* INITIALISE_VFLAGS *************************/
  505. int initialise_vflags(dataptr dz)
  506. {
  507. int n;
  508. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  509. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  510. return(MEMORY_ERROR);
  511. }
  512. for(n=0;n<dz->application->vflag_cnt;n++)
  513. dz->vflag[n] = FALSE;
  514. return FINISHED;
  515. }
  516. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  517. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  518. {
  519. int n;
  520. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  521. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  522. return(MEMORY_ERROR);
  523. }
  524. for(n=0;n<tipc;n++)
  525. ap->default_val[n] = 0.0;
  526. return(FINISHED);
  527. }
  528. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  529. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  530. {
  531. int n;
  532. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  533. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  534. return(MEMORY_ERROR);
  535. }
  536. for(n=0;n<tipc;n++)
  537. dz->is_active[n] = (char)0;
  538. return(FINISHED);
  539. }
  540. /************************* SETUP_THE_APPLICATION *******************/
  541. int setup_the_application(dataptr dz)
  542. {
  543. int exit_status;
  544. aplptr ap;
  545. if((exit_status = establish_application(dz))<0) // GLOBAL
  546. return(FAILED);
  547. ap = dz->application;
  548. // SEE parstruct FOR EXPLANATION of next 2 functions
  549. if((exit_status = set_param_data(ap,0,3,3,"Ddd"))< 0)
  550. return(FAILED);
  551. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))< 0)
  552. return(FAILED);
  553. // set_legal_infile_structure -->
  554. dz->has_otherfile = FALSE;
  555. // assign_process_logic -->
  556. dz->input_data_type = ANALFILE_ONLY;
  557. dz->process_type = BIG_ANALFILE;
  558. dz->outfiletype = ANALFILE_OUT;
  559. return application_init(dz); //GLOBAL
  560. }
  561. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  562. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  563. {
  564. int exit_status;
  565. infileptr infile_info;
  566. if(!sloom) {
  567. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  568. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  569. return(MEMORY_ERROR);
  570. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  571. sprintf(errstr,"Failed tp parse input file %s\n",cmdline[0]);
  572. return(PROGRAM_ERROR);
  573. } else if(infile_info->filetype != ANALFILE) {
  574. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  575. return(DATA_ERROR);
  576. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  577. sprintf(errstr,"Failed to copy file parsing information\n");
  578. return(PROGRAM_ERROR);
  579. }
  580. free(infile_info);
  581. }
  582. dz->clength = dz->wanted / 2;
  583. dz->chwidth = dz->nyquist/(double)(dz->clength-1);
  584. dz->halfchwidth = dz->chwidth/2.0;
  585. return(FINISHED);
  586. }
  587. /************************* SETUP_THE_PARAM_RANGES_AND_DEFAULTS *******************/
  588. int setup_the_param_ranges_and_defaults(dataptr dz)
  589. {
  590. int exit_status;
  591. aplptr ap = dz->application;
  592. // set_param_ranges()
  593. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  594. // NB total_input_param_cnt is > 0 !!!s
  595. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  596. return(FAILED);
  597. // get_param_ranges()
  598. ap->lo[TSTR_STRETCH] = 0.0001;
  599. ap->hi[TSTR_STRETCH] = 10000;
  600. ap->default_val[TSTR_STRETCH] = 1.0;
  601. ap->lo[DISC_RATIO] = 0.0;
  602. ap->hi[DISC_RATIO] = 1.0;
  603. ap->default_val[DISC_RATIO] = 0.75;
  604. ap->lo[DISC_RAND] = 0.0;
  605. ap->hi[DISC_RAND] = 1.0;
  606. ap->default_val[DISC_RAND] = 0.5;
  607. dz->maxmode = 0;
  608. if(!sloom)
  609. put_default_vals_in_all_params(dz);
  610. return(FINISHED);
  611. }
  612. /********************************* PARSE_SLOOM_DATA *********************************/
  613. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  614. {
  615. int exit_status;
  616. int cnt = 1, infilecnt;
  617. int filesize, insams, inbrksize;
  618. double dummy;
  619. int true_cnt = 0;
  620. aplptr ap;
  621. while(cnt<=PRE_CMDLINE_DATACNT) {
  622. if(cnt > argc) {
  623. sprintf(errstr,"Insufficient data sent from TK\n");
  624. return(DATA_ERROR);
  625. }
  626. switch(cnt) {
  627. case(1):
  628. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  629. sprintf(errstr,"Cannot read process no. sent from TK\n");
  630. return(DATA_ERROR);
  631. }
  632. break;
  633. case(2):
  634. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  635. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  636. return(DATA_ERROR);
  637. }
  638. if(dz->mode > 0)
  639. dz->mode--;
  640. //setup_particular_application() =
  641. if((exit_status = setup_the_application(dz))<0)
  642. return(exit_status);
  643. ap = dz->application;
  644. break;
  645. case(3):
  646. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  647. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  648. return(DATA_ERROR);
  649. }
  650. if(infilecnt < 1) {
  651. true_cnt = cnt + 1;
  652. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  653. }
  654. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  655. return(exit_status);
  656. break;
  657. case(INPUT_FILETYPE+4):
  658. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  659. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  660. return(DATA_ERROR);
  661. }
  662. break;
  663. case(INPUT_FILESIZE+4):
  664. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  665. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  666. return(DATA_ERROR);
  667. }
  668. dz->insams[0] = filesize;
  669. break;
  670. case(INPUT_INSAMS+4):
  671. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  672. sprintf(errstr,"Cannot read insams sent from TK\n");
  673. return(DATA_ERROR);
  674. }
  675. dz->insams[0] = insams;
  676. break;
  677. case(INPUT_SRATE+4):
  678. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  679. sprintf(errstr,"Cannot read srate sent from TK\n");
  680. return(DATA_ERROR);
  681. }
  682. break;
  683. case(INPUT_CHANNELS+4):
  684. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  685. sprintf(errstr,"Cannot read channels sent from TK\n");
  686. return(DATA_ERROR);
  687. }
  688. break;
  689. case(INPUT_STYPE+4):
  690. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  691. sprintf(errstr,"Cannot read stype sent from TK\n");
  692. return(DATA_ERROR);
  693. }
  694. break;
  695. case(INPUT_ORIGSTYPE+4):
  696. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  697. sprintf(errstr,"Cannot read origstype sent from TK\n");
  698. return(DATA_ERROR);
  699. }
  700. break;
  701. case(INPUT_ORIGRATE+4):
  702. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  703. sprintf(errstr,"Cannot read origrate sent from TK\n");
  704. return(DATA_ERROR);
  705. }
  706. break;
  707. case(INPUT_MLEN+4):
  708. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  709. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  710. return(DATA_ERROR);
  711. }
  712. break;
  713. case(INPUT_DFAC+4):
  714. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  715. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  716. return(DATA_ERROR);
  717. }
  718. break;
  719. case(INPUT_ORIGCHANS+4):
  720. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  721. sprintf(errstr,"Cannot read origchans sent from TK\n");
  722. return(DATA_ERROR);
  723. }
  724. break;
  725. case(INPUT_SPECENVCNT+4):
  726. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  727. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  728. return(DATA_ERROR);
  729. }
  730. dz->specenvcnt = dz->infile->specenvcnt;
  731. break;
  732. case(INPUT_WANTED+4):
  733. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  734. sprintf(errstr,"Cannot read wanted sent from TK\n");
  735. return(DATA_ERROR);
  736. }
  737. break;
  738. case(INPUT_WLENGTH+4):
  739. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  740. sprintf(errstr,"Cannot read wlength sent from TK\n");
  741. return(DATA_ERROR);
  742. }
  743. break;
  744. case(INPUT_OUT_CHANS+4):
  745. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  746. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  747. return(DATA_ERROR);
  748. }
  749. break;
  750. /* RWD these chanegs to samps - tk will have to deal with that! */
  751. case(INPUT_DESCRIPTOR_BYTES+4):
  752. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  753. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  754. return(DATA_ERROR);
  755. }
  756. break;
  757. case(INPUT_IS_TRANSPOS+4):
  758. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  759. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  760. return(DATA_ERROR);
  761. }
  762. break;
  763. case(INPUT_COULD_BE_TRANSPOS+4):
  764. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  765. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  766. return(DATA_ERROR);
  767. }
  768. break;
  769. case(INPUT_COULD_BE_PITCH+4):
  770. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  771. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  772. return(DATA_ERROR);
  773. }
  774. break;
  775. case(INPUT_DIFFERENT_SRATES+4):
  776. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  777. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  778. return(DATA_ERROR);
  779. }
  780. break;
  781. case(INPUT_DUPLICATE_SNDS+4):
  782. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  783. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  784. return(DATA_ERROR);
  785. }
  786. break;
  787. case(INPUT_BRKSIZE+4):
  788. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  789. sprintf(errstr,"Cannot read brksize sent from TK\n");
  790. return(DATA_ERROR);
  791. }
  792. if(inbrksize > 0) {
  793. switch(dz->input_data_type) {
  794. case(WORDLIST_ONLY):
  795. break;
  796. case(PITCH_AND_PITCH):
  797. case(PITCH_AND_TRANSPOS):
  798. case(TRANSPOS_AND_TRANSPOS):
  799. dz->tempsize = inbrksize;
  800. break;
  801. case(BRKFILES_ONLY):
  802. case(UNRANGED_BRKFILE_ONLY):
  803. case(DB_BRKFILES_ONLY):
  804. case(ALL_FILES):
  805. case(ANY_NUMBER_OF_ANY_FILES):
  806. if(dz->extrabrkno < 0) {
  807. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  808. return(DATA_ERROR);
  809. }
  810. if(dz->brksize == NULL) {
  811. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  812. return(PROGRAM_ERROR);
  813. }
  814. dz->brksize[dz->extrabrkno] = inbrksize;
  815. break;
  816. default:
  817. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  818. dz->input_data_type);
  819. return(PROGRAM_ERROR);
  820. }
  821. break;
  822. }
  823. break;
  824. case(INPUT_NUMSIZE+4):
  825. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  826. sprintf(errstr,"Cannot read numsize sent from TK\n");
  827. return(DATA_ERROR);
  828. }
  829. break;
  830. case(INPUT_LINECNT+4):
  831. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  832. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  833. return(DATA_ERROR);
  834. }
  835. break;
  836. case(INPUT_ALL_WORDS+4):
  837. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  838. sprintf(errstr,"Cannot read all_words sent from TK\n");
  839. return(DATA_ERROR);
  840. }
  841. break;
  842. case(INPUT_ARATE+4):
  843. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  844. sprintf(errstr,"Cannot read arate sent from TK\n");
  845. return(DATA_ERROR);
  846. }
  847. break;
  848. case(INPUT_FRAMETIME+4):
  849. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  850. sprintf(errstr,"Cannot read frametime sent from TK\n");
  851. return(DATA_ERROR);
  852. }
  853. dz->frametime = (float)dummy;
  854. break;
  855. case(INPUT_WINDOW_SIZE+4):
  856. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  857. sprintf(errstr,"Cannot read window_size sent from TK\n");
  858. return(DATA_ERROR);
  859. }
  860. break;
  861. case(INPUT_NYQUIST+4):
  862. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  863. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  864. return(DATA_ERROR);
  865. }
  866. break;
  867. case(INPUT_DURATION+4):
  868. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  869. sprintf(errstr,"Cannot read duration sent from TK\n");
  870. return(DATA_ERROR);
  871. }
  872. break;
  873. case(INPUT_MINBRK+4):
  874. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  875. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  876. return(DATA_ERROR);
  877. }
  878. break;
  879. case(INPUT_MAXBRK+4):
  880. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  881. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  882. return(DATA_ERROR);
  883. }
  884. break;
  885. case(INPUT_MINNUM+4):
  886. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  887. sprintf(errstr,"Cannot read minnum sent from TK\n");
  888. return(DATA_ERROR);
  889. }
  890. break;
  891. case(INPUT_MAXNUM+4):
  892. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  893. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  894. return(DATA_ERROR);
  895. }
  896. break;
  897. default:
  898. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  899. return(PROGRAM_ERROR);
  900. }
  901. cnt++;
  902. }
  903. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  904. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  905. return(DATA_ERROR);
  906. }
  907. if(true_cnt)
  908. cnt = true_cnt;
  909. *cmdlinecnt = 0;
  910. while(cnt < argc) {
  911. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  912. return(exit_status);
  913. cnt++;
  914. }
  915. return(FINISHED);
  916. }
  917. /********************************* GET_TK_CMDLINE_WORD *********************************/
  918. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  919. {
  920. if(*cmdlinecnt==0) {
  921. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  922. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  923. return(MEMORY_ERROR);
  924. }
  925. } else {
  926. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  927. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  928. return(MEMORY_ERROR);
  929. }
  930. }
  931. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  932. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  933. return(MEMORY_ERROR);
  934. }
  935. strcpy((*cmdline)[*cmdlinecnt],q);
  936. (*cmdlinecnt)++;
  937. return(FINISHED);
  938. }
  939. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  940. int assign_file_data_storage(int infilecnt,dataptr dz)
  941. {
  942. int exit_status;
  943. int no_sndfile_system_files = FALSE;
  944. dz->infilecnt = infilecnt;
  945. if((exit_status = allocate_filespace(dz))<0)
  946. return(exit_status);
  947. if(no_sndfile_system_files)
  948. dz->infilecnt = 0;
  949. return(FINISHED);
  950. }
  951. /************************* redundant functions: to ensure libs compile OK *******************/
  952. int assign_process_logic(dataptr dz)
  953. {
  954. return(FINISHED);
  955. }
  956. void set_legal_infile_structure(dataptr dz)
  957. {}
  958. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  959. {
  960. return(FINISHED);
  961. }
  962. int setup_internal_arrays_and_array_pointers(dataptr dz)
  963. {
  964. return(FINISHED);
  965. }
  966. int establish_bufptrs_and_extra_buffers(dataptr dz)
  967. {
  968. return(FINISHED);
  969. }
  970. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  971. {
  972. return(FINISHED);
  973. }
  974. int read_special_data(char *str,dataptr dz)
  975. {
  976. return(FINISHED);
  977. }
  978. int inner_loop
  979. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  980. {
  981. return(FINISHED);
  982. }
  983. /********************************************************************************************/
  984. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  985. {
  986. if (!strcmp(prog_identifier_from_cmdline,"stretch")) dz->process = SPECTSTR;
  987. else {
  988. fprintf(stderr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  989. return(USAGE_ONLY);
  990. }
  991. return(FINISHED);
  992. }
  993. /******************************** USAGE1 ********************************/
  994. int usage1(void)
  995. {
  996. return(usage2("stretch"));
  997. }
  998. /******************************** USAGE2 ********************************/
  999. int usage2(char *str)
  1000. {
  1001. if(!strcmp(str,"stretch")) { /* STRETCH */
  1002. fprintf(stdout,
  1003. "spectstr stretch time infile outfile timestretch d-ratio di-rand\n"
  1004. "\n"
  1005. "TIME-STRETCHING OF INFILE, SUPPRESSING ARTEFACTS WHEN STRETCH IS > 1.0\n"
  1006. "\n"
  1007. "TIMESTRETCH may itself vary over time.\n"
  1008. "D-RATIO proportion of channels to discohere.\n"
  1009. "D-RAND Frequency randomisation of discohered channels.\n");
  1010. } else
  1011. sprintf(errstr,"Unknown option '%s'\n",str);
  1012. return(USAGE_ONLY);
  1013. }
  1014. int usage3(char *str1,char *str2)
  1015. {
  1016. fprintf(stderr,"Insufficient parameters on command line.\n");
  1017. return(USAGE_ONLY);
  1018. }
  1019. /************ SETUP_INTERNAL_PARAMS_FOR_TSTRETCH *************/
  1020. int setup_internal_params_for_tstretch(dataptr dz)
  1021. {
  1022. int exit_status;
  1023. if(dz->brksize[TSTR_STRETCH] && (exit_status = force_value_at_zero_time(TSTR_STRETCH,dz))<0)
  1024. return(exit_status);
  1025. dz->param[TSTR_TOTIME] = (double)dz->wlength * dz->frametime;
  1026. /* dur of orig sound source */
  1027. dz->iparam[TSTR_ARRAYSIZE] = POSMIN;
  1028. if((dz->parray[TSTR_PBUF] = (double *)malloc(dz->iparam[TSTR_ARRAYSIZE] * sizeof(double)))==NULL) {
  1029. sprintf(errstr,"INSUFFICIENT MEMORY for timestretch array.\n");
  1030. return(MEMORY_ERROR);
  1031. }
  1032. dz->ptr[TSTR_PEND] = dz->parray[TSTR_PBUF] + dz->iparam[TSTR_ARRAYSIZE];
  1033. if((dz->parray[TSTR_QBUF] = (double *)malloc(dz->iparam[TSTR_ARRAYSIZE] * sizeof(double)))==NULL) {
  1034. sprintf(errstr,"INSUFFICIENT MEMORY for 2nd timestretch array.\n");
  1035. return(MEMORY_ERROR);
  1036. }
  1037. return(FINISHED);
  1038. }
  1039. /********************* CHECK_FOR_ENOUGH_TSTRETCH_BRKPNT_VALS **********************/
  1040. int check_for_enough_tstretch_brkpnt_vals(dataptr dz)
  1041. {
  1042. if(dz->brksize[TSTR_STRETCH] && dz->brksize[TSTR_STRETCH] < 2) {
  1043. sprintf(errstr,"Not enough data in tsretch data file.\n");
  1044. return(DATA_ERROR);
  1045. }
  1046. dz->do_decohere = 1;
  1047. if(dz->param[DISC_RATIO] == 0.0 || dz->param[DISC_RAND] == 0.0)
  1048. dz->do_decohere = 0;
  1049. dz->iparam[DISC_RATIO] = (int)round(dz->param[DISC_RATIO] * dz->clength);
  1050. return(FINISHED);
  1051. }
  1052. /*************************** ALLOCATE_TSTRETCH_BUFFER ****************************/
  1053. int allocate_tstretch_buffer(dataptr dz)
  1054. {
  1055. unsigned int buffersize;
  1056. dz->bptrcnt = 6;
  1057. if((dz->flbufptr = (float **)malloc(dz->bptrcnt * sizeof(float *)))==NULL) {
  1058. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffer pointers.\n");
  1059. return(MEMORY_ERROR);
  1060. }
  1061. buffersize = dz->wanted * BUF_MULTIPLIER;
  1062. dz->buflen = buffersize;
  1063. buffersize *= 2;
  1064. buffersize += dz->wanted;
  1065. if((dz->bigfbuf = (float *)malloc((size_t)(buffersize * sizeof(float))))==NULL) {
  1066. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n");
  1067. return(MEMORY_ERROR);
  1068. }
  1069. dz->big_fsize = dz->buflen;
  1070. dz->flbufptr[0] = dz->bigfbuf + dz->wanted; /* inbuf */
  1071. dz->flbufptr[1] = dz->flbufptr[0] + dz->big_fsize; /* outbuf & inbufend */
  1072. dz->flbufptr[2] = dz->flbufptr[1] + dz->big_fsize; /* outbufend */
  1073. dz->flbufptr[3] = dz->flbufptr[0]; /* 1st inbuf pointer */
  1074. dz->flbufptr[4] = dz->flbufptr[0] + dz->wanted; /* 2nd inbuf pointer */
  1075. dz->flbufptr[5] = dz->flbufptr[1]; /* outbuf ptr */
  1076. return(FINISHED);
  1077. }
  1078. /********************************** SPECTSTRETCH ***********************************/
  1079. int spectstretch(dataptr dz)
  1080. {
  1081. int exit_status;
  1082. int thatwindow;
  1083. int samps_read;
  1084. if((samps_read = fgetfbufEx(dz->flbufptr[0], dz->big_fsize,dz->ifd[0],0)) < 0) {
  1085. sprintf(errstr,"No data found in input soundfile.\n");
  1086. return(DATA_ERROR);
  1087. }
  1088. if(sloom)
  1089. dz->total_samps_read = samps_read;
  1090. dz->flbufptr[3] = dz->flbufptr[0];
  1091. dz->flbufptr[4] = dz->flbufptr[0] + dz->wanted;
  1092. thatwindow = 0;
  1093. if(dz->brksize[TSTR_STRETCH]==0)
  1094. exit_status = do_constant_timestretch(&thatwindow,dz);
  1095. else
  1096. exit_status = do_timevariable_timestretch(&thatwindow,dz);
  1097. if(exit_status<0)
  1098. return(exit_status);
  1099. if((exit_status = do_timestretching(&thatwindow,dz->ptr[TSTR_PBUF] - dz->parray[TSTR_PBUF],dz))<0)
  1100. return(exit_status);
  1101. if(dz->flbufptr[5] - dz->flbufptr[1] > 0)
  1102. return write_samps(dz->flbufptr[1],dz->flbufptr[5] - dz->flbufptr[1],dz);
  1103. return(FINISHED);
  1104. }
  1105. /******************************* DO_CONSTANT_TIMESTRETCH ****************************
  1106. *
  1107. * Set up array of indices for reading source window values into new
  1108. * windows.
  1109. *
  1110. * (0) Initialise position-buffer pointer and start window.
  1111. * (1) Get first breakpoint pair from brkpoint table.
  1112. * (2) Calculate initial time as whole number of WINDOWS.
  1113. * (3) If time is stretched, output window is SHRUNK relative to input
  1114. * window. Hence val = 1/stretch.
  1115. * (4) Proceed in a loop of all input breakpoint values.
  1116. * (5) Returns if premature end of file encountered.
  1117. */
  1118. int do_constant_timestretch(int *thatwindow,dataptr dz)
  1119. {
  1120. double thiswdur, dcount, dur;
  1121. int count;
  1122. int startwindow = 0;
  1123. dz->ptr[TSTR_PBUF] = dz->parray[TSTR_PBUF];
  1124. thiswdur = 1.0/dz->param[TSTR_STRETCH]; /* 3 */
  1125. dcount = (double)dz->wlength/thiswdur;
  1126. count = round(fabs(dcount));
  1127. dur = (double)dz->wlength/(double)count;
  1128. return divide_time_into_equal_increments(thatwindow,startwindow,dur,count,dz);
  1129. }
  1130. /******************************* DO_TIMEVARIABLE_TIMESTRETCH ****************************
  1131. *
  1132. * Set up array of indices for reading source window values into new
  1133. * windows.
  1134. *
  1135. * (0) Initialise position-buffer pointer and start window.
  1136. * (1) Get first breakpoint pair from brkpoint table.
  1137. * (2) Calculate initial time as whole number of WINDOWS.
  1138. * (3) If time is stretched, output window is SHRUNK relative to input
  1139. * window. Hence val = 1/stretch.
  1140. * (4) Proceed in a loop of all input breakpoint values.
  1141. * (5) Returns if brkpnt times ran off end of source soundfile.
  1142. */
  1143. int do_timevariable_timestretch(int *thatwindow,dataptr dz)
  1144. {
  1145. int exit_status;
  1146. int time0, time1, time2, totaldur;
  1147. double stretch0, stretch1, thiswdur, nextwdur, valdiff, dtime0, dtime1;
  1148. int atend = FALSE;
  1149. int startwindow = 0; /* 0 */
  1150. int timestretch_called = FALSE;
  1151. dz->ptr[TSTR_PBUF] = dz->parray[TSTR_PBUF];
  1152. if((exit_status = get_both_vals_from_brktable(&dtime0,&stretch0,TSTR_STRETCH,dz))<0) /* 1 */
  1153. return(exit_status);
  1154. if(exit_status == FINISHED) {
  1155. sprintf(errstr,"Not enough data in brkfile & not trapped earlier: do_timevariable_timestretch()\n");
  1156. return(PROGRAM_ERROR);
  1157. }
  1158. time0 = round(dtime0/dz->frametime); /* 2 */
  1159. thiswdur = 1.0/stretch0; /* 3 */
  1160. while((exit_status = get_both_vals_from_brktable(&dtime1,&stretch1,TSTR_STRETCH,dz))==CONTINUE) {/* 4 */
  1161. nextwdur = 1.0/stretch1;
  1162. if((time1 = round(dtime1/dz->frametime)) > dz->wlength) {
  1163. time2 = time1; /* 5 */
  1164. time1 = round(dz->param[TSTR_TOTIME]/dz->frametime);
  1165. valdiff = nextwdur - thiswdur;
  1166. valdiff *= (double)(time1 - time0)/(double)(time2 - time0);
  1167. nextwdur = thiswdur + valdiff;
  1168. atend = TRUE;
  1169. }
  1170. totaldur = time1 - time0;
  1171. timestretch_called = TRUE;
  1172. if((exit_status = timestretch_this_segment(thatwindow,startwindow,thiswdur,nextwdur,totaldur,dz))<0)
  1173. return(exit_status);
  1174. startwindow += totaldur;
  1175. thiswdur = nextwdur;
  1176. time0 = time1;
  1177. if(atend)
  1178. break;
  1179. }
  1180. if(timestretch_called == FALSE) {
  1181. sprintf(errstr,"Not enough data in brkfile & not trapped earlier: do_timevariable_timestretch()\n");
  1182. return(PROGRAM_ERROR);
  1183. }
  1184. return(FINISHED);
  1185. }
  1186. /*************************** DO_TIMESTRETCHING ********************************
  1187. *
  1188. * Read/interpolate source analysis windows.
  1189. *
  1190. * (1) initialise the position-buffer pointer. NB we don't use pbufptr as
  1191. * this is (?) still marking the current position in the output buffer.
  1192. * (2) For each entry in the position-array.
  1193. * (3) get the absolute position.
  1194. * (4) The window to intepolate FROM is this truncated
  1195. * e.g. position 3.2 translates to window 3.
  1196. * (5) Is this is a move on from the current window position? How far?
  1197. * (6) If we're in the final window, set the 'atend' flag.
  1198. * (7) If we need to move on, call advance-windows and set up a new PAIR of
  1199. * input windows.
  1200. * (8) Get Interpolation time-fraction.
  1201. * (9) Get output values by interpolation between input values.
  1202. * (10) Write a complete out-windowbuf to output file.
  1203. * (11) Give on-screen user information.
  1204. */
  1205. int do_timestretching(int *thatwindow,int count,dataptr dz)
  1206. {
  1207. int exit_status;
  1208. double amp0, amp1, phas0, phas1;
  1209. int vc;
  1210. int n, thiswindow, step;
  1211. int atend = FALSE;
  1212. double here, frac;
  1213. double *p = dz->parray[TSTR_PBUF]; /* 1 */
  1214. for(n=0;n<count;n++) { /* 2 */
  1215. here = *p++; /* 3 */
  1216. thiswindow = (int)here; /* TRUNCATE */ /* 4 */
  1217. step = thiswindow - *thatwindow; /* 5 */
  1218. if(thiswindow == dz->wlength-1) /* 6 */
  1219. atend = TRUE;
  1220. if(step) {
  1221. if((exit_status = advance_along_input_windows(step,atend,dz))<0) /* 7 */
  1222. return(exit_status);
  1223. }
  1224. frac = here - (double)thiswindow; /* 8 */
  1225. for(vc = 0; vc < dz->wanted; vc += 2) {
  1226. amp0 = dz->flbufptr[3][AMPP]; /* 9 */
  1227. phas0 = dz->flbufptr[3][FREQ];
  1228. amp1 = dz->flbufptr[4][AMPP];
  1229. phas1 = dz->flbufptr[4][FREQ];
  1230. dz->flbufptr[5][AMPP] = (float)(amp0 + ((amp1 - amp0) * frac));
  1231. dz->flbufptr[5][FREQ] = (float)(phas0 + ((phas1 - phas0) * frac));
  1232. }
  1233. if(dz->do_decohere) {
  1234. if((exit_status = decohere(dz->flbufptr[5],dz))<0)
  1235. return(exit_status);
  1236. }
  1237. if((dz->flbufptr[5] += dz->wanted) >= dz->flbufptr[2]) {
  1238. if((exit_status = write_exact_samps(dz->flbufptr[1],dz->big_fsize,dz))<0)
  1239. return(exit_status);
  1240. dz->flbufptr[5] = dz->flbufptr[1];
  1241. }
  1242. *thatwindow = thiswindow;
  1243. }
  1244. return(FINISHED);
  1245. }
  1246. /**************************** DIVIDE_TIME_INTO_EQUAL_INCREMENTS *****************************
  1247. *
  1248. * all time intervals are equal. Divide up total time thus.
  1249. */
  1250. int divide_time_into_equal_increments(int *thatwindow,int startwindow,double dur,int count,dataptr dz)
  1251. {
  1252. int exit_status;
  1253. int n, remnant;
  1254. int end = dz->ptr[TSTR_PEND] - dz->ptr[TSTR_PBUF];
  1255. int start = 0;
  1256. while(count >= end) {
  1257. for(n = start; n < end; n++)
  1258. *(dz->ptr[TSTR_PBUF])++ = (dur * (double)n) + (double)startwindow;
  1259. if((exit_status = do_timestretching(thatwindow,dz->iparam[TSTR_ARRAYSIZE],dz))<0)
  1260. return(exit_status);
  1261. dz->ptr[TSTR_PBUF] = dz->parray[TSTR_PBUF];
  1262. start = end;
  1263. end += dz->iparam[TSTR_ARRAYSIZE];
  1264. }
  1265. if((remnant = count - start)>0) {
  1266. for(n=start;n<count;n++)
  1267. *(dz->ptr[TSTR_PBUF])++ = (dur * (double)n) + (double)startwindow;
  1268. }
  1269. return(FINISHED);
  1270. }
  1271. /**************************** GET_BOTH_VALS_FROM_BRKTABLE ****************************/
  1272. int get_both_vals_from_brktable(double *thistime,double *thisstretch,int brktab_no,dataptr dz)
  1273. {
  1274. double *brkend = dz->brk[brktab_no] + (dz->brksize[brktab_no] * 2);
  1275. if(dz->brkptr[brktab_no]>=brkend)
  1276. return(FINISHED);
  1277. *thistime = *(dz->brkptr[brktab_no])++;
  1278. if(dz->brkptr[brktab_no]>=brkend) {
  1279. sprintf(errstr,"Anomaly in get_both_vals_from_brktable().\n");
  1280. return(PROGRAM_ERROR);
  1281. }
  1282. *thisstretch = *(dz->brkptr[brktab_no])++;
  1283. return(CONTINUE);
  1284. }
  1285. /************************** TIMESTRETCH_THIS_SEGMENT *****************************
  1286. *
  1287. * Takes a group of input windows, counts number of output windows
  1288. * corresponding to this buffer, and sets up, in pbuff, array(s) of values
  1289. * which are the positions in the input array corresponding to the output
  1290. * array positions.
  1291. * (1) If there is (almost) no change in duration of segments, calculates
  1292. * times on simple additive basis.
  1293. * (2) Otherwise, uses exponential formula.
  1294. * (3) If NOT passed a negative number (i.e. flagged), the sequence of time
  1295. * intervals is reversed.
  1296. */
  1297. int timestretch_this_segment(int *thatwindow,int startwindow,double thiswdur,double nextwdur,int totaldur,dataptr dz)
  1298. {
  1299. int stretch_decreasing = TRUE;
  1300. int count;
  1301. double dur, dcount = calculate_number_of_output_windows(thiswdur,nextwdur,totaldur);
  1302. double param0, param1, param2;
  1303. if(dcount < 0.0)
  1304. stretch_decreasing = FALSE;
  1305. count = round(fabs(dcount));
  1306. if(fabs(nextwdur - thiswdur)<=FLTERR) { /* 1 */
  1307. dur = (double)totaldur/(double)count;
  1308. return divide_time_into_equal_increments(thatwindow,startwindow,dur,count,dz);
  1309. }
  1310. param0 = nextwdur - thiswdur; /* 2 */
  1311. param1 = param0/(double)totaldur;
  1312. param2 = thiswdur * (double)totaldur;
  1313. if(stretch_decreasing==TRUE) /* 3 */
  1314. return calc_position_output_wdws_relative_to_input_wdws_for_decreasing_stretch
  1315. (thatwindow,startwindow,count,totaldur,param0,param1,param2,dz);
  1316. else
  1317. return calc_position_output_wdws_relative_to_input_wdws_for_increasing_stretch
  1318. (thatwindow,startwindow,count,param0,param1,param2,dz);
  1319. }
  1320. /*************************** ADVANCE_ALONG_INPUT_WINDOWS ****************************
  1321. *
  1322. * Advance window frame in input.
  1323. * (1) If got to end of data... Advanve ONE LESS than the distance
  1324. * (wdw_to_advance) from last window-pair.
  1325. * (2) Else, advance by cnt windows.
  1326. * (3) If at end of buffer, copy THIS window to start of whole buffer,
  1327. * (4) And read more data in AFTER that (dz->wanted samps from start).
  1328. * (6) If this is the last window in the source file, there is no other
  1329. * window to interpolate to, so set last window to same as this.
  1330. */
  1331. int advance_along_input_windows(int wdw_to_advance,int atend,dataptr dz)
  1332. {
  1333. int n, count;
  1334. int samps_read;
  1335. if(atend) /* 1 */
  1336. count = wdw_to_advance-1;
  1337. else /* 2 */
  1338. count = wdw_to_advance;
  1339. for(n=0;n<count;n++) {
  1340. dz->flbufptr[3] = dz->flbufptr[4]; /* ADVANCE LASTWINDOW TO THISWINDOW */
  1341. if((dz->flbufptr[4] += dz->wanted) > dz->flbufptr[1]) { /* ADVANCE THISWINDOW TO NEXT */
  1342. memmove((char *)dz->bigfbuf,(char *)dz->flbufptr[3],dz->wanted * sizeof(float));
  1343. dz->flbufptr[3] = dz->bigfbuf; /* 3 */
  1344. dz->flbufptr[4] = dz->flbufptr[0]; /* 4 */
  1345. if((samps_read = fgetfbufEx(dz->flbufptr[4],dz->big_fsize,dz->ifd[0],0)) < dz->wanted) {
  1346. if(n <= count-2) {
  1347. sprintf(errstr,"Program miscounted windows: anomaly 1 at EOF? : advance_along_input_windows()\n");
  1348. return(PROGRAM_ERROR);
  1349. }
  1350. if(samps_read < 0) {
  1351. sprintf(errstr,"Program miscounted windows: anomaly 2 at EOF? : advance_along_input_windows()\n");
  1352. return(PROGRAM_ERROR);
  1353. }
  1354. return(FINISHED);
  1355. }
  1356. if(sloom)
  1357. dz->total_samps_read += samps_read;
  1358. }
  1359. }
  1360. if(atend) /* 6 */
  1361. dz->flbufptr[4] = dz->flbufptr[3];
  1362. return(CONTINUE);
  1363. }
  1364. /*************************** CALCULATE_NUMBER_OF_OUTPUT_WINDOWS ***************************
  1365. *
  1366. * Given a sequence of events of varying duration, where initial and
  1367. * final durations are known, together with start and end times of the
  1368. * total sequence, this function calculates the total number of events.
  1369. *
  1370. * NB
  1371. * (1) Where the segment duration is increading, log(startwdur/edndur) is -ve,
  1372. * so the returned count is -ve.
  1373. * (2) Where segment duration is NOT changing, log(startwdur/endwdur), and hence dcount, would be zero.
  1374. * This situation is trapped by first checking for (approx) equality of startwdur and endwdur
  1375. * and calculating the count in simpler manner.
  1376. */
  1377. double calculate_number_of_output_windows(double startwdur,double endwdur,int totaldur)
  1378. {
  1379. double durdiff, dcount;
  1380. if(flteq((durdiff = endwdur - startwdur),0.0)) {
  1381. dcount = (double)totaldur/endwdur;
  1382. return(dcount);
  1383. }
  1384. dcount = startwdur/endwdur;
  1385. dcount = log(dcount);
  1386. dcount *= (double)totaldur/durdiff;
  1387. return(dcount);
  1388. }
  1389. /********* CALC_POSITION_OUTPUT_WDWS_RELATIVE_TO_INPUT_WDWS_FOR_DECREASING_STRETCH ************
  1390. *
  1391. * THE ARRAY OF positions has to be retrograded, but if we have more than
  1392. * a single output-arrayfull of position values, we must start calculating
  1393. * the output position array from the last input position, then invert those
  1394. * positions so they are first positions, and so on!!!
  1395. * (0) As the array is to be filled backwards, start calculating positions
  1396. * from end of input. So end of first pass = end of input (= count).
  1397. * (1) Find how many locations remain in output-position buffer.
  1398. * (2) Start of first pass is this no of positions before end.
  1399. * (3) If start is < 0 this means that all the positions in the current input
  1400. * pass will fit in the output buffer in its current state, so we skip the
  1401. * loop and go to (10), Otherwise...
  1402. * (4) Mark the address in the output buffer where writing-values begins.
  1403. * (5) Store in output buffer, the positions RELATIVE to start of this segment
  1404. * of input values (i.e. relative to ZERO).
  1405. * (6) Retrograde this time-interval set.
  1406. * (7) Increment these relative values by startwindow, to give absolute
  1407. * position values.
  1408. * (8) Do the output, and reinitialise the output buffer pointer to start
  1409. * of buffer. Reset new input-block end to START of previous block
  1410. * (we're working backwards).
  1411. * (9) Decrement start of block by size of output array.
  1412. * (10) If either we did not enter the loop OR we have some input values
  1413. * left on leaving the principle loop, calcuate output positions of
  1414. * these items, and store in the output buffer (which will be flushed
  1415. * either by next call to timestretch_this_segment() or, at end, by flushbuf()).
  1416. */
  1417. int calc_position_output_wdws_relative_to_input_wdws_for_decreasing_stretch
  1418. (int *thatwindow,int startwindow,int count,int totaldur,double param0,double param1,double param2,dataptr dz)
  1419. {
  1420. int exit_status;
  1421. int n, start, end = count; /* 0 */
  1422. double *p, *startptr;
  1423. int tofill = dz->ptr[TSTR_PEND] - dz->ptr[TSTR_PBUF]; /* 1 */
  1424. start = count - tofill; /* 2 */
  1425. while(start>=0) { /* 3 */
  1426. startptr = dz->ptr[TSTR_PBUF]; /* 4 */
  1427. for(n=start;n<end;n++) /* 5 */
  1428. *(dz->ptr[TSTR_PBUF])++ = calculate_position(n,param0,param1,param2);
  1429. /* 6 */
  1430. if((exit_status = retrograde_sequence_of_time_intervals(totaldur - start,end - start,startptr,dz))<0)
  1431. return(exit_status);
  1432. for(p = startptr;p<dz->ptr[TSTR_PEND];p++)
  1433. *p += (double)startwindow; /* 7 */
  1434. if((exit_status = do_timestretching(thatwindow,dz->iparam[TSTR_ARRAYSIZE],dz))<0)
  1435. return(exit_status);
  1436. dz->ptr[TSTR_PBUF] = dz->parray[TSTR_PBUF]; /* 8 */
  1437. end = start;
  1438. start -= dz->iparam[TSTR_ARRAYSIZE]; /* 9 */
  1439. }
  1440. if(end) { /* 10 */
  1441. startptr = dz->ptr[TSTR_PBUF];
  1442. for(n=0;n<end;n++)
  1443. *(dz->ptr[TSTR_PBUF])++ = calculate_position(n,param0,param1,param2);
  1444. if((exit_status = retrograde_sequence_of_time_intervals(totaldur,end,startptr,dz))<0)
  1445. return(exit_status);
  1446. for(p = startptr;p<dz->ptr[TSTR_PBUF];p++)
  1447. *p += (double)startwindow;
  1448. }
  1449. return(FINISHED);
  1450. }
  1451. /********* CALC_POSITION_OUTPUT_WDWS_RELATIVE_TO_INPUT_WDWS_FOR_INCREASING_STRETCH ************
  1452. *
  1453. * Find positions of output samples relative to input samples, buffer
  1454. * by buffer.
  1455. */
  1456. int calc_position_output_wdws_relative_to_input_wdws_for_increasing_stretch
  1457. (int *thatwindow,int startwindow,int count,double param0,double param1,double param2,dataptr dz)
  1458. {
  1459. int exit_status;
  1460. int n;
  1461. int start = 0;
  1462. int end = dz->ptr[TSTR_PEND] - dz->ptr[TSTR_PBUF];
  1463. while(count>=end) {
  1464. for(n=start;n<end;n++)
  1465. *(dz->ptr[TSTR_PBUF])++ = calculate_position(n,param0,param1,param2) + (double)startwindow;
  1466. if((exit_status = do_timestretching(thatwindow,dz->iparam[TSTR_ARRAYSIZE],dz))<0)
  1467. return(exit_status);
  1468. dz->ptr[TSTR_PBUF] = dz->parray[TSTR_PBUF];
  1469. start = end;
  1470. end += dz->iparam[TSTR_ARRAYSIZE];
  1471. }
  1472. if(count-start) {
  1473. for(n=start;n<count;n++)
  1474. *(dz->ptr[TSTR_PBUF])++ = calculate_position(n,param0,param1,param2) + (double)startwindow;
  1475. }
  1476. return(FINISHED);
  1477. }
  1478. /*************************** CALCULATE_POSITION ****************************
  1479. *
  1480. * Do the position calculation using the exponential formula.
  1481. */
  1482. double calculate_position(int x,double param0,double param1,double param2)
  1483. {
  1484. double k;
  1485. k = param1;
  1486. k *= (double)x;
  1487. k = exp(k);
  1488. k *= param2;
  1489. k -= param2;
  1490. return(k/(param0));
  1491. }
  1492. /*************************** RETROGRADE_SEQUENCE_OF_TIME_INTERVALS **************************
  1493. *
  1494. * Accepts a sequence of times, starting in address startptr,
  1495. * and retrogrades the sequence of time-intervals, storing the
  1496. * results back in startptr onwards.
  1497. */
  1498. int retrograde_sequence_of_time_intervals(int endtime,int count,double *startptr,dataptr dz)
  1499. {
  1500. double newtime, lasttime, tsize;
  1501. int n;
  1502. double *p = startptr;
  1503. dz->ptr[TSTR_QBUF] = dz->parray[TSTR_QBUF] + (count-1);
  1504. newtime = endtime;
  1505. for(n=0;n<(count-1);n++) {
  1506. lasttime = *p++;
  1507. tsize = *p - lasttime;
  1508. newtime -= tsize;
  1509. *dz->ptr[TSTR_QBUF]-- = newtime;
  1510. }
  1511. if(dz->ptr[TSTR_QBUF]!=dz->parray[TSTR_QBUF]) {
  1512. sprintf(errstr,"counting problem: retrograde_sequence_of_time_intervals()\n");
  1513. return(PROGRAM_ERROR);
  1514. }
  1515. *dz->ptr[TSTR_QBUF] = *p; /* put starttime at start of intervals inverted array */
  1516. p = startptr;
  1517. for(n=0;n<count;n++)
  1518. *p++ = *dz->ptr[TSTR_QBUF]++;
  1519. return(FINISHED);
  1520. }
  1521. /******************************************* DECOHERE ****************************************/
  1522. int decohere(float *fbuf,dataptr dz)
  1523. {
  1524. int *store = dz->iparray[0];
  1525. int cc, vc, j, k;
  1526. float ampj, ampk;
  1527. double randval, chanfrq, frq;
  1528. for(cc = 0; cc < dz->clength;cc++)
  1529. store[cc] = cc;
  1530. // Sort channel numbers into order of ascending loudness
  1531. for(k = 0; k < dz->clength - 1; k++) {
  1532. vc = store[k] * 2; // vc is index of amps in buffer, cc is count of chans (chan = amp&frq vals)
  1533. ampk = fbuf[AMPP]; // AMPP = vc
  1534. for(j = k+1; j < dz->clength; j++) {
  1535. vc = store[j] * 2;
  1536. ampj = fbuf[AMPP];
  1537. if(ampj < ampk) { // If item higher up array (ampj) has lower amp than item lower down array (ampk)
  1538. cc = store[k]; // Swap the stored channel numbers
  1539. store[k] = store[j];
  1540. store[j] = cc;
  1541. ampk = ampj; // And reset amplitude ampk to the lower amp
  1542. }
  1543. }
  1544. }
  1545. // Randomise frqs in lower amplitude chans
  1546. for(k = 0; k < dz->iparam[DISC_RATIO]; k++) {
  1547. cc = store[k];
  1548. frq = (double)cc * dz->chwidth;
  1549. randval = ((drand48() * 2.0) - 1.0)/2.0; // Range -1/2 to +1/2
  1550. randval *= dz->param[DISC_RAND]; // Range -DISC_RAND/2 to +DISC_RAND/2
  1551. chanfrq = frq + randval;
  1552. if(chanfrq < PITCHZERO || chanfrq >= dz->nyquist) // In lowest channel, avoid v. low or -ve frqs (chan centre is at zero)
  1553. chanfrq = frq - randval; // In highest channel, avoid frq beyond nyquist ((chan centre at nyquist)
  1554. vc = cc * 2;
  1555. fbuf[FREQ] = (float)chanfrq; // FREQ = vc+1
  1556. }
  1557. return FINISHED;
  1558. }