newmorph.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  1. /*
  2. * Copyright (c) 1983-2020 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. //
  22. // MPH_COS = 0
  23. #define NMPH_COSTABSIZE (512)
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <osbind.h>
  27. #include <structures.h>
  28. #include <filetype.h>
  29. #include <pnames.h>
  30. #include <tkglobals.h>
  31. #include <globcon.h>
  32. #include <modeno.h>
  33. #include <logic.h>
  34. #include <morph.h>
  35. #include <cdpmain.h>
  36. #include <formants.h>
  37. #include <speccon.h>
  38. #include <sfsys.h>
  39. #include <string.h>
  40. #include <morph.h>
  41. #include <standalone.h>
  42. #include <speccon.h>
  43. // #include <filetype.h>
  44. // #include <modeno.h>
  45. #include <ctype.h>
  46. //#ifdef unix
  47. #define round(x) lround((x))
  48. //#endif
  49. char errstr[2400];
  50. const char* cdp_version = "7.1.0";
  51. /* extern */ int sloom = 0;
  52. /* extern */ int sloombatch = 0;
  53. /* extern */ int anal_infiles = 1;
  54. /* extern */ int is_converted_to_stereo = -1;
  55. /* CDP LIBRARY FUNCTIONS TRANSFERRED HERE */
  56. static int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist);
  57. static int set_vflgs(aplptr ap,char *optflags,int optcnt,char *optlist,
  58. char *varflags,int vflagcnt, int vparamcnt,char *varlist);
  59. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  60. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  61. static int mark_parameter_types(dataptr dz,aplptr ap);
  62. static int establish_application(dataptr dz);
  63. static int application_init(dataptr dz);
  64. static int initialise_vflags(dataptr dz);
  65. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  66. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  67. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  68. static int assign_file_data_storage(int infilecnt,dataptr dz);
  69. //static int store_wordlist(char *filename,dataptr dz);
  70. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  71. /* CDP LIB FUNCTION MODIFIED TO AVOID CALLING setup_particular_application() */
  72. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  73. /* SIMPLIFICATION OF LIB FUNC TO APPLY TO JUST THIS FUNCTION */
  74. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  75. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz);
  76. static int setup_specmorph_application(dataptr dz);
  77. static int setup_specmorph_param_ranges_and_defaults(dataptr dz);
  78. static int open_the_first_infile(char *filename,dataptr dz);
  79. static int handle_the_extra_infiles(char ***cmdline,int *cmdlinecnt,dataptr dz);
  80. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  81. static int read_specific_file_to_specific_buf(int fileno,int *windows_to_buf,float *floatbuf,dataptr dz);
  82. static int warped_cosin(double *interpratio,double exponent,dataptr dz);
  83. static int time_warp(double *interpratio,double exponent,dataptr dz);
  84. static int cosin_lookup(double *interpratio,dataptr dz);
  85. static int do_newmorph(double alen, dataptr dz);
  86. static void newmorph_core(double interpratio,dataptr dz);
  87. static int find_the_average_spectral_peaks(dataptr dz);
  88. static int make_newmorph_costable(dataptr dz);
  89. static int create_peakcnt_stores(dataptr dz);
  90. static int morph_preprocess(dataptr dz);
  91. static int check_consistency_of_newmorph_params(dataptr dz);
  92. static int allocate_new_triple_buffer(dataptr dz);
  93. static void find_the_spectral_peaks(int *peakcnt,dataptr dz);
  94. static void do_morphenv(dataptr dz);
  95. static int find_the_average_spectral_peaks_and_correlates(dataptr dz);
  96. static int handle_the_special_data(char *,dataptr dz);
  97. static int read_the_special_data(dataptr dz);
  98. /**************************************** MAIN *********************************************/
  99. int main(int argc,char *argv[])
  100. {
  101. int exit_status;
  102. /* FILE *fp = NULL; */
  103. dataptr dz = NULL;
  104. // char *special_data_string = NULL;
  105. char **cmdline;
  106. int cmdlinecnt;
  107. //aplptr ap;
  108. int is_launched = FALSE;
  109. /* CHECK FOR SOUNDLOOM */
  110. //TW UPDATE
  111. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  112. fprintf(stdout,"%s\n",cdp_version);
  113. fflush(stdout);
  114. return 0;
  115. }
  116. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  117. sloom = 0;
  118. sloombatch = 1;
  119. }
  120. if(sflinit("cdp")){
  121. sfperror("cdp: initialisation\n");
  122. return(FAILED);
  123. }
  124. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  125. if((exit_status = establish_datastructure(&dz))<0) {
  126. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  127. return(FAILED);
  128. }
  129. if(!sloom) {
  130. /* INITIAL CHECK OF CMDLINE DATA */
  131. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) {
  132. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  133. return(FAILED);
  134. }
  135. cmdline = argv; /* GET PRE_DATA, ALLOCATE THE APPLICATION, CHECK FOR EXTRA INFILES */
  136. cmdlinecnt = argc;
  137. // get_process_and_mode_from_cmdline -->
  138. if (!strcmp(argv[0],"newmorph")) {
  139. dz->process = SPECMORPH;
  140. dz->maxmode = 7;
  141. } else if (!strcmp(argv[0],"newmorph2")) {
  142. dz->process = SPECMORPH2;
  143. dz->maxmode = 3;
  144. }
  145. else
  146. return usage1();
  147. cmdline++;
  148. cmdlinecnt--;
  149. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  150. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  151. return(exit_status);
  152. }
  153. cmdline++;
  154. cmdlinecnt--;
  155. // setup_particular_application =
  156. if((exit_status = setup_specmorph_application(dz))<0) {
  157. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  158. return(FAILED);
  159. }
  160. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) {
  161. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  162. return(FAILED);
  163. }
  164. } else {
  165. //parse_TK_data() =
  166. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) { /* includes setup_particular_application() */
  167. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);/* and cmdlinelength check = sees extra-infiles */
  168. return(exit_status);
  169. }
  170. }
  171. //ap = dz->application;
  172. // parse_infile_and_hone_type() =
  173. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  174. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  175. return(FAILED);
  176. }
  177. // setup_param_ranges_and_defaults = MOVED IN THIS CASE ONLY TO LATER
  178. /* OPEN FIRST INFILE AND STORE DATA, AND INFORMATION, APPROPRIATELY */
  179. if((exit_status = open_the_first_infile(cmdline[0],dz))<0) {
  180. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  181. return(FAILED);
  182. }
  183. cmdlinecnt--;
  184. cmdline++;
  185. if(dz->process == SPECMORPH) {
  186. if((exit_status = handle_the_extra_infiles(&cmdline,&cmdlinecnt,dz))<0) {
  187. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  188. return(FAILED);
  189. }
  190. }
  191. if((exit_status = setup_specmorph_param_ranges_and_defaults(dz))<0) {
  192. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  193. return(FAILED);
  194. }
  195. // FOR display_virtual_time
  196. if(dz->process == SPECMORPH) {
  197. if(dz->mode == 6)
  198. dz->tempsize = min(dz->insams[0],dz->insams[1]);
  199. else
  200. dz->tempsize = dz->insams[1] + ((int)round(dz->param[NMPH_STAG]/dz->frametime) * dz->wanted);
  201. } else
  202. dz->tempsize = dz->insams[0];
  203. // handle_outfile
  204. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,is_launched,dz))<0) {
  205. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  206. return(FAILED);
  207. }
  208. // handle_formants
  209. // handle_formant_quiksearch
  210. if(dz->process == SPECMORPH2 && dz->mode != 0) {
  211. if((exit_status = handle_the_special_data(cmdline[0],dz))<0) {
  212. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  213. return(FAILED);
  214. }
  215. cmdlinecnt--;
  216. cmdline++;
  217. }
  218. // handle_special_data
  219. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  220. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  221. return(FAILED);
  222. }
  223. if(dz->process == SPECMORPH || dz->mode != 0) {
  224. if((exit_status = check_consistency_of_newmorph_params(dz))<0) {
  225. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  226. return(FAILED);
  227. }
  228. }
  229. // check_param_validity_and_consistency =
  230. is_launched = TRUE;
  231. if((exit_status = allocate_new_triple_buffer(dz))<0){
  232. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  233. return(FAILED);
  234. }
  235. // param_preprocess =
  236. // spec_process_file
  237. if((exit_status = morph_preprocess(dz))<0) {
  238. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  239. return(FAILED);
  240. }
  241. if((exit_status = specmorph(dz))<0) {
  242. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  243. return(FAILED);
  244. }
  245. if(!(dz->process == SPECMORPH && dz->mode == 6)) {
  246. if((exit_status = complete_output(dz))<0) {
  247. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  248. return(FAILED);
  249. }
  250. }
  251. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz);
  252. free(dz);
  253. return(SUCCEEDED);
  254. }
  255. /**********************************************
  256. REPLACED CDP LIB FUNCTIONS
  257. **********************************************/
  258. /****************************** SET_PARAM_DATA *********************************/
  259. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  260. {
  261. ap->special_data = (char)special_data;
  262. ap->param_cnt = (char)paramcnt;
  263. ap->max_param_cnt = (char)maxparamcnt;
  264. if(ap->max_param_cnt>0) {
  265. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  266. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  267. return(MEMORY_ERROR);
  268. }
  269. strcpy(ap->param_list,paramlist);
  270. }
  271. return(FINISHED);
  272. }
  273. /****************************** SET_VFLGS *********************************/
  274. int set_vflgs
  275. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  276. {
  277. ap->option_cnt = (char) optcnt; /*RWD added cast */
  278. if(optcnt) {
  279. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  280. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  281. return(MEMORY_ERROR);
  282. }
  283. strcpy(ap->option_list,optlist);
  284. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  285. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  286. return(MEMORY_ERROR);
  287. }
  288. strcpy(ap->option_flags,optflags);
  289. }
  290. ap->vflag_cnt = (char) vflagcnt;
  291. ap->variant_param_cnt = (char) vparamcnt;
  292. if(vflagcnt) {
  293. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  294. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  295. return(MEMORY_ERROR);
  296. }
  297. strcpy(ap->variant_list,varlist);
  298. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  299. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  300. return(MEMORY_ERROR);
  301. }
  302. strcpy(ap->variant_flags,varflags);
  303. }
  304. return(FINISHED);
  305. }
  306. /***************************** APPLICATION_INIT **************************/
  307. int application_init(dataptr dz)
  308. {
  309. int exit_status;
  310. int storage_cnt, n;
  311. int tipc, brkcnt;
  312. aplptr ap = dz->application;
  313. if(ap->vflag_cnt>0)
  314. initialise_vflags(dz);
  315. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  316. ap->total_input_param_cnt = (char)tipc;
  317. if(tipc>0) {
  318. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  319. return(exit_status);
  320. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  321. return(exit_status);
  322. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  323. return(exit_status);
  324. }
  325. brkcnt = tipc;
  326. if(brkcnt>0) {
  327. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  328. return(exit_status);
  329. }
  330. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  331. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  332. return(exit_status);
  333. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  334. return(exit_status);
  335. }
  336. if((exit_status = mark_parameter_types(dz,ap))<0)
  337. return(exit_status);
  338. // establish_infile_constants() replaced by
  339. dz->infilecnt = -2; /* flags 2 or more */
  340. // establish_bufptrs_and_extra_buffers
  341. dz->extra_bufcnt = 0;
  342. dz->bptrcnt = 5;
  343. // setup_internal_arrays_and_array_pointers()
  344. dz->iarray_cnt = 4;
  345. dz->array_cnt = 5;
  346. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  347. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  348. return(MEMORY_ERROR);
  349. }
  350. for(n=0;n<dz->array_cnt;n++)
  351. dz->parray[n] = NULL;
  352. if((dz->iparray = (int **)malloc(dz->iarray_cnt * sizeof(int *)))==NULL) {
  353. sprintf(errstr,"INSUFFICIENT MEMORY for internal integer arrays.\n");
  354. return(MEMORY_ERROR);
  355. }
  356. for(n=0;n<dz->iarray_cnt;n++)
  357. dz->iparray[n] = NULL;
  358. return establish_spec_bufptrs_and_extra_buffers(dz);
  359. }
  360. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  361. /* RWD mallo changed to calloc; helps debug verison run as release! */
  362. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  363. {
  364. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  365. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  366. return(MEMORY_ERROR);
  367. }
  368. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  369. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  370. return(MEMORY_ERROR);
  371. }
  372. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  373. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  374. return(MEMORY_ERROR);
  375. }
  376. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  377. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  378. return(MEMORY_ERROR);
  379. }
  380. return(FINISHED);
  381. }
  382. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  383. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  384. {
  385. int n;
  386. for(n=0;n<storage_cnt;n++) {
  387. dz->is_int[n] = (char)0;
  388. dz->no_brk[n] = (char)0;
  389. }
  390. return(FINISHED);
  391. }
  392. /***************************** MARK_PARAMETER_TYPES **************************/
  393. int mark_parameter_types(dataptr dz,aplptr ap)
  394. {
  395. int n, m; /* PARAMS */
  396. for(n=0;n<ap->max_param_cnt;n++) {
  397. switch(ap->param_list[n]) {
  398. case('0'): break; /* dz->is_active[n] = 0 is default */
  399. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  400. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  401. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  402. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  403. default:
  404. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  405. return(PROGRAM_ERROR);
  406. }
  407. } /* OPTIONS */
  408. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  409. switch(ap->option_list[n]) {
  410. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  411. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  412. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  413. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  414. default:
  415. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  416. return(PROGRAM_ERROR);
  417. }
  418. } /* VARIANTS */
  419. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  420. switch(ap->variant_list[n]) {
  421. case('0'): break;
  422. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  423. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  424. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  425. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  426. default:
  427. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  428. return(PROGRAM_ERROR);
  429. }
  430. } /* INTERNAL */
  431. for(n=0,
  432. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  433. switch(ap->internal_param_list[n]) {
  434. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  435. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  436. case('d'): dz->no_brk[m] = (char)1; break;
  437. default:
  438. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  439. return(PROGRAM_ERROR);
  440. }
  441. }
  442. return(FINISHED);
  443. }
  444. /***************************** ESTABLISH_APPLICATION **************************/
  445. int establish_application(dataptr dz)
  446. {
  447. aplptr ap;
  448. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  449. sprintf(errstr,"establish_application()\n");
  450. return(MEMORY_ERROR);
  451. }
  452. ap = dz->application;
  453. memset((char *)ap,0,sizeof(struct applic));
  454. return(FINISHED);
  455. }
  456. /************************* INITIALISE_VFLAGS *************************/
  457. int initialise_vflags(dataptr dz)
  458. {
  459. int n;
  460. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  461. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  462. return(MEMORY_ERROR);
  463. }
  464. for(n=0;n<dz->application->vflag_cnt;n++)
  465. dz->vflag[n] = FALSE;
  466. return FINISHED;
  467. }
  468. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  469. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  470. {
  471. int n;
  472. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  473. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  474. return(MEMORY_ERROR);
  475. }
  476. for(n=0;n<tipc;n++)
  477. ap->default_val[n] = 0.0;
  478. return(FINISHED);
  479. }
  480. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  481. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  482. {
  483. int n;
  484. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  485. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  486. return(MEMORY_ERROR);
  487. }
  488. for(n=0;n<tipc;n++)
  489. dz->is_active[n] = (char)0;
  490. return(FINISHED);
  491. }
  492. /************************* SETUP_SPECMORPH_APPLICATION *******************/
  493. int setup_specmorph_application(dataptr dz)
  494. {
  495. int exit_status;
  496. aplptr ap;
  497. if((exit_status = establish_application(dz))<0) // GLOBAL
  498. return(FAILED);
  499. ap = dz->application;
  500. // SEE parstruct FOR EXPLANATION of next 2 functions
  501. if(dz->process == SPECMORPH2) {
  502. switch(dz->mode) {
  503. case(0):
  504. if((exit_status = set_param_data(ap,0 ,6,1,"0000i0" ))<0)
  505. return(FAILED);
  506. if((exit_status = set_vflgs(ap, "",0,"" ,"",0,0,""))<0)
  507. return(FAILED);
  508. break;
  509. default:
  510. if((exit_status = set_param_data(ap,MPEAKS,6,4,"0dddi0" ))<0)
  511. return(FAILED);
  512. if((exit_status = set_vflgs(ap, "r",1,"D" ,"",0,0,""))<0)
  513. return(FAILED);
  514. break;
  515. }
  516. } else {
  517. switch(dz->mode) {
  518. case(4):
  519. case(5):
  520. if((exit_status = set_param_data(ap,0 ,6,5,"ddddi0" ))<0)
  521. return(FAILED);
  522. if((exit_status = set_vflgs(ap, "r",1,"D" ,"" ,0,0,""))<0)
  523. return(FAILED);
  524. break;
  525. case(6):
  526. if((exit_status = set_param_data(ap,0 ,6,2,"0000ii" ))<0)
  527. return(FAILED);
  528. if((exit_status = set_vflgs(ap, "",0,"" ,"enf" ,3,0,"000"))<0)
  529. return(FAILED);
  530. break;
  531. default:
  532. if((exit_status = set_param_data(ap,0 ,6,5,"ddddi0" ))<0)
  533. return(FAILED);
  534. if((exit_status = set_vflgs(ap, "",0,"" ,"enf" ,3,0,"000"))<0)
  535. return(FAILED);
  536. break;
  537. }
  538. }
  539. dz->has_otherfile = FALSE;
  540. if(dz->process == SPECMORPH2) {
  541. if (dz->mode == 0) {
  542. dz->input_data_type = ANALFILE;
  543. dz->process_type = TO_TEXTFILE;
  544. dz->outfiletype = TEXTFILE_OUT;
  545. } else {
  546. dz->input_data_type = ANALFILE;
  547. dz->process_type = EQUAL_ANALFILE;
  548. dz->outfiletype = ANALFILE_OUT;
  549. }
  550. } else {
  551. dz->input_data_type = TWO_ANALFILES;
  552. dz->process_type = BIG_ANALFILE;
  553. dz->outfiletype = ANALFILE_OUT;
  554. }
  555. return application_init(dz); //GLOBAL
  556. }
  557. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  558. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  559. {
  560. int n;
  561. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  562. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  563. return(MEMORY_ERROR);
  564. }
  565. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  566. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  567. return(MEMORY_ERROR);
  568. }
  569. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  570. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  571. return(MEMORY_ERROR);
  572. }
  573. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  574. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  575. return(MEMORY_ERROR);
  576. }
  577. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  578. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  579. return(MEMORY_ERROR);
  580. }
  581. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  582. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  583. return(MEMORY_ERROR);
  584. }
  585. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  586. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  587. return(MEMORY_ERROR);
  588. }
  589. for(n=0;n<brkcnt;n++) {
  590. dz->brk[n] = NULL;
  591. dz->brkptr[n] = NULL;
  592. dz->brkinit[n] = 0;
  593. dz->brksize[n] = 0;
  594. }
  595. return(FINISHED);
  596. }
  597. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  598. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  599. {
  600. int exit_status;
  601. infileptr infile_info;
  602. if(!sloom) {
  603. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  604. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  605. return(MEMORY_ERROR);
  606. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  607. sprintf(errstr,"Failed tp parse input file %s\n",cmdline[0]);
  608. return(PROGRAM_ERROR);
  609. } else if(infile_info->filetype != ANALFILE) {
  610. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  611. return(DATA_ERROR);
  612. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  613. sprintf(errstr,"Failed to copy file parsing information\n");
  614. return(PROGRAM_ERROR);
  615. }
  616. free(infile_info);
  617. }
  618. if((exit_status = set_chunklens_and_establish_windowbufs(dz))<0)
  619. return(exit_status);
  620. return(FINISHED);
  621. }
  622. /************************* SETUP_SPECMORPH_PARAM_RANGES_AND_DEFAULTS *******************/
  623. int setup_specmorph_param_ranges_and_defaults(dataptr dz)
  624. {
  625. int exit_status;
  626. aplptr ap = dz->application;
  627. // set_param_ranges()
  628. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  629. // setup_input_param_range_stores()
  630. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  631. return(FAILED);
  632. // get_param_ranges()
  633. if(dz->process == SPECMORPH2) {
  634. if(dz->mode != 0) {
  635. ap->lo[NMPH_ASTT] = 0.0;
  636. ap->hi[NMPH_ASTT] = (dz->wlength - 1) * dz->frametime;
  637. ap->default_val[NMPH_ASTT] = 0.0;
  638. ap->lo[NMPH_AEND] = dz->frametime * 2;
  639. ap->hi[NMPH_AEND] = BIG_TIME;
  640. ap->default_val[NMPH_AEND] = dz->duration;
  641. ap->lo[NMPH_AEXP] = 0.02;
  642. ap->hi[NMPH_AEXP] = 50.0;
  643. ap->default_val[NMPH_AEXP] = 1.0;
  644. ap->lo[NMPH_APKS] = 1;
  645. ap->hi[NMPH_APKS] = 16;
  646. ap->default_val[NMPH_APKS] = 8;
  647. ap->lo[NMPH_RAND] = 0;
  648. ap->hi[NMPH_RAND] = 1;
  649. ap->default_val[NMPH_RAND] = 1;
  650. } else {
  651. ap->lo[NMPH_APKS] = 1;
  652. ap->hi[NMPH_APKS] = 16;
  653. ap->default_val[NMPH_APKS] = 8;
  654. }
  655. dz->maxmode = 3;
  656. } else {
  657. if(dz->mode == 6) {
  658. ap->lo[NMPH_APKS] = 1;
  659. ap->hi[NMPH_APKS] = 16;
  660. ap->default_val[NMPH_APKS] = 8;
  661. ap->lo[NMPH_OCNT] = 1;
  662. ap->hi[NMPH_OCNT] = 64;
  663. ap->default_val[NMPH_OCNT] = 6;
  664. } else {
  665. ap->lo[NMPH_STAG] = 0.0;
  666. ap->hi[NMPH_STAG] = (dz->wlength-1) * dz->frametime;
  667. ap->default_val[NMPH_STAG] = 0.0;
  668. ap->lo[NMPH_ASTT] = 0.0;
  669. ap->hi[NMPH_ASTT] = (dz->wlength - 1) * dz->frametime;
  670. ap->default_val[NMPH_ASTT] = 0.0;
  671. ap->lo[NMPH_AEND] = dz->frametime * 2;
  672. ap->hi[NMPH_AEND] = BIG_TIME;
  673. ap->default_val[NMPH_AEND] = dz->duration;
  674. ap->lo[NMPH_AEXP] = 0.02;
  675. ap->hi[NMPH_AEXP] = 50.0;
  676. ap->default_val[NMPH_AEXP] = 1.0;
  677. ap->lo[NMPH_APKS] = 1;
  678. ap->hi[NMPH_APKS] = 16;
  679. ap->default_val[NMPH_APKS] = 8;
  680. if(dz->mode == 4 || dz->mode == 5) {
  681. ap->lo[NMPH_RAND] = 0;
  682. ap->hi[NMPH_RAND] = 1;
  683. ap->default_val[NMPH_RAND] = 1;
  684. }
  685. }
  686. dz->maxmode = 7;
  687. }
  688. if(!sloom)
  689. put_default_vals_in_all_params(dz);
  690. return(FINISHED);
  691. }
  692. /********************************* PARSE_SLOOM_DATA *********************************/
  693. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  694. {
  695. int exit_status;
  696. int cnt = 1, infilecnt;
  697. int filesize, insams, inbrksize;
  698. double dummy;
  699. int true_cnt = 0;
  700. //aplptr ap;
  701. while(cnt<=PRE_CMDLINE_DATACNT) {
  702. if(cnt > argc) {
  703. sprintf(errstr,"Insufficient data sent from TK\n");
  704. return(DATA_ERROR);
  705. }
  706. switch(cnt) {
  707. case(1):
  708. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  709. sprintf(errstr,"Cannot read process no. sent from TK\n");
  710. return(DATA_ERROR);
  711. }
  712. break;
  713. case(2):
  714. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  715. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  716. return(DATA_ERROR);
  717. }
  718. if(dz->mode > 0)
  719. dz->mode--;
  720. //setup_particular_application()=
  721. if((exit_status = setup_specmorph_application(dz))<0)
  722. return(exit_status);
  723. //ap = dz->application;
  724. break;
  725. case(3):
  726. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  727. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  728. return(DATA_ERROR);
  729. }
  730. if(infilecnt < 1) {
  731. true_cnt = cnt + 1;
  732. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  733. }
  734. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  735. return(exit_status);
  736. break;
  737. case(INPUT_FILETYPE+4):
  738. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  739. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  740. return(DATA_ERROR);
  741. }
  742. break;
  743. case(INPUT_FILESIZE+4):
  744. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  745. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  746. return(DATA_ERROR);
  747. }
  748. dz->insams[0] = filesize;
  749. break;
  750. case(INPUT_INSAMS+4):
  751. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  752. sprintf(errstr,"Cannot read insams sent from TK\n");
  753. return(DATA_ERROR);
  754. }
  755. dz->insams[0] = insams;
  756. break;
  757. case(INPUT_SRATE+4):
  758. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  759. sprintf(errstr,"Cannot read srate sent from TK\n");
  760. return(DATA_ERROR);
  761. }
  762. break;
  763. case(INPUT_CHANNELS+4):
  764. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  765. sprintf(errstr,"Cannot read channels sent from TK\n");
  766. return(DATA_ERROR);
  767. }
  768. break;
  769. case(INPUT_STYPE+4):
  770. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  771. sprintf(errstr,"Cannot read stype sent from TK\n");
  772. return(DATA_ERROR);
  773. }
  774. break;
  775. case(INPUT_ORIGSTYPE+4):
  776. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  777. sprintf(errstr,"Cannot read origstype sent from TK\n");
  778. return(DATA_ERROR);
  779. }
  780. break;
  781. case(INPUT_ORIGRATE+4):
  782. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  783. sprintf(errstr,"Cannot read origrate sent from TK\n");
  784. return(DATA_ERROR);
  785. }
  786. break;
  787. case(INPUT_MLEN+4):
  788. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  789. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  790. return(DATA_ERROR);
  791. }
  792. break;
  793. case(INPUT_DFAC+4):
  794. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  795. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  796. return(DATA_ERROR);
  797. }
  798. break;
  799. case(INPUT_ORIGCHANS+4):
  800. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  801. sprintf(errstr,"Cannot read origchans sent from TK\n");
  802. return(DATA_ERROR);
  803. }
  804. break;
  805. case(INPUT_SPECENVCNT+4):
  806. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  807. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  808. return(DATA_ERROR);
  809. }
  810. dz->specenvcnt = dz->infile->specenvcnt;
  811. break;
  812. case(INPUT_WANTED+4):
  813. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  814. sprintf(errstr,"Cannot read wanted sent from TK\n");
  815. return(DATA_ERROR);
  816. }
  817. break;
  818. case(INPUT_WLENGTH+4):
  819. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  820. sprintf(errstr,"Cannot read wlength sent from TK\n");
  821. return(DATA_ERROR);
  822. }
  823. break;
  824. case(INPUT_OUT_CHANS+4):
  825. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  826. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  827. return(DATA_ERROR);
  828. }
  829. break;
  830. /* RWD these changes to samps - tk will have to deal with that! */
  831. case(INPUT_DESCRIPTOR_BYTES+4):
  832. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  833. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  834. return(DATA_ERROR);
  835. }
  836. break;
  837. case(INPUT_IS_TRANSPOS+4):
  838. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  839. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  840. return(DATA_ERROR);
  841. }
  842. break;
  843. case(INPUT_COULD_BE_TRANSPOS+4):
  844. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  845. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  846. return(DATA_ERROR);
  847. }
  848. break;
  849. case(INPUT_COULD_BE_PITCH+4):
  850. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  851. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  852. return(DATA_ERROR);
  853. }
  854. break;
  855. case(INPUT_DIFFERENT_SRATES+4):
  856. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  857. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  858. return(DATA_ERROR);
  859. }
  860. break;
  861. case(INPUT_DUPLICATE_SNDS+4):
  862. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  863. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  864. return(DATA_ERROR);
  865. }
  866. break;
  867. case(INPUT_BRKSIZE+4):
  868. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  869. sprintf(errstr,"Cannot read brksize sent from TK\n");
  870. return(DATA_ERROR);
  871. }
  872. if(inbrksize > 0) {
  873. switch(dz->input_data_type) {
  874. case(WORDLIST_ONLY):
  875. break;
  876. case(PITCH_AND_PITCH):
  877. case(PITCH_AND_TRANSPOS):
  878. case(TRANSPOS_AND_TRANSPOS):
  879. dz->tempsize = inbrksize;
  880. break;
  881. case(BRKFILES_ONLY):
  882. case(UNRANGED_BRKFILE_ONLY):
  883. case(DB_BRKFILES_ONLY):
  884. case(ALL_FILES):
  885. case(ANY_NUMBER_OF_ANY_FILES):
  886. if(dz->extrabrkno < 0) {
  887. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  888. return(DATA_ERROR);
  889. }
  890. if(dz->brksize == NULL) {
  891. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  892. return(PROGRAM_ERROR);
  893. }
  894. dz->brksize[dz->extrabrkno] = inbrksize;
  895. break;
  896. default:
  897. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  898. dz->input_data_type);
  899. return(PROGRAM_ERROR);
  900. }
  901. break;
  902. }
  903. break;
  904. case(INPUT_NUMSIZE+4):
  905. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  906. sprintf(errstr,"Cannot read numsize sent from TK\n");
  907. return(DATA_ERROR);
  908. }
  909. break;
  910. case(INPUT_LINECNT+4):
  911. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  912. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  913. return(DATA_ERROR);
  914. }
  915. break;
  916. case(INPUT_ALL_WORDS+4):
  917. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  918. sprintf(errstr,"Cannot read all_words sent from TK\n");
  919. return(DATA_ERROR);
  920. }
  921. break;
  922. case(INPUT_ARATE+4):
  923. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  924. sprintf(errstr,"Cannot read arate sent from TK\n");
  925. return(DATA_ERROR);
  926. }
  927. break;
  928. case(INPUT_FRAMETIME+4):
  929. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  930. sprintf(errstr,"Cannot read frametime sent from TK\n");
  931. return(DATA_ERROR);
  932. }
  933. dz->frametime = (float)dummy;
  934. break;
  935. case(INPUT_WINDOW_SIZE+4):
  936. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  937. sprintf(errstr,"Cannot read window_size sent from TK\n");
  938. return(DATA_ERROR);
  939. }
  940. break;
  941. case(INPUT_NYQUIST+4):
  942. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  943. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  944. return(DATA_ERROR);
  945. }
  946. break;
  947. case(INPUT_DURATION+4):
  948. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  949. sprintf(errstr,"Cannot read duration sent from TK\n");
  950. return(DATA_ERROR);
  951. }
  952. break;
  953. case(INPUT_MINBRK+4):
  954. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  955. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  956. return(DATA_ERROR);
  957. }
  958. break;
  959. case(INPUT_MAXBRK+4):
  960. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  961. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  962. return(DATA_ERROR);
  963. }
  964. break;
  965. case(INPUT_MINNUM+4):
  966. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  967. sprintf(errstr,"Cannot read minnum sent from TK\n");
  968. return(DATA_ERROR);
  969. }
  970. break;
  971. case(INPUT_MAXNUM+4):
  972. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  973. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  974. return(DATA_ERROR);
  975. }
  976. break;
  977. default:
  978. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  979. return(PROGRAM_ERROR);
  980. }
  981. cnt++;
  982. }
  983. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  984. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  985. return(DATA_ERROR);
  986. }
  987. if(true_cnt)
  988. cnt = true_cnt;
  989. *cmdlinecnt = 0;
  990. while(cnt < argc) {
  991. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  992. return(exit_status);
  993. cnt++;
  994. }
  995. return(FINISHED);
  996. }
  997. /********************************* GET_TK_CMDLINE_WORD *********************************/
  998. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  999. {
  1000. if(*cmdlinecnt==0) {
  1001. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  1002. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1003. return(MEMORY_ERROR);
  1004. }
  1005. } else {
  1006. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  1007. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1008. return(MEMORY_ERROR);
  1009. }
  1010. }
  1011. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  1012. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  1013. return(MEMORY_ERROR);
  1014. }
  1015. strcpy((*cmdline)[*cmdlinecnt],q);
  1016. (*cmdlinecnt)++;
  1017. return(FINISHED);
  1018. }
  1019. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  1020. int assign_file_data_storage(int infilecnt,dataptr dz)
  1021. {
  1022. int exit_status;
  1023. int no_sndfile_system_files = FALSE;
  1024. dz->infilecnt = infilecnt;
  1025. if((exit_status = allocate_filespace(dz))<0)
  1026. return(exit_status);
  1027. if(no_sndfile_system_files)
  1028. dz->infilecnt = 0;
  1029. return(FINISHED);
  1030. }
  1031. /************************** OPEN_THE_FIRST_INFILE *****************************/
  1032. int open_the_first_infile(char *filename,dataptr dz)
  1033. {
  1034. if((dz->ifd[0] = sndopenEx(filename,0,CDP_OPEN_RDONLY)) < 0) {
  1035. sprintf(errstr,"Failure to open file %s for input.\n",filename);
  1036. return(SYSTEM_ERROR);
  1037. }
  1038. if(dz->infilecnt<=0 || dz->infile->filetype!=ANALFILE) {
  1039. sprintf(errstr,"%s is wrong type of file for this process.\n",filename);
  1040. return(DATA_ERROR);
  1041. }
  1042. dz->samps_left = dz->insams[0];
  1043. return(FINISHED);
  1044. }
  1045. /************************ HANDLE_THE_EXTRA_INFILES *********************/
  1046. int handle_the_extra_infiles(char ***cmdline,int *cmdlinecnt,dataptr dz)
  1047. {
  1048. /* OPEN ANY FURTHER INFILES, CHECK COMPATIBILITY, STORE DATA AND INFO */
  1049. int exit_status, n;
  1050. char *filename;
  1051. if(dz->infilecnt > 1) {
  1052. for(n=1;n<dz->infilecnt;n++) {
  1053. filename = (*cmdline)[0];
  1054. if((exit_status = handle_other_infile(n,filename,dz))<0)
  1055. return(exit_status);
  1056. (*cmdline)++;
  1057. (*cmdlinecnt)--;
  1058. }
  1059. } else {
  1060. sprintf(errstr,"Insufficient input files for this process\n");
  1061. return(DATA_ERROR);
  1062. }
  1063. return(FINISHED);
  1064. }
  1065. /************************ HANDLE_THE_OUTFILE *********************/
  1066. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,int is_launched,dataptr dz)
  1067. {
  1068. int exit_status;
  1069. char *filename = NULL, *p;
  1070. int n;
  1071. int stype = SAMP_FLOAT;
  1072. n = strlen((*cmdline)[0]);
  1073. if((filename = (char *)malloc((n + 10) * sizeof(char)))==NULL) {
  1074. sprintf(errstr,"INSUFFICIENT MEMORY for outfilename.\n");
  1075. return(MEMORY_ERROR);
  1076. }
  1077. if(sloom) {
  1078. strcpy(filename,((*cmdline)[0]));
  1079. } else {
  1080. if(*cmdlinecnt<=0) {
  1081. sprintf(errstr,"Insufficient cmdline parameters.\n");
  1082. return(USAGE_ONLY);
  1083. }
  1084. strcpy(filename,((*cmdline)[0]));
  1085. if(filename[0]=='-' && filename[1]=='f') {
  1086. sprintf(errstr,"-f flag used incorrectly on command line (output is not a sound file).\n");
  1087. return(USAGE_ONLY);
  1088. }
  1089. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  1090. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  1091. return(DATA_ERROR);
  1092. }
  1093. }
  1094. if(dz->process == SPECMORPH2 && dz->mode == 0) {
  1095. p = filename + strlen(filename);
  1096. p--;
  1097. while(*p != '.') {
  1098. p--;
  1099. if(p == filename)
  1100. break;
  1101. }
  1102. if(*p == '.') {
  1103. *p = ENDOFSTR;
  1104. strcat(filename,".txt");
  1105. }
  1106. strcpy(dz->outfilename,filename);
  1107. if(file_has_reserved_extension(filename)) {
  1108. sprintf(errstr,"Cannot open a textfile (%s) with a reserved extension.\n",filename);
  1109. return(USER_ERROR);
  1110. }
  1111. if((dz->fp = fopen(filename,"w"))==NULL) {
  1112. sprintf(errstr,"Cannot open output file %s\n",filename);
  1113. return(USER_ERROR);
  1114. }
  1115. } else {
  1116. if(dz->process == SPECMORPH && dz->mode == 6) {
  1117. p = filename + strlen(filename);
  1118. p--;
  1119. while(*p != '.') {
  1120. p--;
  1121. if(p == filename)
  1122. break;
  1123. }
  1124. if(*p == '.') {
  1125. if(sloom)
  1126. p--;
  1127. *p = ENDOFSTR;
  1128. } else {
  1129. if(sloom) {
  1130. p = filename + strlen(filename);
  1131. p--;
  1132. *p = ENDOFSTR;
  1133. }
  1134. }
  1135. dz->all_words = 0;
  1136. if((exit_status = store_filename(filename,dz))<0)
  1137. return(exit_status);
  1138. strcpy(dz->outfilename,dz->wordstor[0]);
  1139. strcat(dz->outfilename,"0.ana");
  1140. } else
  1141. strcpy(dz->outfilename,filename);
  1142. dz->true_outfile_stype = stype;
  1143. dz->outfilesize = -1;
  1144. if((dz->ofd = sndcreat_formatted(dz->outfilename,dz->outfilesize,stype,
  1145. dz->infile->channels,dz->infile->srate,CDP_CREATE_NORMAL)) < 0) {
  1146. sprintf(errstr,"Cannot open output file %s\n", dz->outfilename);
  1147. return(DATA_ERROR);
  1148. }
  1149. dz->outchans = dz->infile->channels;
  1150. dz->needpeaks = 1;
  1151. dz->outpeaks = (CHPEAK *) malloc(sizeof(CHPEAK) * dz->outchans);
  1152. if(dz->outpeaks==NULL)
  1153. return MEMORY_ERROR;
  1154. dz->outpeakpos = (unsigned int *) malloc(sizeof(unsigned int) * dz->outchans);
  1155. if(dz->outpeakpos==NULL)
  1156. return MEMORY_ERROR;
  1157. for(n=0;n < dz->outchans;n++){
  1158. dz->outpeaks[n].value = 0.0f;
  1159. dz->outpeaks[n].position = 0;
  1160. dz->outpeakpos[n] = 0;
  1161. }
  1162. strcpy(dz->outfilename,filename);
  1163. }
  1164. (*cmdline)++;
  1165. (*cmdlinecnt)--;
  1166. return(FINISHED);
  1167. }
  1168. /******************************** USAGE ********************************/
  1169. int usage1(void)
  1170. {
  1171. fprintf(stderr,
  1172. "\nNEW TYPES OF SPECTRAL MORPHING\n\n"
  1173. "USAGE: newmorph NAME mode infile1 infile2 outfile parameters: \n"
  1174. "\n"
  1175. "where NAME can be any one of\n"
  1176. "\n"
  1177. "newmorph newmorph2\n\n"
  1178. "Type 'newmorph newmorph' for more info on newmorph newmorph..ETC.\n");
  1179. return(USAGE_ONLY);
  1180. }
  1181. int usage2(char *str)
  1182. {
  1183. if(!strcmp(str,"newmorph")) {
  1184. fprintf(stderr,
  1185. "USAGE: newmorph newmorph 1-6 analfile1 analfile2 outanalfile\n"
  1186. " stagger startmorph endmorph exponent peaks [-e] [-n] [-f]\n"
  1187. "OR: newmorph newmorph 7 analfile1 analfile2 outanalfile\n"
  1188. " peaks outcnt [-e] [-n] [-f]\n"
  1189. "\n"
  1190. "MORPH BETWEEN DISSIMILAR SPECTRA\n"
  1191. "\n"
  1192. "STAGGER Time before entry of file 2.\n"
  1193. "STARTMORPH Time (in 1st file) at which morph starts.\n"
  1194. "ENDMORPH Time (in 1st file) at which morph ends.\n"
  1195. "EXPONENT Exponent of interpolation.\n"
  1196. "PEAKS Number of peaks to interpolate.\n"
  1197. "OUTCNT Mode 7 makes \"outcnt\" distinct output files.\n"
  1198. "-e Retain the loudness envelope of the first sound.\n"
  1199. " (in this case output cuts off when 1st sound reaches its end).\n"
  1200. "-n No interpolation of anything except the peaks.\n"
  1201. "-f Only frq is determined by peaks in sound 2.\n"
  1202. "\n"
  1203. "MODES\n"
  1204. "1 interpolate linearly (exp 1) between the average peak channels\n"
  1205. " or over a curve of increasing (exp >1) or decreasing (exp <1) slope,\n"
  1206. " simultaneously moving spectral peaks, and interpolating all remaining channels.\n"
  1207. "\n"
  1208. "2 interpolate cosinusoidally (exp 1) between the average peak channels\n"
  1209. " or over a warped cosinusoidal spline (exp not equal to 1),\n"
  1210. " simultaneously moving spectral peaks, and interpolating all remaining channels.\n"
  1211. "\n"
  1212. "3 As mode 1, using channel-by-channel calculation of peaks.\n"
  1213. "4 As mode 2, using channel-by-channel calculation of peaks.\n"
  1214. "5 Sound 1 (gradually) tuned to (averaged) harmonic field sound 2. Linear.\n"
  1215. "6 Sound 1 (gradually) tuned to (averaged) harmonic field sound 2. Cosinusoidal.\n"
  1216. "7 Sound 1 morphed towards sound2 in \"outcnt\" steps, each step a new output file.\n"
  1217. "\n");
  1218. } else if(!strcmp(str,"newmorph2")) {
  1219. fprintf(stderr,
  1220. "USAGE: newmorph newmorph2 1 analfileA outtextfile peakcnt\n"
  1221. "\n"
  1222. "Find frequencies of most prominent spectral peaks (in order of prominence).\n"
  1223. "Ouput these as a textfile list, a \"peaksfile\".\n"
  1224. "\n"
  1225. "OR: newmorph newmorph2 2-3 analfileB outanalfile peaksfile \n" /* RWD 11-20, corrected order */
  1226. " startmorph endmorph exponent peakcnt [-rrand]\n"
  1227. "\n"
  1228. "Morph between dissimilar spectra\n"
  1229. "(Peaks of analfileB move towards peaks extracted from analfileA).\n"
  1230. "\n"
  1231. "PEAKCNT Number of most-prominent source-peaks to find.\n"
  1232. "PEAKSFILE Textfile listing goal-peak frequencies: most prominent first.\n"
  1233. "STARTMORPH Time at which morph starts.\n"
  1234. "ENDMORPH Time at which morph ends.\n"
  1235. "EXPONENT Exponent of interpolation.\n"
  1236. "-r Randomisation of the goal peak frequency. (Range 0 - 1)\n"
  1237. "\n"
  1238. "MODES\n"
  1239. "\n"
  1240. "2 Sound (gradually) tuned to harmonic field specified in textfile \"peaksfile\".\n"
  1241. "3 Ditto, but interpolation is timewise-cosinusoidal.\n"
  1242. "\n");
  1243. } else
  1244. fprintf(stdout,"Unknown option '%s'\n",str);
  1245. return(USAGE_ONLY);
  1246. }
  1247. int usage3(char *str1,char *str2)
  1248. {
  1249. sprintf(errstr,"Insufficient parameters on command line.\n");
  1250. return(USAGE_ONLY);
  1251. }
  1252. /************************* redundant functions: to ensure libs compile OK *******************/
  1253. int assign_process_logic(dataptr dz)
  1254. {
  1255. return(FINISHED);
  1256. }
  1257. void set_legal_infile_structure(dataptr dz)
  1258. {}
  1259. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  1260. {
  1261. return(FINISHED);
  1262. }
  1263. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1264. {
  1265. return(FINISHED);
  1266. }
  1267. int setup_internal_arrays_and_array_pointers(dataptr dz)
  1268. {
  1269. return(FINISHED);
  1270. }
  1271. int establish_bufptrs_and_extra_buffers(dataptr dz)
  1272. {
  1273. return(FINISHED);
  1274. }
  1275. int read_special_data(char *str,dataptr dz)
  1276. {
  1277. return(FINISHED);
  1278. }
  1279. int inner_loop
  1280. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  1281. {
  1282. return(FINISHED);
  1283. }
  1284. /****************************** GET_MODE *********************************/
  1285. int get_the_mode_from_cmdline(char *str,dataptr dz)
  1286. {
  1287. char temp[200], *p;
  1288. if(sscanf(str,"%s",temp)!=1) {
  1289. sprintf(errstr,"Cannot read mode of program.\n");
  1290. return(USAGE_ONLY);
  1291. }
  1292. p = temp + strlen(temp) - 1;
  1293. while(p >= temp) {
  1294. if(!isdigit(*p)) {
  1295. fprintf(stderr,"Invalid mode of program entered.\n");
  1296. return(USAGE_ONLY);
  1297. }
  1298. p--;
  1299. }
  1300. if(sscanf(str,"%d",&dz->mode)!=1) {
  1301. fprintf(stderr,"Cannot read mode of program.\n");
  1302. return(USAGE_ONLY);
  1303. }
  1304. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1305. fprintf(stderr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1306. return(USAGE_ONLY);
  1307. }
  1308. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1309. return(FINISHED);
  1310. }
  1311. /***************************** SPECMORPH ***********************/
  1312. int specmorph(dataptr dz)
  1313. {
  1314. #define FILE0 (0)
  1315. #define FILE1 (1)
  1316. int exit_status, cc, vc, peakcnt, n, m;
  1317. int windows_to_buf0 = 0, windows_to_buf1 = 0, minsize;
  1318. int start_of_morph = 0, end_of_morph = 0;
  1319. double alen = 0.0, interpratio;
  1320. int window_position_in_current_buf_for_file1;
  1321. int total_windows_processed_from_file0 = 0,
  1322. total_windows_processed_from_file1 = 0;
  1323. int finished_file0 = 0,
  1324. finished_file1 = 0;
  1325. int total_windows_in_file0 = dz->insams[0]/dz->wanted;
  1326. int total_windows_in_file1 = dz->insams[1]/dz->wanted;
  1327. double *peakfrq1 = dz->parray[3], *peakamp2 = dz->parray[2];
  1328. char temp[64];
  1329. int stype = SAMP_FLOAT;
  1330. if(dz->process == SPECMORPH) {
  1331. if (dz->mode == 6) {
  1332. start_of_morph = 0;
  1333. minsize = dz->insams[0];
  1334. for(n=1;n<dz->infilecnt;n++)
  1335. minsize = min(dz->insams[n],minsize);
  1336. dz->wlength = minsize/dz->wanted;
  1337. end_of_morph = dz->wlength;
  1338. } else {
  1339. start_of_morph = dz->iparam[NMPH_ASTT];
  1340. end_of_morph = dz->iparam[NMPH_AEND];
  1341. alen = (double)(dz->iparam[NMPH_AEND] - dz->iparam[NMPH_ASTT]);
  1342. }
  1343. } else if(dz->mode > 0) { // SPECMORPH2
  1344. start_of_morph = dz->iparam[NMPH_ASTT];
  1345. end_of_morph = dz->iparam[NMPH_AEND];
  1346. alen = (double)(dz->iparam[NMPH_AEND] - dz->iparam[NMPH_ASTT]);
  1347. }
  1348. if(dz->process == SPECMORPH && dz->mode != 6)
  1349. window_position_in_current_buf_for_file1 = -dz->iparam[NMPH_STAG];
  1350. else
  1351. window_position_in_current_buf_for_file1 = 0;
  1352. fflush(stdout);
  1353. if(dz->process == SPECMORPH && dz->mode < 2) {
  1354. fprintf(stdout,"INFO: Searching for spectral peaks\n");
  1355. if((exit_status = find_the_average_spectral_peaks(dz)) < 0)
  1356. return exit_status;
  1357. } else if((dz->process == SPECMORPH && dz->mode >= 4 && dz->mode != 6) || dz->process == SPECMORPH2) {
  1358. if(dz->process == SPECMORPH2 && dz->mode > 0)
  1359. fprintf(stdout,"INFO: Assigning spectral peaks\n");
  1360. else
  1361. fprintf(stdout,"INFO: Searching for spectral peaks\n");
  1362. if((exit_status = find_the_average_spectral_peaks_and_correlates(dz)) < 0)
  1363. return exit_status;
  1364. }
  1365. if(dz->process == SPECMORPH2 && dz->mode == 0) {
  1366. peakcnt = dz->iparam[NMPH_APKS];
  1367. for(cc = 0;cc < peakcnt;cc++) {
  1368. if((fprintf(dz->fp,"%lf\n",peakamp2[cc]))<1) {
  1369. sprintf(errstr,"fput_brk(): Problem writing peak data to file.\n");
  1370. return(PROGRAM_ERROR);
  1371. }
  1372. }
  1373. return FINISHED;
  1374. }
  1375. if(dz->process == SPECMORPH && dz->mode == 6) {
  1376. for(n = 0,m = 1; n < dz->iparam[NMPH_OCNT];n++,m++) {
  1377. fprintf(stdout,"INFO: Generating file %d\n",m);
  1378. fflush(stdout);
  1379. interpratio = (double)m/(double)(dz->iparam[NMPH_OCNT] + 2);
  1380. dz->total_windows = 0;
  1381. while(dz->total_windows < end_of_morph) {
  1382. if((exit_status = read_specific_file_to_specific_buf(FILE0,&windows_to_buf0,dz->flbufptr[0],dz))<0)
  1383. return(exit_status);
  1384. if((exit_status = read_specific_file_to_specific_buf(FILE1,&windows_to_buf1,dz->flbufptr[1],dz))<0)
  1385. return(exit_status);
  1386. newmorph_core(interpratio,dz);
  1387. if((exit_status = write_exact_samps(dz->bigfbuf,dz->big_fsize,dz))<0)
  1388. return(exit_status);
  1389. dz->total_windows++;
  1390. }
  1391. dz->total_samps_written = 0;
  1392. if((exit_status = headwrite(dz->ofd,dz))<0)
  1393. return(exit_status);
  1394. if(sndcloseEx(dz->ofd) < 0) {
  1395. fprintf(stdout,"WARNING: Can't close output soundfile %s\n",dz->outfilename);
  1396. fflush(stdout);
  1397. }
  1398. dz->ofd = -1;
  1399. if(m < dz->iparam[NMPH_OCNT]) {
  1400. if((exit_status = reset_peak_finder(dz))<0)
  1401. return(exit_status);
  1402. strcpy(dz->outfilename,dz->wordstor[0]);
  1403. sprintf(temp,"%d.ana",m);
  1404. strcat(dz->outfilename,temp);
  1405. if((dz->ofd = sndcreat_formatted(dz->outfilename,dz->outfilesize,stype,
  1406. dz->infile->channels,dz->infile->srate,CDP_CREATE_NORMAL)) < 0) {
  1407. sprintf(errstr,"Cannot open output file %s : %s\n", dz->outfilename,sferrstr());
  1408. return(DATA_ERROR);
  1409. }
  1410. if(sndseekEx(dz->ifd[0],0,0)<0) {
  1411. sprintf(errstr,"Sound seek failed in file 1: after file %d\n",m);
  1412. return SYSTEM_ERROR;
  1413. }
  1414. if(sndseekEx(dz->ifd[1],0,0)<0) {
  1415. sprintf(errstr,"Sound seek failed in file 2: after file %d\n",m);
  1416. return SYSTEM_ERROR;
  1417. }
  1418. }
  1419. }
  1420. return(FINISHED);
  1421. }
  1422. dz->total_windows = 0;
  1423. fprintf(stdout,"INFO: Doing the morph\n");
  1424. fflush(stdout);
  1425. do {
  1426. if(dz->process == SPECMORPH && dz->mode < 4) {
  1427. if(!finished_file0) {
  1428. if(total_windows_processed_from_file0 >= end_of_morph) {
  1429. finished_file0 = 1;
  1430. } else {
  1431. if((exit_status = read_specific_file_to_specific_buf(FILE0,&windows_to_buf0,dz->flbufptr[0],dz))<0)
  1432. return(exit_status);
  1433. }
  1434. }
  1435. if(window_position_in_current_buf_for_file1>=0) {
  1436. if((exit_status = read_specific_file_to_specific_buf(FILE1,&windows_to_buf1,dz->flbufptr[1],dz))<0)
  1437. return(exit_status);
  1438. if(total_windows_processed_from_file0 >= start_of_morph) {
  1439. if(!finished_file0) {
  1440. if((exit_status = do_newmorph(alen,dz))<0)
  1441. return(exit_status);
  1442. } else if(dz->vflag[0]) {
  1443. if(total_windows_processed_from_file0 < total_windows_in_file0)
  1444. do_morphenv(dz);
  1445. else
  1446. break;
  1447. } else
  1448. memmove((char *)dz->flbufptr[0],(char *)dz->flbufptr[1],(size_t)(dz->wanted * sizeof(float)));
  1449. }
  1450. if(++total_windows_processed_from_file1 >= total_windows_in_file1)
  1451. finished_file1 = 1;
  1452. }
  1453. if((exit_status = write_exact_samps(dz->bigfbuf,dz->big_fsize,dz))<0)
  1454. return(exit_status);
  1455. total_windows_processed_from_file0++;
  1456. window_position_in_current_buf_for_file1++;
  1457. } else {
  1458. if(total_windows_processed_from_file0 < total_windows_in_file0) {
  1459. if((exit_status = read_specific_file_to_specific_buf(FILE0,&windows_to_buf0,dz->flbufptr[0],dz))<0)
  1460. return(exit_status);
  1461. }
  1462. if(dz->process == SPECMORPH) {
  1463. if(window_position_in_current_buf_for_file1>=0) {
  1464. if((exit_status = read_specific_file_to_specific_buf(FILE1,&windows_to_buf1,dz->flbufptr[1],dz))<0)
  1465. return(exit_status);
  1466. if(total_windows_processed_from_file0 >= start_of_morph) {
  1467. if(total_windows_processed_from_file0 < end_of_morph ) {
  1468. if((exit_status = do_newmorph(alen,dz))<0)
  1469. return(exit_status);
  1470. } else if(total_windows_processed_from_file0 < total_windows_in_file0) {
  1471. for(cc=0,vc = 0;cc < dz->clength;cc++,vc+=2)
  1472. dz->flbufptr[0][FREQ] = (float)peakfrq1[cc];
  1473. } else
  1474. break;
  1475. }
  1476. if(++total_windows_processed_from_file1 >= total_windows_in_file1)
  1477. finished_file1 = 1;
  1478. }
  1479. } else {
  1480. if(total_windows_processed_from_file0 >= start_of_morph) {
  1481. if(total_windows_processed_from_file0 < end_of_morph ) {
  1482. if((exit_status = do_newmorph(alen,dz))<0)
  1483. return(exit_status);
  1484. } else if(total_windows_processed_from_file0 < total_windows_in_file0) {
  1485. for(cc=0,vc = 0;cc < dz->clength;cc++,vc+=2)
  1486. dz->flbufptr[0][FREQ] = (float)peakfrq1[cc];
  1487. } else
  1488. break;
  1489. }
  1490. }
  1491. if((exit_status = write_exact_samps(dz->bigfbuf,dz->big_fsize,dz))<0)
  1492. return(exit_status);
  1493. total_windows_processed_from_file0++;
  1494. window_position_in_current_buf_for_file1++;
  1495. }
  1496. } while(!finished_file1);
  1497. return(FINISHED);
  1498. }
  1499. /***************************** READ_SPECIFIC_FILE_TO_SPECIFIC_BUF ***********************/
  1500. int read_specific_file_to_specific_buf(int fileno,int *windows_to_buf,float *floatbuf,dataptr dz)
  1501. {
  1502. int samps_read;
  1503. if((samps_read = fgetfbufEx(floatbuf,dz->big_fsize,dz->ifd[fileno],0))<=0) {
  1504. if(samps_read<0) {
  1505. sprintf(errstr,"Sound read error.\n");
  1506. return(SYSTEM_ERROR);
  1507. } else {
  1508. sprintf(errstr,"Error in buffer arithmetic in read_specific_file_to_specific_buf()\n");
  1509. return(PROGRAM_ERROR);
  1510. }
  1511. }
  1512. *windows_to_buf = samps_read/dz->wanted;
  1513. return(FINISHED);
  1514. }
  1515. /*************************************** DO_NEWMORPH ******************************************/
  1516. int do_newmorph(double alen, dataptr dz)
  1517. {
  1518. int exit_status;
  1519. double interpratio, time;
  1520. interpratio = (double)dz->total_windows/(double)alen;
  1521. if(dz->process == SPECMORPH) {
  1522. switch(dz->mode) {
  1523. case(NMPH_LINE):
  1524. case(NMPH_LINEX):
  1525. interpratio = pow(interpratio,dz->param[NMPH_AEXP]);
  1526. break;
  1527. case(NMPH_LINET):
  1528. interpratio = pow(interpratio,dz->param[NMPH_AEXP]);
  1529. time = (double)dz->total_windows * dz->frametime;
  1530. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  1531. return(exit_status);
  1532. break;
  1533. case(NMPH_COSIN):
  1534. case(NMPH_COSINX):
  1535. if((exit_status = warped_cosin(&interpratio,dz->param[NMPH_AEXP],dz))<0)
  1536. return(exit_status);
  1537. break;
  1538. case(NMPH_COSINT):
  1539. if((exit_status = warped_cosin(&interpratio,dz->param[NMPH_AEXP],dz))<0)
  1540. return(exit_status);
  1541. time = (double)dz->total_windows * dz->frametime;
  1542. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  1543. return(exit_status);
  1544. break;
  1545. }
  1546. } else {
  1547. switch(dz->mode) {
  1548. case(1):
  1549. time = (double)dz->total_windows * dz->frametime;
  1550. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  1551. return(exit_status);
  1552. break;
  1553. case(2):
  1554. if((exit_status = warped_cosin(&interpratio,dz->param[NMPH_AEXP],dz))<0)
  1555. return(exit_status);
  1556. time = (double)dz->total_windows * dz->frametime;
  1557. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  1558. return(exit_status);
  1559. break;
  1560. }
  1561. }
  1562. newmorph_core(interpratio,dz);
  1563. dz->total_windows++;
  1564. return(FINISHED);
  1565. }
  1566. /****************************** WARPED_COSIN ******************************/
  1567. int warped_cosin(double *interpratio,double exponent,dataptr dz)
  1568. {
  1569. int exit_status;
  1570. if((exit_status = cosin_lookup(interpratio,dz))<0)
  1571. return(exit_status);
  1572. if(*interpratio < .5) {
  1573. if((exit_status = time_warp(interpratio,exponent,dz))<0)
  1574. return(exit_status);
  1575. } else if(*interpratio > .5) {
  1576. *interpratio = 1.0 - *interpratio;
  1577. if((exit_status = time_warp(interpratio,exponent,dz))<0)
  1578. return(exit_status);
  1579. *interpratio = 1.0 - *interpratio;
  1580. }
  1581. return(FINISHED);
  1582. }
  1583. /****************************** TIME_WARP ******************************/
  1584. int time_warp(double *interpratio,double exponent,dataptr dz)
  1585. {
  1586. *interpratio = min(*interpratio * 2.0,1.0);
  1587. *interpratio = pow(*interpratio,exponent);
  1588. *interpratio /= 2.0;
  1589. return(FINISHED);
  1590. }
  1591. /****************************** COSIN_LOOKUP ******************************/
  1592. int cosin_lookup(double *interpratio,dataptr dz)
  1593. {
  1594. double dtabindex = *interpratio * (double)NMPH_COSTABSIZE;
  1595. int tabindex = (int)floor(dtabindex); /* TRUNCATE */
  1596. double frac = dtabindex - (double)tabindex;
  1597. double thisval = dz->parray[0][tabindex++]; /* table has wraparound pnt */
  1598. double nextval = dz->parray[0][tabindex];
  1599. double diff = nextval - thisval;
  1600. double step = diff * frac;
  1601. *interpratio = thisval + step;
  1602. return(FINISHED);
  1603. }
  1604. /********************** CHECK_CONSISTENCY_OF_NEWMORPH_PARAMS **********************/
  1605. int check_consistency_of_newmorph_params(dataptr dz)
  1606. {
  1607. double duration0, duration1 = 0.0;
  1608. int morphing = 0;
  1609. if(dz->process == SPECMORPH && dz->mode == 6)
  1610. return FINISHED;
  1611. duration0 = (dz->insams[0]/dz->wanted) * dz->frametime;
  1612. if(dz->process == SPECMORPH) {
  1613. duration1 = ((dz->insams[1]/dz->wanted) * dz->frametime) + dz->param[NMPH_STAG];
  1614. if(dz->param[NMPH_ASTT] < dz->param[NMPH_STAG]) {
  1615. sprintf(errstr,"start of amp interpolation is set before entry of 2nd soundfile.\n");
  1616. return(DATA_ERROR);
  1617. }
  1618. dz->iparam[NMPH_STAG] = round(dz->param[NMPH_STAG]/dz->frametime);
  1619. }
  1620. if(dz->process == SPECMORPH || dz->mode > 0) // i.e. also if process is SPECMORPH2 and we're not in mode 0
  1621. morphing = 1;
  1622. if(morphing) {
  1623. if(dz->process == SPECMORPH) {
  1624. if(dz->param[NMPH_AEND] > duration0 || dz->param[NMPH_AEND] > duration1) {
  1625. sprintf(errstr, "end of interpolation is beyond end of one of soundfiles.\n");
  1626. return(DATA_ERROR);
  1627. }
  1628. } else {
  1629. if(dz->param[NMPH_AEND] > duration0) {
  1630. fprintf(stdout, "WARNING : end of interpolation beyond end of soundfile, truncated to file duration.\n");
  1631. fflush(stdout);
  1632. dz->param[NMPH_AEND] = duration0;
  1633. }
  1634. }
  1635. if(dz->param[NMPH_AEND] <= dz->param[NMPH_ASTT]) {
  1636. sprintf(errstr,"interpolation starttime is after (or equal to) its endtime.\n");
  1637. return(USER_ERROR);
  1638. }
  1639. dz->iparam[NMPH_AEND] = round(dz->param[NMPH_AEND]/dz->frametime); // Convert to count in windows
  1640. dz->iparam[NMPH_ASTT] = round(dz->param[NMPH_ASTT]/dz->frametime);
  1641. }
  1642. return(FINISHED);
  1643. }
  1644. /************************** MORPH_PREPROCESS ******************************/
  1645. int morph_preprocess(dataptr dz)
  1646. {
  1647. int exit_status;
  1648. if((dz->process == SPECMORPH && (dz->mode==NMPH_COSIN || dz->mode==NMPH_COSINX || dz->mode==NMPH_COSINT)) \
  1649. || (dz->process == SPECMORPH2 && dz->mode==2)) {
  1650. if((exit_status = make_newmorph_costable(dz))<0)
  1651. return(exit_status);
  1652. }
  1653. if(dz->process == SPECMORPH2 && dz->mode > 0) {
  1654. if((exit_status = create_peakcnt_stores(dz))<0)
  1655. return(exit_status);
  1656. return read_the_special_data(dz);
  1657. }
  1658. return create_peakcnt_stores(dz);
  1659. }
  1660. /************************ MAKE_MORPH_COSTABLE ***********************/
  1661. int make_newmorph_costable(dataptr dz)
  1662. {
  1663. double d, d2;
  1664. int n;
  1665. d = PI/(double)NMPH_COSTABSIZE;
  1666. if((dz->parray[0] = (double *)malloc((NMPH_COSTABSIZE + 1) * sizeof(double)))==NULL) {
  1667. sprintf(errstr,"INSUFFICIENT MEMORY for morph cosin table.\n");
  1668. return(MEMORY_ERROR);
  1669. }
  1670. for(n=0;n<NMPH_COSTABSIZE;n++) {
  1671. d2 = cos((double)n * d);
  1672. d2 += 1.0;
  1673. d2 /= 2.0;
  1674. d2 = 1.0 - d2;
  1675. d2 = min(d2,1.0); /* trap calc errors */
  1676. d2 = max(d2,0.0);
  1677. dz->parray[0][n] = d2;
  1678. }
  1679. dz->parray[0][n++] = 1.0; /* wrap-around point */
  1680. return(FINISHED);
  1681. }
  1682. /************************ CREATE_PEAKCNT_STORES ***********************/
  1683. int create_peakcnt_stores(dataptr dz)
  1684. {
  1685. if((dz->iparray[0] = (int *)malloc(dz->clength * sizeof(int)))==NULL) {
  1686. sprintf(errstr,"INSUFFICIENT MEMORY for 1st peak count store.\n");
  1687. return(MEMORY_ERROR);
  1688. }
  1689. if((dz->iparray[1] = (int *)malloc(dz->clength * sizeof(int)))==NULL) {
  1690. sprintf(errstr,"INSUFFICIENT MEMORY for 2nd peak count store.\n");
  1691. return(MEMORY_ERROR);
  1692. }
  1693. if((dz->iparray[2] = (int *)malloc(dz->clength * sizeof(int)))==NULL) {
  1694. sprintf(errstr,"INSUFFICIENT MEMORY for peak location store.\n");
  1695. return(MEMORY_ERROR);
  1696. }
  1697. if((dz->iparray[3] = (int *)malloc(dz->clength * sizeof(int)))==NULL) {
  1698. sprintf(errstr,"INSUFFICIENT MEMORY for peak location store.\n");
  1699. return(MEMORY_ERROR);
  1700. }
  1701. if((dz->parray[1] = (double *)malloc(dz->clength * sizeof(double)))==NULL) {
  1702. sprintf(errstr,"INSUFFICIENT MEMORY for peak levels store.\n");
  1703. return(MEMORY_ERROR);
  1704. }
  1705. if((dz->parray[2] = (double *)malloc(dz->clength * sizeof(double)))==NULL) {
  1706. sprintf(errstr,"INSUFFICIENT MEMORY for peak levels store.\n");
  1707. return(MEMORY_ERROR);
  1708. }
  1709. if((dz->parray[3] = (double *)malloc(dz->clength * sizeof(double)))==NULL) {
  1710. sprintf(errstr,"INSUFFICIENT MEMORY for peak levels store.\n");
  1711. return(MEMORY_ERROR);
  1712. }
  1713. if((dz->parray[4] = (double *)malloc(dz->clength * sizeof(double)))==NULL) {
  1714. sprintf(errstr,"INSUFFICIENT MEMORY for peak levels store.\n");
  1715. return(MEMORY_ERROR);
  1716. }
  1717. return(FINISHED);
  1718. }
  1719. /******************** FIND_THE_AVERAGE_SPECTRAL_PEAKS ****************************/
  1720. int find_the_average_spectral_peaks(dataptr dz)
  1721. {
  1722. int exit_status;
  1723. double *peakamp1 = dz->parray[1], *peakamp2 = dz->parray[2];
  1724. double *peakfrq1 = dz->parray[3], *peakfrq2 = dz->parray[4];
  1725. int *peakno1 = dz->iparray[1], *peakno2 = dz->iparray[2];
  1726. int peakcnt = dz->iparam[NMPH_APKS];
  1727. int n, m, k;
  1728. double tempamp, tempfrq, peaklo, peakhi;
  1729. int windows_to_buf = 0, samps_left, wcnt;
  1730. int totwcnt = 0, cc, vc, done, peaks_possible, peaks_possible1 = 0, peaks_possible2 = 0;
  1731. for(n=0;n < dz->clength;n++) {
  1732. peakamp1[n] = 0.0;
  1733. peakno1[n] = n * 2;
  1734. peakamp2[n] = 0.0;
  1735. peakno2[n] = n * 2;
  1736. }
  1737. samps_left = dz->insams[0];
  1738. while(samps_left) {
  1739. if((exit_status = read_specific_file_to_specific_buf(FILE0,&windows_to_buf,dz->flbufptr[0],dz))<0)
  1740. return(exit_status);
  1741. samps_left -= windows_to_buf * dz->wanted;
  1742. for(wcnt =0;wcnt < windows_to_buf;wcnt++) {
  1743. if(totwcnt == 0) {
  1744. totwcnt++;
  1745. continue;
  1746. }
  1747. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2) {
  1748. peakamp1[cc] += fabs(dz->flbufptr[0][AMPP]);
  1749. if(totwcnt == 1)
  1750. peakfrq1[cc] = fabs(dz->flbufptr[0][FREQ]); // Get total amplitude for each channel
  1751. else
  1752. peakfrq1[cc] = (peakfrq1[cc] + fabs(dz->flbufptr[0][FREQ]))/2.0; // Get average freq for each channel
  1753. totwcnt++;
  1754. }
  1755. }
  1756. }
  1757. for(n=0;n<dz->clength;n++) {
  1758. if(peakamp1[n] > 0.0)
  1759. peaks_possible1++;
  1760. }
  1761. for(n=0;n < dz->clength - 1;n++) { // Sort into loudness order
  1762. for(m = n+1; m < dz->clength;m++) {
  1763. if(peakamp1[n] < peakamp1[m]) {
  1764. tempamp = peakamp1[n];
  1765. tempfrq = peakfrq1[n];
  1766. k = peakno1[n];
  1767. peakamp1[n] = peakamp1[m];
  1768. peakfrq1[n] = peakfrq1[m];
  1769. peakno1[n] = peakno1[m];
  1770. peakamp1[m] = tempamp;
  1771. peakfrq1[m] = tempfrq;
  1772. peakno1[m] = k;
  1773. }
  1774. }
  1775. }
  1776. if(sndseekEx(dz->ifd[0],0,0)<0) {
  1777. sprintf(errstr,"Sound seek failed in file 1\n");
  1778. return SYSTEM_ERROR;
  1779. }
  1780. samps_left = dz->insams[1]; // Same with 2nd file
  1781. totwcnt = 0;
  1782. while(samps_left) {
  1783. if((exit_status = read_specific_file_to_specific_buf(FILE1,&windows_to_buf,dz->bigfbuf,dz))<0)
  1784. return(exit_status);
  1785. samps_left -= windows_to_buf * dz->wanted;
  1786. for(wcnt =0;wcnt < windows_to_buf;wcnt++) {
  1787. if(totwcnt == 0) {
  1788. totwcnt++;
  1789. continue;
  1790. }
  1791. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2) {
  1792. peakamp2[cc] += fabs(dz->flbufptr[0][AMPP]);
  1793. if(totwcnt == 1)
  1794. peakfrq2[cc] = fabs(dz->flbufptr[0][FREQ]);
  1795. else
  1796. peakfrq2[cc] = (peakfrq2[cc] + fabs(dz->flbufptr[0][FREQ]))/2.0;
  1797. }
  1798. totwcnt++;
  1799. }
  1800. }
  1801. for(n=0;n < dz->clength - 1;n++) {
  1802. for(m = n+1; m < dz->clength;m++) {
  1803. if(peakamp2[m] > peakamp2[n]) {
  1804. tempamp = peakamp2[n];
  1805. tempfrq = peakfrq2[n];
  1806. k = peakno2[n];
  1807. peakamp2[n] = peakamp2[m];
  1808. peakfrq2[n] = peakfrq2[m];
  1809. peakno2[n] = peakno2[m];
  1810. peakamp2[m] = tempamp;
  1811. peakfrq2[m] = tempfrq;
  1812. peakno2[m] = k;
  1813. }
  1814. }
  1815. }
  1816. if(sndseekEx(dz->ifd[1],0,0)<0) {
  1817. sprintf(errstr,"Sound seek failed in file 2\n");
  1818. return SYSTEM_ERROR;
  1819. }
  1820. // Eliminate equivalents of peak in adjacent channels
  1821. done = 0;
  1822. for(n=0;n<dz->clength-1;n++) {
  1823. peaklo = peakno1[n] * dz->chwidth;
  1824. peakhi = peaklo + dz->halfchwidth;
  1825. peaklo = peaklo - dz->halfchwidth;
  1826. for(m=n+1;m<dz->clength;m++) {
  1827. if(m >= peakcnt) { // once we have peakcnt valid peaks, quit
  1828. done = 1;
  1829. break;
  1830. }
  1831. if(peakfrq1[m] > peaklo && peakfrq1[m] < peakhi) { // equivalent peak (with lower amp, as amps sorted into descending order)
  1832. for(k = m+1; k < dz->clength; k++) {
  1833. peakamp1[k-1] = peakamp1[k]; // eliminate
  1834. peakfrq1[k-1] = peakfrq1[k];
  1835. peakno1[k-1] = peakno1[k];
  1836. }
  1837. m--; // stay at same place in loop
  1838. }
  1839. }
  1840. if(done)
  1841. break;
  1842. }
  1843. done = 0;
  1844. // Same for 2nd file
  1845. for(n=0;n<dz->clength-1;n++) {
  1846. peaklo = peakno2[n] * dz->chwidth;
  1847. peakhi = peaklo + dz->halfchwidth;
  1848. peaklo = peaklo - dz->halfchwidth;
  1849. for(m=n+1;m<dz->clength;m++) {
  1850. if(m >= peakcnt) {
  1851. done = 1;
  1852. break;
  1853. }
  1854. if(peakfrq2[m] > peaklo && peakfrq2[m] < peakhi) {
  1855. for(k = m+1; k < dz->clength; k++) {
  1856. peakamp2[k-1] = peakamp2[k];
  1857. peakfrq2[k-1] = peakfrq2[k];
  1858. peakno2[k-1] = peakno2[k];
  1859. }
  1860. m--;
  1861. }
  1862. }
  1863. if(done)
  1864. break;
  1865. }
  1866. for(n=0;n<dz->clength;n++) {
  1867. if(peakamp1[n] > 0.0)
  1868. peaks_possible1++;
  1869. if(peakamp2[n] > 0.0)
  1870. peaks_possible2++;
  1871. }
  1872. peaks_possible = min(peaks_possible1,peaks_possible2);
  1873. if(peaks_possible < peakcnt) {
  1874. fprintf(stdout,"WARNING: Less peaks in spectrum (%d) than peakcnt specified (%d) : reducing peakcnt\n",peaks_possible,peakcnt);
  1875. fflush(stdout);
  1876. peakcnt = peaks_possible;
  1877. dz->iparam[NMPH_APKS] = peakcnt;
  1878. }
  1879. // Finally : sort peaks into ascending order
  1880. for(n=0;n<peakcnt-1;n++) {
  1881. for(m = n+1;m < peakcnt;m++) {
  1882. if(peakno1[n] > peakno1[m]) {
  1883. k = peakno1[n];
  1884. peakno1[n] = peakno1[m];
  1885. peakno1[m] = k;
  1886. }
  1887. if(peakno2[n] > peakno2[m]) {
  1888. k = peakno2[n];
  1889. peakno2[n] = peakno2[m];
  1890. peakno2[m] = k;
  1891. }
  1892. }
  1893. }
  1894. return FINISHED;
  1895. }
  1896. /******************** NEWMORPH_CORE ****************************/
  1897. void newmorph_core(double interpratio,dataptr dz)
  1898. {
  1899. int *peakno1 = dz->iparray[1], *peakno2 = dz->iparray[2], *newpeak = dz->iparray[3];
  1900. double *peakfrq1 = dz->parray[3];
  1901. int n, cc, vc, origvc, done, peakcnt = dz->iparam[NMPH_APKS];
  1902. double amp, amp0 = 0.0, amp1 = 0.0, frq, frq0, frq1, hiamp, loamp, totamp_before, totamp_after, normaliser, rr, gapup, gapdn;
  1903. memset((char *)dz->flbufptr[2],0,dz->wanted * sizeof(float));
  1904. if((dz->process == SPECMORPH && dz->mode >= 4) || dz->process == SPECMORPH2) {
  1905. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2) { // Now do normal interpolation
  1906. frq0 = dz->flbufptr[0][FREQ];
  1907. frq1 = peakfrq1[cc];
  1908. if(dz->param[NMPH_RAND] > 0.0) {
  1909. if(cc < dz->clength - 1)
  1910. gapup = peakfrq1[cc+1] - peakfrq1[cc];
  1911. else
  1912. gapup = min(dz->nyquist - peakfrq1[cc], peakfrq1[cc] - peakfrq1[cc-1]);
  1913. if(cc > 0)
  1914. gapdn = peakfrq1[cc] - peakfrq1[cc-1];
  1915. else
  1916. gapdn = min(peakfrq1[cc], peakfrq1[cc+1] - peakfrq1[cc]);
  1917. rr = ((drand48() * 2.0) - 1.0) * 0.5;
  1918. if(rr >= 0.0)
  1919. frq1 += (rr * dz->param[NMPH_RAND] * gapup);
  1920. else
  1921. frq1 -= (rr * dz->param[NMPH_RAND] * gapdn);
  1922. }
  1923. frq = (frq1 - frq0) * interpratio;
  1924. frq += frq0;
  1925. dz->flbufptr[0][FREQ] = (float)frq;
  1926. }
  1927. return;
  1928. }
  1929. if(dz->mode > 1)
  1930. find_the_spectral_peaks(&peakcnt,dz);
  1931. for(n=0;n<peakcnt;n++) { // Interpolate between peak values, storing in 3rd buffer
  1932. vc = peakno1[n];
  1933. amp0 = dz->flbufptr[0][AMPP];
  1934. frq0 = dz->flbufptr[0][FREQ];
  1935. vc = peakno2[n];
  1936. amp1 = dz->flbufptr[1][AMPP];
  1937. frq1 = dz->flbufptr[1][FREQ];
  1938. amp = (amp1 - amp0) * interpratio;
  1939. amp += amp0;
  1940. frq = (frq1 - frq0) * interpratio;
  1941. frq += frq0;
  1942. cc = (int)round(frq/dz->chwidth);
  1943. vc = cc * 2;
  1944. if(dz->vflag[2]) // If only the frq is determined by sound 2, retain the sound 1 (peak) amp
  1945. dz->flbufptr[2][AMPP] = dz->flbufptr[0][AMPP];
  1946. else
  1947. dz->flbufptr[2][AMPP] = (float)amp;
  1948. dz->flbufptr[2][FREQ] = (float)frq;
  1949. newpeak[n] = vc;
  1950. }
  1951. totamp_before = 0.0;
  1952. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2) { // Now do normal interpolation
  1953. totamp_before += dz->flbufptr[0][AMPP];
  1954. frq0 = dz->flbufptr[0][FREQ];
  1955. frq1 = dz->flbufptr[1][FREQ];
  1956. frq = (frq1 - frq0) * interpratio;
  1957. frq += frq0;
  1958. origvc = vc;
  1959. done = 0;
  1960. for(n=0;n < peakcnt; n++) { // For channels with peak value, but not in same channel as peak in other file,
  1961. if(vc == peakno1[n] && vc != peakno2[n] ) { // give (originally a peak) an amp intermediate between adjacent channels (kludge!!)
  1962. vc -= 2; // IF peak moces out oc channel, then orig peak-channel will not be too loud (hopefully)
  1963. if(vc < 0) // If peaks doesn't move between chans, this interpd-val will be overwritten below.
  1964. loamp = 0.0;
  1965. else
  1966. loamp = dz->flbufptr[0][AMPP];
  1967. vc += 4;
  1968. if(vc >= dz->wanted)
  1969. hiamp = 0.0;
  1970. else
  1971. hiamp = dz->flbufptr[0][AMPP];
  1972. amp0 = (loamp + hiamp)/2.0;
  1973. vc = origvc;
  1974. amp1 = dz->flbufptr[1][AMPP];
  1975. done = 1;
  1976. break;
  1977. } else if(vc == peakno2[n] && vc != peakno1[n] ) { // ditto
  1978. vc -= 2;
  1979. if(vc < 0)
  1980. loamp = 0.0;
  1981. else
  1982. loamp = dz->flbufptr[1][AMPP];
  1983. vc += 4;
  1984. if(vc >= dz->wanted)
  1985. hiamp = 0.0;
  1986. else
  1987. hiamp = dz->flbufptr[1][AMPP];
  1988. amp1 = (loamp + hiamp)/2.0;
  1989. vc = origvc;
  1990. amp0 = dz->flbufptr[0][AMPP];
  1991. done = 1;
  1992. break;
  1993. }
  1994. }
  1995. vc = origvc;
  1996. if(dz->vflag[1]) { // if -n flag set (no other interps), only interp the "done" (peak) chans
  1997. if(done) { // leaving all other channels with their file1 values
  1998. amp = (amp1 - amp0) * interpratio;
  1999. amp += amp0;
  2000. dz->flbufptr[0][AMPP] = (float)amp;
  2001. dz->flbufptr[0][FREQ] = (float)frq;
  2002. }
  2003. } else {
  2004. if (!done) { // In standard case (NOT -n): if no pre-interp already done
  2005. amp0 = dz->flbufptr[0][AMPP]; // set up original chans for normal interp
  2006. amp1 = dz->flbufptr[1][AMPP];
  2007. }
  2008. amp = (amp1 - amp0) * interpratio; // Now do the interp on pre-interpd (peak) chans AND on other chans
  2009. amp += amp0;
  2010. dz->flbufptr[0][AMPP] = (float)amp;
  2011. dz->flbufptr[0][FREQ] = (float)frq;
  2012. }
  2013. } // THEN
  2014. for(n=0;n < peakcnt; n++) { // Overwrite normal interp with interpd-peaks
  2015. vc = newpeak[n];
  2016. dz->flbufptr[0][AMPP] = dz->flbufptr[2][AMPP];
  2017. dz->flbufptr[0][FREQ] = dz->flbufptr[2][FREQ];
  2018. }
  2019. if(dz->vflag[0]) { // If -e, set window level to orig file1 window-level
  2020. totamp_after = 0.0;
  2021. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2)
  2022. totamp_after += dz->flbufptr[0][AMPP];
  2023. if(totamp_after > 0) {
  2024. normaliser = totamp_before/totamp_after;
  2025. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2)
  2026. dz->flbufptr[0][AMPP] = (float)(dz->flbufptr[0][AMPP] * normaliser);
  2027. }
  2028. }
  2029. }
  2030. /**************************** ALLOCATE_NEW_TRIPLE_BUFFER ******************************/
  2031. int allocate_new_triple_buffer(dataptr dz)
  2032. {
  2033. unsigned int buffersize;
  2034. if(dz->bptrcnt < 4) {
  2035. sprintf(errstr,"Insufficient bufptrs established in allocate_new_triple_buffer()\n");
  2036. return(PROGRAM_ERROR);
  2037. }
  2038. //TW REVISED: buffers don't need to be multiples of secsize
  2039. buffersize = dz->wanted;
  2040. dz->buflen = buffersize;
  2041. if((dz->bigfbuf = (float*)malloc(dz->buflen*3 * sizeof(float)))==NULL) {
  2042. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n");
  2043. return(MEMORY_ERROR);
  2044. }
  2045. dz->big_fsize = dz->buflen;
  2046. dz->flbufptr[0] = dz->bigfbuf;
  2047. dz->flbufptr[1] = dz->flbufptr[0] + dz->big_fsize;
  2048. dz->flbufptr[2] = dz->flbufptr[1] + dz->big_fsize;
  2049. dz->flbufptr[3] = dz->flbufptr[2] + dz->big_fsize;
  2050. return(FINISHED);
  2051. }
  2052. /******************** FIND_THE_AVERAGE_PEAKS ****************************/
  2053. void find_the_spectral_peaks(int *peakcnt,dataptr dz)
  2054. {
  2055. double *peakamp1 = dz->parray[1], *peakamp2 = dz->parray[2];
  2056. double *peakfrq1 = dz->parray[3], *peakfrq2 = dz->parray[4];
  2057. int *peakno1 = dz->iparray[1], *peakno2 = dz->iparray[2];
  2058. int n, m, k;
  2059. double tempamp, tempfrq, peaklo, peakhi;
  2060. int cc, vc, done, peaks_possible, peaks_possible1 = 0, peaks_possible2 = 0;
  2061. for(n=0;n < dz->clength;n++) {
  2062. peakamp1[n] = 0.0;
  2063. peakno1[n] = n * 2;
  2064. peakamp2[n] = 0.0;
  2065. peakno2[n] = n * 2;
  2066. }
  2067. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2) {
  2068. peakamp1[cc] = fabs(dz->flbufptr[0][AMPP]);
  2069. peakfrq1[cc] = fabs(dz->flbufptr[0][FREQ]); // Get total amplitude for each channel
  2070. }
  2071. for(n=0;n < dz->clength - 1;n++) { // Sort into loudness order
  2072. for(m = n+1; m < dz->clength;m++) {
  2073. if(peakamp1[n] < peakamp1[m]) {
  2074. tempamp = peakamp1[n];
  2075. tempfrq = peakfrq1[n];
  2076. k = peakno1[n];
  2077. peakamp1[n] = peakamp1[m];
  2078. peakfrq1[n] = peakfrq1[m];
  2079. peakno1[n] = peakno1[m];
  2080. peakamp1[m] = tempamp;
  2081. peakfrq1[m] = tempfrq;
  2082. peakno1[m] = k;
  2083. }
  2084. }
  2085. }
  2086. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2) {
  2087. peakamp2[cc] = fabs(dz->flbufptr[1][AMPP]);
  2088. peakfrq2[cc] = fabs(dz->flbufptr[1][FREQ]); // Get total amplitude for each channel
  2089. }
  2090. for(n=0;n < dz->clength - 1;n++) { // Sort into loudness order
  2091. for(m = n+1; m < dz->clength;m++) {
  2092. if(peakamp2[n] < peakamp2[m]) {
  2093. tempamp = peakamp2[n];
  2094. tempfrq = peakfrq2[n];
  2095. k = peakno2[n];
  2096. peakamp2[n] = peakamp2[m];
  2097. peakfrq2[n] = peakfrq2[m];
  2098. peakno2[n] = peakno2[m];
  2099. peakamp2[m] = tempamp;
  2100. peakfrq2[m] = tempfrq;
  2101. peakno2[m] = k;
  2102. }
  2103. }
  2104. }
  2105. done = 0;
  2106. for(n=0;n<dz->clength-1;n++) {
  2107. peaklo = peakno1[n] * dz->chwidth;
  2108. peakhi = peaklo + dz->halfchwidth;
  2109. peaklo = peaklo - dz->halfchwidth;
  2110. for(m=n+1;m<dz->clength;m++) {
  2111. if(m >= *peakcnt) { // once we have peakcnt valid peaks, quit
  2112. done = 1;
  2113. break;
  2114. }
  2115. if(peakfrq1[m] > peaklo && peakfrq1[m] < peakhi) { // equivalent peak (with lower amp, as amps sorted into descending order)
  2116. for(k = m+1; k < dz->clength; k++) {
  2117. peakamp1[k-1] = peakamp1[k]; // eliminate
  2118. peakfrq1[k-1] = peakfrq1[k];
  2119. peakno1[k-1] = peakno1[k];
  2120. }
  2121. m--; // stay at same place in loop
  2122. }
  2123. }
  2124. if(done)
  2125. break;
  2126. }
  2127. done = 0;
  2128. // Same for 2nd file
  2129. for(n=0;n<dz->clength-1;n++) {
  2130. peaklo = peakno2[n] * dz->chwidth;
  2131. peakhi = peaklo + dz->halfchwidth;
  2132. peaklo = peaklo - dz->halfchwidth;
  2133. for(m=n+1;m<dz->clength;m++) {
  2134. if(m >= *peakcnt) {
  2135. done = 1;
  2136. break;
  2137. }
  2138. if(peakfrq2[m] > peaklo && peakfrq2[m] < peakhi) {
  2139. for(k = m+1; k < dz->clength; k++) {
  2140. peakamp2[k-1] = peakamp2[k];
  2141. peakfrq2[k-1] = peakfrq2[k];
  2142. peakno2[k-1] = peakno2[k];
  2143. }
  2144. m--;
  2145. }
  2146. }
  2147. if(done)
  2148. break;
  2149. }
  2150. for(n=0;n<dz->clength;n++) {
  2151. if(peakamp1[n] > 0.0)
  2152. peaks_possible1++;
  2153. if(peakamp2[n] > 0.0)
  2154. peaks_possible2++;
  2155. }
  2156. peaks_possible = min(peaks_possible1,peaks_possible2);
  2157. if(peaks_possible < *peakcnt) {
  2158. *peakcnt = peaks_possible;
  2159. }
  2160. // Finally : sort peaks into ascending order
  2161. for(n=0;n<(*peakcnt)-1;n++) {
  2162. for(m = n+1;m < *peakcnt;m++) {
  2163. if(peakno1[n] > peakno1[m]) {
  2164. k = peakno1[n];
  2165. peakno1[n] = peakno1[m];
  2166. peakno1[m] = k;
  2167. }
  2168. if(peakno2[n] > peakno2[m]) {
  2169. k = peakno2[n];
  2170. peakno2[n] = peakno2[m];
  2171. peakno2[m] = k;
  2172. }
  2173. }
  2174. }
  2175. }
  2176. /*************************************** DO_MORPHENV ******************************************/
  2177. void do_morphenv(dataptr dz)
  2178. {
  2179. int cc, vc;
  2180. double totamp_before = 0.0, totamp_after = 0.0, normaliser;
  2181. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2) { // Now do normal interpolation
  2182. totamp_before += dz->flbufptr[0][AMPP];
  2183. totamp_after += dz->flbufptr[1][AMPP];
  2184. if(totamp_after > 0) {
  2185. normaliser = totamp_before/totamp_after;
  2186. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2)
  2187. dz->flbufptr[0][AMPP] = (float)(dz->flbufptr[1][AMPP] * normaliser);
  2188. }
  2189. }
  2190. dz->total_windows++;
  2191. }
  2192. /******************** FIND_THE_AVERAGE_SPECTRAL_PEAKS_AND_CORRELATES ****************************/
  2193. int find_the_average_spectral_peaks_and_correlates(dataptr dz)
  2194. {
  2195. int exit_status, gotit;
  2196. double *peakamp2 = dz->parray[2];
  2197. double *peakfrq1 = dz->parray[3], *peakfrq2 = dz->parray[4];
  2198. int *peakno2 = dz->iparray[2];
  2199. int peakcnt = dz->iparam[NMPH_APKS];
  2200. int n, m, k;
  2201. double tempamp, tempfrq, peaklo, peakhi, frq;
  2202. int windows_to_buf = 0, samps_left = 0, wcnt;
  2203. int totwcnt = 0, cc, vc, done, peaks_possible = 0;
  2204. if((dz->process != SPECMORPH2) || dz->mode == 0) {
  2205. for(n=0;n < dz->clength;n++) {
  2206. peakamp2[n] = 0.0;
  2207. peakno2[n] = n * 2;
  2208. }
  2209. totwcnt = 0;
  2210. if(dz->process == SPECMORPH2)
  2211. samps_left = dz->insams[0];
  2212. else
  2213. samps_left = dz->insams[1];
  2214. while(samps_left) {
  2215. if(dz->process == SPECMORPH2) {
  2216. if((exit_status = read_specific_file_to_specific_buf(FILE0,&windows_to_buf,dz->bigfbuf,dz))<0)
  2217. return(exit_status);
  2218. } else {
  2219. if((exit_status = read_specific_file_to_specific_buf(FILE1,&windows_to_buf,dz->bigfbuf,dz))<0)
  2220. return(exit_status);
  2221. }
  2222. samps_left -= windows_to_buf * dz->wanted;
  2223. for(wcnt =0;wcnt < windows_to_buf;wcnt++) {
  2224. if(totwcnt == 0) {
  2225. totwcnt++;
  2226. continue;
  2227. }
  2228. for(cc=0,vc=0;cc < dz->clength;cc++,vc+=2) {
  2229. peakamp2[cc] += fabs(dz->flbufptr[0][AMPP]);
  2230. if(totwcnt == 1)
  2231. peakfrq2[cc] = fabs(dz->flbufptr[0][FREQ]);
  2232. else
  2233. peakfrq2[cc] = (peakfrq2[cc] + fabs(dz->flbufptr[0][FREQ]))/2.0;
  2234. }
  2235. totwcnt++;
  2236. }
  2237. }
  2238. for(n=0;n < dz->clength - 1;n++) {
  2239. for(m = n+1; m < dz->clength;m++) {
  2240. if(peakamp2[m] > peakamp2[n]) {
  2241. tempamp = peakamp2[n];
  2242. tempfrq = peakfrq2[n];
  2243. k = peakno2[n];
  2244. peakamp2[n] = peakamp2[m];
  2245. peakfrq2[n] = peakfrq2[m];
  2246. peakno2[n] = peakno2[m];
  2247. peakamp2[m] = tempamp;
  2248. peakfrq2[m] = tempfrq;
  2249. peakno2[m] = k;
  2250. }
  2251. }
  2252. }
  2253. if(dz->process != SPECMORPH2) {
  2254. if(sndseekEx(dz->ifd[1],0,0)<0) {
  2255. sprintf(errstr,"Sound seek failed in file 2\n");
  2256. return SYSTEM_ERROR;
  2257. }
  2258. }
  2259. // Eliminate equivalents of peak in adjacent channels
  2260. done = 0;
  2261. for(n=0;n<dz->clength-1;n++) {
  2262. peaklo = peakno2[n] * dz->chwidth;
  2263. peakhi = peaklo + dz->halfchwidth;
  2264. peaklo = peaklo - dz->halfchwidth;
  2265. for(m=n+1;m<dz->clength;m++) {
  2266. if(m >= peakcnt) {
  2267. done = 1;
  2268. break;
  2269. }
  2270. if(peakfrq2[m] > peaklo && peakfrq2[m] < peakhi) {
  2271. for(k = m+1; k < dz->clength; k++) {
  2272. peakamp2[k-1] = peakamp2[k];
  2273. peakfrq2[k-1] = peakfrq2[k];
  2274. peakno2[k-1] = peakno2[k];
  2275. }
  2276. m--;
  2277. }
  2278. }
  2279. if(done)
  2280. break;
  2281. }
  2282. for(n=0;n<dz->clength;n++) {
  2283. if(peakamp2[n] > 0.0)
  2284. peaks_possible++;
  2285. }
  2286. if(peaks_possible < peakcnt) {
  2287. fprintf(stdout,"WARNING: Less peaks in spectrum (%d) than peakcnt specified (%d) : reducing peakcnt\n",peaks_possible,peakcnt);
  2288. fflush(stdout);
  2289. peakcnt = peaks_possible;
  2290. dz->iparam[NMPH_APKS] = peakcnt;
  2291. }
  2292. if(dz->process == SPECMORPH2 && dz->mode == 0)
  2293. return FINISHED;
  2294. }
  2295. // Finally : assign freqs to all channels, to correspond (octavewise to peak data)
  2296. if(dz->process == SPECMORPH2 && dz->mode > 0)
  2297. peakcnt = min(peakcnt,dz->itemcnt);
  2298. for(cc=0,vc = 0;cc < dz->clength;cc++,vc+=2) {
  2299. peaklo = cc * dz->chwidth;
  2300. peakhi = peaklo + dz->halfchwidth;
  2301. peaklo = peaklo - dz->halfchwidth;
  2302. gotit = 0;
  2303. for(n=0;n<peakcnt;n++) {
  2304. if(peakfrq2[n] >= peaklo && peakfrq2[n] <= peakhi) {
  2305. peakfrq1[cc] = peakfrq2[n];
  2306. gotit = 1;
  2307. } else {
  2308. frq = peakfrq2[n];
  2309. gotit = 0;
  2310. if(frq > peakhi) {
  2311. while(frq > peakhi)
  2312. frq /= 2;
  2313. if(frq > peaklo) {
  2314. peakfrq1[cc] = frq;
  2315. gotit = 1;
  2316. }
  2317. } else if (frq < peaklo) {
  2318. while(frq < peaklo)
  2319. frq *= 2;
  2320. if(frq < peakhi) {
  2321. peakfrq1[cc] = frq;
  2322. gotit = 1;
  2323. }
  2324. }
  2325. }
  2326. if(gotit)
  2327. break;
  2328. }
  2329. if(!gotit)
  2330. peakfrq1[cc] = -10000;
  2331. }
  2332. for(cc=0,vc = 0;cc < dz->clength;cc++,vc+=2) {
  2333. if(peakfrq1[cc] == -10000) {
  2334. if(cc - 1 >= 0 && peakfrq1[cc-1] != -10000)
  2335. peakfrq1[cc] = peakfrq1[cc-1];
  2336. else if(cc + 1 < dz->clength && peakfrq1[cc+1] != -10000)
  2337. peakfrq1[cc] = peakfrq1[cc+1];
  2338. else
  2339. peakfrq1[cc] = cc * dz->chwidth;
  2340. }
  2341. }
  2342. return FINISHED;
  2343. }
  2344. /**************************** HANDLE_THE_SPECIAL_DATA ****************************/
  2345. int handle_the_special_data(char *str,dataptr dz)
  2346. {
  2347. int exit_status;
  2348. double dummy = 0.0;
  2349. FILE *fp;
  2350. int cnt = 0, linecnt;
  2351. char temp[800], *p;
  2352. if((fp = fopen(str,"r"))==NULL) {
  2353. sprintf(errstr,"Cannot open file %s to read times.\n",str);
  2354. return(DATA_ERROR);
  2355. }
  2356. linecnt = 0;
  2357. while(fgets(temp,200,fp)!=NULL) {
  2358. p = temp;
  2359. while(isspace(*p))
  2360. p++;
  2361. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  2362. continue;
  2363. while(get_float_from_within_string(&p,&dummy)) {
  2364. if(dummy < 0.0 || dummy > dz->nyquist) {
  2365. sprintf(errstr,"Invalid peak data (%lf) at line %d.\n",dummy,linecnt+1);
  2366. return(DATA_ERROR);
  2367. }
  2368. cnt++;
  2369. }
  2370. }
  2371. if(cnt > dz->clength) {
  2372. sprintf(errstr,"Too many peak frqs (%d) : max = %d.\n",cnt,dz->clength);
  2373. return(DATA_ERROR);
  2374. }
  2375. dz->itemcnt = cnt;
  2376. dz->all_words = 0;
  2377. if((exit_status = store_filename(str,dz))<0)
  2378. return(exit_status);
  2379. fclose(fp);
  2380. return FINISHED;
  2381. }
  2382. /**************************** READ_THE_SPECIAL_DATA ****************************/
  2383. int read_the_special_data(dataptr dz)
  2384. {
  2385. double dummy, *peakfrq2 = dz->parray[4];
  2386. int cnt = 0;
  2387. FILE *fp;
  2388. char temp[800], *p;
  2389. if((fp = fopen(dz->wordstor[0],"r"))==NULL) {
  2390. sprintf(errstr,"Cannot open file %s to read peak data.\n",dz->wordstor[0]);
  2391. return(DATA_ERROR);
  2392. }
  2393. while(fgets(temp,200,fp)!=NULL) {
  2394. p = temp;
  2395. while(isspace(*p))
  2396. p++;
  2397. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  2398. continue;
  2399. while(get_float_from_within_string(&p,&dummy))
  2400. peakfrq2[cnt++] = dummy;
  2401. }
  2402. fclose(fp);
  2403. return FINISHED;
  2404. }