granula1.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659
  1. /*
  2. * Copyright (c) 1983-2013 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. * ABOUT THIS PROCESS
  23. *
  24. * There are (up to) 5 buffers invloved here.....
  25. * GRS_SBUF into which the input data is read from the file.
  26. * GRS_BUF into which the input data is mixed down to mono, where necessary. Otherwise, initial reads go directly to this buffer.
  27. * This buffer has an overflow sector of GRS_BUF_SMPXS samples.
  28. * The 1st read fills the buffer and the overflow.
  29. * After the first read these overflow samples are copied to the bufstart, and a new input buffer start is marked, overflow-samples into the GRS_BUF..
  30. * GRS_IBUF is then the point at which further samples are read into the input buffer.
  31. * GRS_LBUF is buffer into which grains are copied for processing to the output, (they are timestretched, pshifted or and spliced).
  32. * GRS_OBUF is the buffer into which the finished grains are copied ready for output.
  33. *
  34. * GRS_INCHANS is the number of channels in the input grains i.e. after they have (or have not) been mixed down to mono.
  35. * GRS_OUTCHANS is the number of channels in the output, which can be
  36. * Mono, where no spatial processing takes place, and the input is mono
  37. * Multichannel, where spatial processing takes place and the input, is
  38. * (a) Mono
  39. * (b) A selected (mono) channel from a multichannel input.
  40. * (c) A mix down of a multichannel input into a mono source.
  41. * Multichannel, where the input is multichannel, and no spatialisation is requested.
  42. */
  43. /* floatsam version */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <limits.h>
  47. #include <string.h>
  48. #include <structures.h>
  49. #include <tkglobals.h>
  50. #include <globcon.h>
  51. #include <processno.h>
  52. #include <modeno.h>
  53. #include <arrays.h>
  54. #include <filetype.h>
  55. #include <modify.h>
  56. #include <cdpmain.h>
  57. #include <sfsys.h>
  58. #include <osbind.h>
  59. //#ifdef unix
  60. #define round(x) lround((x))
  61. //#endif
  62. #define SAUS_SBUF (0)
  63. #define SAUS_OBUF (1)
  64. #define SAUS_BUF(x) (((x)*2)+2)
  65. #define SAUS_IBUF(x) (((x)*2)+3)
  66. static int read_samps_granula(int firsttime,dataptr dz);
  67. static int granulate(int *thissnd,long *aipc,long *aopc,float **iiiiptr,float **LLLLptr,
  68. double inv_sr,long *samptotal,float **maxwrite,int *nctr,dataptr dz);
  69. static int read_a_large_buf(dataptr dz);
  70. static int read_a_normal_buf_with_wraparound(dataptr dz);
  71. static int baktrak_granula(long samptotal,int absiicnt_per_chan,float **iiptr,dataptr dz);
  72. static int reset_granula(int resetskip,dataptr dz);
  73. static int make_grain(float *b,float **iiptr,float aamp,int gsize_per_chan,double *transpos,dataptr dz);
  74. static int make_stereo_grain(float *b,float **iiptr,float aamp,int gsize_per_chan,double *transpos,dataptr dz);
  75. static int write_grain(float **maxwrite,float **Fptr, float **FFptr,int gsize_per_chan,int *nctr,dataptr dz);
  76. static int write_stereo_grain(double rpos,float **maxwrite,float **Fptr,float **FFptr,
  77. int gsize_per_chan,int *nctr,dataptr dz);
  78. static double dehole(double pos);
  79. static int write_samps_granula(long k,int *nctr,dataptr dz);
  80. static void do_splices(int gsize_per_chan,int bspl,int espl,dataptr dz);
  81. static void do_btab_splice(dataptr dz);
  82. static void do_bsplice(int gsize_per_chan,dataptr dz,int bspl);
  83. static void do_etab_splice(int gsize_per_chan,dataptr dz);
  84. static void do_esplice(int gsize_per_chan,dataptr dz,int espl);
  85. static void do_stereo_splices(int gsize_per_chan,int bspl,int espl,dataptr dz);
  86. static void do_stereo_btab_splice(dataptr dz);
  87. static void do_stereo_bsplice(int gsize_per_chan,dataptr dz,int bspl);
  88. static void do_stereo_etab_splice(int gsize_per_chan,dataptr dz);
  89. static void do_stereo_esplice(int gsize_per_chan,dataptr dz,int espl);
  90. static float interp_gval_with_amp(float *s,double flcnt_frac,int chans,float ampp);
  91. static float interp_gval(float *s,double flcnt_frac,int chans);
  92. static int set_instep(int ostep_per_chan,dataptr dz);
  93. static int set_outstep(int gsize_per_chan,dataptr dz);
  94. static int set_range(int absiicnt,dataptr dz);
  95. static long do_scatter(int ostep_per_chan,dataptr dz);
  96. static int set_ivalue(int flag,int paramno,int hparamno,int rangeno,dataptr dz);
  97. static double set_dvalue(int flag,int paramno,int hparamno,int rangeno,dataptr dz);
  98. static int renormalise(int nctr,dataptr dz);
  99. static int read_samps_sausage(int firsttime,dataptr dz);
  100. static int read_a_specific_large_buf(int k,dataptr dz);
  101. static int read_a_specific_normal_buf_with_wraparound(int k,dataptr dz);
  102. static int baktrak_sausage(int thissnd,long samptotal,int absiicnt_per_chan,float **iiptr,dataptr dz);
  103. static int reset_sausage(int thissnd,int resetskip,dataptr dz);
  104. static int get_next_insnd(dataptr dz);
  105. static void perm_sausage(int cnt,dataptr dz);
  106. static void insert(int n,int t,int cnt_less_one,dataptr dz);
  107. static void prefix(int n,int cnt_less_one,dataptr dz);
  108. static void shuflup(int k,int cnt_less_one,dataptr dz);
  109. static int grab_an_appropriate_block_of_sausage_memory(long *this_bloksize,long standard_block,int bufdivisor,dataptr dz);
  110. #ifdef MULTICHAN
  111. static int make_multichan_grain(float *b,float **iiptr,float aamp,int gsize_per_chan,double *transpos,dataptr dz);
  112. static void do_multichan_splices(int gsize_per_chan,int bspl,int espl,dataptr dz);
  113. static void do_multichan_btab_splice(dataptr dz);
  114. static void do_multichan_bsplice(int gsize_per_chan,dataptr dz,int bspl);
  115. static void do_multichan_etab_splice(int gsize_per_chan,dataptr dz);
  116. static void do_multichan_esplice(int gsize_per_chan,dataptr dz,int espl);
  117. 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);
  118. #endif
  119. /********************************* GRANULA_PROCESS *************************************/
  120. int granula_process(dataptr dz)
  121. {
  122. int exit_status;
  123. int firsttime = TRUE, thissnd = 0;
  124. int nctr = 0; /* normalisation vals counter */
  125. long absicnt_per_chan = 0, absocnt_per_chan = 0;
  126. float *iptr = NULL;
  127. float *Fptr = dz->fptr[GRS_LBUF];
  128. long vals_to_write, total_ssampsread;
  129. double sr = (double)dz->infile->srate;
  130. double inverse_sr = 1.0/sr;
  131. float *maxwrite = dz->fptr[GRS_LBUF]; /* pointer to last sample created */
  132. //TW AGREED DELETIONS
  133. if(sloom)
  134. dz->total_samps_read = 0;
  135. dz->itemcnt = 0;
  136. switch(dz->process) {
  137. case(SAUSAGE):
  138. if((exit_status = read_samps_sausage(firsttime,dz))<0)
  139. return(exit_status);
  140. iptr = dz->sampbuf[SAUS_BUF(thissnd)];
  141. break;
  142. case(BRASSAGE):
  143. if((exit_status = read_samps_granula(firsttime,dz))<0)
  144. return(exit_status);
  145. iptr = dz->sampbuf[GRS_BUF];
  146. break;
  147. }
  148. total_ssampsread = dz->ssampsread;
  149. display_virtual_time(0L,dz);
  150. do {
  151. if((exit_status = granulate(&thissnd,&absicnt_per_chan,&absocnt_per_chan,&iptr,&Fptr,
  152. inverse_sr,&total_ssampsread,&maxwrite,&nctr,dz))<0)
  153. return(exit_status);
  154. } while(exit_status==CONTINUE);
  155. vals_to_write = maxwrite - dz->fptr[GRS_LBUF];
  156. while(vals_to_write > dz->iparam[GRS_LONGS_BUFLEN]) {
  157. // TW REVISED July 2004
  158. // if((exit_status = write_samps_granula(dz->buflen,&nctr,dz))<0)
  159. if((exit_status = write_samps_granula(dz->iparam[GRS_LONGS_BUFLEN],&nctr,dz))<0)
  160. return(exit_status);
  161. memmove((char *)dz->fptr[GRS_LBUF],(char *)dz->fptr[GRS_LBUFEND],
  162. dz->iparam[GRS_LBUF_SMPXS] * sizeof(float));
  163. memset((char *)dz->fptr[GRS_LBUFMID],0,dz->iparam[GRS_LONGS_BUFLEN] * sizeof(float));
  164. vals_to_write -= dz->iparam[GRS_LONGS_BUFLEN];
  165. }
  166. if(vals_to_write > 0) {
  167. if((exit_status = write_samps_granula(vals_to_write,&nctr,dz))<0)
  168. return(exit_status);
  169. }
  170. if(dz->total_samps_written <= 0) {
  171. sprintf(errstr,"SOURCE POSSIBLY TOO SHORT FOR THIS OPTION: Try 'Full Monty'\n");
  172. return(GOAL_FAILED);
  173. }
  174. display_virtual_time(0,dz);
  175. #ifdef MULTICHAN
  176. dz->infile->channels = dz->iparam[GRS_OUTCHANS]; // setup ONLY NOW for headwrite
  177. #endif
  178. return renormalise(nctr,dz);
  179. }
  180. /******************************* READ_SAMPS_GRANULA *******************************/
  181. int read_samps_granula(int firsttime,dataptr dz)
  182. {
  183. int exit_status;
  184. if(firsttime) {
  185. if((exit_status = read_a_large_buf(dz))<0)
  186. return(exit_status);
  187. } else {
  188. if((exit_status = read_a_normal_buf_with_wraparound(dz))<0)
  189. return(exit_status);
  190. }
  191. if(firsttime)
  192. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread;
  193. else
  194. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread + dz->iparam[GRS_BUF_SMPXS];
  195. return(FINISHED);
  196. }
  197. /* INPUT BUFFER :-
  198. *
  199. * |-----------BUFLEN-----------|
  200. *
  201. * buf ibuf bufend
  202. * |_________|__________________|buf_smpxs|
  203. * /
  204. * |buf_smpxs| <<-COPY_________/
  205. *
  206. * |-----------BUFLEN-----------|
  207. */
  208. /***************************** RENORMALISE ****************************
  209. *
  210. * (1) Find smallest normalising factor S (creating gretest level reduction).
  211. * (2) For each normalising factor N, find value which will bring it
  212. * down to S. That is S/N. Reinsert these values in the normalising
  213. * factor array.
  214. * REnormalising with these factors, will ensure whole file is normalised
  215. * using same factor (S), which is also the smallest factor required.
  216. * (3) Seek to start of outfile.
  217. * (4) Set infile pointer to same as outfile pointer, so that read_samps()
  218. * now reads from OUTFILE.
  219. * (5) Reset samps_read counter.
  220. * (6) Size of buffers we read depends on whether we output a stereo
  221. * file or a mono file.
  222. * (7) Reset normalisation-factor array pointer (m).
  223. * While we still have a complete buffer to read..
  224. * (8) Read samps into output buffer (as, if output is stereo, this
  225. * may be larger than ibuf, and will accomodate the data).
  226. * (9) Renormalise all values.
  227. * (10) Increment pointer to normalisation factors.
  228. * (11) Seek to start of current buffer, in file.
  229. * (12) Overwrite data in file.
  230. * (13) Re-seek to end of current buffer, in file.
  231. * (14) Calcualte how many samps left over at end, and if any...
  232. * (16) Read last (short) buffer.
  233. * (17) Set buffer size to actual number of samps left.
  234. * (18) Renormalise.
  235. * (19) Seek to start of ACTUAL buffer in file.
  236. * (20) Write the real number of samps left in buffer.
  237. * (21) Check the arithmetic.
  238. * (22) Restore pointer to original infile, so finish() works correctly.
  239. */
  240. int renormalise(int nctr,dataptr dz)
  241. {
  242. int exit_status;
  243. long n, m = 0;
  244. // long total_samps_read = 0;
  245. long samp_total = dz->total_samps_written, samps_remaining/* , last_total_samps_read*/;
  246. double min_norm = dz->parray[GRS_NORMFACT][0]; /* 1 */
  247. float *s = NULL;
  248. double nf;
  249. long cnt;
  250. /* RWD Nov 2 2010: hack to avoid crash! */
  251. SFPROPS inprops;
  252. int inchans = 0;
  253. if(snd_headread(dz->ifd[0],&inprops)){
  254. inchans = inprops.chans;
  255. }
  256. else {
  257. sprintf(errstr, "WARNING: Can't read infile properties for normalization.\n");
  258. return SYSTEM_ERROR;
  259. }
  260. //TW NEW MECHANISM writes renormalised vals from original file created (tempfile) into true outfile
  261. if(sndcloseEx(dz->ifd[0]) < 0) {
  262. sprintf(errstr, "WARNING: Can't close input soundfile\n");
  263. return(SYSTEM_ERROR);
  264. }
  265. if(sndcloseEx(dz->ofd) < 0) {
  266. sprintf(errstr, "WARNING: Can't close output soundfile\n");
  267. return(SYSTEM_ERROR);
  268. }
  269. if((dz->ifd[0] = sndopenEx(dz->outfilename,0,CDP_OPEN_RDWR)) < 0) { /*RWD Nov 2003 need RDWR to enable sndunlink to work */
  270. sprintf(errstr,"Failure to reopen file %s for renormalisation.\n",dz->outfilename);
  271. return(SYSTEM_ERROR);
  272. }
  273. sndseekEx(dz->ifd[0],0,0);
  274. // if(!sloom)
  275. // dz->wordstor[0][strlen(dz->wordstor[0]) -9] = ENDOFSTR;
  276. if((exit_status = create_sized_outfile(dz->wordstor[0],dz))<0) {
  277. sprintf(errstr,"Failure to create file %s for renormalisation.\n",dz->wordstor[0]);
  278. return(exit_status);
  279. }
  280. if((exit_status = reset_peak_finder(dz))<0)
  281. return(exit_status);
  282. switch(dz->process) {
  283. case(BRASSAGE): s = dz->sampbuf[GRS_OBUF]; break;
  284. case(SAUSAGE): s = dz->sampbuf[SAUS_OBUF]; break;
  285. }
  286. dz->total_samps_written = 0;
  287. for(m=1;m<nctr;m++) {
  288. if((dz->parray[GRS_NORMFACT])[m] < min_norm)
  289. min_norm = (dz->parray[GRS_NORMFACT])[m];
  290. }
  291. if(min_norm < 1.0) {
  292. sprintf(errstr,"Renormalising by %lf\n",min_norm);
  293. print_outmessage_flush(errstr);
  294. for(m=0;m<nctr;m++) /* 2 */
  295. (dz->parray[GRS_NORMFACT])[m] = min_norm/(dz->parray[GRS_NORMFACT])[m];
  296. }
  297. //TW new mechanism: lines not needed
  298. dz->total_samps_read = 0; /* 5 */
  299. display_virtual_time(0L,dz); /* works on dz->total_samps_read here, so param irrelevant */
  300. /* RWD nov 2010 hack, part 2: do this multipLy only if infile is mono */
  301. if(inchans ==1)
  302. dz->buflen *= dz->iparam[GRS_OUTCHANS]; /* 6 */
  303. m = 0; /* 7 */
  304. cnt = dz->buflen;
  305. while(dz->total_samps_read + dz->buflen < samp_total) {
  306. if((exit_status = read_samps(s,dz))<0) {
  307. close_and_delete_tempfile(dz->outfilename,dz);
  308. return(exit_status);
  309. } /* 8 */
  310. // total_samps_read += dz->ssampsread;
  311. if(min_norm < 1.0) {
  312. nf = (dz->parray[GRS_NORMFACT])[m];
  313. for(n=0;n<cnt;n++) /* 9 */
  314. s[n] = /*round*/ (float)((double)s[n] * nf);
  315. m++; /* 10 */
  316. }
  317. if((exit_status = write_samps(s,dz->buflen,dz))<0) {
  318. close_and_delete_tempfile(dz->outfilename,dz);
  319. return(exit_status); /* 12 */
  320. }
  321. } /* 14 */
  322. if((samps_remaining = samp_total - dz->total_samps_read)>0) {
  323. // last_total_samps_read = dz->total_samps_read;
  324. if((exit_status = read_samps(s,dz))<0) {
  325. close_and_delete_tempfile(dz->outfilename,dz);
  326. return(exit_status); /* 16 */
  327. }
  328. dz->buflen = samps_remaining; /* 17 */
  329. if(min_norm < 1.0) {
  330. nf = (dz->parray[GRS_NORMFACT])[m];
  331. for(n=0;n<dz->buflen;n++) /* 18 */
  332. s[n] = /*round*/(float) ((double)s[n] * nf);
  333. }
  334. if(samps_remaining > 0) {
  335. if((exit_status = write_samps(s,samps_remaining,dz))<0) {
  336. close_and_delete_tempfile(dz->outfilename,dz);
  337. return(exit_status); /* 20 */
  338. }
  339. }
  340. }
  341. if(dz->total_samps_written != samp_total) { /* 21 */
  342. sprintf(errstr,"ccounting problem: Renormalise()\n");
  343. close_and_delete_tempfile(dz->outfilename,dz);
  344. return(PROGRAM_ERROR);
  345. }
  346. close_and_delete_tempfile(dz->outfilename,dz);
  347. return(FINISHED);
  348. }
  349. /*************************** GRANULATE *********************************
  350. * iptr = advancing base-pointer in input buffer.
  351. * iiptr = true read-pointer in input buffer.
  352. * absicnt_per_chan = advancing-base-counter in input stream, counting TOTAL samps.
  353. * absiicnt_per_chan = true counter in input stream, counting TOTAL samps to write pos.
  354. * Fptr = advancing base-pointer in output buffer.
  355. * FFptr = true write pointer in output buffer.
  356. * absocnt_per_chan = advancing-base-counter in output stream, measuring OUTsize.
  357. */
  358. /* rwd: some major changes here to clear bugs */
  359. /* tw: some more major changes here to clear implementation errors */
  360. //TW removed redundant 'sr' parameter
  361. int granulate(int *thissnd,long *aipc,long *aopc,float **iiiiptr,float **LLLLptr,
  362. double inv_sr,long *samptotal,float **maxwrite,int *nctr,dataptr dz)
  363. {
  364. int exit_status;
  365. long absicnt_per_chan = *aipc, absocnt_per_chan = *aopc;
  366. float *iiptr, *iptr = *iiiiptr, *endbufptr = NULL, *startbufptr = NULL, *thisbuf = NULL;
  367. float *FFptr, *Fptr = *LLLLptr;
  368. long isauspos, iisauspos;
  369. float aamp = -1;
  370. int bspl = 0, espl = 0, lastsnd;
  371. int firsttime = FALSE;
  372. int gsize_per_chan, ostep_per_chan, istep_per_chan, absiicnt_per_chan, rang_per_chan;
  373. long smpscat_per_chan;
  374. int resetskip = 0;
  375. double time = (double)absicnt_per_chan * inv_sr;
  376. double transpos = 1.0, position;
  377. long saved_total_samps_read = 0;
  378. #ifdef MULTICHAN
  379. int chana, chanb;
  380. #endif
  381. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  382. return(exit_status);
  383. gsize_per_chan = set_ivalue(dz->iparray[GRS_FLAGS][G_GRAINSIZE_FLAG],GRS_GRAINSIZE,GRS_HGRAINSIZE,GRS_GRANGE,dz);
  384. if(absicnt_per_chan + gsize_per_chan >= dz->iparam[ORIG_SMPSIZE])
  385. return(FINISHED);
  386. ostep_per_chan = set_outstep(gsize_per_chan,dz);
  387. if(dz->iparam[GRS_OUTLEN]>0 && (absocnt_per_chan>=dz->iparam[GRS_OUTLEN]))
  388. return(FINISHED); /* IF outfile LENGTH SPECIFIED HAS BEEN MADE: EXIT */
  389. FFptr = Fptr;
  390. if(dz->iparray[GRS_FLAGS][G_SCATTER_FLAG]) { /* If grains scattered, scatter FFptr */
  391. smpscat_per_chan = do_scatter(ostep_per_chan,dz);
  392. FFptr += (smpscat_per_chan * dz->iparam[GRS_OUTCHANS]);
  393. }
  394. if(FFptr < dz->fptr[GRS_LBUF]) {
  395. sprintf(errstr,"Array overrun 1: granula()\n");
  396. return(PROGRAM_ERROR); /* FFptr can be outside the Lbuffer because Fptr is outside it */
  397. }
  398. if(FFptr >= dz->fptr[GRS_LTAILEND]) {
  399. if((exit_status = write_samps_granula(dz->iparam[GRS_LONGS_BUFLEN],nctr,dz))<0)
  400. return(exit_status);
  401. /* IF CURRENT POINTER AT END OF LBUF WRITE SAMPS, WRAP AROUND POINTERS */
  402. memmove((char *)dz->fptr[GRS_LBUF],(char *)dz->fptr[GRS_LBUFEND],
  403. dz->iparam[GRS_LBUF_SMPXS] * sizeof(float));
  404. memset((char *)dz->fptr[GRS_LBUFMID],0,dz->iparam[GRS_LONGS_BUFLEN] * sizeof(float));
  405. FFptr -= dz->iparam[GRS_LONGS_BUFLEN];
  406. if((Fptr -= dz->iparam[GRS_LONGS_BUFLEN]) < dz->fptr[GRS_LBUF]) {
  407. sprintf(errstr,"Array overrun 2: granula()\n");
  408. return(PROGRAM_ERROR);
  409. }
  410. *maxwrite -= dz->iparam[GRS_LONGS_BUFLEN];
  411. }
  412. istep_per_chan = set_instep(ostep_per_chan,dz);
  413. if(istep_per_chan==0 && dz->iparam[GRS_OUTLEN]==0) {
  414. sprintf(errstr,"velocity or instep has become so small that file will be infinitely long!!\n"
  415. "Try slightly increasing your very small values.\n");
  416. return(GOAL_FAILED);
  417. }
  418. iiptr = iptr;
  419. if(dz->process==SAUSAGE) {
  420. lastsnd = *thissnd;
  421. isauspos = iptr - dz->sampbuf[SAUS_BUF(lastsnd)];
  422. iisauspos = iiptr - dz->sampbuf[SAUS_BUF(lastsnd)];
  423. *thissnd = get_next_insnd(dz);
  424. iiptr = dz->sampbuf[SAUS_BUF(*thissnd)] + iisauspos;
  425. iptr = dz->sampbuf[SAUS_BUF(*thissnd)] + isauspos;
  426. }
  427. absiicnt_per_chan = absicnt_per_chan;
  428. if(dz->iparray[GRS_FLAGS][G_RANGE_FLAG]) {
  429. rang_per_chan = set_range(absiicnt_per_chan,dz); /* RESET iiptr etc WITHIN SEARCHRANGE */
  430. absiicnt_per_chan -= rang_per_chan;
  431. iiptr -= rang_per_chan * dz->iparam[GRS_INCHANS];
  432. }
  433. switch(dz->process) {
  434. case(BRASSAGE):
  435. endbufptr = dz->sbufptr[GRS_BUF];
  436. startbufptr = dz->sampbuf[GRS_BUF];
  437. break;
  438. case(SAUSAGE):
  439. endbufptr = dz->sbufptr[SAUS_BUF(*thissnd)];
  440. startbufptr = dz->sampbuf[SAUS_BUF(*thissnd)];
  441. break;
  442. }
  443. if(iiptr >= endbufptr) {
  444. while(iiptr >= endbufptr) {
  445. switch(dz->process) {
  446. case(BRASSAGE):
  447. if((read_samps_granula(firsttime,dz))<0) /* IF iiptr OFF END OF IBUF */
  448. return(exit_status);
  449. break;
  450. case(SAUSAGE):
  451. if((read_samps_sausage(firsttime,dz))<0) /* IF iiptr OFF END OF IBUF */
  452. return(exit_status);
  453. break;
  454. }
  455. *samptotal += dz->ssampsread;
  456. if(dz->ssampsread<=0)
  457. return(FINISHED);
  458. /* READ SAMPS, WRAP BACK POINTER */
  459. iiptr -= dz->buflen;
  460. iptr -= dz->buflen;
  461. }
  462. } else if(iiptr < startbufptr) { /* IF RANGE TAKES US BAK OUT OF THIS BUF, */
  463. if(sloom)
  464. saved_total_samps_read = dz->total_samps_read; /* saved so display_virtual_time() works during baktrak!! */
  465. if((resetskip = *samptotal - dz->iparam[SAMPS_IN_INBUF])<0) { /* SET RESETSKIP TO START OF CURRENT BUFFER */
  466. sprintf(errstr,"Error in baktraking: granula()\n");
  467. return(PROGRAM_ERROR);
  468. }
  469. switch(dz->process) {
  470. case(BRASSAGE):
  471. if((exit_status = baktrak_granula(*samptotal,absiicnt_per_chan,&iiptr,dz))<0)
  472. return(exit_status); /* SEEK BACKWDS, & RESET iiptr In NEW BUF */
  473. break;
  474. case(SAUSAGE):
  475. if((exit_status = baktrak_sausage(*thissnd,*samptotal,absiicnt_per_chan,&iiptr,dz))<0)
  476. return(exit_status); /* SEEK BACKWDS, & RESET iiptr In NEW BUF */
  477. break;
  478. }
  479. if(sloom)
  480. dz->total_samps_read = saved_total_samps_read; /* restore so its not changed by the baktraking!! */
  481. }
  482. if(dz->iparray[GRS_FLAGS][G_AMP_FLAG])
  483. /* RWD was set_ivalue*/
  484. aamp = (float) set_dvalue(dz->iparray[GRS_FLAGS][G_AMP_FLAG],GRS_AMP,GRS_HAMP,GRS_ARANGE,dz);
  485. /* GET SPLICE VALUES */
  486. bspl = set_ivalue(dz->iparray[GRS_FLAGS][G_BSPLICE_FLAG],GRS_BSPLICE,GRS_HBSPLICE,GRS_BRANGE,dz);
  487. espl = set_ivalue(dz->iparray[GRS_FLAGS][G_ESPLICE_FLAG],GRS_ESPLICE,GRS_HESPLICE,GRS_ERANGE,dz);
  488. switch(dz->process) {
  489. case(BRASSAGE): thisbuf = dz->sampbuf[GRS_BUF]; break;
  490. case(SAUSAGE): thisbuf = dz->sampbuf[SAUS_BUF(*thissnd)]; break;
  491. }
  492. #ifndef MULTICHAN
  493. switch(dz->iparam[GRS_INCHANS]) {
  494. case(1):
  495. if(!make_grain(thisbuf,&iiptr,aamp,gsize_per_chan,&transpos,dz))
  496. return(FINISHED); /* COPYGRAIN TO GRAINBUF,INCLUDING ANY PSHIFT */
  497. do_splices(gsize_per_chan,bspl,espl,dz); /* DO SPLICES IN GRAINBUF */
  498. break;
  499. case(2):
  500. if(!make_stereo_grain(thisbuf,&iiptr,aamp,gsize_per_chan,&transpos,dz))
  501. return(FINISHED); /* COPYGRAIN TO GRAINBUF,INCLUDING ANY PSHIFT */
  502. do_stereo_splices(gsize_per_chan,bspl,espl,dz); /* DO SPLICES IN GRAINBUF */
  503. break;
  504. }
  505. #else
  506. if(!make_multichan_grain(thisbuf,&iiptr,aamp,gsize_per_chan,&transpos,dz))
  507. return(FINISHED); /* COPYGRAIN TO GRAINBUF,INCLUDING ANY PSHIFT */
  508. do_multichan_splices(gsize_per_chan,bspl,espl,dz); /* DO SPLICES IN GRAINBUF */
  509. #endif
  510. if(dz->iparray[GRS_FLAGS][G_SPACE_FLAG]) { /* IF SPATIAL INFO, GET IT,WRITE STEREO GRAIN */
  511. position = set_dvalue(dz->iparray[GRS_FLAGS][G_SPACE_FLAG],GRS_SPACE,GRS_HSPACE,GRS_SPRANGE,dz);
  512. #ifndef MULTICHAN
  513. if((exit_status = write_stereo_grain(position,maxwrite,&Fptr,&FFptr,gsize_per_chan,nctr,dz))<0)
  514. return(exit_status);
  515. #else
  516. if(dz->out_chans > 2) {
  517. chana = (int)floor(position);
  518. position -= (double)chana; // position is relative position between 2 adjacent out-chans
  519. chanb = chana + 1; // chanb is adjacent to chana
  520. if(chana > dz->out_chans) // chana beyond last lspkr wraps around to 1st lspkr
  521. chana = 1;
  522. else if(chana < 1) // chana below 1st loudspeaker wraps around to last-lspkr
  523. chana = dz->out_chans;
  524. if((exit_status = write_multichan_grain(position,chana,chanb,maxwrite,&Fptr,&FFptr,gsize_per_chan,nctr,dz))<0)
  525. return(exit_status);
  526. } else {
  527. if((exit_status = write_stereo_grain(position,maxwrite,&Fptr,&FFptr,gsize_per_chan,nctr,dz))<0)
  528. return(exit_status);
  529. }
  530. #endif
  531. } else { /* WRITE GRAIN */
  532. if((exit_status = write_grain(maxwrite,&Fptr,&FFptr,gsize_per_chan,nctr,dz))<0)
  533. return(exit_status);
  534. }
  535. if(sloom)
  536. saved_total_samps_read = dz->total_samps_read; /* saved so not disturbed by restoring of baktrakd-from buffer */
  537. switch(dz->process) {
  538. case(BRASSAGE): /* IF BAKTRAKD : RESTORE ORIGINAL BUFFER */
  539. if(resetskip && (exit_status = reset_granula(resetskip,dz))<0)
  540. return(exit_status);
  541. break;
  542. case(SAUSAGE):
  543. if(resetskip && (exit_status = reset_sausage(*thissnd,resetskip,dz))<0)
  544. return(exit_status);
  545. break;
  546. }
  547. if(sloom)
  548. dz->total_samps_read = saved_total_samps_read;
  549. #ifndef MULTICHAN
  550. iptr += istep_per_chan; /* MOVE FORWARD IN INFILE */
  551. if(dz->iparam[GRS_INCHANS]==STEREO)
  552. iptr += istep_per_chan; /* move further if using STEREO INPUT DATA */
  553. #else
  554. iptr += (istep_per_chan * dz->iparam[GRS_INCHANS]); /* MOVE FORWARD IN postmixdown input-buffer, which is either MONO or multichannel */
  555. #endif
  556. absicnt_per_chan += istep_per_chan; /* rwd: moved here from top of input section */
  557. #ifndef MULTICHAN
  558. Fptr += ostep_per_chan; /* Fptr may still be outside (bottom of) Lbuf */
  559. if(dz->iparray[GRS_FLAGS][G_SPACE_FLAG] || dz->iparam[GRS_INCHANS]==STEREO)
  560. Fptr += ostep_per_chan; /* Advance by stereo step, for STEREO OUTPUT */
  561. #else
  562. Fptr += ostep_per_chan * dz->iparam[GRS_OUTCHANS]; /* Move forward in output buffer, which can be mono, spatialised-stereo or multichannel */
  563. #endif
  564. /* so we don't lose first grain */
  565. absocnt_per_chan += ostep_per_chan;
  566. *aipc = absicnt_per_chan; /* RETURN VALUES OF RETAINED VARIABLES */
  567. *aopc = absocnt_per_chan;
  568. *iiiiptr = iptr;
  569. *LLLLptr = Fptr;
  570. return(CONTINUE);
  571. }
  572. /*************************** BAKTRAK_GRANULA ******************************
  573. *
  574. * <------------ baktrak(b) ---------------->
  575. *
  576. * <--(b-x)-->
  577. * <------------ x -------------->
  578. * |-------- current bufer --------|
  579. *
  580. * |-------- new buffer --------|
  581. * <------------x------------->
  582. *
  583. */
  584. #ifndef MULTICHAN
  585. int baktrak_granula(long samptotal,int absiicnt_per_chan,float **iiptr,dataptr dz)
  586. {
  587. int exit_status;
  588. long bktrk, new_position;
  589. unsigned long reset_size = dz->buflen + dz->iparam[GRS_BUF_SMPXS];
  590. bktrk = samptotal - (absiicnt_per_chan * dz->iparam[GRS_INCHANS]);
  591. //TW I agree, this is FINE!!, and, with sndseekEx, algo is more efficient
  592. // whenever the search-range is relatively small (and equally efficient otherwise)
  593. *iiptr = dz->sampbuf[GRS_BUF];
  594. if((new_position = samptotal - bktrk)<0) {
  595. fprintf(stdout,"WARNING: Non-fatal program error:\nRange arithmetic problem - 2, in baktraking.\n");
  596. fflush(stdout);
  597. new_position = 0;
  598. *iiptr = dz->sampbuf[GRS_BUF];
  599. }
  600. if(dz->iparam[GRS_CHAN_TO_XTRACT]) /* IF we've converted from STEREO file */
  601. new_position *= 2;
  602. if(sndseekEx(dz->ifd[0],new_position,0)<0) {
  603. sprintf(errstr,"sndseekEx error: baktrak_granula()\n");
  604. return(SYSTEM_ERROR);
  605. }
  606. memset((char *)dz->sampbuf[GRS_BUF],0,reset_size * sizeof(float));
  607. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  608. reset_size *= 2;
  609. memset((char *)dz->sampbuf[GRS_SBUF],0,reset_size * sizeof(float));
  610. }
  611. if((exit_status = read_a_large_buf(dz))<0)
  612. return(exit_status);
  613. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread;
  614. return(FINISHED);
  615. }
  616. #else
  617. int baktrak_granula(long samptotal,int absiicnt_per_chan,float **iiptr,dataptr dz)
  618. {
  619. int exit_status, chans = dz->infile->channels;
  620. long bktrk, new_position;
  621. unsigned long reset_size = dz->buflen + dz->iparam[GRS_BUF_SMPXS];
  622. bktrk = samptotal - (absiicnt_per_chan * dz->iparam[GRS_INCHANS]);
  623. //TW I agree, this is FINE!!, and, with sndseekEx, algo is more efficient
  624. // whenever the search-range is relatively small (and equally efficient otherwise)
  625. *iiptr = dz->sampbuf[GRS_BUF];
  626. if((new_position = samptotal - bktrk)<0) {
  627. fprintf(stdout,"WARNING: Non-fatal program error:\nRange arithmetic problem - 2, in baktraking.\n");
  628. fflush(stdout);
  629. new_position = 0;
  630. *iiptr = dz->sampbuf[GRS_BUF];
  631. }
  632. if(dz->iparam[GRS_CHAN_TO_XTRACT]) /* IF we've converted from STEREO file */
  633. new_position *= chans;
  634. if(sndseekEx(dz->ifd[0],new_position,0)<0) {
  635. sprintf(errstr,"sndseekEx error: baktrak_granula()\n");
  636. return(SYSTEM_ERROR);
  637. }
  638. memset((char *)dz->sampbuf[GRS_BUF],0,reset_size * sizeof(float));
  639. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  640. reset_size *= chans;
  641. memset((char *)dz->sampbuf[GRS_SBUF],0,reset_size * sizeof(float));
  642. }
  643. if((exit_status = read_a_large_buf(dz))<0)
  644. return(exit_status);
  645. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread;
  646. return(FINISHED);
  647. }
  648. #endif
  649. /*************************** READ_A_LARGE_BUF ******************************/
  650. #ifndef MULTICHAN
  651. int read_a_large_buf(dataptr dz)
  652. {
  653. int exit_status;
  654. long n, m, k;
  655. dz->buflen += dz->iparam[GRS_BUF_SMPXS];
  656. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  657. dz->buflen *= 2;
  658. if((exit_status = read_samps(dz->sampbuf[GRS_SBUF],dz))<0)
  659. return(exit_status);
  660. dz->buflen /= 2;
  661. dz->ssampsread /= 2;
  662. switch(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  663. case(1):
  664. for(n=0,m=0;n<dz->ssampsread;n++,m+=2)
  665. (dz->sampbuf[GRS_BUF])[n] = (dz->sampbuf[GRS_SBUF])[m];
  666. break;
  667. case(2):
  668. for(n=0,m=1;n<dz->ssampsread;n++,m+=2)
  669. (dz->sampbuf[GRS_BUF])[n] = (dz->sampbuf[GRS_SBUF])[m];
  670. break;
  671. case(BOTH_CHANNELS):
  672. for(n=0,m=0,k=1;n<dz->ssampsread;n++,m+=2,k+=2)
  673. (dz->sampbuf[GRS_BUF])[n] = (float)(((dz->sampbuf[GRS_SBUF])[m] + (dz->sampbuf[GRS_SBUF])[k]) * 0.5f);
  674. break;
  675. }
  676. } else {
  677. if((exit_status = read_samps(dz->sampbuf[GRS_BUF],dz))<0)
  678. return(exit_status);
  679. }
  680. dz->buflen -= dz->iparam[GRS_BUF_SMPXS];
  681. return(FINISHED);
  682. }
  683. #else
  684. int read_a_large_buf(dataptr dz)
  685. {
  686. int exit_status;
  687. long n, m, ibufcnt;
  688. int chans = dz->infile->channels;
  689. double sum;
  690. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  691. dz->buflen += dz->iparam[GRS_BUF_SMPXS];
  692. dz->buflen *= chans;
  693. if((exit_status = read_samps(dz->sampbuf[GRS_SBUF],dz))<0)
  694. return(exit_status);
  695. dz->buflen /= chans;
  696. dz->buflen -= dz->iparam[GRS_BUF_SMPXS];
  697. dz->ssampsread /= chans;
  698. switch(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  699. case(ALL_CHANNELS):
  700. ibufcnt = 0;
  701. for(n=0;n<dz->ssampsread;n++) {
  702. sum = 0.0;
  703. for(m=0;m<chans;m++)
  704. sum += dz->sampbuf[GRS_SBUF][ibufcnt++];
  705. dz->sampbuf[GRS_BUF][n] = (float)(sum/(double)chans);
  706. }
  707. break;
  708. default:
  709. for(n=0,m=dz->iparam[GRS_CHAN_TO_XTRACT];n<dz->ssampsread;n++,m+=chans)
  710. dz->sampbuf[GRS_BUF][n] = dz->sampbuf[GRS_SBUF][m];
  711. break;
  712. }
  713. } else {
  714. dz->buflen += dz->iparam[GRS_BUF_SMPXS];
  715. if((exit_status = read_samps(dz->sampbuf[GRS_BUF],dz))<0)
  716. return(exit_status);
  717. dz->buflen -= dz->iparam[GRS_BUF_SMPXS];
  718. }
  719. return(FINISHED);
  720. }
  721. #endif
  722. /*************************** READ_A_NORMAL_BUF_WITH_WRAPAROUND ******************************/
  723. #ifndef MULTICHAN
  724. int read_a_normal_buf_with_wraparound(dataptr dz)
  725. {
  726. int exit_status;
  727. long n,m,k;
  728. memmove((char *)dz->sampbuf[GRS_BUF],(char *)(dz->sbufptr[GRS_BUF]),
  729. dz->iparam[GRS_BUF_SMPXS] * sizeof(float));
  730. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  731. dz->buflen *= 2;
  732. if((exit_status = read_samps(dz->sampbuf[GRS_SBUF],dz))<0)
  733. return(exit_status);
  734. dz->buflen /= 2;
  735. dz->ssampsread /= 2;
  736. switch(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  737. case(1):
  738. for(n=0,m=0;n<dz->ssampsread;n++,m+=2)
  739. (dz->sampbuf[GRS_IBUF])[n] = (dz->sampbuf[GRS_SBUF])[m];
  740. break;
  741. case(2):
  742. for(n=0,m=1;n<dz->ssampsread;n++,m+=2)
  743. (dz->sampbuf[GRS_IBUF])[n] = (dz->sampbuf[GRS_SBUF])[m];
  744. break;
  745. case(BOTH_CHANNELS):
  746. for(n=0,m=0,k=1;n<dz->ssampsread;n++,m+=2,k+=2)
  747. (dz->sampbuf[GRS_IBUF])[n] = (float)(((dz->sampbuf[GRS_SBUF])[m] + (dz->sampbuf[GRS_SBUF])[k]) * 0.5f);
  748. break;
  749. }
  750. } else {
  751. if((exit_status = read_samps(dz->sampbuf[GRS_IBUF],dz))<0)
  752. return(exit_status);
  753. }
  754. return(FINISHED);
  755. }
  756. #else
  757. int read_a_normal_buf_with_wraparound(dataptr dz)
  758. {
  759. int exit_status;
  760. long n,m,ibufcnt;
  761. double sum;
  762. int chans = dz->infile->channels;
  763. memmove((char *)dz->sampbuf[GRS_BUF],(char *)(dz->sbufptr[GRS_BUF]),
  764. dz->iparam[GRS_BUF_SMPXS] * sizeof(float));
  765. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  766. dz->buflen *= chans;
  767. if((exit_status = read_samps(dz->sampbuf[GRS_SBUF],dz))<0)
  768. return(exit_status);
  769. dz->buflen /= chans;
  770. dz->ssampsread /= chans;
  771. switch(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  772. case(ALL_CHANNELS):
  773. ibufcnt = 0;
  774. for(n=0;n<dz->ssampsread;n++) {
  775. sum = 0.0;
  776. for(m=0;m<chans;m++)
  777. sum += dz->sampbuf[GRS_SBUF][ibufcnt++];
  778. dz->sampbuf[GRS_IBUF][n] = (float)(sum/(double)chans);
  779. }
  780. break;
  781. default:
  782. for(n=0,m=dz->iparam[GRS_CHAN_TO_XTRACT];n<dz->ssampsread;n++,m+=chans)
  783. dz->sampbuf[GRS_IBUF][n] = dz->sampbuf[GRS_SBUF][m];
  784. break;
  785. }
  786. } else {
  787. if((exit_status = read_samps(dz->sampbuf[GRS_IBUF],dz))<0)
  788. return(exit_status);
  789. }
  790. return(FINISHED);
  791. }
  792. #endif
  793. /****************************** RESET_GRANULA *******************************/
  794. #ifndef MULTICHAN
  795. int reset_granula(int resetskip,dataptr dz)
  796. {
  797. unsigned long reset_size = dz->buflen + dz->iparam[GRS_BUF_SMPXS];
  798. memset((char *)dz->sampbuf[GRS_BUF],0,reset_size * sizeof(float));
  799. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  800. reset_size *= 2;
  801. memset((char *)dz->sampbuf[GRS_SBUF],0,reset_size * sizeof(float));
  802. }
  803. if(dz->iparam[GRS_CHAN_TO_XTRACT]) /* If we've converted from a multichannel file */
  804. resetskip *= 2;
  805. if(sndseekEx(dz->ifd[0],resetskip,0)<0) {
  806. sprintf(errstr,"sndseek error: reset_granula()\n");
  807. return(SYSTEM_ERROR);
  808. }
  809. return read_a_large_buf(dz);
  810. }
  811. #else
  812. int reset_granula(int resetskip,dataptr dz)
  813. {
  814. unsigned long reset_size = dz->buflen + dz->iparam[GRS_BUF_SMPXS];
  815. memset((char *)dz->sampbuf[GRS_BUF],0,reset_size * sizeof(float));
  816. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  817. reset_size *= dz->infile->channels;
  818. memset((char *)dz->sampbuf[GRS_SBUF],0,reset_size * sizeof(float));
  819. }
  820. if(dz->iparam[GRS_CHAN_TO_XTRACT]) /* If we've converted from a multichannel file */
  821. resetskip *= dz->infile->channels;
  822. if(sndseekEx(dz->ifd[0],resetskip,0)<0) {
  823. sprintf(errstr,"sndseek error: reset_granula()\n");
  824. return(SYSTEM_ERROR);
  825. }
  826. return read_a_large_buf(dz);
  827. }
  828. #endif
  829. /************************** DO_SPLICES *****************************/
  830. void do_splices(int gsize_per_chan,int bspl,int espl,dataptr dz)
  831. {
  832. if(dz->iparam[GRS_IS_BTAB] && (dz->iparam[GRS_BSPLICE] < gsize_per_chan/2))
  833. do_btab_splice(dz);
  834. else
  835. do_bsplice(gsize_per_chan,dz,bspl);
  836. if(dz->iparam[GRS_IS_ETAB] && (dz->iparam[GRS_ESPLICE] < gsize_per_chan/2))
  837. do_etab_splice(gsize_per_chan,dz);
  838. else
  839. do_esplice(gsize_per_chan,dz,espl);
  840. }
  841. /************************** DO_BTAB_SPLICE ****************************/
  842. void do_btab_splice(dataptr dz)
  843. {
  844. long n, k = dz->iparam[GRS_BSPLICE];
  845. double *d;
  846. float *gbufptr = dz->extrabuf[GRS_GBUF];
  847. if(k==0)
  848. return;
  849. d = dz->parray[GRS_BSPLICETAB];
  850. for(n=0;n<k;n++) {
  851. *gbufptr = (float)/*round*/(*gbufptr * *d);
  852. gbufptr++;
  853. d++;
  854. }
  855. }
  856. /************************** DO_BSPLICE ****************************
  857. *
  858. * rwd: changed to avoid f/p division, added exponential option
  859. */
  860. void do_bsplice(int gsize_per_chan,dataptr dz,int bspl)
  861. {
  862. double dif,val,lastval,length,newsum,lastsum,twodif;
  863. long n, k = min(bspl,gsize_per_chan);
  864. float *gbufptr = dz->extrabuf[GRS_GBUF];
  865. if(k==0)
  866. return;
  867. val = 0.0;
  868. length = (double)k;
  869. if(!dz->vflag[GRS_EXPON]){
  870. dif = 1.0/length;
  871. lastval = dif;
  872. *gbufptr++ = (float)val;
  873. *gbufptr = (float)/*round*/(*gbufptr * lastval);
  874. gbufptr++;
  875. for(n=2;n<k;n++) {
  876. val = lastval + dif;
  877. lastval = val;
  878. *gbufptr = (float) /*round*/(*gbufptr * val);
  879. gbufptr++;
  880. }
  881. } else { /* fast quasi-exponential */
  882. dif = 1.0/(length*length);
  883. twodif = dif * 2.0;
  884. lastsum = 0.0;
  885. lastval = dif;
  886. *gbufptr++ = (float)val;
  887. *gbufptr = (float) /*round*/(*gbufptr * lastval);
  888. gbufptr++;
  889. for(n=2;n<k;n++) {
  890. newsum = lastsum + twodif;
  891. val = lastval + newsum + dif;
  892. *gbufptr = (float) /*round*/(*gbufptr * val);
  893. gbufptr++;
  894. lastval = val;
  895. lastsum = newsum;
  896. }
  897. }
  898. }
  899. /************************** DO_ETAB_SPLICE ****************************/
  900. void do_etab_splice(int gsize_per_chan,dataptr dz)
  901. {
  902. long n, k = (long)dz->iparam[GRS_ESPLICE];
  903. double *d;
  904. float *gbufptr = dz->extrabuf[GRS_GBUF] + gsize_per_chan - k;
  905. if(k==0)
  906. return;
  907. d = dz->parray[GRS_ESPLICETAB];
  908. for(n=0;n<k;n++) {
  909. *gbufptr = (float) /*round*/(*gbufptr * *d);
  910. gbufptr++;
  911. d++;
  912. }
  913. }
  914. /************************** DO_ESPLICE ****************************/
  915. /* rwd: changed to avoid f/p division, added exponential option */
  916. void do_esplice(int gsize_per_chan,dataptr dz,int espl)
  917. {
  918. double dif,val,lastval,length,newsum,lastsum,twodif;
  919. long n, k = min(espl,gsize_per_chan);
  920. float *gbufptr = dz->extrabuf[GRS_GBUF] + gsize_per_chan;
  921. if(k==0)
  922. return;
  923. val = 0.0;
  924. length = (double) k;
  925. if(!dz->vflag[GRS_EXPON]) {
  926. dif = 1.0/length;
  927. lastsum = dif;
  928. *--gbufptr = (float)val;
  929. gbufptr--;
  930. *gbufptr = (float) /*round*/(*gbufptr * lastsum);
  931. for(n=k-3;n>=0;n--) {
  932. val = lastsum + dif;
  933. lastsum = val;
  934. gbufptr--;
  935. *gbufptr = (float) /*round*/(*gbufptr * val);
  936. }
  937. } else { /* fast quasi-exponential */
  938. dif = 1.0/(length * length);
  939. twodif = dif * 2.0;
  940. lastsum = 0.0;
  941. lastval = dif;
  942. *--gbufptr = (float)val;
  943. gbufptr--;
  944. *gbufptr = (float) /*round*/(*gbufptr * lastval);
  945. for(n=k-3;n>=0;n--) {
  946. newsum =lastsum + twodif;
  947. val = lastval + newsum + dif;
  948. gbufptr--;
  949. *gbufptr = (float) /*round*/ (*gbufptr * val);
  950. lastval = val;
  951. lastsum = newsum;
  952. }
  953. }
  954. }
  955. /***************************** MAKE_GRAIN ***************************
  956. * iicnt = RELATIVE read-pointer in input buffer.
  957. */
  958. int make_grain(float *b,float **iiptr,float aamp,int gsize_per_chan,double *transpos,dataptr dz)
  959. { /* rwd: added interpolation option */
  960. long n,real_gsize = gsize_per_chan;
  961. double flcnt;
  962. long iicnt;
  963. double flcnt_frac;
  964. float *s = *iiptr, *gbufptr = dz->extrabuf[GRS_GBUF];
  965. if(aamp>=0) {
  966. if(!dz->iparray[GRS_FLAGS][G_PITCH_FLAG]) {
  967. if(real_gsize >= (long)dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  968. return(0);
  969. for(n=0;n<real_gsize;n++) /* COPY GRAIN TO GRAINBUF & RE-LEVEL ETC */
  970. *gbufptr++ = (float)(*s++ * aamp);
  971. } else {
  972. iicnt = s - b;
  973. *transpos = set_dvalue(dz->iparray[GRS_FLAGS][G_PITCH_FLAG],GRS_PITCH,GRS_HPITCH,GRS_PRANGE,dz);
  974. *transpos = pow(2.0,*transpos);
  975. if((int)(real_gsize * (*transpos))+1+iicnt >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  976. return(0);
  977. flcnt = (double)iicnt;
  978. if(!dz->vflag[GRS_NO_INTERP]){
  979. flcnt_frac = flcnt - (double)iicnt;
  980. for(n=0;n<real_gsize;n++) {
  981. *gbufptr++ = interp_gval_with_amp(s,flcnt_frac,dz->iparam[GRS_INCHANS],aamp);
  982. flcnt += *transpos;
  983. iicnt = (long)flcnt;
  984. s = b + iicnt;
  985. flcnt_frac = flcnt - (double) iicnt;
  986. }
  987. } else { /* do truncate as originally */
  988. for(n=0;n<real_gsize;n++){
  989. *gbufptr++ = (float)(*s * aamp);
  990. flcnt += *transpos;
  991. iicnt = round(flcnt);
  992. s = b + iicnt;
  993. }
  994. }
  995. }
  996. } else { /* NO CHANGE IN AMPLITUDE */
  997. if(!dz->iparray[GRS_FLAGS][G_PITCH_FLAG]) {
  998. if(real_gsize >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  999. return(0);
  1000. for(n=0;n<real_gsize;n++)
  1001. *gbufptr++ = *s++;
  1002. } else {
  1003. iicnt = s - b;
  1004. *transpos = set_dvalue(dz->iparray[GRS_FLAGS][G_PITCH_FLAG],GRS_PITCH,GRS_HPITCH,GRS_PRANGE,dz);
  1005. *transpos = pow(2.0,*transpos);
  1006. if((int)(real_gsize * (*transpos))+1+iicnt >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1007. return(0);
  1008. flcnt = (double)iicnt;
  1009. if(dz->vflag[GRS_NO_INTERP]){ /* do truncate as originally */
  1010. for(n=0;n<gsize_per_chan;n++){
  1011. *gbufptr++ = *s;
  1012. flcnt += *transpos;
  1013. iicnt = round(flcnt);
  1014. s = b + iicnt;
  1015. }
  1016. } else {
  1017. flcnt_frac = flcnt - (double)iicnt;
  1018. for(n=0;n<real_gsize;n++) {
  1019. *gbufptr++ = interp_gval(s,flcnt_frac,dz->iparam[GRS_INCHANS]);
  1020. flcnt += *transpos;
  1021. iicnt = (long)flcnt;
  1022. s = b + iicnt;
  1023. flcnt_frac = flcnt - (double)iicnt;
  1024. }
  1025. }
  1026. }
  1027. }
  1028. *iiptr = s;
  1029. return(1);
  1030. }
  1031. /***************************** MAKE_STEREO_GRAIN ***************************
  1032. * iicnt = RELATIVE read-pointer in input buffer (starts at 0).
  1033. */
  1034. int make_stereo_grain(float *b,float **iiptr,float aamp,int gsize_per_chan,double *transpos,dataptr dz)
  1035. {
  1036. long n;
  1037. double flcnt;
  1038. long iicnt, real_gsize = gsize_per_chan * 2;
  1039. double flcnt_frac;
  1040. float *s = *iiptr, *gbufptr = dz->extrabuf[GRS_GBUF];
  1041. if(aamp>=0) {
  1042. if(!dz->iparray[GRS_FLAGS][G_PITCH_FLAG]) {
  1043. if(real_gsize >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1044. return(0);
  1045. for(n=0;n<real_gsize;n++) /* COPY GRAIN TO GRAINBUF & RE-LEVEL ETC */
  1046. *gbufptr++ = (*s++ * aamp);
  1047. } else {
  1048. iicnt = (s - b)/2;
  1049. *transpos = set_dvalue(dz->iparray[GRS_FLAGS][G_PITCH_FLAG],GRS_PITCH,GRS_HPITCH,GRS_PRANGE,dz);
  1050. *transpos = pow(2.0,*transpos);
  1051. if((((int)(gsize_per_chan * *transpos)+1) + iicnt) * 2 >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1052. return(0);
  1053. flcnt = (double)iicnt;
  1054. if(dz->vflag[GRS_NO_INTERP]){ /* do truncate as originally */
  1055. for(n=0;n<gsize_per_chan;n++){
  1056. *gbufptr++ = (*s * aamp);
  1057. s++;
  1058. *gbufptr++ = (*s * aamp);
  1059. flcnt += *transpos;
  1060. iicnt = round(flcnt);
  1061. s = b + (iicnt * 2);
  1062. }
  1063. } else {
  1064. flcnt_frac = flcnt - (double)iicnt;
  1065. for(n=0;n<gsize_per_chan;n++) {
  1066. *gbufptr++ = interp_gval_with_amp(s,flcnt_frac,dz->iparam[GRS_INCHANS],aamp);
  1067. s++;
  1068. *gbufptr++ = interp_gval_with_amp(s,flcnt_frac,dz->iparam[GRS_INCHANS],aamp);
  1069. flcnt += *transpos;
  1070. iicnt = (long)flcnt;
  1071. s = b + (iicnt * 2);
  1072. flcnt_frac = flcnt - (double) iicnt;
  1073. }
  1074. }
  1075. }
  1076. } else { /* NO CHANGE IN AMPLITUDE */
  1077. if(!dz->iparray[GRS_FLAGS][G_PITCH_FLAG]) {
  1078. if(real_gsize >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1079. return(0);
  1080. for(n=0;n<real_gsize;n++)
  1081. *gbufptr++ = *s++;
  1082. } else {
  1083. iicnt = (s - b)/2;
  1084. *transpos = set_dvalue(dz->iparray[GRS_FLAGS][G_PITCH_FLAG],GRS_PITCH,GRS_HPITCH,GRS_PRANGE,dz);
  1085. *transpos = pow(2.0,*transpos);
  1086. if((((int)(gsize_per_chan * *transpos)+1) + iicnt) * 2 >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1087. return(0);
  1088. flcnt = (double)iicnt;
  1089. if(!dz->vflag[GRS_NO_INTERP]){
  1090. flcnt_frac = flcnt - (double)iicnt;
  1091. for(n=0;n<gsize_per_chan;n++) {
  1092. *gbufptr++ = interp_gval(s,flcnt_frac,dz->iparam[GRS_INCHANS]);
  1093. s++;
  1094. *gbufptr++ = interp_gval(s,flcnt_frac,dz->iparam[GRS_INCHANS]);
  1095. flcnt += *transpos;
  1096. iicnt = (long)flcnt;
  1097. s = b + (iicnt * 2);
  1098. flcnt_frac = flcnt - (double)iicnt;
  1099. }
  1100. } else { /* do truncate as originally */
  1101. for(n=0;n<gsize_per_chan;n++){
  1102. *gbufptr++ = *s++;
  1103. *gbufptr++ = *s;
  1104. flcnt += *transpos;
  1105. iicnt = round(flcnt);
  1106. s = b + (iicnt * 2);
  1107. }
  1108. }
  1109. }
  1110. }
  1111. *iiptr = s;
  1112. return(1);
  1113. }
  1114. /***************************** MAKE_MULTICHAN_GRAIN ***************************
  1115. * iicnt = RELATIVE read-pointer in input buffer (starts at 0).
  1116. */
  1117. int make_multichan_grain(float *b,float **iiptr,float aamp,int gsize_per_chan,double *transpos,dataptr dz)
  1118. {
  1119. long n, k;
  1120. int chans = dz->iparam[GRS_INCHANS]; // GRS_INCHANS = no of chans in input grain, i.e. mono if its been mixed down to mono
  1121. double flcnt; // but = dz->infile->channels, if not
  1122. long iicnt, real_gsize = gsize_per_chan * chans;
  1123. double flcnt_frac;
  1124. float *s = *iiptr, *gbufptr = dz->extrabuf[GRS_GBUF];
  1125. if(aamp>=0) {
  1126. if(!dz->iparray[GRS_FLAGS][G_PITCH_FLAG]) {
  1127. if(real_gsize >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1128. return(0);
  1129. for(n=0;n<real_gsize;n++) /* COPY GRAIN TO GRAINBUF & RE-LEVEL ETC */
  1130. *gbufptr++ = (*s++ * aamp);
  1131. } else {
  1132. iicnt = (s - b)/chans;
  1133. *transpos = set_dvalue(dz->iparray[GRS_FLAGS][G_PITCH_FLAG],GRS_PITCH,GRS_HPITCH,GRS_PRANGE,dz);
  1134. *transpos = pow(2.0,*transpos);
  1135. if((((int)(gsize_per_chan * *transpos)+1) + iicnt) * chans >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1136. return(0);
  1137. flcnt = (double)iicnt;
  1138. if(dz->vflag[GRS_NO_INTERP]){ /* do truncate as originally */
  1139. for(n=0;n<gsize_per_chan;n++){
  1140. for(k = 0; k < chans;k++) {
  1141. *gbufptr++ = (*s * aamp);
  1142. s++;
  1143. }
  1144. flcnt += *transpos;
  1145. iicnt = round(flcnt);
  1146. s = b + (iicnt * chans);
  1147. }
  1148. } else {
  1149. flcnt_frac = flcnt - (double)iicnt;
  1150. for(n=0;n<gsize_per_chan;n++) {
  1151. for(k = 0; k < chans;k++) {
  1152. *gbufptr++ = interp_gval_with_amp(s,flcnt_frac,dz->iparam[GRS_INCHANS],aamp);
  1153. s++;
  1154. }
  1155. flcnt += *transpos;
  1156. iicnt = (long)flcnt;
  1157. s = b + (iicnt * chans);
  1158. flcnt_frac = flcnt - (double) iicnt;
  1159. }
  1160. }
  1161. }
  1162. } else { /* NO CHANGE IN AMPLITUDE */
  1163. if(!dz->iparray[GRS_FLAGS][G_PITCH_FLAG]) {
  1164. if(real_gsize >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1165. return(0);
  1166. for(n=0;n<real_gsize;n++)
  1167. *gbufptr++ = *s++;
  1168. } else {
  1169. iicnt = (s - b)/chans;
  1170. *transpos = set_dvalue(dz->iparray[GRS_FLAGS][G_PITCH_FLAG],GRS_PITCH,GRS_HPITCH,GRS_PRANGE,dz);
  1171. *transpos = pow(2.0,*transpos);
  1172. if((((int)(gsize_per_chan * *transpos)+1) + iicnt) * chans >= dz->iparam[SAMPS_IN_INBUF]) /* JUNE 1996 */
  1173. return(0);
  1174. flcnt = (double)iicnt;
  1175. if(!dz->vflag[GRS_NO_INTERP]){
  1176. flcnt_frac = flcnt - (double)iicnt;
  1177. for(n=0;n<gsize_per_chan;n++) {
  1178. for(k=0;k < chans;k++) {
  1179. *gbufptr++ = interp_gval(s,flcnt_frac,dz->iparam[GRS_INCHANS]);
  1180. s++;
  1181. }
  1182. flcnt += *transpos;
  1183. iicnt = (long)flcnt;
  1184. s = b + (iicnt * chans);
  1185. flcnt_frac = flcnt - (double)iicnt;
  1186. }
  1187. } else { /* do truncate as originally */
  1188. for(n=0;n<gsize_per_chan;n++){
  1189. for(k=0;k < chans;k++)
  1190. *gbufptr++ = *s++;
  1191. flcnt += *transpos;
  1192. iicnt = round(flcnt);
  1193. s = b + (iicnt * chans);
  1194. }
  1195. }
  1196. }
  1197. }
  1198. *iiptr = s;
  1199. return(1);
  1200. }
  1201. /************************* WRITE_GRAIN ****************************/
  1202. int write_grain(float **maxwrite,float **Fptr, float **FFptr,int gsize_per_chan,int *nctr,dataptr dz)
  1203. {
  1204. int exit_status;
  1205. long n, to_doo, exess;
  1206. float *writend;
  1207. long real_gsize = gsize_per_chan * dz->iparam[GRS_INCHANS];
  1208. float *fptr = *Fptr, *ffptr = *FFptr;
  1209. float *gbufptr = dz->extrabuf[GRS_GBUF];
  1210. if((writend = (ffptr + real_gsize)) > *maxwrite)
  1211. *maxwrite = writend;
  1212. if(ffptr + real_gsize > dz->fptr[GRS_LTAILEND]) {
  1213. to_doo = dz->fptr[GRS_LTAILEND] - ffptr;
  1214. if((exess = real_gsize - to_doo) >= dz->buflen) {
  1215. sprintf(errstr,"Array overflow:write_grain().\n");
  1216. return(PROGRAM_ERROR);
  1217. }
  1218. for(n = 0; n < to_doo; n++)
  1219. *ffptr++ += *gbufptr++;
  1220. if((exit_status = write_samps_granula(dz->buflen,nctr,dz))<0)
  1221. return(exit_status);
  1222. memmove((char *)dz->fptr[GRS_LBUF],(char *)dz->fptr[GRS_LBUFEND],
  1223. (size_t)dz->iparam[GRS_LBUF_SMPXS] * sizeof(float));
  1224. memset((char *)dz->fptr[GRS_LBUFMID],0,dz->iparam[GRS_LONGS_BUFLEN] * sizeof(float));
  1225. ffptr -= dz->iparam[GRS_LONGS_BUFLEN];
  1226. fptr -= dz->iparam[GRS_LONGS_BUFLEN];
  1227. *maxwrite -= dz->iparam[GRS_LONGS_BUFLEN]; /* APRIL 1996 */
  1228. for(n = 0; n < exess; n++)
  1229. *ffptr++ += *gbufptr++;
  1230. *Fptr = fptr;
  1231. *FFptr = ffptr;
  1232. return(FINISHED);
  1233. }
  1234. for(n=0;n<real_gsize;n++)
  1235. *ffptr++ += *gbufptr++;
  1236. *Fptr = fptr;
  1237. *FFptr = ffptr;
  1238. return(FINISHED);
  1239. }
  1240. /************************* WRITE_STEREO_GRAIN ****************************/
  1241. int write_stereo_grain(double rpos,float **maxwrite,float **Fptr,float **FFptr,int gsize_per_chan,int *nctr,dataptr dz)
  1242. {
  1243. int exit_status;
  1244. long n, to_doo, exess;
  1245. long /**writend,*/ real_gsize = gsize_per_chan * STEREO;
  1246. double lpos;
  1247. double adjust = dehole(rpos);
  1248. float *writend, *fptr = *Fptr, *ffptr = *FFptr;
  1249. float *gbufptr = dz->extrabuf[GRS_GBUF];
  1250. lpos = (1.0 - rpos) * adjust;
  1251. rpos *= adjust;
  1252. if((writend = (ffptr + real_gsize)) > *maxwrite)
  1253. *maxwrite = writend;
  1254. if(ffptr + real_gsize >= dz->fptr[GRS_LTAILEND]) {
  1255. to_doo = dz->fptr[GRS_LTAILEND] - ffptr;
  1256. if((exess = real_gsize - to_doo) >= dz->iparam[GRS_LONGS_BUFLEN]) {
  1257. sprintf(errstr,"Array overflow: write_stereo_grain()\n");
  1258. return(PROGRAM_ERROR);
  1259. }
  1260. to_doo /= 2;
  1261. for(n = 0; n < to_doo; n++) {
  1262. *ffptr++ += (float) (*gbufptr * lpos);
  1263. *ffptr++ += (float) (*gbufptr++ * rpos);
  1264. }
  1265. if((exit_status = write_samps_granula(dz->iparam[GRS_LONGS_BUFLEN],nctr,dz))<0)
  1266. return(exit_status);
  1267. memmove((char *)dz->fptr[GRS_LBUF],(char *)dz->fptr[GRS_LBUFEND],
  1268. (size_t)dz->iparam[GRS_LBUF_SMPXS] * sizeof(float));
  1269. memset((char *)dz->fptr[GRS_LBUFMID],0,dz->iparam[GRS_LONGS_BUFLEN] * sizeof(float));
  1270. ffptr -= dz->iparam[GRS_LONGS_BUFLEN];
  1271. fptr -= dz->iparam[GRS_LONGS_BUFLEN];
  1272. *maxwrite -= dz->buflen; /* APR 1996 */
  1273. exess /= 2;
  1274. for(n = 0; n < exess; n++) {
  1275. *ffptr++ += /*round*/(float)(*gbufptr * lpos);
  1276. *ffptr++ += /*round*/(float)(*gbufptr++ * rpos);
  1277. }
  1278. *Fptr = fptr;
  1279. *FFptr = ffptr;
  1280. return(FINISHED);
  1281. }
  1282. for(n=0;n<gsize_per_chan;n++) {
  1283. *ffptr++ += /*round*/(float)(*gbufptr * lpos); /*rwd: was *gbuf */
  1284. *ffptr++ += /*round*/(float)(*gbufptr++ * rpos);
  1285. }
  1286. *Fptr = fptr;
  1287. *FFptr = ffptr;
  1288. return(FINISHED);
  1289. }
  1290. /****************************** DEHOLE ************************/
  1291. #define NONLIN 0.5
  1292. #define DEVIATE 0.25
  1293. double dehole(double pos)
  1294. {
  1295. double comp = 1.0 - (fabs((pos * 2.0) - 1.0)); /* 1 */
  1296. comp = pow(comp,NONLIN); /* 2 */
  1297. comp *= DEVIATE; /* 3 */
  1298. comp += (1.0 - DEVIATE); /* 4 */
  1299. return(comp);
  1300. }
  1301. /**************************** DO_SCATTER *****************************
  1302. * scatter forward by fraction of randpart of 1 event separation.
  1303. *
  1304. * Possible scatter | |-------------->|
  1305. * Range selected | |------> |
  1306. * Random choice within range | |---> |
  1307. */
  1308. long do_scatter(int ostep_per_chan,dataptr dz)
  1309. {
  1310. double scat = dz->param[GRS_SCATTER] * drand48();
  1311. long smpscat_per_chan = round((double)ostep_per_chan * scat);
  1312. return(smpscat_per_chan);
  1313. }
  1314. /******************************** WRITE_SAMPS_GRANULA ****************************
  1315. *
  1316. * Normalise each buffer before writing to obuf, then to file.
  1317. * Save the normalisation factor in array normfact[].
  1318. */
  1319. int write_samps_granula(long k,int *nctr,dataptr dz)
  1320. {
  1321. int exit_status;
  1322. /*long*/double val = 0.0 /*0L, max_long = 0L*/;
  1323. long n;
  1324. double thisnorm = 1.0, max_double = 0.0;
  1325. float *s = NULL;
  1326. float *l = dz->fptr[GRS_LBUF];
  1327. switch(dz->process) {
  1328. case(BRASSAGE): s = dz->sampbuf[GRS_OBUF]; break;
  1329. case(SAUSAGE): s = dz->sampbuf[SAUS_OBUF]; break;
  1330. }
  1331. for(n=0;n<k;n++) {
  1332. if((val = fabs(l[n])) > max_double)
  1333. max_double = val;
  1334. }
  1335. if(/*max_long*/max_double > F_MAXSAMP) {
  1336. thisnorm = (double)F_MAXSAMP/max_double;
  1337. for(n=0;n<k;n++)
  1338. s[n] = /*round*/(float) ((double)(l[n]) * thisnorm);
  1339. } else {
  1340. for(n=0;n<k;n++)
  1341. s[n] = l[n];
  1342. }
  1343. if((exit_status = write_samps(s,k,dz))<0)
  1344. return(exit_status);
  1345. (dz->parray[GRS_NORMFACT])[(*nctr)++] = thisnorm;
  1346. if(*nctr >= dz->iparam[GRS_ARRAYSIZE]) {
  1347. dz->iparam[GRS_ARRAYSIZE] += BIGARRAY;
  1348. if((dz->parray[GRS_NORMFACT] =
  1349. (double *)realloc((char *)(dz->parray[GRS_NORMFACT]),dz->iparam[GRS_ARRAYSIZE]*sizeof(double)))==NULL) {
  1350. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate normalisation array.\n");
  1351. return(MEMORY_ERROR);
  1352. }
  1353. }
  1354. return(FINISHED);
  1355. }
  1356. #ifndef MULTICHAN
  1357. /************************** DO_STEREO_SPLICES *****************************/
  1358. void do_stereo_splices(int gsize_per_chan,int bspl,int espl,dataptr dz)
  1359. {
  1360. if(dz->iparam[GRS_IS_BTAB] && (dz->iparam[GRS_BSPLICE] < gsize_per_chan/2))
  1361. do_stereo_btab_splice(dz);
  1362. else
  1363. do_stereo_bsplice(gsize_per_chan,dz,bspl);
  1364. if(dz->iparam[GRS_IS_ETAB] && (dz->iparam[GRS_ESPLICE] < gsize_per_chan/2))
  1365. do_stereo_etab_splice(gsize_per_chan,dz);
  1366. else
  1367. do_stereo_esplice(gsize_per_chan,dz,espl);
  1368. }
  1369. /************************** DO_STEREO_BTAB_SPLICE ****************************/
  1370. void do_stereo_btab_splice(dataptr dz)
  1371. {
  1372. long n, k = dz->iparam[GRS_BSPLICE];
  1373. double *d;
  1374. float *gbufptr = dz->extrabuf[GRS_GBUF];
  1375. if(k==0)
  1376. return;
  1377. d = dz->parray[GRS_BSPLICETAB];
  1378. for(n=0;n<k;n++) {
  1379. *gbufptr = (float)/*round*/(*gbufptr * *d);
  1380. gbufptr++;
  1381. *gbufptr = (float)/*round*/(*gbufptr * *d);
  1382. gbufptr++;
  1383. d++;
  1384. }
  1385. }
  1386. /************************** DO_STEREO_BSPLICE ****************************
  1387. *
  1388. * rwd: changed to avoid f/p division, added exponential option
  1389. */
  1390. void do_stereo_bsplice(int gsize_per_chan,dataptr dz,int bspl)
  1391. {
  1392. double dif,val,lastval,length,newsum,lastsum,twodif;
  1393. long n, k = min(bspl,gsize_per_chan);
  1394. float *gbufptr = dz->extrabuf[GRS_GBUF];
  1395. if(k==0)
  1396. return;
  1397. val = 0.0;
  1398. length = (double)k;
  1399. if(!dz->vflag[GRS_EXPON]){
  1400. dif = 1.0/length;
  1401. lastval = dif;
  1402. *gbufptr++ = (float)val;
  1403. *gbufptr++ = (float)val;
  1404. *gbufptr = (float)/*round*/(*gbufptr * lastval);
  1405. gbufptr++;
  1406. *gbufptr = (float) /*round*/(*gbufptr * lastval);
  1407. gbufptr++;
  1408. for(n=2;n<k;n++) {
  1409. val = lastval + dif;
  1410. lastval = val;
  1411. *gbufptr = (float) /*round*/(*gbufptr * val);
  1412. gbufptr++;
  1413. *gbufptr = (float) /*round*/(*gbufptr * val);
  1414. gbufptr++;
  1415. }
  1416. } else { /* fast quasi-exponential */
  1417. dif = 1.0/(length*length);
  1418. twodif = dif * 2.0;
  1419. lastsum = 0.0;
  1420. lastval = dif;
  1421. *gbufptr++ = (float)val;/* mca - round or truncate? */
  1422. *gbufptr++ = (float)val;/* mca - round or truncate? */
  1423. *gbufptr = (float) /*round*/(*gbufptr * lastval); /*** fixed MAY 1998 ***/
  1424. gbufptr++;
  1425. *gbufptr = (float) /*round*/(*gbufptr * lastval); /*** fixed MAY 1998 ***/
  1426. gbufptr++;
  1427. for(n=2;n<k;n++) {
  1428. newsum = lastsum + twodif;
  1429. val = lastval + newsum + dif;
  1430. *gbufptr = (float) /*round*/(*gbufptr * val);
  1431. gbufptr++;
  1432. *gbufptr = (float) /*round*/(*gbufptr * val);
  1433. gbufptr++;
  1434. lastval = val;
  1435. lastsum = newsum;
  1436. }
  1437. }
  1438. }
  1439. /************************** DO_STEREO_ETAB_SPLICE ****************************/
  1440. void do_stereo_etab_splice(int gsize_per_chan,dataptr dz)
  1441. {
  1442. long n, k = dz->iparam[GRS_ESPLICE];
  1443. double *d;
  1444. float *gbufptr = dz->extrabuf[GRS_GBUF] + ((gsize_per_chan - k) * 2);
  1445. if(k==0)
  1446. return;
  1447. d = dz->parray[GRS_ESPLICETAB];
  1448. for(n=0;n<k;n++) {
  1449. *gbufptr = (float) /*round*/(*gbufptr * *d);
  1450. gbufptr++;
  1451. *gbufptr = (float) /*round*/(*gbufptr * *d);
  1452. gbufptr++;
  1453. d++;
  1454. }
  1455. }
  1456. /************************** DO_STEREO_ESPLICE ****************************/
  1457. /* rwd: changed to avoid f/p division, added exponential option */
  1458. void do_stereo_esplice(int gsize_per_chan,dataptr dz,int espl)
  1459. {
  1460. double dif,val,lastval,length,newsum,lastsum,twodif;
  1461. long n, k = min(espl,gsize_per_chan);
  1462. long real_gsize = gsize_per_chan * 2;
  1463. float *gbufptr = dz->extrabuf[GRS_GBUF] + real_gsize;
  1464. if(k==0)
  1465. return;
  1466. val = 0.0;
  1467. length = (double) k;
  1468. if(!dz->vflag[GRS_EXPON]) {
  1469. dif = 1.0/length;
  1470. lastsum = dif;
  1471. gbufptr--;
  1472. *gbufptr = (float)val;
  1473. gbufptr--;
  1474. *gbufptr = (float)val;
  1475. gbufptr--;
  1476. *gbufptr = (float) /*round*/(*gbufptr * lastsum);
  1477. gbufptr--;
  1478. *gbufptr = (float) /*round*/(*gbufptr * lastsum);
  1479. for(n=k-3;n>=0;n--) {
  1480. val = lastsum + dif;
  1481. lastsum = val;
  1482. gbufptr--;
  1483. *gbufptr = (float) /*round*/(*gbufptr * val);
  1484. gbufptr--;
  1485. *gbufptr = (float) /*round*/(*gbufptr * val);
  1486. }
  1487. } else { /* fast quasi-exponential */
  1488. dif = 1.0/(length * length);
  1489. twodif = dif * 2.0;
  1490. lastsum = 0.0;
  1491. lastval = dif;
  1492. gbufptr--;
  1493. *gbufptr = (float)val;
  1494. gbufptr--;
  1495. *gbufptr = (float)val;
  1496. gbufptr--;
  1497. *gbufptr = (float) /*round*/(*gbufptr * lastval);
  1498. gbufptr--;
  1499. *gbufptr = (float)/*round*/(*gbufptr * lastval);
  1500. for(n=k-3;n>=0;n--) {
  1501. newsum =lastsum + twodif;
  1502. val = lastval + newsum + dif;
  1503. gbufptr--;
  1504. *gbufptr = (float)/*round*/(*gbufptr * val);
  1505. gbufptr--;
  1506. *gbufptr = (float)/*round*/(*gbufptr * val);
  1507. lastval = val;
  1508. lastsum = newsum;
  1509. }
  1510. }
  1511. }
  1512. #else
  1513. /************************** DO_MULTICHAN_SPLICES *****************************/
  1514. void do_multichan_splices(int gsize_per_chan,int bspl,int espl,dataptr dz)
  1515. {
  1516. if(dz->iparam[GRS_IS_BTAB] && (dz->iparam[GRS_BSPLICE] < gsize_per_chan/2))
  1517. do_multichan_btab_splice(dz);
  1518. else
  1519. do_multichan_bsplice(gsize_per_chan,dz,bspl);
  1520. if(dz->iparam[GRS_IS_ETAB] && (dz->iparam[GRS_ESPLICE] < gsize_per_chan/2))
  1521. do_multichan_etab_splice(gsize_per_chan,dz);
  1522. else
  1523. do_multichan_esplice(gsize_per_chan,dz,espl);
  1524. }
  1525. /************************** DO_MULTICHAN_BTAB_SPLICE ****************************/
  1526. void do_multichan_btab_splice(dataptr dz)
  1527. {
  1528. long n, j, k = dz->iparam[GRS_BSPLICE];
  1529. double *d;
  1530. float *gbufptr = dz->extrabuf[GRS_GBUF];
  1531. if(k==0)
  1532. return;
  1533. d = dz->parray[GRS_BSPLICETAB];
  1534. for(n=0;n<k;n++) {
  1535. for(j=0;j<dz->iparam[GRS_INCHANS];j++) {
  1536. *gbufptr = (float)/*round*/(*gbufptr * *d);
  1537. gbufptr++;
  1538. }
  1539. d++;
  1540. }
  1541. }
  1542. /************************** DO_MULTICHAN_BSPLICE ****************************
  1543. *
  1544. * rwd: changed to avoid f/p division, added exponential option
  1545. */
  1546. void do_multichan_bsplice(int gsize_per_chan,dataptr dz,int bspl)
  1547. {
  1548. double dif,val,lastval,length,newsum,lastsum,twodif;
  1549. long n, j, k = min(bspl,gsize_per_chan);
  1550. float *gbufptr = dz->extrabuf[GRS_GBUF];
  1551. int chans = dz->iparam[GRS_INCHANS];
  1552. if(k==0)
  1553. return;
  1554. val = 0.0;
  1555. length = (double)k;
  1556. if(!dz->vflag[GRS_EXPON]){
  1557. dif = 1.0/length;
  1558. lastval = dif;
  1559. for(j= 0; j<chans;j++)
  1560. *gbufptr++ = (float)val;
  1561. for(j= 0; j<chans;j++) {
  1562. *gbufptr = (float)/*round*/(*gbufptr * lastval);
  1563. gbufptr++;
  1564. }
  1565. for(n=2;n<k;n++) {
  1566. val = lastval + dif;
  1567. lastval = val;
  1568. for(j= 0; j<chans;j++) {
  1569. *gbufptr = (float) /*round*/(*gbufptr * val);
  1570. gbufptr++;
  1571. }
  1572. }
  1573. } else { /* fast quasi-exponential */
  1574. dif = 1.0/(length*length);
  1575. twodif = dif * 2.0;
  1576. lastsum = 0.0;
  1577. lastval = dif;
  1578. for(j=0;j<chans;j++)
  1579. *gbufptr++ = (float)val;/* mca - round or truncate? */
  1580. for(j=0;j<chans;j++) {
  1581. *gbufptr = (float) /*round*/(*gbufptr * lastval); /*** fixed MAY 1998 ***/
  1582. gbufptr++;
  1583. }
  1584. for(n=2;n<k;n++) {
  1585. newsum = lastsum + twodif;
  1586. val = lastval + newsum + dif;
  1587. for(j=0;j<chans;j++) {
  1588. *gbufptr = (float) /*round*/(*gbufptr * val);
  1589. gbufptr++;
  1590. }
  1591. lastval = val;
  1592. lastsum = newsum;
  1593. }
  1594. }
  1595. }
  1596. /************************** DO_MULTICHAN_ETAB_SPLICE ****************************/
  1597. void do_multichan_etab_splice(int gsize_per_chan,dataptr dz)
  1598. {
  1599. long n, j, k = dz->iparam[GRS_ESPLICE];
  1600. double *d;
  1601. int chans = dz->iparam[GRS_INCHANS];
  1602. float *gbufptr = dz->extrabuf[GRS_GBUF] + ((gsize_per_chan - k) * chans);
  1603. if(k==0)
  1604. return;
  1605. d = dz->parray[GRS_ESPLICETAB];
  1606. for(n=0;n<k;n++) {
  1607. for(j=0;j<chans;j++) {
  1608. *gbufptr = (float) /*round*/(*gbufptr * *d);
  1609. gbufptr++;
  1610. }
  1611. d++;
  1612. }
  1613. }
  1614. /************************** DO_MULTICHAN_ESPLICE ****************************/
  1615. /* rwd: changed to avoid f/p division, added exponential option */
  1616. void do_multichan_esplice(int gsize_per_chan,dataptr dz,int espl)
  1617. {
  1618. double dif,val,lastval,length,newsum,lastsum,twodif;
  1619. long n, j, k = min(espl,gsize_per_chan);
  1620. int chans = dz->iparam[GRS_INCHANS];
  1621. long real_gsize = gsize_per_chan * chans;
  1622. float *gbufptr = dz->extrabuf[GRS_GBUF] + real_gsize;
  1623. if(k==0)
  1624. return;
  1625. val = 0.0;
  1626. length = (double) k;
  1627. if(!dz->vflag[GRS_EXPON]) {
  1628. dif = 1.0/length;
  1629. lastsum = dif;
  1630. for(j=0;j<chans;j++) {
  1631. gbufptr--;
  1632. *gbufptr = (float)val;
  1633. }
  1634. for(j=0;j<chans;j++) {
  1635. gbufptr--;
  1636. *gbufptr = (float) /*round*/(*gbufptr * lastsum);
  1637. }
  1638. for(n=k-3;n>=0;n--) {
  1639. val = lastsum + dif;
  1640. lastsum = val;
  1641. for(j=0;j<chans;j++) {
  1642. gbufptr--;
  1643. *gbufptr = (float) /*round*/(*gbufptr * val);
  1644. }
  1645. }
  1646. } else { /* fast quasi-exponential */
  1647. dif = 1.0/(length * length);
  1648. twodif = dif * 2.0;
  1649. lastsum = 0.0;
  1650. lastval = dif;
  1651. for(j=0;j<chans;j++) {
  1652. gbufptr--;
  1653. *gbufptr = (float)val;
  1654. }
  1655. for(j=0;j<chans;j++) {
  1656. gbufptr--;
  1657. *gbufptr = (float) /*round*/(*gbufptr * lastval);
  1658. }
  1659. for(n=k-3;n>=0;n--) {
  1660. newsum =lastsum + twodif;
  1661. val = lastval + newsum + dif;
  1662. for(j=0;j<chans;j++) {
  1663. gbufptr--;
  1664. *gbufptr = (float)/*round*/(*gbufptr * val);
  1665. }
  1666. lastval = val;
  1667. lastsum = newsum;
  1668. }
  1669. }
  1670. }
  1671. #endif
  1672. /************************** INTERP_GVAL_WITH_AMP ****************************/
  1673. float interp_gval_with_amp(float *s,double flcnt_frac,int chans,float ampp)
  1674. {
  1675. /*long*/float tthis = /*(long)*/ *s * ampp;
  1676. /*long*/float next = /*(long)*/ *(s+chans) * ampp;
  1677. //long val = this + round((double)(next-this) * flcnt_frac);
  1678. //return(short)((val+TWO_POW_14) >> 15);
  1679. return (float) (tthis + ((next-tthis) * flcnt_frac));
  1680. }
  1681. /************************** INTERP_GVAL ****************************/
  1682. float interp_gval(float *s,double flcnt_frac,int chans)
  1683. {
  1684. float tthis = *s;
  1685. float next = *(s+chans);
  1686. float val = (float)(tthis + /*round*/((double)(next-tthis) * flcnt_frac));
  1687. return(val);
  1688. }
  1689. /*************************** SET_RANGE ******************************/
  1690. int set_range(int absiicnt,dataptr dz)
  1691. {
  1692. long val;
  1693. val = min(dz->iparam[GRS_SRCHRANGE],absiicnt);
  1694. val = round((double)val * drand48());
  1695. return(val);
  1696. }
  1697. /*************************** SET_OUTSTEP ******************************/
  1698. int set_outstep(int gsize_per_chan,dataptr dz)
  1699. {
  1700. double dens;
  1701. int val = 0;
  1702. /* TW 4 :2002 */
  1703. switch(dz->process){
  1704. case(BRASSAGE):
  1705. switch(dz->mode) {
  1706. case(GRS_BRASSAGE):
  1707. case(GRS_FULL_MONTY):
  1708. case(GRS_REVERB):
  1709. case(GRS_GRANULATE):
  1710. dens = set_dvalue(dz->iparray[GRS_FLAGS][G_DENSITY_FLAG],GRS_DENSITY,GRS_HDENSITY,GRS_DRANGE,dz);
  1711. val = round((double)gsize_per_chan/(double)dens);
  1712. break;
  1713. default:
  1714. val = round((double)gsize_per_chan/(double)GRS_DEFAULT_DENSITY);
  1715. }
  1716. break;
  1717. case(SAUSAGE):
  1718. dens = set_dvalue(dz->iparray[GRS_FLAGS][G_DENSITY_FLAG],GRS_DENSITY,GRS_HDENSITY,GRS_DRANGE,dz);
  1719. val = round((double)gsize_per_chan/(double)dens);
  1720. break;
  1721. }
  1722. return(val);
  1723. }
  1724. /*************************** SET_INSTEP ******************************/
  1725. int set_instep(int ostep_per_chan,dataptr dz)
  1726. { /* rwd: added range error traps */
  1727. double velocity;
  1728. int istep_per_chan = 0;
  1729. switch(dz->process) { /* TW 4:2002 */
  1730. case(BRASSAGE):
  1731. switch(dz->mode) {
  1732. case(GRS_BRASSAGE):
  1733. case(GRS_FULL_MONTY):
  1734. case(GRS_TIMESTRETCH):
  1735. velocity = set_dvalue(dz->iparray[GRS_FLAGS][G_VELOCITY_FLAG],GRS_VELOCITY,GRS_HVELOCITY,GRS_VRANGE,dz);
  1736. istep_per_chan = round(velocity * (double)ostep_per_chan);
  1737. break;
  1738. default:
  1739. istep_per_chan = ostep_per_chan; /* default velocity is 1.0 */
  1740. break;
  1741. }
  1742. break;
  1743. case(SAUSAGE):
  1744. velocity = set_dvalue(dz->iparray[GRS_FLAGS][G_VELOCITY_FLAG],GRS_VELOCITY,GRS_HVELOCITY,GRS_VRANGE,dz);
  1745. istep_per_chan = round(velocity * (double)ostep_per_chan);
  1746. break;
  1747. }
  1748. return(istep_per_chan);
  1749. }
  1750. /*************************** SET_IVALUE ******************************/
  1751. int set_ivalue(int flag,int paramno,int hparamno,int rangeno,dataptr dz)
  1752. {
  1753. int val = 0;
  1754. switch(flag) {
  1755. case(NOT_SET):
  1756. case(FIXED):
  1757. case(VARIABLE):
  1758. val = dz->iparam[paramno];
  1759. break;
  1760. case(RANGE_VLO):
  1761. case(RANGE_VHI):
  1762. case(RANGE_VHILO):
  1763. dz->iparam[rangeno] = dz->iparam[hparamno] - dz->iparam[paramno];
  1764. /* fall thro */
  1765. case(RANGED):
  1766. val = round((drand48() * dz->iparam[rangeno]) + (double)dz->iparam[paramno]);
  1767. break;
  1768. }
  1769. return(val);
  1770. }
  1771. /*************************** SET_DVALUE ******************************/
  1772. double set_dvalue(int flag,int paramno,int hparamno,int rangeno,dataptr dz)
  1773. {
  1774. double val = 0;
  1775. switch(flag) {
  1776. case(NOT_SET):
  1777. case(FIXED):
  1778. case(VARIABLE):
  1779. val = dz->param[paramno];
  1780. break;
  1781. case(RANGE_VLO):
  1782. case(RANGE_VHI):
  1783. case(RANGE_VHILO):
  1784. dz->param[rangeno] = dz->param[hparamno] - dz->param[paramno];
  1785. /* fall thro */
  1786. case(RANGED):
  1787. val = (drand48() * dz->param[rangeno]) + dz->param[paramno];
  1788. break;
  1789. }
  1790. return(val);
  1791. }
  1792. /******************************* READ_SAMPS_SAUSAGE *******************************/
  1793. int read_samps_sausage(int firsttime,dataptr dz)
  1794. {
  1795. int exit_status, n;
  1796. int samps_read = INT_MAX; /* i.e. larger than possible */
  1797. for(n=0;n<dz->infilecnt;n++) {
  1798. if(firsttime) {
  1799. if((exit_status = read_a_specific_large_buf(n,dz))<0)
  1800. return(exit_status);
  1801. samps_read = min(samps_read,dz->ssampsread);
  1802. } else {
  1803. if((exit_status = read_a_specific_normal_buf_with_wraparound(n,dz))<0)
  1804. return(exit_status);
  1805. samps_read = min(samps_read,dz->ssampsread);
  1806. }
  1807. }
  1808. if(samps_read==INT_MAX) { // RWD 06-2019 was LONG_MAX
  1809. sprintf(errstr,"Problem reading sound buffers.\n");
  1810. return(PROGRAM_ERROR);
  1811. }
  1812. if(firsttime)
  1813. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread;
  1814. else
  1815. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread + dz->iparam[GRS_BUF_SMPXS];
  1816. return(FINISHED);
  1817. }
  1818. /*************************** READ_A_SPECIFIC_LARGE_BUF ******************************/
  1819. #ifndef MULTICHAN
  1820. int read_a_specific_large_buf(int j,dataptr dz)
  1821. {
  1822. long bigbufsize = dz->buflen; /* RWD odd one, this... */
  1823. long n, m, k, samps_read;
  1824. int bufno = SAUS_BUF(j);
  1825. bigbufsize += dz->iparam[GRS_BUF_SMPXS];
  1826. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  1827. bigbufsize *= 2;
  1828. if((samps_read = fgetfbufEx(dz->sampbuf[SAUS_SBUF],bigbufsize,dz->ifd[j],0))<0) {
  1829. sprintf(errstr,"Failed to read samps from file %d.\n",j+1);
  1830. return(SYSTEM_ERROR);
  1831. }
  1832. dz->ssampsread = samps_read;
  1833. bigbufsize /= 2; /* RWD still in samps...buflen + smpxs */
  1834. dz->ssampsread /= 2;
  1835. switch(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  1836. case(1):
  1837. for(n=0,m=0;n<dz->ssampsread;n++,m+=2)
  1838. (dz->sampbuf[bufno])[n] = (dz->sampbuf[SAUS_SBUF])[m];
  1839. break;
  1840. case(2):
  1841. for(n=0,m=1;n<dz->ssampsread;n++,m+=2)
  1842. (dz->sampbuf[bufno])[n] = (dz->sampbuf[SAUS_SBUF])[m];
  1843. break;
  1844. case(BOTH_CHANNELS):
  1845. for(n=0,m=0,k=1;n<dz->ssampsread;n++,m+=2,k+=2)
  1846. (dz->sampbuf[bufno])[n] = (float)(((dz->sampbuf[SAUS_SBUF])[m] + (dz->sampbuf[SAUS_SBUF])[k])* 0.5f);
  1847. break;
  1848. }
  1849. } else {
  1850. if((samps_read = fgetfbufEx(dz->sampbuf[bufno],bigbufsize,dz->ifd[j],0))<0) {
  1851. sprintf(errstr,"Failed to read samps from file %d.\n",j+1);
  1852. return(SYSTEM_ERROR);
  1853. }
  1854. dz->ssampsread = samps_read;
  1855. }
  1856. /* dz->bigbufsize -= dz->iparam[GRS_BUF_XS]; */ /* leaves dz->buflen unchanged, which is fine */
  1857. return(FINISHED);
  1858. }
  1859. #else
  1860. int read_a_specific_large_buf(int j,dataptr dz)
  1861. {
  1862. long bigbufsize = dz->buflen; /* RWD odd one, this... */
  1863. long n, m, samps_read, ibufcnt;
  1864. int bufno = SAUS_BUF(j), chans = dz->infile->channels;
  1865. double sum;
  1866. bigbufsize += dz->iparam[GRS_BUF_SMPXS];
  1867. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  1868. bigbufsize *= chans;
  1869. if((samps_read = fgetfbufEx(dz->sampbuf[SAUS_SBUF],bigbufsize,dz->ifd[j],0))<0) {
  1870. sprintf(errstr,"Failed to read samps from file %d.\n",j+1);
  1871. return(SYSTEM_ERROR);
  1872. }
  1873. dz->ssampsread = samps_read;
  1874. bigbufsize /= chans; /* RWD still in samps...buflen + smpxs */
  1875. dz->ssampsread /= chans;
  1876. switch(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  1877. case(ALL_CHANNELS):
  1878. ibufcnt = 0;
  1879. for(n=0;n<dz->ssampsread;n++) {
  1880. sum = 0.0;
  1881. for(m=0;m<chans;m++)
  1882. sum += dz->sampbuf[GRS_SBUF][ibufcnt++];
  1883. dz->sampbuf[bufno][n] = (float)(sum/(double)chans);
  1884. }
  1885. break;
  1886. default:
  1887. for(n=0,m=dz->iparam[GRS_CHAN_TO_XTRACT];n<dz->ssampsread;n++,m+=chans)
  1888. dz->sampbuf[bufno][n] = dz->sampbuf[GRS_SBUF][m];
  1889. break;
  1890. }
  1891. } else {
  1892. if((samps_read = fgetfbufEx(dz->sampbuf[bufno],bigbufsize,dz->ifd[j],0))<0) {
  1893. sprintf(errstr,"Failed to read samps from file %d.\n",j+1);
  1894. return(SYSTEM_ERROR);
  1895. }
  1896. dz->ssampsread = samps_read;
  1897. }
  1898. return(FINISHED);
  1899. }
  1900. #endif
  1901. /*************************** READ_A_SPECIFIC_NORMAL_BUF_WITH_WRAPAROUND ******************************/
  1902. #ifndef MULTICHAN
  1903. int read_a_specific_normal_buf_with_wraparound(int j,dataptr dz)
  1904. {
  1905. long bigbufsize = dz->buflen;
  1906. long n,m,k,samps_read;
  1907. int bufno = SAUS_BUF(j);
  1908. int ibufno = SAUS_IBUF(j);
  1909. memmove((char *)dz->sampbuf[bufno],(char *)(dz->sbufptr[bufno]),
  1910. dz->iparam[GRS_BUF_SMPXS] * sizeof(float));
  1911. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  1912. bigbufsize *= 2;
  1913. if((samps_read = fgetfbufEx(dz->sampbuf[SAUS_SBUF],bigbufsize,dz->ifd[j],0))<0) {
  1914. sprintf(errstr,"Failed to read from file %d.\n",j+1);
  1915. return(SYSTEM_ERROR);
  1916. }
  1917. dz->ssampsread = samps_read;
  1918. bigbufsize /= 2;
  1919. dz->ssampsread /= 2;
  1920. switch(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  1921. case(1):
  1922. for(n=0,m=0;n<dz->ssampsread;n++,m+=2)
  1923. (dz->sampbuf[ibufno])[n] = (dz->sampbuf[GRS_SBUF])[m];
  1924. break;
  1925. case(2):
  1926. for(n=0,m=1;n<dz->ssampsread;n++,m+=2)
  1927. (dz->sampbuf[ibufno])[n] = (dz->sampbuf[GRS_SBUF])[m];
  1928. break;
  1929. case(BOTH_CHANNELS):
  1930. for(n=0,m=0,k=1;n<dz->ssampsread;n++,m+=2,k+=2)
  1931. (dz->sampbuf[ibufno])[n] = (float)(((dz->sampbuf[GRS_SBUF])[m] + (dz->sampbuf[GRS_SBUF])[k])* 0.5f);
  1932. break;
  1933. }
  1934. } else {
  1935. if((samps_read = fgetfbufEx(dz->sampbuf[ibufno],bigbufsize,dz->ifd[j],0))<0) {
  1936. sprintf(errstr,"Failed to read from file %d.\n",j+1);
  1937. return(SYSTEM_ERROR);
  1938. }
  1939. dz->ssampsread = samps_read;
  1940. }
  1941. return(FINISHED);
  1942. }
  1943. #else
  1944. int read_a_specific_normal_buf_with_wraparound(int j,dataptr dz)
  1945. {
  1946. long bigbufsize = dz->buflen;
  1947. long n,m,samps_read, ibufcnt;
  1948. double sum;
  1949. int bufno = SAUS_BUF(j);
  1950. int ibufno = SAUS_IBUF(j), chans = dz->infile->channels;
  1951. memmove((char *)dz->sampbuf[bufno],(char *)(dz->sbufptr[bufno]),dz->iparam[GRS_BUF_SMPXS] * sizeof(float));
  1952. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  1953. bigbufsize *= chans;
  1954. if((samps_read = fgetfbufEx(dz->sampbuf[SAUS_SBUF],bigbufsize,dz->ifd[j],0))<0) {
  1955. sprintf(errstr,"Failed to read from file %d.\n",j+1);
  1956. return(SYSTEM_ERROR);
  1957. }
  1958. dz->ssampsread = samps_read;
  1959. bigbufsize /= chans;
  1960. dz->ssampsread /= chans;
  1961. switch(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  1962. case(ALL_CHANNELS):
  1963. ibufcnt = 0;
  1964. for(n=0;n<dz->ssampsread;n++) {
  1965. sum = 0.0;
  1966. for(m=0;m<chans;m++)
  1967. sum += dz->sampbuf[GRS_SBUF][ibufcnt++];
  1968. dz->sampbuf[ibufno][n] = (float)(sum/(double)chans);
  1969. }
  1970. break;
  1971. default:
  1972. for(n=0,m=dz->iparam[GRS_CHAN_TO_XTRACT];n<dz->ssampsread;n++,m+=chans)
  1973. dz->sampbuf[ibufno][n] = dz->sampbuf[GRS_SBUF][m];
  1974. break;
  1975. }
  1976. } else {
  1977. if((samps_read = fgetfbufEx(dz->sampbuf[ibufno],bigbufsize,dz->ifd[j],0))<0) {
  1978. sprintf(errstr,"Failed to read from file %d.\n",j+1);
  1979. return(SYSTEM_ERROR);
  1980. }
  1981. dz->ssampsread = samps_read;
  1982. }
  1983. return(FINISHED);
  1984. }
  1985. #endif
  1986. /*************************** BAKTRAK_SAUSAGE ******************************
  1987. *
  1988. * <------------ baktrak(b) ---------------->
  1989. *
  1990. * <--(b-x)-->
  1991. * <------------ x -------------->
  1992. * |-------- current bufer --------|
  1993. *
  1994. * |-------- new buffer --------|
  1995. * <------------x------------->
  1996. *
  1997. */
  1998. #ifndef MULTICHAN
  1999. int baktrak_sausage(int thissnd,long samptotal,int absiicnt_per_chan,float **iiptr,dataptr dz)
  2000. {
  2001. int exit_status;
  2002. long bktrk, new_position;
  2003. unsigned long reset_size = dz->buflen + dz->iparam[GRS_BUF_SMPXS];
  2004. //TW I agree, this is FINE!!, and, with sndseekEx, algo is more efficient
  2005. // whenever the search-range is relatively small (and equally efficient otherwise)
  2006. bktrk = samptotal - (absiicnt_per_chan * dz->iparam[GRS_INCHANS]);
  2007. *iiptr = dz->sampbuf[SAUS_BUF(thissnd)];
  2008. if((new_position = samptotal - bktrk)<0) {
  2009. fprintf(stdout,"WARNING: Non-fatal program error:\nRange arithmetic problem - 2, in baktraking.\n");
  2010. fflush(stdout);
  2011. new_position = 0;
  2012. *iiptr = dz->sampbuf[SAUS_BUF(thissnd)];
  2013. }
  2014. if(dz->iparam[GRS_CHAN_TO_XTRACT]) /* IF we've converted from STEREO file */
  2015. new_position *= 2;
  2016. if(sndseekEx(dz->ifd[thissnd],new_position,0)<0) {
  2017. sprintf(errstr,"sndseek error: baktrak_sausage()\n");
  2018. return(SYSTEM_ERROR);
  2019. }
  2020. memset((char *)dz->sampbuf[SAUS_BUF(thissnd)],0,reset_size * sizeof(float));
  2021. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  2022. reset_size *= 2;
  2023. memset((char *)dz->sampbuf[SAUS_SBUF],0,reset_size * sizeof(float));
  2024. }
  2025. if((exit_status = read_a_specific_large_buf(thissnd,dz))<0)
  2026. return(exit_status);
  2027. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread;
  2028. return(FINISHED);
  2029. }
  2030. #else
  2031. int baktrak_sausage(int thissnd,long samptotal,int absiicnt_per_chan,float **iiptr,dataptr dz)
  2032. {
  2033. int exit_status, chans = dz->infile->channels;
  2034. long bktrk, new_position;
  2035. unsigned long reset_size = dz->buflen + dz->iparam[GRS_BUF_SMPXS];
  2036. bktrk = samptotal - (absiicnt_per_chan * dz->iparam[GRS_INCHANS]);
  2037. *iiptr = dz->sampbuf[SAUS_BUF(thissnd)];
  2038. if((new_position = samptotal - bktrk)<0) {
  2039. fprintf(stdout,"WARNING: Non-fatal program error:\nRange arithmetic problem - 2, in baktraking.\n");
  2040. fflush(stdout);
  2041. new_position = 0;
  2042. *iiptr = dz->sampbuf[SAUS_BUF(thissnd)];
  2043. }
  2044. if(dz->iparam[GRS_CHAN_TO_XTRACT]) /* IF we've converted from MULTICHAN file */
  2045. new_position *= chans;
  2046. if(sndseekEx(dz->ifd[thissnd],new_position,0)<0) {
  2047. sprintf(errstr,"sndseek error: baktrak_sausage()\n");
  2048. return(SYSTEM_ERROR);
  2049. }
  2050. memset((char *)dz->sampbuf[SAUS_BUF(thissnd)],0,reset_size * sizeof(float));
  2051. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  2052. reset_size *= chans;
  2053. memset((char *)dz->sampbuf[SAUS_SBUF],0,reset_size * sizeof(float));
  2054. }
  2055. if((exit_status = read_a_specific_large_buf(thissnd,dz))<0)
  2056. return(exit_status);
  2057. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread;
  2058. return(FINISHED);
  2059. }
  2060. #endif
  2061. /****************************** RESET_SAUSAGE *******************************/
  2062. #ifndef MULTICHAN
  2063. int reset_sausage(int thissnd,int resetskip,dataptr dz)
  2064. {
  2065. int exit_status;
  2066. unsigned long reset_size = dz->buflen + dz->iparam[GRS_BUF_SMPXS];
  2067. memset((char *)dz->sampbuf[SAUS_BUF(thissnd)],0,reset_size * sizeof(float));
  2068. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  2069. reset_size *= 2;
  2070. memset((char *)dz->sampbuf[SAUS_SBUF],0,reset_size * sizeof(float));
  2071. }
  2072. if(dz->iparam[GRS_CHAN_TO_XTRACT]) /* If we've converted from a stereo file */
  2073. resetskip *= 2;
  2074. if(sndseekEx(dz->ifd[thissnd],resetskip,0)<0) {
  2075. sprintf(errstr,"sndseek error: reset_sausage()\n");
  2076. return(SYSTEM_ERROR);
  2077. }
  2078. if((exit_status = read_a_specific_large_buf(thissnd,dz))<0)
  2079. return(exit_status);
  2080. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread;
  2081. return(FINISHED);
  2082. }
  2083. #else
  2084. int reset_sausage(int thissnd,int resetskip,dataptr dz)
  2085. {
  2086. int exit_status, chans = dz->infile->channels;
  2087. unsigned long reset_size = dz->buflen + dz->iparam[GRS_BUF_SMPXS];
  2088. memset((char *)dz->sampbuf[SAUS_BUF(thissnd)],0,reset_size * sizeof(float));
  2089. if(dz->iparam[GRS_CHAN_TO_XTRACT]) {
  2090. reset_size *= chans;
  2091. memset((char *)dz->sampbuf[SAUS_SBUF],0,reset_size * sizeof(float));
  2092. }
  2093. if(dz->iparam[GRS_CHAN_TO_XTRACT]) /* If we've converted from a stereo file */
  2094. resetskip *= chans;
  2095. if(sndseekEx(dz->ifd[thissnd],resetskip,0)<0) {
  2096. sprintf(errstr,"sndseek error: reset_sausage()\n");
  2097. return(SYSTEM_ERROR);
  2098. }
  2099. if((exit_status = read_a_specific_large_buf(thissnd,dz))<0)
  2100. return(exit_status);
  2101. dz->iparam[SAMPS_IN_INBUF] = dz->ssampsread;
  2102. return(FINISHED);
  2103. }
  2104. #endif
  2105. /****************************** GET_NEXT_INSND *******************************/
  2106. int get_next_insnd(dataptr dz)
  2107. {
  2108. if(dz->itemcnt <= 0) {
  2109. perm_sausage(dz->infilecnt,dz);
  2110. dz->itemcnt = dz->infilecnt;
  2111. }
  2112. dz->itemcnt--;
  2113. return(dz->iparray[SAUS_PERM][dz->itemcnt]);
  2114. }
  2115. /****************************** PERM_SAUSAGE *******************************/
  2116. void perm_sausage(int cnt,dataptr dz)
  2117. {
  2118. int n, t;
  2119. for(n=0;n<cnt;n++) {
  2120. t = (int)(drand48() * (double)(n+1)); /* TRUNCATE */
  2121. if(t==n)
  2122. prefix(n,cnt-1,dz);
  2123. else
  2124. insert(n,t,cnt-1,dz);
  2125. }
  2126. }
  2127. /****************************** INSERT ****************************/
  2128. void insert(int n,int t,int cnt_less_one,dataptr dz)
  2129. {
  2130. shuflup(t+1,cnt_less_one,dz);
  2131. dz->iparray[SAUS_PERM][t+1] = n;
  2132. }
  2133. /****************************** PREFIX ****************************/
  2134. void prefix(int n,int cnt_less_one,dataptr dz)
  2135. {
  2136. shuflup(0,cnt_less_one,dz);
  2137. dz->iparray[SAUS_PERM][0] = n;
  2138. }
  2139. /****************************** SHUFLUP ****************************/
  2140. void shuflup(int k,int cnt_less_one,dataptr dz)
  2141. {
  2142. int n;
  2143. for(n = cnt_less_one; n > k; n--)
  2144. dz->iparray[SAUS_PERM][n] = dz->iparray[SAUS_PERM][n-1];
  2145. }
  2146. /****************************** SAUSAGE_PREPROCESS ****************************/
  2147. int sausage_preprocess(dataptr dz)
  2148. {
  2149. if((dz->iparray[SAUS_PERM] = (int *)malloc(dz->infilecnt * sizeof(int)))==NULL) {
  2150. sprintf(errstr,"INSUFFICIENT MEMORY for permutation array for sausage.\n");
  2151. return(MEMORY_ERROR);
  2152. }
  2153. dz->iparray[SAUS_PERM][0] = dz->infilecnt + 1; /* impossible value to initialise perm */
  2154. return create_sized_outfile(dz->outfilename,dz);
  2155. /*return(FINISHED);*/
  2156. }
  2157. /*************************** CREATE_SAUSAGE_BUFFERS **************************/
  2158. #ifndef MULTICHAN
  2159. int create_sausage_buffers(dataptr dz)
  2160. {
  2161. long standard_block = (long)Malloc(-1);
  2162. long this_bloksize;
  2163. int exit_status, n;
  2164. int convert_to_stereo = FALSE, overall_size, bufdivisor = 0;
  2165. float *tailend;
  2166. long stereo_buflen = 0, stereo_bufxs = 0, outbuflen;
  2167. //TW All buffers are in floats, so this not needed
  2168. // int lfactor = sizeof(long)/sizeof(float), n;
  2169. if(dz->iparray[GRS_FLAGS][G_SPACE_FLAG])
  2170. convert_to_stereo = TRUE;
  2171. if((dz->extrabuf[GRS_GBUF] = (float *)malloc(dz->iparam[GRS_GLBUF_SMPXS] * sizeof(float)))==NULL) {
  2172. sprintf(errstr,"INSUFFICIENT MEMORY to create grain buffer.\n"); /* GRAIN BUFFER */
  2173. return(MEMORY_ERROR);
  2174. }
  2175. /* CALCULATE NUMBER OF BUFFER CHUNKS REQUIRED : bufdivisor */
  2176. if(dz->iparam[GRS_CHANNELS]>0)
  2177. bufdivisor += 2; /* 2 for stereo-infile, before reducing to mono */
  2178. //TW All buffers are in floats
  2179. // bufdivisor += dz->infilecnt + 1 + lfactor; /* infilecnt IN mono, 1 OUT, 1 long OUT */
  2180. bufdivisor += dz->infilecnt + 2; /* infilecnt IN mono, 1 OUT, 1 LBUF OUT */
  2181. if(convert_to_stereo)
  2182. //TW All buffers are in floats
  2183. // bufdivisor += 1 + lfactor; /* 2nd OUT, 2nd long OUT */
  2184. bufdivisor += 2; /* 2nd OUT, 2nd LBUF OUT */
  2185. this_bloksize = standard_block;
  2186. for(;;) {
  2187. if((exit_status = grab_an_appropriate_block_of_sausage_memory(&this_bloksize,standard_block,bufdivisor,dz))<0)
  2188. return(exit_status);
  2189. /* CALCULATE AND ALLOCATE TOTAL MEMORY REQUIRED : overall_size */
  2190. overall_size = (dz->buflen * bufdivisor) + (dz->iparam[GRS_BUF_SMPXS] * dz->infilecnt) + dz->iparam[GRS_LBUF_SMPXS];
  2191. if(dz->iparam[GRS_CHANNELS])
  2192. overall_size += 2 * dz->iparam[GRS_BUF_SMPXS]; /* IF stereo, also allow for bufxs in stereo inbuf */
  2193. //TW if(overall_size<0)
  2194. if(overall_size * sizeof(float)<0) {
  2195. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n"); /* arithmetic overflow */
  2196. return(MEMORY_ERROR);
  2197. }
  2198. if((dz->bigbuf=(float *)malloc(overall_size * sizeof(float)))==NULL) {
  2199. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  2200. return(MEMORY_ERROR);
  2201. }
  2202. /* SET SIZE OF inbuf, outbuf, AND Lbuf (FOR CALCS) */
  2203. outbuflen = dz->buflen;
  2204. if(convert_to_stereo) /* For stereo out, outbuflen is double length of inbuf */
  2205. outbuflen *= 2;
  2206. if(dz->iparam[GRS_CHANNELS]) {
  2207. stereo_buflen = dz->buflen * 2;
  2208. stereo_bufxs = dz->iparam[GRS_BUF_SMPXS] * 2;
  2209. }
  2210. dz->iparam[GRS_LONGS_BUFLEN] = outbuflen; /* Longs buffer is same size as obuf */
  2211. if(dz->iparam[GRS_LBUF_SMPXS] > dz->iparam[GRS_LONGS_BUFLEN])
  2212. continue;
  2213. break;
  2214. }
  2215. /* DIVIDE UP ALLOCATED MEMORY IN SPECIALISED BUFFERS */
  2216. if(dz->iparam[GRS_CHANNELS]) { /* sbuf : extra stereo input buffer, if required */
  2217. dz->sampbuf[SAUS_SBUF] = dz->bigbuf;
  2218. dz->sampbuf[SAUS_BUF(0)] = dz->sampbuf[SAUS_SBUF] + stereo_buflen + stereo_bufxs;
  2219. } else
  2220. dz->sampbuf[SAUS_BUF(0)] = dz->bigbuf;
  2221. dz->sbufptr[SAUS_BUF(0)] = dz->sampbuf[SAUS_BUF(0)] + dz->buflen;
  2222. dz->sampbuf[SAUS_IBUF(0)] = dz->sampbuf[SAUS_BUF(0)] + dz->iparam[GRS_BUF_SMPXS];
  2223. tailend = dz->sbufptr[SAUS_BUF(0)] + dz->iparam[GRS_BUF_SMPXS];
  2224. for(n=1;n<dz->infilecnt;n++) {
  2225. dz->sampbuf[SAUS_BUF(n)] = tailend; /* Lbuf: buffer for calculations */
  2226. dz->sbufptr[SAUS_BUF(n)] = dz->sampbuf[SAUS_BUF(n)] + dz->buflen;
  2227. dz->sampbuf[SAUS_IBUF(n)] = dz->sampbuf[SAUS_BUF(n)] + dz->iparam[GRS_BUF_SMPXS];
  2228. tailend = dz->sbufptr[SAUS_BUF(n)] + dz->iparam[GRS_BUF_SMPXS];
  2229. }
  2230. dz->fptr[GRS_LBUF] = tailend;
  2231. dz->fptr[GRS_LBUFEND] = dz->fptr[GRS_LBUF] + dz->iparam[GRS_LONGS_BUFLEN];
  2232. dz->fptr[GRS_LTAILEND] = dz->fptr[GRS_LBUFEND] + dz->iparam[GRS_LBUF_SMPXS];
  2233. dz->fptr[GRS_LBUFMID] = dz->fptr[GRS_LBUF] + dz->iparam[GRS_LBUF_SMPXS];
  2234. dz->sampbuf[SAUS_OBUF] = /*(short *)*/(dz->fptr[GRS_LTAILEND]);
  2235. /* INITIALISE BUFFERS */
  2236. memset((char *)dz->bigbuf,0,overall_size * sizeof(float));
  2237. return(FINISHED);
  2238. }
  2239. #else
  2240. int create_sausage_buffers(dataptr dz)
  2241. {
  2242. long standard_block = (long)Malloc(-1);
  2243. long this_bloksize;
  2244. int exit_status, n, chans = dz->infile->channels;
  2245. int overall_size, bufdivisor = 0;
  2246. float *tailend;
  2247. long multichan_buflen = 0, multichan_bufxs = 0, outbuflen;
  2248. if((dz->extrabuf[GRS_GBUF] = (float *)malloc(dz->iparam[GRS_GLBUF_SMPXS] * sizeof(float)))==NULL) {
  2249. sprintf(errstr,"INSUFFICIENT MEMORY to create grain buffer.\n"); /* GRAIN BUFFER */
  2250. return(MEMORY_ERROR);
  2251. }
  2252. /* CALCULATE NUMBER OF BUFFER CHUNKS REQUIRED : bufdivisor */
  2253. if(dz->iparam[GRS_CHANNELS]>0)
  2254. bufdivisor += chans; /* chans for multichan-infile, before reducing to mono */
  2255. bufdivisor += dz->infilecnt; /* infilecnt IN mono, 1 OUT, 1 LBUF OUT */
  2256. for(n=0;n<dz->outfile->channels;n++)
  2257. bufdivisor += 1 + sizeof(long)/sizeof(float); /* 1 float and 1 long buf for each input channel */
  2258. this_bloksize = standard_block;
  2259. for(;;) {
  2260. if((exit_status = grab_an_appropriate_block_of_sausage_memory(&this_bloksize,standard_block,bufdivisor,dz))<0)
  2261. return(exit_status);
  2262. /* CALCULATE AND ALLOCATE TOTAL MEMORY REQUIRED : overall_size */
  2263. overall_size = (dz->buflen * bufdivisor) + (dz->iparam[GRS_BUF_SMPXS] * dz->infilecnt) + dz->iparam[GRS_LBUF_SMPXS];
  2264. if(dz->iparam[GRS_CHANNELS])
  2265. overall_size += chans * dz->iparam[GRS_BUF_SMPXS]; /* IF multichan, also allow for bufxs in multichan inbuf */
  2266. if(overall_size * sizeof(float)<0) {
  2267. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n"); /* arithmetic overflow */
  2268. return(MEMORY_ERROR);
  2269. }
  2270. if((dz->bigbuf=(float *)malloc(overall_size * sizeof(float)))==NULL) {
  2271. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  2272. return(MEMORY_ERROR);
  2273. }
  2274. /* SET SIZE OF inbuf, outbuf, AND Lbuf (FOR CALCS) */
  2275. outbuflen = dz->buflen;
  2276. outbuflen *= dz->out_chans;
  2277. if(dz->iparam[GRS_CHANNELS]) {
  2278. multichan_buflen = dz->buflen * chans;
  2279. multichan_bufxs = dz->iparam[GRS_BUF_SMPXS] * chans;
  2280. }
  2281. dz->iparam[GRS_LONGS_BUFLEN] = outbuflen; /* Longs buffer is same size as obuf */
  2282. if(dz->iparam[GRS_LBUF_SMPXS] > dz->iparam[GRS_LONGS_BUFLEN])
  2283. continue;
  2284. break;
  2285. }
  2286. /* DIVIDE UP ALLOCATED MEMORY IN SPECIALISED BUFFERS */
  2287. if(dz->iparam[GRS_CHANNELS]) { /* sbuf : extra stereo input buffer, if required */
  2288. dz->sampbuf[SAUS_SBUF] = dz->bigbuf;
  2289. dz->sampbuf[SAUS_BUF(0)] = dz->sampbuf[SAUS_SBUF] + multichan_buflen + multichan_bufxs;
  2290. } else
  2291. dz->sampbuf[SAUS_BUF(0)] = dz->bigbuf;
  2292. dz->sbufptr[SAUS_BUF(0)] = dz->sampbuf[SAUS_BUF(0)] + dz->buflen;
  2293. dz->sampbuf[SAUS_IBUF(0)] = dz->sampbuf[SAUS_BUF(0)] + dz->iparam[GRS_BUF_SMPXS];
  2294. tailend = dz->sbufptr[SAUS_BUF(0)] + dz->iparam[GRS_BUF_SMPXS];
  2295. for(n=1;n<dz->infilecnt;n++) {
  2296. dz->sampbuf[SAUS_BUF(n)] = tailend; /* Lbuf: buffer for calculations */
  2297. dz->sbufptr[SAUS_BUF(n)] = dz->sampbuf[SAUS_BUF(n)] + dz->buflen;
  2298. dz->sampbuf[SAUS_IBUF(n)] = dz->sampbuf[SAUS_BUF(n)] + dz->iparam[GRS_BUF_SMPXS];
  2299. tailend = dz->sbufptr[SAUS_BUF(n)] + dz->iparam[GRS_BUF_SMPXS];
  2300. }
  2301. dz->fptr[GRS_LBUF] = tailend;
  2302. dz->fptr[GRS_LBUFEND] = dz->fptr[GRS_LBUF] + dz->iparam[GRS_LONGS_BUFLEN];
  2303. dz->fptr[GRS_LTAILEND] = dz->fptr[GRS_LBUFEND] + dz->iparam[GRS_LBUF_SMPXS];
  2304. dz->fptr[GRS_LBUFMID] = dz->fptr[GRS_LBUF] + dz->iparam[GRS_LBUF_SMPXS];
  2305. dz->sampbuf[SAUS_OBUF] = /*(short *)*/(dz->fptr[GRS_LTAILEND]);
  2306. /* INITIALISE BUFFERS */
  2307. memset((char *)dz->bigbuf,0,overall_size * sizeof(float));
  2308. return(FINISHED);
  2309. }
  2310. #endif
  2311. /* INPUT BUFFERS :-
  2312. *
  2313. * |-----------BUFLEN-----------|
  2314. *
  2315. * buf ibuf bufend tailend
  2316. * |_________|__________________|buf_smpxs| ..... (obuf->)
  2317. * /
  2318. * |buf_smpxs| <<-COPY_________/
  2319. *
  2320. * |-----------BUFLEN-----------|
  2321. *
  2322. *
  2323. *
  2324. * OUTPUT LONGS BUFFER:-
  2325. *
  2326. * Lbuf Lbufmid Lbufend
  2327. * |____________|_______________|_Lbuf_smpxs_|
  2328. * /
  2329. * |_Lbuf_smpxs_| <<-COPY___________/
  2330. *
  2331. */
  2332. /*************************** GRAB_AN_APPROPRIATE_BLOCK_OF_SAUSAGE_MEMORY **************************/
  2333. #ifndef MULTICHAN
  2334. int grab_an_appropriate_block_of_sausage_memory(long *this_bloksize,long standard_block,int bufdivisor,dataptr dz)
  2335. {
  2336. /*int sector_blok;*/
  2337. do {
  2338. //TW if((dz->buflen = *this_bloksize)< 0) { /* arithmetic overflow */
  2339. if((dz->buflen = *this_bloksize) * sizeof(float) < 0) { /* arithmetic overflow */
  2340. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n");
  2341. return(MEMORY_ERROR);
  2342. }
  2343. *this_bloksize += standard_block;
  2344. /* CALCULATE SIZE OF BUFFER REQUIRED : dz->bigbufsize */
  2345. dz->buflen -= (dz->infilecnt * dz->iparam[GRS_BUF_SMPXS]) + dz->iparam[GRS_LBUF_SMPXS];
  2346. /* Allow for overflow areas */
  2347. if(dz->iparam[GRS_CHANNELS])
  2348. dz->buflen -= 2 * dz->iparam[GRS_BUF_SMPXS]; /* Allow for overflow space in additional stereo inbuf */
  2349. dz->buflen /= bufdivisor; /* get unit buffersize */
  2350. /* sector_blok = SECSIZE; */ /* Read and write buf sizes must be multiples of SECSIZE */
  2351. if(dz->iparam[GRS_CHANNELS]) /* If reading stereo: 2* SECSIZE reduces to single mono SECSIZE */
  2352. /* sector_blok *= 2; */ /* So dz->bigbufsize must be a multiple of (2 * SECSIZE) */
  2353. dz->buflen = (dz->buflen / STEREO) * STEREO; /*RWD: */
  2354. /* dz->buflen = (dz->bigbufsize/sector_blok) * sector_blok;*/
  2355. } while(dz->buflen <= 0);
  2356. return(FINISHED);
  2357. }
  2358. #else
  2359. int grab_an_appropriate_block_of_sausage_memory(long *this_bloksize,long standard_block,int bufdivisor,dataptr dz)
  2360. {
  2361. do {
  2362. if((dz->buflen = *this_bloksize) * sizeof(float) < 0) { /* arithmetic overflow */
  2363. sprintf(errstr,"INSUFFICIENT MEMORY for sound buffers.\n");
  2364. return(MEMORY_ERROR);
  2365. }
  2366. *this_bloksize += standard_block;
  2367. dz->buflen -= (dz->infilecnt * dz->iparam[GRS_BUF_SMPXS]) + dz->iparam[GRS_LBUF_SMPXS];
  2368. /* Allow for overflow areas */
  2369. if(dz->iparam[GRS_CHANNELS])
  2370. dz->buflen -= dz->infile->channels * dz->iparam[GRS_BUF_SMPXS]; /* Allow for overflow space in additional multichan inbuf */
  2371. dz->buflen /= bufdivisor; /* get unit buffersize */
  2372. if(dz->iparam[GRS_CHANNELS]) /* If reading multichano: chans * SECSIZE reduces to single mono SECSIZE */
  2373. dz->buflen = (dz->buflen / dz->infile->channels) * dz->infile->channels;
  2374. } while(dz->buflen <= 0);
  2375. return(FINISHED);
  2376. }
  2377. #endif
  2378. #ifdef MULTICHAN
  2379. /************************* WRITE_MULTICHAN_GRAIN ****************************/
  2380. int write_multichan_grain(double rpos,int chana,int chanb,float **maxwrite,float **Fptr,float **FFptr,int gsize_per_chan,int *nctr,dataptr dz)
  2381. {
  2382. int exit_status;
  2383. long n, to_doo, exess;
  2384. long real_gsize = gsize_per_chan * dz->out_chans;
  2385. double lpos;
  2386. double adjust = dehole(rpos);
  2387. float *writend, *fptr = *Fptr, *ffptr = *FFptr;
  2388. float *gbufptr = dz->extrabuf[GRS_GBUF];
  2389. lpos = (1.0 - rpos) * adjust;
  2390. rpos *= adjust;
  2391. chana--;
  2392. chanb--;
  2393. if((writend = (ffptr + real_gsize)) > *maxwrite)
  2394. *maxwrite = writend;
  2395. if(ffptr + real_gsize >= dz->fptr[GRS_LTAILEND]) {
  2396. to_doo = dz->fptr[GRS_LTAILEND] - ffptr;
  2397. if((exess = real_gsize - to_doo) >= dz->iparam[GRS_LONGS_BUFLEN]) {
  2398. sprintf(errstr,"Array overflow: write_stereo_grain()\n");
  2399. return(PROGRAM_ERROR);
  2400. }
  2401. to_doo /= dz->out_chans;
  2402. for(n = 0; n < to_doo; n++) {
  2403. *(ffptr + chana) += (float) (*gbufptr * lpos);
  2404. *(ffptr + chanb) += (float) (*gbufptr * rpos);
  2405. gbufptr++;
  2406. ffptr += dz->out_chans;
  2407. }
  2408. if((exit_status = write_samps_granula(dz->iparam[GRS_LONGS_BUFLEN],nctr,dz))<0)
  2409. return(exit_status);
  2410. memmove((char *)dz->fptr[GRS_LBUF],(char *)dz->fptr[GRS_LBUFEND],
  2411. (size_t)dz->iparam[GRS_LBUF_SMPXS] * sizeof(float));
  2412. memset((char *)dz->fptr[GRS_LBUFMID],0,dz->iparam[GRS_LONGS_BUFLEN] * sizeof(float));
  2413. ffptr -= dz->iparam[GRS_LONGS_BUFLEN];
  2414. fptr -= dz->iparam[GRS_LONGS_BUFLEN];
  2415. *maxwrite -= dz->buflen; /* APR 1996 */
  2416. exess /= dz->out_chans;
  2417. for(n = 0; n < exess; n++) {
  2418. *(ffptr + chana) += (float) (*gbufptr * lpos);
  2419. *(ffptr + chanb) += (float) (*gbufptr * rpos);
  2420. gbufptr++;
  2421. ffptr += dz->out_chans;
  2422. }
  2423. *Fptr = fptr;
  2424. *FFptr = ffptr;
  2425. return(FINISHED);
  2426. }
  2427. for(n=0;n<gsize_per_chan;n++) {
  2428. *(ffptr + chana) += (float) (*gbufptr * lpos);
  2429. *(ffptr + chanb) += (float) (*gbufptr * rpos);
  2430. gbufptr++;
  2431. ffptr += dz->out_chans;
  2432. }
  2433. *Fptr = fptr;
  2434. *FFptr = ffptr;
  2435. return(FINISHED);
  2436. }
  2437. #endif