synspline.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. /*
  2. * Copyright (c) 1983-2023 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. // TEST IS
  22. // _cdprogs\synspline synspline test.wav 44100 2 440 7 8 2
  23. // see HEREH for issues
  24. // + still crashed beyond obufpos = 1560
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <structures.h>
  28. #include <tkglobals.h>
  29. #include <pnames.h>
  30. #include <filetype.h>
  31. #include <processno.h>
  32. #include <modeno.h>
  33. #include <logic.h>
  34. #include <globcon.h>
  35. #include <cdpmain.h>
  36. #include <math.h>
  37. #include <mixxcon.h>
  38. #include <osbind.h>
  39. #include <standalone.h>
  40. #include <science.h>
  41. #include <ctype.h>
  42. #include <sfsys.h>
  43. #include <string.h>
  44. #include <srates.h>
  45. #define MAXOUTAMP 0.95
  46. #define CYCLENORM 0
  47. #define is_drift is_transpos
  48. #define maxdrift scalefact
  49. #ifdef unix
  50. #define round(x) lround((x))
  51. #endif
  52. char errstr[2400];
  53. int anal_infiles = 1;
  54. int sloom = 0;
  55. int sloombatch = 0;
  56. const char* cdp_version = "7.0.0";
  57. //CDP LIB REPLACEMENTS
  58. static int setup_synspline_application(dataptr dz);
  59. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  60. static int setup_synspline_param_ranges_and_defaults(dataptr dz);
  61. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  62. static int open_the_outfile(dataptr dz);
  63. //static int handle_the_special_data(char *str,dataptr dz);
  64. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  65. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  66. static int establish_application(dataptr dz);
  67. static int initialise_vflags(dataptr dz);
  68. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  69. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  70. static int mark_parameter_types(dataptr dz,aplptr ap);
  71. static int assign_file_data_storage(int infilecnt,dataptr dz);
  72. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  73. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  74. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  75. static int synspline(dataptr dz);
  76. //static void incr_sinptr(int n,double time,double onehzincr,dataptr dz);
  77. //static double read_level(int n,double time,dataptr dz);
  78. static int create_synspline_sndbufs(dataptr dz);
  79. //static int generate_packet_envelope (dataptr dz);
  80. //static double read_packet_envelope(int kk,double incr,dataptr dz);
  81. //static int modify_packet_envelope(dataptr dz);
  82. //static void rndintperm(int *perm,int cnt);
  83. //static void get_current_partial_vals(double time,double *pvals,int totalpartials,dataptr dz);
  84. //static void pancalc(double position,double *leftgain,double *rightgain);
  85. //static void sort_partials_into_ascending_frq_order(int total_partialcnt,double *pvals,double *sinptr,
  86. // double **llev,double **rlev,int **onoff,int **lmost,int **origspl,int *splordr,dataptr dz);
  87. //static void resort_partials_into_original_frq_order(int total_partialcnt,double *pvals,double *sinptr,
  88. // double **llev,double **rlev,int **onoff,int **lmost,int **origspl,int *splordr,dataptr dz);
  89. //static void xclusive(int *perm,int *permon,int *permoff,int max_partials_cnt,int partials_in_play, int **onoff,int stepcnt);
  90. //static double emergepos(int emergchan,int chans,double time,double timespan);
  91. //static double convergepos(int converchan,int chans,double time,double convergetime,double dur);
  92. //static void spacebox_apply(double pos, double lev,int chans,int *lmost, int *rmost,double *rlev,double *llev,int spacetyp);
  93. //static void spacebox(double *pos, int *switchpos, double posstep, int chans, int spacetyp, int configno, int configcnt,int *superperm);
  94. //static double sinread(double *tabpos,double frq,dataptr dz);
  95. static int check_synspline_param_validity_and_consistency(dataptr dz);
  96. static int do_a_spline(int *wavelen,double srate,dataptr dz);
  97. static double cosplint(int *hi,double time,int splincnt,dataptr dz);
  98. static int cospline(int half_wavelen,int splincnt,dataptr dz) ;
  99. /**************************************** MAIN *********************************************/
  100. int main(int argc,char *argv[])
  101. {
  102. int exit_status;
  103. dataptr dz = NULL;
  104. char **cmdline;
  105. int cmdlinecnt;
  106. // aplptr ap;
  107. int is_launched = FALSE;
  108. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  109. fprintf(stdout,"%s\n",cdp_version);
  110. fflush(stdout);
  111. return 0;
  112. }
  113. /* CHECK FOR SOUNDLOOM */
  114. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  115. sloom = 0;
  116. sloombatch = 1;
  117. }
  118. if(sflinit("cdp")){
  119. sfperror("cdp: initialisation\n");
  120. return(FAILED);
  121. }
  122. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  123. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  124. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  125. return(FAILED);
  126. }
  127. if(!sloom) {
  128. if(argc == 1) {
  129. usage1();
  130. return(FAILED);
  131. } else if(argc == 2) {
  132. usage2(argv[1]);
  133. return(FAILED);
  134. }
  135. }
  136. if(!sloom) {
  137. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  138. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  139. return(FAILED);
  140. }
  141. cmdline = argv;
  142. cmdlinecnt = argc;
  143. if((get_the_process_no(argv[0],dz))<0)
  144. return(FAILED);
  145. cmdline++;
  146. cmdlinecnt--;
  147. dz->maxmode = 0;
  148. // setup_particular_application =
  149. if(cmdlinecnt < 7 || cmdlinecnt > 10) {
  150. usage2("synspline");
  151. return(FAILED);
  152. }
  153. if((exit_status = setup_synspline_application(dz))<0) {
  154. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  155. return(FAILED);
  156. }
  157. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  158. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  159. return(FAILED);
  160. }
  161. } else {
  162. //parse_TK_data() =
  163. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  164. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  165. return(exit_status);
  166. }
  167. }
  168. // ap = dz->application;
  169. dz->infile->channels = 1;
  170. // parse_infile_and_hone_type() =
  171. // setup_param_ranges_and_defaults() =
  172. if((exit_status = setup_synspline_param_ranges_and_defaults(dz))<0) {
  173. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  174. return(FAILED);
  175. }
  176. // open_first_infile() : redundant
  177. // handle_extra_infiles() : redundant
  178. // handle_outfile() =
  179. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  180. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  181. return(FAILED);
  182. }
  183. // handle_formants() redundant
  184. // handle_formant_quiksearch() redundant
  185. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  186. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  187. return(FAILED);
  188. }
  189. // check_param_validity_and_consistency()
  190. if((exit_status = check_synspline_param_validity_and_consistency(dz)) < 0) {
  191. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  192. return(FAILED);
  193. }
  194. is_launched = TRUE;
  195. if((exit_status = create_synspline_sndbufs(dz))<0) { // CDP LIB
  196. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  197. return(FAILED);
  198. }
  199. if((exit_status = open_the_outfile(dz))<0) {
  200. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  201. return(FAILED);
  202. }
  203. if((exit_status = synspline(dz))<0) {
  204. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  205. return(FAILED);
  206. }
  207. if((exit_status = complete_output(dz))<0) { // CDP LIB
  208. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  209. return(FAILED);
  210. }
  211. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  212. free(dz);
  213. return(SUCCEEDED);
  214. }
  215. /**********************************************
  216. REPLACED CDP LIB FUNCTIONS
  217. **********************************************/
  218. /****************************** SET_PARAM_DATA *********************************/
  219. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  220. {
  221. ap->special_data = (char)special_data;
  222. ap->param_cnt = (char)paramcnt;
  223. ap->max_param_cnt = (char)maxparamcnt;
  224. if(ap->max_param_cnt>0) {
  225. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  226. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  227. return(MEMORY_ERROR);
  228. }
  229. strcpy(ap->param_list,paramlist);
  230. }
  231. return(FINISHED);
  232. }
  233. /****************************** SET_VFLGS *********************************/
  234. int set_vflgs
  235. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  236. {
  237. ap->option_cnt = (char) optcnt; /*RWD added cast */
  238. if(optcnt) {
  239. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  240. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  241. return(MEMORY_ERROR);
  242. }
  243. strcpy(ap->option_list,optlist);
  244. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  245. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  246. return(MEMORY_ERROR);
  247. }
  248. strcpy(ap->option_flags,optflags);
  249. }
  250. ap->vflag_cnt = (char) vflagcnt;
  251. ap->variant_param_cnt = (char) vparamcnt;
  252. if(vflagcnt) {
  253. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  254. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  255. return(MEMORY_ERROR);
  256. }
  257. strcpy(ap->variant_list,varlist);
  258. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  259. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  260. return(MEMORY_ERROR);
  261. }
  262. strcpy(ap->variant_flags,varflags);
  263. }
  264. return(FINISHED);
  265. }
  266. /***************************** APPLICATION_INIT **************************/
  267. int application_init(dataptr dz)
  268. {
  269. int exit_status;
  270. int storage_cnt;
  271. int tipc, brkcnt;
  272. aplptr ap = dz->application;
  273. if(ap->vflag_cnt>0)
  274. initialise_vflags(dz);
  275. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  276. ap->total_input_param_cnt = (char)tipc;
  277. if(tipc>0) {
  278. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  279. return(exit_status);
  280. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  281. return(exit_status);
  282. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  283. return(exit_status);
  284. }
  285. brkcnt = tipc;
  286. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  287. if(brkcnt>0) {
  288. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  289. return(exit_status);
  290. }
  291. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  292. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  293. return(exit_status);
  294. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  295. return(exit_status);
  296. }
  297. if((exit_status = mark_parameter_types(dz,ap))<0)
  298. return(exit_status);
  299. // establish_infile_constants() replaced by
  300. dz->infilecnt = 1;
  301. //establish_bufptrs_and_extra_buffers():
  302. return(FINISHED);
  303. }
  304. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  305. /* RWD mallo changed to calloc; helps debug verison run as release! */
  306. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  307. {
  308. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  309. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  310. return(MEMORY_ERROR);
  311. }
  312. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  313. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  314. return(MEMORY_ERROR);
  315. }
  316. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  317. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  318. return(MEMORY_ERROR);
  319. }
  320. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  321. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  322. return(MEMORY_ERROR);
  323. }
  324. return(FINISHED);
  325. }
  326. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  327. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  328. {
  329. int n;
  330. for(n=0;n<storage_cnt;n++) {
  331. dz->is_int[n] = (char)0;
  332. dz->no_brk[n] = (char)0;
  333. }
  334. return(FINISHED);
  335. }
  336. /***************************** MARK_PARAMETER_TYPES **************************/
  337. int mark_parameter_types(dataptr dz,aplptr ap)
  338. {
  339. int n, m; /* PARAMS */
  340. for(n=0;n<ap->max_param_cnt;n++) {
  341. switch(ap->param_list[n]) {
  342. case('0'): break; /* dz->is_active[n] = 0 is default */
  343. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  344. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  345. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  346. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  347. default:
  348. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  349. return(PROGRAM_ERROR);
  350. }
  351. } /* OPTIONS */
  352. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  353. switch(ap->option_list[n]) {
  354. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  355. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  356. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  357. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  358. default:
  359. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  360. return(PROGRAM_ERROR);
  361. }
  362. } /* VARIANTS */
  363. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  364. switch(ap->variant_list[n]) {
  365. case('0'): break;
  366. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  367. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  368. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  369. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  370. default:
  371. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  372. return(PROGRAM_ERROR);
  373. }
  374. } /* INTERNAL */
  375. for(n=0,
  376. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  377. switch(ap->internal_param_list[n]) {
  378. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  379. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  380. case('d'): dz->no_brk[m] = (char)1; break;
  381. default:
  382. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  383. return(PROGRAM_ERROR);
  384. }
  385. }
  386. return(FINISHED);
  387. }
  388. /************************ HANDLE_THE_OUTFILE *********************/
  389. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  390. {
  391. char *filename = (*cmdline)[0], *p;
  392. if(filename[0]=='-' && filename[1]=='f') {
  393. dz->floatsam_output = 1;
  394. dz->true_outfile_stype = SAMP_FLOAT;
  395. filename+= 2;
  396. }
  397. if(!sloom) {
  398. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  399. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  400. return(DATA_ERROR);
  401. }
  402. }
  403. p = filename; // Drop file extension
  404. while(*p != ENDOFSTR) {
  405. if(*p == '.') {
  406. *p = ENDOFSTR;
  407. break;
  408. }
  409. p++;
  410. }
  411. strcpy(dz->outfilename,filename);
  412. (*cmdline)++;
  413. (*cmdlinecnt)--;
  414. return(FINISHED);
  415. }
  416. /************************ OPEN_THE_OUTFILE *********************/
  417. int open_the_outfile(dataptr dz)
  418. {
  419. int exit_status;
  420. dz->infile->srate = dz->iparam[SPLIN_SRATE];
  421. dz->infile->channels = 1;
  422. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  423. return(exit_status);
  424. return(FINISHED);
  425. }
  426. /***************************** ESTABLISH_APPLICATION **************************/
  427. int establish_application(dataptr dz)
  428. {
  429. aplptr ap;
  430. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  431. sprintf(errstr,"establish_application()\n");
  432. return(MEMORY_ERROR);
  433. }
  434. ap = dz->application;
  435. memset((char *)ap,0,sizeof(struct applic));
  436. return(FINISHED);
  437. }
  438. /************************* INITIALISE_VFLAGS *************************/
  439. int initialise_vflags(dataptr dz)
  440. {
  441. int n;
  442. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  443. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  444. return(MEMORY_ERROR);
  445. }
  446. for(n=0;n<dz->application->vflag_cnt;n++)
  447. dz->vflag[n] = FALSE;
  448. return FINISHED;
  449. }
  450. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  451. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  452. {
  453. int n;
  454. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  455. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  456. return(MEMORY_ERROR);
  457. }
  458. for(n=0;n<tipc;n++)
  459. ap->default_val[n] = 0.0;
  460. return(FINISHED);
  461. }
  462. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  463. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  464. {
  465. int n;
  466. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  467. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  468. return(MEMORY_ERROR);
  469. }
  470. for(n=0;n<tipc;n++)
  471. dz->is_active[n] = (char)0;
  472. return(FINISHED);
  473. }
  474. /************************* SETUP_SYNSPLINE_APPLICATION *******************/
  475. int setup_synspline_application(dataptr dz)
  476. {
  477. int exit_status;
  478. aplptr ap;
  479. if((exit_status = establish_application(dz))<0) // GLOBAL
  480. return(FAILED);
  481. ap = dz->application;
  482. // SEE parstruct FOR EXPLANATION of next 2 functions
  483. if((exit_status = set_param_data(ap,0,6,6,"idDIIi"))<0)
  484. return(FAILED);
  485. if((exit_status = set_vflgs(ap,"sidv",4,"IIDD","n",1,0,"0"))<0)
  486. return(exit_status);
  487. // set_legal_infile_structure -->
  488. dz->has_otherfile = FALSE;
  489. // assign_process_logic -->
  490. dz->input_data_type = NO_FILE_AT_ALL;
  491. dz->process_type = UNEQUAL_SNDFILE;
  492. dz->outfiletype = SNDFILE_OUT;
  493. return application_init(dz); //GLOBAL
  494. }
  495. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  496. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  497. {
  498. int exit_status;
  499. infileptr infile_info;
  500. if(!sloom) {
  501. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  502. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  503. return(MEMORY_ERROR);
  504. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  505. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  506. return(PROGRAM_ERROR);
  507. } else if(infile_info->filetype != SNDFILE) {
  508. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  509. return(DATA_ERROR);
  510. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  511. sprintf(errstr,"Failed to copy file parsing information\n");
  512. return(PROGRAM_ERROR);
  513. }
  514. free(infile_info);
  515. }
  516. return(FINISHED);
  517. }
  518. /************************* SETUP_SYNSPLINE_PARAM_RANGES_AND_DEFAULTS *******************/
  519. int setup_synspline_param_ranges_and_defaults(dataptr dz)
  520. {
  521. int exit_status;
  522. aplptr ap = dz->application;
  523. // set_param_ranges()
  524. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  525. // NB total_input_param_cnt is > 0 !!!
  526. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  527. return(FAILED);
  528. // get_param_ranges()
  529. ap->lo[SPLIN_SRATE] = 16000;
  530. ap->hi[SPLIN_SRATE] = 96000;
  531. ap->default_val[SPLIN_SRATE] = 44100.0;
  532. ap->lo[SPLIN_DUR] = 0.0;
  533. ap->hi[SPLIN_DUR] = 32767.0;
  534. ap->default_val[SPLIN_DUR] = 4.0;
  535. ap->lo[SPLIN_FRQ] = .001;
  536. ap->hi[SPLIN_FRQ] = 10000;
  537. ap->default_val[SPLIN_FRQ] = 440;
  538. ap->lo[SPLIN_CNT] = 0;
  539. ap->hi[SPLIN_CNT] = 64;
  540. ap->default_val[SPLIN_CNT] = 2;
  541. ap->lo[SPLIN_INTP] = 0;
  542. ap->hi[SPLIN_INTP] = 4096;
  543. ap->default_val[SPLIN_INTP] = 24;
  544. ap->lo[SPLIN_SEED] = 0;
  545. ap->hi[SPLIN_SEED] = 64;
  546. ap->default_val[SPLIN_SEED] = 1;
  547. ap->lo[SPLIN_MCNT] = 0;
  548. ap->hi[SPLIN_MCNT] = 64;
  549. ap->default_val[SPLIN_MCNT] = 0;
  550. ap->lo[SPLIN_MINTP] = 0;
  551. ap->hi[SPLIN_MINTP] = 4096;
  552. ap->default_val[SPLIN_MINTP] = 0;
  553. ap->lo[SPLIN_DRIFT] = 0;
  554. ap->hi[SPLIN_DRIFT] = 12;
  555. ap->default_val[SPLIN_DRIFT] = 0;
  556. ap->lo[SPLIN_DRVEL] = 0;
  557. ap->hi[SPLIN_DRVEL] = 1000;
  558. ap->default_val[SPLIN_DRVEL] = 0;
  559. dz->maxmode = 0;
  560. if(!sloom)
  561. put_default_vals_in_all_params(dz);
  562. return(FINISHED);
  563. }
  564. /********************************* PARSE_SLOOM_DATA *********************************/
  565. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  566. {
  567. int exit_status;
  568. int cnt = 1, infilecnt;
  569. int filesize, insams, inbrksize;
  570. double dummy;
  571. int true_cnt = 0;
  572. // aplptr ap;
  573. while(cnt<=PRE_CMDLINE_DATACNT) {
  574. if(cnt > argc) {
  575. sprintf(errstr,"Insufficient data sent from TK\n");
  576. return(DATA_ERROR);
  577. }
  578. switch(cnt) {
  579. case(1):
  580. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  581. sprintf(errstr,"Cannot read process no. sent from TK\n");
  582. return(DATA_ERROR);
  583. }
  584. break;
  585. case(2):
  586. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  587. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  588. return(DATA_ERROR);
  589. }
  590. if(dz->mode > 0)
  591. dz->mode--;
  592. //setup_particular_application() =
  593. if((exit_status = setup_synspline_application(dz))<0)
  594. return(exit_status);
  595. // ap = dz->application;
  596. break;
  597. case(3):
  598. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  599. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  600. return(DATA_ERROR);
  601. }
  602. if(infilecnt < 1) {
  603. true_cnt = cnt + 1;
  604. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  605. }
  606. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  607. return(exit_status);
  608. break;
  609. case(INPUT_FILETYPE+4):
  610. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  611. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  612. return(DATA_ERROR);
  613. }
  614. break;
  615. case(INPUT_FILESIZE+4):
  616. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  617. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  618. return(DATA_ERROR);
  619. }
  620. dz->insams[0] = filesize;
  621. break;
  622. case(INPUT_INSAMS+4):
  623. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  624. sprintf(errstr,"Cannot read insams sent from TK\n");
  625. return(DATA_ERROR);
  626. }
  627. dz->insams[0] = insams;
  628. break;
  629. case(INPUT_SRATE+4):
  630. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  631. sprintf(errstr,"Cannot read srate sent from TK\n");
  632. return(DATA_ERROR);
  633. }
  634. break;
  635. case(INPUT_CHANNELS+4):
  636. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  637. sprintf(errstr,"Cannot read channels sent from TK\n");
  638. return(DATA_ERROR);
  639. }
  640. break;
  641. case(INPUT_STYPE+4):
  642. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  643. sprintf(errstr,"Cannot read stype sent from TK\n");
  644. return(DATA_ERROR);
  645. }
  646. break;
  647. case(INPUT_ORIGSTYPE+4):
  648. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  649. sprintf(errstr,"Cannot read origstype sent from TK\n");
  650. return(DATA_ERROR);
  651. }
  652. break;
  653. case(INPUT_ORIGRATE+4):
  654. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  655. sprintf(errstr,"Cannot read origrate sent from TK\n");
  656. return(DATA_ERROR);
  657. }
  658. break;
  659. case(INPUT_MLEN+4):
  660. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  661. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  662. return(DATA_ERROR);
  663. }
  664. break;
  665. case(INPUT_DFAC+4):
  666. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  667. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  668. return(DATA_ERROR);
  669. }
  670. break;
  671. case(INPUT_ORIGCHANS+4):
  672. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  673. sprintf(errstr,"Cannot read origchans sent from TK\n");
  674. return(DATA_ERROR);
  675. }
  676. break;
  677. case(INPUT_SPECENVCNT+4):
  678. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  679. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  680. return(DATA_ERROR);
  681. }
  682. dz->specenvcnt = dz->infile->specenvcnt;
  683. break;
  684. case(INPUT_WANTED+4):
  685. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  686. sprintf(errstr,"Cannot read wanted sent from TK\n");
  687. return(DATA_ERROR);
  688. }
  689. break;
  690. case(INPUT_WLENGTH+4):
  691. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  692. sprintf(errstr,"Cannot read wlength sent from TK\n");
  693. return(DATA_ERROR);
  694. }
  695. break;
  696. case(INPUT_OUT_CHANS+4):
  697. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  698. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  699. return(DATA_ERROR);
  700. }
  701. break;
  702. /* RWD these chanegs to samps - tk will have to deal with that! */
  703. case(INPUT_DESCRIPTOR_BYTES+4):
  704. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  705. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  706. return(DATA_ERROR);
  707. }
  708. break;
  709. case(INPUT_IS_TRANSPOS+4):
  710. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  711. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  712. return(DATA_ERROR);
  713. }
  714. break;
  715. case(INPUT_COULD_BE_TRANSPOS+4):
  716. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  717. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  718. return(DATA_ERROR);
  719. }
  720. break;
  721. case(INPUT_COULD_BE_PITCH+4):
  722. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  723. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  724. return(DATA_ERROR);
  725. }
  726. break;
  727. case(INPUT_DIFFERENT_SRATES+4):
  728. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  729. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  730. return(DATA_ERROR);
  731. }
  732. break;
  733. case(INPUT_DUPLICATE_SNDS+4):
  734. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  735. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  736. return(DATA_ERROR);
  737. }
  738. break;
  739. case(INPUT_BRKSIZE+4):
  740. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  741. sprintf(errstr,"Cannot read brksize sent from TK\n");
  742. return(DATA_ERROR);
  743. }
  744. if(inbrksize > 0) {
  745. switch(dz->input_data_type) {
  746. case(WORDLIST_ONLY):
  747. break;
  748. case(PITCH_AND_PITCH):
  749. case(PITCH_AND_TRANSPOS):
  750. case(TRANSPOS_AND_TRANSPOS):
  751. dz->tempsize = inbrksize;
  752. break;
  753. case(BRKFILES_ONLY):
  754. case(UNRANGED_BRKFILE_ONLY):
  755. case(DB_BRKFILES_ONLY):
  756. case(ALL_FILES):
  757. case(ANY_NUMBER_OF_ANY_FILES):
  758. if(dz->extrabrkno < 0) {
  759. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  760. return(DATA_ERROR);
  761. }
  762. if(dz->brksize == NULL) {
  763. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  764. return(PROGRAM_ERROR);
  765. }
  766. dz->brksize[dz->extrabrkno] = inbrksize;
  767. break;
  768. default:
  769. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  770. dz->input_data_type);
  771. return(PROGRAM_ERROR);
  772. }
  773. break;
  774. }
  775. break;
  776. case(INPUT_NUMSIZE+4):
  777. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  778. sprintf(errstr,"Cannot read numsize sent from TK\n");
  779. return(DATA_ERROR);
  780. }
  781. break;
  782. case(INPUT_LINECNT+4):
  783. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  784. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  785. return(DATA_ERROR);
  786. }
  787. break;
  788. case(INPUT_ALL_WORDS+4):
  789. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  790. sprintf(errstr,"Cannot read all_words sent from TK\n");
  791. return(DATA_ERROR);
  792. }
  793. break;
  794. case(INPUT_ARATE+4):
  795. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  796. sprintf(errstr,"Cannot read arate sent from TK\n");
  797. return(DATA_ERROR);
  798. }
  799. break;
  800. case(INPUT_FRAMETIME+4):
  801. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  802. sprintf(errstr,"Cannot read frametime sent from TK\n");
  803. return(DATA_ERROR);
  804. }
  805. dz->frametime = (float)dummy;
  806. break;
  807. case(INPUT_WINDOW_SIZE+4):
  808. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  809. sprintf(errstr,"Cannot read window_size sent from TK\n");
  810. return(DATA_ERROR);
  811. }
  812. break;
  813. case(INPUT_NYQUIST+4):
  814. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  815. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  816. return(DATA_ERROR);
  817. }
  818. break;
  819. case(INPUT_DURATION+4):
  820. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  821. sprintf(errstr,"Cannot read duration sent from TK\n");
  822. return(DATA_ERROR);
  823. }
  824. break;
  825. case(INPUT_MINBRK+4):
  826. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  827. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  828. return(DATA_ERROR);
  829. }
  830. break;
  831. case(INPUT_MAXBRK+4):
  832. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  833. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  834. return(DATA_ERROR);
  835. }
  836. break;
  837. case(INPUT_MINNUM+4):
  838. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  839. sprintf(errstr,"Cannot read minnum sent from TK\n");
  840. return(DATA_ERROR);
  841. }
  842. break;
  843. case(INPUT_MAXNUM+4):
  844. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  845. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  846. return(DATA_ERROR);
  847. }
  848. break;
  849. default:
  850. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  851. return(PROGRAM_ERROR);
  852. }
  853. cnt++;
  854. }
  855. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  856. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  857. return(DATA_ERROR);
  858. }
  859. if(true_cnt)
  860. cnt = true_cnt;
  861. *cmdlinecnt = 0;
  862. while(cnt < argc) {
  863. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  864. return(exit_status);
  865. cnt++;
  866. }
  867. return(FINISHED);
  868. }
  869. /********************************* GET_TK_CMDLINE_WORD *********************************/
  870. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  871. {
  872. if(*cmdlinecnt==0) {
  873. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  874. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  875. return(MEMORY_ERROR);
  876. }
  877. } else {
  878. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  879. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  880. return(MEMORY_ERROR);
  881. }
  882. }
  883. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  884. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  885. return(MEMORY_ERROR);
  886. }
  887. strcpy((*cmdline)[*cmdlinecnt],q);
  888. (*cmdlinecnt)++;
  889. return(FINISHED);
  890. }
  891. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  892. int assign_file_data_storage(int infilecnt,dataptr dz)
  893. {
  894. int exit_status;
  895. int no_sndfile_system_files = FALSE;
  896. dz->infilecnt = infilecnt;
  897. if((exit_status = allocate_filespace(dz))<0)
  898. return(exit_status);
  899. if(no_sndfile_system_files)
  900. dz->infilecnt = 0;
  901. return(FINISHED);
  902. }
  903. /************************* redundant functions: to ensure libs compile OK *******************/
  904. int assign_process_logic(dataptr dz)
  905. {
  906. return(FINISHED);
  907. }
  908. void set_legal_infile_structure(dataptr dz)
  909. {}
  910. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  911. {
  912. return(FINISHED);
  913. }
  914. int setup_internal_arrays_and_array_pointers(dataptr dz)
  915. {
  916. return(FINISHED);
  917. }
  918. int establish_bufptrs_and_extra_buffers(dataptr dz)
  919. {
  920. return(FINISHED);
  921. }
  922. int read_special_data(char *str,dataptr dz)
  923. {
  924. return(FINISHED);
  925. }
  926. int inner_loop
  927. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  928. {
  929. return(FINISHED);
  930. }
  931. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  932. {
  933. return(FINISHED);
  934. }
  935. /******************************** USAGE1 ********************************/
  936. int usage1(void)
  937. {
  938. usage2("synspline");
  939. return(USAGE_ONLY);
  940. }
  941. /********************************************************************************************/
  942. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  943. {
  944. if(!strcmp(prog_identifier_from_cmdline,"synspline")) dz->process = SYNSPLINE;
  945. else {
  946. fprintf(stderr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  947. return(USAGE_ONLY);
  948. }
  949. return(FINISHED);
  950. }
  951. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  952. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  953. {
  954. int n;
  955. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  956. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  957. return(MEMORY_ERROR);
  958. }
  959. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  960. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  961. return(MEMORY_ERROR);
  962. }
  963. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  964. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  965. return(MEMORY_ERROR);
  966. }
  967. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  968. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  969. return(MEMORY_ERROR);
  970. }
  971. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  972. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  973. return(MEMORY_ERROR);
  974. }
  975. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  976. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  977. return(MEMORY_ERROR);
  978. }
  979. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  980. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  981. return(MEMORY_ERROR);
  982. }
  983. for(n=0;n<brkcnt;n++) {
  984. dz->brk[n] = NULL;
  985. dz->brkptr[n] = NULL;
  986. dz->brkinit[n] = 0;
  987. dz->brksize[n] = 0;
  988. }
  989. return(FINISHED);
  990. }
  991. /******************************** USAGE2 ********************************/
  992. int usage2(char *str)
  993. {
  994. if(!strcmp(str,"synspline")) {
  995. fprintf(stderr,
  996. "USAGE: synspline synspline outfile srate dur frq splinecnt interpval seed\n"
  997. " [-smaxspline] [-imaxinterp] [-dpdrift -vdriftrate] [-n]\n"
  998. "\n"
  999. "Synthesis from waveforms made by smoothly joining randomly generated points.\n"
  1000. "\n"
  1001. "SRATE Sample rate of synthesized sound.\n"
  1002. "DUR Duration of synthesized sound.\n"
  1003. "FRQ Fundamental frq of output (Range 0.001 to 10000 Hz) (PTR).\n"
  1004. "SPLINECNT No. of rand vals (to smooth between) per half-wavecycle (0-64) (PTR).\n"
  1005. "INTERPVAL No. of wavecycles over which 1 waveshape morphs to next (0 - 4096) (PTR).\n"
  1006. "SEED Same seed value produces identical output with same parameters.\n"
  1007. "MAXSPLINE Max splinecnt: (PTR) Causes random vals of \"splinecnt\" to be generated,\n"
  1008. " in range \"SPLINECNT\" to \"MAXSPLINE\" (Ignored if set to zero).\n"
  1009. "MAXINTERP Max interpval: (PTR) Causes random vals of \"interpval\" to be generated,\n"
  1010. " in range \"INTERPVAL\" to \"MAXINTERP\" (Ignored if set to zero).\n"
  1011. "PDRIFT Semitone (half)range of random drift in frequency. (0 - 12).\n"
  1012. "DRIFTRATE Average time (mS) to next drift offset (True time 1/2 to 3/2 of this) (1-1000).\n"
  1013. "-n Normalise every cycle (default: cycles retain random valued amplitudes).\n"
  1014. "\n"
  1015. "(PTR = possibly time-varying).\n");
  1016. } else
  1017. fprintf(stdout,"Unknown option '%s'\n",str);
  1018. return(USAGE_ONLY);
  1019. }
  1020. int usage3(char *str1,char *str2)
  1021. {
  1022. fprintf(stderr,"Insufficient parameters on command line.\n");
  1023. return(USAGE_ONLY);
  1024. }
  1025. /******************************** SYNSPLINE *********************************/
  1026. int synspline(dataptr dz)
  1027. {
  1028. int exit_status, firsttime = 1, splinintp, rval;
  1029. int wavelen1, wavelen2 = 0, this_wavelen, wavelendiff, j, k, n, m, obufpos = 0, driftpos;
  1030. double thistime = 0.0, ampinterp1, ampinterp2, val1, val2, val, frac, diff, dpos1, dpos2, dincr1, dincr2, rrange, srate = dz->iparam[SPLIN_SRATE];
  1031. float *splinebuf1 = dz->sampbuf[0], *splinebuf2 = dz->sampbuf[1], *obuf = dz->sampbuf[2], *ovflwbuf = dz->sampbuf[3];
  1032. float *driftbuf = dz->sampbuf[4];
  1033. double lastdrifttime = 0.0, nextdrifttime = 0.0, driftincr1 = 1.0, driftincr2 = 1.0, driftincrincr = 0.0, driftend, dbufpos/*, abstime*/;
  1034. int lastdriftsamps = 0, nextdriftsamps = 0, abssamps = 0, driftbufpos = 0;
  1035. memset((char *)splinebuf1,0,dz->buflen * sizeof(float));
  1036. memset((char *)splinebuf2,0,dz->buflen * sizeof(float));
  1037. if(dz->is_drift) {
  1038. lastdrifttime = 0;
  1039. lastdriftsamps = 0;
  1040. if(dz->brksize[SPLIN_DRIFT]) {
  1041. if((read_value_from_brktable(lastdrifttime,SPLIN_DRIFT,dz))<0)
  1042. return DATA_ERROR;
  1043. }
  1044. driftincr1 = (drand48() * 2.0) - 1.0; // Range 0 to 1 -> 0 to 2 -> -1 to 1
  1045. driftincr1 *= dz->param[SPLIN_DRIFT];
  1046. driftincr1 = pow(2.0,driftincr1/SEMITONES_PER_OCTAVE); // Read increment to produce initial transposition value
  1047. if(dz->brksize[SPLIN_DRVEL]) {
  1048. if((read_value_from_brktable(0,SPLIN_DRVEL,dz))<0)
  1049. return DATA_ERROR;
  1050. }
  1051. nextdrifttime = (drand48()) + 0.5; // Range 0 to 1 -> 0 to 1/2 to 3/2
  1052. nextdrifttime *= dz->param[SPLIN_DRVEL] * MS_TO_SECS;
  1053. nextdriftsamps = (int)floor(nextdrifttime * srate);
  1054. if(dz->brksize[SPLIN_DRIFT]) {
  1055. if((read_value_from_brktable(nextdrifttime,SPLIN_DRIFT,dz))<0)
  1056. return DATA_ERROR;
  1057. }
  1058. driftincr2 = ((drand48() * 2.0) - 1.0) - 0.5;
  1059. driftincr2 *= dz->param[SPLIN_DRIFT];
  1060. driftincr2 = pow(2.0,driftincr2/SEMITONES_PER_OCTAVE); // Read increment to produce end transposition value
  1061. driftincrincr = (double)(driftincr2 - driftincr1)/(nextdrifttime - lastdrifttime);
  1062. } // Increment of increment
  1063. while(thistime < dz->param[SPLIN_DUR]) {
  1064. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  1065. return exit_status;
  1066. if(firsttime) {
  1067. if((exit_status = do_a_spline(&wavelen2,srate,dz))<0) // Create a spline in splinebuf2
  1068. return exit_status;
  1069. firsttime = 0;
  1070. } else {
  1071. memset((char *)splinebuf1,0,dz->buflen * sizeof(float)); // Copy spline2 to spline1
  1072. memcpy((char *)splinebuf1,(char *)splinebuf2,dz->buflen * sizeof(float));
  1073. memset((char *)splinebuf2,0,dz->buflen * sizeof(float));
  1074. wavelen1 = wavelen2;
  1075. if((exit_status = do_a_spline(&wavelen2,srate,dz))<0) // Create a 2nd spline in splinebuf2
  1076. return exit_status;
  1077. if(dz->is_drift) {
  1078. // Output is written to driftbuf (rather than directly to obuf)
  1079. memset((char *)driftbuf,0,dz->buflen2 * sizeof(float));
  1080. driftpos = 0;
  1081. for(n=0;n<wavelen1;n++) // Write 1st spline to outbuf
  1082. driftbuf[driftpos++] = splinebuf1[n];
  1083. thistime += (double)wavelen1/srate; // Update time in output
  1084. if(thistime >= dz->param[SPLIN_DUR])
  1085. break;
  1086. splinintp = dz->iparam[SPLIN_INTP]; // Get numjber of interpolations
  1087. if(dz->iparam[SPLIN_MINTP] > 0) { // (and if there are min and max values,
  1088. rrange = dz->iparam[SPLIN_MINTP] - dz->iparam[SPLIN_INTP]; // generate a rand val in the range)
  1089. rval = (int)floor(drand48() * (double)rrange);
  1090. splinintp += rval;
  1091. } // Note any change in wavelength
  1092. wavelendiff = wavelen2 - wavelen1;
  1093. for(j = 0; j < splinintp;j++) { // Create the interpolated splines.
  1094. ampinterp1 = (double)(splinintp - j)/(double)splinintp; // Relative level of 1st spline at this count
  1095. ampinterp2 = (double)j/(double)splinintp; // Relative level of 2nd spline at this count
  1096. if(wavelendiff == 0) {
  1097. for(n = 0; n < wavelen1 ;n++) { // If wavelens are same length
  1098. val1 = splinebuf1[n] * ampinterp1; // do straightforward intepolation, saple by sample
  1099. val2 = splinebuf2[n] * ampinterp2;
  1100. driftbuf[driftpos++] = (float)(val1 + val2);
  1101. }
  1102. thistime += (double)wavelen1/srate; // Update time in output
  1103. if(thistime >= dz->param[SPLIN_DUR])
  1104. break;
  1105. } else { // If wavelens at start and endare different, wavelen at each interpolated wavecycle will be different,
  1106. // so both start and end waveforms will need to be read by interpolation
  1107. dpos1 = 0.0; // Fractional sample-read in spline1, initially zero
  1108. dpos2 = 0.0; // Fractional sample-read in spline2, initially zero
  1109. // Then interpolate for current interpolated-spline's length
  1110. this_wavelen = wavelen1 + (int)round((double)wavelendiff * ((double)(j+1)/(double)(splinintp+1)));
  1111. dincr1 = (double)wavelen1/(double)this_wavelen; // Fractional sample-advance in 1st spline
  1112. dincr2 = (double)wavelen2/(double)this_wavelen; // Fractional sample-advance in 2nd spline
  1113. for(n = 0, m = this_wavelen; n < this_wavelen ;n++,m--) {
  1114. // spline 1 : Interpolate timewise for value
  1115. k = (int)floor(dpos1);
  1116. frac = dpos1 - (double)k;
  1117. diff = splinebuf1[k+1] - splinebuf1[k];
  1118. val1 = splinebuf1[k] + (diff * frac);
  1119. // spline 2 : Interpolate timewise for value
  1120. k = (int)floor(dpos2);
  1121. frac = dpos2 - (double)k;
  1122. diff = splinebuf2[k+1] - splinebuf2[k];
  1123. val2 = splinebuf2[k] + (diff * frac);
  1124. // Interpolate values for relative amplitude
  1125. val1 *= ampinterp1;
  1126. val2 *= ampinterp2;
  1127. driftbuf[driftpos++] = (float)(val1 + val2);
  1128. // Advance in original-splines
  1129. dpos1 += dincr1;
  1130. dpos2 += dincr2;
  1131. }
  1132. thistime += (double)this_wavelen/srate; // Update time in output
  1133. if(thistime >= dz->param[SPLIN_DUR])
  1134. break;
  1135. }
  1136. }
  1137. // Once all interpolating cycles have been written to driftbuf
  1138. // data is transpose-copied to outbuf
  1139. driftend = (double)driftpos; // Note end of sampoles in driftbuf (later samples are zeroed..deals with wraparound)
  1140. dbufpos = 0.0; // Set to zero (possibly) fractional pointer into driftbuf.
  1141. while(dbufpos < driftend) { // Advance by interpolated increments in driftbuf until end of buf reached.
  1142. abssamps = dz->total_samps_written + obufpos;
  1143. // abstime = (double)abssamps/srate; // Absolute time (for reading any brktables) measured from absolute outbuf position
  1144. if(abssamps == nextdriftsamps) {
  1145. driftincr1 = driftincr2; // If we've reached the next designated time for generating a new transposition(incr) value,
  1146. lastdrifttime = nextdrifttime; // Set the "last" values as what were the "next" values
  1147. lastdriftsamps = nextdriftsamps;
  1148. if(dz->brksize[SPLIN_DRVEL]) {
  1149. if((read_value_from_brktable(nextdrifttime,SPLIN_DRVEL,dz))<0)
  1150. return DATA_ERROR; // Get the timestep to the next transposition val
  1151. } // Get next-transposition-value average-timing by adding step to current time
  1152. val = (drand48()) + 0.5; // Generate rand variation of this in range 1/2 to 3/2 of value
  1153. val *= dz->param[SPLIN_DRVEL] * MS_TO_SECS;
  1154. nextdrifttime += val;
  1155. nextdriftsamps = (int)floor(nextdrifttime * srate);
  1156. if(dz->brksize[SPLIN_DRIFT]) { // Get next-transposition-value
  1157. if((read_value_from_brktable(nextdrifttime,SPLIN_DRIFT,dz))<0)
  1158. return DATA_ERROR;
  1159. }
  1160. driftincr2 = ((drand48() * 2.0) - 1.0) - 0.5;
  1161. driftincr2 *= dz->param[SPLIN_DRIFT]; // Calc next table-read increment
  1162. driftincr2 = pow(2.0,driftincr2/SEMITONES_PER_OCTAVE);
  1163. // and set the step change from current incr (driftincr1) to the next (driftincr2)
  1164. driftincrincr = (double)(driftincr2 - driftincr1)/(double)(nextdriftsamps - lastdriftsamps);
  1165. }
  1166. driftbufpos = (int)floor(dbufpos);
  1167. frac = dbufpos - (double)driftbufpos; // Read values from driftbuf by interpolation,
  1168. diff = driftbuf[driftbufpos+1] - driftbuf[driftbufpos];
  1169. diff *= frac; // and put interpolated value in obuf.
  1170. obuf[obufpos++] = (float)(driftbuf[driftbufpos] + diff);
  1171. dbufpos += driftincr1; // Advance in driftbuf
  1172. driftincr1 += driftincrincr; // AND alter advance-incr appropriately
  1173. if(driftincrincr >= 0) // If driftincr(1) increasing
  1174. driftincr1 = min(driftincr1,driftincr2); // don't let driftincr rise above value of incr2
  1175. else // Else, driftincr(1) falling, so
  1176. driftincr1 = max(driftincr1,driftincr2); // don't let driftincr fall below value of incr2
  1177. if(obufpos >= dz->buflen2) { // If obuf fills up, write to file
  1178. if((exit_status = write_samps(obuf,dz->buflen2,dz))<0)
  1179. return(exit_status);
  1180. memset((char *)obuf,0,dz->buflen2 * sizeof(float));
  1181. memcpy((char *)obuf,(char *)ovflwbuf,dz->buflen2 * sizeof(float));
  1182. memset((char *)ovflwbuf,0,dz->buflen2 * sizeof(float));
  1183. obufpos -= dz->buflen2;
  1184. }
  1185. }
  1186. } else {
  1187. for(n=0;n<wavelen1;n++) // Write 1st spline to outbuf
  1188. obuf[obufpos++] = splinebuf1[n];
  1189. thistime += (double)wavelen1/srate; // Update time in output
  1190. if(thistime >= dz->param[SPLIN_DUR])
  1191. break;
  1192. if(obufpos >= dz->buflen2) {
  1193. if((exit_status = write_samps(obuf,dz->buflen2,dz))<0)
  1194. return(exit_status);
  1195. memset((char *)obuf,0,dz->buflen2 * sizeof(float));
  1196. memcpy((char *)obuf,(char *)ovflwbuf,dz->buflen2 * sizeof(float));
  1197. memset((char *)ovflwbuf,0,dz->buflen2 * sizeof(float));
  1198. obufpos -= dz->buflen2;
  1199. }
  1200. splinintp = dz->iparam[SPLIN_INTP];
  1201. if(dz->iparam[SPLIN_MINTP] > 0) {
  1202. rrange = dz->iparam[SPLIN_MINTP] - dz->iparam[SPLIN_INTP];
  1203. rval = (int)floor(drand48() * (double)rrange);
  1204. splinintp += rval;
  1205. }
  1206. wavelendiff = wavelen2 - wavelen1;
  1207. for(j = 0; j < splinintp;j++) { // Create the interpolated splines.
  1208. ampinterp1 = (double)(splinintp - j)/(double)splinintp; // Relative level of 1st spline at this count
  1209. ampinterp2 = (double)j/(double)splinintp; // Relative level of 2nd spline at this count
  1210. if(wavelendiff == 0) { // If splines are of same length
  1211. for(n = 0; n < wavelen1 ;n++) {
  1212. val1 = splinebuf1[n] * ampinterp1;
  1213. val2 = splinebuf2[n] * ampinterp2;
  1214. obuf[obufpos++] = (float)(val1 + val2);
  1215. }
  1216. thistime += (double)wavelen1/srate; // Update time in output
  1217. if(thistime >= dz->param[SPLIN_DUR])
  1218. break;
  1219. } else {
  1220. dpos1 = 0.0; // Fractional sample-read in spline1, initially zero
  1221. dpos2 = 0.0; // Fractional sample-read in spline2, initially zero
  1222. // Interpolate for current interpoalted-spline's length
  1223. this_wavelen = wavelen1 + (int)round((double)wavelendiff * ((double)(j+1)/(double)(splinintp+1)));
  1224. dincr1 = (double)wavelen1/(double)this_wavelen; // Fractional sample-advance in 1st spline
  1225. dincr2 = (double)wavelen2/(double)this_wavelen; // Fractional sample-advance in 2nd spline
  1226. for(n = 0, m = this_wavelen; n < this_wavelen ;n++,m--) {
  1227. // spline 1 : Interpolate timewise for value
  1228. k = (int)floor(dpos1);
  1229. frac = dpos1 - (double)k;
  1230. diff = splinebuf1[k+1] - splinebuf1[k];
  1231. val1 = splinebuf1[k] + (diff * frac);
  1232. // spline 2 : Interpolate timewise for value
  1233. k = (int)floor(dpos2);
  1234. frac = dpos2 - (double)k;
  1235. diff = splinebuf2[k+1] - splinebuf2[k];
  1236. val2 = splinebuf2[k] + (diff * frac);
  1237. // Interpolate values for relative amplitude
  1238. val1 *= ampinterp1;
  1239. val2 *= ampinterp2;
  1240. obuf[obufpos++] = (float)(val1 + val2);
  1241. // Advance in original-splines
  1242. dpos1 += dincr1;
  1243. dpos2 += dincr2;
  1244. }
  1245. thistime += (double)this_wavelen/srate; // Update time in output
  1246. if(thistime >= dz->param[SPLIN_DUR])
  1247. break;
  1248. }
  1249. if(obufpos >= dz->buflen2) {
  1250. if((exit_status = write_samps(obuf,dz->buflen2,dz))<0)
  1251. return(exit_status);
  1252. memset((char *)obuf,0,dz->buflen2 * sizeof(float));
  1253. memcpy((char *)obuf,(char *)ovflwbuf,dz->buflen2 * sizeof(float));
  1254. memset((char *)ovflwbuf,0,dz->buflen2 * sizeof(float));
  1255. obufpos -= dz->buflen2;
  1256. }
  1257. }
  1258. }
  1259. }
  1260. }
  1261. if(obufpos > 0) {
  1262. if((exit_status = write_samps(obuf,obufpos,dz))<0)
  1263. return(exit_status);
  1264. }
  1265. return FINISHED;
  1266. }
  1267. /**************************** CREATE_SYNSPLINE_SNDBUFS ****************************/
  1268. int create_synspline_sndbufs(dataptr dz)
  1269. {
  1270. int exit_status;
  1271. int bigbufsize;
  1272. int framesize, maxwavelen, segsize, max_intp;
  1273. double frqmin, maxintp;
  1274. framesize = F_SECSIZE;
  1275. if(dz->brksize[SPLIN_FRQ]) {
  1276. if((exit_status = get_minvalue_in_brktable(&frqmin,SPLIN_FRQ,dz))<0)
  1277. return exit_status;
  1278. } else
  1279. frqmin = dz->param[SPLIN_FRQ];
  1280. if(dz->is_drift)
  1281. frqmin *= pow(2.0,-dz->maxdrift/SEMITONES_PER_OCTAVE);
  1282. maxwavelen = (int)ceil((double)dz->iparam[SPLIN_SRATE]/frqmin);
  1283. segsize = maxwavelen/framesize;
  1284. if(segsize + framesize != maxwavelen)
  1285. segsize++;
  1286. maxwavelen = segsize * framesize;
  1287. dz->buflen = maxwavelen + 4; // SAFETY
  1288. if(dz->brksize[SPLIN_INTP]) {
  1289. if((exit_status = get_maxvalue_in_brktable(&maxintp,SPLIN_INTP,dz))<0)
  1290. return exit_status;
  1291. max_intp = (int)round(maxintp);
  1292. } else
  1293. max_intp = dz->iparam[SPLIN_INTP];
  1294. dz->buflen2 = (dz->buflen * (max_intp + 1)) + 4;
  1295. bigbufsize = ((dz->buflen + dz->buflen2) * 2) * sizeof(float);
  1296. if(dz->is_drift)
  1297. bigbufsize += dz->buflen2 * sizeof(float);
  1298. if((dz->bigbuf = (float *)malloc(bigbufsize)) == NULL) {
  1299. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  1300. return(PROGRAM_ERROR);
  1301. }
  1302. dz->bufcnt = 5;
  1303. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  1304. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  1305. return(MEMORY_ERROR);
  1306. }
  1307. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  1308. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  1309. return(MEMORY_ERROR);
  1310. }
  1311. dz->sbufptr[0] = dz->sampbuf[0] = dz->bigbuf; // splinebuf1
  1312. dz->sbufptr[1] = dz->sampbuf[1] = dz->sampbuf[0] + dz->buflen; // splinebuf2
  1313. /*dz->sampbuf[2] = */ dz->sampbuf[2] = dz->sampbuf[1] + dz->buflen; // obuf
  1314. /*dz->sampbuf[3] = */ dz->sampbuf[3] = dz->sampbuf[2] + dz->buflen2; // ovflwbuf
  1315. if(dz->is_drift) {
  1316. /* dz->sampbuf[4] = */ dz->sampbuf[4] = dz->sampbuf[3] + dz->buflen2; // pre-drift interpolation buffer
  1317. dz->sampbuf[5] = dz->sampbuf[5] + dz->buflen2; // end of pre-drift buffer
  1318. } else
  1319. dz->sampbuf[4] = dz->sampbuf[4] + dz->buflen2; // end of ovflwbuf
  1320. return FINISHED;
  1321. }
  1322. /**************************** CHECK_SYNSPLINE_PARAM_VALIDITY_AND_CONSISTENCY *************************/
  1323. int check_synspline_param_validity_and_consistency(dataptr dz)
  1324. {
  1325. int exit_status;
  1326. double maxfrq, firstfrq, dmaxcnt, dmmaxcnt, maxdriftvel, mindriftvel;
  1327. int minwavelen, firstwavelen, maxcnt, mmaxcnt, sampdur;
  1328. dz->maxdrift = 0.0;
  1329. if(dz->brksize[SPLIN_FRQ]) {
  1330. if((exit_status = get_maxvalue_in_brktable(&maxfrq,SPLIN_FRQ,dz))<0)
  1331. return exit_status;
  1332. } else
  1333. maxfrq = dz->param[SPLIN_FRQ];
  1334. if(dz->brksize[SPLIN_DRIFT]) {
  1335. if((exit_status = get_maxvalue_in_brktable(&(dz->maxdrift),SPLIN_FRQ,dz))<0)
  1336. return exit_status;
  1337. } else
  1338. dz->maxdrift = dz->param[SPLIN_DRIFT];
  1339. if(dz->maxdrift == 0.0) {
  1340. if(dz->brksize[SPLIN_DRVEL] || dz->param[SPLIN_DRVEL] > 0.0) {
  1341. fprintf(stdout,"WARNING: Drift transposition zero or not set. Ignoring drift step value(s)\n");
  1342. fflush(stdout);
  1343. dz->param[SPLIN_DRVEL] = 0.0;
  1344. dz->brksize[SPLIN_DRVEL] = 0;
  1345. }
  1346. } else {
  1347. if(dz->brksize[SPLIN_DRVEL]) {
  1348. if((exit_status = get_maxvalue_in_brktable(&maxdriftvel,SPLIN_DRVEL,dz))<0)
  1349. return exit_status;
  1350. if((exit_status = get_minvalue_in_brktable(&mindriftvel,SPLIN_DRVEL,dz))<0)
  1351. return exit_status;
  1352. } else {
  1353. mindriftvel = dz->param[SPLIN_DRVEL];
  1354. maxdriftvel = dz->param[SPLIN_DRVEL];
  1355. }
  1356. if(mindriftvel < 1.0) {
  1357. sprintf(errstr,"If drift transposition set (and not 0), vals for drift step must be set (and not less than 1mS).\n");
  1358. return DATA_ERROR;
  1359. }
  1360. }
  1361. if(dz->maxdrift > 0.0) {
  1362. maxfrq *= pow(2.0,dz->maxdrift/SEMITONES_PER_OCTAVE);
  1363. dz->is_drift = 1;
  1364. }
  1365. minwavelen = (int)floor((double)dz->iparam[SPLIN_SRATE]/maxfrq);
  1366. if(dz->brksize[SPLIN_CNT]) {
  1367. if((exit_status = get_maxvalue_in_brktable(&dmaxcnt,SPLIN_CNT,dz))<0)
  1368. return exit_status;
  1369. maxcnt = (int)round(dmaxcnt);
  1370. } else
  1371. maxcnt = dz->iparam[SPLIN_CNT];
  1372. if(dz->brksize[SPLIN_MCNT]) {
  1373. if((exit_status = get_maxvalue_in_brktable(&dmmaxcnt,SPLIN_MCNT,dz))<0)
  1374. return exit_status;
  1375. mmaxcnt = (int)round(dmmaxcnt);
  1376. } else
  1377. mmaxcnt = dz->iparam[SPLIN_MCNT];
  1378. maxcnt = max(maxcnt,mmaxcnt);
  1379. dz->itemcnt = maxcnt + 4; // SAFETY
  1380. if((dz->parray = (double **)malloc(5 * sizeof(double *)))==NULL) {
  1381. sprintf(errstr,"INSUFFICIENT MEMORY to store splint randvals. (1)\n");
  1382. return(MEMORY_ERROR);
  1383. }
  1384. if((dz->parray[0] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1385. sprintf(errstr,"INSUFFICIENT MEMORY to store splint randvals. (2)\n");
  1386. return(MEMORY_ERROR);
  1387. }
  1388. if((dz->parray[1] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1389. sprintf(errstr,"INSUFFICIENT MEMORY for storing spline times.\n");
  1390. return(MEMORY_ERROR);
  1391. }
  1392. if((dz->parray[2] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1393. sprintf(errstr,"INSUFFICIENT MEMORY for storing u-coordinates for spline.\n");
  1394. return(MEMORY_ERROR);
  1395. }
  1396. if((dz->parray[3] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1397. sprintf(errstr,"INSUFFICIENT MEMORY for storing 2nd derivatives for spline.\n");
  1398. return(MEMORY_ERROR);
  1399. }
  1400. if((dz->parray[4] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1401. sprintf(errstr,"INSUFFICIENT MEMORY for storing rand values for splining.\n");
  1402. return(MEMORY_ERROR);
  1403. }
  1404. if((dz->lparray = (int **)malloc(sizeof(int *)))==NULL) {
  1405. sprintf(errstr,"INSUFFICIENT MEMORY to store splint sampvals. (1)\n");
  1406. return(MEMORY_ERROR);
  1407. }
  1408. if((dz->lparray[0] = (int *)malloc(dz->itemcnt * sizeof(int)))==NULL) {
  1409. sprintf(errstr,"INSUFFICIENT MEMORY to store splint sampvals. (2)\n");
  1410. return(MEMORY_ERROR);
  1411. }
  1412. if(maxcnt > 0) {
  1413. if(minwavelen/2/maxcnt < 2) {
  1414. sprintf(errstr,"Maximum frq and maximum spline count are incompatible. Reduce one or both of these.\n");
  1415. return DATA_ERROR;
  1416. }
  1417. }
  1418. sampdur = (int)floor(dz->param[SPLIN_DUR] * (double)dz->iparam[SPLIN_SRATE]);
  1419. if(dz->brksize[SPLIN_FRQ])
  1420. firstfrq = dz->brk[SPLIN_FRQ][1];
  1421. else
  1422. firstfrq = dz->param[SPLIN_FRQ];
  1423. firstwavelen = (int)ceil((double)dz->iparam[SPLIN_SRATE]/firstfrq);
  1424. if(firstwavelen > sampdur) {
  1425. sprintf(errstr,"Duration too short to generate a complete wavecycle.\n");
  1426. return DATA_ERROR;
  1427. }
  1428. if(dz->iparam[SPLIN_SEED] > 0)
  1429. srand(dz->iparam[SPLIN_SEED]);
  1430. else
  1431. initrand48();
  1432. return FINISHED;
  1433. }
  1434. /*************************************** COSPLINE *******************************
  1435. *
  1436. * Establish times for cosplint
  1437. */
  1438. int cospline(int half_wavelen, int splincnt, dataptr dz)
  1439. {
  1440. int *splintsamps = dz->lparray[0];
  1441. double *splintime = dz->parray[1];
  1442. int n;
  1443. double srate = (double)dz->param[SPLIN_SRATE];
  1444. splintime[0] = 0.0;
  1445. for(n=0;n < splincnt;n++) // There are SPLIN-CNT+1 points
  1446. splintime[n] = (double)splintsamps[n]/srate;
  1447. splintime[n] = (double)half_wavelen/srate;
  1448. return FINISHED;
  1449. }
  1450. /*************************************** SPLINT *******************************
  1451. *
  1452. * Do cosinusoidal spline.
  1453. */
  1454. double cosplint(int *hi,double time,int splincnt,dataptr dz)
  1455. {
  1456. double *splintime = dz->parray[1], *splintamp = dz->parray[4];
  1457. double timestep, timelo, timefrac, valstep, vallo, val;
  1458. if(time <= 0.0)
  1459. return 0.0;
  1460. while(time > splintime[*hi]) {
  1461. (*hi)++;
  1462. if(*hi > splincnt + 1)
  1463. return 0.0; // end of in table
  1464. }
  1465. vallo = splintamp[(*hi)-1];
  1466. timelo = splintime[(*hi)-1];
  1467. timestep = splintime[*hi] - timelo;
  1468. valstep = splintamp[*hi] - vallo;
  1469. timefrac = (time - timelo)/timestep;
  1470. val = cos(timefrac * PI); // cos 0-PI: range 1 to -1
  1471. val = -val; // -cos: range -1 to 1
  1472. val += 1; // -cos+1: range 0 to 2
  1473. val /= 2; // (-cos+1)/2 range 0 to 1
  1474. val *= valstep;
  1475. val += vallo;
  1476. return val;
  1477. }
  1478. /******************************************* DO_A_SPLINE *****************************************/
  1479. int do_a_spline(int *wavelen,double srate,dataptr dz)
  1480. {
  1481. int gotit = 0, n, m, hi = 0, orig_splincnt, rval, splincnt = 0;
  1482. int half_wavelen;
  1483. double maxrand, temp,/*maxsamp,*/ rrange;
  1484. int *splintsamps = dz->lparray[0];
  1485. double *splintrand = dz->parray[4], *splintamp = dz->parray[4];
  1486. float *splinebuf2 = dz->sampbuf[1];
  1487. orig_splincnt = dz->iparam[SPLIN_CNT];
  1488. if(dz->iparam[SPLIN_MCNT] > 0) {
  1489. rrange = dz->iparam[SPLIN_MCNT] - dz->iparam[SPLIN_CNT];
  1490. rval = (int)floor(drand48() * (double)rrange);
  1491. orig_splincnt += rval;
  1492. }
  1493. *wavelen = (int)ceil((double)dz->iparam[SPLIN_SRATE]/dz->param[SPLIN_FRQ]);
  1494. half_wavelen = (*wavelen)/2;
  1495. *wavelen = half_wavelen * 2;
  1496. if(orig_splincnt == 0) {
  1497. for(n = 0; n < *wavelen; n++)
  1498. splinebuf2[n] = (float)(MAXOUTAMP * sin(((double)n/(double)*wavelen) * TWOPI));
  1499. return FINISHED;
  1500. }
  1501. while(!gotit) {
  1502. splincnt = orig_splincnt;
  1503. for(n = 0; n <= splincnt;n++)
  1504. splintrand[n] = drand48(); // Generate splincnt+1 rand vals for splint positions
  1505. maxrand = splintrand[0];
  1506. for(n = 1; n <= splincnt;n++)
  1507. maxrand = max(maxrand,splintrand[n]); // Find maxrand value
  1508. for(n = 0; n <= splincnt;n++)
  1509. splintrand[n] /= maxrand; // Divide all by maxrand, so values in range 0 to 1
  1510. for(n = 0; n < splincnt;n++) {
  1511. for(m = n; m <= splincnt;m++) {// Sort vals into ascending order
  1512. if(splintrand[m] < splintrand[n]) {
  1513. temp = splintrand[n];
  1514. splintrand[n] = splintrand[m];
  1515. splintrand[m] = temp;
  1516. }
  1517. }
  1518. }
  1519. for(n = 0; n < splincnt;n++) // Convert sorted rands to sample counts within half-wavelen
  1520. splintsamps[n] = (int)round(splintrand[n] * (double)half_wavelen);
  1521. splintsamps[n] = half_wavelen; // Force last val to be at half-wavelen end.
  1522. for(n = 0; n < splincnt;n++) { // Remove any identical sample values, and reduce splint_cnt appropriately
  1523. if(splintsamps[n+1] == splintsamps[n]) {
  1524. for(m = n;m < splincnt;m++)
  1525. splintsamps[n] = splintsamps[n+1];
  1526. splincnt--;
  1527. }
  1528. }
  1529. if(splincnt <= 0)
  1530. continue;
  1531. if(splintsamps[0] < 2) { // Remove too sudden onsets
  1532. for(n = 0;n < splincnt;n++)
  1533. splintsamps[n] = splintsamps[n+1];
  1534. splincnt--;
  1535. }
  1536. if(splincnt <= 0)
  1537. continue;
  1538. if(half_wavelen - splintsamps[splincnt - 1] < 2) {
  1539. for(n = 0;n < splincnt;n++)
  1540. splintsamps[n] = splintsamps[n+1];
  1541. splincnt--;
  1542. }
  1543. if(splincnt <= 0)
  1544. continue;
  1545. gotit = 1;
  1546. }
  1547. memset((char *)splintrand,0, dz->itemcnt * sizeof(float));
  1548. for(n = 0; n < splincnt;n++)
  1549. splintamp[n] = drand48(); // Generate splincnt rand vals for amplitude
  1550. if(dz->vflag[CYCLENORM]) {
  1551. maxrand = splintamp[0];
  1552. for(n = 1; n < splincnt;n++)
  1553. maxrand = max(maxrand,splintamp[n]); // Find maxrand value
  1554. for(n = 0; n < splincnt;n++) // And normalise (amp) values within cycle
  1555. splintamp[n] /= maxrand;
  1556. }
  1557. for(n = 0; n < splincnt;n++) // Keep output within distortion limits
  1558. splintamp[n] = (float)(splintamp[n] * MAXOUTAMP);
  1559. splintamp[n] = 0.0; // Force final point to amplitude zero
  1560. cospline(half_wavelen,splincnt,dz); // Calculate times for splining
  1561. for(n=0; n < half_wavelen;n++) // Do COSIN-interps
  1562. splinebuf2[n] = (float)cosplint(&hi,(double)n/srate,splincnt,dz);
  1563. // maxsamp = 0.0;
  1564. for(n=0,m = half_wavelen; n < half_wavelen;n++,m++)
  1565. splinebuf2[m] = (float)(-splinebuf2[n]);
  1566. return FINISHED;
  1567. }