repeater.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  1. // _cdprogs\repeater repeater 3 alan_bellydancefc.wav test.wav repeater2.txt 8 .66 .66 -r2 -p.5
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <structures.h>
  5. #include <tkglobals.h>
  6. #include <pnames.h>
  7. #include <filetype.h>
  8. #include <processno.h>
  9. #include <modeno.h>
  10. #include <logic.h>
  11. #include <globcon.h>
  12. #include <cdpmain.h>
  13. #include <math.h>
  14. #include <mixxcon.h>
  15. #include <osbind.h>
  16. #include <standalone.h>
  17. #include <science.h>
  18. #include <ctype.h>
  19. #include <sfsys.h>
  20. #include <string.h>
  21. #include <srates.h>
  22. #define SAFETY 64
  23. #define maxmaxbuf total_windows
  24. #define envwindowlen ringsize
  25. #define arraysize rampbrksize
  26. #define REPCLIP 0.95 // level to normalise to
  27. #define REPMINDEL 0.02 // minimum delay to produce oscillaor effect
  28. #ifdef unix
  29. #define round(x) lround((x))
  30. #endif
  31. char errstr[2400];
  32. int anal_infiles = 1;
  33. int sloom = 0;
  34. int sloombatch = 0;
  35. const char* cdp_version = "6.1.0";
  36. //CDP LIB REPLACEMENTS
  37. static int setup_repeater_application(dataptr dz);
  38. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  39. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  40. static int setup_repeater_param_ranges_and_defaults(dataptr dz);
  41. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  42. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  43. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  44. static int establish_application(dataptr dz);
  45. static int initialise_vflags(dataptr dz);
  46. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  47. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  48. static int mark_parameter_types(dataptr dz,aplptr ap);
  49. static int assign_file_data_storage(int infilecnt,dataptr dz);
  50. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  51. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  52. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  53. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  54. static int create_repeater_sndbufs(double maxseglen, double maxovlp, dataptr dz);
  55. static int handle_the_special_data(char *str,double *maxseglen,double *maxovlp,dataptr dz);
  56. static int repeater(dataptr dz);
  57. static int setup_repeater_param_ranges_and_defaults(dataptr dz);
  58. static int setup_repeater_application(dataptr dz);
  59. static int write_and_reset_obuf(int samps_to_write,int *obufpos,dataptr dz);
  60. static int reset_ibuf_and_read(int *ibufpos,dataptr dz) ;
  61. static int normalise_buffer(int samplen,dataptr dz);
  62. static int calc_output_dur(int *dursamps,dataptr dz);
  63. /**************************************** MAIN *********************************************/
  64. int main(int argc,char *argv[])
  65. {
  66. int exit_status;
  67. dataptr dz = NULL;
  68. char **cmdline;
  69. int cmdlinecnt;
  70. int n;
  71. double maxseglen, maxovlp;
  72. aplptr ap;
  73. int is_launched = FALSE;
  74. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  75. fprintf(stdout,"%s\n",cdp_version);
  76. fflush(stdout);
  77. return 0;
  78. }
  79. /* CHECK FOR SOUNDLOOM */
  80. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  81. sloom = 0;
  82. sloombatch = 1;
  83. }
  84. if(sflinit("cdp")){
  85. sfperror("cdp: initialisation\n");
  86. return(FAILED);
  87. }
  88. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  89. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  90. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  91. return(FAILED);
  92. }
  93. if(!sloom) {
  94. if(argc == 1) {
  95. usage1();
  96. return(FAILED);
  97. } else if(argc == 2) {
  98. usage2(argv[1]);
  99. return(FAILED);
  100. }
  101. }
  102. if(!sloom) {
  103. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  104. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  105. return(FAILED);
  106. }
  107. cmdline = argv;
  108. cmdlinecnt = argc;
  109. if((get_the_process_no(argv[0],dz))<0)
  110. return(FAILED);
  111. cmdline++;
  112. cmdlinecnt--;
  113. dz->maxmode = 3;
  114. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  115. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  116. return(exit_status);
  117. }
  118. cmdline++;
  119. cmdlinecnt--;
  120. // setup_particular_application =
  121. if((exit_status = setup_repeater_application(dz))<0) {
  122. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  123. return(FAILED);
  124. }
  125. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  126. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  127. return(FAILED);
  128. }
  129. } else {
  130. //parse_TK_data() =
  131. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  132. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  133. return(exit_status);
  134. }
  135. }
  136. ap = dz->application;
  137. // parse_infile_and_hone_type() =
  138. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  139. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  140. return(FAILED);
  141. }
  142. // setup_param_ranges_and_defaults() =
  143. if((exit_status = setup_repeater_param_ranges_and_defaults(dz))<0) {
  144. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  145. return(FAILED);
  146. }
  147. // open_first_infile CDP LIB
  148. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  149. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  150. return(FAILED);
  151. }
  152. cmdlinecnt--;
  153. cmdline++;
  154. // handle_extra_infiles() : redundant
  155. // handle_outfile() =
  156. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  157. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  158. return(FAILED);
  159. }
  160. // handle_formants() redundant
  161. // handle_formant_quiksearch() redundant
  162. // handle_special_data() =
  163. if((exit_status = handle_the_special_data(cmdline[0],&maxseglen,&maxovlp,dz))<0) {
  164. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  165. return(FAILED);
  166. }
  167. cmdlinecnt--;
  168. cmdline++;
  169. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  170. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  171. return(FAILED);
  172. }
  173. // check_param_validity_and_consistency() redundant
  174. is_launched = TRUE;
  175. dz->bufcnt = 7;
  176. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  177. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  178. return(MEMORY_ERROR);
  179. }
  180. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  181. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  182. return(MEMORY_ERROR);
  183. }
  184. for(n = 0;n <dz->bufcnt; n++)
  185. dz->sampbuf[n] = dz->sbufptr[n] = (float *)0;
  186. dz->sampbuf[n] = (float *)0;
  187. // create_sndbufs() =
  188. if((exit_status = create_repeater_sndbufs(maxseglen,maxovlp,dz))<0) { // CDP LIB
  189. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  190. return(FAILED);
  191. }
  192. //param_preprocess() redundant
  193. //spec_process_file =
  194. if((exit_status = repeater(dz))<0) {
  195. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  196. return(FAILED);
  197. }
  198. if((exit_status = complete_output(dz))<0) { // CDP LIB
  199. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  200. return(FAILED);
  201. }
  202. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  203. free(dz);
  204. return(SUCCEEDED);
  205. }
  206. /**********************************************
  207. REPLACED CDP LIB FUNCTIONS
  208. **********************************************/
  209. /****************************** SET_PARAM_DATA *********************************/
  210. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  211. {
  212. ap->special_data = (char)special_data;
  213. ap->param_cnt = (char)paramcnt;
  214. ap->max_param_cnt = (char)maxparamcnt;
  215. if(ap->max_param_cnt>0) {
  216. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  217. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  218. return(MEMORY_ERROR);
  219. }
  220. strcpy(ap->param_list,paramlist);
  221. }
  222. return(FINISHED);
  223. }
  224. /****************************** SET_VFLGS *********************************/
  225. int set_vflgs
  226. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  227. {
  228. ap->option_cnt = (char) optcnt; /*RWD added cast */
  229. if(optcnt) {
  230. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  231. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  232. return(MEMORY_ERROR);
  233. }
  234. strcpy(ap->option_list,optlist);
  235. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  236. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  237. return(MEMORY_ERROR);
  238. }
  239. strcpy(ap->option_flags,optflags);
  240. }
  241. ap->vflag_cnt = (char) vflagcnt;
  242. ap->variant_param_cnt = (char) vparamcnt;
  243. if(vflagcnt) {
  244. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  245. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  246. return(MEMORY_ERROR);
  247. }
  248. strcpy(ap->variant_list,varlist);
  249. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  250. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  251. return(MEMORY_ERROR);
  252. }
  253. strcpy(ap->variant_flags,varflags);
  254. }
  255. return(FINISHED);
  256. }
  257. /***************************** APPLICATION_INIT **************************/
  258. int application_init(dataptr dz)
  259. {
  260. int exit_status;
  261. int storage_cnt;
  262. int tipc, brkcnt;
  263. aplptr ap = dz->application;
  264. if(ap->vflag_cnt>0)
  265. initialise_vflags(dz);
  266. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  267. ap->total_input_param_cnt = (char)tipc;
  268. if(tipc>0) {
  269. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  270. return(exit_status);
  271. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  272. return(exit_status);
  273. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  274. return(exit_status);
  275. }
  276. brkcnt = tipc;
  277. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  278. if(brkcnt>0) {
  279. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  280. return(exit_status);
  281. }
  282. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  283. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  284. return(exit_status);
  285. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  286. return(exit_status);
  287. }
  288. if((exit_status = mark_parameter_types(dz,ap))<0)
  289. return(exit_status);
  290. // establish_infile_constants() replaced by
  291. dz->infilecnt = 1;
  292. //establish_bufptrs_and_extra_buffers():
  293. return(FINISHED);
  294. }
  295. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  296. /* RWD mallo changed to calloc; helps debug verison run as release! */
  297. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  298. {
  299. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  300. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  301. return(MEMORY_ERROR);
  302. }
  303. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  304. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  305. return(MEMORY_ERROR);
  306. }
  307. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  308. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  309. return(MEMORY_ERROR);
  310. }
  311. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  312. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  313. return(MEMORY_ERROR);
  314. }
  315. return(FINISHED);
  316. }
  317. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  318. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  319. {
  320. int n;
  321. for(n=0;n<storage_cnt;n++) {
  322. dz->is_int[n] = (char)0;
  323. dz->no_brk[n] = (char)0;
  324. }
  325. return(FINISHED);
  326. }
  327. /***************************** MARK_PARAMETER_TYPES **************************/
  328. int mark_parameter_types(dataptr dz,aplptr ap)
  329. {
  330. int n, m; /* PARAMS */
  331. for(n=0;n<ap->max_param_cnt;n++) {
  332. switch(ap->param_list[n]) {
  333. case('0'): break; /* dz->is_active[n] = 0 is default */
  334. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  335. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  336. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  337. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  338. default:
  339. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  340. return(PROGRAM_ERROR);
  341. }
  342. } /* OPTIONS */
  343. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  344. switch(ap->option_list[n]) {
  345. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  346. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  347. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  348. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  349. default:
  350. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  351. return(PROGRAM_ERROR);
  352. }
  353. } /* VARIANTS */
  354. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  355. switch(ap->variant_list[n]) {
  356. case('0'): break;
  357. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  358. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  359. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  360. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  361. default:
  362. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  363. return(PROGRAM_ERROR);
  364. }
  365. } /* INTERNAL */
  366. for(n=0,
  367. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  368. switch(ap->internal_param_list[n]) {
  369. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  370. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  371. case('d'): dz->no_brk[m] = (char)1; break;
  372. default:
  373. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  374. return(PROGRAM_ERROR);
  375. }
  376. }
  377. return(FINISHED);
  378. }
  379. /************************ HANDLE_THE_OUTFILE *********************/
  380. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  381. {
  382. int exit_status;
  383. char *filename = (*cmdline)[0];
  384. if(filename[0]=='-' && filename[1]=='f') {
  385. dz->floatsam_output = 1;
  386. dz->true_outfile_stype = SAMP_FLOAT;
  387. filename+= 2;
  388. }
  389. if(!sloom) {
  390. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  391. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  392. return(DATA_ERROR);
  393. }
  394. }
  395. strcpy(dz->outfilename,filename);
  396. if((exit_status = create_sized_outfile(filename,dz))<0)
  397. return(exit_status);
  398. (*cmdline)++;
  399. (*cmdlinecnt)--;
  400. return(FINISHED);
  401. }
  402. /***************************** ESTABLISH_APPLICATION **************************/
  403. int establish_application(dataptr dz)
  404. {
  405. aplptr ap;
  406. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  407. sprintf(errstr,"establish_application()\n");
  408. return(MEMORY_ERROR);
  409. }
  410. ap = dz->application;
  411. memset((char *)ap,0,sizeof(struct applic));
  412. return(FINISHED);
  413. }
  414. /************************* INITIALISE_VFLAGS *************************/
  415. int initialise_vflags(dataptr dz)
  416. {
  417. int n;
  418. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  419. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  420. return(MEMORY_ERROR);
  421. }
  422. for(n=0;n<dz->application->vflag_cnt;n++)
  423. dz->vflag[n] = FALSE;
  424. return FINISHED;
  425. }
  426. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  427. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  428. {
  429. int n;
  430. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  431. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  432. return(MEMORY_ERROR);
  433. }
  434. for(n=0;n<tipc;n++)
  435. ap->default_val[n] = 0.0;
  436. return(FINISHED);
  437. }
  438. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  439. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  440. {
  441. int n;
  442. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  443. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  444. return(MEMORY_ERROR);
  445. }
  446. for(n=0;n<tipc;n++)
  447. dz->is_active[n] = (char)0;
  448. return(FINISHED);
  449. }
  450. /************************* SETUP_REPEATER_APPLICATION *******************/
  451. int setup_repeater_application(dataptr dz)
  452. {
  453. int exit_status;
  454. aplptr ap;
  455. if((exit_status = establish_application(dz))<0) // GLOBAL
  456. return(FAILED);
  457. ap = dz->application;
  458. // SEE parstruct FOR EXPLANATION of next 2 functions
  459. if(dz->mode < 2)
  460. exit_status = set_param_data(ap,REPEATDATA,3,0,"000");
  461. else
  462. exit_status = set_param_data(ap,REPEATDATA,3,3,"DDD");
  463. if(exit_status <0)
  464. return(FAILED);
  465. if((exit_status = set_vflgs(ap,"rp",3,"DDi","",0,0,""))<0)
  466. return(FAILED);
  467. // set_legal_infile_structure -->
  468. dz->has_otherfile = FALSE;
  469. // assign_process_logic -->
  470. dz->input_data_type = SNDFILES_ONLY;
  471. dz->process_type = UNEQUAL_SNDFILE;
  472. dz->outfiletype = SNDFILE_OUT;
  473. return application_init(dz); //GLOBAL
  474. }
  475. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  476. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  477. {
  478. int exit_status;
  479. infileptr infile_info;
  480. if(!sloom) {
  481. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  482. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  483. return(MEMORY_ERROR);
  484. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  485. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  486. return(PROGRAM_ERROR);
  487. } else if(infile_info->filetype != SNDFILE) {
  488. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  489. return(DATA_ERROR);
  490. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  491. sprintf(errstr,"Failed to copy file parsing information\n");
  492. return(PROGRAM_ERROR);
  493. }
  494. free(infile_info);
  495. }
  496. return(FINISHED);
  497. }
  498. /************************* SETUP_REPEATER_PARAM_RANGES_AND_DEFAULTS *******************/
  499. int setup_repeater_param_ranges_and_defaults(dataptr dz)
  500. {
  501. int exit_status;
  502. aplptr ap = dz->application;
  503. // set_param_ranges()
  504. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  505. // NB total_input_param_cnt is > 0 !!!
  506. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  507. return(FAILED);
  508. // get_param_ranges()
  509. if(dz->mode == 2) {
  510. ap->lo[REP_ACCEL] = 1.0;
  511. ap->hi[REP_ACCEL] = 10.0;
  512. ap->default_val[REP_ACCEL] = 2.0;
  513. ap->lo[REP_WARP] = 0.1;
  514. ap->hi[REP_WARP] = 10.0;
  515. ap->default_val[REP_WARP] = 0.66;
  516. ap->lo[REP_FADE] = 0.1;
  517. ap->hi[REP_FADE] = 10.0;
  518. ap->default_val[REP_FADE] = .33;
  519. }
  520. ap->lo[REP_RAND] = 1.0;
  521. if(dz->mode == 1)
  522. ap->hi[REP_RAND] = 8.0;
  523. else
  524. ap->hi[REP_RAND] = 2.0;
  525. ap->default_val[REP_RAND] = 1.0;
  526. ap->lo[REP_TRNSP] = 0.0;
  527. ap->hi[REP_TRNSP] = 12.0;
  528. ap->default_val[REP_TRNSP] = 0.0;
  529. ap->lo[REP_SEED] = 0;
  530. ap->hi[REP_SEED] = 256;
  531. ap->default_val[REP_SEED] = 0;
  532. dz->maxmode = 3;
  533. if(!sloom)
  534. put_default_vals_in_all_params(dz);
  535. return(FINISHED);
  536. }
  537. /********************************* PARSE_SLOOM_DATA *********************************/
  538. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  539. {
  540. int exit_status;
  541. int cnt = 1, infilecnt;
  542. int filesize, insams, inbrksize;
  543. double dummy;
  544. int true_cnt = 0;
  545. aplptr ap;
  546. while(cnt<=PRE_CMDLINE_DATACNT) {
  547. if(cnt > argc) {
  548. sprintf(errstr,"Insufficient data sent from TK\n");
  549. return(DATA_ERROR);
  550. }
  551. switch(cnt) {
  552. case(1):
  553. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  554. sprintf(errstr,"Cannot read process no. sent from TK\n");
  555. return(DATA_ERROR);
  556. }
  557. break;
  558. case(2):
  559. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  560. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  561. return(DATA_ERROR);
  562. }
  563. if(dz->mode > 0)
  564. dz->mode--;
  565. //setup_particular_application() =
  566. if((exit_status = setup_repeater_application(dz))<0)
  567. return(exit_status);
  568. ap = dz->application;
  569. break;
  570. case(3):
  571. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  572. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  573. return(DATA_ERROR);
  574. }
  575. if(infilecnt < 1) {
  576. true_cnt = cnt + 1;
  577. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  578. }
  579. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  580. return(exit_status);
  581. break;
  582. case(INPUT_FILETYPE+4):
  583. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  584. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  585. return(DATA_ERROR);
  586. }
  587. break;
  588. case(INPUT_FILESIZE+4):
  589. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  590. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  591. return(DATA_ERROR);
  592. }
  593. dz->insams[0] = filesize;
  594. break;
  595. case(INPUT_INSAMS+4):
  596. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  597. sprintf(errstr,"Cannot read insams sent from TK\n");
  598. return(DATA_ERROR);
  599. }
  600. dz->insams[0] = insams;
  601. break;
  602. case(INPUT_SRATE+4):
  603. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  604. sprintf(errstr,"Cannot read srate sent from TK\n");
  605. return(DATA_ERROR);
  606. }
  607. break;
  608. case(INPUT_CHANNELS+4):
  609. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  610. sprintf(errstr,"Cannot read channels sent from TK\n");
  611. return(DATA_ERROR);
  612. }
  613. break;
  614. case(INPUT_STYPE+4):
  615. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  616. sprintf(errstr,"Cannot read stype sent from TK\n");
  617. return(DATA_ERROR);
  618. }
  619. break;
  620. case(INPUT_ORIGSTYPE+4):
  621. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  622. sprintf(errstr,"Cannot read origstype sent from TK\n");
  623. return(DATA_ERROR);
  624. }
  625. break;
  626. case(INPUT_ORIGRATE+4):
  627. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  628. sprintf(errstr,"Cannot read origrate sent from TK\n");
  629. return(DATA_ERROR);
  630. }
  631. break;
  632. case(INPUT_MLEN+4):
  633. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  634. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  635. return(DATA_ERROR);
  636. }
  637. break;
  638. case(INPUT_DFAC+4):
  639. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  640. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  641. return(DATA_ERROR);
  642. }
  643. break;
  644. case(INPUT_ORIGCHANS+4):
  645. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  646. sprintf(errstr,"Cannot read origchans sent from TK\n");
  647. return(DATA_ERROR);
  648. }
  649. break;
  650. case(INPUT_SPECENVCNT+4):
  651. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  652. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  653. return(DATA_ERROR);
  654. }
  655. dz->specenvcnt = dz->infile->specenvcnt;
  656. break;
  657. case(INPUT_WANTED+4):
  658. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  659. sprintf(errstr,"Cannot read wanted sent from TK\n");
  660. return(DATA_ERROR);
  661. }
  662. break;
  663. case(INPUT_WLENGTH+4):
  664. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  665. sprintf(errstr,"Cannot read wlength sent from TK\n");
  666. return(DATA_ERROR);
  667. }
  668. break;
  669. case(INPUT_OUT_CHANS+4):
  670. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  671. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  672. return(DATA_ERROR);
  673. }
  674. break;
  675. /* RWD these chanegs to samps - tk will have to deal with that! */
  676. case(INPUT_DESCRIPTOR_BYTES+4):
  677. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  678. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  679. return(DATA_ERROR);
  680. }
  681. break;
  682. case(INPUT_IS_TRANSPOS+4):
  683. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  684. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  685. return(DATA_ERROR);
  686. }
  687. break;
  688. case(INPUT_COULD_BE_TRANSPOS+4):
  689. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  690. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  691. return(DATA_ERROR);
  692. }
  693. break;
  694. case(INPUT_COULD_BE_PITCH+4):
  695. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  696. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  697. return(DATA_ERROR);
  698. }
  699. break;
  700. case(INPUT_DIFFERENT_SRATES+4):
  701. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  702. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  703. return(DATA_ERROR);
  704. }
  705. break;
  706. case(INPUT_DUPLICATE_SNDS+4):
  707. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  708. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  709. return(DATA_ERROR);
  710. }
  711. break;
  712. case(INPUT_BRKSIZE+4):
  713. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  714. sprintf(errstr,"Cannot read brksize sent from TK\n");
  715. return(DATA_ERROR);
  716. }
  717. if(inbrksize > 0) {
  718. switch(dz->input_data_type) {
  719. case(WORDLIST_ONLY):
  720. break;
  721. case(PITCH_AND_PITCH):
  722. case(PITCH_AND_TRANSPOS):
  723. case(TRANSPOS_AND_TRANSPOS):
  724. dz->tempsize = inbrksize;
  725. break;
  726. case(BRKFILES_ONLY):
  727. case(UNRANGED_BRKFILE_ONLY):
  728. case(DB_BRKFILES_ONLY):
  729. case(ALL_FILES):
  730. case(ANY_NUMBER_OF_ANY_FILES):
  731. if(dz->extrabrkno < 0) {
  732. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  733. return(DATA_ERROR);
  734. }
  735. if(dz->brksize == NULL) {
  736. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  737. return(PROGRAM_ERROR);
  738. }
  739. dz->brksize[dz->extrabrkno] = inbrksize;
  740. break;
  741. default:
  742. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  743. dz->input_data_type);
  744. return(PROGRAM_ERROR);
  745. }
  746. break;
  747. }
  748. break;
  749. case(INPUT_NUMSIZE+4):
  750. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  751. sprintf(errstr,"Cannot read numsize sent from TK\n");
  752. return(DATA_ERROR);
  753. }
  754. break;
  755. case(INPUT_LINECNT+4):
  756. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  757. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  758. return(DATA_ERROR);
  759. }
  760. break;
  761. case(INPUT_ALL_WORDS+4):
  762. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  763. sprintf(errstr,"Cannot read all_words sent from TK\n");
  764. return(DATA_ERROR);
  765. }
  766. break;
  767. case(INPUT_ARATE+4):
  768. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  769. sprintf(errstr,"Cannot read arate sent from TK\n");
  770. return(DATA_ERROR);
  771. }
  772. break;
  773. case(INPUT_FRAMETIME+4):
  774. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  775. sprintf(errstr,"Cannot read frametime sent from TK\n");
  776. return(DATA_ERROR);
  777. }
  778. dz->frametime = (float)dummy;
  779. break;
  780. case(INPUT_WINDOW_SIZE+4):
  781. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  782. sprintf(errstr,"Cannot read window_size sent from TK\n");
  783. return(DATA_ERROR);
  784. }
  785. break;
  786. case(INPUT_NYQUIST+4):
  787. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  788. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  789. return(DATA_ERROR);
  790. }
  791. break;
  792. case(INPUT_DURATION+4):
  793. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  794. sprintf(errstr,"Cannot read duration sent from TK\n");
  795. return(DATA_ERROR);
  796. }
  797. break;
  798. case(INPUT_MINBRK+4):
  799. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  800. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  801. return(DATA_ERROR);
  802. }
  803. break;
  804. case(INPUT_MAXBRK+4):
  805. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  806. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  807. return(DATA_ERROR);
  808. }
  809. break;
  810. case(INPUT_MINNUM+4):
  811. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  812. sprintf(errstr,"Cannot read minnum sent from TK\n");
  813. return(DATA_ERROR);
  814. }
  815. break;
  816. case(INPUT_MAXNUM+4):
  817. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  818. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  819. return(DATA_ERROR);
  820. }
  821. break;
  822. default:
  823. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  824. return(PROGRAM_ERROR);
  825. }
  826. cnt++;
  827. }
  828. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  829. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  830. return(DATA_ERROR);
  831. }
  832. if(true_cnt)
  833. cnt = true_cnt;
  834. *cmdlinecnt = 0;
  835. while(cnt < argc) {
  836. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  837. return(exit_status);
  838. cnt++;
  839. }
  840. return(FINISHED);
  841. }
  842. /********************************* GET_TK_CMDLINE_WORD *********************************/
  843. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  844. {
  845. if(*cmdlinecnt==0) {
  846. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  847. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  848. return(MEMORY_ERROR);
  849. }
  850. } else {
  851. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  852. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  853. return(MEMORY_ERROR);
  854. }
  855. }
  856. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  857. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  858. return(MEMORY_ERROR);
  859. }
  860. strcpy((*cmdline)[*cmdlinecnt],q);
  861. (*cmdlinecnt)++;
  862. return(FINISHED);
  863. }
  864. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  865. int assign_file_data_storage(int infilecnt,dataptr dz)
  866. {
  867. int exit_status;
  868. int no_sndfile_system_files = FALSE;
  869. dz->infilecnt = infilecnt;
  870. if((exit_status = allocate_filespace(dz))<0)
  871. return(exit_status);
  872. if(no_sndfile_system_files)
  873. dz->infilecnt = 0;
  874. return(FINISHED);
  875. }
  876. /************************* redundant functions: to ensure libs compile OK *******************/
  877. int assign_process_logic(dataptr dz)
  878. {
  879. return(FINISHED);
  880. }
  881. void set_legal_infile_structure(dataptr dz)
  882. {}
  883. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  884. {
  885. return(FINISHED);
  886. }
  887. int setup_internal_arrays_and_array_pointers(dataptr dz)
  888. {
  889. return(FINISHED);
  890. }
  891. int establish_bufptrs_and_extra_buffers(dataptr dz)
  892. {
  893. return(FINISHED);
  894. }
  895. int read_special_data(char *str,dataptr dz)
  896. {
  897. return(FINISHED);
  898. }
  899. int inner_loop
  900. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  901. {
  902. return(FINISHED);
  903. }
  904. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  905. {
  906. return(FINISHED);
  907. }
  908. /******************************** USAGE1 ********************************/
  909. int usage1(void)
  910. {
  911. usage2("repeater");
  912. return(USAGE_ONLY);
  913. }
  914. /********************************************************************************************/
  915. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  916. {
  917. if(!strcmp(prog_identifier_from_cmdline,"repeater")) dz->process = REPEATER;
  918. else {
  919. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  920. return(USAGE_ONLY);
  921. }
  922. return(FINISHED);
  923. }
  924. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  925. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  926. {
  927. int n;
  928. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  929. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  930. return(MEMORY_ERROR);
  931. }
  932. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  933. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  934. return(MEMORY_ERROR);
  935. }
  936. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  937. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  938. return(MEMORY_ERROR);
  939. }
  940. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  941. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  942. return(MEMORY_ERROR);
  943. }
  944. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  945. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  946. return(MEMORY_ERROR);
  947. }
  948. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  949. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  950. return(MEMORY_ERROR);
  951. }
  952. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  953. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  954. return(MEMORY_ERROR);
  955. }
  956. for(n=0;n<brkcnt;n++) {
  957. dz->brk[n] = NULL;
  958. dz->brkptr[n] = NULL;
  959. dz->brkinit[n] = 0;
  960. dz->brksize[n] = 0;
  961. }
  962. return(FINISHED);
  963. }
  964. /******************************** USAGE2 ********************************/
  965. int usage2(char *str)
  966. {
  967. if(!strcmp(str,"repeater")) {
  968. fprintf(stderr,
  969. "USAGE: repeater repeater\n"
  970. "1-2 infile outfile datafile [-rrand] [-prand] [-sseed]\n"
  971. "3 infile outfile datafile accel warp fade [-rrand] [-prand] [-sseed]\n"
  972. "\n"
  973. "Play source, with specified elements repeating.\n"
  974. "MODE 3 produces dimming, accelerating output, like bouncing object.\n"
  975. "\n"
  976. "MODES 1 & 3 DATAFILE has sets of 4-values, being....\n"
  977. " \"Start-time\" \"End-time\" \"Repeat-cnt\" \"Delay\"\n"
  978. " with one set-of-values for each element to be repeated.\n"
  979. " Elements can overlap, or baktrak in src, & must be at >= %.3lf secs.\n"
  980. " \"Delay\", is time between start of 1 repeated element & start of next.\n"
  981. " Delay zero will produce a delay equal to the segment length.\n"
  982. " Otherwise, delays < 0.05 secs may produce output like oscillator.\n"
  983. "\n"
  984. "MODE 2 DATAFILE has sets of 4-values, being....\n"
  985. " \"Start-time\" \"End-time\" \"Repeat-cnt\" \"Offset\"\n"
  986. " Similar to MODE 1 but using \"Offset\" instead of \"Delay\".\n"
  987. " \"Offset\", for any repeating segment,\n"
  988. " is the gap between end of one repeated element and start of next.\n"
  989. "\n"
  990. "RAND Randomise delay:\n"
  991. " Mode 1&3: Extend each delay-time by a random multiple.\n"
  992. " Multiplier generated in range you specify (between 1 & 2).\n"
  993. " Mode 2: Extend each offset-time by a random multiple.\n"
  994. " Multiplier generated in range you specify (between 1 & 8).\n"
  995. " Value 1 gives NO randomisation. \"RAND\" may vary through time.\n"
  996. "\n"
  997. "PRAND Randomise pitch of repeats within given semitone range (between 0 & 12)\n"
  998. " \"PRAND\" may vary through time.\n"
  999. "\n"
  1000. "SEED An integer value. repeated runs of process with same input\n"
  1001. " and same seed value will give identical output.\n"
  1002. "\n"
  1003. "ACCEL Delay (& segment) shortening by end of repeats\n"
  1004. " e.g. accel = 2 gradually shortens delay to 1/2 its duration.\n"
  1005. "WARP Warps delay change. 1 no warp. > 1 shortens less initially, more later.\n"
  1006. "FADE Decay curve. 1 linear, >1 fast then slow decay, <1 slow then fast.\n",(int)round(REPSPLEN * 2) * MS_TO_SECS,(int)round(REPSPLEN * 2) * MS_TO_SECS);
  1007. } else
  1008. fprintf(stderr,"Unknown option '%s'\n",str);
  1009. return(USAGE_ONLY);
  1010. }
  1011. int usage3(char *str1,char *str2)
  1012. {
  1013. fprintf(stderr,"Insufficient parameters on command line.\n");
  1014. return(USAGE_ONLY);
  1015. }
  1016. /******************************** REPEATER ********************************/
  1017. int repeater(dataptr dz)
  1018. {
  1019. int exit_status, chans = dz->infile->channels, ch, at_start, overlap, varypitch;
  1020. float *ibuf = dz->sampbuf[0], *iovflwbuf = dz->sampbuf[1], *obuf = dz->sampbuf[2], *repbuf = dz->sampbuf[4], *reprepbuf = dz->sampbuf[5], *segbuf;
  1021. double splicelen = REPSPLEN * MS_TO_SECS, srate = (double)dz->infile->srate, maxtransdown = 1.0, maxexpand = 1.0;
  1022. int gp_splicesamps = (int)round(splicelen * srate), possible_gp_samps_to_read, maxrepbufpos, samps_written, outsamps = 0, lastrepbufpos;
  1023. double dgp_splicesamps = (double)gp_splicesamps, val, rnd, thistime, incr, md, frac, diff;
  1024. int total_splicesamps = gp_splicesamps * chans, last_gp_absendsamp, ibufpos, obufpos, repbufpos, bufpos_in_iovflw, totalreps, rep, n, m, k, baktrak;
  1025. int samps_to_read, delaysamps, gp_abssttsamp, gp_absendsamp, repeats, gp_delaysamps, gp_samps_to_read, samps_to_write, startdelay = 0, datacnt, thisdata;
  1026. double *segdata = dz->parray[0];
  1027. int isshorten;
  1028. double lenchange, lenchangeincr = 0.0, lenfact, thisfade, endspliceval;
  1029. int inital_gp_delaysamps = 0, initial_gp_samps_to_read = 0, gp_endsplice = 0, gp_endsplice_stt = 0, endsplice = 0, endsplice_stt = 0;
  1030. srand((int)dz->iparam[REP_SEED]);
  1031. if(sloom) {
  1032. if((exit_status = calc_output_dur(&outsamps,dz))<0)
  1033. return exit_status;
  1034. dz->tempsize = outsamps;
  1035. }
  1036. if(dz->mode != 1) { // Setup enveloping arrays, for signal normalisation
  1037. dz->envwindowlen = gp_splicesamps * 2; // half-windowlen must be no larger than splicesamps (ensuring envelope is val 1 throughout splice)
  1038. dz->envwindowlen *= chans;
  1039. dz->arraysize = dz->buflen2/dz->envwindowlen;
  1040. dz->arraysize += SAFETY;
  1041. if((dz->parray[1] = (double *)malloc(dz->arraysize * sizeof(double)))==NULL) {
  1042. sprintf(errstr,"INSUFFICIENT MEMORY to create envelope array.\n");
  1043. return(MEMORY_ERROR);
  1044. }
  1045. if((dz->iparray = (int **)malloc(sizeof(int *)))==NULL) {
  1046. sprintf(errstr,"INSUFFICIENT MEMORY to create envelope max locations array (1).\n");
  1047. return(MEMORY_ERROR);
  1048. }
  1049. if((dz->iparray[0] = (int *)malloc(dz->arraysize * sizeof(int)))==NULL) {
  1050. sprintf(errstr,"INSUFFICIENT MEMORY to create envelope max locations array (2).\n");
  1051. return(MEMORY_ERROR);
  1052. }
  1053. }
  1054. at_start = 1;
  1055. last_gp_absendsamp = 0;
  1056. if((exit_status = read_samps(iovflwbuf,dz))<0) // Initially read into input-overflow-buf
  1057. return exit_status;
  1058. ibufpos = dz->buflen; // and point into it
  1059. obufpos = 0;
  1060. totalreps = dz->itemcnt/4; // For every repeat unit in data
  1061. for(rep=0, datacnt = 0; rep< totalreps;rep++,datacnt+=4) {
  1062. thisdata = datacnt; // Get the repeat-params
  1063. thistime = segdata[thisdata];
  1064. gp_abssttsamp = (int)round(segdata[thisdata++] * srate);
  1065. gp_absendsamp = (int)round(segdata[thisdata++] * srate);
  1066. repeats = (int)round(segdata[thisdata++]);
  1067. gp_delaysamps = (int)round(segdata[thisdata] * srate);
  1068. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  1069. return exit_status;
  1070. varypitch = 0;
  1071. if(dz->param[REP_TRNSP] != 0.0) {
  1072. varypitch = 1;
  1073. maxtransdown = pow(2.0,-dz->param[REP_TRNSP]/SEMITONES_PER_OCTAVE);
  1074. maxexpand = 1.0/maxtransdown;
  1075. }
  1076. gp_samps_to_read = gp_abssttsamp - last_gp_absendsamp;
  1077. if(gp_samps_to_read > 0) { // If next segment is beyond end of last segment, read from infile.
  1078. // READ any of infile BETWEEN SEGS
  1079. while(obufpos >= dz->buflen + total_splicesamps) {
  1080. if((exit_status = write_and_reset_obuf(dz->buflen,&obufpos,dz))<0)
  1081. return(exit_status); // check if obuf has overflowed
  1082. }
  1083. gp_samps_to_read += gp_splicesamps; // Allow for endsplice-down in read-segment by reading extra from input
  1084. for(n=0,m = gp_samps_to_read - 1;n < gp_samps_to_read; n++,m--) {
  1085. if(n < gp_splicesamps) {
  1086. if(at_start)
  1087. val = 1.0;
  1088. else
  1089. val = (double)n/dgp_splicesamps;// Copy to output, with splices at start, if not at start of infile
  1090. } else if (m < gp_splicesamps)
  1091. val = (double)m/dgp_splicesamps; // and splice at end
  1092. else
  1093. val = 1.0;
  1094. for(ch = 0;ch < chans; ch++) {
  1095. obuf[obufpos] = (float)(obuf[obufpos] + (ibuf[ibufpos] * val));
  1096. obufpos++;
  1097. ibufpos++;
  1098. }
  1099. if(obufpos >= dz->buflen + total_splicesamps) {
  1100. if((exit_status = write_and_reset_obuf(dz->buflen,&obufpos,dz))<0)
  1101. return(exit_status);
  1102. }
  1103. if(ibufpos >= dz->buflen * 2) {
  1104. if((exit_status = reset_ibuf_and_read(&ibufpos,dz))<0)
  1105. return(exit_status);
  1106. if(dz->ssampsread == 0) {
  1107. sprintf(errstr,"Reached end of input prematurely\n");
  1108. return PROGRAM_ERROR;
  1109. }
  1110. }
  1111. }
  1112. at_start = 0;
  1113. obufpos -= total_splicesamps; // Baktrack by splicelen in output;
  1114. ibufpos -= total_splicesamps; // Baktrack by splicelen in input, for start-read of segment.
  1115. } else {
  1116. // ELSE go directly to the new segment
  1117. baktrak = (last_gp_absendsamp - gp_abssttsamp) * chans;
  1118. if(baktrak > 0) { // If this seg starts BEFORE END but AFTER START of previous seg, baktrak in current buf.
  1119. ibufpos -= baktrak; // ibuf has a prebuf as big as the largest possible segment, so baktracking will fall inside inbuf.
  1120. if(ibufpos < 0) { // However, if this segment starts BEFORE START of previous segment, baktraking may go beyond start of prebuf,
  1121. sndseekEx(dz->ifd[0],0,0); // in which case return to start of file ...
  1122. dz->total_samps_read = 0;
  1123. dz->ssampsread = 0; // ...and read...
  1124. memset((char *)ibuf,0,2 * dz->buflen * sizeof(float));
  1125. if((exit_status = read_samps(iovflwbuf,dz))<0)
  1126. return exit_status;
  1127. ibufpos = dz->buflen; // ...until we find required sample-position
  1128. while(dz->total_samps_read < gp_abssttsamp * chans) {
  1129. if((exit_status = reset_ibuf_and_read(&ibufpos,dz))<0)
  1130. return(exit_status);
  1131. if(dz->ssampsread == 0) {
  1132. sprintf(errstr,"Reached end of input prematurely during baktrak in infile.\n");
  1133. return PROGRAM_ERROR;
  1134. }
  1135. }
  1136. if(dz->total_samps_read == dz->ssampsread)
  1137. at_start = 1;
  1138. }
  1139. }
  1140. }
  1141. // READ SEGMENT
  1142. gp_samps_to_read = gp_absendsamp - gp_abssttsamp;// Read the rep-segment into rep-buffer
  1143. if(dz->mode != 1) {
  1144. if(gp_delaysamps == 0)
  1145. gp_delaysamps = gp_samps_to_read; // Special delay value, zero, delays sample by its complete length
  1146. }
  1147. gp_samps_to_read += gp_splicesamps;
  1148. if(varypitch && (maxexpand > 1.0)) // IF segments are transposed downwards, check if they will overlap
  1149. possible_gp_samps_to_read = (int)ceil((double)gp_samps_to_read * maxexpand);
  1150. else
  1151. possible_gp_samps_to_read = gp_samps_to_read;
  1152. if(dz->mode != 1 && (gp_delaysamps < possible_gp_samps_to_read)) {
  1153. overlap = 1; // Delays WILL (POSSIBLY) OVERLAP: write ALL into reprepbuf
  1154. segbuf = reprepbuf;
  1155. memset((char *)segbuf,0,dz->buflen2 * sizeof(float));
  1156. } else { // Delays do not overlap, write 1 segment in repbuf
  1157. segbuf = repbuf;
  1158. overlap = 0;
  1159. } // Zero the segment buffer
  1160. memset((char *)repbuf,0,dz->buflen2 * sizeof(float));
  1161. repbufpos = 0; // Write one repeat segment (at original pitch) to appropriate buffer
  1162. for(n=0,m = gp_samps_to_read - 1;n < gp_samps_to_read; n++,m--) {
  1163. if(n < gp_splicesamps) {
  1164. if(at_start)
  1165. val = 1.0;
  1166. else
  1167. val = (double)n/dgp_splicesamps; // Copy to relevant segment-buffer, with splice at start, if not at start of infile
  1168. } else if (m < gp_splicesamps)
  1169. val = (double)m/dgp_splicesamps; // and splice at end
  1170. else
  1171. val = 1.0;
  1172. for(ch = 0;ch < chans; ch++) {
  1173. segbuf[repbufpos] = (float)(ibuf[ibufpos] * val);
  1174. repbufpos++;
  1175. ibufpos++;
  1176. }
  1177. if(repbufpos >= dz->buflen2) {
  1178. sprintf(errstr,"Input segment has overflowed segment buffer.\n");
  1179. return PROGRAM_ERROR;
  1180. }
  1181. if(ibufpos >= 2 * dz->buflen) {
  1182. if((exit_status = reset_ibuf_and_read(&ibufpos,dz))<0)
  1183. return(exit_status);
  1184. }
  1185. }
  1186. at_start = 0;
  1187. samps_to_read = gp_samps_to_read * chans;
  1188. delaysamps = gp_delaysamps * chans;
  1189. isshorten = 0;
  1190. thisfade = 1.0;
  1191. endspliceval = 1.0;
  1192. if(overlap) { // Delays OVERLAP: write ALL into repbuf, from single copy in reprepbuf
  1193. if(dz->mode == 2) {
  1194. if(dz->param[REP_ACCEL] != 1.0) {
  1195. lenchange = 1.0 - (1.0/dz->param[REP_ACCEL]);
  1196. lenchangeincr = lenchange/(double)(repeats - 1);
  1197. isshorten = 1;
  1198. }
  1199. inital_gp_delaysamps = gp_delaysamps;
  1200. initial_gp_samps_to_read = gp_samps_to_read;
  1201. }
  1202. maxrepbufpos = 0;
  1203. lastrepbufpos = 0;
  1204. for(n=0;n<repeats;n++) { // Copy repeating segments into segment-buffer
  1205. if(dz->mode == 2) {
  1206. if(isshorten) {
  1207. lenfact = 1.0 - pow(lenchangeincr * (double)n,dz->param[REP_WARP]);
  1208. gp_delaysamps = (int)round(inital_gp_delaysamps * lenfact);
  1209. delaysamps = gp_delaysamps * chans;
  1210. gp_samps_to_read = (int)round(initial_gp_samps_to_read * lenfact);
  1211. gp_endsplice = min(gp_samps_to_read,gp_splicesamps);
  1212. gp_endsplice_stt = gp_samps_to_read - gp_endsplice;
  1213. endsplice = gp_endsplice * chans;
  1214. endsplice_stt = gp_endsplice_stt * chans;
  1215. samps_to_read = gp_samps_to_read * chans;
  1216. }
  1217. thisfade = pow((double)(repeats - n)/(double)repeats,dz->param[REP_FADE]) ;
  1218. }
  1219. if(dz->param[REP_RAND] > 1.0) {
  1220. val = dz->param[REP_RAND] - 1.0; // 0 to maxval-1 (1-2 -> 0-1: 1-8 -> 0-7)
  1221. rnd = drand48(); // 0 to 1
  1222. rnd *= val; // 0 to 1*rnd OR 0 to 7*rnd (rnd < 1)
  1223. rnd += 1.0; // 1 to 2*rnd OR 1 to 8*rnd (rnd < 1)
  1224. delaysamps = (int)round(gp_delaysamps * rnd) * chans;
  1225. }
  1226. repbufpos = lastrepbufpos + delaysamps; // Advancing by delay-time
  1227. lastrepbufpos = repbufpos;
  1228. endspliceval = 1.0;
  1229. if(varypitch && (n > 0)) { // If there's pitch-variation
  1230. val = (drand48() * 2.0) - 1.0; // Get random value in range 0 to +- given semitone-range
  1231. val *= dz->param[REP_TRNSP];
  1232. incr = pow(2.0,val/SEMITONES_PER_OCTAVE); // Convert to an increment for table read
  1233. md = 0;
  1234. while(md < gp_samps_to_read) { // Transpose repeated segment before copying segment buffer
  1235. m = (int)floor(md);
  1236. frac = md - (double)m;
  1237. if(isshorten && (md > gp_endsplice_stt))
  1238. endspliceval = 1.0 - ((md - (double)gp_endsplice_stt)/(double)gp_endsplice);
  1239. for(ch=0,k=m*chans;ch<chans;ch++,k++) {
  1240. val = segbuf[k];
  1241. diff = segbuf[k+chans] - val;
  1242. val += diff*frac;
  1243. if(dz->mode == 2)
  1244. val *= thisfade*endspliceval;
  1245. repbuf[repbufpos] = (float)(repbuf[repbufpos] + val);
  1246. if(++repbufpos >= dz->buflen2) {
  1247. sprintf(errstr,"segment buffer too short to contain repeated overlapping segments (1).\n");
  1248. return PROGRAM_ERROR;
  1249. }
  1250. }
  1251. md += incr;
  1252. }
  1253. maxrepbufpos = max(maxrepbufpos,repbufpos);
  1254. } else {
  1255. for(m=0; m< samps_to_read;m++) {
  1256. if(isshorten && (m > endsplice_stt))
  1257. endspliceval = 1.0 - ((m - (double)endsplice_stt)/(double)endsplice);
  1258. repbuf[repbufpos] = (float)(repbuf[repbufpos] + (segbuf[m] * thisfade * endspliceval)); // Add repeating units back into segment buffer
  1259. if(++repbufpos >= dz->buflen2) {
  1260. sprintf(errstr,"segment buffer too short to contain repeated overlapping segments (1).\n");
  1261. return PROGRAM_ERROR;
  1262. }
  1263. }
  1264. maxrepbufpos = max(maxrepbufpos,repbufpos);
  1265. }
  1266. }
  1267. samps_to_write = maxrepbufpos;
  1268. if((exit_status = normalise_buffer(samps_to_write,dz))<0) // Normalise the output in such a way
  1269. return(exit_status); // that start and end normalisations are 1.0
  1270. for(n = 0; n < samps_to_write; n++) {
  1271. obuf[obufpos] = (float)(obuf[obufpos] + repbuf[n]); // Add whole set of repeating units to output
  1272. if(++obufpos >= dz->buflen + total_splicesamps) {
  1273. if((exit_status = write_and_reset_obuf(dz->buflen,&obufpos,dz))<0)
  1274. return(exit_status);
  1275. }
  1276. }
  1277. } else { // Delays will NOT overlap
  1278. if(dz->mode == 2) {
  1279. if(dz->param[REP_ACCEL] != 1.0) {
  1280. lenchange = 1.0 - (1.0/dz->param[REP_ACCEL]);
  1281. lenchangeincr = lenchange/(double)(repeats - 1);
  1282. isshorten = 1;
  1283. }
  1284. inital_gp_delaysamps = gp_delaysamps;
  1285. initial_gp_samps_to_read = gp_samps_to_read;
  1286. }
  1287. if(dz->mode == 1) // In mode 1
  1288. startdelay = 0; // Delaytime starts at END of segment
  1289. for(n=0;n<repeats;n++) {
  1290. if(dz->mode == 2) {
  1291. if(isshorten) {
  1292. lenfact = 1.0 - pow(lenchangeincr * (double)n,dz->param[REP_WARP]);
  1293. gp_delaysamps = (int)round(inital_gp_delaysamps * lenfact);
  1294. gp_samps_to_read = (int)round(initial_gp_samps_to_read * lenfact);
  1295. gp_endsplice = min(gp_samps_to_read,gp_splicesamps);
  1296. gp_endsplice_stt = gp_samps_to_read - gp_endsplice;
  1297. endsplice = gp_endsplice * chans;
  1298. endsplice_stt = gp_endsplice_stt * chans;
  1299. samps_to_read = gp_samps_to_read * chans;
  1300. }
  1301. thisfade = pow((double)(repeats - n)/(double)repeats,dz->param[REP_FADE]) ;
  1302. }
  1303. samps_written = 0;
  1304. repbufpos = 0;
  1305. endspliceval = 1.0;
  1306. if(varypitch && (n > 0)) { // If there's pitch-variation
  1307. val = (drand48() * 2.0) - 1.0;
  1308. val *= dz->param[REP_TRNSP];
  1309. incr = pow(2.0,val/SEMITONES_PER_OCTAVE);
  1310. md = 0;
  1311. while(md < gp_samps_to_read) { // Transpose repeated segment before copying to output buffer
  1312. m = (int)floor(md);
  1313. frac = md - (double)m;
  1314. if(isshorten && (md > gp_endsplice_stt))
  1315. endspliceval = 1.0 - ((md - (double)gp_endsplice_stt)/(double)gp_endsplice);
  1316. for(ch=0,k=m*chans;ch<chans;ch++,k++) {
  1317. val = segbuf[k];
  1318. diff = segbuf[k+chans] - val;
  1319. val += diff*frac;
  1320. if(dz->mode == 2)
  1321. val *= thisfade*endspliceval;
  1322. obuf[obufpos] = (float)(obuf[obufpos] + val);
  1323. if(++obufpos >= dz->buflen + total_splicesamps) { // (startsplice overlaps with existing obuf data, so use "add")
  1324. if((exit_status = write_and_reset_obuf(dz->buflen,&obufpos,dz))<0)
  1325. return(exit_status);
  1326. }
  1327. samps_written++;
  1328. }
  1329. md += incr;
  1330. }
  1331. } else {
  1332. while(repbufpos < samps_to_read) {
  1333. if(isshorten && (repbufpos > endsplice_stt))
  1334. endspliceval = 1.0 - ((repbufpos - (double)endsplice_stt)/(double)endsplice);
  1335. if(dz->mode == 2)
  1336. repbuf[repbufpos] = (float)(repbuf[repbufpos] * thisfade * endspliceval); // Do any fades or endsplicing
  1337. obuf[obufpos] = (float)(obuf[obufpos] + repbuf[repbufpos++]);// Add repeating unit to output
  1338. if(++obufpos >= dz->buflen + total_splicesamps) { // (startsplice overlaps with existing obuf data, so use "add")
  1339. if((exit_status = write_and_reset_obuf(dz->buflen,&obufpos,dz))<0)
  1340. return(exit_status);
  1341. }
  1342. samps_written++;
  1343. }
  1344. }
  1345. if(dz->param[REP_RAND] > 1.0) {
  1346. val = dz->param[REP_RAND] - 1.0;
  1347. rnd = drand48();
  1348. rnd *= val;
  1349. rnd += 1.0;
  1350. delaysamps = (int)round(gp_delaysamps * rnd) * chans;
  1351. } else
  1352. delaysamps = gp_delaysamps * chans;
  1353. if(dz->mode != 1) // In modes 0 & 2
  1354. startdelay = samps_written; // Delaytime starts at START of segment
  1355. for(m=startdelay;m < delaysamps; m++) {
  1356. if(++obufpos >= dz->buflen + total_splicesamps) {
  1357. if((exit_status = write_and_reset_obuf(dz->buflen,&obufpos,dz))<0)
  1358. return(exit_status);
  1359. }
  1360. }
  1361. }
  1362. }
  1363. obufpos -= total_splicesamps; // Baktrack by splicelen in output;
  1364. ibufpos -= total_splicesamps; // Restore ibufpos to true end-of-segment time, ready for next read.
  1365. last_gp_absendsamp = gp_absendsamp; // Set sample position of end of segment read
  1366. }
  1367. bufpos_in_iovflw = ibufpos - dz->buflen; // ibufpos is normally in iovflwbuf (>= dz->buflen) unless it's baktracked
  1368. if(bufpos_in_iovflw < dz->ssampsread) { // If there are still input samples remaining to be read (these are always in iovflw)
  1369. n = 0;
  1370. while(bufpos_in_iovflw < dz->ssampsread) {
  1371. if(n < gp_splicesamps)
  1372. val = (double)n/dgp_splicesamps; // Copy to repeat-buffer, with splices at start
  1373. else
  1374. val = 1.0;
  1375. for(ch = 0;ch < chans; ch++) {
  1376. obuf[obufpos] = (float)(obuf[obufpos] + (ibuf[ibufpos] * val));
  1377. obufpos++;
  1378. ibufpos++;
  1379. bufpos_in_iovflw++;
  1380. }
  1381. if(obufpos >= dz->buflen + total_splicesamps) {
  1382. if((exit_status = write_and_reset_obuf(dz->buflen,&obufpos,dz))<0)
  1383. return(exit_status);
  1384. }
  1385. if(ibufpos >= 2 * dz->buflen) {
  1386. if((exit_status = reset_ibuf_and_read(&ibufpos,dz))<0)
  1387. return(exit_status);
  1388. bufpos_in_iovflw = 0;
  1389. }
  1390. n++;
  1391. }
  1392. } else
  1393. obufpos += total_splicesamps; // Restore obufpos to its true position
  1394. dz->process = GREV;
  1395. if((exit_status = write_samps(obuf,obufpos,dz))<0)
  1396. return(exit_status);
  1397. dz->process = REPEATER;
  1398. return FINISHED;
  1399. }
  1400. /**************************** HANDLE_THE_SPECIAL_DATA ****************************/
  1401. int handle_the_special_data(char *str,double *maxseglen,double *maxovlp,dataptr dz)
  1402. {
  1403. int n, k, cnt, curtail, idummy, linecnt, warned = 0, chans = dz->infile->channels;
  1404. FILE *fp;
  1405. char temp[200], *p;
  1406. double dummy = 0, lasttime, lastendtime = 0.0, seglen, srate = (double)dz->infile->srate;
  1407. double splicelen, twosplicelen, lastdur = 0.0;
  1408. double max_ovlpbuf; // Finds size of buffer needed for any overlapping sets of delayed segments
  1409. int splicesamps, repeats = 0;
  1410. *maxovlp = 0.0;
  1411. splicelen = REPSPLEN * MS_TO_SECS; // Find minimum permissible size of segments == >two-splicelengths
  1412. splicesamps = (int)ceil(splicelen * srate);
  1413. splicelen = (double)(splicesamps + chans)/srate; // Add a sample (for each chan) for splicelen, for safety
  1414. twosplicelen = (double)((splicesamps + chans) * 2)/srate;
  1415. if((fp = fopen(str,"r"))==NULL) {
  1416. sprintf(errstr,"Cannot open file \"%s\" to read repeater data.\n",str);
  1417. return(DATA_ERROR);
  1418. }
  1419. cnt = 0;
  1420. lasttime = 0.0;
  1421. curtail = 0;
  1422. linecnt = 0;
  1423. while(fgets(temp,200,fp)!=NULL) {
  1424. p = temp;
  1425. while(isspace(*p))
  1426. p++;
  1427. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  1428. continue;
  1429. cnt = 0;
  1430. while(get_float_from_within_string(&p,&dummy)) {
  1431. if(curtail == 1)
  1432. break; // if in midst of current seg (cnt > 0), but fell off end of data, break now before linecnt is incremented
  1433. k = cnt % 4;
  1434. if(k == 0 && curtail == 2) // if at start of a new seg (cnt == 0), but we reached end of file in last segment, break now
  1435. break;
  1436. switch(k) {
  1437. case(0):
  1438. if(dummy < 0.0) {
  1439. sprintf(errstr,"Segment start-time (%lf) less than zero at line %d in file \"%s\".\n",dummy,linecnt+1,str);
  1440. return DATA_ERROR;
  1441. }
  1442. if(dummy >= dz->duration - splicelen) {
  1443. if(linecnt == 0) {
  1444. sprintf(errstr,"1st segment start-time (%lf) close to or > infile-end (%lf) in file \"%s\".\n",dummy,dz->duration,str);
  1445. return DATA_ERROR;
  1446. } else {
  1447. fprintf(stdout,"WARNING: line %d in file \"%s\" : start-time (%lf) close to or > infile-end (%lf).\n",linecnt+1,str,dummy,dz->duration);
  1448. fprintf(stdout,"WARNING: Ignoring repetition-data at and beyond this time in file \"%s\".\n",str);
  1449. fflush(stdout);
  1450. curtail = 1;
  1451. }
  1452. }
  1453. lasttime = dummy;
  1454. break;
  1455. case(1):
  1456. if(dummy - lasttime <= twosplicelen) {
  1457. sprintf(errstr,"Segment on line %d in file \"%s\", dur %lf, too short for splicing (min dur %lf).\n",linecnt+1,str,dummy - lasttime,twosplicelen);
  1458. return DATA_ERROR;
  1459. }
  1460. if(dummy >= dz->duration) {
  1461. if(dz->duration - lasttime <= twosplicelen) {
  1462. fprintf(stdout,"WARNING: Segment on line %d in file \"%s\", ends after infile-end and is hence too short.\n",linecnt+1,str,twosplicelen);
  1463. fprintf(stdout,"WARNING: Ignoring this and later segments.\n");
  1464. fflush(stdout);
  1465. curtail = 1; // cnt is complete after end of previous viable segment
  1466. break;
  1467. } else {
  1468. fprintf(stdout,"WARNING: line %d in file \"%s\" : segment end-time (%lf) beyond infile-end (%lf).\n",linecnt+1,str,dummy,dz->duration);
  1469. fprintf(stdout,"WARNING: Curtailing segment to finish at end of src-file (and ignoring any subsequent segments).\n");
  1470. fflush(stdout);
  1471. curtail = 2; // read rest of this seg, but ignore any further segs
  1472. }
  1473. }
  1474. lastendtime = dummy;
  1475. lastdur = dummy - lasttime;
  1476. break;
  1477. case(2):
  1478. idummy = (int)round(dummy);
  1479. if(dummy != (double)idummy) {
  1480. sprintf(errstr,"Non-integer repeat value on line %d in file \"%s\".\n",linecnt+1,str);
  1481. return DATA_ERROR;
  1482. }
  1483. if(idummy < 2 && idummy != 0) {
  1484. sprintf(errstr,"Repeat value less than 2 on line %d in file \"%s\".\n",linecnt+1,str);
  1485. return DATA_ERROR;
  1486. }
  1487. repeats= idummy;
  1488. break;
  1489. case(3):
  1490. switch(dz->mode) {
  1491. case(0):
  1492. case(2):
  1493. if(dummy < REPMINDEL && dummy != 0.0) {
  1494. if(!warned) {
  1495. fprintf(stdout,"WARNING: (Non-zero) Delay (%.3lf) <= %.3lf on line %d in file \"%s\".\n",dummy,REPMINDEL,linecnt+1,str);
  1496. fprintf(stdout,"WARNING: This may produce unexpected output, like an oscillator.\n");
  1497. fflush(stdout); // ---------------------------
  1498. warned = 1;
  1499. }
  1500. } // | |---------------------------
  1501. if(dummy < lastdur + splicelen) { // If delayed repeats overlap // delay |---------------------------
  1502. max_ovlpbuf = (dummy * repeats) + lastdur + splicelen; // | | | |--------------------------- __
  1503. *maxovlp = max(*maxovlp,max_ovlpbuf); // Remember maxoverlap dur // | | | |
  1504. } // For bufsize calculations. // |delay*rpts | + (last)dur |+splicelen
  1505. break;
  1506. case(1): // Delayed repeats never overlap.
  1507. break;
  1508. }
  1509. break;
  1510. default:
  1511. sprintf(errstr,"Too many values (%d) on line %d in file \"%s\": Need only 4.\n",cnt,linecnt+1,str);
  1512. return DATA_ERROR;
  1513. }
  1514. cnt++;
  1515. }
  1516. if(cnt < 4) {
  1517. sprintf(errstr,"Too few values (%d) on line %d in file \"%s\": Need 4.\n",cnt,linecnt+1,str);
  1518. return DATA_ERROR;
  1519. }
  1520. linecnt++;
  1521. }
  1522. if(linecnt == 0) {
  1523. sprintf(errstr,"No viable repetition data found in file \"%s\".\n",str);
  1524. return(DATA_ERROR);
  1525. }
  1526. dz->itemcnt = linecnt * 4;
  1527. if((dz->parray = (double **)malloc(2 * sizeof(double *)))==NULL) {
  1528. sprintf(errstr,"INSUFFICIENT MEMORY to create Repetition data array.\n");
  1529. return(MEMORY_ERROR);
  1530. }
  1531. if((dz->parray[0] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  1532. sprintf(errstr,"INSUFFICIENT MEMORY to create Repetition data array.\n");
  1533. return(MEMORY_ERROR);
  1534. }
  1535. cnt = 0;
  1536. fseek(fp,0,0);
  1537. while(fgets(temp,200,fp)!=NULL) {
  1538. p = temp;
  1539. while(isspace(*p))
  1540. p++;
  1541. if(*p == ';' || *p == ENDOFSTR) // Allow comments in file
  1542. continue;
  1543. while(get_float_from_within_string(&p,&dummy))
  1544. dz->parray[0][cnt++] = dummy;
  1545. if(cnt >= dz->itemcnt)
  1546. break;
  1547. }
  1548. lastendtime = dz->parray[0][dz->itemcnt - 3];
  1549. if(lastendtime > dz->duration)
  1550. dz->parray[0][dz->itemcnt - 3] = dz->duration;
  1551. *maxseglen = 0.0;
  1552. for(n = 0; n < dz->itemcnt;n+=4) {
  1553. seglen = dz->parray[0][n+1] - dz->parray[0][n];
  1554. *maxseglen = max(seglen,*maxseglen);
  1555. }
  1556. return FINISHED;
  1557. }
  1558. /******************************** CREATE_REPEATER_SNDBUFS ********************************/
  1559. int create_repeater_sndbufs(double maxseglen, double maxovlp, dataptr dz)
  1560. {
  1561. int exit_status, chans = dz->infile->channels;
  1562. int bigbufsize, secsize, maxovlpsamps;
  1563. int framesize = F_SECSIZE * chans;
  1564. double maxrand = 0.0, maxtransp = 0.0, maxtranspdn, maxexpand = 1.0, srate = (double)dz->infile->srate;
  1565. int splicespace;
  1566. if(dz->sbufptr == 0 || dz->sampbuf == 0) {
  1567. sprintf(errstr,"buffer pointers not allocated: create_sndbufs()\n");
  1568. return(PROGRAM_ERROR);
  1569. }
  1570. splicespace = (int)ceil(REPSPLEN * MS_TO_SECS * srate) * chans;// Allow for inbuf splice-backtraks and splice-beyond-endtime as segments cut
  1571. // BUFLEN2 to contain segments for repeating
  1572. if(dz->brksize[REP_RAND]) {
  1573. if((exit_status = get_maxvalue_in_brktable(&maxrand,REP_RAND,dz))<0)
  1574. return exit_status;
  1575. } else if(dz->param[REP_RAND] > 1.0) // Start with the max seglen to handle overlapping segments
  1576. maxrand = dz->param[REP_RAND];
  1577. if(maxrand > 1.0) // Allow for maximum possible expansion of "overlapping" segs, by random expansion!!
  1578. maxovlp *= maxrand;
  1579. if(dz->brksize[REP_TRNSP]) {
  1580. if((exit_status = get_maxvalue_in_brktable(&maxtransp,REP_TRNSP,dz))<0)
  1581. return exit_status;
  1582. } else // Find the maximum transposition
  1583. maxtransp = dz->param[REP_TRNSP];
  1584. if(maxtransp > 0.0) { // If segments are transposed, they could be transposed downwards (and therefore be longer)
  1585. maxtranspdn = pow(2.0,-maxtransp/SEMITONES_PER_OCTAVE); // Convert semitones to frq-ratio for fownwarfd transposition.
  1586. maxexpand = 1.0/maxtranspdn; // Transposing down an 8va (frq ratio 1/2) makes sound 2 * longer, so take reciprocal
  1587. maxovlp *= maxexpand; // Increase length of overlap-buffer by this amount as all segs could be expanded and overlap
  1588. maxseglen *= maxexpand; // Increase length of single-segment-buffer by this amount
  1589. }
  1590. maxovlpsamps = (int)ceil(maxovlp * srate) * chans;
  1591. dz->buflen2 = (int)ceil(maxseglen * srate) * chans; // Size = max seglen
  1592. dz->buflen2 = max(dz->buflen2,maxovlpsamps); // Or = max length of any overlapping repeats captured in segment buffer
  1593. dz->buflen2 += splicespace * 2; // Must be large enough to fit and splice area at end of segment
  1594. bigbufsize = (int)Malloc(-1);
  1595. dz->buflen = bigbufsize/sizeof(float); // dz->buflen2 will accomodate largest cut-segment
  1596. dz->buflen = max(dz->buflen,dz->buflen2) + (splicespace * 2); // must be large enough to fit largest cut-segment and splice baktrak
  1597. secsize = dz->buflen/framesize;
  1598. if(secsize * framesize != dz->buflen)
  1599. secsize++;
  1600. dz->buflen = secsize * framesize;
  1601. secsize = dz->buflen2/framesize;
  1602. if(secsize * framesize != dz->buflen2)
  1603. secsize++;
  1604. dz->buflen2 = secsize * framesize;
  1605. if(dz->buflen <= 0) {
  1606. sprintf(errstr,"INSUFFICIENT MEMORY to create input and output sound buffers.\n");
  1607. return(PROGRAM_ERROR);
  1608. }
  1609. if(dz->buflen2 <= 0) {
  1610. sprintf(errstr,"INSUFFICIENT MEMORY to create delay-segments sound buffer.\n");
  1611. return(PROGRAM_ERROR);
  1612. }
  1613. bigbufsize = ((dz->buflen * 4) + (dz->buflen2 * 2)) * sizeof(float);
  1614. if((dz->bigbuf = (float *)malloc(bigbufsize)) == NULL) {
  1615. sprintf(errstr,"INSUFFICIENT MEMORY to create total sound buffers.\n");
  1616. return(PROGRAM_ERROR);
  1617. }
  1618. dz->sbufptr[0] = dz->sampbuf[0] = dz->bigbuf; // Inbuf
  1619. dz->sbufptr[1] = dz->sampbuf[1] = dz->sampbuf[0] + dz->buflen; // Inbuf overflow
  1620. dz->sbufptr[2] = dz->sampbuf[2] = dz->sampbuf[1] + dz->buflen; // Outbuf
  1621. dz->sbufptr[3] = dz->sampbuf[3] = dz->sampbuf[2] + dz->buflen; // Ovflwbuf
  1622. dz->sbufptr[4] = dz->sampbuf[4] = dz->sampbuf[3] + dz->buflen; // segment-store
  1623. dz->sbufptr[5] = dz->sampbuf[5] = dz->sampbuf[4] + dz->buflen2; // Repeated-segment-store
  1624. dz->sampbuf[6] = dz->sampbuf[5] + dz->buflen2;
  1625. return(FINISHED);
  1626. }
  1627. /******************************** WRITE_AND_RESET_OBUF ********************************/
  1628. int write_and_reset_obuf(int samps_to_write,int *obufpos,dataptr dz)
  1629. {
  1630. int exit_status;
  1631. float *obuf = dz->sampbuf[2], *ovflwbuf = dz->sampbuf[3];
  1632. dz->process = GREV;
  1633. if((exit_status = write_samps(obuf,samps_to_write,dz))<0)
  1634. return(exit_status);
  1635. dz->process = REPEATER;
  1636. memset((char *)obuf,0,dz->buflen * sizeof(float));
  1637. memcpy((char *)obuf,(char *)ovflwbuf,dz->buflen * sizeof(float));
  1638. memset((char *)ovflwbuf,0,dz->buflen * sizeof(float));
  1639. *obufpos -= dz->buflen;
  1640. return FINISHED;
  1641. }
  1642. /******************************** RESET_IBUF_AND_READ ********************************/
  1643. int reset_ibuf_and_read(int *ibufpos,dataptr dz)
  1644. {
  1645. int exit_status;
  1646. float *ibuf = dz->sampbuf[0], *iovflwbuf = dz->sampbuf[1];
  1647. memcpy((char *)ibuf,(char *)iovflwbuf,dz->buflen * sizeof(float)); // Copy input overflow back into ibuf
  1648. memset((char *)iovflwbuf,0,dz->buflen * sizeof(float)); // Set overflow to zero
  1649. if((exit_status = read_samps(iovflwbuf,dz))<0) // Read into overflow
  1650. return(exit_status);
  1651. *ibufpos = dz->buflen; // Reset ibufpos to start of iovflwbuf
  1652. return FINISHED;
  1653. }
  1654. /******************************** NORMALISE_BUFFER ********************************/
  1655. int normalise_buffer(int samplen,dataptr dz)
  1656. {
  1657. double *env = dz->parray[1], maxsamp, thiseval, nexteval, diff, eval;
  1658. float *buf = dz->sampbuf[4];
  1659. int *loc = dz->iparray[0], chans = dz->infile->channels, ch;
  1660. int e, m, k, envsize, maxloc, windowstart, thispos, goalpos, samppos, gap, win_in_buf;
  1661. int needs_enveloping = 0, ethis, enext, done, shortwindow = 0;
  1662. int halfwindow = dz->envwindowlen/2;
  1663. memset((char *)env,0,dz->arraysize * sizeof(double));
  1664. win_in_buf = samplen/dz->envwindowlen; // Number of complete windows in buf
  1665. shortwindow = samplen - (win_in_buf * dz->envwindowlen); // Length of any short window
  1666. // To force the final window to be full-length, we will make the penultimate window the short window
  1667. samppos = 0;
  1668. do {
  1669. samppos = 0;
  1670. for(e = 0; e < win_in_buf;e++) { // For all the normalisable samples, advance by windowlen blocks
  1671. maxsamp = 0.0;
  1672. maxloc = 0;
  1673. for(m=0,k=samppos;m < dz->envwindowlen;m++,k++) { // In each normal window, find the maxsamp
  1674. if(fabs(buf[k]) > maxsamp) {
  1675. maxsamp = fabs(buf[k]);
  1676. maxloc = m;
  1677. }
  1678. }
  1679. samppos += dz->envwindowlen;
  1680. if(e >= dz->arraysize) {
  1681. sprintf(errstr,"envelope arraysize exceeded.\n");
  1682. return PROGRAM_ERROR;
  1683. }
  1684. env[e] = maxsamp; // And store the envelope val
  1685. loc[e] = (maxloc/chans) * chans; // And position of maximum to chan-grp boundary
  1686. }
  1687. if(shortwindow) {
  1688. maxsamp = 0.0;
  1689. maxloc = 0;
  1690. for(m=0,k=samppos;m < shortwindow;m++,k++) {
  1691. if(fabs(buf[k]) > maxsamp) {
  1692. maxsamp = fabs(buf[k]);
  1693. maxloc = m;
  1694. }
  1695. }
  1696. loc[e] = (maxloc/chans) * chans;
  1697. e++;
  1698. }
  1699. envsize = e;
  1700. needs_enveloping = 0;
  1701. for(e = 0;e < envsize;e++) { // Check where signal exceeds max (REPCLIP)
  1702. if(env[e] > REPCLIP) { // and force (re-)envelope to reduce level here
  1703. env[e] = REPCLIP/env[e];
  1704. needs_enveloping = 1; // AND note the re-envelopeing is necessary
  1705. } else // otherwise leave envelope level at 1.0 (no change)
  1706. env[e] = 1.0;
  1707. }
  1708. if(needs_enveloping) { // If enveloping required
  1709. if(env[0] < 1.0) { // If 1st window overloads, do a presmooth
  1710. for(samppos=0;samppos < loc[0];samppos++)
  1711. buf[samppos] = (float)(buf[samppos] * env[0]);
  1712. }
  1713. if(env[envsize-1] < 1.0) { // If last window overloads, do a presmooth
  1714. thispos = loc[envsize-1] + (dz->envwindowlen * (envsize-2));
  1715. goalpos = samplen;
  1716. for(samppos=thispos;samppos < goalpos;samppos++)
  1717. buf[samppos] = (float)(buf[samppos] * env[envsize-1]);
  1718. }
  1719. ethis = -1; // Interpolate the re-envelope vals, in order to envelope the src, in situ
  1720. enext = 0;
  1721. done = 0;
  1722. for(windowstart = 0; windowstart < samplen; windowstart+=dz->envwindowlen) {
  1723. ethis++;
  1724. enext++;
  1725. thiseval = env[ethis];
  1726. nexteval = env[enext];
  1727. if(thiseval < 1.0 && nexteval == 1.0) {
  1728. thispos = windowstart + loc[ethis]; // Interp from maximum in this-window to middle of non-normalised next-window
  1729. goalpos = windowstart + dz->envwindowlen + halfwindow;
  1730. } else if(thiseval == 1.0 && nexteval < 1.0) {
  1731. thispos = windowstart + halfwindow; // Interp from middle of non-normalised this-window to maximum in next
  1732. if(enext >= envsize)
  1733. goalpos = samplen;
  1734. else
  1735. goalpos = windowstart + dz->envwindowlen + loc[enext];
  1736. } else if(thiseval < 1.0 && nexteval < 1.0) {
  1737. thispos = windowstart + loc[ethis]; // Interp from max in this window to max in next
  1738. if(enext >= envsize)
  1739. goalpos = samplen;
  1740. else
  1741. goalpos = windowstart + dz->envwindowlen + loc[enext];
  1742. } else { // (thiseval == 1.0 && nexteval == 1.0) do nothing
  1743. continue;
  1744. }
  1745. samppos = thispos;
  1746. gap = (goalpos - thispos)/chans;
  1747. diff = nexteval - thiseval;
  1748. for(m=0;m < gap;m++) {
  1749. if(samppos >= dz->buflen) {
  1750. done = 1;
  1751. break;
  1752. }
  1753. eval = (double)m/(double)gap;
  1754. eval *= diff;
  1755. eval += thiseval;
  1756. for(ch= 0;ch < chans;ch++) {
  1757. buf[samppos] = (float)(buf[samppos] * eval);
  1758. samppos++;
  1759. }
  1760. }
  1761. if(done)
  1762. break;
  1763. }
  1764. }
  1765. } while(needs_enveloping); // Do this recursively until nothing is too loud
  1766. return FINISHED;
  1767. }
  1768. /****************************** GET_MODE *********************************/
  1769. int get_the_mode_from_cmdline(char *str,dataptr dz)
  1770. {
  1771. char temp[200], *p;
  1772. if(sscanf(str,"%s",temp)!=1) {
  1773. sprintf(errstr,"Cannot read mode of program.\n");
  1774. return(USAGE_ONLY);
  1775. }
  1776. p = temp + strlen(temp) - 1;
  1777. while(p >= temp) {
  1778. if(!isdigit(*p)) {
  1779. fprintf(stderr,"Invalid mode of program entered.\n");
  1780. return(USAGE_ONLY);
  1781. }
  1782. p--;
  1783. }
  1784. if(sscanf(str,"%d",&dz->mode)!=1) {
  1785. fprintf(stderr,"Cannot read mode of program.\n");
  1786. return(USAGE_ONLY);
  1787. }
  1788. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  1789. fprintf(stderr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  1790. return(USAGE_ONLY);
  1791. }
  1792. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  1793. return(FINISHED);
  1794. }
  1795. /****************************** CALC_OUTPUT_DUR *********************************/
  1796. int calc_output_dur(int *dursamps,dataptr dz)
  1797. {
  1798. int exit_status, chans = dz->infile->channels;
  1799. double *segdata = dz->parray[0];
  1800. double stttime, endtime, repeats, delay, seglen, repsdur = 0.0, advance;
  1801. double lastendtime = 0.0, totaldur = 0.0, maxrand = 1.0, srate = (double)dz->infile->srate;
  1802. int n, m;
  1803. if(dz->brksize[REP_RAND]) {
  1804. if((exit_status = get_maxvalue_in_brktable(&maxrand,REP_RAND,dz))<0)
  1805. return exit_status;
  1806. } else if(dz->param[REP_RAND] > 1.0)
  1807. maxrand = dz->param[REP_RAND];
  1808. for(n = 0; n < dz->itemcnt;n+=4) {
  1809. m = n;
  1810. stttime = segdata[m++];
  1811. endtime = segdata[m++];
  1812. repeats = segdata[m++];
  1813. delay = segdata[m++];
  1814. if((advance = stttime - lastendtime) > 0.0) // If we advance in input
  1815. totaldur += advance; // add duration of advance-step to total output duration
  1816. seglen = endtime - stttime;
  1817. switch(dz->mode) {
  1818. case(0): // Find approx duration covered by repeats of segment
  1819. case(2):
  1820. repsdur = (repeats * delay) + seglen;
  1821. break;
  1822. case(1):
  1823. repsdur = (seglen + delay) * repeats;
  1824. break;
  1825. }
  1826. if(maxrand > 1.0) // Allow for max possible randomisation-increase
  1827. repsdur *= maxrand;
  1828. totaldur += repsdur; // and add to total output duration
  1829. lastendtime = endtime;
  1830. }
  1831. if((advance = dz->duration - lastendtime) > 0.0) // IF not yet at end of file
  1832. totaldur += advance; // add duration of step to end-of-file to total output dur
  1833. *dursamps = (int)round(totaldur * srate) * chans;
  1834. return FINISHED;
  1835. }