strans.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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. /* floatsam version */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <memory.h>
  25. #include <structures.h>
  26. #include <tkglobals.h>
  27. #include <globcon.h>
  28. #include <cdpmain.h>
  29. #include <modeno.h>
  30. #include <pnames.h>
  31. #include <arrays.h>
  32. #include <flags.h>
  33. #include <modify.h>
  34. #include <sfsys.h>
  35. #ifdef unix
  36. #define round(x) lround((x))
  37. #else
  38. #define round(x) cdp_round((x))
  39. #endif
  40. #define CALCLIM (0.02)
  41. #define MININC (0.002) /* rwd - black hole avoidance */
  42. static int create_vtrans_buffers(dataptr dz);
  43. static int create_strans_buffers(dataptr dz);
  44. static int read_wrapped_samps_mono(dataptr dz);
  45. static int read_wrapped_samps_stereo(dataptr dz);
  46. static int strans_process(dataptr dz);
  47. static int vtrans_process(dataptr dz);
  48. static int do_acceleration(dataptr dz);
  49. static int do_vibrato(dataptr dz);
  50. static int vtrans_vtrans(dataptr dz);
  51. static int read_samps_for_vtrans(dataptr dz);
  52. static int write_samps_with_intime_display(float *buffer,int samps_to_write,dataptr dz);
  53. static int convert_brkpnts(dataptr dz);
  54. static int timevents(int *obufcnt,int intime0,int intime1,double insize0,double insize1,dataptr dz);
  55. static double cntevents(int dur,double s0,double s1);
  56. static int samesize(int *obufcnt,int intime0,double insize0,double insize1,int duration,dataptr dz);
  57. static int integral_times(int *obufcnt,int intime0,int isize,int number,dataptr dz);
  58. static int putintval(int *obufcnt,int i,dataptr dz);
  59. static int unvarying_times(int *obufcnt,int intime0,double size,int number,dataptr dz);
  60. static int getetime(int *obufcnt,int t0,int t1,double s0,double s1,int number,dataptr dz);
  61. static double refinesize(double hibound,double lobound,double fnumber,int duration,double error,double insize0);
  62. static int putval(int *obufcnt,double pos,dataptr dz);
  63. static int sizeq(double f1,double f2);
  64. static int change_frame(dataptr dz);
  65. static int inv_cntevents(int dur,double s0,double s1);
  66. static int force_value_at_end_time(int paramno,dataptr dz);
  67. static void splice_end(int obufcnt,dataptr dz);
  68. static double interp_read_sintable(double *sintab,double sfindex);
  69. /************************* VTRANS_PREPROCESS *****************************/
  70. int vtrans_preprocess(dataptr dz)
  71. {
  72. double acceltime, tabratio, *p, *pend;
  73. int n;
  74. switch(dz->mode) {
  75. case(MOD_TRANSPOS):
  76. case(MOD_TRANSPOS_SEMIT):
  77. case(MOD_TRANSPOS_INFO):
  78. case(MOD_TRANSPOS_SEMIT_INFO):
  79. dz->iparam[UNITSIZE] = dz->insams[0]/dz->infile->channels;
  80. dz->param[VTRANS_SR] = (double)dz->infile->srate;
  81. dz->iparam[TOTAL_UNITS_READ] = 0;
  82. break;
  83. case(MOD_ACCEL):
  84. acceltime = dz->param[ACCEL_GOALTIME] - dz->param[ACCEL_STARTTIME];
  85. if(acceltime < MINTIME_ACCEL) {
  86. sprintf(errstr,"time for acceleration (%lf) must be greater than %.3fsecs\n",acceltime,MINTIME_ACCEL);
  87. return(DATA_ERROR);
  88. }
  89. dz->param[ACCEL_ACCEL] = pow(dz->param[ACCEL_ACCEL],(1.0/(acceltime *(double)dz->infile->srate)));
  90. dz->iparam[ACCEL_STARTTIME] = /* convert to time-in-samples */
  91. round(dz->param[ACCEL_STARTTIME] * (double)dz->infile->srate) * dz->infile->channels;
  92. break;
  93. case(MOD_VIBRATO):
  94. tabratio = (double)VIB_TABSIZE/(double)dz->infile->srate; /* converts frq to sintable step */
  95. if(dz->brksize[VIB_FRQ]) {
  96. p = dz->brk[VIB_FRQ] + 1;
  97. pend = dz->brk[VIB_FRQ] + (dz->brksize[VIB_FRQ] * 2);
  98. while(p < pend) {
  99. *p *= tabratio;
  100. p += 2;
  101. }
  102. } else
  103. dz->param[VIB_FRQ] *= tabratio;
  104. if(dz->brksize[VIB_DEPTH]) { /* converts semitones to 8vas */
  105. p = dz->brk[VIB_DEPTH] + 1;
  106. pend = dz->brk[VIB_DEPTH] + (dz->brksize[VIB_DEPTH] * 2);
  107. while(p < pend) {
  108. *p *= OCTAVES_PER_SEMITONE;
  109. p += 2;
  110. }
  111. } else
  112. dz->param[VIB_DEPTH] *= OCTAVES_PER_SEMITONE;
  113. if((dz->parray[VIB_SINTAB] = (double *)malloc((VIB_TABSIZE + 1) * sizeof(double)))==NULL) {
  114. sprintf(errstr,"INSUFFICIENT MEMORY for vibrato sintable.\n");
  115. return(MEMORY_ERROR);
  116. }
  117. tabratio = TWOPI/(double)VIB_TABSIZE; /* converts table-step to fraction of 2PI */
  118. for(n=0;n<VIB_TABSIZE;n++)
  119. dz->parray[VIB_SINTAB][n] = sin((double)n * tabratio);
  120. dz->parray[VIB_SINTAB][n] = 0.0;
  121. break;
  122. }
  123. return(FINISHED);
  124. }
  125. /************************* CREATE_MODSPEED_BUFFERS *****************************/
  126. int create_modspeed_buffers(dataptr dz)
  127. {
  128. switch(dz->mode) {
  129. case(MOD_TRANSPOS):
  130. case(MOD_TRANSPOS_SEMIT):
  131. case(MOD_TRANSPOS_INFO):
  132. case(MOD_TRANSPOS_SEMIT_INFO):
  133. if(dz->brksize[VTRANS_TRANS]==0)
  134. return create_strans_buffers(dz);
  135. return create_vtrans_buffers(dz);
  136. case(MOD_ACCEL):
  137. case(MOD_VIBRATO):
  138. return create_strans_buffers(dz);
  139. default:
  140. sprintf(errstr,"Buffers not established for this MOD_SPEED mode.\n");
  141. break;
  142. }
  143. return(PROGRAM_ERROR);
  144. }
  145. /************************* CREATE_STRANS_BUFFERS *****************************/
  146. //TW replaced by global
  147. //#define FSECSIZE (256)
  148. int create_strans_buffers(dataptr dz)
  149. {
  150. size_t bigbufsize;
  151. int framesize;
  152. int guardspace = dz->infile->channels * sizeof(float); /* WRAPPED POINT(S) */
  153. bigbufsize = (size_t)Malloc(-1);
  154. /* RWD belt'n'braces */
  155. if(dz->infile->channels>STEREO) {
  156. sprintf(errstr, "File has too many channels.\n");
  157. return(DATA_ERROR);
  158. }
  159. if(dz->infile->channels==STEREO) {
  160. //TW Safety first: stick to existing secsize-convention for now
  161. //6 takes into account STEREO channel-count; no need to realign.
  162. //Will do it completely differently when go to 4+ channels
  163. framesize = F_SECSIZE * sizeof(float) * 6;
  164. if((bigbufsize = (bigbufsize/framesize) * framesize)<=0)
  165. bigbufsize = framesize;
  166. dz->buflen = (int)(bigbufsize / 3 / sizeof(float));
  167. bigbufsize = dz->buflen * 3 * sizeof(float);
  168. dz->iparam[VTRANS_HBUFSIZE] = dz->buflen/2;
  169. if((dz->bigbuf = (float *)Malloc(bigbufsize + guardspace)) == 0) {
  170. sprintf(errstr,"INSUFFICIENT MEMORY FOR SND BUFFERS.\n");
  171. return(MEMORY_ERROR);
  172. }
  173. memset((char *)dz->bigbuf,0,bigbufsize + guardspace);
  174. /*dz->bigbufsize = dz->bigbufsize/3;*/
  175. dz->sampbuf[TRUE_LBUF] = dz->bigbuf;
  176. dz->sampbuf[L_GRDPNT] = dz->sampbuf[TRUE_LBUF]; /* GUARD POINT */
  177. dz->sampbuf[LBUF] = dz->bigbuf + 1;
  178. dz->sampbuf[TRUE_RBUF] = dz->sampbuf[LBUF] + dz->iparam[VTRANS_HBUFSIZE];
  179. dz->sampbuf[R_GRDPNT] = dz->sampbuf[TRUE_RBUF];
  180. dz->sampbuf[RBUF] = dz->sampbuf[R_GRDPNT] + 1;
  181. dz->sampbuf[INBUF] = dz->sampbuf[RBUF] + dz->iparam[VTRANS_HBUFSIZE];
  182. dz->sampbuf[OUTBUF] = dz->sampbuf[INBUF] + dz->buflen;
  183. dz->sampbuf[OBUFEND] = dz->sampbuf[OUTBUF] + dz->buflen;
  184. } else {
  185. //TW Safety first: stick to existing secsize-convention for now
  186. framesize = F_SECSIZE * sizeof(float) * 2;
  187. bigbufsize = (bigbufsize/framesize) * framesize;
  188. dz->buflen = bigbufsize/2/sizeof(float);
  189. /* RWD probably unnecessary, but may be when we move to nChannels */
  190. bigbufsize = dz->buflen * 2 * sizeof(float);
  191. if((dz->bigbuf = (float *)malloc(bigbufsize + guardspace)) == 0) {
  192. sprintf(errstr, "Can't allocate memory for sound.\n");
  193. return(MEMORY_ERROR);
  194. }
  195. memset((char *)dz->bigbuf,0,bigbufsize + guardspace);
  196. /*dz->bigbufsize /= 2;*/
  197. dz->sampbuf[TRUE_LBUF] = dz->bigbuf;
  198. dz->sampbuf[L_GRDPNT] = dz->sampbuf[TRUE_LBUF];
  199. dz->sampbuf[INBUF] = dz->bigbuf + 1;
  200. dz->sampbuf[OUTBUF] = dz->sampbuf[INBUF] + dz->buflen;
  201. dz->sampbuf[OBUFEND] = dz->sampbuf[OUTBUF] + dz->buflen;
  202. }
  203. return(FINISHED);
  204. }
  205. /*************************** CREATE_VTRANS_BUFFERS **************************/
  206. int create_vtrans_buffers(dataptr dz)
  207. {
  208. size_t bigbufsize;
  209. //TW: SAFETY FIRST for now
  210. int fsecbytesize = F_SECSIZE * sizeof(float);
  211. bigbufsize = (size_t)Malloc(-1);
  212. bigbufsize /= dz->bufcnt;
  213. //TW: SAFETY FIRST for now
  214. bigbufsize = (bigbufsize/fsecbytesize) * fsecbytesize;
  215. dz->buflen = bigbufsize/sizeof(float);
  216. dz->iparam[UNIT_BUFLEN] = dz->buflen/dz->infile->channels;
  217. bigbufsize = dz->buflen * sizeof(float);
  218. if((dz->bigbuf = (float *)malloc((bigbufsize * dz->bufcnt) + fsecbytesize)) == NULL) {
  219. sprintf(errstr, "Can't allocate memory for sound.\n");
  220. return(MEMORY_ERROR);
  221. }
  222. dz->sampbuf[INBUF] = dz->bigbuf;
  223. dz->sampbuf[OUTBUF] = dz->sampbuf[INBUF] + dz->buflen + F_SECSIZE;
  224. return(FINISHED);
  225. }
  226. /************************* PROCESS_VARISPEED ******************************/
  227. int process_varispeed(dataptr dz)
  228. {
  229. switch(dz->mode) {
  230. case(MOD_ACCEL):
  231. return do_acceleration(dz);
  232. case(MOD_VIBRATO):
  233. return do_vibrato(dz);
  234. case(MOD_TRANSPOS):
  235. case(MOD_TRANSPOS_SEMIT):
  236. case(MOD_TRANSPOS_INFO):
  237. case(MOD_TRANSPOS_SEMIT_INFO):
  238. if(dz->brksize[VTRANS_TRANS]==0)
  239. return strans_process(dz);
  240. return vtrans_process(dz);
  241. }
  242. /* RWD 9:2001 need retval*/
  243. return PROGRAM_ERROR;
  244. }
  245. /************************* STRANS_PROCESS ******************************/
  246. int strans_process(dataptr dz)
  247. {
  248. int exit_status;
  249. int place;
  250. double flplace = 1.0, fracsamp, step;
  251. //HEREH
  252. double interp;
  253. float *obufptr = dz->sampbuf[OUTBUF];
  254. float *obuf = dz->sampbuf[OUTBUF];
  255. float *obufend = dz->sampbuf[OBUFEND];
  256. float *true_lbuf = dz->sampbuf[TRUE_LBUF];
  257. float *true_rbuf;
  258. double place_inc = dz->param[VTRANS_TRANS];
  259. switch(dz->mode) {
  260. case(MOD_TRANSPOS_INFO):
  261. sprintf(errstr,"Output duration = %lf secs\n",dz->duration/dz->param[VTRANS_TRANS]);
  262. if(!sloom) {
  263. fprintf(stdout,"INFO: END\n");
  264. fflush(stdout);
  265. }
  266. return(FINISHED);
  267. case(MOD_TRANSPOS_SEMIT_INFO):
  268. place_inc = pow(2.0,(place_inc * OCTAVES_PER_SEMITONE));
  269. sprintf(errstr,"Output duration = %lf secs\n",dz->duration/place_inc);
  270. if(!sloom) {
  271. fprintf(stdout,"INFO: END\n");
  272. fflush(stdout);
  273. }
  274. return(FINISHED);
  275. case(MOD_TRANSPOS_SEMIT):
  276. place_inc = pow(2.0,(place_inc * OCTAVES_PER_SEMITONE));
  277. break;
  278. }
  279. switch(dz->infile->channels) {
  280. case(MONO):
  281. if((exit_status = read_wrapped_samps_mono(dz))<0)
  282. return(exit_status);
  283. while(dz->ssampsread > 0) {
  284. while(flplace < (double)dz->ssampsread) {
  285. place = (int)flplace;
  286. fracsamp = flplace - (double)place;
  287. step = true_lbuf[place+1] - true_lbuf[place];
  288. // interp = round(fracsamp * step);
  289. interp = fracsamp * step;
  290. *obufptr = (float)(true_lbuf[place] + interp);
  291. if(++obufptr >= obufend) {
  292. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  293. return(exit_status);
  294. obufptr = obuf;
  295. }
  296. flplace += place_inc;
  297. }
  298. if((exit_status = read_wrapped_samps_mono(dz))<0)
  299. return(exit_status);
  300. flplace -= (double)dz->buflen;
  301. }
  302. break;
  303. case(STEREO):
  304. true_rbuf = dz->sampbuf[TRUE_RBUF];
  305. if((exit_status = read_wrapped_samps_stereo(dz))<0)
  306. return(exit_status);
  307. while(dz->ssampsread > 0) { /* 2 */
  308. while(flplace < (double)dz->ssampsread) {
  309. place = (int)flplace;
  310. fracsamp = flplace - (double)place;
  311. step = true_lbuf[place+1] - true_lbuf[place];
  312. // interp = round(fracsamp * step);
  313. interp = fracsamp * step;
  314. *obufptr++ = (float)(true_lbuf[place] + interp);
  315. step = true_rbuf[place+1] - true_rbuf[place];
  316. // interp = round(fracsamp * step);
  317. interp = fracsamp * step;
  318. *obufptr++ = (float)(true_rbuf[place] + interp);
  319. if(obufptr >= obufend) {
  320. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  321. return(exit_status);
  322. obufptr = obuf;
  323. }
  324. flplace += place_inc;
  325. }
  326. if((exit_status = read_wrapped_samps_stereo(dz))<0)
  327. return(exit_status);
  328. flplace -= (double)dz->iparam[VTRANS_HBUFSIZE];
  329. }
  330. break;
  331. }
  332. if(obufptr!=obuf) /* 12 */
  333. return write_samps(obuf,obufptr - obuf,dz);
  334. return(FINISHED);
  335. }
  336. /************************** READ_WRAPPED_SAMPS_MONO *****************************/
  337. int read_wrapped_samps_mono(dataptr dz)
  338. {
  339. *(dz->sampbuf[L_GRDPNT]) = *(dz->sampbuf[INBUF] + dz->buflen - 1);
  340. return read_samps(dz->sampbuf[INBUF], dz);
  341. }
  342. /************************* READ_WRAPPED_SAMPS_STEREO *****************************/
  343. int read_wrapped_samps_stereo(dataptr dz)
  344. {
  345. int exit_status;
  346. int n, m;
  347. float *lbuf = dz->sampbuf[LBUF];
  348. float *rbuf = dz->sampbuf[RBUF];
  349. float *l_grdpnt = dz->sampbuf[L_GRDPNT];
  350. float *r_grdpnt = dz->sampbuf[R_GRDPNT];
  351. *l_grdpnt = *(lbuf + dz->iparam[VTRANS_HBUFSIZE] - 1);
  352. *r_grdpnt = *(rbuf + dz->iparam[VTRANS_HBUFSIZE] - 1);
  353. if((exit_status = read_samps(dz->sampbuf[INBUF],dz))<0)
  354. return(exit_status);
  355. for(n = 0, m = 0; n < dz->ssampsread; m++, n+=2) {
  356. lbuf[m] = dz->sampbuf[INBUF][n];
  357. rbuf[m] = dz->sampbuf[INBUF][n+1];
  358. }
  359. dz->ssampsread /= 2; /* stereo */
  360. return(FINISHED);
  361. }
  362. /****************************** VTRANS_PROCESS *************************/
  363. int vtrans_process(dataptr dz)
  364. {
  365. int exit_status;
  366. if((exit_status = convert_brkpnts(dz))<0)
  367. return(exit_status);
  368. if((exit_status = change_frame(dz))<0)
  369. return(exit_status);
  370. if(dz->mode==MOD_TRANSPOS_INFO || dz->mode==MOD_TRANSPOS_SEMIT_INFO)
  371. return(FINISHED);
  372. if((exit_status = create_vtrans_buffers(dz))<0)
  373. return(exit_status);
  374. return vtrans_vtrans(dz);
  375. }
  376. /****************************** VTRANS_VTRANS *************************/
  377. int vtrans_vtrans(dataptr dz)
  378. {
  379. int exit_status;
  380. int n, m;
  381. double *dbrk = dz->brk[VTRANS_TRANS];
  382. int obufcnt = 0;
  383. display_virtual_time(0L,dz);
  384. if((exit_status = read_samps_for_vtrans(dz))<0)
  385. return(exit_status);
  386. for(n=1,m=2;n<dz->brksize[VTRANS_TRANS];n++,m+=2) {
  387. if((exit_status =
  388. timevents(&obufcnt,(int)round(dbrk[m-2]),(int)round(dbrk[m]),dbrk[m-1],dbrk[m+1],dz))!=CONTINUE)
  389. break;
  390. }
  391. if(exit_status < 0)
  392. return(exit_status);
  393. splice_end(obufcnt,dz);
  394. return write_samps_with_intime_display(dz->sampbuf[OUTBUF],obufcnt ,dz);
  395. return FINISHED;
  396. }
  397. /*************************** READ_SAMPS_FOR_VTRANS **************************/
  398. int read_samps_for_vtrans(dataptr dz)
  399. {
  400. //TW MUST READ EXTRA SECTOR to get wrap-around value
  401. float *buffer = dz->sampbuf[INBUF];
  402. int samps_read, samps_to_read = dz->buflen + F_SECSIZE;
  403. dz->iparam[UNITS_RD_PRE_THISBUF] = dz->iparam[TOTAL_UNITS_READ];
  404. if((samps_read = fgetfbufEx(buffer, samps_to_read,dz->ifd[0],0)) < 0) {
  405. sprintf(errstr, "Can't read from input soundfile.\n");
  406. return(SYSTEM_ERROR);
  407. }
  408. dz->samps_left -= samps_read;
  409. if(samps_read == samps_to_read) {
  410. if((sndseekEx(dz->ifd[0],-(int)F_SECSIZE,1))<0) { /* WE'VE READ EXTRA SECTOR,for wrap-around */
  411. sprintf(errstr,"sndseekEx() failed.\n");
  412. return(SYSTEM_ERROR);
  413. }
  414. samps_read -= F_SECSIZE;
  415. //TW: correcting an omission in original
  416. dz->samps_left += F_SECSIZE;
  417. }
  418. dz->total_samps_read += samps_read;
  419. dz->iparam[UNITS_READ] = samps_read/dz->infile->channels;
  420. dz->iparam[TOTAL_UNITS_READ] += dz->iparam[UNITS_READ];
  421. return(FINISHED);
  422. }
  423. /*************************** WRITE_SAMPS_WITH_INTIME_DISPLAY ***********************/
  424. int write_samps_with_intime_display(float *buf,int samps_to_write,dataptr dz)
  425. {
  426. int exit_status;
  427. int samps_written;
  428. // if((samps_written = fputfbufEx(buf,samps_to_write,dz->ofd))<0) {
  429. if(samps_to_write > 0) {
  430. if((exit_status = write_samps_no_report(buf,samps_to_write,&samps_written,dz))<0) {
  431. sprintf(errstr, "Can't write to output soundfile.\n");
  432. return(SYSTEM_ERROR);
  433. }
  434. display_virtual_time(dz->total_samps_read,dz);
  435. }
  436. return(FINISHED);
  437. }
  438. /************************ CONVERT_BRKPNTS ************************/
  439. int convert_brkpnts(dataptr dz)
  440. {
  441. int exit_status;
  442. double *p, *pend;
  443. if((exit_status= force_value_at_zero_time(VTRANS_TRANS,dz))<0)
  444. return(exit_status);
  445. if((exit_status= force_value_at_end_time(VTRANS_TRANS,dz))<0)
  446. return(exit_status);
  447. p = dz->brk[VTRANS_TRANS];
  448. pend = p + (dz->brksize[VTRANS_TRANS] * 2);
  449. while(p < pend) {
  450. *p = (double)round((*p) * dz->param[VTRANS_SR]); /* convert to (mono) sample frame */
  451. p += 2;
  452. }
  453. if(dz->mode==MOD_TRANSPOS_SEMIT || dz->mode==MOD_TRANSPOS_SEMIT_INFO) {
  454. p = dz->brk[VTRANS_TRANS] + 1;
  455. while(p < pend) {
  456. *p = pow(2.0,(*p) * OCTAVES_PER_SEMITONE); /* convert to ratios */
  457. p += 2;
  458. }
  459. }
  460. return(FINISHED);
  461. }
  462. /************************** TIMEVENTS *******************************/
  463. /*
  464. * Generates event positions from startsize and end size
  465. *
  466. * Let start-time be T0 and end time be T1
  467. * Let start size be S0 and end size be S1
  468. *
  469. * number of event is given by :-
  470. *
  471. * N = (T1-T0) log S1
  472. * ------- e__
  473. * (S1-S0) S0
  474. *
  475. * In general this will be non-integral, and we should
  476. * round N to an integer, and recalculate S1 by successive
  477. * approximation.
  478. *
  479. * Then positions of events are given by
  480. *
  481. * for X = 0 to N (S1-S0)
  482. * ----- X
  483. * T = (S1T0 - S0T1) S0(T1 - T0) (T1-T0)
  484. * s ----------- + ---------- e
  485. * (S1 - S0) (S1 - S0)
  486. *
  487. * If difference in eventsizes input to the main program is very small
  488. *
  489. * This function calculates the number of events within the time-interval,
  490. * and generates the times of these events.
  491. */
  492. int timevents(int *obufcnt,int intime0,int intime1,double insize0,double insize1,dataptr dz)
  493. {
  494. int exit_status;
  495. int number;
  496. double fnum, fnumber, error, pos;
  497. double lobound, hibound;
  498. int duration;
  499. if(flteq(insize0,0.0) || flteq(insize1,0.0)) {
  500. sprintf(errstr,"Event size of zero encountered in pair %lf %lf at time %lf\n",
  501. insize0,insize1,(double)intime0/dz->param[VTRANS_SR]);
  502. return(DATA_ERROR);
  503. }
  504. duration = intime1-intime0;
  505. if(duration<=0) {
  506. sprintf(errstr,"Inconsistent input times [ %lf : %lf ] to timevents()\n",
  507. (double)intime0/dz->param[VTRANS_SR],(double)intime1/dz->param[VTRANS_SR]);
  508. return(DATA_ERROR);
  509. }
  510. if(sizeq(insize1,insize0)) /* 1 */
  511. return(samesize(obufcnt,intime0,insize0,insize1,duration,dz));
  512. fnum =cntevents(duration,insize0,insize1); /* 2 */
  513. number = round(fnum);
  514. if(number<2) { /* 3 */
  515. pos = (double)(intime0 - dz->iparam[UNITS_RD_PRE_THISBUF]);
  516. if(pos >= (double)dz->iparam[UNITS_READ]) {
  517. if(dz->samps_left <= 0)
  518. return(FINISHED);
  519. if((exit_status = read_samps_for_vtrans(dz))<0)
  520. return(exit_status);
  521. pos -= (double)dz->iparam[UNIT_BUFLEN];
  522. }
  523. if((exit_status = putval(obufcnt,pos,dz))<0)
  524. return(exit_status);
  525. } else {
  526. fnumber = (double)number; /* 4 */
  527. error = fabs(fnum - fnumber);
  528. /* HIBOUND is a SMALLER value of SIZE to give BIGGER value of NUM-OF-SEGS */
  529. /* LOBOUND is a LARGER value of SIZE to give SMALLER value of NUM-OF-SEGS */
  530. lobound = max(insize1,insize0); /* 5 */
  531. hibound = min(insize1,insize0);
  532. if(error > FLTERR) /* 6 */
  533. insize1 = refinesize(hibound,lobound,fnumber,duration,error,insize0);
  534. else
  535. insize1 = (hibound+lobound)/2;
  536. return(getetime(obufcnt,intime0,intime1,insize0,insize1,number,dz));
  537. } /* 7 */
  538. return(CONTINUE);
  539. }
  540. /*************************** CNTEVENTS *****************************/
  541. double cntevents(int dur,double s0,double s1)
  542. {
  543. double f1 = (double)dur,f2;
  544. if(sizeq(s1,s0))
  545. return((f1*2.0)/(s1+s0));
  546. f2 = s1-s0;
  547. f1 /= f2;
  548. f2 = s1/s0;
  549. f2 = log(f2);
  550. f1 *= f2;
  551. return(fabs(f1));
  552. }
  553. /******************************* SAMESIZE(): *******************************
  554. *
  555. * get event positions, if eventsize approx same throughout segment.
  556. *
  557. *(1) Get average size, find number of events and round to nearest int.
  558. *(2) If too many or zero events, do exception.
  559. *(3) Recalculate size, and thence event times.
  560. */
  561. int samesize(int *obufcnt,int intime0,double insize0,double insize1,int duration,dataptr dz)
  562. {
  563. int exit_status;
  564. int number, isize;
  565. double size, pos; /* 1 */
  566. size = (insize0+insize1)/2;
  567. number = round((double)duration/size);
  568. if(number==0) { /* 2 */
  569. pos = (double)(intime0 - dz->iparam[UNITS_RD_PRE_THISBUF]);
  570. if(pos >= (double)dz->iparam[UNITS_READ]) {
  571. if(dz->samps_left <= 0)
  572. return(FINISHED);
  573. if((exit_status = read_samps_for_vtrans(dz))<0)
  574. return(exit_status);
  575. pos -= (double)dz->iparam[UNIT_BUFLEN];
  576. }
  577. if((exit_status = putval(obufcnt,pos,dz))<0)
  578. return(exit_status);
  579. return(CONTINUE);
  580. }
  581. size = (double)duration/(double)number;
  582. if(flteq((double)(isize = (int)round(size)),size))
  583. return integral_times(obufcnt,intime0,isize,number,dz);
  584. return unvarying_times(obufcnt,intime0,size,number,dz);
  585. }
  586. /************************ INTEGRAL_TIMES ***************************
  587. *
  588. * GENERATE vals for equally spaced events, spaced by whole nos. of samps.
  589. */
  590. int integral_times(int *obufcnt,int intime0,int isize,int number,dataptr dz)
  591. {
  592. int exit_status;
  593. int k, pos = intime0 - dz->iparam[UNITS_RD_PRE_THISBUF];
  594. for(k=0;k<number;k++) {
  595. if(pos >= dz->iparam[UNITS_READ]) {
  596. if(dz->samps_left <= 0)
  597. return(FINISHED);
  598. if((exit_status = read_samps_for_vtrans(dz))<0)
  599. return(exit_status);
  600. pos -= dz->iparam[UNIT_BUFLEN];
  601. }
  602. if((exit_status = putintval(obufcnt,pos,dz))<0)
  603. return(exit_status);
  604. pos += isize;
  605. }
  606. return(CONTINUE);
  607. }
  608. /****************************** PUTINTVAL ******************************/
  609. int putintval(int *obufcnt,int i,dataptr dz)
  610. {
  611. int exit_status;
  612. int n;
  613. float *buffer = dz->sampbuf[INBUF];
  614. float *obuf = dz->sampbuf[OUTBUF];
  615. i *= dz->infile->channels;
  616. for(n=0;n<dz->infile->channels;n++) {
  617. obuf[(*obufcnt)++] = buffer[i];
  618. if(*obufcnt >= dz->buflen) {
  619. if((exit_status = write_samps_with_intime_display(obuf,dz->buflen,dz))<0)
  620. return(exit_status);
  621. *obufcnt = 0;
  622. }
  623. i++;
  624. }
  625. return(FINISHED);
  626. }
  627. /************************ UNVARYING_TIMES ***************************
  628. *
  629. * GENERATE vals for equally spaced events.
  630. */
  631. int unvarying_times(int *obufcnt,int intime0,double size,int number,dataptr dz)
  632. {
  633. int exit_status;
  634. int k;
  635. double pos = (double)(intime0 - dz->iparam[UNITS_RD_PRE_THISBUF]);
  636. /******* k = 0 *********/
  637. for(k=0;k<number;k++) {
  638. /****************/
  639. if(pos >= (double)dz->iparam[UNITS_READ]) {
  640. if(dz->samps_left <= 0)
  641. return(FINISHED);
  642. if((exit_status = read_samps_for_vtrans(dz))<0)
  643. return(exit_status);
  644. pos -= (double)dz->iparam[UNIT_BUFLEN];
  645. }
  646. if((exit_status = putval(obufcnt,pos,dz))<0)
  647. return(exit_status);
  648. pos += size;
  649. }
  650. return(CONTINUE);
  651. }
  652. /******************************* GETETIME(): ********************************
  653. *
  654. * Calculate time-positions of events that vary in size between s0 and s1.
  655. * NB We need to invert the time order, if s1 < s0.
  656. */
  657. int getetime(int *obufcnt,int t0,int t1,double s0,double s1,int number,dataptr dz)
  658. {
  659. int exit_status;
  660. int n;
  661. double sdiff = s1-s0, tdiff = (double)(t1-t0), d1, d2, d3, pos;
  662. /***** TW n=0 NOT 1 ********/
  663. // for(n=1;n<number;n++) {
  664. for(n=0;n<number;n++) {
  665. /***** TW ********/
  666. d1 = sdiff/tdiff;
  667. d1 *= (double)n;
  668. d1 = exp(d1);
  669. d2 = s0*tdiff;
  670. d2 /= sdiff;
  671. d1 *= d2;
  672. d2 = s1*t0;
  673. d3 = s0*t1;
  674. d2 -= d3;
  675. d2 /= sdiff;
  676. pos = d1 + d2;
  677. pos -= (double)dz->iparam[UNITS_RD_PRE_THISBUF];
  678. if(pos >= (double)dz->iparam[UNITS_READ]) {
  679. if(dz->samps_left <= 0)
  680. return(FINISHED);
  681. if((exit_status = read_samps_for_vtrans(dz))<0)
  682. return(exit_status);
  683. pos -= (double)dz->iparam[UNIT_BUFLEN];
  684. }
  685. if((exit_status = putval(obufcnt,pos,dz))<0)
  686. return(exit_status);
  687. }
  688. return(CONTINUE);
  689. }
  690. /***************************** REFINESIZE(): ******************************
  691. *
  692. * refine size of final event to reduce error within bounds.
  693. */
  694. double refinesize(double hibound,double lobound,double fnumber,int duration,double error,double insize0)
  695. {
  696. double size, fnum, lasterror;
  697. double flterr_squared = FLTERR * FLTERR;
  698. do {
  699. lasterror = error;
  700. size = (hibound+lobound)/2.0;
  701. fnum = cntevents(duration,insize0,size);
  702. error = fabs(fnumber-fnum);
  703. if(error>FLTERR) {
  704. if(fnum<fnumber)
  705. lobound = size;
  706. else
  707. hibound = size;
  708. }
  709. if(fabs(lasterror - error) < flterr_squared)
  710. break; /* In case error cannot be reduced */
  711. } while(error > FLTERR);
  712. return(size);
  713. }
  714. /****************************** PUTVAL ******************************/
  715. int putval(int *obufcnt,double pos,dataptr dz)
  716. {
  717. int exit_status;
  718. int i = (int)pos; /* TRUNCATE */
  719. double frac = pos - (double)i, diff;
  720. float val1, val2;
  721. float *buffer = dz->sampbuf[INBUF];
  722. float *obuf = dz->sampbuf[OUTBUF];
  723. int chans = dz->infile->channels;
  724. int n;
  725. i *= chans;
  726. for(n=0;n<chans;n++) {
  727. val1 = buffer[i];
  728. val2 = buffer[i+chans];
  729. diff = (double)(val2-val1);
  730. //TW obuf[(*obufcnt)++] = val1 + (float)/*round*/(frac * diff);
  731. obuf[(*obufcnt)++] = (float)(val1 + (frac * diff));
  732. if(*obufcnt >= dz->buflen) {
  733. if((exit_status = write_samps_with_intime_display(obuf,dz->buflen,dz))<0)
  734. return(exit_status);
  735. *obufcnt = 0;
  736. }
  737. i++;
  738. }
  739. return(CONTINUE);
  740. }
  741. /**************************** SIZEQ *******************************/
  742. int sizeq(double f1,double f2)
  743. {
  744. double upperbnd, lowerbnd;
  745. upperbnd = f2 + CALCLIM;
  746. lowerbnd = f2 - CALCLIM;
  747. if((f1>upperbnd) || (f1<lowerbnd))
  748. return(FALSE);
  749. return(TRUE);
  750. }
  751. /********************** CHANGE_FRAME *****************************/
  752. int change_frame(dataptr dz)
  753. {
  754. int n, m;
  755. int *newtime;
  756. double *dbrk = dz->brk[VTRANS_TRANS];
  757. double lasttime, lastval, thistime, thisval, intime, outtime;
  758. if((newtime = (int *)malloc(dz->brksize[VTRANS_TRANS] * 2 * sizeof(double)))==NULL) {
  759. sprintf(errstr,"INSUFFICIENT MEMORY to create transformed brktable.\n");
  760. return(MEMORY_ERROR);
  761. }
  762. newtime[0] = 0;
  763. lasttime = dbrk[0];
  764. lastval = dbrk[1];
  765. switch(dz->mode) {
  766. case(MOD_TRANSPOS_INFO):
  767. case(MOD_TRANSPOS_SEMIT_INFO):
  768. for(n=1,m=2;n<dz->brksize[VTRANS_TRANS];n++,m+=2) {
  769. thistime = dbrk[m];
  770. thisval = dbrk[m+1];
  771. if(dz->vflag[VTRANS_OUTTIMES])
  772. newtime[n] = newtime[n-1] + inv_cntevents((int)round(thistime - lasttime),lastval,thisval);
  773. else
  774. newtime[n] = newtime[n-1] + round(cntevents((int)round(thistime - lasttime),lastval,thisval));
  775. lasttime = thistime;
  776. lastval = thisval;
  777. }
  778. print_outmessage_flush("TIME GIVEN\tOCCURS AT OUTTIME\n");
  779. for(n=0,m=0;n<dz->brksize[VTRANS_TRANS];n++,m+=2) {
  780. intime = dbrk[m]/dz->param[VTRANS_SR];
  781. outtime = (double)newtime[n]/dz->param[VTRANS_SR];
  782. sprintf(errstr,"%lf\t%lf\n",intime,outtime);
  783. print_outmessage_flush(errstr);
  784. }
  785. if(!sloom) {
  786. fprintf(stdout,"INFO: END\n");
  787. fflush(stdout);
  788. }
  789. break;
  790. case(MOD_TRANSPOS):
  791. case(MOD_TRANSPOS_SEMIT):
  792. if(dz->vflag[VTRANS_OUTTIMES]) { /* convert to intimes */
  793. for(n=1,m=2;n<dz->brksize[VTRANS_TRANS];n++,m+=2) {
  794. thistime = dbrk[m];
  795. thisval = dbrk[m+1];
  796. newtime[n] = newtime[n-1] +
  797. inv_cntevents((int)round(thistime - lasttime),lastval,thisval);
  798. lasttime = thistime;
  799. lastval = thisval;
  800. }
  801. for(n=1,m=2;n<dz->brksize[VTRANS_TRANS];n++,m+=2)
  802. dbrk[m] = (double)newtime[n];
  803. }
  804. break;
  805. }
  806. free(newtime);
  807. return(FINISHED);
  808. }
  809. /*************************** INV_CNTEVENTS *****************************
  810. *
  811. * no. of event in input (N) corresponding to output (T1-T0) given by :-
  812. *
  813. * N = (T1-T0) log S1
  814. * ------- e__
  815. * (S1-S0) S0
  816. *
  817. * Hence,
  818. * no. of event in output (T1-T0) corresponding to input (N) given by :-
  819. *
  820. * (S1-S0)N = (T1-T0)
  821. * --------
  822. * log S1
  823. * e__
  824. * S0
  825. *
  826. * Except where S1==S0, when (T1-T0) = S * N
  827. */
  828. int inv_cntevents(int dur,double s0,double s1)
  829. {
  830. double ftime;
  831. int time;
  832. if(sizeq(s1,s0))
  833. ftime = (double)dur * s0;
  834. else
  835. ftime = ((double)dur * (s1-s0))/log(s1/s0);
  836. time = round(ftime);
  837. if(time==0)
  838. time = 1;
  839. return(time);
  840. }
  841. /*************************** FORCE_VALUE_AT_END_TIME *****************************/
  842. int force_value_at_end_time(int paramno,dataptr dz)
  843. {
  844. double duration = (double)dz->iparam[UNITSIZE]/dz->param[VTRANS_SR];
  845. double *p = dz->brk[paramno] + ((dz->brksize[paramno]-1) * 2);
  846. double timegap, newgap, step, newstep, timeratio;
  847. if(dz->brksize[paramno] < 2) {
  848. sprintf(errstr,"Brktable too small (< 2 pairs).\n");
  849. return(DATA_ERROR);
  850. }
  851. if(flteq(*p,duration)) {
  852. *p = duration;
  853. return(FINISHED);
  854. } else if(*p < duration) {
  855. dz->brksize[paramno]++;
  856. if((dz->brk[paramno] = realloc((char *)dz->brk[paramno],dz->brksize[paramno] * 2 * sizeof(double)))==NULL) {
  857. sprintf(errstr,"INSUFFICIENT MEMORY TO GROW BRKTABLE.\n");
  858. return(MEMORY_ERROR);
  859. }
  860. p = dz->brk[paramno] + ((dz->brksize[paramno]-1) * 2);
  861. *p = duration;
  862. *(p+1) = *(p-1);
  863. return(FINISHED);
  864. } else { /* *p > duration) */
  865. while(p > dz->brk[paramno]) {
  866. p -= 2;
  867. if(flteq(*p,duration)) {
  868. *p = duration;
  869. dz->brksize[paramno] = (p - dz->brk[paramno] + 2)/2;
  870. return(FINISHED);
  871. } else if(*p < duration) {
  872. timegap = *(p+2) - *p;
  873. newgap = duration - *p;
  874. timeratio = newgap/timegap;
  875. switch(dz->mode) {
  876. case(MOD_TRANSPOS_SEMIT):
  877. case(MOD_TRANSPOS_SEMIT_INFO):
  878. step = *(p+3) - *(p+1);
  879. newstep = step * timeratio;
  880. *(p+3) = *(p+1) + newstep;
  881. break;
  882. case(MOD_TRANSPOS):
  883. case(MOD_TRANSPOS_INFO):
  884. step = LOG2(*(p+3)) - LOG2(*(p+1));
  885. newstep = pow(2.0,step * timeratio);
  886. *(p+3) = *(p+1) * newstep;
  887. break;
  888. }
  889. *(p+2) = duration;
  890. dz->brksize[paramno] = (p - dz->brk[paramno] + 4)/2;
  891. return(FINISHED);
  892. }
  893. }
  894. }
  895. sprintf(errstr,"Failed to place brkpnt at snd endtime.\n");
  896. return(PROGRAM_ERROR);
  897. }
  898. /*************************** SPLICE_END *****************************/
  899. void splice_end(int obufcnt,dataptr dz) /* A kludge to avoid clicks at end */
  900. {
  901. #define VTRANS_SPLICELEN (5.0)
  902. float *buf = dz->sampbuf[OUTBUF];
  903. int chans = dz->infile->channels;
  904. int splicelen = round(VTRANS_SPLICELEN * MS_TO_SECS * dz->infile->srate) * dz->infile->channels;
  905. int n, m, k = min(obufcnt,splicelen);
  906. int startsamp = obufcnt - k;
  907. double inv_k;
  908. k /= chans;
  909. inv_k = 1.0/(double)k;
  910. for(n = k-1;n >= 0;n--) {
  911. for(m=0;m<chans;m++) {
  912. buf[startsamp] = (float)(buf[startsamp] * n * inv_k);
  913. startsamp++;
  914. }
  915. }
  916. }
  917. /******************************* DO_ACCELERATION ***************************/
  918. int do_acceleration(dataptr dz)
  919. {
  920. int exit_status;
  921. int place;
  922. double flplace, fracsamp, step;
  923. // int OK = 1, interp;
  924. int OK = 1;
  925. double interp;
  926. float *ibuf = dz->sampbuf[INBUF];
  927. float *obuf = dz->sampbuf[OUTBUF];
  928. float *true_lbuf = dz->sampbuf[TRUE_LBUF];
  929. float *true_rbuf = dz->sampbuf[TRUE_RBUF];
  930. float *obufend = dz->sampbuf[OBUFEND];
  931. float *obufptr = obuf;
  932. double place_inc = 1.0;
  933. int startsamp = dz->iparam[ACCEL_STARTTIME];
  934. int previous_total_ssampsread = 0;
  935. double accel = dz->param[ACCEL_ACCEL];
  936. dz->total_samps_read = 0;
  937. display_virtual_time(0L,dz);
  938. if(dz->infile->channels==STEREO) {
  939. while(dz->total_samps_read<=startsamp) {
  940. previous_total_ssampsread = dz->total_samps_read;
  941. if((exit_status = read_wrapped_samps_stereo(dz))<0)
  942. return(exit_status);
  943. if(dz->total_samps_read <= startsamp)
  944. if((exit_status = write_samps_with_intime_display(ibuf,dz->ssampsread * STEREO,dz))<0)
  945. return(exit_status);
  946. }
  947. startsamp -= previous_total_ssampsread;
  948. if(startsamp > 0)
  949. memcpy((char *)obuf,(char *)ibuf,startsamp * sizeof(float));
  950. obufptr += startsamp;
  951. startsamp /= 2; /* STEREO divided into two buffers */
  952. place = startsamp + 1;
  953. flplace = place;
  954. while(dz->ssampsread > place) {
  955. while(place < dz->ssampsread) {
  956. fracsamp = flplace - (double)place;
  957. step = true_lbuf[place+1] - true_lbuf[place];
  958. // interp = round(fracsamp * step);
  959. interp = fracsamp * step;
  960. *obufptr++ = (float)(true_lbuf[place] + interp);
  961. step = true_rbuf[place+1] - true_rbuf[place];
  962. // interp = round(fracsamp * step);
  963. interp = fracsamp * step;
  964. *obufptr++ = (float)(true_rbuf[place] + interp);
  965. if(obufptr >= obufend) {
  966. if((exit_status = write_samps_with_intime_display(obuf,dz->buflen,dz))<0)
  967. return(exit_status);
  968. obufptr = obuf;
  969. }
  970. flplace += place_inc;
  971. place_inc *= accel;
  972. if(place_inc <= MININC) {
  973. fprintf(stdout, "INFO: Acceleration reached black hole! - finishing\n");
  974. fflush(stdout);
  975. OK = 0;
  976. break;
  977. }
  978. place = (int)flplace;
  979. }
  980. if(!OK) {
  981. break;
  982. }
  983. if(dz->samps_left > 0) {
  984. flplace -= (double)dz->ssampsread;
  985. place -= dz->ssampsread;
  986. if((exit_status = read_wrapped_samps_stereo(dz))<0)
  987. return(exit_status);
  988. if(dz->ssampsread<=0)
  989. break;
  990. } else
  991. break;
  992. }
  993. } else {
  994. while(dz->total_samps_read<=startsamp) {
  995. previous_total_ssampsread = dz->total_samps_read;
  996. if((exit_status = read_wrapped_samps_mono(dz))<0)
  997. return(exit_status);
  998. if(dz->total_samps_read <= startsamp) {
  999. if((exit_status = write_samps_with_intime_display(ibuf,dz->ssampsread,dz))<0)
  1000. return(exit_status);
  1001. }
  1002. }
  1003. startsamp -= previous_total_ssampsread;
  1004. if(startsamp > 0)
  1005. memcpy((char *)obuf,(char *)ibuf,startsamp * sizeof(float));
  1006. obufptr += startsamp;
  1007. place = startsamp + 1;
  1008. flplace = place;
  1009. while(dz->ssampsread > place) {
  1010. while(place < dz->ssampsread) {
  1011. fracsamp = flplace - (double)place;
  1012. step = true_lbuf[place+1] - true_lbuf[place];
  1013. // interp = round(fracsamp * step);
  1014. interp = fracsamp * step;
  1015. *obufptr++ = (float)(true_lbuf[place] + interp);
  1016. if(obufptr >= obufend) {
  1017. if((exit_status = write_samps_with_intime_display(obuf,dz->buflen,dz))<0)
  1018. return(exit_status);
  1019. obufptr = obuf;
  1020. }
  1021. flplace += place_inc;
  1022. place_inc *= accel;
  1023. if(place_inc <= MININC) {
  1024. fprintf(stdout, "INFO: Acceleration reached black hole! - finishing\n\n");
  1025. fflush(stdout);
  1026. OK = 0;
  1027. break;
  1028. }
  1029. place = (int)flplace;
  1030. }
  1031. if(!OK) {
  1032. break;
  1033. }
  1034. if(dz->samps_left > 0) {
  1035. flplace -= (double)dz->ssampsread;
  1036. place -= dz->ssampsread;
  1037. if((exit_status = read_wrapped_samps_mono(dz))<0)
  1038. return(exit_status);
  1039. if(dz->ssampsread<=0)
  1040. break;
  1041. } else
  1042. break;
  1043. }
  1044. }
  1045. splice_end(obufptr - obuf,dz);
  1046. return write_samps_with_intime_display(obuf,obufptr - obuf ,dz);
  1047. }
  1048. /******************************* DO_VIBRATO ********************************/
  1049. int do_vibrato(dataptr dz)
  1050. {
  1051. int exit_status;
  1052. float *ibuf = dz->sampbuf[INBUF];
  1053. float *obuf = dz->sampbuf[OUTBUF];
  1054. float *obufend = dz->sampbuf[OBUFEND];
  1055. float *true_lbuf = dz->sampbuf[TRUE_LBUF];
  1056. float *true_rbuf = NULL;
  1057. double *sintab = dz->parray[VIB_SINTAB];
  1058. int chans = dz->infile->channels;
  1059. double sr = (double)dz->infile->srate;
  1060. double tabsize = (double)VIB_TABSIZE;
  1061. double bloksize = (double)VIB_BLOKSIZE;
  1062. double inv_bloksize = 1.0/bloksize;
  1063. double thistime = 0.0;
  1064. double time_incr = bloksize/sr; /* timestep between param reads */
  1065. int effective_buflen = dz->buflen/chans;
  1066. int place = 0; /* integer position in sndfile */
  1067. double flplace = 0.0; /* float position in sndfile */
  1068. double incr; /* incr of position in sndfile */
  1069. double sfindex = 0.0; /* float position in sintable */
  1070. double sinval; /* current sintable value */
  1071. double current_depth, lastdepth, depth_incr;
  1072. double current_frq, lastfrq, frq_incr;
  1073. double fracsamp; /* fraction of samptime to interp*/
  1074. double step; /* step between snd samples */
  1075. // int interp; /* part of sampstep to use */
  1076. double interp; /* part of sampstep to use */
  1077. int blokcnt = 0; /* distance through blokd params */
  1078. float *obufptr = obuf;
  1079. if(dz->brksize[VIB_FRQ]) {
  1080. if((exit_status = read_value_from_brktable(thistime,VIB_FRQ,dz))<0)
  1081. return(exit_status);
  1082. } else
  1083. frq_incr = 0.0;
  1084. if(dz->brksize[VIB_DEPTH]) {
  1085. if((exit_status = read_value_from_brktable(thistime,VIB_DEPTH,dz))<0)
  1086. return(exit_status);
  1087. } else
  1088. depth_incr = 0.0;
  1089. current_frq = lastfrq = dz->param[VIB_FRQ];
  1090. current_depth = lastdepth = dz->param[VIB_DEPTH];
  1091. thistime += time_incr; /* now time is 1 bloklength ahead */
  1092. switch(chans) {
  1093. case(MONO):
  1094. if((exit_status = read_wrapped_samps_mono(dz))<0)
  1095. return(exit_status);
  1096. obuf[0] = ibuf[0];
  1097. obufptr++;
  1098. break;
  1099. case(STEREO):
  1100. true_rbuf = dz->sampbuf[TRUE_RBUF];
  1101. if((exit_status = read_wrapped_samps_stereo(dz))<0)
  1102. return(exit_status);
  1103. obuf[0] = ibuf[0];
  1104. obufptr++;
  1105. obuf[1] = ibuf[1];
  1106. obufptr++;
  1107. break;
  1108. }
  1109. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  1110. return(exit_status); /* read value 1 bloklength ahead */
  1111. frq_incr = (dz->param[VIB_FRQ] - lastfrq) * inv_bloksize;
  1112. depth_incr = (dz->param[VIB_DEPTH] - lastdepth) * inv_bloksize;
  1113. lastfrq = dz->param[VIB_FRQ];
  1114. lastdepth = dz->param[VIB_DEPTH];
  1115. blokcnt = VIB_BLOKSIZE - 1; /* -1 as we've read 1 (stereo) sample already. */
  1116. thistime += time_incr; /* Now time is 2 bloklengths ahead */
  1117. /* but we won't read till another 1 blok has passed. */
  1118. for(;;) {
  1119. if(--blokcnt <= 0) { /* look at params only every BLOKCNT (stereo)samples */
  1120. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  1121. return(exit_status); /* & read vals 1 bloklen ahead,to calc par incr */
  1122. frq_incr = (dz->param[VIB_FRQ] - lastfrq) * inv_bloksize;
  1123. depth_incr = (dz->param[VIB_DEPTH] - lastdepth) * inv_bloksize;
  1124. lastfrq = dz->param[VIB_FRQ];
  1125. lastdepth = dz->param[VIB_DEPTH];
  1126. blokcnt = VIB_BLOKSIZE;
  1127. thistime += time_incr;
  1128. }
  1129. current_depth += depth_incr; /* increment params sample by (stereo) sample */
  1130. current_frq += frq_incr;
  1131. if((sfindex += current_frq) >= tabsize)
  1132. sfindex -= tabsize; /* advance in sintable,wrapping around if ness */
  1133. sinval = interp_read_sintable(sintab,sfindex); /* read sintab by interpolation */
  1134. incr = pow(2.0,sinval * current_depth); /* convert speed & depth from 8vas to ratio */
  1135. if((flplace += incr) >= (double)dz->ssampsread) { /* advance through src sound */
  1136. if(dz->samps_left <=0) /* if at end of src, finish */
  1137. break; /* else if at end of buffer, get more sound in */
  1138. switch(chans) {
  1139. case(MONO):
  1140. if((exit_status = read_wrapped_samps_mono(dz))<0)
  1141. return(exit_status);
  1142. break;
  1143. case(STEREO):
  1144. if((exit_status = read_wrapped_samps_stereo(dz))<0)
  1145. return(exit_status);
  1146. break;
  1147. }
  1148. flplace -= effective_buflen;
  1149. }
  1150. place = (int)flplace; /* TRUNCATE */ /* read sndfile by interpolation */
  1151. fracsamp = flplace - (double)place;
  1152. step = true_lbuf[place+1] - true_lbuf[place];
  1153. // interp = round(fracsamp * step);
  1154. interp = fracsamp * step;
  1155. *obufptr++ = (float)(true_lbuf[place] + interp);
  1156. if(chans==STEREO) {
  1157. step = true_rbuf[place+1] - true_rbuf[place];
  1158. // interp = round(fracsamp * step);
  1159. interp = fracsamp * step;
  1160. *obufptr++ = (float)(true_rbuf[place] + interp);
  1161. }
  1162. if(obufptr >= obufend) { /* if at end of outbuf, write a buffer */
  1163. if((exit_status = write_samps_with_intime_display(obuf,dz->buflen,dz))<0)
  1164. return(exit_status);
  1165. obufptr = obuf;
  1166. }
  1167. }
  1168. if(obufptr < obufend) /* if samples remain in outbuf, write them */
  1169. return write_samps_with_intime_display(obuf,obufptr - obuf,dz);
  1170. return(FINISHED);
  1171. }
  1172. /******************************* INTERP_READ_SINTABLE ********************************/
  1173. double interp_read_sintable(double *sintab,double sfindex)
  1174. {
  1175. int sindex0 = (int)sfindex; /* TRUNCATE */
  1176. int sindex1 = sindex0 + 1;
  1177. double frac = sfindex - (double)sindex0;
  1178. double val0 = sintab[sindex0];
  1179. double val1 = sintab[sindex1];
  1180. double valdiff = val1 - val0;
  1181. double valfrac = valdiff * frac;
  1182. return(val0 + valfrac);
  1183. }