wrappage.c 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401
  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. /*
  22. * INPUT CAN BE MULTICHANNEL BUT WILL BE MIXED TO MONO
  23. * Code based on SAUSAGE with spatialisation altered
  24. *
  25. * ABOUT THIS PROCESS
  26. *
  27. * There are 3 + (infiles * 2) buffers invloved here.....
  28. * WRAP_SBUF into which the input data is read from the file.
  29. * THIS_BUF(x) several buffers, one for each infile,
  30. * into which the input data for each file is mixed down to mono, where necessary. Otherwise, initial reads go directly to this buffer.
  31. * This buffer has an overflow sector of GRS_BUF_SMPXS samples.
  32. * The 1st read fills the buffer and the overflow.After the first read these overflow samples are copied to the bufstart,
  33. * and a new input buffer start is marked, overflow-samples into the GRS_BUF at GRS_IBUF.
  34. * THIS_IBUF(x) are then the points at which further samples are read into the input buffers.
  35. * GRS_LBUF is buffer into which grains are copied for processing to the output, (they are timestretched, pshifted or and spliced).
  36. * WRAP_OBUF is the buffer into which the finished grains are copied ready for output.
  37. *
  38. * GRS_INCHANS is the number of channels in the input grains i.e. after they have (or have not) been mixed down to mono.
  39. * In this program, this is ALWAYS MONO
  40. * GRS_OUTCHANS is the number of channels in the output.
  41. */
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <structures.h>
  45. #include <tkglobals.h>
  46. #include <pnames.h>
  47. #include <filetype.h>
  48. #include <processno.h>
  49. #include <modeno.h>
  50. #include <logic.h>
  51. #include <globcon.h>
  52. #include <cdpmain.h>
  53. #include <math.h>
  54. #include <osbind.h>
  55. #include <standalone.h>
  56. #include <ctype.h>
  57. #include <sfsys.h>
  58. #include <string.h>
  59. #include <srates.h>
  60. #include <limits.h>
  61. #include <float.h>
  62. #include <sfsys.h>
  63. #include <osbind.h>
  64. //#if defined unix || defined __GNUC__
  65. #define round(x) lround((x))
  66. //#endif
  67. #ifndef HUGE
  68. #define HUGE 3.40282347e+38F
  69. #endif
  70. char errstr[2400];
  71. int anal_infiles = 0;
  72. int sloom = 0;
  73. int sloombatch = 0;
  74. const char* cdp_version = "7.1.0";
  75. //CDP LIB REPLACEMENTS
  76. static int check_wrappage_param_validity_and_consistency(dataptr dz);
  77. static int setup_wrappage_application(dataptr dz);
  78. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  79. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  80. static int setup_wrappage_param_ranges_and_defaults(dataptr dz);
  81. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  82. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  83. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  84. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  85. static int establish_application(dataptr dz);
  86. static int initialise_vflags(dataptr dz);
  87. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  88. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  89. static int mark_parameter_types(dataptr dz,aplptr ap);
  90. static int assign_file_data_storage(int infilecnt,dataptr dz);
  91. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  92. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  93. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  94. #define WRAP_SBUF (0)
  95. #define WRAP_OBUF (1)
  96. #define WRAP_IBUF (2)
  97. #define THIS_BUF(x) (((x)*2)+2)
  98. #define THIS_IBUF(x) (((x)*2)+3)
  99. #define SAFETY ((int)4096)
  100. #define OSAFETY ((int)256)
  101. static int wrappage(dataptr dz);
  102. static int granulate(int *thissnd,int *aipc,int *aopc,float **iiiiptr,float **LLLLptr,
  103. double inv_sr,int *samptotal,float **maxwrite,int *nctr,dataptr dz);
  104. static double dehole(double pos);
  105. static int write_samps_granula(int k,int *nctr,dataptr dz);
  106. static float interp_gval_with_amp(float *s,double flcnt_frac,int chans,float ampp);
  107. static float interp_gval(float *s,double flcnt_frac,int chans);
  108. static int set_instep(int ostep_per_chan,dataptr dz);
  109. static int set_outstep(int gsize_per_chan,dataptr dz);
  110. static int set_range(int absiicnt,dataptr dz);
  111. static int do_scatter(int ostep_per_chan,dataptr dz);
  112. static int set_ivalue(int flag,int paramno,int hparamno,int rangeno,dataptr dz);
  113. static double set_dvalue(int flag,int paramno,int hparamno,int rangeno,dataptr dz);
  114. static int renormalise(int nctr,dataptr dz);
  115. static int read_samps_wrappage(int firsttime,dataptr dz);
  116. static int read_a_specific_large_buf(int k,dataptr dz);
  117. static int read_a_specific_normal_buf_with_wraparound(int k,dataptr dz);
  118. static int baktrak_wrappage(int thissnd,int samptotal,int absiicnt_per_chan,float **iiptr,dataptr dz);
  119. static int reset_wrappage(int thissnd,int resetskip,dataptr dz);
  120. static int get_next_insnd(dataptr dz);
  121. static void perm_wrappage(int cnt,dataptr dz);
  122. static void insert(int n,int t,int cnt_less_one,dataptr dz);
  123. static void prefix(int n,int cnt_less_one,dataptr dz);
  124. static void shuflup(int k,int cnt_less_one,dataptr dz);
  125. static int grab_an_appropriate_block_of_wrappage_memory(int *this_bloksize,int standard_block,int bufdivisor,dataptr dz);
  126. static int make_multichan_grain(float *b,float **iiptr,float aamp,int gsize_per_chan,double *transpos,dataptr dz);
  127. static void do_multichan_splices(int gsize_per_chan,int bspl,int espl,dataptr dz);
  128. static void do_multichan_btab_splice(dataptr dz);
  129. static void do_multichan_bsplice(int gsize_per_chan,dataptr dz,int bspl);
  130. static void do_multichan_etab_splice(int gsize_per_chan,dataptr dz);
  131. static void do_multichan_esplice(int gsize_per_chan,dataptr dz,int espl);
  132. static int write_multichan_grain(double rpos,int chan, int chanb,float **maxwrite,float **Fptr,float **FFptr,int gsize_per_chan,int *nctr,dataptr dz);
  133. static double get_wrappage_position(dataptr dz);
  134. static int create_wrappage_buffers(dataptr dz);
  135. static int wrappage_preprocess(dataptr dz);
  136. static int initialise_unused_wrappage_vflags(dataptr dz);
  137. static void param_check_and_convert(int *zerovel,dataptr dz);
  138. static int check_spatialisation_data(dataptr dz);
  139. static void convert_pitch(int paramno,dataptr dz);
  140. static int initialise_data(dataptr dz);
  141. static int set_internal_flags(dataptr dz);
  142. static void initialise_channel_configuration(dataptr dz);
  143. static int granula_setup(dataptr dz);
  144. static int check_for_zeroes(int paramno,dataptr dz);
  145. static int calc_overflows(dataptr dz);
  146. static void set_ranges(dataptr dz);
  147. static int make_splice_tables(dataptr dz);
  148. static void convert_value_to_int(int paramno,double convertor,dataptr dz);
  149. static int check_for_zeroes(int paramno,dataptr dz);
  150. static void set_this_range(int rangeno,int hino, int lono, int flagno, dataptr dz);
  151. static void set_this_drange(int rangeno,int hino, int lono, int flagno, dataptr dz);
  152. static int calc_max_scatter(int mgi,int *max_scatlen, dataptr dz);
  153. static void adjust_overflows(int mscat, dataptr dz);
  154. static int store_the_filename(char *filename,dataptr dz);
  155. static void get_centre(double thistime,dataptr dz);
  156. static int handle_the_special_data(int *cmdlinecnt,char ***cmdline,dataptr dz);
  157. static int read_centre_data(char *filename,dataptr dz);
  158. static int read_timevarying_data(double itime, double otime, dataptr dz);
  159. #define wrapcentre scalefact // USe dz->scalefact to store wrapcentre
  160. /**************************************** MAIN *********************************************/
  161. int main(int argc,char *argv[])
  162. {
  163. int exit_status;
  164. dataptr dz = NULL;
  165. char **cmdline;
  166. int cmdlinecnt;
  167. //aplptr ap;
  168. int is_launched = FALSE;
  169. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  170. fprintf(stdout,"%s\n",cdp_version);
  171. fflush(stdout);
  172. return 0;
  173. }
  174. /* CHECK FOR SOUNDLOOM */
  175. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  176. sloom = 0;
  177. sloombatch = 1;
  178. }
  179. if(sflinit("cdp")){
  180. sfperror("cdp: initialisation\n");
  181. return(FAILED);
  182. }
  183. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  184. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  185. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  186. return(FAILED);
  187. }
  188. if(!sloom) {
  189. if(argc == 1) {
  190. usage1();
  191. return(FAILED);
  192. } else if(argc == 2) {
  193. usage2(argv[1]);
  194. return(FAILED);
  195. }
  196. }
  197. dz->maxmode = 0;
  198. if(!sloom) {
  199. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  200. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  201. return(FAILED);
  202. }
  203. cmdline = argv;
  204. cmdlinecnt = argc;
  205. if((get_the_process_no(argv[0],dz))<0)
  206. return(FAILED);
  207. cmdline++;
  208. cmdlinecnt--;
  209. // setup_particular_application =
  210. if((exit_status = setup_wrappage_application(dz))<0) {
  211. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  212. return(FAILED);
  213. }
  214. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  215. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  216. return(FAILED);
  217. }
  218. } else {
  219. //parse_TK_data() =
  220. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  221. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  222. return(exit_status);
  223. }
  224. }
  225. //ap = dz->application;
  226. // parse_infile_and_hone_type() =
  227. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  228. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  229. return(FAILED);
  230. }
  231. // open_first_infile CDP LIB
  232. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  233. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  234. return(FAILED);
  235. }
  236. cmdlinecnt--;
  237. cmdline++;
  238. // handle_extra_infiles() ........
  239. if((exit_status = handle_extra_infiles(&cmdline,&cmdlinecnt,dz))<0) {
  240. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  241. return(FAILED);
  242. }
  243. //NEW
  244. // setup_param_ranges_and_defaults() =
  245. if((exit_status = setup_wrappage_param_ranges_and_defaults(dz))<0) {
  246. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  247. return(FAILED);
  248. }
  249. // handle_outfile() =
  250. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) { // Just takes its name for now
  251. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  252. return(FAILED);
  253. }
  254. // handle_formants() redundant
  255. // handle_formant_quiksearch() redundant
  256. // handle_special_data() redundant
  257. if((exit_status = handle_the_special_data(&cmdlinecnt,&cmdline,dz))<0) {
  258. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  259. return(FAILED);
  260. }
  261. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  262. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  263. return(FAILED);
  264. }
  265. // check_param_validity_and_consistency....
  266. if((exit_status = check_wrappage_param_validity_and_consistency(dz))<0) {
  267. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  268. return(FAILED);
  269. }
  270. is_launched = TRUE;
  271. //param_preprocess()
  272. if((exit_status = wrappage_preprocess(dz))<0) {
  273. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  274. return(FAILED);
  275. }
  276. if((exit_status = create_wrappage_buffers(dz))<0) {
  277. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  278. return(FAILED);
  279. }
  280. //spec_process_file =
  281. if((exit_status = wrappage(dz))<0) {
  282. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  283. return(FAILED);
  284. }
  285. if((exit_status = complete_output(dz))<0) { // CDP LIB
  286. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  287. return(FAILED);
  288. }
  289. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  290. free(dz);
  291. return(SUCCEEDED);
  292. }
  293. /**************************************************/
  294. /* GENERAL FUNCTIONS, REPLACING CDP LIB FUNCTIONS */
  295. /**************************************************/
  296. /****************************** SET_PARAM_DATA *********************************/
  297. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  298. {
  299. ap->special_data = (char)special_data;
  300. ap->param_cnt = (char)paramcnt;
  301. ap->max_param_cnt = (char)maxparamcnt;
  302. if(ap->max_param_cnt>0) {
  303. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  304. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  305. return(MEMORY_ERROR);
  306. }
  307. strcpy(ap->param_list,paramlist);
  308. }
  309. return(FINISHED);
  310. }
  311. /****************************** SET_VFLGS *********************************/
  312. int set_vflgs
  313. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  314. {
  315. ap->option_cnt = (char) optcnt; /*RWD added cast */
  316. if(optcnt) {
  317. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  318. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  319. return(MEMORY_ERROR);
  320. }
  321. strcpy(ap->option_list,optlist);
  322. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  323. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  324. return(MEMORY_ERROR);
  325. }
  326. strcpy(ap->option_flags,optflags);
  327. }
  328. ap->vflag_cnt = (char) vflagcnt;
  329. ap->variant_param_cnt = (char) vparamcnt;
  330. if(vflagcnt) {
  331. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  332. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  333. return(MEMORY_ERROR);
  334. }
  335. strcpy(ap->variant_list,varlist);
  336. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  337. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  338. return(MEMORY_ERROR);
  339. }
  340. strcpy(ap->variant_flags,varflags);
  341. }
  342. return(FINISHED);
  343. }
  344. /***************************** APPLICATION_INIT **************************/
  345. int application_init(dataptr dz)
  346. {
  347. int exit_status;
  348. int storage_cnt;
  349. int tipc, brkcnt;
  350. aplptr ap = dz->application;
  351. if(ap->vflag_cnt>0)
  352. initialise_vflags(dz);
  353. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  354. ap->total_input_param_cnt = (char)tipc;
  355. if(tipc>0) {
  356. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  357. return(exit_status);
  358. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  359. return(exit_status);
  360. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  361. return(exit_status);
  362. }
  363. brkcnt = tipc;
  364. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  365. if(brkcnt>0) {
  366. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  367. return(exit_status);
  368. }
  369. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  370. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  371. return(exit_status);
  372. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  373. return(exit_status);
  374. }
  375. if((exit_status = mark_parameter_types(dz,ap))<0)
  376. return(exit_status);
  377. // establish_infile_constants() replaced by
  378. switch(dz->process) {
  379. case(PSOW_INTERLEAVE):
  380. case(PSOW_REPLACE):
  381. dz->infilecnt = 2;
  382. break;
  383. default:
  384. dz->infilecnt = 1;
  385. break;
  386. }
  387. if((exit_status = setup_internal_arrays_and_array_pointers(dz))<0)
  388. return(exit_status);
  389. //establish_bufptrs_and_extra_buffers():
  390. return(FINISHED);
  391. }
  392. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  393. /* RWD mallo changed to calloc; helps debug verison run as release! */
  394. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  395. {
  396. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  397. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  398. return(MEMORY_ERROR);
  399. }
  400. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  401. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  402. return(MEMORY_ERROR);
  403. }
  404. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  405. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  406. return(MEMORY_ERROR);
  407. }
  408. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  409. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  410. return(MEMORY_ERROR);
  411. }
  412. return(FINISHED);
  413. }
  414. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  415. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  416. {
  417. int n;
  418. for(n=0;n<storage_cnt;n++) {
  419. dz->is_int[n] = (char)0;
  420. dz->no_brk[n] = (char)0;
  421. }
  422. return(FINISHED);
  423. }
  424. /***************************** MARK_PARAMETER_TYPES **************************/
  425. int mark_parameter_types(dataptr dz,aplptr ap)
  426. {
  427. int n, m; /* PARAMS */
  428. for(n=0;n<ap->max_param_cnt;n++) {
  429. switch(ap->param_list[n]) {
  430. case('0'): break; /* dz->is_active[n] = 0 is default */
  431. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  432. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  433. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  434. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  435. default:
  436. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  437. return(PROGRAM_ERROR);
  438. }
  439. } /* OPTIONS */
  440. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  441. switch(ap->option_list[n]) {
  442. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  443. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  444. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  445. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  446. default:
  447. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  448. return(PROGRAM_ERROR);
  449. }
  450. } /* VARIANTS */
  451. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  452. switch(ap->variant_list[n]) {
  453. case('0'): break;
  454. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  455. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  456. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  457. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  458. default:
  459. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  460. return(PROGRAM_ERROR);
  461. }
  462. } /* INTERNAL */
  463. for(n=0,
  464. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  465. switch(ap->internal_param_list[n]) {
  466. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  467. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  468. case('d'): dz->no_brk[m] = (char)1; break;
  469. default:
  470. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  471. return(PROGRAM_ERROR);
  472. }
  473. }
  474. return(FINISHED);
  475. }
  476. /************************ HANDLE_THE_OUTFILE *********************/
  477. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  478. {
  479. int exit_status;
  480. char *filename = (*cmdline)[0];
  481. if(filename[0]=='-' && filename[1]=='f') {
  482. dz->floatsam_output = 1;
  483. dz->true_outfile_stype = SAMP_FLOAT;
  484. filename+= 2;
  485. }
  486. if(!sloom) {
  487. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  488. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  489. return(DATA_ERROR);
  490. }
  491. }
  492. if((exit_status = store_the_filename(filename,dz))<0)
  493. return(exit_status);
  494. (*cmdline)++;
  495. (*cmdlinecnt)--;
  496. return(FINISHED);
  497. }
  498. /***************************** ESTABLISH_APPLICATION **************************/
  499. int establish_application(dataptr dz)
  500. {
  501. aplptr ap;
  502. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  503. sprintf(errstr,"establish_application()\n");
  504. return(MEMORY_ERROR);
  505. }
  506. ap = dz->application;
  507. memset((char *)ap,0,sizeof(struct applic));
  508. return(FINISHED);
  509. }
  510. /************************* INITIALISE_VFLAGS *************************/
  511. int initialise_vflags(dataptr dz)
  512. {
  513. int n;
  514. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  515. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  516. return(MEMORY_ERROR);
  517. }
  518. for(n=0;n<dz->application->vflag_cnt;n++)
  519. dz->vflag[n] = FALSE;
  520. return FINISHED;
  521. }
  522. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  523. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  524. {
  525. int n;
  526. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  527. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  528. return(MEMORY_ERROR);
  529. }
  530. for(n=0;n<tipc;n++)
  531. ap->default_val[n] = 0.0;
  532. return(FINISHED);
  533. }
  534. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  535. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  536. {
  537. int n;
  538. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  539. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  540. return(MEMORY_ERROR);
  541. }
  542. for(n=0;n<tipc;n++)
  543. dz->is_active[n] = (char)0;
  544. return(FINISHED);
  545. }
  546. /************************* SETUP_WRAPPAGE_APPLICATION *******************/
  547. int setup_wrappage_application(dataptr dz)
  548. {
  549. int exit_status;
  550. aplptr ap;
  551. if((exit_status = establish_application(dz))<0) // GLOBAL
  552. return(FAILED);
  553. ap = dz->application;
  554. // SEE parstruct FOR EXPLANATION of next 2 functions
  555. if((exit_status = set_param_data(ap,WRAP_FOCUS,20,20,"iDDDDDDDDDDDDDDDDDDd")) < 0)
  556. return(FAILED);
  557. if((exit_status = set_vflgs(ap,"b",1,"i","eo",2,0,"00")) < 0)
  558. return(FAILED);
  559. if((exit_status = set_internalparam_data("ddddiiiiiiiiiiiiiiiii",ap)) < 0)
  560. return(FAILED);
  561. // set_legal_infile_structure -->
  562. dz->has_otherfile = FALSE;
  563. // assign_process_logic -->
  564. dz->input_data_type = ONE_OR_MANY_SNDFILES;
  565. dz->process_type = UNEQUAL_SNDFILE;
  566. dz->outfiletype = SNDFILE_OUT;
  567. return application_init(dz); //GLOBAL
  568. }
  569. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  570. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  571. {
  572. int n;
  573. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  574. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  575. return(MEMORY_ERROR);
  576. }
  577. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  578. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  579. return(MEMORY_ERROR);
  580. }
  581. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  582. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  583. return(MEMORY_ERROR);
  584. }
  585. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  586. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  587. return(MEMORY_ERROR);
  588. }
  589. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  590. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  591. return(MEMORY_ERROR);
  592. }
  593. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  594. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  595. return(MEMORY_ERROR);
  596. }
  597. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  598. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  599. return(MEMORY_ERROR);
  600. }
  601. for(n=0;n<brkcnt;n++) {
  602. dz->brk[n] = NULL;
  603. dz->brkptr[n] = NULL;
  604. dz->brkinit[n] = 0;
  605. dz->brksize[n] = 0;
  606. }
  607. return(FINISHED);
  608. }
  609. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  610. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  611. {
  612. int exit_status;
  613. infileptr infile_info;
  614. if(!sloom) {
  615. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  616. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  617. return(MEMORY_ERROR);
  618. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  619. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  620. return(PROGRAM_ERROR);
  621. } else if(infile_info->filetype != SNDFILE) {
  622. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  623. return(DATA_ERROR);
  624. } else if(infile_info->channels != 1) {
  625. sprintf(errstr,"File %s is not of correct type (must be mono)\n",cmdline[0]);
  626. return(DATA_ERROR);
  627. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  628. sprintf(errstr,"Failed to copy file parsing information\n");
  629. return(PROGRAM_ERROR);
  630. }
  631. free(infile_info);
  632. }
  633. return(FINISHED);
  634. }
  635. /************************* SETUP_WRAPPAGE_PARAM_RANGES_AND_DEFAULTS *******************/
  636. int setup_wrappage_param_ranges_and_defaults(dataptr dz)
  637. {
  638. int exit_status, n, chans = dz->infile->channels;
  639. int infilesize_in_samps;
  640. double sr = (double)dz->infile->srate, duration;
  641. aplptr ap = dz->application;
  642. infilesize_in_samps = dz->insams[0];
  643. for(n = 1;n < dz->infilecnt;n++)
  644. infilesize_in_samps = min(dz->insams[1],infilesize_in_samps);
  645. infilesize_in_samps /= chans;
  646. duration = (double)infilesize_in_samps/sr;
  647. // set_param_ranges()
  648. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  649. // NB total_input_param_cnt is > 0 !!!s
  650. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  651. return(FAILED);
  652. // get_param_ranges()
  653. ap->lo[WRAP_OUTCHANS] = 2.0;
  654. ap->hi[WRAP_OUTCHANS] = 16.0;
  655. ap->default_val[WRAP_OUTCHANS] = 2.0;
  656. ap->lo[WRAP_SPREAD] = 0.0;
  657. ap->hi[WRAP_SPREAD] = 16.0;
  658. ap->default_val[WRAP_SPREAD] = 16.0;
  659. ap->lo[WRAP_DEPTH] = 0.0;
  660. ap->hi[WRAP_DEPTH] = 16.0;
  661. ap->default_val[WRAP_DEPTH] = 4.0;
  662. ap->lo[WRAP_VELOCITY] = 0.0;
  663. ap->hi[WRAP_VELOCITY] = WRAP_MAX_VELOCITY;
  664. ap->default_val[WRAP_VELOCITY] = 1.0;
  665. ap->lo[WRAP_HVELOCITY] = 0.0;
  666. ap->hi[WRAP_HVELOCITY] = WRAP_MAX_VELOCITY;
  667. ap->default_val[WRAP_HVELOCITY] = 1.0;
  668. ap->lo[WRAP_DENSITY] = FLTERR;
  669. ap->hi[WRAP_DENSITY] = WRAP_MAX_DENSITY;
  670. ap->default_val[WRAP_DENSITY] = WRAP_DEFAULT_DENSITY;
  671. ap->lo[WRAP_HDENSITY] = (1.0/sr);
  672. ap->hi[WRAP_HDENSITY] = (double)MAXSHORT/2.0;
  673. ap->default_val[WRAP_HDENSITY] = WRAP_DEFAULT_DENSITY;
  674. ap->lo[WRAP_GRAINSIZE] = WRAP_MIN_SPLICELEN * 2.0;
  675. ap->hi[WRAP_GRAINSIZE] = (infilesize_in_samps/sr) * SECS_TO_MS;
  676. ap->default_val[WRAP_GRAINSIZE] = WRAP_DEFAULT_GRAINSIZE;
  677. ap->lo[WRAP_HGRAINSIZE] = WRAP_MIN_SPLICELEN * 2.0;
  678. ap->hi[WRAP_HGRAINSIZE] = (infilesize_in_samps/sr) * SECS_TO_MS;
  679. ap->default_val[WRAP_HGRAINSIZE]= WRAP_DEFAULT_GRAINSIZE;
  680. ap->hi[WRAP_PITCH] = LOG2(dz->nyquist/WRAP_MIN_LIKELY_PITCH) * SEMITONES_PER_OCTAVE;
  681. ap->lo[WRAP_PITCH] = -(ap->hi[WRAP_PITCH]);
  682. ap->default_val[WRAP_PITCH] = 0.0;
  683. ap->hi[WRAP_HPITCH] = LOG2(dz->nyquist/WRAP_MIN_LIKELY_PITCH) * SEMITONES_PER_OCTAVE;
  684. ap->lo[WRAP_HPITCH] = -(ap->hi[WRAP_HPITCH]);
  685. ap->default_val[WRAP_HPITCH] = 0.0;
  686. ap->lo[WRAP_AMP] = 0.0;
  687. ap->hi[WRAP_AMP] = 1.0;
  688. ap->default_val[WRAP_AMP] = 1.0;
  689. ap->lo[WRAP_HAMP] = 0.0;
  690. ap->hi[WRAP_HAMP] = 1.0;
  691. ap->default_val[WRAP_HAMP] = 1.0;
  692. ap->lo[WRAP_BSPLICE] = WRAP_MIN_SPLICELEN;
  693. ap->hi[WRAP_BSPLICE] = ap->hi[WRAP_GRAINSIZE]/2.0;
  694. ap->default_val[WRAP_BSPLICE] = WRAP_DEFAULT_SPLICELEN;
  695. ap->lo[WRAP_HBSPLICE] = WRAP_MIN_SPLICELEN;
  696. ap->hi[WRAP_HBSPLICE] = ap->hi[WRAP_GRAINSIZE]/2.0;
  697. ap->default_val[WRAP_HBSPLICE] = WRAP_DEFAULT_SPLICELEN;
  698. ap->lo[WRAP_ESPLICE] = WRAP_MIN_SPLICELEN;
  699. ap->hi[WRAP_ESPLICE] = ap->hi[WRAP_GRAINSIZE]/2.0;
  700. ap->default_val[WRAP_ESPLICE] = WRAP_DEFAULT_SPLICELEN;
  701. ap->lo[WRAP_HESPLICE] = WRAP_MIN_SPLICELEN;
  702. ap->hi[WRAP_HESPLICE] = ap->hi[WRAP_GRAINSIZE]/2.0;
  703. ap->default_val[WRAP_HESPLICE] = WRAP_DEFAULT_SPLICELEN;
  704. ap->lo[WRAP_SRCHRANGE] = 0.0;
  705. ap->hi[WRAP_SRCHRANGE] = (duration * 2.0) * SECS_TO_MS;
  706. ap->default_val[WRAP_SRCHRANGE] = 0.0;
  707. ap->lo[WRAP_SCATTER] = 0.0;
  708. ap->hi[WRAP_SCATTER] = 1.0;
  709. ap->default_val[WRAP_SCATTER] = WRAP_DEFAULT_SCATTER;
  710. ap->lo[WRAP_OUTLEN] = 0.0;
  711. ap->hi[WRAP_OUTLEN] = BIG_TIME;
  712. ap->default_val[WRAP_OUTLEN] = 0.0;
  713. ap->lo[WRAP_BUFXX] = 0.0;
  714. ap->hi[WRAP_BUFXX] = 64.0;
  715. ap->default_val[WRAP_BUFXX] = 0.0;
  716. if(!sloom)
  717. put_default_vals_in_all_params(dz);
  718. return(FINISHED);
  719. }
  720. /********************************* PARSE_SLOOM_DATA *********************************/
  721. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  722. {
  723. int exit_status;
  724. int cnt = 1, infilecnt;
  725. int filesize, insams, inbrksize;
  726. double dummy;
  727. int true_cnt = 0;
  728. //aplptr ap;
  729. while(cnt<=PRE_CMDLINE_DATACNT) {
  730. if(cnt > argc) {
  731. sprintf(errstr,"Insufficient data sent from TK\n");
  732. return(DATA_ERROR);
  733. }
  734. switch(cnt) {
  735. case(1):
  736. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  737. sprintf(errstr,"Cannot read process no. sent from TK\n");
  738. return(DATA_ERROR);
  739. }
  740. break;
  741. case(2):
  742. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  743. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  744. return(DATA_ERROR);
  745. }
  746. if(dz->mode > 0)
  747. dz->mode--;
  748. //setup_particular_application() =
  749. if((exit_status = setup_wrappage_application(dz))<0)
  750. return(exit_status);
  751. //ap = dz->application;
  752. break;
  753. case(3):
  754. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  755. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  756. return(DATA_ERROR);
  757. }
  758. if(infilecnt < 1) {
  759. true_cnt = cnt + 1;
  760. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  761. }
  762. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  763. return(exit_status);
  764. break;
  765. case(INPUT_FILETYPE+4):
  766. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  767. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  768. return(DATA_ERROR);
  769. }
  770. break;
  771. case(INPUT_FILESIZE+4):
  772. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  773. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  774. return(DATA_ERROR);
  775. }
  776. dz->insams[0] = filesize;
  777. break;
  778. case(INPUT_INSAMS+4):
  779. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  780. sprintf(errstr,"Cannot read insams sent from TK\n");
  781. return(DATA_ERROR);
  782. }
  783. dz->insams[0] = insams;
  784. break;
  785. case(INPUT_SRATE+4):
  786. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  787. sprintf(errstr,"Cannot read srate sent from TK\n");
  788. return(DATA_ERROR);
  789. }
  790. break;
  791. case(INPUT_CHANNELS+4):
  792. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  793. sprintf(errstr,"Cannot read channels sent from TK\n");
  794. return(DATA_ERROR);
  795. }
  796. break;
  797. case(INPUT_STYPE+4):
  798. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  799. sprintf(errstr,"Cannot read stype sent from TK\n");
  800. return(DATA_ERROR);
  801. }
  802. break;
  803. case(INPUT_ORIGSTYPE+4):
  804. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  805. sprintf(errstr,"Cannot read origstype sent from TK\n");
  806. return(DATA_ERROR);
  807. }
  808. break;
  809. case(INPUT_ORIGRATE+4):
  810. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  811. sprintf(errstr,"Cannot read origrate sent from TK\n");
  812. return(DATA_ERROR);
  813. }
  814. break;
  815. case(INPUT_MLEN+4):
  816. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  817. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  818. return(DATA_ERROR);
  819. }
  820. break;
  821. case(INPUT_DFAC+4):
  822. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  823. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  824. return(DATA_ERROR);
  825. }
  826. break;
  827. case(INPUT_ORIGCHANS+4):
  828. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  829. sprintf(errstr,"Cannot read origchans sent from TK\n");
  830. return(DATA_ERROR);
  831. }
  832. break;
  833. case(INPUT_SPECENVCNT+4):
  834. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  835. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  836. return(DATA_ERROR);
  837. }
  838. dz->specenvcnt = dz->infile->specenvcnt;
  839. break;
  840. case(INPUT_WANTED+4):
  841. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  842. sprintf(errstr,"Cannot read wanted sent from TK\n");
  843. return(DATA_ERROR);
  844. }
  845. break;
  846. case(INPUT_WLENGTH+4):
  847. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  848. sprintf(errstr,"Cannot read wlength sent from TK\n");
  849. return(DATA_ERROR);
  850. }
  851. break;
  852. case(INPUT_OUT_CHANS+4):
  853. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  854. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  855. return(DATA_ERROR);
  856. }
  857. break;
  858. /* RWD these chanegs to samps - tk will have to deal with that! */
  859. case(INPUT_DESCRIPTOR_BYTES+4):
  860. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  861. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  862. return(DATA_ERROR);
  863. }
  864. break;
  865. case(INPUT_IS_TRANSPOS+4):
  866. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  867. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  868. return(DATA_ERROR);
  869. }
  870. break;
  871. case(INPUT_COULD_BE_TRANSPOS+4):
  872. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  873. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  874. return(DATA_ERROR);
  875. }
  876. break;
  877. case(INPUT_COULD_BE_PITCH+4):
  878. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  879. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  880. return(DATA_ERROR);
  881. }
  882. break;
  883. case(INPUT_DIFFERENT_SRATES+4):
  884. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  885. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  886. return(DATA_ERROR);
  887. }
  888. break;
  889. case(INPUT_DUPLICATE_SNDS+4):
  890. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  891. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  892. return(DATA_ERROR);
  893. }
  894. break;
  895. case(INPUT_BRKSIZE+4):
  896. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  897. sprintf(errstr,"Cannot read brksize sent from TK\n");
  898. return(DATA_ERROR);
  899. }
  900. if(inbrksize > 0) {
  901. switch(dz->input_data_type) {
  902. case(WORDLIST_ONLY):
  903. break;
  904. case(PITCH_AND_PITCH):
  905. case(PITCH_AND_TRANSPOS):
  906. case(TRANSPOS_AND_TRANSPOS):
  907. dz->tempsize = inbrksize;
  908. break;
  909. case(BRKFILES_ONLY):
  910. case(UNRANGED_BRKFILE_ONLY):
  911. case(DB_BRKFILES_ONLY):
  912. case(ALL_FILES):
  913. case(ANY_NUMBER_OF_ANY_FILES):
  914. if(dz->extrabrkno < 0) {
  915. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  916. return(DATA_ERROR);
  917. }
  918. if(dz->brksize == NULL) {
  919. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  920. return(PROGRAM_ERROR);
  921. }
  922. dz->brksize[dz->extrabrkno] = inbrksize;
  923. break;
  924. default:
  925. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  926. dz->input_data_type);
  927. return(PROGRAM_ERROR);
  928. }
  929. break;
  930. }
  931. break;
  932. case(INPUT_NUMSIZE+4):
  933. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  934. sprintf(errstr,"Cannot read numsize sent from TK\n");
  935. return(DATA_ERROR);
  936. }
  937. break;
  938. case(INPUT_LINECNT+4):
  939. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  940. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  941. return(DATA_ERROR);
  942. }
  943. break;
  944. case(INPUT_ALL_WORDS+4):
  945. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  946. sprintf(errstr,"Cannot read all_words sent from TK\n");
  947. return(DATA_ERROR);
  948. }
  949. break;
  950. case(INPUT_ARATE+4):
  951. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  952. sprintf(errstr,"Cannot read arate sent from TK\n");
  953. return(DATA_ERROR);
  954. }
  955. break;
  956. case(INPUT_FRAMETIME+4):
  957. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  958. sprintf(errstr,"Cannot read frametime sent from TK\n");
  959. return(DATA_ERROR);
  960. }
  961. dz->frametime = (float)dummy;
  962. break;
  963. case(INPUT_WINDOW_SIZE+4):
  964. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  965. sprintf(errstr,"Cannot read window_size sent from TK\n");
  966. return(DATA_ERROR);
  967. }
  968. break;
  969. case(INPUT_NYQUIST+4):
  970. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  971. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  972. return(DATA_ERROR);
  973. }
  974. break;
  975. case(INPUT_DURATION+4):
  976. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  977. sprintf(errstr,"Cannot read duration sent from TK\n");
  978. return(DATA_ERROR);
  979. }
  980. break;
  981. case(INPUT_MINBRK+4):
  982. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  983. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  984. return(DATA_ERROR);
  985. }
  986. break;
  987. case(INPUT_MAXBRK+4):
  988. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  989. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  990. return(DATA_ERROR);
  991. }
  992. break;
  993. case(INPUT_MINNUM+4):
  994. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  995. sprintf(errstr,"Cannot read minnum sent from TK\n");
  996. return(DATA_ERROR);
  997. }
  998. break;
  999. case(INPUT_MAXNUM+4):
  1000. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  1001. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  1002. return(DATA_ERROR);
  1003. }
  1004. break;
  1005. default:
  1006. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  1007. return(PROGRAM_ERROR);
  1008. }
  1009. cnt++;
  1010. }
  1011. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  1012. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  1013. return(DATA_ERROR);
  1014. }
  1015. if(true_cnt)
  1016. cnt = true_cnt;
  1017. *cmdlinecnt = 0;
  1018. while(cnt < argc) {
  1019. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  1020. return(exit_status);
  1021. cnt++;
  1022. }
  1023. return(FINISHED);
  1024. }
  1025. /********************************* GET_TK_CMDLINE_WORD *********************************/
  1026. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  1027. {
  1028. if(*cmdlinecnt==0) {
  1029. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  1030. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1031. return(MEMORY_ERROR);
  1032. }
  1033. } else {
  1034. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  1035. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1036. return(MEMORY_ERROR);
  1037. }
  1038. }
  1039. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  1040. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  1041. return(MEMORY_ERROR);
  1042. }
  1043. strcpy((*cmdline)[*cmdlinecnt],q);
  1044. (*cmdlinecnt)++;
  1045. return(FINISHED);
  1046. }
  1047. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  1048. int assign_file_data_storage(int infilecnt,dataptr dz)
  1049. {
  1050. int exit_status;
  1051. int no_sndfile_system_files = FALSE;
  1052. dz->infilecnt = infilecnt;
  1053. if((exit_status = allocate_filespace(dz))<0)
  1054. return(exit_status);
  1055. if(no_sndfile_system_files)
  1056. dz->infilecnt = 0;
  1057. return(FINISHED);
  1058. }
  1059. /************************* SETUP_INTERNAL_ARRAYS_AND_ARRAY_POINTERS *******************/
  1060. int setup_internal_arrays_and_array_pointers(dataptr dz)
  1061. {
  1062. int n;
  1063. dz->array_cnt = 4;
  1064. dz->iarray_cnt = 2;
  1065. dz->larray_cnt = 0;
  1066. dz->ptr_cnt = 0;
  1067. dz->fptr_cnt = 4;
  1068. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  1069. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  1070. return(MEMORY_ERROR);
  1071. }
  1072. for(n=0;n<dz->array_cnt;n++)
  1073. dz->parray[n] = NULL;
  1074. if((dz->iparray = (int **)malloc(dz->iarray_cnt * sizeof(int *)))==NULL) {
  1075. sprintf(errstr,"INSUFFICIENT MEMORY for internal int arrays.\n");
  1076. return(MEMORY_ERROR);
  1077. }
  1078. for(n=0;n<dz->iarray_cnt;n++)
  1079. dz->iparray[n] = NULL;
  1080. if((dz->fptr = (float **)malloc(dz->fptr_cnt * sizeof(float *)))==NULL) {
  1081. sprintf(errstr,"INSUFFICIENT MEMORY for internal float-pointer arrays.\n");
  1082. return(MEMORY_ERROR);
  1083. }
  1084. for(n=0;n<dz->fptr_cnt;n++)
  1085. dz->fptr[n] = NULL;
  1086. return(FINISHED);
  1087. }
  1088. /**************************** CHECK_WRAPPAGE_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  1089. int check_wrappage_param_validity_and_consistency(dataptr dz)
  1090. {
  1091. int exit_status;
  1092. int zerovel = FALSE;
  1093. int total_flags = WRAP_SFLAGCNT+WRAP_FLAGCNT;
  1094. int n;
  1095. int min_infilesize = INT_MAX;
  1096. initrand48();
  1097. if(dz->application->vflag_cnt < WRAP_MAX_VFLAGS
  1098. &&(exit_status = initialise_unused_wrappage_vflags(dz))<0)
  1099. return(exit_status); /* Create flags inaccessible to user in certain modes */
  1100. for(n=0;n<dz->infilecnt;n++)
  1101. min_infilesize = min(min_infilesize,dz->insams[n]);
  1102. if(min_infilesize == INT_MAX) {
  1103. sprintf(errstr,"Problem estimating min infilesize.\n");
  1104. return(PROGRAM_ERROR);
  1105. }
  1106. dz->iparam[WRAP_ORIG_SMPSIZE] = min_infilesize/dz->infile->channels;
  1107. if((dz->iparray[WRAP_FLAGS]=(int *)malloc(total_flags * sizeof(int)))==NULL) {
  1108. sprintf(errstr,"INSUFFICIENT MEMORY for wrappage internal flags.\n");
  1109. return(MEMORY_ERROR); /* establish internal flagging */
  1110. }
  1111. for(n=0;n<total_flags;n++)
  1112. dz->iparray[WRAP_FLAGS][n] = 0;
  1113. param_check_and_convert(&zerovel,dz);
  1114. if(zerovel && dz->param[WRAP_OUTLEN]==0.0) {
  1115. sprintf(errstr, "Zero VELOCITY found: Outfile length must be specified.\n");
  1116. return(USER_ERROR);
  1117. }
  1118. dz->out_chans = dz->iparam[WRAP_OUTCHANS];
  1119. if((exit_status = check_spatialisation_data(dz))<0)
  1120. return(exit_status);
  1121. convert_pitch(WRAP_PITCH,dz);
  1122. convert_pitch(WRAP_HPITCH,dz);
  1123. dz->iparam[WRAP_ARRAYSIZE] = BIGARRAY;
  1124. if((exit_status = initialise_data(dz))<0) /* before setup_environment */
  1125. return(exit_status);
  1126. if((exit_status = set_internal_flags(dz))<0)
  1127. return(exit_status);
  1128. initialise_channel_configuration(dz);
  1129. dz->outfile->channels = dz->iparam[WRAP_OUTCHANS]; /* preset channel count for OUTPUT */
  1130. if((exit_status = granula_setup(dz))<0)
  1131. return(exit_status); /* has to be done before buffers.c */
  1132. dz->extra_bufcnt = 1;
  1133. dz->bufcnt = 2;
  1134. dz->bufcnt += (dz->infilecnt * 2);
  1135. if((dz->sampbuf = (float **)malloc(dz->bufcnt * sizeof(float *)))==NULL) {
  1136. sprintf(errstr,"INSUFFICIENT MEMORY (1)to reallocate space for wrappage.\n");
  1137. return(MEMORY_ERROR);
  1138. }
  1139. if((dz->sbufptr = (float **)malloc(dz->bufcnt * sizeof(float *)))==NULL) {
  1140. sprintf(errstr,"INSUFFICIENT MEMORY (2)to reallocate space for wrappage.\n");
  1141. return(MEMORY_ERROR);
  1142. }
  1143. if((dz->extrabuf = (float **)malloc(dz->extra_bufcnt * sizeof(float *)))==NULL) {
  1144. sprintf(errstr,"INSUFFICIENT MEMORY (3)to reallocate space for wrappage.\n");
  1145. return(MEMORY_ERROR);
  1146. }
  1147. dz->outfile->channels = dz->iparam[0];
  1148. return(FINISHED);
  1149. }
  1150. /******************************************* GET_THE_PROCESS_NO *****************************/
  1151. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1152. {
  1153. if (!strcmp(prog_identifier_from_cmdline,"wrappage")) dz->process = WRAPPAGE;
  1154. else {
  1155. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  1156. return(USAGE_ONLY);
  1157. }
  1158. return FINISHED;
  1159. }
  1160. /******************************** USAGE1 ********************************/
  1161. int usage1(void)
  1162. {
  1163. return usage2("wrappage");
  1164. }
  1165. /******************************** USAGE2 ********************************/
  1166. /******************************** USAGE2 ********************************/
  1167. int usage2(char *str)
  1168. {
  1169. if (!strcmp(str,"wrappage")) {
  1170. fprintf(stdout,
  1171. "GRANULAR RECONSTITUTION OF ONE OR MORE SOUNDFILES OVER MULTICHANNEL SPACE.\n\n"
  1172. "USAGE (name of outfile must NOT end with a '1')\n"
  1173. "wrappage wrappage infile [infile2 ...] outfile centre outchans spread depth\n"
  1174. "veloc hveloc dens hdens gsize hgsize pshift hpshift amp hamp bsplice hbsplice\n"
  1175. "esplice hesplice range jitter outlength [-bmult] [-e] [-o]\n\n"
  1176. "CENTRE: central position of output image in outputfile. (Range 0-outchans).\n"
  1177. " Centre values below 1 are positions between last chan and 1st chan.\n"
  1178. " Numeric, or a file of triples \"time centre direction\". Direction \n"
  1179. " (1 clockwise, -1 anticlock) is of centre's move from previous position.\n"
  1180. "OUTCHANS: Number of channels in output file.\n"
  1181. "SPREAD: Width (from far left to far right) of spatialisation around centre.\n"
  1182. "DEPTH: No of channels with sound, behind leading edges of spread.\n"
  1183. " (Will always be forced to <= half the spread value).\n"
  1184. "VELOC: speed of advance in infiles, relative to outfile. (>=0)\n"
  1185. " inverse of timestretch, (permits infinite timestretch).\n"
  1186. "DENS: grain overlap (>0 : <1 leaves intergrain silence)\n"
  1187. " Extremely small values don't perform predictably.\n"
  1188. "GSIZE: grainsize in MS (must be > 2 * splicelen) (Default %.0lf)\n"
  1189. "PSHIFT: pitchshift in +|- (fractions of) semitones.\n"
  1190. "AMP: gain on grains (range 0-1) (Default 1.0)\n"
  1191. " use if amp variation required (over a range &/or in time)\n"
  1192. "BSPLICE: grain-startsplice length,in MS (Default %.0lf)\n"
  1193. "ESPLICE: grain-endsplice length,in MS (Default %.0lf)\n"
  1194. "RANGE: of search for nextgrain (in mS), before infile 'now' (Default 0).\n"
  1195. "JITTER: Randomisation of grain position (Range 0-1) Default (%.2lf).\n"
  1196. "OUTLENGTH: max outfile length (if end of data not reached).\n"
  1197. " Set to zero (Default) to ignore.\n"
  1198. " BUT if VELOCITY is ANYWHERE 0: OUTLENGTH must be given.\n"
  1199. "\n"
  1200. "HVELOC,HDENS,HGSIZE,HPSHIFT,HAMP,HBSPLICE,HESPLICE\n"
  1201. "allow a range of values to be specified for any of these params. e.g. With\n"
  1202. "PSHIFT & HPSHIFT set, random pitchshift chosen between these limits.\n"
  1203. "AND NB PSHIFT & HPSHIFT can both vary through time.\n\n"
  1204. "All params, except OUTLENGTH, OUTCHANS, can vary through time.\n"
  1205. "\n"
  1206. "MULT Long silences in output may require larger buffers to be used\n"
  1207. " \"Mult\" enlarges output buffers \"mult\" times. \n"
  1208. "-e Use exponential splices. Default, linear.\n"
  1209. "-o Velocity params ALWAYS read relative to time in INPUT file.\n"
  1210. " Other params normally read relative to time in INPUT file.\n"
  1211. " Flag forces them to be read relative to time in OUTPUT file.\n"
  1212. " \n",WRAP_DEFAULT_GRAINSIZE,WRAP_DEFAULT_SPLICELEN,WRAP_DEFAULT_SPLICELEN,WRAP_DEFAULT_SCATTER);
  1213. } else
  1214. fprintf(stdout,"Unknown option '%s'\n",str);
  1215. return(USAGE_ONLY);
  1216. }
  1217. /******************************** USAGE3 ********************************/
  1218. int usage3(char *str1,char *str2)
  1219. {
  1220. fprintf(stderr,"Insufficient parameters on command line.\n");
  1221. return(USAGE_ONLY);
  1222. }
  1223. /************************* redundant functions: to ensure libs compile OK *******************/
  1224. int assign_process_logic(dataptr dz)
  1225. {
  1226. return(FINISHED);
  1227. }
  1228. void set_legal_infile_structure(dataptr dz)
  1229. {}
  1230. int establish_bufptrs_and_extra_buffers(dataptr dz)
  1231. {
  1232. return(FINISHED);
  1233. }
  1234. int inner_loop
  1235. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  1236. {
  1237. return(FINISHED);
  1238. }
  1239. int read_special_data(char *str,dataptr dz)
  1240. {
  1241. return(FINISHED);
  1242. }
  1243. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1244. {
  1245. return(FINISHED);
  1246. }
  1247. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  1248. {
  1249. return(FINISHED);
  1250. }
  1251. /*************************** CREATE_WRAPPAGE_BUFFERS **************************/
  1252. int create_wrappage_buffers(dataptr dz)
  1253. {
  1254. size_t standard_block = (size_t) Malloc(-1);
  1255. int this_bloksize;
  1256. int exit_status, n, chans = dz->infile->channels;
  1257. int overall_size, bufdivisor = 0;
  1258. float *tailend;
  1259. int multichan_buflen = 0, multichan_bufxs = 0, outbuflen;
  1260. if((dz->extrabuf[WRAP_GBUF] = (float *)malloc(dz->iparam[WRAP_GLBUF_SMPXS] * sizeof(float)))==NULL) {
  1261. sprintf(errstr,"INSUFFICIENT MEMORY to create grain buffer.\n"); /* GRAIN BUFFER */
  1262. return(MEMORY_ERROR);
  1263. }
  1264. /* CALCULATE NUMBER OF BUFFER CHUNKS REQUIRED : bufdivisor */
  1265. if(dz->iparam[WRAP_CHANNELS]>0)
  1266. bufdivisor += chans; /* chans for multichan-infile, before reducing to mono */
  1267. bufdivisor += dz->infilecnt; /* 1 MONO buffer for each infile */
  1268. for(n=0;n<dz->iparam[WRAP_OUTCHANS];n++)
  1269. bufdivisor += 1 + (sizeof(int)/sizeof(float)); /* 1 float and 1 long buf for output, both * sizeof outchans */
  1270. this_bloksize = (int) standard_block;
  1271. for(;;) {
  1272. if((exit_status = grab_an_appropriate_block_of_wrappage_memory(&this_bloksize,(int) standard_block,bufdivisor,dz))<0)
  1273. return(exit_status);
  1274. /* CALCULATE AND ALLOCATE TOTAL MEMORY REQUIRED : overall_size */
  1275. overall_size = (dz->buflen * bufdivisor) + (dz->iparam[WRAP_BUF_SMPXS] * dz->infilecnt) + dz->iparam[WRAP_LBUF_SMPXS];
  1276. if(dz->iparam[WRAP_CHANNELS])
  1277. overall_size += chans * dz->iparam[WRAP_BUF_SMPXS]; /* IF multichan, also allow for bufxs in multichan inbuf */
  1278. if(overall_size * sizeof(float)<0) {
  1279. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n"); /* arithmetic overflow */
  1280. return(MEMORY_ERROR);
  1281. }
  1282. if((dz->bigbuf=(float *)malloc(overall_size * sizeof(float)))==NULL) {
  1283. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  1284. return(MEMORY_ERROR);
  1285. }
  1286. /* SET SIZE OF inbuf, outbuf, AND Lbuf (FOR CALCS) */
  1287. outbuflen = dz->buflen;
  1288. outbuflen *= dz->out_chans;
  1289. if(dz->iparam[WRAP_CHANNELS]) {
  1290. multichan_buflen = dz->buflen * chans;
  1291. multichan_bufxs = dz->iparam[WRAP_BUF_SMPXS] * chans;
  1292. }
  1293. dz->iparam[WRAP_LONGS_BUFLEN] = outbuflen; /* Longs buffer is same size as obuf */
  1294. if(dz->iparam[WRAP_LBUF_SMPXS] > dz->iparam[WRAP_LONGS_BUFLEN])
  1295. continue;
  1296. break;
  1297. }
  1298. /* DIVIDE UP ALLOCATED MEMORY IN SPECIALISED BUFFERS */
  1299. if(dz->iparam[WRAP_CHANNELS]) { /* sbuf : extra multichan input buffer, if required */
  1300. dz->sampbuf[WRAP_SBUF] = dz->bigbuf;
  1301. dz->sampbuf[THIS_BUF(0)] = dz->sampbuf[WRAP_SBUF] + multichan_buflen + multichan_bufxs;
  1302. } else
  1303. dz->sampbuf[THIS_BUF(0)] = dz->bigbuf;
  1304. dz->sbufptr[THIS_BUF(0)] = dz->sampbuf[THIS_BUF(0)] + dz->buflen;
  1305. dz->sampbuf[THIS_IBUF(0)] = dz->sampbuf[THIS_BUF(0)] + dz->iparam[WRAP_BUF_SMPXS];
  1306. tailend = dz->sbufptr[THIS_BUF(0)] + dz->iparam[WRAP_BUF_SMPXS];
  1307. for(n=1;n<dz->infilecnt;n++) {
  1308. dz->sampbuf[THIS_BUF(n)] = tailend; /* Lbuf: buffer for calculations */
  1309. dz->sbufptr[THIS_BUF(n)] = dz->sampbuf[THIS_BUF(n)] + dz->buflen;
  1310. dz->sampbuf[THIS_IBUF(n)] = dz->sampbuf[THIS_BUF(n)] + dz->iparam[WRAP_BUF_SMPXS];
  1311. tailend = dz->sbufptr[THIS_BUF(n)] + dz->iparam[WRAP_BUF_SMPXS];
  1312. }
  1313. dz->fptr[WRAP_LBUF] = tailend;
  1314. dz->fptr[WRAP_LBUFEND] = dz->fptr[WRAP_LBUF] + dz->iparam[WRAP_LONGS_BUFLEN];
  1315. dz->fptr[WRAP_LTAILEND] = dz->fptr[WRAP_LBUFEND] + dz->iparam[WRAP_LBUF_SMPXS];
  1316. dz->fptr[WRAP_LBUFMID] = dz->fptr[WRAP_LBUF] + dz->iparam[WRAP_LBUF_SMPXS];
  1317. dz->sampbuf[WRAP_OBUF] = dz->fptr[WRAP_LTAILEND];
  1318. /* INITIALISE BUFFERS */
  1319. memset((char *)dz->bigbuf,0,overall_size * sizeof(float));
  1320. return(FINISHED);
  1321. }
  1322. /* INPUT BUFFERS :-
  1323. *
  1324. * |-----------BUFLEN-----------|
  1325. *
  1326. * buf ibuf bufend tailend
  1327. * |_________|__________________|buf_smpxs| ..... (obuf->)
  1328. * /
  1329. * |buf_smpxs| <<-COPY_________/
  1330. *
  1331. * |-----------BUFLEN-----------|
  1332. *
  1333. *
  1334. *
  1335. * OUTPUT LONGS BUFFER:-
  1336. *
  1337. * Lbuf Lbufmid Lbufend
  1338. * |____________|_______________|_Lbuf_smpxs_|
  1339. * /
  1340. * |_Lbuf_smpxs_| <<-COPY___________/
  1341. *
  1342. */
  1343. /****************************** WRAPPAGE_PREPROCESS ****************************/
  1344. int wrappage_preprocess(dataptr dz)
  1345. {
  1346. int exit_status, orig_infilecnt;
  1347. if((dz->iparray[WRAP_PERM] = (int *)malloc(dz->infilecnt * sizeof(int)))==NULL) {
  1348. sprintf(errstr,"INSUFFICIENT MEMORY for permutation array for wrappage.\n");
  1349. return(MEMORY_ERROR);
  1350. }
  1351. dz->iparray[WRAP_PERM][0] = dz->infilecnt + 1; /* impossible value to initialise perm */
  1352. orig_infilecnt = dz->infile->channels;
  1353. dz->infile->channels = dz->iparam[0];
  1354. if((dz->outfilename = (char *)malloc(strlen("cdptest1.wav") + 1)) == NULL) {
  1355. sprintf(errstr,"Insufficient memory for temporary file name.\n");
  1356. return MEMORY_ERROR;
  1357. }
  1358. strcpy(dz->outfilename,"cdptest1.wav");
  1359. if((exit_status = create_sized_outfile(dz->outfilename,dz))<0)
  1360. return(exit_status);
  1361. dz->infile->channels = orig_infilecnt;
  1362. return(FINISHED);
  1363. }
  1364. /************************** CHECK_SPATIALISATION_DATA ************************************/
  1365. int check_spatialisation_data(dataptr dz)
  1366. {
  1367. int imaxspaceh = 0, n;
  1368. double maxspaceh = 0.0;
  1369. double *p;
  1370. if(!dz->zeroset) {
  1371. maxspaceh = -DBL_MAX;
  1372. p = dz->parray[WRAP_CENTRE];
  1373. p++;
  1374. for(n=0;n<dz->ringsize;n++) {
  1375. if(*p > maxspaceh)
  1376. maxspaceh = *p;
  1377. p+=3;
  1378. }
  1379. } else
  1380. maxspaceh = dz->wrapcentre;
  1381. imaxspaceh = (int)ceil(maxspaceh);
  1382. if(imaxspaceh > dz->out_chans) {
  1383. sprintf(errstr,"SPATIALIASATION CENTRE MAX VALUE (%lf) INCOMPATIBLE WITH OUTPUT CHANNEL-CNT (%d)\n",maxspaceh,dz->iparam[WRAP_OUTCHANS]);
  1384. return(DATA_ERROR);
  1385. }
  1386. return(FINISHED);
  1387. }
  1388. /********************************* WRAPPAGE *************************************/
  1389. int wrappage(dataptr dz)
  1390. {
  1391. int exit_status;
  1392. int firsttime = TRUE, thissnd = 0;
  1393. int nctr = 0; /* normalisation vals counter */
  1394. int absicnt_per_chan = 0, absocnt_per_chan = 0;
  1395. float *iptr;
  1396. float *Fptr = dz->fptr[WRAP_LBUF];
  1397. int vals_to_write, total_ssampsread;
  1398. double sr = (double)dz->infile->srate;
  1399. double inverse_sr = 1.0/sr;
  1400. float *maxwrite = dz->fptr[WRAP_LBUF]; /* pointer to last sample created */
  1401. if(sloom)
  1402. dz->total_samps_read = 0;
  1403. dz->itemcnt = 0;
  1404. if((exit_status = read_samps_wrappage(firsttime,dz))<0)
  1405. return(exit_status);
  1406. iptr = dz->sampbuf[THIS_BUF(thissnd)];
  1407. total_ssampsread = dz->ssampsread;
  1408. display_virtual_time(0L,dz);
  1409. do {
  1410. if((exit_status = granulate(&thissnd,&absicnt_per_chan,&absocnt_per_chan,&iptr,&Fptr,
  1411. inverse_sr,&total_ssampsread,&maxwrite,&nctr,dz))<0)
  1412. return(exit_status);
  1413. } while(exit_status==CONTINUE);
  1414. vals_to_write = maxwrite - dz->fptr[WRAP_LBUF];
  1415. while(vals_to_write > dz->iparam[WRAP_LONGS_BUFLEN]) {
  1416. if((exit_status = write_samps_granula(dz->iparam[WRAP_LONGS_BUFLEN],&nctr,dz))<0)
  1417. return(exit_status);
  1418. memmove((char *)dz->fptr[WRAP_LBUF],(char *)dz->fptr[WRAP_LBUFEND],
  1419. dz->iparam[WRAP_LBUF_SMPXS] * sizeof(float));
  1420. memset((char *)dz->fptr[WRAP_LBUFMID],0,dz->iparam[WRAP_LONGS_BUFLEN] * sizeof(float));
  1421. vals_to_write -= dz->iparam[WRAP_LONGS_BUFLEN];
  1422. }
  1423. if(vals_to_write > 0) {
  1424. if((exit_status = write_samps_granula(vals_to_write,&nctr,dz))<0)
  1425. return(exit_status);
  1426. }
  1427. if(dz->total_samps_written <= 0) {
  1428. sprintf(errstr,"SOURCE POSSIBLY TOO SHORT FOR THIS OPTION: Try 'Full Monty'\n");
  1429. return(GOAL_FAILED);
  1430. }
  1431. display_virtual_time(0,dz);
  1432. dz->infile->channels = dz->iparam[WRAP_OUTCHANS]; // setup NOW for headwrite, as headwrite uses dz->infile->channels as its OUTCHAN count
  1433. return renormalise(nctr,dz);
  1434. }
  1435. /***************************** RENORMALISE ****************************
  1436. *
  1437. * (1) Find smallest normalising factor S (creating gretest level reduction).
  1438. * (2) For each normalising factor N, find value which will bring it
  1439. * down to S. That is S/N. Reinsert these values in the normalising
  1440. * factor array.
  1441. * REnormalising with these factors, will ensure whole file is normalised
  1442. * using same factor (S), which is also the smallest factor required.
  1443. * (3) Seek to start of outfile.
  1444. * (4) Set infile pointer to same as outfile pointer, so that read_samps()
  1445. * now reads from OUTFILE.
  1446. * (5) Reset samps_read counter.
  1447. * (6) Size of buffers we read depends on whether we output a stereo
  1448. * file or a mono file.
  1449. * (7) Reset normalisation-factor array pointer (m).
  1450. * While we still have a complete buffer to read..
  1451. * (8) Read samps into output buffer (as, if output is stereo, this
  1452. * may be larger than ibuf, and will accomodate the data).
  1453. * (9) Renormalise all values.
  1454. * (10) Increment pointer to normalisation factors.
  1455. * (11) Seek to start of current buffer, in file.
  1456. * (12) Overwrite data in file.
  1457. * (13) Re-seek to end of current buffer, in file.
  1458. * (14) Calcualte how many samps left over at end, and if any...
  1459. * (16) Read last (short) buffer.
  1460. * (17) Set buffer size to actual number of samps left.
  1461. * (18) Renormalise.
  1462. * (19) Seek to start of ACTUAL buffer in file.
  1463. * (20) Write the real number of samps left in buffer.
  1464. * (21) Check the arithmetic.
  1465. * (22) Restore pointer to original infile, so finish() works correctly.
  1466. */
  1467. int renormalise(int nctr,dataptr dz)
  1468. {
  1469. int exit_status;
  1470. int n, m = 0;
  1471. // int total_samps_read = 0;
  1472. int samp_total = dz->total_samps_written, samps_remaining;
  1473. double min_norm = dz->parray[WRAP_NORMFACT][0]; /* 1 */
  1474. float *s = NULL;
  1475. double nf;
  1476. int cnt;
  1477. //TW NEW MECHANISM writes renormalised vals from original file created (tempfile) into true outfile
  1478. if(sndcloseEx(dz->ifd[0]) < 0) {
  1479. sprintf(errstr, "WARNING: Can't close input soundfile\n");
  1480. return(SYSTEM_ERROR);
  1481. }
  1482. if(sndcloseEx(dz->ofd) < 0) {
  1483. sprintf(errstr, "WARNING: Can't close output soundfile\n");
  1484. return(SYSTEM_ERROR);
  1485. }
  1486. if((dz->ifd[0] = sndopenEx(dz->outfilename,0,CDP_OPEN_RDWR)) < 0) { /*RWD Nov 2003 need RDWR to enable sndunlink to work */
  1487. sprintf(errstr,"Failure to reopen file %s for renormalisation.\n",dz->outfilename);
  1488. return(SYSTEM_ERROR);
  1489. }
  1490. if((sndseekEx(dz->ifd[0],0,0)<0)){
  1491. sprintf(errstr,"sndseek() failed\n");
  1492. return SYSTEM_ERROR;
  1493. }
  1494. if(!sloom){
  1495. // RWD
  1496. //printf("dz->wordstor[0] = %s\n",dz->wordstor[0]);
  1497. //dz->wordstor[0][strlen(dz->wordstor[0]) -9] = ENDOFSTR;
  1498. }
  1499. if((exit_status = create_sized_outfile(dz->wordstor[0],dz))<0) {
  1500. sprintf(errstr,"Failure to create file %s for renormalisation.\n",dz->wordstor[0]);
  1501. return(exit_status);
  1502. }
  1503. s = dz->sampbuf[WRAP_OBUF];
  1504. dz->total_samps_written = 0;
  1505. for(m=1;m<nctr;m++) {
  1506. if((dz->parray[WRAP_NORMFACT])[m] < min_norm)
  1507. min_norm = (dz->parray[WRAP_NORMFACT])[m];
  1508. }
  1509. if(min_norm < 1.0) {
  1510. sprintf(errstr,"Renormalising by %lf\n",min_norm);
  1511. print_outmessage_flush(errstr);
  1512. for(m=0;m<nctr;m++) /* 2 */
  1513. (dz->parray[WRAP_NORMFACT])[m] = min_norm/(dz->parray[WRAP_NORMFACT])[m];
  1514. }
  1515. // if(!sloom) {
  1516. // sprintf(errstr,"\nCopying to output\n");
  1517. // print_outmessage_flush(errstr);
  1518. // }
  1519. //TW new mechanism: lines not needed
  1520. dz->total_samps_read = 0; /* 5 */
  1521. display_virtual_time(0L,dz); /* works on dz->total_samps_read here, so param irrelevant */
  1522. dz->buflen *= dz->outfile->channels; /* 6 */
  1523. m = 0; /* 7 */
  1524. cnt = dz->buflen;
  1525. while(dz->total_samps_read + dz->buflen < samp_total) {
  1526. //if(!sloom) {
  1527. // sprintf(errstr,"\nCopying to output\n");
  1528. // print_outmessage_flush(errstr);
  1529. //}
  1530. if((exit_status = read_samps(s,dz))<0) {
  1531. close_and_delete_tempfile(dz->outfilename,dz);
  1532. return(exit_status);
  1533. } /* 8 */
  1534. // total_samps_read += dz->ssampsread;
  1535. if(min_norm < 1.0) {
  1536. nf = (dz->parray[WRAP_NORMFACT])[m];
  1537. for(n=0;n<cnt;n++) /* 9 */
  1538. s[n] = /*round*/ (float)((double)s[n] * nf);
  1539. m++; /* 10 */
  1540. }
  1541. dz->process = BRASSAGE; /* Forces correct time-bar display */
  1542. if((exit_status = write_samps(s,dz->buflen,dz))<0) {
  1543. close_and_delete_tempfile(dz->outfilename,dz);
  1544. return(exit_status); /* 12 */
  1545. }
  1546. dz->process = WRAPPAGE;
  1547. } /* 14 */
  1548. if((samps_remaining = samp_total - dz->total_samps_read)>0) {
  1549. //last_total_samps_read = dz->total_samps_read;
  1550. if((exit_status = read_samps(s,dz))<0) {
  1551. close_and_delete_tempfile(dz->outfilename,dz);
  1552. return(exit_status); /* 16 */
  1553. }
  1554. dz->buflen = samps_remaining; /* 17 */
  1555. if(min_norm < 1.0) {
  1556. nf = (dz->parray[WRAP_NORMFACT])[m];
  1557. for(n=0;n<dz->buflen;n++) /* 18 */
  1558. s[n] = /*round*/(float) ((double)s[n] * nf);
  1559. }
  1560. if(samps_remaining > 0) {
  1561. dz->process = BRASSAGE;
  1562. if((exit_status = write_samps(s,samps_remaining,dz))<0) {
  1563. close_and_delete_tempfile(dz->outfilename,dz);
  1564. return(exit_status); /* 20 */
  1565. }
  1566. dz->process = WRAPPAGE;
  1567. }
  1568. }
  1569. if(dz->total_samps_written != samp_total) { /* 21 */
  1570. sprintf(errstr,"ccounting problem: Renormalise()\n");
  1571. close_and_delete_tempfile(dz->outfilename,dz);
  1572. return(PROGRAM_ERROR);
  1573. }
  1574. close_and_delete_tempfile(dz->outfilename,dz);
  1575. return(FINISHED);
  1576. }
  1577. /*************************** GRANULATE *********************************
  1578. * iptr = advancing base-pointer in input buffer.
  1579. * iiptr = true read-pointer in input buffer.
  1580. * absicnt_per_chan = advancing-base-counter in input stream, counting TOTAL samps.
  1581. * absiicnt_per_chan = true counter in input stream, counting TOTAL samps to write pos.
  1582. * Fptr = advancing base-pointer in output buffer.
  1583. * FFptr = true write pointer in output buffer.
  1584. * absocnt_per_chan = advancing-base-counter in output stream, measuring OUTsize.
  1585. */
  1586. /* rwd: some major changes here to clear bugs */
  1587. /* tw: some more major changes here to clear implementation errors */
  1588. //TW removed redundant 'sr' parameter
  1589. int granulate(int *thissnd,int *aipc,int *aopc,float **iiiiptr,float **LLLLptr,
  1590. double inv_sr,int *samptotal,float **maxwrite,int *nctr,dataptr dz)
  1591. {
  1592. int exit_status;
  1593. int absicnt_per_chan = *aipc, absocnt_per_chan = *aopc;
  1594. float *iiptr, *iptr = *iiiiptr, *endbufptr = NULL, *startbufptr = NULL, *thisbuf = NULL;
  1595. float *FFptr, *Fptr = *LLLLptr;
  1596. int isauspos, iisauspos;
  1597. float aamp = -1;
  1598. int bspl = 0, espl = 0, lastsnd;
  1599. int firsttime = FALSE;
  1600. int gsize_per_chan, ostep_per_chan, istep_per_chan, absiicnt_per_chan, rang_per_chan;
  1601. int smpscat_per_chan;
  1602. int resetskip = 0;
  1603. double itime = (double)absicnt_per_chan * inv_sr;
  1604. double otime = (double)absocnt_per_chan * inv_sr;
  1605. double transpos = 1.0, position;
  1606. int saved_total_samps_read = 0;
  1607. int chana, chanb;
  1608. if((exit_status = read_timevarying_data(itime,otime,dz))<0)
  1609. return(exit_status);
  1610. gsize_per_chan = set_ivalue(dz->iparray[WRAP_FLAGS][WRAP_GRAINSIZE_FLAG],WRAP_GRAINSIZE,WRAP_HGRAINSIZE,WRAP_GRANGE,dz);
  1611. if(absicnt_per_chan + gsize_per_chan >= dz->iparam[WRAP_ORIG_SMPSIZE])
  1612. return(FINISHED);
  1613. ostep_per_chan = set_outstep(gsize_per_chan,dz);
  1614. if(dz->iparam[WRAP_OUTLEN]>0 && (absocnt_per_chan>=dz->iparam[WRAP_OUTLEN]))
  1615. return(FINISHED); /* IF outfile LENGTH SPECIFIED HAS BEEN MADE: EXIT */
  1616. FFptr = Fptr;
  1617. if(dz->iparray[WRAP_FLAGS][WRAP_SCATTER_FLAG]) { /* If grains scattered, scatter FFptr */
  1618. smpscat_per_chan = do_scatter(ostep_per_chan,dz);
  1619. FFptr += (smpscat_per_chan * dz->iparam[WRAP_OUTCHANS]);
  1620. }
  1621. if(FFptr < dz->fptr[WRAP_LBUF]) {
  1622. sprintf(errstr,"Array overrun 1: granula()\n");
  1623. return(PROGRAM_ERROR); /* FFptr can be outside the Lbuffer because Fptr is outside it */
  1624. }
  1625. if(FFptr >= dz->fptr[WRAP_LTAILEND]) {
  1626. if((exit_status = write_samps_granula(dz->iparam[WRAP_LONGS_BUFLEN],nctr,dz))<0)
  1627. return(exit_status);
  1628. /* IF CURRENT POINTER AT END OF LBUF WRITE SAMPS, WRAP AROUND POINTERS */
  1629. memmove((char *)dz->fptr[WRAP_LBUF],(char *)dz->fptr[WRAP_LBUFEND],
  1630. dz->iparam[WRAP_LBUF_SMPXS] * sizeof(float));
  1631. memset((char *)dz->fptr[WRAP_LBUFMID],0,dz->iparam[WRAP_LONGS_BUFLEN] * sizeof(float));
  1632. FFptr -= dz->iparam[WRAP_LONGS_BUFLEN];
  1633. if((Fptr -= dz->iparam[WRAP_LONGS_BUFLEN]) < dz->fptr[WRAP_LBUF]) {
  1634. sprintf(errstr,"Array overrun 2: granula()\n");
  1635. return(PROGRAM_ERROR);
  1636. }
  1637. *maxwrite -= dz->iparam[WRAP_LONGS_BUFLEN];
  1638. }
  1639. istep_per_chan = set_instep(ostep_per_chan,dz);
  1640. if(istep_per_chan==0 && dz->iparam[WRAP_OUTLEN]==0) {
  1641. sprintf(errstr,"velocity or instep has become so small that file will be infinitely long!!\n"
  1642. "Try slightly increasing your very small values.\n");
  1643. return(GOAL_FAILED);
  1644. }
  1645. iiptr = iptr;
  1646. lastsnd = *thissnd;
  1647. isauspos = iptr - dz->sampbuf[THIS_BUF(lastsnd)];
  1648. iisauspos = iiptr - dz->sampbuf[THIS_BUF(lastsnd)];
  1649. *thissnd = get_next_insnd(dz);
  1650. iiptr = dz->sampbuf[THIS_BUF(*thissnd)] + iisauspos;
  1651. iptr = dz->sampbuf[THIS_BUF(*thissnd)] + isauspos;
  1652. absiicnt_per_chan = absicnt_per_chan;
  1653. if(dz->iparray[WRAP_FLAGS][WRAP_RANGE_FLAG]) {
  1654. rang_per_chan = set_range(absiicnt_per_chan,dz); /* RESET iiptr etc WITHIN SEARCHRANGE */
  1655. absiicnt_per_chan -= rang_per_chan;
  1656. iiptr -= rang_per_chan * dz->iparam[WRAP_INCHANS];
  1657. }
  1658. endbufptr = dz->sbufptr[THIS_BUF(*thissnd)];
  1659. startbufptr = dz->sampbuf[THIS_BUF(*thissnd)];
  1660. if(iiptr >= endbufptr) {
  1661. while(iiptr >= endbufptr) {
  1662. if((read_samps_wrappage(firsttime,dz))<0) /* IF iiptr OFF END OF IBUF */
  1663. return(exit_status);
  1664. *samptotal += dz->ssampsread;
  1665. if(dz->ssampsread<=0)
  1666. return(FINISHED);
  1667. /* READ SAMPS, WRAP BACK POINTER */
  1668. iiptr -= dz->buflen;
  1669. iptr -= dz->buflen;
  1670. }
  1671. } else if(iiptr < startbufptr) { /* IF RANGE TAKES US BAK OUT OF THIS BUF, */
  1672. if(sloom)
  1673. saved_total_samps_read = dz->total_samps_read; /* saved so display_virtual_time() works during baktrak!! */
  1674. if((resetskip = *samptotal - dz->iparam[WRAP_SAMPS_IN_INBUF])<0) { /* SET RESETSKIP TO START OF CURRENT BUFFER */
  1675. sprintf(errstr,"Error in baktraking: granula()\n");
  1676. return(PROGRAM_ERROR);
  1677. }
  1678. if((exit_status = baktrak_wrappage(*thissnd,*samptotal,absiicnt_per_chan,&iiptr,dz))<0)
  1679. return(exit_status); /* SEEK BACKWDS, & RESET iiptr In NEW BUF */
  1680. if(sloom)
  1681. dz->total_samps_read = saved_total_samps_read; /* restore so its not changed by the baktraking!! */
  1682. }
  1683. if(dz->iparray[WRAP_FLAGS][WRAP_AMP_FLAG])
  1684. /* RWD was set_ivalue*/
  1685. aamp = (float) set_dvalue(dz->iparray[WRAP_FLAGS][WRAP_AMP_FLAG],WRAP_AMP,WRAP_HAMP,WRAP_ARANGE,dz);
  1686. /* GET SPLICE VALUES */
  1687. bspl = set_ivalue(dz->iparray[WRAP_FLAGS][WRAP_BSPLICE_FLAG],WRAP_BSPLICE,WRAP_HBSPLICE,WRAP_BRANGE,dz);
  1688. espl = set_ivalue(dz->iparray[WRAP_FLAGS][WRAP_ESPLICE_FLAG],WRAP_ESPLICE,WRAP_HESPLICE,WRAP_ERANGE,dz);
  1689. thisbuf = dz->sampbuf[THIS_BUF(*thissnd)];
  1690. if(!make_multichan_grain(thisbuf,&iiptr,aamp,gsize_per_chan,&transpos,dz))
  1691. return(FINISHED); /* COPYGRAIN TO GRAINBUF,INCLUDING ANY PSHIFT */
  1692. do_multichan_splices(gsize_per_chan,bspl,espl,dz); /* DO SPLICES IN GRAINBUF */
  1693. position = get_wrappage_position(dz);
  1694. chana = (int)floor(position);
  1695. position -= (double)chana; // position is relative position between 2 adjacent out-chans
  1696. chanb = chana + 1; // chanb is adjacent to chana
  1697. if(chana > dz->out_chans) // chana beyond last lspkr wraps around to 1st lspkr
  1698. chana = 1;
  1699. else if(chana < 1) // chana below 1st loudspeaker wraps around to last-lspkr
  1700. chana = dz->out_chans;
  1701. if((exit_status = write_multichan_grain(position,chana,chanb,maxwrite,&Fptr,&FFptr,gsize_per_chan,nctr,dz))<0)
  1702. return(exit_status);
  1703. if(sloom)
  1704. saved_total_samps_read = dz->total_samps_read; /* saved so not disturbed by restoring of baktrakd-from buffer */
  1705. if(resetskip && (exit_status = reset_wrappage(*thissnd,resetskip,dz))<0)
  1706. return(exit_status);
  1707. if(sloom)
  1708. dz->total_samps_read = saved_total_samps_read;
  1709. iptr += (istep_per_chan * dz->iparam[WRAP_INCHANS]); /* MOVE FORWARD IN postmixdown input-buffer, which is either MONO or multichannel */
  1710. absicnt_per_chan += istep_per_chan; /* rwd: moved here from top of input section */
  1711. Fptr += ostep_per_chan * dz->iparam[WRAP_OUTCHANS]; /* Move forward in output buffer, which can be mono, spatialised-stereo or multichannel */
  1712. /* so we don't lose first grain */
  1713. absocnt_per_chan += ostep_per_chan;
  1714. *aipc = absicnt_per_chan; /* RETURN VALUES OF RETAINED VARIABLES */
  1715. *aopc = absocnt_per_chan;
  1716. *iiiiptr = iptr;
  1717. *LLLLptr = Fptr;
  1718. return(CONTINUE);
  1719. }
  1720. /***************************** MAKE_MULTICHAN_GRAIN ***************************
  1721. * iicnt = RELATIVE read-pointer in input buffer (starts at 0).
  1722. */
  1723. int make_multichan_grain(float *b,float **iiptr,float aamp,int gsize_per_chan,double *transpos,dataptr dz)
  1724. {
  1725. int n, k;
  1726. int chans = dz->iparam[WRAP_INCHANS]; // WRAP_INCHANS = no of chans in input grain, i.e. mono if its been mixed down to mono
  1727. double flcnt; // but = dz->infile->channels, if not
  1728. int iicnt, real_gsize = gsize_per_chan * chans;
  1729. double flcnt_frac;
  1730. float *s = *iiptr, *gbufptr = dz->extrabuf[WRAP_GBUF];
  1731. if(aamp>=0) {
  1732. if(!dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG]) {
  1733. if(real_gsize >= dz->iparam[WRAP_SAMPS_IN_INBUF]) /* JUNE 1996 */
  1734. return(0);
  1735. for(n=0;n<real_gsize;n++) /* COPY GRAIN TO GRAINBUF & RE-LEVEL ETC */
  1736. *gbufptr++ = (*s++ * aamp);
  1737. } else {
  1738. iicnt = (s - b)/chans;
  1739. *transpos = set_dvalue(dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG],WRAP_PITCH,WRAP_HPITCH,WRAP_PRANGE,dz);
  1740. *transpos = pow(2.0,*transpos);
  1741. if((((int)(gsize_per_chan * *transpos)+1) + iicnt) * chans >= dz->iparam[WRAP_SAMPS_IN_INBUF]) /* JUNE 1996 */
  1742. return(0);
  1743. flcnt = (double)iicnt;
  1744. flcnt_frac = flcnt - (double)iicnt;
  1745. for(n=0;n<gsize_per_chan;n++) {
  1746. for(k = 0; k < chans;k++) {
  1747. *gbufptr++ = interp_gval_with_amp(s,flcnt_frac,dz->iparam[WRAP_INCHANS],aamp);
  1748. s++;
  1749. }
  1750. flcnt += *transpos;
  1751. iicnt = (int)flcnt;
  1752. s = b + (iicnt * chans);
  1753. flcnt_frac = flcnt - (double) iicnt;
  1754. }
  1755. }
  1756. } else { /* NO CHANGE IN AMPLITUDE */
  1757. if(!dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG]) {
  1758. if(real_gsize >= dz->iparam[WRAP_SAMPS_IN_INBUF]) /* JUNE 1996 */
  1759. return(0);
  1760. for(n=0;n<real_gsize;n++)
  1761. *gbufptr++ = *s++;
  1762. } else {
  1763. iicnt = (s - b)/chans;
  1764. *transpos = set_dvalue(dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG],WRAP_PITCH,WRAP_HPITCH,WRAP_PRANGE,dz);
  1765. *transpos = pow(2.0,*transpos);
  1766. if((((int)(gsize_per_chan * *transpos)+1) + iicnt) * chans >= dz->iparam[WRAP_SAMPS_IN_INBUF]) /* JUNE 1996 */
  1767. return(0);
  1768. flcnt = (double)iicnt;
  1769. flcnt_frac = flcnt - (double)iicnt;
  1770. for(n=0;n<gsize_per_chan;n++) {
  1771. for(k=0;k < chans;k++) {
  1772. *gbufptr++ = interp_gval(s,flcnt_frac,dz->iparam[WRAP_INCHANS]);
  1773. s++;
  1774. }
  1775. flcnt += *transpos;
  1776. iicnt = (int)flcnt;
  1777. s = b + (iicnt * chans);
  1778. flcnt_frac = flcnt - (double)iicnt;
  1779. }
  1780. }
  1781. }
  1782. *iiptr = s;
  1783. return(1);
  1784. }
  1785. /****************************** DEHOLE ************************/
  1786. #define NONLIN 0.5
  1787. #define DEVIATE 0.25
  1788. double dehole(double pos)
  1789. {
  1790. double comp = 1.0 - (fabs((pos * 2.0) - 1.0)); /* 1 */
  1791. comp = pow(comp,NONLIN); /* 2 */
  1792. comp *= DEVIATE; /* 3 */
  1793. comp += (1.0 - DEVIATE); /* 4 */
  1794. return(comp);
  1795. }
  1796. /**************************** DO_SCATTER *****************************
  1797. * scatter forward by fraction of randpart of 1 event separation.
  1798. *
  1799. * Possible scatter | |-------------->|
  1800. * Range selected | |------> |
  1801. * Random choice within range | |---> |
  1802. */
  1803. int do_scatter(int ostep_per_chan,dataptr dz)
  1804. {
  1805. double scat = dz->param[WRAP_SCATTER] * drand48();
  1806. int smpscat_per_chan = round((double)ostep_per_chan * scat);
  1807. return(smpscat_per_chan);
  1808. }
  1809. /******************************** WRITE_SAMPS_GRANULA ****************************
  1810. *
  1811. * Normalise each buffer before writing to obuf, then to file.
  1812. * Save the normalisation factor in array normfact[].
  1813. */
  1814. int write_samps_granula(int k,int *nctr,dataptr dz)
  1815. {
  1816. int exit_status;
  1817. /*int*/double val = 0.0 /*0L, max_long = 0L*/;
  1818. int n;
  1819. double thisnorm = 1.0, max_double = 0.0;
  1820. float *s = NULL;
  1821. float *l = dz->fptr[WRAP_LBUF];
  1822. s = dz->sampbuf[WRAP_OBUF];
  1823. for(n=0;n<k;n++) {
  1824. if((val = fabs(l[n])) > max_double)
  1825. max_double = val;
  1826. }
  1827. if(/*max_long*/max_double > F_MAXSAMP) {
  1828. thisnorm = (double)F_MAXSAMP/max_double;
  1829. for(n=0;n<k;n++)
  1830. s[n] = (float) ((double)(l[n]) * thisnorm);
  1831. } else {
  1832. for(n=0;n<k;n++)
  1833. s[n] = l[n];
  1834. }
  1835. dz->process = BRASSAGE; /* Forces correct time-bar display */
  1836. if((exit_status = write_samps(s,k,dz))<0)
  1837. return(exit_status);
  1838. dz->process = WRAPPAGE;
  1839. (dz->parray[WRAP_NORMFACT])[(*nctr)++] = thisnorm;
  1840. if(*nctr >= dz->iparam[WRAP_ARRAYSIZE]) {
  1841. dz->iparam[WRAP_ARRAYSIZE] += BIGARRAY;
  1842. if((dz->parray[WRAP_NORMFACT] =
  1843. (double *)realloc((char *)(dz->parray[WRAP_NORMFACT]),dz->iparam[WRAP_ARRAYSIZE]*sizeof(double)))==NULL) {
  1844. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate normalisation array.\n");
  1845. return(MEMORY_ERROR);
  1846. }
  1847. }
  1848. return(FINISHED);
  1849. }
  1850. /************************** DO_MULTICHAN_SPLICES *****************************/
  1851. void do_multichan_splices(int gsize_per_chan,int bspl,int espl,dataptr dz)
  1852. {
  1853. if(dz->iparam[WRAP_IS_BTAB] && (dz->iparam[WRAP_BSPLICE] < gsize_per_chan/2))
  1854. do_multichan_btab_splice(dz);
  1855. else
  1856. do_multichan_bsplice(gsize_per_chan,dz,bspl);
  1857. if(dz->iparam[WRAP_IS_ETAB] && (dz->iparam[WRAP_ESPLICE] < gsize_per_chan/2))
  1858. do_multichan_etab_splice(gsize_per_chan,dz);
  1859. else
  1860. do_multichan_esplice(gsize_per_chan,dz,espl);
  1861. }
  1862. /************************** DO_MULTICHAN_BTAB_SPLICE ****************************/
  1863. void do_multichan_btab_splice(dataptr dz)
  1864. {
  1865. int n, j, k = dz->iparam[WRAP_BSPLICE];
  1866. double *d;
  1867. float *gbufptr = dz->extrabuf[WRAP_GBUF];
  1868. if(k==0)
  1869. return;
  1870. d = dz->parray[WRAP_BSPLICETAB];
  1871. for(n=0;n<k;n++) {
  1872. for(j=0;j<dz->iparam[WRAP_INCHANS];j++) {
  1873. *gbufptr = (float)/*round*/(*gbufptr * *d);
  1874. gbufptr++;
  1875. }
  1876. d++;
  1877. }
  1878. }
  1879. /************************** DO_MULTICHAN_BSPLICE ****************************
  1880. *
  1881. * rwd: changed to avoid f/p division, added exponential option
  1882. */
  1883. void do_multichan_bsplice(int gsize_per_chan,dataptr dz,int bspl)
  1884. {
  1885. double dif,val,lastval,length,newsum,lastsum,twodif;
  1886. int n, j, k = min(bspl,gsize_per_chan);
  1887. float *gbufptr = dz->extrabuf[WRAP_GBUF];
  1888. int chans = dz->iparam[WRAP_INCHANS];
  1889. if(k==0)
  1890. return;
  1891. val = 0.0;
  1892. length = (double)k;
  1893. if(!dz->vflag[WRAP_EXPON]){
  1894. dif = 1.0/length;
  1895. lastval = dif;
  1896. for(j= 0; j<chans;j++)
  1897. *gbufptr++ = (float)val;
  1898. for(j= 0; j<chans;j++) {
  1899. *gbufptr = (float)/*round*/(*gbufptr * lastval);
  1900. gbufptr++;
  1901. }
  1902. for(n=2;n<k;n++) {
  1903. val = lastval + dif;
  1904. lastval = val;
  1905. for(j= 0; j<chans;j++) {
  1906. *gbufptr = (float) /*round*/(*gbufptr * val);
  1907. gbufptr++;
  1908. }
  1909. }
  1910. } else { /* fast quasi-exponential */
  1911. dif = 1.0/(length*length);
  1912. twodif = dif * 2.0;
  1913. lastsum = 0.0;
  1914. lastval = dif;
  1915. for(j=0;j<chans;j++)
  1916. *gbufptr++ = (float)val;/* mca - round or truncate? */
  1917. for(j=0;j<chans;j++) {
  1918. *gbufptr = (float) /*round*/(*gbufptr * lastval); /*** fixed MAY 1998 ***/
  1919. gbufptr++;
  1920. }
  1921. for(n=2;n<k;n++) {
  1922. newsum = lastsum + twodif;
  1923. val = lastval + newsum + dif;
  1924. for(j=0;j<chans;j++) {
  1925. *gbufptr = (float) /*round*/(*gbufptr * val);
  1926. gbufptr++;
  1927. }
  1928. lastval = val;
  1929. lastsum = newsum;
  1930. }
  1931. }
  1932. }
  1933. /************************** DO_MULTICHAN_ETAB_SPLICE ****************************/
  1934. void do_multichan_etab_splice(int gsize_per_chan,dataptr dz)
  1935. {
  1936. int n, j, k = dz->iparam[WRAP_ESPLICE];
  1937. double *d;
  1938. int chans = dz->iparam[WRAP_INCHANS];
  1939. float *gbufptr = dz->extrabuf[WRAP_GBUF] + ((gsize_per_chan - k) * chans);
  1940. if(k==0)
  1941. return;
  1942. d = dz->parray[WRAP_ESPLICETAB];
  1943. for(n=0;n<k;n++) {
  1944. for(j=0;j<chans;j++) {
  1945. *gbufptr = (float) /*round*/(*gbufptr * *d);
  1946. gbufptr++;
  1947. }
  1948. d++;
  1949. }
  1950. }
  1951. /************************** DO_MULTICHAN_ESPLICE ****************************/
  1952. /* rwd: changed to avoid f/p division, added exponential option */
  1953. void do_multichan_esplice(int gsize_per_chan,dataptr dz,int espl)
  1954. {
  1955. double dif,val,lastval,length,newsum,lastsum,twodif;
  1956. int n, j, k = min(espl,gsize_per_chan);
  1957. int chans = dz->iparam[WRAP_INCHANS];
  1958. int real_gsize = gsize_per_chan * chans;
  1959. float *gbufptr = dz->extrabuf[WRAP_GBUF] + real_gsize;
  1960. if(k==0)
  1961. return;
  1962. val = 0.0;
  1963. length = (double) k;
  1964. if(!dz->vflag[WRAP_EXPON]) {
  1965. dif = 1.0/length;
  1966. lastsum = dif;
  1967. for(j=0;j<chans;j++) {
  1968. gbufptr--;
  1969. *gbufptr = (float)val;
  1970. }
  1971. for(j=0;j<chans;j++) {
  1972. gbufptr--;
  1973. *gbufptr = (float) /*round*/(*gbufptr * lastsum);
  1974. }
  1975. for(n=k-3;n>=0;n--) {
  1976. val = lastsum + dif;
  1977. lastsum = val;
  1978. for(j=0;j<chans;j++) {
  1979. gbufptr--;
  1980. *gbufptr = (float) /*round*/(*gbufptr * val);
  1981. }
  1982. }
  1983. } else { /* fast quasi-exponential */
  1984. dif = 1.0/(length * length);
  1985. twodif = dif * 2.0;
  1986. lastsum = 0.0;
  1987. lastval = dif;
  1988. for(j=0;j<chans;j++) {
  1989. gbufptr--;
  1990. *gbufptr = (float)val;
  1991. }
  1992. for(j=0;j<chans;j++) {
  1993. gbufptr--;
  1994. *gbufptr = (float) /*round*/(*gbufptr * lastval);
  1995. }
  1996. for(n=k-3;n>=0;n--) {
  1997. newsum =lastsum + twodif;
  1998. val = lastval + newsum + dif;
  1999. for(j=0;j<chans;j++) {
  2000. gbufptr--;
  2001. *gbufptr = (float)/*round*/(*gbufptr * val);
  2002. }
  2003. lastval = val;
  2004. lastsum = newsum;
  2005. }
  2006. }
  2007. }
  2008. /************************** INTERP_GVAL_WITH_AMP ****************************/
  2009. float interp_gval_with_amp(float *s,double flcnt_frac,int chans,float ampp)
  2010. {
  2011. /*long*/float tthis = /*(long)*/ *s * ampp;
  2012. /*long*/float next = /*(long)*/ *(s+chans) * ampp;
  2013. //long val = this + round((double)(next-this) * flcnt_frac);
  2014. //return(short)((val+TWO_POW_14) >> 15);
  2015. return (float) (tthis + ((next-tthis) * flcnt_frac));
  2016. }
  2017. /************************** INTERP_GVAL ****************************/
  2018. float interp_gval(float *s,double flcnt_frac,int chans)
  2019. {
  2020. float tthis = *s;
  2021. float next = *(s+chans);
  2022. float val = (float)(tthis + /*round*/((double)(next-tthis) * flcnt_frac));
  2023. return(val);
  2024. }
  2025. /*************************** SET_RANGE ******************************/
  2026. int set_range(int absiicnt,dataptr dz)
  2027. {
  2028. int val;
  2029. val = min(dz->iparam[WRAP_SRCHRANGE],absiicnt);
  2030. val = round((double)val * drand48());
  2031. return(val);
  2032. }
  2033. /*************************** SET_OUTSTEP ******************************/
  2034. int set_outstep(int gsize_per_chan,dataptr dz)
  2035. {
  2036. double dens;
  2037. int val = 0;
  2038. /* TW 4 :2002 */
  2039. dens = set_dvalue(dz->iparray[WRAP_FLAGS][WRAP_DENSITY_FLAG],WRAP_DENSITY,WRAP_HDENSITY,WRAP_DRANGE,dz);
  2040. val = round((double)gsize_per_chan/(double)dens);
  2041. return(val);
  2042. }
  2043. /*************************** SET_INSTEP ******************************/
  2044. int set_instep(int ostep_per_chan,dataptr dz)
  2045. { /* rwd: added range error traps */
  2046. double velocity;
  2047. int istep_per_chan = 0;
  2048. velocity = set_dvalue(dz->iparray[WRAP_FLAGS][WRAP_VELOCITY_FLAG],WRAP_VELOCITY,WRAP_HVELOCITY,WRAP_VRANGE,dz);
  2049. istep_per_chan = round(velocity * (double)ostep_per_chan);
  2050. return(istep_per_chan);
  2051. }
  2052. /*************************** SET_IVALUE ******************************/
  2053. int set_ivalue(int flag,int paramno,int hparamno,int rangeno,dataptr dz)
  2054. {
  2055. int val = 0;
  2056. switch(flag) {
  2057. case(NOT_SET):
  2058. case(FIXED):
  2059. case(VARIABLE):
  2060. val = dz->iparam[paramno];
  2061. break;
  2062. case(RANGE_VLO):
  2063. case(RANGE_VHI):
  2064. case(RANGE_VHILO):
  2065. dz->iparam[rangeno] = dz->iparam[hparamno] - dz->iparam[paramno];
  2066. /* fall thro */
  2067. case(RANGED):
  2068. val = round((drand48() * dz->iparam[rangeno]) + (double)dz->iparam[paramno]);
  2069. break;
  2070. }
  2071. return(val);
  2072. }
  2073. /*************************** SET_DVALUE ******************************/
  2074. double set_dvalue(int flag,int paramno,int hparamno,int rangeno,dataptr dz)
  2075. {
  2076. double val = 0;
  2077. switch(flag) {
  2078. case(NOT_SET):
  2079. case(FIXED):
  2080. case(VARIABLE):
  2081. val = dz->param[paramno];
  2082. break;
  2083. case(RANGE_VLO):
  2084. case(RANGE_VHI):
  2085. case(RANGE_VHILO):
  2086. dz->param[rangeno] = dz->param[hparamno] - dz->param[paramno];
  2087. /* fall thro */
  2088. case(RANGED):
  2089. val = (drand48() * dz->param[rangeno]) + dz->param[paramno];
  2090. break;
  2091. }
  2092. return(val);
  2093. }
  2094. /******************************* READ_SAMPS_SAUSAGE *******************************/
  2095. int read_samps_wrappage(int firsttime,dataptr dz)
  2096. {
  2097. int exit_status, n;
  2098. int samps_read = INT_MAX; /* i.e. larger than possible */
  2099. for(n=0;n<dz->infilecnt;n++) {
  2100. if(firsttime) {
  2101. dz->total_samps_read = 0;
  2102. if((exit_status = read_a_specific_large_buf(n,dz))<0)
  2103. return(exit_status);
  2104. samps_read = min(samps_read,dz->ssampsread);
  2105. } else {
  2106. if((exit_status = read_a_specific_normal_buf_with_wraparound(n,dz))<0)
  2107. return(exit_status);
  2108. samps_read = min(samps_read,dz->ssampsread);
  2109. }
  2110. }
  2111. dz->total_samps_read += samps_read;
  2112. if(samps_read==INT_MAX) {
  2113. sprintf(errstr,"Problem reading sound buffers.\n");
  2114. return(PROGRAM_ERROR);
  2115. }
  2116. if(firsttime)
  2117. dz->iparam[WRAP_SAMPS_IN_INBUF] = dz->ssampsread;
  2118. else
  2119. dz->iparam[WRAP_SAMPS_IN_INBUF] = dz->ssampsread + dz->iparam[WRAP_BUF_SMPXS];
  2120. return(FINISHED);
  2121. }
  2122. /*************************** READ_A_SPECIFIC_LARGE_BUF ******************************/
  2123. int read_a_specific_large_buf(int j,dataptr dz)
  2124. {
  2125. int bigbufsize = dz->buflen; /* RWD odd one, this... */
  2126. int n, m, samps_read, ibufcnt;
  2127. int bufno = THIS_BUF(j), chans = dz->infile->channels;
  2128. double sum;
  2129. bigbufsize += dz->iparam[WRAP_BUF_SMPXS];
  2130. if(chans > 1) {
  2131. bigbufsize *= chans;
  2132. if((samps_read = fgetfbufEx(dz->sampbuf[WRAP_SBUF],bigbufsize,dz->ifd[j],0))<0) {
  2133. sprintf(errstr,"Failed to read samps from file %d.\n",j+1);
  2134. return(SYSTEM_ERROR);
  2135. }
  2136. dz->ssampsread = samps_read;
  2137. bigbufsize /= chans; /* RWD still in samps...buflen + smpxs */
  2138. dz->ssampsread /= chans;
  2139. ibufcnt = 0;
  2140. for(n=0;n<dz->ssampsread;n++) {
  2141. sum = 0.0;
  2142. for(m=0;m<chans;m++)
  2143. sum += dz->sampbuf[WRAP_SBUF][ibufcnt++];
  2144. dz->sampbuf[bufno][n] = (float)(sum/(double)chans);
  2145. }
  2146. } else {
  2147. if((samps_read = fgetfbufEx(dz->sampbuf[bufno],bigbufsize,dz->ifd[j],0))<0) {
  2148. sprintf(errstr,"Failed to read samps from file %d.\n",j+1);
  2149. return(SYSTEM_ERROR);
  2150. }
  2151. dz->ssampsread = samps_read;
  2152. }
  2153. return(FINISHED);
  2154. }
  2155. /*************************** READ_A_SPECIFIC_NORMAL_BUF_WITH_WRAPAROUND ******************************/
  2156. int read_a_specific_normal_buf_with_wraparound(int j,dataptr dz)
  2157. {
  2158. int bigbufsize = dz->buflen;
  2159. int n,m,samps_read, ibufcnt;
  2160. double sum;
  2161. int bufno = THIS_BUF(j);
  2162. int ibufno = THIS_IBUF(j), chans = dz->infile->channels;
  2163. memmove((char *)dz->sampbuf[bufno],(char *)(dz->sbufptr[bufno]),dz->iparam[WRAP_BUF_SMPXS] * sizeof(float));
  2164. if(chans > 1) {
  2165. bigbufsize *= chans;
  2166. if((samps_read = fgetfbufEx(dz->sampbuf[WRAP_SBUF],bigbufsize,dz->ifd[j],0))<0) {
  2167. sprintf(errstr,"Failed to read from file %d.\n",j+1);
  2168. return(SYSTEM_ERROR);
  2169. }
  2170. dz->ssampsread = samps_read;
  2171. bigbufsize /= chans;
  2172. dz->ssampsread /= chans;
  2173. ibufcnt = 0;
  2174. for(n=0;n<dz->ssampsread;n++) {
  2175. sum = 0.0;
  2176. for(m=0;m<chans;m++)
  2177. sum += dz->sampbuf[WRAP_SBUF][ibufcnt++];
  2178. dz->sampbuf[ibufno][n] = (float)(sum/(double)chans);
  2179. }
  2180. } else {
  2181. if((samps_read = fgetfbufEx(dz->sampbuf[ibufno],bigbufsize,dz->ifd[j],0))<0) {
  2182. sprintf(errstr,"Failed to read from file %d.\n",j+1);
  2183. return(SYSTEM_ERROR);
  2184. }
  2185. dz->ssampsread = samps_read;
  2186. }
  2187. return(FINISHED);
  2188. }
  2189. /*************************** BAKTRAK_SAUSAGE ******************************
  2190. *
  2191. * <------------ baktrak(b) ---------------->
  2192. *
  2193. * <--(b-x)-->
  2194. * <------------ x -------------->
  2195. * |-------- current bufer --------|
  2196. *
  2197. * |-------- new buffer --------|
  2198. * <------------x------------->
  2199. *
  2200. */
  2201. int baktrak_wrappage(int thissnd,int samptotal,int absiicnt_per_chan,float **iiptr,dataptr dz)
  2202. {
  2203. int exit_status, chans = dz->infile->channels;
  2204. int bktrk, new_position;
  2205. unsigned int reset_size = dz->buflen + dz->iparam[WRAP_BUF_SMPXS];
  2206. bktrk = samptotal - (absiicnt_per_chan * dz->iparam[WRAP_INCHANS]);
  2207. *iiptr = dz->sampbuf[THIS_BUF(thissnd)];
  2208. if((new_position = samptotal - bktrk)<0) {
  2209. fprintf(stdout,"WARNING: Non-fatal program error:\nRange arithmetic problem - 2, in baktraking.\n");
  2210. fflush(stdout);
  2211. new_position = 0;
  2212. *iiptr = dz->sampbuf[THIS_BUF(thissnd)];
  2213. }
  2214. if(chans > 1) /* IF we've converted from MULTICHAN file */
  2215. new_position *= chans;
  2216. if(sndseekEx(dz->ifd[thissnd],new_position,0)<0) {
  2217. sprintf(errstr,"sndseek error: baktrak_wrappage()\n");
  2218. return(SYSTEM_ERROR);
  2219. }
  2220. memset((char *)dz->sampbuf[THIS_BUF(thissnd)],0,reset_size * sizeof(float));
  2221. if(chans > 1) {
  2222. reset_size *= chans;
  2223. memset((char *)dz->sampbuf[WRAP_SBUF],0,reset_size * sizeof(float));
  2224. }
  2225. if((exit_status = read_a_specific_large_buf(thissnd,dz))<0)
  2226. return(exit_status);
  2227. dz->iparam[WRAP_SAMPS_IN_INBUF] = dz->ssampsread;
  2228. return(FINISHED);
  2229. }
  2230. /****************************** RESET_SAUSAGE *******************************/
  2231. int reset_wrappage(int thissnd,int resetskip,dataptr dz)
  2232. {
  2233. int exit_status, chans = dz->infile->channels;
  2234. unsigned int reset_size = dz->buflen + dz->iparam[WRAP_BUF_SMPXS];
  2235. memset((char *)dz->sampbuf[THIS_BUF(thissnd)],0,reset_size * sizeof(float));
  2236. if(chans > 1) {
  2237. reset_size *= chans;
  2238. memset((char *)dz->sampbuf[WRAP_SBUF],0,reset_size * sizeof(float));
  2239. resetskip *= chans;
  2240. }
  2241. if(sndseekEx(dz->ifd[thissnd],resetskip,0)<0) {
  2242. sprintf(errstr,"sndseek error: reset_wrappage()\n");
  2243. return(SYSTEM_ERROR);
  2244. }
  2245. if((exit_status = read_a_specific_large_buf(thissnd,dz))<0)
  2246. return(exit_status);
  2247. dz->iparam[WRAP_SAMPS_IN_INBUF] = dz->ssampsread;
  2248. return(FINISHED);
  2249. }
  2250. /****************************** GET_NEXT_INSND *******************************/
  2251. int get_next_insnd(dataptr dz)
  2252. {
  2253. if(dz->itemcnt <= 0) {
  2254. perm_wrappage(dz->infilecnt,dz);
  2255. dz->itemcnt = dz->infilecnt;
  2256. }
  2257. dz->itemcnt--;
  2258. return(dz->iparray[WRAP_PERM][dz->itemcnt]);
  2259. }
  2260. /****************************** PERM_SAUSAGE *******************************/
  2261. void perm_wrappage(int cnt,dataptr dz)
  2262. {
  2263. int n, t;
  2264. for(n=0;n<cnt;n++) {
  2265. t = (int)(drand48() * (double)(n+1)); /* TRUNCATE */
  2266. if(t==n)
  2267. prefix(n,cnt-1,dz);
  2268. else
  2269. insert(n,t,cnt-1,dz);
  2270. }
  2271. }
  2272. /****************************** INSERT ****************************/
  2273. void insert(int n,int t,int cnt_less_one,dataptr dz)
  2274. {
  2275. shuflup(t+1,cnt_less_one,dz);
  2276. dz->iparray[WRAP_PERM][t+1] = n;
  2277. }
  2278. /****************************** PREFIX ****************************/
  2279. void prefix(int n,int cnt_less_one,dataptr dz)
  2280. {
  2281. shuflup(0,cnt_less_one,dz);
  2282. dz->iparray[WRAP_PERM][0] = n;
  2283. }
  2284. /****************************** SHUFLUP ****************************/
  2285. void shuflup(int k,int cnt_less_one,dataptr dz)
  2286. {
  2287. int n;
  2288. for(n = cnt_less_one; n > k; n--)
  2289. dz->iparray[WRAP_PERM][n] = dz->iparray[WRAP_PERM][n-1];
  2290. }
  2291. /*************************** GRAB_AN_APPROPRIATE_BLOCK_OF_WRAPPAGE_MEMORY **************************/
  2292. int grab_an_appropriate_block_of_wrappage_memory(int *this_bloksize,int standard_block,int bufdivisor,dataptr dz)
  2293. {
  2294. do {
  2295. if((dz->buflen = *this_bloksize) * sizeof(float) < 0) { /* arithmetic overflow */
  2296. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n");
  2297. return(MEMORY_ERROR);
  2298. }
  2299. *this_bloksize += standard_block;
  2300. dz->buflen -= (dz->infilecnt * dz->iparam[WRAP_BUF_SMPXS]) + dz->iparam[WRAP_LBUF_SMPXS];
  2301. /* Allow for overflow areas */
  2302. if(dz->iparam[WRAP_CHANNELS])
  2303. dz->buflen -= dz->infile->channels * dz->iparam[WRAP_BUF_SMPXS]; /* Allow for overflow space in additional multichan inbuf */
  2304. dz->buflen /= bufdivisor; /* get unit buffersize */
  2305. if(dz->iparam[WRAP_CHANNELS]) /* If reading multichano: chans * SECSIZE reduces to single mono SECSIZE */
  2306. dz->buflen = (dz->buflen / dz->infile->channels) * dz->infile->channels;
  2307. } while(dz->buflen <= 0);
  2308. if(dz->iparam[WRAP_BUFXX] > 1) {
  2309. if((*this_bloksize *= dz->iparam[WRAP_BUFXX]) < 0) {
  2310. sprintf(errstr,"Cannot create a buffer this large.\n");
  2311. return(DATA_ERROR);
  2312. }
  2313. if((dz->buflen = *this_bloksize) * sizeof(float) < 0) { /* arithmetic overflow */
  2314. sprintf(errstr,"Cannot create a buffer this large.\n");
  2315. return(MEMORY_ERROR);
  2316. }
  2317. *this_bloksize += standard_block;
  2318. dz->buflen -= (dz->infilecnt * dz->iparam[WRAP_BUF_SMPXS]) + dz->iparam[WRAP_LBUF_SMPXS];
  2319. /* Allow for overflow areas */
  2320. if(dz->iparam[WRAP_CHANNELS])
  2321. dz->buflen -= dz->infile->channels * dz->iparam[WRAP_BUF_SMPXS]; /* Allow for overflow space in additional multichan inbuf */
  2322. dz->buflen /= bufdivisor; /* get unit buffersize */
  2323. if(dz->iparam[WRAP_CHANNELS]) /* If reading multichano: chans * SECSIZE reduces to single mono SECSIZE */
  2324. dz->buflen = (dz->buflen / dz->infile->channels) * dz->infile->channels;
  2325. }
  2326. return(FINISHED);
  2327. }
  2328. /************************* WRITE_MULTICHAN_GRAIN ****************************/
  2329. int write_multichan_grain(double rpos,int chana,int chanb,float **maxwrite,float **Fptr,float **FFptr,int gsize_per_chan,int *nctr,dataptr dz)
  2330. {
  2331. int exit_status;
  2332. int n, to_doo, exess;
  2333. int real_gsize = gsize_per_chan * dz->out_chans;
  2334. double lpos;
  2335. double adjust = dehole(rpos);
  2336. float *writend, *fptr = *Fptr, *ffptr = *FFptr;
  2337. float *gbufptr = dz->extrabuf[WRAP_GBUF];
  2338. lpos = (1.0 - rpos) * adjust;
  2339. rpos *= adjust;
  2340. chana--;
  2341. chanb--;
  2342. if((writend = (ffptr + real_gsize)) > *maxwrite)
  2343. *maxwrite = writend;
  2344. if(ffptr + real_gsize >= dz->fptr[WRAP_LTAILEND]) {
  2345. to_doo = dz->fptr[WRAP_LTAILEND] - ffptr;
  2346. if((exess = real_gsize - to_doo) >= dz->iparam[WRAP_LONGS_BUFLEN]) {
  2347. sprintf(errstr,"Array overflow: write_multichan_grain()\n");
  2348. return(PROGRAM_ERROR);
  2349. }
  2350. to_doo /= dz->out_chans;
  2351. for(n = 0; n < to_doo; n++) {
  2352. *(ffptr + chana) += (float) (*gbufptr * lpos);
  2353. *(ffptr + chanb) += (float) (*gbufptr * rpos);
  2354. gbufptr++;
  2355. ffptr += dz->out_chans;
  2356. }
  2357. if((exit_status = write_samps_granula(dz->iparam[WRAP_LONGS_BUFLEN],nctr,dz))<0)
  2358. return(exit_status);
  2359. memmove((char *)dz->fptr[WRAP_LBUF],(char *)dz->fptr[WRAP_LBUFEND],
  2360. (size_t)dz->iparam[WRAP_LBUF_SMPXS] * sizeof(float));
  2361. memset((char *)dz->fptr[WRAP_LBUFMID],0,dz->iparam[WRAP_LONGS_BUFLEN] * sizeof(float));
  2362. ffptr -= dz->iparam[WRAP_LONGS_BUFLEN];
  2363. fptr -= dz->iparam[WRAP_LONGS_BUFLEN];
  2364. *maxwrite -= dz->buflen; /* APR 1996 */
  2365. exess /= dz->out_chans;
  2366. for(n = 0; n < exess; n++) {
  2367. *(ffptr + chana) += (float) (*gbufptr * lpos);
  2368. *(ffptr + chanb) += (float) (*gbufptr * rpos);
  2369. gbufptr++;
  2370. ffptr += dz->out_chans;
  2371. }
  2372. *Fptr = fptr;
  2373. *FFptr = ffptr;
  2374. return(FINISHED);
  2375. }
  2376. for(n=0;n<gsize_per_chan;n++) {
  2377. *(ffptr + chana) += (float) (*gbufptr * lpos);
  2378. *(ffptr + chanb) += (float) (*gbufptr * rpos);
  2379. gbufptr++;
  2380. ffptr += dz->out_chans;
  2381. }
  2382. *Fptr = fptr;
  2383. *FFptr = ffptr;
  2384. return(FINISHED);
  2385. }
  2386. /************************* GET_WRAPPAGE_POSITION ****************************
  2387. *
  2388. * The output position is calculated from centre, spread and depth.
  2389. *
  2390. * depth depth
  2391. * _____ _____ effective_with = depth;
  2392. * | | | | position random between 1 & -1;
  2393. * | | centre | | position *= depth; (range -depth TO +depth)
  2394. * |------------------|------------------| if(position < 0.0)
  2395. * | halfspread halfspread | position = centre - half_spread + depth + position.
  2396. * |_____________________________________| else
  2397. * spread position = centre + half_spread - depth + position.
  2398. */
  2399. double get_wrappage_position(dataptr dz)
  2400. {
  2401. double pos;
  2402. double spread = min(dz->iparam[WRAP_OUTCHANS],dz->param[WRAP_SPREAD]);
  2403. double halfspread = spread/2.0;
  2404. double depth = min(spread/2.0,dz->param[WRAP_DEPTH]);
  2405. double centre = dz->wrapcentre - 1.0; // Convert from 1-N frame to 0 to (N-1) frame
  2406. pos = (drand48() * 2.0) - 1.0;
  2407. pos *= depth;
  2408. if(pos < 0.0)
  2409. pos += (centre - halfspread + depth);
  2410. else
  2411. pos += (centre + halfspread - depth);
  2412. while(pos >= dz->param[WRAP_OUTCHANS])
  2413. pos -= dz->param[WRAP_OUTCHANS];
  2414. while(pos < 0.0)
  2415. pos += dz->param[WRAP_OUTCHANS];
  2416. pos += 1.0; // Convert from 0 to (N-1) frame to 1-N frame
  2417. if(pos > dz->param[WRAP_OUTCHANS])
  2418. pos -= dz->param[WRAP_OUTCHANS]; // Positions beyond top chan, are between chan N and chan 1
  2419. return pos;
  2420. }
  2421. /************************* INITIALISE_UNUSED_BRASSAGE_VFLAGS *************************/
  2422. int initialise_unused_wrappage_vflags(dataptr dz)
  2423. {
  2424. int n;
  2425. if(dz->vflag==NULL) {
  2426. if((dz->vflag = (char *)malloc(WRAP_MAX_VFLAGS * sizeof(char)))==NULL) {
  2427. sprintf(errstr,"INSUFFICIENT MEMORY for wrappage flags.\n");
  2428. return(MEMORY_ERROR);
  2429. }
  2430. } else {
  2431. if((dz->vflag = (char *)realloc(dz->vflag,WRAP_MAX_VFLAGS * sizeof(char)))==NULL) {
  2432. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate wrappage flags.\n");
  2433. return(MEMORY_ERROR);
  2434. }
  2435. }
  2436. for(n=dz->application->vflag_cnt;n<WRAP_MAX_VFLAGS;n++)
  2437. dz->vflag[n] = FALSE;
  2438. return(FINISHED);
  2439. }
  2440. /************************** PARAM_CHECK_AND_CONVERT ************************************/
  2441. void param_check_and_convert(int *zerovel,dataptr dz)
  2442. {
  2443. double sr = (double)dz->infile->srate;
  2444. int paramno, total_params = dz->application->max_param_cnt + dz->application->option_cnt;
  2445. for(paramno = 0;paramno < total_params; paramno++) {
  2446. switch(paramno) {
  2447. case(WRAP_VELOCITY):
  2448. case(WRAP_HVELOCITY):
  2449. if(*zerovel == FALSE)
  2450. *zerovel = check_for_zeroes(paramno,dz);
  2451. break;
  2452. case(WRAP_AMP):
  2453. case(WRAP_HAMP):
  2454. /* not required for floats */
  2455. /*convert_value_to_int(paramno,TWO_POW_15,dz);*/
  2456. break;
  2457. case(WRAP_OUTLEN):
  2458. convert_value_to_int(paramno,sr,dz);
  2459. break;
  2460. case(WRAP_SRCHRANGE):
  2461. case(WRAP_GRAINSIZE):
  2462. case(WRAP_HGRAINSIZE):
  2463. case(WRAP_BSPLICE):
  2464. case(WRAP_HBSPLICE):
  2465. case(WRAP_ESPLICE):
  2466. case(WRAP_HESPLICE):
  2467. convert_value_to_int(paramno,MS_TO_SECS * sr,dz);
  2468. break;
  2469. }
  2470. }
  2471. }
  2472. /************************** CONVERT_PITCH ************************************/
  2473. void convert_pitch(int paramno,dataptr dz)
  2474. {
  2475. double *p, *end;
  2476. if(dz->brksize[paramno]) {
  2477. p = dz->brk[paramno] + 1;
  2478. end = dz->brk[paramno] + (dz->brksize[paramno] * 2);
  2479. while(p < end) {
  2480. *p *= OCTAVES_PER_SEMITONE;
  2481. p += 2;
  2482. }
  2483. } else
  2484. dz->param[paramno] = dz->param[paramno] * OCTAVES_PER_SEMITONE;
  2485. }
  2486. /******************************* INITIALISE_DATA **************************/
  2487. int initialise_data(dataptr dz)
  2488. {
  2489. /* NEED TO BE INITALISED (??) AS THEY ARE INTERNAL */
  2490. dz->param[WRAP_VRANGE] = 0.0;
  2491. dz->param[WRAP_DRANGE] = 0.0;
  2492. dz->param[WRAP_PRANGE] = 0.0;
  2493. dz->param[WRAP_SPRANGE] = 0.0;
  2494. dz->iparam[WRAP_ARANGE] = 0;
  2495. dz->iparam[WRAP_GRANGE] = 0;
  2496. dz->iparam[WRAP_BRANGE] = 0;
  2497. dz->iparam[WRAP_ERANGE] = 0;
  2498. dz->iparam[WRAP_LONGS_BUFLEN] = 0;
  2499. dz->iparam[WRAP_BUF_XS] = 0;
  2500. dz->iparam[WRAP_LBUF_XS] = 0;
  2501. dz->iparam[WRAP_BUF_SMPXS] = 0;
  2502. dz->iparam[WRAP_LBUF_SMPXS] = 0;
  2503. dz->iparam[WRAP_GLBUF_SMPXS] = 0;
  2504. dz->iparam[WRAP_SAMPS_IN_INBUF] = 0;
  2505. dz->iparam[WRAP_IS_BTAB] = FALSE;
  2506. dz->iparam[WRAP_IS_ETAB] = FALSE;
  2507. if((dz->parray[WRAP_NORMFACT] = (double *)malloc(dz->iparam[WRAP_ARRAYSIZE] * sizeof(double)))==NULL) {
  2508. sprintf(errstr,"initialise_data()\n");
  2509. return(PROGRAM_ERROR);
  2510. }
  2511. return(FINISHED);
  2512. }
  2513. /************************** SET_INTERNAL_FLAGS ************************************/
  2514. int set_internal_flags(dataptr dz)
  2515. {
  2516. if(dz->brksize[WRAP_HVELOCITY]) dz->iparray[WRAP_FLAGS][WRAP_VELOCITY_FLAG] |= WRAP_VARIABLE_HI_BOUND;
  2517. else dz->iparray[WRAP_FLAGS][WRAP_VELOCITY_FLAG] |= WRAP_HI_BOUND;
  2518. if(dz->brksize[WRAP_HDENSITY]) dz->iparray[WRAP_FLAGS][WRAP_DENSITY_FLAG] |= WRAP_VARIABLE_HI_BOUND;
  2519. else dz->iparray[WRAP_FLAGS][WRAP_DENSITY_FLAG] |= WRAP_HI_BOUND;
  2520. if(dz->brksize[WRAP_HGRAINSIZE]) dz->iparray[WRAP_FLAGS][WRAP_GRAINSIZE_FLAG] |= WRAP_VARIABLE_HI_BOUND;
  2521. else dz->iparray[WRAP_FLAGS][WRAP_GRAINSIZE_FLAG] |= WRAP_HI_BOUND;
  2522. if(dz->brksize[WRAP_HPITCH]) dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG] |= WRAP_VARIABLE_HI_BOUND;
  2523. else dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG] |= WRAP_HI_BOUND;
  2524. if(dz->brksize[WRAP_HAMP]) dz->iparray[WRAP_FLAGS][WRAP_AMP_FLAG] |= WRAP_VARIABLE_HI_BOUND;
  2525. else dz->iparray[WRAP_FLAGS][WRAP_AMP_FLAG] |= WRAP_HI_BOUND;
  2526. if(dz->brksize[WRAP_HBSPLICE]) dz->iparray[WRAP_FLAGS][WRAP_BSPLICE_FLAG] |= WRAP_VARIABLE_HI_BOUND;
  2527. else dz->iparray[WRAP_FLAGS][WRAP_BSPLICE_FLAG] |= WRAP_HI_BOUND;
  2528. if(dz->brksize[WRAP_HESPLICE]) dz->iparray[WRAP_FLAGS][WRAP_ESPLICE_FLAG] |= WRAP_VARIABLE_HI_BOUND;
  2529. else dz->iparray[WRAP_FLAGS][WRAP_ESPLICE_FLAG] |= WRAP_HI_BOUND;
  2530. if(dz->brksize[WRAP_VELOCITY]) dz->iparray[WRAP_FLAGS][WRAP_VELOCITY_FLAG] |= WRAP_VARIABLE_VAL;
  2531. else dz->iparray[WRAP_FLAGS][WRAP_VELOCITY_FLAG] |= WRAP_FIXED_VAL;
  2532. if(dz->brksize[WRAP_DENSITY]) dz->iparray[WRAP_FLAGS][WRAP_DENSITY_FLAG] |= WRAP_VARIABLE_VAL;
  2533. else dz->iparray[WRAP_FLAGS][WRAP_DENSITY_FLAG] |= WRAP_FIXED_VAL;
  2534. if(dz->brksize[WRAP_GRAINSIZE]) dz->iparray[WRAP_FLAGS][WRAP_GRAINSIZE_FLAG] |= WRAP_VARIABLE_VAL;
  2535. else dz->iparray[WRAP_FLAGS][WRAP_GRAINSIZE_FLAG] |= WRAP_FIXED_VAL;
  2536. if(dz->brksize[WRAP_PITCH]) dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG] |= WRAP_VARIABLE_VAL;
  2537. else dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG] |= WRAP_FIXED_VAL;
  2538. if(dz->brksize[WRAP_AMP]) dz->iparray[WRAP_FLAGS][WRAP_AMP_FLAG] |= WRAP_VARIABLE_VAL;
  2539. else dz->iparray[WRAP_FLAGS][WRAP_AMP_FLAG] |= WRAP_FIXED_VAL;
  2540. if(dz->brksize[WRAP_BSPLICE]) dz->iparray[WRAP_FLAGS][WRAP_BSPLICE_FLAG] |= WRAP_VARIABLE_VAL;
  2541. else dz->iparray[WRAP_FLAGS][WRAP_BSPLICE_FLAG] |= WRAP_FIXED_VAL;
  2542. if(dz->brksize[WRAP_ESPLICE]) dz->iparray[WRAP_FLAGS][WRAP_ESPLICE_FLAG] |= WRAP_VARIABLE_VAL;
  2543. else dz->iparray[WRAP_FLAGS][WRAP_ESPLICE_FLAG] |= WRAP_FIXED_VAL;
  2544. if(dz->brksize[WRAP_SRCHRANGE]) dz->iparray[WRAP_FLAGS][WRAP_RANGE_FLAG] |= WRAP_VARIABLE_VAL;
  2545. else dz->iparray[WRAP_FLAGS][WRAP_RANGE_FLAG] |= WRAP_FIXED_VAL;
  2546. if(dz->brksize[WRAP_SCATTER]) dz->iparray[WRAP_FLAGS][WRAP_SCATTER_FLAG] |= WRAP_VARIABLE_VAL;
  2547. else dz->iparray[WRAP_FLAGS][WRAP_SCATTER_FLAG] |= WRAP_FIXED_VAL;
  2548. return(FINISHED);
  2549. }
  2550. /**************************** INITIALISE_CHANNEL_CONFIGURATION *******************************/
  2551. void initialise_channel_configuration(dataptr dz)
  2552. {
  2553. if(dz->infile->channels > MONO) {
  2554. dz->iparam[WRAP_CHANNELS] = TRUE;
  2555. } else {
  2556. dz->iparam[WRAP_CHANNELS] = FALSE;
  2557. }
  2558. dz->iparam[WRAP_INCHANS] = MONO; /* input is mono, grain is mono */
  2559. }
  2560. /*************************** GRANULA_SETUP ***********************/
  2561. int granula_setup(dataptr dz)
  2562. {
  2563. int exit_status;
  2564. if((exit_status = make_splice_tables(dz))<0)
  2565. return(exit_status);
  2566. set_ranges(dz);
  2567. if((exit_status = calc_overflows(dz))<0)
  2568. return(exit_status);
  2569. dz->iparam[WRAP_BUF_SMPXS] *= dz->iparam[WRAP_INCHANS]; // extra space in INPUT buffer (must accept all input channels)
  2570. dz->iparam[WRAP_GLBUF_SMPXS] *= dz->iparam[WRAP_INCHANS]; // extra space in the GRAIN buffer is either mono
  2571. // (where channel is extracted, or all channels mixed to mono)
  2572. // or it is multichannel: WRAP_INCHANS knows this
  2573. dz->iparam[WRAP_LBUF_SMPXS] *= dz->iparam[WRAP_OUTCHANS]; // The output grain can be multichan via spatialisation, or via using multichan input
  2574. // or multichan if multichan input goes direct to multichan output
  2575. return(FINISHED);
  2576. }
  2577. /************************** CHECK_FOR_ZEROES ************************************/
  2578. int check_for_zeroes(int paramno,dataptr dz)
  2579. {
  2580. double *p, *end;
  2581. if(dz->brksize[paramno]) {
  2582. p = dz->brk[paramno] + 1;
  2583. end = dz->brk[paramno] + (dz->brksize[paramno] * 2);
  2584. while(p < end) {
  2585. if(flteq(*p,0.0))
  2586. return TRUE;
  2587. p++;
  2588. }
  2589. } else {
  2590. if(flteq(dz->param[paramno],0.0))
  2591. return TRUE;
  2592. }
  2593. return (FALSE);
  2594. }
  2595. /************************** CONVERT_VALUE_TO_INT ************************************/
  2596. void convert_value_to_int(int paramno,double convertor,dataptr dz)
  2597. {
  2598. double *p, *end;
  2599. if(dz->brksize[paramno]) {
  2600. p = dz->brk[paramno] + 1;
  2601. end = dz->brk[paramno] + (dz->brksize[paramno] * 2);
  2602. while(p < end) {
  2603. *p = (double)round(*p * convertor);
  2604. p += 2;
  2605. }
  2606. dz->is_int[paramno] = TRUE;
  2607. } else
  2608. dz->iparam[paramno] = round(dz->param[paramno] * convertor);
  2609. }
  2610. /************************ MAKE_SPLICE_TABLES ******************************/
  2611. int make_splice_tables(dataptr dz)
  2612. { /* rwd: changed to eliminate f/p division */
  2613. int n; /* plus quasi-exponential option */
  2614. double dif,val,lastval;
  2615. double length, newsum,lastsum,twodif;
  2616. double *local_btabptr; /* better safe than sorry! */
  2617. double *local_etabptr;
  2618. if(dz->iparray[WRAP_FLAGS][WRAP_BSPLICE_FLAG]<=1) {
  2619. if(dz->iparam[WRAP_BSPLICE]==0) {
  2620. dz->iparam[WRAP_IS_BTAB] = TRUE; /* even though it's empty */
  2621. return(FINISHED);
  2622. }
  2623. if((dz->parray[WRAP_BSPLICETAB] = (double *)malloc(dz->iparam[WRAP_BSPLICE] * sizeof(double)))==NULL) {
  2624. sprintf(errstr,"make_splice_tables(): 1\n");
  2625. return(PROGRAM_ERROR);
  2626. }
  2627. local_btabptr = dz->parray[WRAP_BSPLICETAB];
  2628. val = 0.0;
  2629. length = (double)dz->iparam[WRAP_BSPLICE];
  2630. if(!dz->vflag[WRAP_EXPON]) {
  2631. dif = 1.0/length;
  2632. lastval = dif;
  2633. *local_btabptr++ = val;
  2634. *local_btabptr++ = lastval;
  2635. for(n=2;n<dz->iparam[WRAP_BSPLICE];n++) {
  2636. val = lastval + dif;
  2637. lastval = val;
  2638. *local_btabptr++ = val;
  2639. }
  2640. } else { /* do quasi-exponential splice */
  2641. dif = 1.0/(length*length);
  2642. twodif = dif*2.0;
  2643. lastsum = 0.0;
  2644. lastval = dif;
  2645. *local_btabptr++ = val;
  2646. *local_btabptr++ = lastval;
  2647. for(n=2;n<dz->iparam[WRAP_BSPLICE];n++) {
  2648. newsum = lastsum + twodif;
  2649. val = lastval + newsum + dif;
  2650. *local_btabptr++ = val;
  2651. lastval = val;
  2652. lastsum = newsum;
  2653. }
  2654. }
  2655. dz->iparam[WRAP_IS_BTAB] = TRUE;
  2656. }
  2657. if(dz->iparray[WRAP_FLAGS][WRAP_ESPLICE_FLAG]<=1) {
  2658. if(dz->iparam[WRAP_ESPLICE]==0) {
  2659. dz->iparam[WRAP_IS_ETAB] = TRUE; /* even thogh it's empty! */
  2660. return(FINISHED);
  2661. }
  2662. if((dz->parray[WRAP_ESPLICETAB] = (double *)malloc(dz->iparam[WRAP_ESPLICE] * sizeof(double)))==NULL) {
  2663. sprintf(errstr,"make_splice_tables(): 2\n");
  2664. return(PROGRAM_ERROR);
  2665. }
  2666. local_etabptr = dz->parray[WRAP_ESPLICETAB] + dz->iparam[WRAP_ESPLICE];
  2667. val = 0.0;
  2668. length = (double)dz->iparam[WRAP_ESPLICE];
  2669. if(!dz->vflag[WRAP_EXPON]) {
  2670. dif = 1.0/length;
  2671. lastsum = dif;
  2672. *--local_etabptr = val;
  2673. *--local_etabptr = lastsum;
  2674. for(n=dz->iparam[WRAP_ESPLICE]-3;n>=0;n--){
  2675. val = lastsum + dif;
  2676. lastsum = val;
  2677. *--local_etabptr = val;
  2678. }
  2679. } else {
  2680. dif = 1.0/(length*length);
  2681. twodif = dif*2.0;
  2682. lastsum = 0.0;
  2683. lastval = dif;
  2684. *--local_etabptr = val;
  2685. *--local_etabptr = lastval;
  2686. for(n=dz->iparam[WRAP_ESPLICE]-3;n>=0;n--) {
  2687. newsum = lastsum + twodif;
  2688. val = lastval + newsum + dif;
  2689. *--local_etabptr = val;
  2690. lastval = val;
  2691. lastsum = newsum;
  2692. }
  2693. }
  2694. dz->iparam[WRAP_IS_ETAB] = TRUE;
  2695. }
  2696. return(FINISHED);
  2697. }
  2698. /************************* SET_RANGES *****************************/
  2699. void set_ranges(dataptr dz)
  2700. {
  2701. int n;
  2702. for(n=0;n<WRAP_SFLAGCNT;n++) {
  2703. if(dz->iparray[WRAP_FLAGS][n]==RANGED) {
  2704. switch(n) {
  2705. case(WRAP_VELOCITY_FLAG):
  2706. set_this_drange(WRAP_VRANGE,WRAP_HVELOCITY,WRAP_VELOCITY,WRAP_VELOCITY_FLAG,dz);
  2707. break;
  2708. case(WRAP_DENSITY_FLAG):
  2709. set_this_drange(WRAP_DRANGE,WRAP_HDENSITY,WRAP_DENSITY,WRAP_DENSITY_FLAG,dz);
  2710. break;
  2711. case(WRAP_GRAINSIZE_FLAG):
  2712. set_this_range(WRAP_GRANGE,WRAP_HGRAINSIZE,WRAP_GRAINSIZE,WRAP_GRAINSIZE_FLAG,dz);
  2713. break;
  2714. case(WRAP_BSPLICE_FLAG):
  2715. set_this_range(WRAP_BRANGE,WRAP_HBSPLICE,WRAP_BSPLICE,WRAP_BSPLICE_FLAG,dz);
  2716. break;
  2717. case(WRAP_ESPLICE_FLAG):
  2718. set_this_range(WRAP_ERANGE,WRAP_HESPLICE,WRAP_ESPLICE,WRAP_ESPLICE_FLAG,dz);
  2719. break;
  2720. case(WRAP_PITCH_FLAG):
  2721. set_this_drange(WRAP_PRANGE,WRAP_HPITCH,WRAP_PITCH,WRAP_PITCH_FLAG,dz);
  2722. break;
  2723. case(WRAP_AMP_FLAG):
  2724. /*set_this_range*/set_this_drange(WRAP_ARANGE,WRAP_HAMP,WRAP_AMP,WRAP_AMP_FLAG,dz);
  2725. break;
  2726. }
  2727. }
  2728. }
  2729. }
  2730. /************************ CALC_OVERFLOWS *************************
  2731. *
  2732. * We will attempt to put data into output buffer until we reach its 'end'.
  2733. * But out grain will extend GRAINSIZE beyond this> So we must have
  2734. * an overflow area on the buffer equal to the maximum grainsize.
  2735. *
  2736. * On the input side, we will attempt to read from input buffer until we
  2737. * get to its end, BUT, grainsize extends beyond this. Also, to create
  2738. * an output of GRAINSIZE we need to use an input size of
  2739. * (grainsize*transposition). So overflow on input buffer = maximum val
  2740. * of (grainsize*transposition).
  2741. * Lbuf_smpxs = overflow in Lbuf
  2742. * gLbuf_smpxs = MAXIMUM GRAINSIZE
  2743. * buf_smpxs = overflow in inbuf
  2744. */
  2745. int calc_overflows(dataptr dz) /* DO THIS AFTER THE CONVERSION OF PITCH from SEMITONES */
  2746. {
  2747. int exit_status;
  2748. int max_scatter;
  2749. double mt = 1.0; /* MAXIMUM TRANSPOSITION */
  2750. double mg = 1.0; /* MAXIMUM GRAINSIZE */
  2751. double max1, max2;
  2752. int mgi;
  2753. switch(dz->iparray[WRAP_FLAGS][WRAP_PITCH_FLAG]) {
  2754. case(NOT_SET):
  2755. mt = 1.0;
  2756. break;
  2757. case(FIXED):
  2758. mt = dz->param[WRAP_PITCH];
  2759. break;
  2760. case(VARIABLE):
  2761. if((exit_status = get_maxvalue_in_brktable(&max1,WRAP_PITCH,dz))<0)
  2762. return(exit_status);
  2763. mt = max1;
  2764. break;
  2765. case(RANGED):
  2766. mt = max(dz->param[WRAP_HPITCH],dz->param[WRAP_PITCH]);
  2767. break;
  2768. case(RANGE_VLO):
  2769. if((exit_status = get_maxvalue_in_brktable(&max1,WRAP_PITCH,dz))<0)
  2770. return(exit_status);
  2771. mt = max(max1,dz->param[WRAP_HPITCH]);
  2772. break;
  2773. case(RANGE_VHI):
  2774. if((exit_status = get_maxvalue_in_brktable(&max2,WRAP_HPITCH,dz))<0)
  2775. return(exit_status);
  2776. mt = max(max2,dz->param[WRAP_PITCH]);
  2777. break;
  2778. case(RANGE_VHILO):
  2779. if((exit_status = get_maxvalue_in_brktable(&max1,WRAP_PITCH,dz))<0)
  2780. return(exit_status);
  2781. if((exit_status = get_maxvalue_in_brktable(&max2,WRAP_HPITCH,dz))<0)
  2782. return(exit_status);
  2783. mt = max(max1,max2);
  2784. break;
  2785. }
  2786. mt = pow(2.0,mt); /* CONVERT OCTAVES TO TRANSPOSITION RATIO */
  2787. switch(dz->iparray[WRAP_FLAGS][WRAP_GRAINSIZE_FLAG]) {
  2788. case(NOT_SET):
  2789. case(FIXED):
  2790. mg = (double)dz->iparam[WRAP_GRAINSIZE];
  2791. break;
  2792. case(VARIABLE):
  2793. if((exit_status = get_maxvalue_in_brktable(&max1,WRAP_GRAINSIZE,dz))<0)
  2794. return(exit_status);
  2795. mg = max1;
  2796. break;
  2797. case(RANGED):
  2798. // BUG: OCtober 2009
  2799. mg = max(dz->iparam[WRAP_HGRAINSIZE],dz->iparam[WRAP_GRAINSIZE]);
  2800. break;
  2801. case(RANGE_VLO):
  2802. if((exit_status = get_maxvalue_in_brktable(&max1,WRAP_GRAINSIZE,dz))<0)
  2803. return(exit_status);
  2804. mg = max(max1,(double)dz->iparam[WRAP_HGRAINSIZE]);
  2805. break;
  2806. case(RANGE_VHI):
  2807. if((exit_status = get_maxvalue_in_brktable(&max2,WRAP_HGRAINSIZE,dz))<0)
  2808. return(exit_status);
  2809. mg = max(max2,(double)dz->iparam[WRAP_GRAINSIZE]);
  2810. break;
  2811. case(RANGE_VHILO):
  2812. if((exit_status = get_maxvalue_in_brktable(&max1,WRAP_GRAINSIZE,dz))<0)
  2813. return(exit_status);
  2814. if((exit_status = get_maxvalue_in_brktable(&max2,WRAP_HGRAINSIZE,dz))<0)
  2815. return(exit_status);
  2816. mg = max(max1,max2);
  2817. break;
  2818. }
  2819. mgi = round(mg)+1;
  2820. dz->iparam[WRAP_GLBUF_SMPXS] = mgi; /* Overflow in outbuf = MAX-grainsize */
  2821. dz->iparam[WRAP_BUF_SMPXS] = round((double)mgi * mt); /* Overflow in inbuf = MAX-grainsize * transpos */
  2822. if((exit_status = calc_max_scatter(mgi,&max_scatter,dz))<0)
  2823. return(exit_status);
  2824. adjust_overflows(max_scatter,dz);
  2825. return(FINISHED);
  2826. }
  2827. /************************ SET_THIS_RANGE ******************************/
  2828. void set_this_range(int rangeno,int hino, int lono, int flagno, dataptr dz)
  2829. {
  2830. dz->iparam[rangeno] = dz->iparam[hino] - dz->iparam[lono];
  2831. if(dz->iparam[rangeno]==0)
  2832. dz->iparray[WRAP_FLAGS][flagno] = FIXED;
  2833. }
  2834. /************************ SET_THIS_DRANGE ******************************/
  2835. void set_this_drange(int rangeno,int hino, int lono, int flagno, dataptr dz)
  2836. {
  2837. dz->param[rangeno] = dz->param[hino] - dz->param[lono];
  2838. if(flteq(dz->param[rangeno],0.0))
  2839. dz->iparray[WRAP_FLAGS][flagno] = FIXED;
  2840. }
  2841. /******************************** CALC_MAX_SCATTER ****************************/
  2842. int calc_max_scatter(int mgi,int *max_scatlen, dataptr dz) /* mgi = MAX GRAIN SIZE */
  2843. {
  2844. #define WRAP_ROUNDUP (0.9999)
  2845. int exit_status;
  2846. int os = 0;
  2847. double sc = 0.0;
  2848. double min1, min2, minn/*, sr = (double)dz->infile->srate*/;
  2849. int k = dz->iparray[WRAP_FLAGS][WRAP_DENSITY_FLAG];
  2850. switch(k) {
  2851. case(NOT_SET):
  2852. case(FIXED):
  2853. case(RANGED):
  2854. os = (int)(((double)mgi/dz->param[WRAP_DENSITY]) + WRAP_ROUNDUP); /* ROUND UP */
  2855. break;
  2856. case(VARIABLE):
  2857. if((exit_status = get_minvalue_in_brktable(&min1,WRAP_DENSITY,dz))<0)
  2858. return(exit_status);
  2859. os = (int)(((double)mgi/min1) + WRAP_ROUNDUP);
  2860. break;
  2861. case(RANGE_VLO):
  2862. if((exit_status = get_minvalue_in_brktable(&min1,WRAP_DENSITY,dz))<0)
  2863. return(exit_status);
  2864. minn = min(min1,dz->param[WRAP_HDENSITY]);
  2865. os = (int)(((double)mgi/minn) + WRAP_ROUNDUP);
  2866. break;
  2867. case(RANGE_VHI):
  2868. if((exit_status = get_minvalue_in_brktable(&min2,WRAP_HDENSITY,dz))<0)
  2869. return(exit_status);
  2870. minn = min(min2,dz->param[WRAP_DENSITY]);
  2871. os = (int)(((double)mgi/minn) + WRAP_ROUNDUP);
  2872. break;
  2873. case(RANGE_VHILO):
  2874. if((exit_status = get_minvalue_in_brktable(&min1,WRAP_DENSITY,dz))<0)
  2875. return(exit_status);
  2876. if((exit_status = get_minvalue_in_brktable(&min2,WRAP_HDENSITY,dz))<0)
  2877. return(exit_status);
  2878. minn = min(min1,min2);
  2879. os = (int)(((double)mgi/minn) + WRAP_ROUNDUP);
  2880. break;
  2881. }
  2882. switch(dz->iparray[WRAP_FLAGS][WRAP_SCATTER_FLAG]) {
  2883. case(NOT_SET):
  2884. case(FIXED):
  2885. sc = dz->param[WRAP_SCATTER];
  2886. break;
  2887. case(VARIABLE):
  2888. if((exit_status = get_maxvalue_in_brktable(&sc,WRAP_SCATTER,dz))<0)
  2889. return(exit_status);
  2890. break;
  2891. }
  2892. *max_scatlen = (int)(((double)os * sc) + WRAP_ROUNDUP); /* ROUND UP */
  2893. return(FINISHED);
  2894. }
  2895. /************************ ADJUST_OVERFLOWS ***************************
  2896. * WRAP_LBUF_SMPXS = overflow in calculation buffer Lbuf
  2897. * iparam[WRAP_GLBUF_SMPXS] = MAXIMUM GRAINSIZE
  2898. * WRAP_BUF_SMPXS = overflow in inbuf
  2899. */
  2900. /* RW NB we eliminate almost everything! */
  2901. void adjust_overflows(int mscat, dataptr dz)
  2902. {
  2903. dz->iparam[WRAP_BUF_SMPXS] += SAFETY; /* ADD SAFETY MARGINS !! */
  2904. dz->iparam[WRAP_GLBUF_SMPXS] += OSAFETY;
  2905. dz->iparam[WRAP_LBUF_SMPXS] = dz->iparam[WRAP_GLBUF_SMPXS] + mscat;
  2906. }
  2907. /****************************** STORE_FILENAME *********************************/
  2908. int store_the_filename(char *filename,dataptr dz)
  2909. {
  2910. if(dz->wordstor != NULL) {
  2911. sprintf(errstr,"Cannot store filename: wordstor already allocated.\n");
  2912. return(PROGRAM_ERROR);
  2913. }
  2914. if((dz->wordstor = (char **)malloc(sizeof(char *)))==NULL) {
  2915. sprintf(errstr,"Cannot store filename.\n");
  2916. return(MEMORY_ERROR);
  2917. }
  2918. if((dz->wordstor[0] = (char *)malloc((strlen(filename)+1) * sizeof(char)))==NULL) {
  2919. sprintf(errstr,"Cannot store filename.\n");
  2920. return(MEMORY_ERROR);
  2921. }
  2922. if(strcpy(dz->wordstor[0],filename)!=dz->wordstor[0]) {
  2923. sprintf(errstr,"Failed to copy filename to store.\n");
  2924. return(PROGRAM_ERROR);
  2925. }
  2926. dz->all_words++;
  2927. return(FINISHED);
  2928. }
  2929. /************************ HANDLE_THE_SPECIAL_DATA *********************/
  2930. int handle_the_special_data(int *cmdlinecnt,char ***cmdline,dataptr dz)
  2931. {
  2932. int exit_status;
  2933. aplptr ap = dz->application;
  2934. if(!sloom) {
  2935. if(*cmdlinecnt <= 0) {
  2936. sprintf(errstr,"Insufficient parameters on command line.\n");
  2937. return(USAGE_ONLY);
  2938. }
  2939. }
  2940. ap->min_special = 0;
  2941. ap->max_special = 16;
  2942. ap->min_special2 = -1;
  2943. ap->max_special2 = 1;
  2944. if((exit_status = read_centre_data((*cmdline)[0],dz))<0)
  2945. return(exit_status);
  2946. (*cmdline)++;
  2947. (*cmdlinecnt)--;
  2948. return(FINISHED);
  2949. }
  2950. /************************ READ_CENTRE_DATA *********************/
  2951. int read_centre_data(char *filename,dataptr dz)
  2952. {
  2953. double *time, *position, *direction, lasttime, dummy;
  2954. int cnt;
  2955. char temp[200], *q;
  2956. aplptr ap;
  2957. FILE *fp;
  2958. ap = dz->application;
  2959. dz->zeroset = 0;
  2960. if(sloom) {
  2961. if(filename[0] == NUMERICVAL_MARKER) {
  2962. q = filename + 1;
  2963. dz->wrapcentre = atof(q);
  2964. dz->zeroset = 1;
  2965. }
  2966. } else if(isdigit(filename[0]) || ((filename[0] == '.') && isdigit(filename[1]))) {
  2967. dz->wrapcentre = atof(filename);
  2968. dz->zeroset = 1;
  2969. }
  2970. if(dz->zeroset)
  2971. return FINISHED;
  2972. if((fp = fopen(filename,"r"))==NULL) {
  2973. sprintf(errstr, "Can't open file %s to read data.\n",filename);
  2974. return(DATA_ERROR);
  2975. }
  2976. cnt = 0;
  2977. while(fgets(temp,200,fp)==temp) {
  2978. q = temp;
  2979. while(get_float_from_within_string(&q,&dummy)) {
  2980. cnt++;
  2981. }
  2982. }
  2983. if(cnt == 0) {
  2984. sprintf(errstr,"No data in file %s\n",filename);
  2985. return(DATA_ERROR);
  2986. }
  2987. if(((dz->ringsize = cnt/3) * 3) != cnt) {
  2988. sprintf(errstr,"Data not grouped correctly in file %s\n",filename);
  2989. return(DATA_ERROR);
  2990. }
  2991. if((dz->parray[WRAP_CENTRE] = (double *)malloc(cnt * sizeof(double)))==NULL) {
  2992. sprintf(errstr,"INSUFFICIENT MEMORY for data in file %s.\n",filename);
  2993. return(MEMORY_ERROR);
  2994. }
  2995. time = dz->parray[WRAP_CENTRE];
  2996. position = time+1;
  2997. direction = time+2;
  2998. rewind(fp);
  2999. lasttime = -1.0;
  3000. cnt = 0;
  3001. while(fgets(temp,200,fp)==temp) {
  3002. q = temp;
  3003. while(get_float_from_within_string(&q,&dummy)) {
  3004. switch(cnt) {
  3005. case(0):
  3006. if(dummy < 0.0 || dummy <= lasttime) {
  3007. sprintf(errstr,"Times do not advance correctly in file %s\n",filename);
  3008. return(DATA_ERROR);
  3009. }
  3010. *time = dummy;
  3011. time += 3;
  3012. break;
  3013. case(1):
  3014. if(dummy < ap->min_special || dummy > ap->max_special) {
  3015. sprintf(errstr,"Invalid position value (%lf) in file %s\n",dummy,filename);
  3016. return(DATA_ERROR);
  3017. }
  3018. *position = dummy;
  3019. position += 3;
  3020. break;
  3021. case(2):
  3022. if(!(flteq(dummy,ap->min_special2) || flteq(dummy,ap->max_special2))) {
  3023. sprintf(errstr,"Invalid direction value (%lf) in file %s\n",dummy,filename);
  3024. return(DATA_ERROR);
  3025. }
  3026. *direction = dummy;
  3027. direction += 3;
  3028. break;
  3029. }
  3030. cnt++;
  3031. cnt %= 3;
  3032. }
  3033. }
  3034. if(fclose(fp)<0) {
  3035. fprintf(stdout,"WARNING: Failed to close file %s.\n",filename);
  3036. fflush(stdout);
  3037. }
  3038. return(FINISHED);
  3039. }
  3040. /************************ GET_CENTRE *********************/
  3041. void get_centre(double thistime,dataptr dz)
  3042. {
  3043. double *time, *position, *direction, *nexttime, *lasttime, *nextpos, *lastpos;
  3044. double lastplace, nextplace, time_step, time_frac, position_step, new_position;
  3045. int k;
  3046. // DATA is time - position - direction triples : total size is dz->ringsize;
  3047. time = dz->parray[WRAP_CENTRE];
  3048. k = 0;
  3049. while(*time < thistime) {
  3050. k += 3;
  3051. if(k >= dz->ringsize) { // Fell off end of data
  3052. position = time + 1;
  3053. dz->wrapcentre = *position;
  3054. return;
  3055. }
  3056. time += 3;
  3057. }
  3058. nexttime = time;
  3059. nextpos = time + 1;
  3060. if(k - 3 < 0) { // Time is at or before first entry in data
  3061. dz->wrapcentre = *nextpos;
  3062. return;
  3063. }
  3064. time -= 3;
  3065. lasttime = time;
  3066. lastpos = time + 1;
  3067. direction = time + 2;
  3068. lastplace = *lastpos;
  3069. nextplace = *nextpos;
  3070. time_step = *nexttime - *lasttime;
  3071. time_frac = (thistime - *lasttime)/time_step;
  3072. if(*direction > 0) {
  3073. if(nextplace < lastplace)
  3074. nextplace += dz->iparam[WRAP_OUTCHANS];
  3075. position_step = nextplace - lastplace;
  3076. position_step *= time_frac;
  3077. new_position = lastplace + position_step;
  3078. while(new_position > dz->iparam[WRAP_OUTCHANS])
  3079. new_position -= dz->iparam[WRAP_OUTCHANS];
  3080. } else {
  3081. if(nextplace > lastplace)
  3082. lastplace += dz->iparam[WRAP_OUTCHANS];
  3083. position_step = lastplace - nextplace;
  3084. position_step *= time_frac;
  3085. new_position = lastplace - position_step;
  3086. while(new_position > dz->iparam[WRAP_OUTCHANS])
  3087. new_position -= dz->iparam[WRAP_OUTCHANS];
  3088. }
  3089. dz->wrapcentre = new_position;
  3090. }
  3091. /************************ READ_TIMEVARYING_DATA *********************/
  3092. int read_timevarying_data(double itime, double otime, dataptr dz)
  3093. {
  3094. int exit_status, k;
  3095. if(!dz->zeroset) {
  3096. if(dz->vflag[WRAP_OTIME])
  3097. get_centre(otime,dz);
  3098. else
  3099. get_centre(itime,dz);
  3100. }
  3101. for(k = WRAP_SPREAD; k <= WRAP_SCATTER;k++) {
  3102. if(dz->brksize[k]) {
  3103. switch(k) {
  3104. case(WRAP_VELOCITY):
  3105. case(WRAP_HVELOCITY):
  3106. if((exit_status = read_value_from_brktable(itime,k,dz))<0)
  3107. return(exit_status);
  3108. break;
  3109. default:
  3110. if(dz->vflag[WRAP_OTIME]) {
  3111. if((exit_status = read_value_from_brktable(otime,k,dz))<0)
  3112. return(exit_status);
  3113. } else {
  3114. if((exit_status = read_value_from_brktable(itime,k,dz))<0)
  3115. return(exit_status);
  3116. }
  3117. break;
  3118. }
  3119. }
  3120. }
  3121. return FINISHED;
  3122. }