timeseries.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /*
  2. * Copyright (c) 1983-2023 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. // defined in science.h
  22. // #define
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <structures.h>
  26. #include <tkglobals.h>
  27. #include <pnames.h>
  28. #include <filetype.h>
  29. #include <processno.h>
  30. #include <modeno.h>
  31. #include <logic.h>
  32. #include <globcon.h>
  33. #include <cdpmain.h>
  34. #include <math.h>
  35. #include <mixxcon.h>
  36. #include <osbind.h>
  37. #include <science.h>
  38. #include <ctype.h>
  39. #include <sfsys.h>
  40. #include <string.h>
  41. #include <srates.h>
  42. #ifdef unix
  43. #define round(x) lround((x))
  44. #endif
  45. #ifndef HUGE
  46. #define HUGE 3.40282347e+38F
  47. #endif
  48. char errstr[2400];
  49. int anal_infiles = 0;
  50. int sloom = 0;
  51. int sloombatch = 0;
  52. const char* cdp_version = "6.1.0";
  53. //CDP LIB REPLACEMENTS
  54. static int check_timeseries_param_validity_and_consistency(dataptr dz);
  55. static int setup_timeseries_application(dataptr dz);
  56. static int setup_timeseries_param_ranges_and_defaults(dataptr dz);
  57. static void init_specsynth(dataptr dz);
  58. static int allocate_timeseries_buffer(dataptr dz);
  59. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  60. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  61. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  62. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  63. static int establish_application(dataptr dz);
  64. static int initialise_vflags(dataptr dz);
  65. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  66. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  67. static int mark_parameter_types(dataptr dz,aplptr ap);
  68. static int assign_file_data_storage(int infilecnt,dataptr dz);
  69. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  70. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  71. static int count_and_test_for_infiles(int argc,char *argv[],dataptr dz);
  72. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  73. static int redefine_textfile_types2(infileptr infile_info,dataptr dz);
  74. static int handle_the_special_data(int *cmdlinecnt,char ***cmdline,dataptr dz);
  75. static int open_the_outfile(dataptr dz);
  76. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  77. //static void insert_harmonics(int **peaked,int *peakcnt,double harmamp,float fundamental,double pkwidth,dataptr dz);
  78. //static void randomiseamps(int *perm,int *peaked,dataptr dz);
  79. //static void rndintperm(int *perm,int cnt);
  80. //static void spread_peaks(int *peaked,double spreaddnratio,double spreadupratio,dataptr dz);
  81. static int get_float_with_e_from_within_string(char **str,double *val);
  82. static int spline(double *nums,double *secondderiv,double *x,double *y,dataptr dz);
  83. static int splint(double pointer,double *thisval,int *hi,int arraycnt,double *secondderiv, double *x, double *y);
  84. static int ts_oscil(dataptr dz);
  85. static void find_mean(double *mean,double *top,double *bottom,dataptr dz);
  86. static void scale_data_in_one_to_minusone_range(double mean,double top,double bottom,dataptr dz);
  87. static int ts_trace(dataptr dz);
  88. static double calcsample (double thisval,int sintabsize,double incrementor,dataptr dz);
  89. static int create_sintab (dataptr dz);
  90. /**************************************** MAIN *********************************************/
  91. int main(int argc,char *argv[])
  92. {
  93. int exit_status;
  94. dataptr dz = NULL;
  95. char **cmdline;
  96. int cmdlinecnt;
  97. // aplptr ap;
  98. int is_launched = FALSE;
  99. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  100. fprintf(stdout,"%s\n",cdp_version);
  101. fflush(stdout);
  102. return 0;
  103. }
  104. /* CHECK FOR SOUNDLOOM */
  105. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  106. sloom = 0;
  107. sloombatch = 1;
  108. }
  109. if(sflinit("cdp")){
  110. sfperror("cdp: initialisation\n");
  111. return(FAILED);
  112. }
  113. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  114. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  115. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  116. return(FAILED);
  117. }
  118. if(!sloom) {
  119. if(argc == 1) {
  120. usage1();
  121. return(FAILED);
  122. } else if(argc == 2) {
  123. usage2(argv[1]);
  124. return(FAILED);
  125. }
  126. }
  127. if(!sloom) {
  128. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  129. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  130. return(FAILED);
  131. }
  132. cmdline = argv;
  133. cmdlinecnt = argc;
  134. dz->maxmode = 0;
  135. if((get_the_process_no(argv[0],dz))<0)
  136. return(FAILED);
  137. cmdline++;
  138. cmdlinecnt--;
  139. // setup_particular_application =
  140. if((exit_status = setup_timeseries_application(dz))<0) {
  141. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  142. return(FAILED);
  143. }
  144. if((exit_status = count_and_test_for_infiles(cmdlinecnt,cmdline,dz))<0) {
  145. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  146. return(FAILED);
  147. }
  148. } else {
  149. //parse_TK_data() =
  150. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  151. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  152. return(exit_status);
  153. }
  154. }
  155. // ap = dz->application;
  156. // parse_infile_and_hone_type() =
  157. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  158. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  159. return(FAILED);
  160. }
  161. // setup_param_ranges_and_defaults() =
  162. init_specsynth(dz);
  163. if((exit_status = setup_timeseries_param_ranges_and_defaults(dz))<0) {
  164. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  165. return(FAILED);
  166. }
  167. // handle_infile() =
  168. // open_first_infile CDP LIB
  169. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  170. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  171. return(FAILED);
  172. }
  173. cmdlinecnt--;
  174. cmdline++;
  175. // handle_outfile() =
  176. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  177. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  178. return(FAILED);
  179. }
  180. if(dz->process == TS_TRACE) {
  181. if((exit_status = handle_the_special_data(&cmdlinecnt,&cmdline,dz))<0) {
  182. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  183. return(FAILED);
  184. }
  185. }
  186. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  187. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  188. return(FAILED);
  189. }
  190. // check_param_validity_and_consistency....
  191. if((exit_status = check_timeseries_param_validity_and_consistency(dz))<0) {
  192. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  193. return(FAILED);
  194. }
  195. is_launched = TRUE;
  196. if((exit_status = open_the_outfile(dz))<0) {
  197. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  198. return(FAILED);
  199. }
  200. if((exit_status = allocate_timeseries_buffer(dz))<0) { // CDP LIB
  201. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  202. return(FAILED);
  203. }
  204. switch(dz->process) {
  205. case(TS_OSCIL):
  206. if((exit_status = ts_oscil(dz))<0) {
  207. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  208. return(FAILED);
  209. }
  210. break;
  211. case(TS_TRACE):
  212. if((exit_status = ts_trace(dz))<0) {
  213. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  214. return(FAILED);
  215. }
  216. break;
  217. }
  218. if((exit_status = complete_output(dz))<0) { // CDP LIB
  219. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  220. return(FAILED);
  221. }
  222. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  223. free(dz);
  224. return(SUCCEEDED);
  225. }
  226. /**********************************************
  227. REPLACED CDP LIB FUNCTIONS
  228. **********************************************/
  229. /****************************** SET_PARAM_DATA *********************************/
  230. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  231. {
  232. ap->special_data = (char)special_data;
  233. ap->param_cnt = (char)paramcnt;
  234. ap->max_param_cnt = (char)maxparamcnt;
  235. if(ap->max_param_cnt>0) {
  236. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  237. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  238. // if(!sloom)
  239. // fprintf(stderr,errstr);
  240. return(MEMORY_ERROR);
  241. }
  242. strcpy(ap->param_list,paramlist);
  243. }
  244. return(FINISHED);
  245. }
  246. /****************************** SET_VFLGS *********************************/
  247. int set_vflgs
  248. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  249. {
  250. ap->option_cnt = (char) optcnt; /*RWD added cast */
  251. if(optcnt) {
  252. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  253. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  254. // if(!sloom)
  255. // fprintf(stderr,errstr);
  256. return(MEMORY_ERROR);
  257. }
  258. strcpy(ap->option_list,optlist);
  259. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  260. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  261. // if(!sloom)
  262. // fprintf(stderr,errstr);
  263. return(MEMORY_ERROR);
  264. }
  265. strcpy(ap->option_flags,optflags);
  266. }
  267. ap->vflag_cnt = (char) vflagcnt;
  268. ap->variant_param_cnt = (char) vparamcnt;
  269. if(vflagcnt) {
  270. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  271. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  272. // if(!sloom)
  273. // fprintf(stderr,errstr);
  274. return(MEMORY_ERROR);
  275. }
  276. strcpy(ap->variant_list,varlist);
  277. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  278. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  279. // if(!sloom)
  280. // fprintf(stderr,errstr);
  281. return(MEMORY_ERROR);
  282. }
  283. strcpy(ap->variant_flags,varflags);
  284. }
  285. return(FINISHED);
  286. }
  287. /***************************** APPLICATION_INIT **************************/
  288. int application_init(dataptr dz)
  289. {
  290. int exit_status;
  291. int storage_cnt;
  292. int tipc, brkcnt;
  293. aplptr ap = dz->application;
  294. if(ap->vflag_cnt>0)
  295. initialise_vflags(dz);
  296. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  297. ap->total_input_param_cnt = (char)tipc;
  298. if(tipc>0) {
  299. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  300. return(exit_status);
  301. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  302. return(exit_status);
  303. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  304. return(exit_status);
  305. }
  306. brkcnt = tipc;
  307. if(dz->input_data_type==UNRANGED_BRKFILE_ONLY) {
  308. dz->extrabrkno = brkcnt;
  309. brkcnt++; /* create brktable poniter for param0, and use point to and read (parray) input data during process */
  310. } else
  311. brkcnt++;
  312. if(brkcnt>0) {
  313. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  314. return(exit_status);
  315. }
  316. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  317. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  318. return(exit_status);
  319. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  320. return(exit_status);
  321. }
  322. if((exit_status = mark_parameter_types(dz,ap))<0)
  323. return(exit_status);
  324. if((exit_status = establish_bufptrs_and_extra_buffers(dz))<0)
  325. return(exit_status);
  326. if((exit_status = setup_internal_arrays_and_array_pointers(dz))<0)
  327. return(exit_status);
  328. return(FINISHED);
  329. }
  330. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  331. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  332. {
  333. int n;
  334. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  335. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  336. // if(!sloom)
  337. // fprintf(stderr,errstr);
  338. return(MEMORY_ERROR);
  339. }
  340. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  341. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  342. // if(!sloom)
  343. // fprintf(stderr,errstr);
  344. return(MEMORY_ERROR);
  345. }
  346. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  347. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  348. // if(!sloom)
  349. // fprintf(stderr,errstr);
  350. return(MEMORY_ERROR);
  351. }
  352. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  353. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  354. // if(!sloom)
  355. // fprintf(stderr,errstr);
  356. return(MEMORY_ERROR);
  357. }
  358. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  359. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  360. // if(!sloom)
  361. // fprintf(stderr,errstr);
  362. return(MEMORY_ERROR);
  363. }
  364. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  365. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  366. // if(!sloom)
  367. // fprintf(stderr,errstr);
  368. return(MEMORY_ERROR);
  369. }
  370. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  371. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  372. // if(!sloom)
  373. // fprintf(stderr,errstr);
  374. return(MEMORY_ERROR);
  375. }
  376. for(n=0;n<brkcnt;n++) {
  377. dz->brk[n] = NULL;
  378. dz->brkptr[n] = NULL;
  379. dz->brkinit[n] = 0;
  380. dz->brksize[n] = 0;
  381. }
  382. return(FINISHED);
  383. }
  384. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  385. /* RWD mallo changed to calloc; helps debug verison run as release! */
  386. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  387. {
  388. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  389. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  390. // if(!sloom)
  391. // fprintf(stderr,errstr);
  392. return(MEMORY_ERROR);
  393. }
  394. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  395. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  396. // if(!sloom)
  397. // fprintf(stderr,errstr);
  398. return(MEMORY_ERROR);
  399. }
  400. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  401. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  402. // if(!sloom)
  403. // fprintf(stderr,errstr);
  404. return(MEMORY_ERROR);
  405. }
  406. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  407. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  408. // if(!sloom)
  409. // fprintf(stderr,errstr);
  410. return(MEMORY_ERROR);
  411. }
  412. return(FINISHED);
  413. }
  414. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  415. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  416. {
  417. int n;
  418. for(n=0;n<storage_cnt;n++) {
  419. dz->is_int[n] = (char)0;
  420. dz->no_brk[n] = (char)0;
  421. }
  422. return(FINISHED);
  423. }
  424. /***************************** MARK_PARAMETER_TYPES **************************/
  425. int mark_parameter_types(dataptr dz,aplptr ap)
  426. {
  427. int n, m; /* PARAMS */
  428. for(n=0;n<ap->max_param_cnt;n++) {
  429. switch(ap->param_list[n]) {
  430. case('0'): break; /* dz->is_active[n] = 0 is default */
  431. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  432. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  433. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  434. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  435. default:
  436. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  437. // if(!sloom)
  438. // fprintf(stderr,errstr);
  439. return(PROGRAM_ERROR);
  440. }
  441. } /* OPTIONS */
  442. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  443. switch(ap->option_list[n]) {
  444. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  445. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  446. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  447. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  448. default:
  449. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  450. // if(!sloom)
  451. // fprintf(stderr,errstr);
  452. return(PROGRAM_ERROR);
  453. }
  454. } /* VARIANTS */
  455. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  456. switch(ap->variant_list[n]) {
  457. case('0'): break;
  458. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  459. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  460. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  461. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  462. default:
  463. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  464. // if(!sloom)
  465. // fprintf(stderr,errstr);
  466. return(PROGRAM_ERROR);
  467. }
  468. } /* INTERNAL */
  469. for(n=0,
  470. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  471. switch(ap->internal_param_list[n]) {
  472. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  473. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  474. case('d'): dz->no_brk[m] = (char)1; break;
  475. default:
  476. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  477. // if(!sloom)
  478. // fprintf(stderr,errstr);
  479. return(PROGRAM_ERROR);
  480. }
  481. }
  482. return(FINISHED);
  483. }
  484. /************************ HANDLE_THE_OUTFILE *********************/
  485. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  486. {
  487. char *filename = (*cmdline)[0];
  488. if(filename[0]=='-' && filename[1]=='f') {
  489. dz->floatsam_output = 1;
  490. dz->true_outfile_stype = SAMP_FLOAT;
  491. filename+= 2;
  492. }
  493. if(!sloom) {
  494. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  495. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  496. // if(!sloom)
  497. // fprintf(stderr,errstr);
  498. return(DATA_ERROR);
  499. }
  500. }
  501. strcpy(dz->outfilename,filename);
  502. (*cmdline)++;
  503. (*cmdlinecnt)--;
  504. return(FINISHED);
  505. }
  506. /************************ OPEN_THE_OUTFILE *********************/
  507. int open_the_outfile(dataptr dz)
  508. {
  509. int exit_status;
  510. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  511. return(exit_status);
  512. return(FINISHED);
  513. }
  514. /***************************** ESTABLISH_APPLICATION **************************/
  515. int establish_application(dataptr dz)
  516. {
  517. aplptr ap;
  518. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  519. sprintf(errstr,"establish_application()\n");
  520. // if(!sloom)
  521. // fprintf(stderr,errstr);
  522. return(MEMORY_ERROR);
  523. }
  524. ap = dz->application;
  525. memset((char *)ap,0,sizeof(struct applic));
  526. return(FINISHED);
  527. }
  528. /************************* INITIALISE_VFLAGS *************************/
  529. int initialise_vflags(dataptr dz)
  530. {
  531. int n;
  532. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  533. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  534. // if(!sloom)
  535. // fprintf(stderr,errstr);
  536. return(MEMORY_ERROR);
  537. }
  538. for(n=0;n<dz->application->vflag_cnt;n++)
  539. dz->vflag[n] = FALSE;
  540. return FINISHED;
  541. }
  542. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  543. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  544. {
  545. int n;
  546. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  547. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  548. // if(!sloom)
  549. // fprintf(stderr,errstr);
  550. return(MEMORY_ERROR);
  551. }
  552. for(n=0;n<tipc;n++)
  553. ap->default_val[n] = 0.0;
  554. return(FINISHED);
  555. }
  556. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  557. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  558. {
  559. int n;
  560. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  561. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  562. // if(!sloom)
  563. // fprintf(stderr,errstr);
  564. return(MEMORY_ERROR);
  565. }
  566. for(n=0;n<tipc;n++)
  567. dz->is_active[n] = (char)0;
  568. return(FINISHED);
  569. }
  570. /************************* SETUP_TIMESERIES_APPLICATION *******************/
  571. int setup_timeseries_application(dataptr dz)
  572. {
  573. int exit_status;
  574. aplptr ap;
  575. if((exit_status = establish_application(dz))<0) // GLOBAL
  576. return(FAILED);
  577. ap = dz->application;
  578. dz->bufcnt = 1;
  579. // SEE parstruct FOR EXPLANATION of next 2 functions
  580. switch(dz->process) {
  581. case(TS_OSCIL):
  582. if((exit_status = set_param_data(ap,0 ,1,1,"D"))<0)
  583. return(FAILED);
  584. if((exit_status = set_vflgs(ap,"d",1,"d","cf",2,0,"00"))<0)
  585. return(FAILED);
  586. dz->input_data_type = NUMLIST_ONLY;
  587. dz->process_type = UNEQUAL_SNDFILE;
  588. dz->outfiletype = SNDFILE_OUT;
  589. break;
  590. case(TS_TRACE):
  591. if((exit_status = set_param_data(ap,TS_HARM ,3,3,"Ddd"))<0)
  592. return(FAILED);
  593. if((exit_status = set_vflgs(ap,"d",1,"d","cf",2,0,"00"))<0)
  594. return(FAILED);
  595. dz->input_data_type = NUMLIST_ONLY;
  596. dz->process_type = UNEQUAL_SNDFILE;
  597. dz->outfiletype = SNDFILE_OUT;
  598. break;
  599. }
  600. // set_legal_infile_structure -->
  601. dz->has_otherfile = FALSE;
  602. // assign_process_logic -->
  603. return application_init(dz); //GLOBAL
  604. }
  605. /************************* SETUP_TIMESERIES_PARAM_RANGES_AND_DEFAULTS *******************/
  606. int setup_timeseries_param_ranges_and_defaults(dataptr dz)
  607. {
  608. int exit_status;
  609. aplptr ap = dz->application;
  610. // set_param_ranges()
  611. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  612. // NB total_input_param_cnt is > 0 !!!
  613. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  614. return(FAILED);
  615. // get_param_ranges()
  616. switch(dz->process) {
  617. case(TS_OSCIL):
  618. ap->lo[TS_TSTRETCH] = 0;
  619. ap->hi[TS_TSTRETCH] = TS_MAXOCT;
  620. ap->default_val[TS_TSTRETCH] = 0;
  621. ap->lo[TS_OMAXDUR] = 1;
  622. ap->hi[TS_OMAXDUR] = 60;
  623. ap->default_val[TS_OMAXDUR] = 10;
  624. break;
  625. case(TS_TRACE):
  626. ap->lo[TS_TSTRETCH] = 1;
  627. ap->hi[TS_TSTRETCH] = TS_MAXTSTRETCH;
  628. ap->default_val[TS_TSTRETCH] = 0;
  629. ap->lo[TS_FRQ] = TS_MINFRQ;
  630. ap->hi[TS_FRQ] = dz->nyquist/2.0;
  631. ap->default_val[TS_FRQ] = 440.0;
  632. ap->lo[TS_HALFRANGE] = 0;
  633. ap->hi[TS_HALFRANGE] = TS_MAXRANGE;
  634. ap->default_val[TS_HALFRANGE] = TS_DFLTRANGE;
  635. ap->lo[TS_TMAXDUR] = 0;
  636. ap->hi[TS_TMAXDUR] = 600;
  637. ap->default_val[TS_TMAXDUR] = 0;
  638. break;
  639. }
  640. dz->maxmode = 0;
  641. if(!sloom)
  642. put_default_vals_in_all_params(dz);
  643. return(FINISHED);
  644. }
  645. /********************************* PARSE_SLOOM_DATA *********************************/
  646. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  647. {
  648. int exit_status;
  649. int cnt = 1, infilecnt;
  650. int filesize, insams, inbrksize;
  651. double dummy;
  652. int true_cnt = 0;
  653. // aplptr ap;
  654. while(cnt<=PRE_CMDLINE_DATACNT) {
  655. if(cnt > argc) {
  656. sprintf(errstr,"Insufficient data sent from TK\n");
  657. // if(!sloom)
  658. // fprintf(stderr,errstr);
  659. return(DATA_ERROR);
  660. }
  661. switch(cnt) {
  662. case(1):
  663. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  664. sprintf(errstr,"Cannot read process no. sent from TK\n");
  665. // if(!sloom)
  666. // fprintf(stderr,errstr);
  667. return(DATA_ERROR);
  668. }
  669. break;
  670. case(2):
  671. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  672. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  673. // if(!sloom)
  674. // fprintf(stderr,errstr);
  675. return(DATA_ERROR);
  676. }
  677. if(dz->mode > 0)
  678. dz->mode--;
  679. //setup_particular_application() =
  680. if((exit_status = setup_timeseries_application(dz))<0)
  681. return(exit_status);
  682. // ap = dz->application;
  683. break;
  684. case(3):
  685. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  686. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  687. // if(!sloom)
  688. // fprintf(stderr,errstr);
  689. return(DATA_ERROR);
  690. }
  691. if(infilecnt < 1) {
  692. true_cnt = cnt + 1;
  693. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  694. }
  695. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  696. return(exit_status);
  697. break;
  698. case(INPUT_FILETYPE+4):
  699. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  700. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  701. // if(!sloom)
  702. // fprintf(stderr,errstr);
  703. return(DATA_ERROR);
  704. }
  705. break;
  706. case(INPUT_FILESIZE+4):
  707. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  708. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  709. // if(!sloom)
  710. // fprintf(stderr,errstr);
  711. return(DATA_ERROR);
  712. }
  713. dz->insams[0] = filesize;
  714. break;
  715. case(INPUT_INSAMS+4):
  716. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  717. sprintf(errstr,"Cannot read insams sent from TK\n");
  718. // if(!sloom)
  719. // fprintf(stderr,errstr);
  720. return(DATA_ERROR);
  721. }
  722. dz->insams[0] = insams;
  723. break;
  724. case(INPUT_SRATE+4):
  725. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  726. sprintf(errstr,"Cannot read srate sent from TK\n");
  727. // if(!sloom)
  728. // fprintf(stderr,errstr);
  729. return(DATA_ERROR);
  730. }
  731. break;
  732. case(INPUT_CHANNELS+4):
  733. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  734. sprintf(errstr,"Cannot read channels sent from TK\n");
  735. // if(!sloom)
  736. // fprintf(stderr,errstr);
  737. return(DATA_ERROR);
  738. }
  739. break;
  740. case(INPUT_STYPE+4):
  741. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  742. sprintf(errstr,"Cannot read stype sent from TK\n");
  743. // if(!sloom)
  744. // fprintf(stderr,errstr);
  745. return(DATA_ERROR);
  746. }
  747. break;
  748. case(INPUT_ORIGSTYPE+4):
  749. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  750. sprintf(errstr,"Cannot read origstype sent from TK\n");
  751. // if(!sloom)
  752. // fprintf(stderr,errstr);
  753. return(DATA_ERROR);
  754. }
  755. break;
  756. case(INPUT_ORIGRATE+4):
  757. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  758. sprintf(errstr,"Cannot read origrate sent from TK\n");
  759. // if(!sloom)
  760. // fprintf(stderr,errstr);
  761. return(DATA_ERROR);
  762. }
  763. break;
  764. case(INPUT_MLEN+4):
  765. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  766. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  767. // if(!sloom)
  768. // fprintf(stderr,errstr);
  769. return(DATA_ERROR);
  770. }
  771. break;
  772. case(INPUT_DFAC+4):
  773. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  774. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  775. // if(!sloom)
  776. // fprintf(stderr,errstr);
  777. return(DATA_ERROR);
  778. }
  779. break;
  780. case(INPUT_ORIGCHANS+4):
  781. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  782. sprintf(errstr,"Cannot read origchans sent from TK\n");
  783. // if(!sloom)
  784. // fprintf(stderr,errstr);
  785. return(DATA_ERROR);
  786. }
  787. break;
  788. case(INPUT_SPECENVCNT+4):
  789. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  790. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  791. // if(!sloom)
  792. // fprintf(stderr,errstr);
  793. return(DATA_ERROR);
  794. }
  795. dz->specenvcnt = dz->infile->specenvcnt;
  796. break;
  797. case(INPUT_WANTED+4):
  798. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  799. sprintf(errstr,"Cannot read wanted sent from TK\n");
  800. // if(!sloom)
  801. // fprintf(stderr,errstr);
  802. return(DATA_ERROR);
  803. }
  804. break;
  805. case(INPUT_WLENGTH+4):
  806. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  807. sprintf(errstr,"Cannot read wlength sent from TK\n");
  808. // if(!sloom)
  809. // fprintf(stderr,errstr);
  810. return(DATA_ERROR);
  811. }
  812. break;
  813. case(INPUT_OUT_CHANS+4):
  814. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  815. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  816. // if(!sloom)
  817. // fprintf(stderr,errstr);
  818. return(DATA_ERROR);
  819. }
  820. break;
  821. /* RWD these chanegs to samps - tk will have to deal with that! */
  822. case(INPUT_DESCRIPTOR_BYTES+4):
  823. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  824. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  825. // if(!sloom)
  826. // fprintf(stderr,errstr);
  827. return(DATA_ERROR);
  828. }
  829. break;
  830. case(INPUT_IS_TRANSPOS+4):
  831. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  832. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  833. // if(!sloom)
  834. // fprintf(stderr,errstr);
  835. return(DATA_ERROR);
  836. }
  837. break;
  838. case(INPUT_COULD_BE_TRANSPOS+4):
  839. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  840. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  841. // if(!sloom)
  842. // fprintf(stderr,errstr);
  843. return(DATA_ERROR);
  844. }
  845. break;
  846. case(INPUT_COULD_BE_PITCH+4):
  847. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  848. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  849. // if(!sloom)
  850. // fprintf(stderr,errstr);
  851. return(DATA_ERROR);
  852. }
  853. break;
  854. case(INPUT_DIFFERENT_SRATES+4):
  855. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  856. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  857. // if(!sloom)
  858. // fprintf(stderr,errstr);
  859. return(DATA_ERROR);
  860. }
  861. break;
  862. case(INPUT_DUPLICATE_SNDS+4):
  863. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  864. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  865. // if(!sloom)
  866. // fprintf(stderr,errstr);
  867. return(DATA_ERROR);
  868. }
  869. break;
  870. case(INPUT_BRKSIZE+4):
  871. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  872. sprintf(errstr,"Cannot read brksize sent from TK\n");
  873. // if(!sloom)
  874. // fprintf(stderr,errstr);
  875. return(DATA_ERROR);
  876. }
  877. if(inbrksize > 0) {
  878. switch(dz->input_data_type) {
  879. case(WORDLIST_ONLY):
  880. case(NUMLIST_ONLY):
  881. break;
  882. case(PITCH_AND_PITCH):
  883. case(PITCH_AND_TRANSPOS):
  884. case(TRANSPOS_AND_TRANSPOS):
  885. dz->tempsize = inbrksize;
  886. break;
  887. case(BRKFILES_ONLY):
  888. case(UNRANGED_BRKFILE_ONLY):
  889. case(DB_BRKFILES_ONLY):
  890. case(ALL_FILES):
  891. case(ANY_NUMBER_OF_ANY_FILES):
  892. if(dz->extrabrkno < 0) {
  893. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  894. // if(!sloom)
  895. // fprintf(stderr,errstr);
  896. return(DATA_ERROR);
  897. }
  898. if(dz->brksize == NULL) {
  899. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  900. // if(!sloom)
  901. // fprintf(stderr,errstr);
  902. return(PROGRAM_ERROR);
  903. }
  904. dz->brksize[dz->extrabrkno] = inbrksize;
  905. break;
  906. default:
  907. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",dz->input_data_type);
  908. // if(!sloom)
  909. // fprintf(stderr,errstr);
  910. return(PROGRAM_ERROR);
  911. }
  912. break;
  913. }
  914. break;
  915. case(INPUT_NUMSIZE+4):
  916. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  917. sprintf(errstr,"Cannot read numsize sent from TK\n");
  918. // if(!sloom)
  919. // fprintf(stderr,errstr);
  920. return(DATA_ERROR);
  921. }
  922. break;
  923. case(INPUT_LINECNT+4):
  924. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  925. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  926. // if(!sloom)
  927. // fprintf(stderr,errstr);
  928. return(DATA_ERROR);
  929. }
  930. break;
  931. case(INPUT_ALL_WORDS+4):
  932. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  933. sprintf(errstr,"Cannot read all_words sent from TK\n");
  934. // if(!sloom)
  935. // fprintf(stderr,errstr);
  936. return(DATA_ERROR);
  937. }
  938. break;
  939. case(INPUT_ARATE+4):
  940. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  941. sprintf(errstr,"Cannot read arate sent from TK\n");
  942. // if(!sloom)
  943. // fprintf(stderr,errstr);
  944. return(DATA_ERROR);
  945. }
  946. break;
  947. case(INPUT_FRAMETIME+4):
  948. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  949. sprintf(errstr,"Cannot read frametime sent from TK\n");
  950. // if(!sloom)
  951. // fprintf(stderr,errstr);
  952. return(DATA_ERROR);
  953. }
  954. dz->frametime = (float)dummy;
  955. break;
  956. case(INPUT_WINDOW_SIZE+4):
  957. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  958. sprintf(errstr,"Cannot read window_size sent from TK\n");
  959. // if(!sloom)
  960. // fprintf(stderr,errstr);
  961. return(DATA_ERROR);
  962. }
  963. break;
  964. case(INPUT_NYQUIST+4):
  965. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  966. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  967. // if(!sloom)
  968. // fprintf(stderr,errstr);
  969. return(DATA_ERROR);
  970. }
  971. break;
  972. case(INPUT_DURATION+4):
  973. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  974. sprintf(errstr,"Cannot read duration sent from TK\n");
  975. // if(!sloom)
  976. // fprintf(stderr,errstr);
  977. return(DATA_ERROR);
  978. }
  979. break;
  980. case(INPUT_MINBRK+4):
  981. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  982. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  983. // if(!sloom)
  984. // fprintf(stderr,errstr);
  985. return(DATA_ERROR);
  986. }
  987. break;
  988. case(INPUT_MAXBRK+4):
  989. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  990. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  991. // if(!sloom)
  992. // fprintf(stderr,errstr);
  993. return(DATA_ERROR);
  994. }
  995. break;
  996. case(INPUT_MINNUM+4):
  997. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  998. sprintf(errstr,"Cannot read minnum sent from TK\n");
  999. // if(!sloom)
  1000. // fprintf(stderr,errstr);
  1001. return(DATA_ERROR);
  1002. }
  1003. break;
  1004. case(INPUT_MAXNUM+4):
  1005. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  1006. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  1007. // if(!sloom)
  1008. // fprintf(stderr,errstr);
  1009. return(DATA_ERROR);
  1010. }
  1011. break;
  1012. default:
  1013. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  1014. // if(!sloom)
  1015. // fprintf(stderr,errstr);
  1016. return(PROGRAM_ERROR);
  1017. }
  1018. cnt++;
  1019. }
  1020. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  1021. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  1022. // if(!sloom)
  1023. // fprintf(stderr,errstr);
  1024. return(DATA_ERROR);
  1025. }
  1026. if(true_cnt)
  1027. cnt = true_cnt;
  1028. *cmdlinecnt = 0;
  1029. while(cnt < argc) {
  1030. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  1031. return(exit_status);
  1032. cnt++;
  1033. }
  1034. return(FINISHED);
  1035. }
  1036. /********************************* GET_TK_CMDLINE_WORD *********************************/
  1037. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  1038. {
  1039. if(*cmdlinecnt==0) {
  1040. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  1041. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1042. // if(!sloom)
  1043. // fprintf(stderr,errstr);
  1044. return(MEMORY_ERROR);
  1045. }
  1046. } else {
  1047. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  1048. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1049. // if(!sloom)
  1050. // fprintf(stderr,errstr);
  1051. return(MEMORY_ERROR);
  1052. }
  1053. }
  1054. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  1055. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  1056. // if(!sloom)
  1057. // fprintf(stderr,errstr);
  1058. return(MEMORY_ERROR);
  1059. }
  1060. strcpy((*cmdline)[*cmdlinecnt],q);
  1061. (*cmdlinecnt)++;
  1062. return(FINISHED);
  1063. }
  1064. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  1065. int assign_file_data_storage(int infilecnt,dataptr dz)
  1066. {
  1067. int exit_status;
  1068. int no_sndfile_system_files = FALSE;
  1069. dz->infilecnt = infilecnt;
  1070. if((exit_status = allocate_filespace(dz))<0)
  1071. return(exit_status);
  1072. if(no_sndfile_system_files)
  1073. dz->infilecnt = 0;
  1074. return(FINISHED);
  1075. }
  1076. /************************* SETUP_INTERNAL_ARRAYS_AND_ARRAY_POINTERS *******************/
  1077. int setup_internal_arrays_and_array_pointers(dataptr dz)
  1078. {
  1079. int n;
  1080. dz->array_cnt = 3;
  1081. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  1082. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  1083. return(MEMORY_ERROR);
  1084. }
  1085. for(n=0;n<dz->array_cnt;n++)
  1086. dz->parray[n] = NULL;
  1087. return(FINISHED);
  1088. }
  1089. /************************* redundant functions: to ensure libs compile OK *******************/
  1090. int assign_process_logic(dataptr dz)
  1091. {
  1092. return(FINISHED);
  1093. }
  1094. void set_legal_infile_structure(dataptr dz)
  1095. {}
  1096. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  1097. {
  1098. return(FINISHED);
  1099. }
  1100. int establish_bufptrs_and_extra_buffers(dataptr dz)
  1101. {
  1102. dz->extra_bufcnt = 0;
  1103. dz->bptrcnt = 1;
  1104. dz->bufcnt = 1;
  1105. return establish_groucho_bufptrs_and_extra_buffers(dz);
  1106. }
  1107. int read_special_data(char *str,dataptr dz)
  1108. {
  1109. return(FINISHED);
  1110. }
  1111. int inner_loop
  1112. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  1113. {
  1114. return(FINISHED);
  1115. }
  1116. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1117. {
  1118. return(FINISHED);
  1119. }
  1120. /******************************** USAGE1 ********************************/
  1121. int usage1(void)
  1122. {
  1123. fprintf(stderr,
  1124. "\nCREATE SOUND FROM TIME-SERIES TEXT DATA\n\n"
  1125. "USAGE: ts NAME parameters\n"
  1126. "\n"
  1127. "where NAME can be any one of\n"
  1128. "\n"
  1129. "oscil trace\n\n"
  1130. "Type 'ts oscil' for more info on ts oscil..ETC.\n");
  1131. return(USAGE_ONLY);
  1132. }
  1133. /**************************** CHECK_TIMESERIES_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  1134. int check_timeseries_param_validity_and_consistency(dataptr dz)
  1135. {
  1136. int exit_status;
  1137. double centrefrq, maxtranspos, maxfrq, maxharm, brkmax;
  1138. int maxpos;
  1139. if(dz->process == TS_TRACE) {
  1140. if(dz->brksize[TS_FRQ] > 0) {
  1141. if ((exit_status = get_maxvalue_in_brktable(&brkmax,TS_FRQ,dz)) < 0)
  1142. return(exit_status);
  1143. centrefrq = brkmax;
  1144. } else
  1145. centrefrq = dz->param[TS_FRQ];
  1146. if(dz->brksize[TS_HALFRANGE] > 0) {
  1147. if ((exit_status = get_maxvalue_in_brktable(&brkmax,TS_HALFRANGE,dz)) < 0)
  1148. return(exit_status);
  1149. maxtranspos = pow(2.0,(brkmax/12.0));
  1150. } else
  1151. maxtranspos = pow(2.0,(dz->param[TS_HALFRANGE]/12.0));
  1152. maxfrq = centrefrq * maxtranspos;
  1153. maxharm = dz->scalefact;
  1154. maxfrq *= maxharm;
  1155. if(maxfrq >= dz->nyquist) {
  1156. maxpos = (int)floor(dz->nyquist/(centrefrq * maxtranspos));
  1157. if(maxpos < 1)
  1158. sprintf(errstr,"All output too high for (max) frq and range.");
  1159. else
  1160. sprintf(errstr,"Max harmonic (%d) too high for (max) frq and range.\nMax possible harmonic = %d\n",(int) round(maxharm),maxpos); //RWD added cast
  1161. return(DATA_ERROR);
  1162. }
  1163. if(dz->param[TS_TMAXDUR] <= 0.0) {
  1164. if(dz->vflag[1]) {
  1165. sprintf(errstr,"Loop-forcing flag (-f) set, but no max duration given.");
  1166. return(DATA_ERROR);
  1167. }
  1168. dz->wanted = 0;
  1169. }
  1170. else if(dz->param[TS_TMAXDUR] < 1.0) {
  1171. sprintf(errstr,"Maximum duration must be >= 1 second\n");
  1172. return(DATA_ERROR);
  1173. } else
  1174. dz->wanted = (int)round(dz->param[TS_TMAXDUR] * SPEKSR);
  1175. } else {
  1176. if(!dz->brksize[TS_TSTRETCH])
  1177. dz->param[TS_TSTRETCH] = pow(2.0,dz->param[TS_TSTRETCH]); /* convert octaves to frq(time) ratio, for "oscil" */
  1178. if(dz->param[TS_OMAXDUR] <= 0.0)
  1179. dz->wanted = 0;
  1180. else if(dz->param[TS_OMAXDUR] < 1.0) {
  1181. sprintf(errstr,"Maximum duration must be >= 1 second\n");
  1182. return(DATA_ERROR);
  1183. } else
  1184. dz->wanted = (int)round(dz->param[TS_OMAXDUR] * SPEKSR);
  1185. }
  1186. return(FINISHED);
  1187. }
  1188. /********************************************************************************************/
  1189. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1190. {
  1191. if(!strcmp(prog_identifier_from_cmdline,"oscil")) dz->process = TS_OSCIL;
  1192. else if(!strcmp(prog_identifier_from_cmdline,"trace")) dz->process = TS_TRACE;
  1193. else {
  1194. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  1195. // if(!sloom)
  1196. // fprintf(stderr,errstr);
  1197. return(USAGE_ONLY);
  1198. }
  1199. return(FINISHED);
  1200. }
  1201. /******************************** USAGE2 ********************************/
  1202. int usage2(char *str)
  1203. {
  1204. if(!strcmp(str,"oscil")) {
  1205. fprintf(stderr,
  1206. "USAGE: ts oscil indata outsnd downsample [-dmaxdur] [-c] [-f]\n"
  1207. "\n"
  1208. "Treat data in time-series format (list of numbers) as a soundwave plot.\n"
  1209. "\n"
  1210. "INDATA text data as a list of numerical values.\n"
  1211. "OUTSOUND sound output file.\n"
  1212. "DOWNSAMPLE downward transposition of data in octaves (0 - %d)\n"
  1213. " (= timestretch as a power of two)\n"
  1214. " Can vary over time (time values are times in output sound).\n"
  1215. "MAXDUR max duration of output (range 1 - 600).\n"
  1216. "-c Interpolate downsampled data using cubic spline (default linear).\n"
  1217. "-f Force duration to 'maxdur' (if ness) by looping input data.\n"
  1218. " (invalid flag if no 'maxdur' specified).\n"
  1219. "\n",TS_MAXOCT);
  1220. } else if(!strcmp(str,"trace")) {
  1221. fprintf(stderr,
  1222. "USAGE: ts trace indata outsnd harmdata tstr frq hrange [-dmaxdur] [-c] [-f]\n"
  1223. "\n"
  1224. "Treat data in time-series format (list of numbers) as the pitch-trace\n"
  1225. "of some defined waveform.\n"
  1226. "\n"
  1227. "INDATA text data as a list of numerical values.\n"
  1228. "OUTSOUND sound output file.\n"
  1229. "HARMDATA textfile: list of number pairs = 'harmonic-number amplitude'\n"
  1230. " for each harmonic in waveform. \n"
  1231. " Fractional values (> 1) will generate inharmonic spectra.\n"
  1232. " '0' (zero), instead of a filename, will output a pure sinetone.\n"
  1233. "TSTR time stretch of data (range 1 - %d).\n"
  1234. " Can vary over time (time values are times in output sound).\n"
  1235. "FRQ frequency of the mean pitch of the output (range %.0lf - %d).\n"
  1236. "HRANGE pitch range upwards (or down) from mean, in semitones (range 0 - %d).\n"
  1237. "MAXDUR max duration of output (range 1 - 600).\n"
  1238. "-c Interpolate time-stretched data using cubic spline (default linear).\n"
  1239. "-f Force duration to 'maxdur' (if ness) by looping input data.\n"
  1240. " (invalid flag if no 'maxdur' specified).\n"
  1241. "\n",TS_MAXTSTRETCH,TS_MINFRQ,SPEKSR/4,TS_MAXRANGE);
  1242. } else
  1243. fprintf(stdout,"Unknown option '%s'\n",str);
  1244. return(USAGE_ONLY);
  1245. }
  1246. int usage3(char *str1,char *str2)
  1247. {
  1248. fprintf(stderr,"Insufficient parameters on command line.\n");
  1249. return(USAGE_ONLY);
  1250. }
  1251. /******************************** INIT_SPECSYNTH (redundant) ********************************/
  1252. void init_specsynth(dataptr dz)
  1253. {
  1254. dz->outfile->stype = dz->infile->stype = SAMP_SHORT;
  1255. dz->outfile->channels = dz->infile->channels = 1;
  1256. dz->outfile->srate = dz->infile->srate = SPEKSR;
  1257. dz->nyquist = (double)(SPEKSR / 2.0);
  1258. dz->needpeaks = 0;
  1259. }
  1260. /**************************** ALLOCATE_TIMESERIES_BUFFER ******************************/
  1261. int allocate_timeseries_buffer(dataptr dz)
  1262. {
  1263. int exit_status;
  1264. dz->bufcnt = 1;
  1265. exit_status = create_sndbufs(dz);
  1266. return(exit_status);
  1267. }
  1268. /************************** HANDLE_THE_SPECIAL_DATA **********************************/
  1269. int handle_the_special_data(int *cmdlinecnt,char ***cmdline,dataptr dz)
  1270. {
  1271. int cnt, is_harmno, isfile = 1, m, n;
  1272. char *filename = (*cmdline)[0];
  1273. FILE *fp = NULL;
  1274. double *p, dummy, maxharmno = -1.0;
  1275. char temp[200], *q;
  1276. if(!strcmp(filename,"0")) {
  1277. cnt = 2;
  1278. isfile = 0;
  1279. } else {
  1280. if((fp = fopen(filename,"r"))==NULL) {
  1281. sprintf(errstr, "Can't open data file %s to read data.\n",filename);
  1282. return(DATA_ERROR);
  1283. }
  1284. cnt = 0;
  1285. p = &dummy;
  1286. while(fgets(temp,200,fp)==temp) {
  1287. q = temp;
  1288. if(*q == ';') // Allow comments in file
  1289. continue;
  1290. while(get_float_with_e_from_within_string(&q,p))
  1291. cnt++;
  1292. }
  1293. if(cnt == 0) {
  1294. sprintf(errstr,"No data in data file %s\n",filename);
  1295. return(DATA_ERROR);
  1296. }
  1297. if((cnt % 2) != 0) {
  1298. sprintf(errstr,"data in file %s not paired correctly.\n",filename);
  1299. return(DATA_ERROR);
  1300. }
  1301. }
  1302. dz->itemcnt = 0;
  1303. if((dz->parray[TS_HARMONICS] = (double *)malloc(cnt * sizeof(double)))==NULL) {
  1304. sprintf(errstr,"INSUFFICIENT MEMORY for input data in file %s.\n",filename);
  1305. return(MEMORY_ERROR);
  1306. }
  1307. if(!isfile) {
  1308. dz->parray[TS_HARMONICS][0] = 1.0;
  1309. dz->parray[TS_HARMONICS][1] = 1.0;
  1310. dz->itemcnt = 1;
  1311. dz->scalefact = 1.0;
  1312. } else {
  1313. fseek(fp,0,0);
  1314. p = dz->parray[TS_HARMONICS];
  1315. is_harmno = 1;
  1316. while(fgets(temp,200,fp)==temp) {
  1317. q = temp;
  1318. if(*q == ';') // Allow comments in file
  1319. continue;
  1320. while(get_float_with_e_from_within_string(&q,p)) {
  1321. dummy = *p;
  1322. if(is_harmno) {
  1323. if(dummy < 1) {
  1324. sprintf(errstr,"Invalid harmonic number (%lf) in file %s (min = 1)\n",dummy,filename);
  1325. return(DATA_ERROR);
  1326. }
  1327. if(dummy > maxharmno)
  1328. maxharmno = dummy;
  1329. } else {
  1330. if((dummy <= 0.0) || (dummy > 1.0)) {
  1331. sprintf(errstr,"harmonic amplitude (%lf) out of range (>0-1) in file %s.\n",dummy,filename);
  1332. return(DATA_ERROR);
  1333. }
  1334. dz->itemcnt++;
  1335. }
  1336. is_harmno = !is_harmno;
  1337. p++;
  1338. }
  1339. }
  1340. if(fclose(fp)<0) {
  1341. fprintf(stdout,"WARNING: Failed to close file %s.\n",filename);
  1342. fflush(stdout);
  1343. }
  1344. dz->scalefact = maxharmno;
  1345. for(n = 0; n < dz->itemcnt-1; n++) {
  1346. m = n+1;
  1347. while(m < dz->itemcnt) {
  1348. if(flteq(dz->parray[TS_HARMONICS][n*2],dz->parray[TS_HARMONICS][m*2])) {
  1349. sprintf(errstr,"Duplicated harmonic number (%lf) in file %s.\n",dz->parray[TS_HARMONICS][n*2], filename); //RWD added filename arg - correct?
  1350. return(DATA_ERROR);
  1351. }
  1352. m++;
  1353. }
  1354. }
  1355. }
  1356. if((dz->fbandbot = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) { // Stores pointers into sintable
  1357. sprintf(errstr,"INSUFFICIENT MEMORY to create harmonics-reading pointers.\n");
  1358. return(MEMORY_ERROR);
  1359. }
  1360. (*cmdline)++;
  1361. (*cmdlinecnt)--;
  1362. return(FINISHED);
  1363. }
  1364. /************************** GET_FLOAT_WITH_E_FROM_WITHIN_STRING **************************
  1365. * takes a pointer TO A POINTER to a string. If it succeeds in finding
  1366. * a float it returns the float value (*val), and it's new position in the
  1367. * string (*str).
  1368. */
  1369. int get_float_with_e_from_within_string(char **str,double *val)
  1370. {
  1371. char *p, *valstart;
  1372. int decimal_point_cnt = 0, has_digits = 0, has_e = 0, lastchar = 0;
  1373. p = *str;
  1374. while(isspace(*p))
  1375. p++;
  1376. valstart = p;
  1377. switch(*p) {
  1378. case('-'): break;
  1379. case('.'): decimal_point_cnt=1; break;
  1380. default:
  1381. if(!isdigit(*p))
  1382. return(FALSE);
  1383. has_digits = TRUE;
  1384. break;
  1385. }
  1386. p++;
  1387. while(!isspace(*p) && *p!=NEWLINE && *p!=ENDOFSTR) {
  1388. if(isdigit(*p))
  1389. has_digits = TRUE;
  1390. else if(*p == 'e') {
  1391. if(has_e || !has_digits)
  1392. return(FALSE);
  1393. has_e = 1;
  1394. } else if(*p == '-') {
  1395. if(!has_e || (lastchar != 'e'))
  1396. return(FALSE);
  1397. } else if(*p == '.') {
  1398. if(has_e || (++decimal_point_cnt>1))
  1399. return(FALSE);
  1400. } else
  1401. return(FALSE);
  1402. lastchar = *p;
  1403. p++;
  1404. }
  1405. if(!has_digits || sscanf(valstart,"%lf",val)!=1)
  1406. return(FALSE);
  1407. *str = p;
  1408. return(TRUE);
  1409. }
  1410. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  1411. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  1412. {
  1413. int exit_status;
  1414. infileptr infile_info;
  1415. if(!sloom) {
  1416. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  1417. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  1418. return(MEMORY_ERROR);
  1419. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  1420. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  1421. return(PROGRAM_ERROR);
  1422. } else if((exit_status = redefine_textfile_types2(infile_info,dz))<0) {
  1423. sprintf(errstr,"Unknown data type in file %s\n",cmdline[0]);
  1424. return(DATA_ERROR);
  1425. } else if(infile_info->filetype != NUMLIST) {
  1426. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  1427. return(DATA_ERROR);
  1428. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  1429. sprintf(errstr,"Failed to copy file parsing information\n");
  1430. return(PROGRAM_ERROR);
  1431. }
  1432. free(infile_info);
  1433. }
  1434. return(FINISHED);
  1435. }
  1436. /************************************* REDEFINE_TEXTFILE_TYPES2 ******************************/
  1437. int redefine_textfile_types2(infileptr infile_info,dataptr dz)
  1438. {
  1439. switch(infile_info->filetype) {
  1440. case(TRANSPOS_OR_NORMD_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1441. case(TRANSPOS_OR_NORMD_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1442. case(TRANSPOS_OR_PITCH_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1443. case(TRANSPOS_OR_PITCH_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1444. case(TRANSPOS_OR_UNRANGED_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1445. case(TRANSPOS_OR_UNRANGED_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1446. case(NORMD_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1447. case(NORMD_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1448. case(DB_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1449. case(DB_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1450. case(PITCH_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1451. case(PITCH_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1452. case(PITCH_POSITIVE_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1453. case(PITCH_POSITIVE_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1454. case(UNRANGED_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1455. case(UNRANGED_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1456. case(NUMLIST_OR_LINELIST_OR_WORDLIST):
  1457. case(NUMLIST_OR_WORDLIST):
  1458. case(POSITIVE_BRKFILE):
  1459. case(POSITIVE_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST):
  1460. case(POSITIVE_BRKFILE_OR_NUMLIST_OR_WORDLIST):
  1461. switch(dz->process) {
  1462. case(TS_OSCIL):
  1463. case(TS_TRACE):
  1464. infile_info->filetype = NUMLIST;
  1465. break;
  1466. }
  1467. break;
  1468. default:
  1469. sprintf(errstr,"Unknown input textfile type\n");
  1470. return(PROGRAM_ERROR);
  1471. }
  1472. return FINISHED;
  1473. }
  1474. /************************************* TS_OSCIL ******************************/
  1475. int ts_oscil(dataptr dz)
  1476. {
  1477. int exit_status;
  1478. double mean, top, bottom, numend, pointer, step = 0.0, thisval, jj, val, valstep, atten, maxsamp, time, firststep = 1.0;
  1479. double *secondderiv = NULL, *x = NULL, *y = NULL, *nums;
  1480. int n, sampcnt, totsamps, hi, ii, samps_to_write;
  1481. float *obuf;
  1482. obuf = dz->sampbuf[0];
  1483. nums = dz->parray[TS_DATA];
  1484. numend = (double)(dz->numsize - 1);
  1485. find_mean(&mean,&top,&bottom,dz);
  1486. scale_data_in_one_to_minusone_range(mean,top,bottom,dz);
  1487. if(dz->brksize[TS_TSTRETCH] || dz->param[TS_TSTRETCH] > 1) {
  1488. sampcnt = 0;
  1489. if(dz->brksize[TS_TSTRETCH]) {
  1490. if((exit_status = read_value_from_brktable(0.0,TS_TSTRETCH,dz))<0)
  1491. return(exit_status);
  1492. dz->param[TS_TSTRETCH] = pow(2.0,dz->param[TS_TSTRETCH]);
  1493. firststep = 1.0/dz->param[TS_TSTRETCH];
  1494. } else
  1495. step = 1.0/dz->param[TS_TSTRETCH];
  1496. if (dz->vflag[0]) {
  1497. // Cubic spline arrays
  1498. if((secondderiv = (double *)malloc(dz->numsize * sizeof(double)))==NULL) {
  1499. fprintf(stdout,"INSUFFICIENT MEMORY for storing slope of input data.\n");
  1500. fflush(stdout);
  1501. return(MEMORY_ERROR);
  1502. }
  1503. if((x = (double *)malloc(dz->numsize * sizeof(double)))==NULL) {
  1504. fprintf(stdout,"INSUFFICIENT MEMORY for storing x-coords of input data.\n");
  1505. fflush(stdout);
  1506. return(MEMORY_ERROR);
  1507. }
  1508. if((y = (double *)malloc(dz->numsize * sizeof(double)))==NULL) {
  1509. fprintf(stdout,"INSUFFICIENT MEMORY for storing y-coords of input data.\n");
  1510. fflush(stdout);
  1511. return(MEMORY_ERROR);
  1512. }
  1513. if((exit_status = spline(nums,secondderiv,x,y,dz))<0)
  1514. return(exit_status);
  1515. // Test level, in case splining causes oscillator to curve out of range
  1516. if(dz->brksize[TS_TSTRETCH])
  1517. step = firststep;
  1518. pointer = step;
  1519. hi = 0;
  1520. atten = 1.0;
  1521. maxsamp = 0.0;
  1522. while(pointer < numend) {
  1523. if((exit_status = splint(pointer,&thisval,&hi,dz->numsize,secondderiv,x,y))<0)
  1524. return(FAILED);
  1525. if(fabs(thisval) > maxsamp)
  1526. maxsamp = fabs(thisval);
  1527. pointer += step;
  1528. }
  1529. if(maxsamp > TS_MAXLEVEL)
  1530. atten = TS_MAXLEVEL/maxsamp;
  1531. for(;;) {
  1532. totsamps = 0;
  1533. if(dz->brksize[TS_TSTRETCH])
  1534. step = firststep;
  1535. obuf[sampcnt++] = 0.0f;
  1536. totsamps++;
  1537. hi = 0;
  1538. while(pointer < numend) {
  1539. if((exit_status = splint(pointer,&thisval,&hi,dz->numsize,secondderiv,x,y))<0)
  1540. return(FAILED);
  1541. obuf[sampcnt++] = (float)(thisval * atten);
  1542. if(sampcnt >= dz->buflen) {
  1543. if((dz->wanted > 0) && dz->wanted <= dz->total_samps_written + dz->buflen) {
  1544. samps_to_write = dz->wanted - dz->total_samps_written;
  1545. if((exit_status = write_samps(obuf,samps_to_write,dz))<0)
  1546. return(exit_status);
  1547. return FINISHED;
  1548. }
  1549. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1550. return(exit_status);
  1551. sampcnt = 0;
  1552. }
  1553. if(dz->brksize[TS_TSTRETCH] && (totsamps % 256 == 0)) {
  1554. time = (double)totsamps/(double)dz->infile->srate;
  1555. if((exit_status = read_value_from_brktable(time,TS_TSTRETCH,dz))<0)
  1556. return(exit_status);
  1557. dz->param[TS_TSTRETCH] = pow(2.0,dz->param[TS_TSTRETCH]);
  1558. step = 1.0/dz->param[TS_TSTRETCH];
  1559. }
  1560. totsamps++;
  1561. pointer += step;
  1562. }
  1563. if(!dz->vflag[1] || (dz->total_samps_written + sampcnt >= dz->wanted))
  1564. break;
  1565. }
  1566. } else { // LINEAR INTERP
  1567. for(;;) {
  1568. totsamps = 0;
  1569. if(dz->brksize[TS_TSTRETCH])
  1570. step = firststep;
  1571. pointer = 0.0;
  1572. while (pointer < numend) {
  1573. ii = (int)floor(pointer);
  1574. jj = pointer - (double)ii;
  1575. val = nums[ii];
  1576. valstep = nums[ii+1] - nums[ii];
  1577. valstep *= jj;
  1578. obuf[sampcnt++] = (float)(val + valstep);
  1579. if(sampcnt >= dz->buflen) {
  1580. if((dz->wanted > 0) && dz->wanted <= dz->total_samps_written + dz->buflen) {
  1581. samps_to_write = dz->wanted - dz->total_samps_written;
  1582. if((exit_status = write_samps(obuf,samps_to_write,dz))<0)
  1583. return(exit_status);
  1584. return FINISHED;
  1585. }
  1586. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1587. return(exit_status);
  1588. sampcnt = 0;
  1589. }
  1590. if(dz->brksize[TS_TSTRETCH] && (totsamps % 256 == 0)) {
  1591. time = (double)totsamps/(double)dz->infile->srate;
  1592. if((exit_status = read_value_from_brktable(time,TS_TSTRETCH,dz))<0)
  1593. return(exit_status);
  1594. dz->param[TS_TSTRETCH] = pow(2.0,dz->param[TS_TSTRETCH]);
  1595. step = 1.0/dz->param[TS_TSTRETCH];
  1596. }
  1597. totsamps++;
  1598. pointer += step;
  1599. }
  1600. if(!dz->vflag[1] || (dz->total_samps_written + sampcnt >= dz->wanted))
  1601. break;
  1602. }
  1603. }
  1604. } else { // NO TIMESTRETCH
  1605. sampcnt = 0;
  1606. for(;;) {
  1607. for(n=0;n < dz->numsize;n++) {
  1608. if(sampcnt >= dz->buflen) {
  1609. if((dz->wanted > 0) && dz->wanted <= dz->total_samps_written + dz->buflen) {
  1610. samps_to_write = dz->wanted - dz->total_samps_written;
  1611. if((exit_status = write_samps(obuf,samps_to_write,dz))<0)
  1612. return(exit_status);
  1613. return FINISHED;
  1614. }
  1615. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1616. return(exit_status);
  1617. sampcnt = 0;
  1618. }
  1619. obuf[sampcnt++] = (float)nums[n];
  1620. }
  1621. if(!dz->vflag[1] || (dz->total_samps_written + sampcnt >= dz->wanted))
  1622. break;
  1623. }
  1624. }
  1625. if(sampcnt > 0) {
  1626. if((dz->wanted > 0) && dz->wanted < dz->total_samps_written + sampcnt)
  1627. sampcnt = dz->wanted - dz->total_samps_written;
  1628. if((exit_status = write_samps(obuf,sampcnt,dz))<0)
  1629. return(exit_status);
  1630. }
  1631. return FINISHED;
  1632. }
  1633. /************************************* TS_TRACE ******************************/
  1634. int ts_trace(dataptr dz)
  1635. {
  1636. int exit_status, h, dotest = 0;
  1637. double mean, top, bottom, numend, pointer, step = 0.0, thisval, jj, val, valstep, time, firststep = 1.0;
  1638. double outval, maxoutval, atten, midpitch;
  1639. double *secondderiv = NULL, *x = NULL, *y = NULL, *nums;
  1640. int n, sampcnt, hi, ii, samps_to_write, totsamps;
  1641. float *obuf;
  1642. double incrementor = (double)TS_SINTABSIZE/(double)dz->infile->srate; // step forward in sintable, at 1 Hz
  1643. if((exit_status = create_sintab(dz)) < 0)
  1644. return(exit_status);
  1645. obuf = dz->sampbuf[0];
  1646. nums = dz->parray[TS_DATA];
  1647. numend = (double)(dz->numsize - 1);
  1648. find_mean(&mean,&top,&bottom,dz);
  1649. // Scale data range to pitch-range specified
  1650. scale_data_in_one_to_minusone_range(mean,top,bottom,dz);
  1651. for(n = 0;n < dz->numsize; n++)
  1652. nums[n] *= dz->param[TS_HALFRANGE];
  1653. midpitch = unchecked_hztomidi(dz->param[TS_FRQ]);
  1654. for(n = 0;n < dz->numsize; n++)
  1655. nums[n] += midpitch;
  1656. if(dz->brksize[TS_TSTRETCH] || (dz->param[TS_TSTRETCH] > 1)) {
  1657. if(dz->brksize[TS_TSTRETCH]) {
  1658. if((exit_status = read_value_from_brktable(0.0,TS_TSTRETCH,dz))<0)
  1659. return(exit_status);
  1660. dz->param[TS_TSTRETCH] = pow(2.0,dz->param[TS_TSTRETCH]);
  1661. firststep = 1.0/dz->param[TS_TSTRETCH];
  1662. } else
  1663. step = 1.0/dz->param[TS_TSTRETCH];
  1664. if (dz->vflag[0]) {
  1665. // Set up arrays and 2nd derivatives, for cubic spline
  1666. if((secondderiv = (double *)malloc(dz->numsize * sizeof(double)))==NULL) {
  1667. fprintf(stdout,"INSUFFICIENT MEMORY for storing slope of input data.\n");
  1668. fflush(stdout);
  1669. return(MEMORY_ERROR);
  1670. }
  1671. if((x = (double *)malloc(dz->numsize * sizeof(double)))==NULL) {
  1672. fprintf(stdout,"INSUFFICIENT MEMORY for storing x-coords of input data.\n");
  1673. fflush(stdout);
  1674. return(MEMORY_ERROR);
  1675. }
  1676. if((y = (double *)malloc(dz->numsize * sizeof(double)))==NULL) {
  1677. fprintf(stdout,"INSUFFICIENT MEMORY for storing y-coords of input data.\n");
  1678. fflush(stdout);
  1679. return(MEMORY_ERROR);
  1680. }
  1681. if((exit_status = spline(nums,secondderiv,x,y,dz))<0)
  1682. return(exit_status);
  1683. find_mean(&mean,&top,&bottom,dz); // Find range of expanded data
  1684. totsamps = 0;
  1685. if(dz->brksize[TS_TSTRETCH])
  1686. step = firststep;
  1687. pointer = step; // run cubic spline to test new top frq is within range
  1688. hi = 0;
  1689. while(pointer < numend) {
  1690. if((exit_status = splint(pointer,&thisval,&hi,dz->numsize,secondderiv,x,y))<0)
  1691. return(FAILED);
  1692. if(thisval > top) {
  1693. top = thisval;
  1694. dotest = 1;
  1695. }
  1696. pointer += step;
  1697. }
  1698. if(dotest) {
  1699. top = miditohz(top);
  1700. if(top * dz->scalefact >= dz->nyquist) { // scalefact = max harmonic number
  1701. fprintf(stdout,"CUBIC SPLINING adjustment sends highest harmonics beyond nyquist.\n");
  1702. fflush(stdout);
  1703. return(DATA_ERROR);
  1704. }
  1705. }
  1706. }
  1707. }
  1708. for(h = 0; h < dz->itemcnt;h++) // Preset-to-zero harmonic-pointers within sintab
  1709. dz->fbandbot[h] = 0.0;
  1710. maxoutval = 0.0; // Preset maxlevel, for maxlevel test
  1711. atten = 1.0; // Preset overall attenuation
  1712. sampcnt = 0;
  1713. totsamps = 0;
  1714. if(dz->brksize[TS_TSTRETCH])
  1715. step = firststep;
  1716. if(dz->brksize[TS_TSTRETCH] || (dz->param[TS_TSTRETCH] > 1)) {
  1717. if(dz->vflag[0]) { // CUBIC SPLINE INTERP
  1718. // Do first pass for level test
  1719. pointer = step;
  1720. hi = 0;
  1721. while(pointer < numend) {
  1722. if((exit_status = splint(pointer,&thisval,&hi,dz->numsize,secondderiv,x,y))<0)
  1723. return(FAILED);
  1724. outval = calcsample(thisval,TS_SINTABSIZE,incrementor,dz);
  1725. if(fabs(outval) >= maxoutval)
  1726. maxoutval = outval;
  1727. pointer += step;
  1728. }
  1729. if(maxoutval > TS_MAXLEVEL)
  1730. atten = TS_MAXLEVEL/maxoutval;
  1731. // Do real pass
  1732. for(h = 0; h < dz->itemcnt;h++) // Reset harmonic-pointers within sintab
  1733. dz->fbandbot[h] = 0.0;
  1734. for(;;) {
  1735. totsamps = 0;
  1736. if(dz->brksize[TS_TSTRETCH])
  1737. step = firststep;
  1738. obuf[sampcnt++] = 0.0f; // Splining assumes signal starts at zero
  1739. totsamps++;
  1740. pointer = step;
  1741. hi = 0;
  1742. while(pointer < numend) {
  1743. if((exit_status = splint(pointer,&thisval,&hi,dz->numsize,secondderiv,x,y))<0)
  1744. return(FAILED);
  1745. outval = calcsample(thisval,TS_SINTABSIZE,incrementor,dz);
  1746. obuf[sampcnt++] = (float)(outval * atten); // Scale by any attenuation required and advance to next sample
  1747. if(sampcnt >= dz->buflen) {
  1748. if((dz->wanted > 0) && dz->wanted <= dz->total_samps_written + dz->buflen) {
  1749. samps_to_write = dz->wanted - dz->total_samps_written;
  1750. if((exit_status = write_samps(obuf,samps_to_write,dz))<0)
  1751. return(exit_status);
  1752. return FINISHED;
  1753. }
  1754. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1755. return(exit_status);
  1756. sampcnt = 0;
  1757. }
  1758. totsamps++;
  1759. if(dz->brksize[TS_TSTRETCH] && (totsamps % 256 == 0)) {
  1760. time = (double)totsamps/(double)dz->infile->srate;
  1761. if((exit_status = read_value_from_brktable(time,TS_TSTRETCH,dz))<0)
  1762. return(exit_status);
  1763. dz->param[TS_TSTRETCH] = pow(2.0,dz->param[TS_TSTRETCH]);
  1764. step = 1.0/dz->param[TS_TSTRETCH];
  1765. }
  1766. pointer += step;
  1767. }
  1768. if(!dz->vflag[1] || (dz->total_samps_written + sampcnt >= dz->wanted))
  1769. break;
  1770. }
  1771. } else { // LINEAR INTERP
  1772. pointer = 0.0;
  1773. // Do first pass for level test
  1774. while (pointer < numend) {
  1775. ii = (int)floor(pointer);
  1776. jj = pointer - (double)ii;
  1777. val = nums[ii];
  1778. valstep = nums[ii+1] - nums[ii];
  1779. valstep *= jj;
  1780. val += valstep;
  1781. outval = calcsample(val,TS_SINTABSIZE,incrementor,dz);
  1782. if(fabs(outval) >= maxoutval)
  1783. maxoutval = outval;
  1784. pointer += step;
  1785. }
  1786. if(maxoutval > TS_MAXLEVEL)
  1787. atten = TS_MAXLEVEL/maxoutval;
  1788. // Do real pass
  1789. for(h = 0; h < dz->itemcnt;h++) // Preset harmonic-pointers within sintab
  1790. dz->fbandbot[h] = 0.0;
  1791. for(;;) {
  1792. if(dz->brksize[TS_TSTRETCH])
  1793. step = firststep;
  1794. totsamps = 0;
  1795. pointer = 0.0;
  1796. while (pointer < numend) {
  1797. ii = (int)floor(pointer);
  1798. jj = pointer - (double)ii;
  1799. val = nums[ii];
  1800. valstep = nums[ii+1] - nums[ii];
  1801. valstep *= jj;
  1802. val += valstep;
  1803. outval = calcsample(val,TS_SINTABSIZE,incrementor,dz);
  1804. obuf[sampcnt++] = (float)(outval * atten); // Scale by any attenuation required and advance to next sample
  1805. if(sampcnt >= dz->buflen) {
  1806. if((dz->wanted > 0) && dz->wanted <= dz->total_samps_written + dz->buflen) {
  1807. samps_to_write = dz->wanted - dz->total_samps_written;
  1808. if((exit_status = write_samps(obuf,samps_to_write,dz))<0)
  1809. return(exit_status);
  1810. return FINISHED;
  1811. }
  1812. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1813. return(exit_status);
  1814. sampcnt = 0;
  1815. }
  1816. if(dz->brksize[TS_TSTRETCH] && (totsamps % 256 == 0)) {
  1817. time = (double)totsamps/(double)dz->infile->srate;
  1818. if((exit_status = read_value_from_brktable(time,TS_TSTRETCH,dz))<0)
  1819. return(exit_status);
  1820. dz->param[TS_TSTRETCH] = pow(2.0,dz->param[TS_TSTRETCH]);
  1821. step = 1.0/dz->param[TS_TSTRETCH];
  1822. }
  1823. totsamps++;
  1824. pointer += step;
  1825. }
  1826. if(!dz->vflag[1] || (dz->total_samps_written + sampcnt >= dz->wanted))
  1827. break;
  1828. }
  1829. }
  1830. } else { // NO TIMESTRETCH
  1831. // Do first pass for level test
  1832. for(n=0;n < dz->numsize;n++) {
  1833. outval = calcsample(nums[n],TS_SINTABSIZE,incrementor,dz);
  1834. if(fabs(outval) > maxoutval)
  1835. maxoutval = outval;
  1836. }
  1837. if(maxoutval > TS_MAXLEVEL)
  1838. atten = TS_MAXLEVEL/maxoutval;
  1839. // Do real pass
  1840. for(;;) {
  1841. for(n=0;n < dz->numsize;n++) {
  1842. outval = calcsample(nums[n],TS_SINTABSIZE,incrementor,dz);
  1843. obuf[sampcnt++] = (float)(outval * atten); // Scale by any attenuation required and advance to next sample
  1844. if(sampcnt >= dz->buflen) {
  1845. if((dz->wanted > 0) && dz->wanted <= dz->total_samps_written + dz->buflen) {
  1846. samps_to_write = dz->wanted - dz->total_samps_written;
  1847. if((exit_status = write_samps(obuf,samps_to_write,dz))<0)
  1848. return(exit_status);
  1849. return FINISHED;
  1850. }
  1851. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  1852. return(exit_status);
  1853. sampcnt = 0;
  1854. }
  1855. }
  1856. if(!dz->vflag[1] || (dz->total_samps_written + sampcnt >= dz->wanted))
  1857. break;
  1858. }
  1859. }
  1860. if(sampcnt > 0) {
  1861. if((dz->wanted > 0) && dz->wanted < dz->total_samps_written + sampcnt)
  1862. sampcnt = dz->wanted - dz->total_samps_written;
  1863. if((exit_status = write_samps(obuf,sampcnt,dz))<0)
  1864. return(exit_status);
  1865. }
  1866. return FINISHED;
  1867. }
  1868. /************************************* FIND_MEAN ******************************/
  1869. void find_mean(double *mean,double *top,double *bottom,dataptr dz)
  1870. {
  1871. double sum = 0.0, numax = -HUGE, numin = HUGE;
  1872. double *nums = dz->parray[TS_DATA];
  1873. int n;
  1874. sum = nums[0];
  1875. numax = nums[0];
  1876. numin = nums[0];
  1877. for(n = 1;n < dz->numsize; n++) {
  1878. sum += nums[n];
  1879. if(nums[n] > numax)
  1880. numax = nums[n];
  1881. if(nums[n] < numin)
  1882. numin = nums[n];
  1883. }
  1884. *mean = sum /(double)dz->numsize;
  1885. *top = numax;
  1886. *bottom = numin;
  1887. }
  1888. /************************************* SCALE_DATA_IN_ONE_TO_MINUSONE_RANGE ******************************/
  1889. void scale_data_in_one_to_minusone_range(double mean,double top,double bottom,dataptr dz)
  1890. {
  1891. int n;
  1892. double *nums = dz->parray[TS_DATA];
  1893. double uprange = top - mean;
  1894. double dnrange = mean - bottom;
  1895. double maxrange = max(uprange,dnrange);
  1896. for(n = 0;n < dz->numsize; n++)
  1897. nums[n] -= mean;
  1898. if(maxrange > 1.0) {
  1899. for(n = 0;n < dz->numsize; n++)
  1900. nums[n] /= maxrange;
  1901. }
  1902. }
  1903. /************************************ SPLINE ************************************
  1904. *
  1905. * This function returns the second derivative at all points of the curve.
  1906. *
  1907. * We assume that the curve is flat (first derivative = 0) at start and end points
  1908. */
  1909. int spline(double *nums,double *secondderiv,double *x,double *y,dataptr dz)
  1910. {
  1911. double firstderivstt = 0.0, firstderivend = 0.0;
  1912. double *u, sig, ppp, qend, uend;
  1913. int i, k, arraylen = dz->numsize;
  1914. if((u = (double *)malloc(arraylen * sizeof(double)))==NULL) {
  1915. fprintf(stdout,"INSUFFICIENT MEMORY for slope calculations 1.\n");
  1916. fflush(stdout);
  1917. return(MEMORY_ERROR);
  1918. }
  1919. for(i=0;i<dz->numsize;i++) {
  1920. x[i] = (double)i; // TIME is the independent, monotonically increasing variable
  1921. y[i] = nums[i];
  1922. }
  1923. secondderiv[0] = -0.5;
  1924. u[0] = (3.0/(x[1] - x[0])) * (((y[1] - y[0])/(x[1] - x[0])) - firstderivstt);
  1925. for(i = 1;i < arraylen-1; i++) {
  1926. sig = (x[i] - x[i-1])/(x[i+1] - x[i-1]);
  1927. ppp = (sig * secondderiv[i-1]) + 2.0;
  1928. secondderiv[i] = (sig - 1.0)/ppp;
  1929. u[i] = ((y[i+1] - y[i])/(x[i+1] - x[i])) - ((y[i] - y[i-1])/(x[i] - x[i-1]));
  1930. u[i] = (((6.0 * u[i])/(x[i+1] - x[i-1])) - (sig * u[i-1]))/ppp;
  1931. }
  1932. // i = arraylen-1; = last entry in array
  1933. qend = 0.5;
  1934. uend = (3.0/(x[i] - x[i-1])) * (firstderivend - ((y[i] - y[i-1])/(x[i] - x[i-1])));
  1935. secondderiv[i] = (uend - (qend * u[i-1]))/((qend * secondderiv[i-1]) + 1.0);
  1936. for(k = i-1;k >= 0;k--) {
  1937. secondderiv[k] = (secondderiv[k] * secondderiv[k+1]) + u[k];
  1938. }
  1939. free(u);
  1940. return(FINISHED);
  1941. }
  1942. /************************************ SPLINT ************************************
  1943. *
  1944. * This function returns the value at a specified frq on curve, using cspline interp (2nd-deriv) data..
  1945. */
  1946. int splint(double pointer,double *thisval,int *hi,int arraycnt,double *secondderiv, double *x, double *y)
  1947. {
  1948. int klo, khi = *hi;
  1949. double fullstep, a, b, amp, jjj, kkk;
  1950. /* Establish which input values bracket 'pointer' */
  1951. while(x[khi] <= pointer) {
  1952. khi++;
  1953. if(khi >= arraycnt) {
  1954. *thisval = y[arraycnt - 1];
  1955. return(FINISHED);
  1956. }
  1957. }
  1958. klo = khi - 1;
  1959. if(klo < 0) {
  1960. *thisval = y[0];
  1961. return(FINISHED);
  1962. }
  1963. fullstep = x[khi] - x[klo];
  1964. a = (x[khi] - pointer)/fullstep;
  1965. b = (pointer - x[klo])/fullstep;
  1966. amp = a * y[klo];
  1967. amp += b * y[khi];
  1968. jjj = a * a * a;
  1969. jjj -= a;
  1970. jjj *= secondderiv[klo];
  1971. kkk = b * b * b;
  1972. kkk -= b;
  1973. kkk *= secondderiv[khi];
  1974. jjj += kkk;
  1975. jjj *= fullstep * fullstep;
  1976. jjj /= 6.0;
  1977. amp += jjj;
  1978. *thisval = amp;
  1979. *hi = khi;
  1980. return(FINISHED);
  1981. }
  1982. /************************************ CALCSAMPLE ************************************/
  1983. double calcsample (double thisval,int sintabsize,double incrementor,dataptr dz)
  1984. {
  1985. int h, nn, aa;
  1986. int tabindex;
  1987. double hno, hamp, tabfrac, tabstep, tabval, outval, thisfrq, basefrq;
  1988. double *sintab = dz->parray[TS_SINETAB];
  1989. outval = 0.0;
  1990. basefrq = miditohz(thisval); // Convert midi-pitch to frq
  1991. for(h = 0,nn = 0, aa = 1; h < dz->itemcnt;h++,nn+=2,aa+=2) { // h count harmonics,
  1992. hno = dz->parray[TS_HARMONICS][nn]; // Get the harmonic number from parray[1]
  1993. thisfrq = basefrq * hno; // 'nn' points to harmonic number, 'aa' points to harmonic amplitude
  1994. hamp = dz->parray[TS_HARMONICS][aa]; // Get the harmonic amplitude from parray[1]
  1995. tabindex = (int)floor(dz->fbandbot[h]); // Read table with this harmonic's pointer
  1996. tabfrac = dz->fbandbot[h] - (double)tabindex;
  1997. tabstep = sintab[tabindex+1] - sintab[tabindex];
  1998. tabstep *= tabfrac;
  1999. tabval = sintab[tabindex] + tabstep;
  2000. tabval *= hamp; // Scale by amplitude of this harmonic
  2001. outval += tabval; // Add value from this harmonic into value at sample
  2002. dz->fbandbot[h] += incrementor * thisfrq; // Advance this harmonic pointer appropriately
  2003. if(dz->fbandbot[h] >= (double)TS_SINTABSIZE) // wrapping around the sinetable
  2004. dz->fbandbot[h] -= (double)TS_SINTABSIZE;
  2005. }
  2006. return outval;
  2007. }
  2008. /************************************ CREATE_SINTAB ************************************/
  2009. int create_sintab(dataptr dz)
  2010. {
  2011. int n;
  2012. double step, *tab;
  2013. if((dz->parray[TS_SINETAB] = (double *)malloc((TS_SINTABSIZE + 1) * sizeof(double)))==NULL) {
  2014. sprintf(errstr,"INSUFFICIENT MEMORY for sine table.\n");
  2015. return(MEMORY_ERROR);
  2016. }
  2017. tab = dz->parray[TS_SINETAB];
  2018. step = (2.0 * PI)/(double)TS_SINTABSIZE;
  2019. for(n=0;n<TS_SINTABSIZE;n++)
  2020. tab[n] = sin(step * (double)n) * F_MAXSAMP;
  2021. tab[TS_SINTABSIZE] = 0.0; // wraparound point
  2022. return(FINISHED);
  2023. }
  2024. /************************************ COUNT_AND_TEST_FOR_INFILES ************************************/
  2025. int count_and_test_for_infiles(int argc,char *argv[],dataptr dz)
  2026. {
  2027. switch(dz->input_data_type) {
  2028. case(NUMLIST_ONLY):
  2029. dz->infilecnt = 1;
  2030. break;
  2031. default:
  2032. sprintf(errstr,"Unknown input_data_type: count_and_allocate_for_infiles()\n");
  2033. return(PROGRAM_ERROR);
  2034. }
  2035. if((dz->insams = (int *)malloc(dz->infilecnt * sizeof(int)))==NULL) {
  2036. sprintf(errstr,"INSUFFICIENT MEMORY for dummy infile-sampsize array.\n");
  2037. return(MEMORY_ERROR);
  2038. }
  2039. return FINISHED;
  2040. }