ap_grain.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /*
  2. * Copyright (c) 1983-2020 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 aint 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 <string.h>
  25. #include <math.h>
  26. #include <ctype.h>
  27. #include <structures.h>
  28. #include <cdpmain.h>
  29. #include <tkglobals.h>
  30. #include <pnames.h>
  31. #include <grain.h>
  32. #include <processno.h>
  33. #include <modeno.h>
  34. #include <globcon.h>
  35. #include <logic.h>
  36. #include <filetype.h>
  37. #include <mixxcon.h>
  38. #include <flags.h>
  39. #include <speccon.h>
  40. #include <arrays.h>
  41. #include <special.h>
  42. #include <formants.h>
  43. #include <sfsys.h>
  44. #include <osbind.h>
  45. #include <srates.h>
  46. #define FSECSIZE (256)
  47. #define maxtime scalefact
  48. //#ifdef unix
  49. #define round(x) lround((x))
  50. //#endif
  51. static int create_grain_sndbufs(dataptr dz);
  52. static void establish_grain_envelope_windowsize_in_samps(dataptr dz);
  53. static int create_rrr_sndbufs(dataptr dz);
  54. static int check_grain_consistency(dataptr dz);
  55. static int get_reorder_sequence(char *str,dataptr dz);
  56. static int get_grain_ratios(int special,char *filename,dataptr dz);
  57. static int get_two_grain_ratios(char *filename,dataptr dz);
  58. static int get_grain_synctimes(char *filename,dataptr dz);
  59. static int generate_samp_windowsizer(dataptr dz);
  60. static int create_ssss_sndbufs(dataptr dz);
  61. /***************************** ESTABLISH_BUFPTRS_AND_EXTRA_BUFFERS **************************/
  62. int establish_bufptrs_and_extra_buffers(dataptr dz)
  63. {
  64. // int is_spec = FALSE;
  65. dz->extra_bufcnt = -1; /* ENSURE EVERY CASE HAS A PAIR OF ENTRIES !! */
  66. dz->bptrcnt = 0;
  67. dz->bufcnt = 0;
  68. switch(dz->process) {
  69. case(GRAIN_COUNT):
  70. case(GRAIN_GET):
  71. case(GRAIN_ASSESS):
  72. dz->extra_bufcnt = 0; dz->bufcnt = 2;
  73. break;
  74. case(GRAIN_OMIT):
  75. case(GRAIN_DUPLICATE):
  76. case(GRAIN_REORDER):
  77. case(GRAIN_REPITCH):
  78. case(GRAIN_TIMEWARP):
  79. case(GRAIN_REVERSE):
  80. dz->extra_bufcnt = 2; dz->bufcnt = 3;
  81. break;
  82. case(GRAIN_RERHYTHM):
  83. case(GRAIN_REMOTIF):
  84. case(GRAIN_POSITION):
  85. case(GRAIN_ALIGN):
  86. dz->extra_bufcnt = 3; dz->bufcnt = 3;
  87. break;
  88. case(RRRR_EXTEND):
  89. case(SSSS_EXTEND):
  90. dz->extra_bufcnt = 0; dz->bufcnt = 2;
  91. break;
  92. case(GREV):
  93. dz->extra_bufcnt = 0;
  94. if(dz->mode == GREV_TSTRETCH || dz->mode == GREV_PUT)
  95. dz->bufcnt = 3;
  96. else
  97. dz->bufcnt = 2;
  98. break;
  99. default:
  100. sprintf(errstr,"Unknown program type [%d] in establish_bufptrs_and_extra_buffers()\n",dz->process);
  101. return(PROGRAM_ERROR);
  102. }
  103. if(dz->extra_bufcnt < 0) {
  104. sprintf(errstr,"bufcnts have not been set: establish_bufptrs_and_extra_buffers()\n");
  105. return(PROGRAM_ERROR);
  106. }
  107. return establish_groucho_bufptrs_and_extra_buffers(dz);
  108. }
  109. /***************************** SETUP_INTERNAL_ARRAYS_AND_ARRAY_POINTERS **************************/
  110. int setup_internal_arrays_and_array_pointers(dataptr dz)
  111. {
  112. int n;
  113. dz->ptr_cnt = -1; /* base constructor...process */
  114. dz->array_cnt = -1;
  115. dz->iarray_cnt = -1;
  116. dz->larray_cnt = -1;
  117. switch(dz->process) {
  118. case(GRAIN_COUNT):
  119. case(GRAIN_ASSESS):
  120. dz->array_cnt = 2; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 4; dz->fptr_cnt = 0; break;
  121. case(GRAIN_REORDER):
  122. dz->array_cnt = 3; dz->iarray_cnt = 1; dz->larray_cnt = 2; dz->ptr_cnt = 4; dz->fptr_cnt = 0; break;
  123. case(GRAIN_RERHYTHM):
  124. case(GRAIN_REPITCH):
  125. case(GRAIN_REMOTIF):
  126. dz->array_cnt = 4; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 4; dz->fptr_cnt = 0; break;
  127. case(GRAIN_ALIGN):
  128. case(GRAIN_GET):
  129. case(GRAIN_POSITION):
  130. dz->array_cnt = 4; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 4; dz->fptr_cnt = 0; break;
  131. case(GRAIN_OMIT):
  132. case(GRAIN_DUPLICATE):
  133. case(GRAIN_TIMEWARP):
  134. dz->array_cnt = 3; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 4; dz->fptr_cnt = 0; break;
  135. case(GRAIN_REVERSE):
  136. dz->array_cnt = 3; dz->iarray_cnt = 0; dz->larray_cnt = 1; dz->ptr_cnt = 4; dz->fptr_cnt = 0; break;
  137. case(RRRR_EXTEND):
  138. dz->array_cnt = 1; dz->iarray_cnt = 0; dz->larray_cnt = 1; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  139. case(SSSS_EXTEND):
  140. dz->array_cnt = 0; dz->iarray_cnt = 0; dz->larray_cnt = 1; dz->ptr_cnt = 0; dz->fptr_cnt = 0; break;
  141. case(GREV):
  142. if(dz->mode == GREV_PUT) {
  143. dz->array_cnt = 4; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 0; dz->fptr_cnt = 0;
  144. } else {
  145. dz->array_cnt = 0; dz->iarray_cnt = 0; dz->larray_cnt = 0; dz->ptr_cnt = 0; dz->fptr_cnt = 0;
  146. }
  147. break;
  148. }
  149. /*** WARNING ***
  150. ANY APPLICATION DEALING WITH A NUMLIST INPUT: MUST establish AT LEAST 1 double array: i.e. dz->array_cnt = at least 1
  151. **** WARNING ***/
  152. if(dz->array_cnt < 0 || dz->iarray_cnt < 0 || dz->larray_cnt < 0 || dz->ptr_cnt < 0 || dz->fptr_cnt < 0) {
  153. sprintf(errstr,"array_cnt not set in setup_internal_arrays_and_array_pointers()\n");
  154. return(PROGRAM_ERROR);
  155. }
  156. if(dz->array_cnt > 0) {
  157. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  158. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  159. return(MEMORY_ERROR);
  160. }
  161. for(n=0;n<dz->array_cnt;n++)
  162. dz->parray[n] = NULL;
  163. }
  164. if(dz->iarray_cnt > 0) {
  165. if((dz->iparray = (int **)malloc(dz->iarray_cnt * sizeof(int *)))==NULL) {
  166. sprintf(errstr,"INSUFFICIENT MEMORY for internal int arrays.\n");
  167. return(MEMORY_ERROR);
  168. }
  169. for(n=0;n<dz->iarray_cnt;n++)
  170. dz->iparray[n] = NULL;
  171. }
  172. if(dz->larray_cnt > 0) {
  173. if((dz->lparray = (int **)malloc(dz->larray_cnt * sizeof(int *)))==NULL) {
  174. sprintf(errstr,"INSUFFICIENT MEMORY for internal int arrays.\n");
  175. return(MEMORY_ERROR);
  176. }
  177. for(n=0;n<dz->larray_cnt;n++)
  178. dz->lparray[n] = NULL;
  179. }
  180. if(dz->ptr_cnt > 0) {
  181. if((dz->ptr = (double **)malloc(dz->ptr_cnt * sizeof(double *)))==NULL) {
  182. sprintf(errstr,"INSUFFICIENT MEMORY for internal pointer arrays.\n");
  183. return(MEMORY_ERROR);
  184. }
  185. for(n=0;n<dz->ptr_cnt;n++)
  186. dz->ptr[n] = NULL;
  187. }
  188. if(dz->fptr_cnt > 0) {
  189. if((dz->fptr = (float **)malloc(dz->fptr_cnt * sizeof(float *)))==NULL) {
  190. sprintf(errstr,"INSUFFICIENT MEMORY for internal float-pointer arrays.\n");
  191. return(MEMORY_ERROR);
  192. }
  193. for(n=0;n<dz->fptr_cnt;n++)
  194. dz->fptr[n] = NULL;
  195. }
  196. return(FINISHED);
  197. }
  198. /****************************** ASSIGN_PROCESS_LOGIC *********************************/
  199. int assign_process_logic(dataptr dz)
  200. {
  201. switch(dz->process) {
  202. case(GRAIN_ASSESS):
  203. case(GRAIN_COUNT): setup_process_logic(SNDFILES_ONLY, SCREEN_MESSAGE, NO_OUTPUTFILE, dz); break;
  204. case(GRAIN_OMIT): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  205. case(GRAIN_DUPLICATE): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  206. case(GRAIN_REORDER): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  207. case(GRAIN_REPITCH): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  208. case(GRAIN_RERHYTHM): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  209. case(GRAIN_REMOTIF): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  210. case(GRAIN_TIMEWARP): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  211. case(GRAIN_REVERSE): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  212. case(GRAIN_POSITION): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  213. case(GRAIN_ALIGN): setup_process_logic(TWO_SNDFILES, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  214. case(GRAIN_GET): setup_process_logic(SNDFILES_ONLY, TO_TEXTFILE, TEXTFILE_OUT, dz); break;
  215. case(RRRR_EXTEND): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  216. case(SSSS_EXTEND): setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz); break;
  217. case(GREV):
  218. if(dz->mode==GREV_GET)
  219. setup_process_logic(SNDFILES_ONLY, TO_TEXTFILE, TEXTFILE_OUT, dz);
  220. else
  221. setup_process_logic(SNDFILES_ONLY, UNEQUAL_SNDFILE, SNDFILE_OUT, dz);
  222. break;
  223. default:
  224. sprintf(errstr,"Unknown process: assign_process_logic()\n");
  225. return(PROGRAM_ERROR);
  226. break;
  227. }
  228. if(dz->has_otherfile) {
  229. switch(dz->input_data_type) {
  230. case(ALL_FILES):
  231. case(TWO_SNDFILES):
  232. case(SNDFILE_AND_ENVFILE):
  233. case(SNDFILE_AND_BRKFILE):
  234. case(SNDFILE_AND_UNRANGED_BRKFILE):
  235. case(SNDFILE_AND_DB_BRKFILE):
  236. break;
  237. case(MANY_SNDFILES):
  238. if(dz->process==INFO_TIMELIST)
  239. break;
  240. /* fall thro */
  241. default:
  242. sprintf(errstr,"Most processes accepting files with different properties\n"
  243. "can only take 2 sound infiles.\n");
  244. return(PROGRAM_ERROR);
  245. }
  246. }
  247. return(FINISHED);
  248. }
  249. /***************************** SET_LEGAL_INFILE_STRUCTURE **************************
  250. *
  251. * Allows 2nd infile to have different props to first infile.
  252. */
  253. void set_legal_infile_structure(dataptr dz)
  254. {
  255. switch(dz->process) {
  256. case(GRAIN_ALIGN):
  257. dz->has_otherfile = TRUE;
  258. break;
  259. default:
  260. dz->has_otherfile = FALSE;
  261. break;
  262. }
  263. }
  264. /***************************************************************************************/
  265. /****************************** FORMERLY IN internal.c *********************************/
  266. /***************************************************************************************/
  267. /****************************** SET_LEGAL_INTERNALPARAM_STRUCTURE *********************************/
  268. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  269. {
  270. int exit_status = FINISHED;
  271. mode = 0;
  272. switch(process) {
  273. case(GRAIN_COUNT): case(GRAIN_OMIT): case(GRAIN_REPITCH):
  274. case(GRAIN_TIMEWARP): case(GRAIN_GET): case(GRAIN_DUPLICATE):
  275. case(GRAIN_RERHYTHM): case(GRAIN_ALIGN): case(GRAIN_POSITION):
  276. case(GRAIN_REORDER): case(GRAIN_REMOTIF): case(GRAIN_REVERSE):
  277. exit_status = set_internalparam_data("diiiiiiiiidddiiiii",ap); break;
  278. case(GRAIN_ASSESS):
  279. exit_status = set_internalparam_data("00ddd0diiiiiiiiidddiiiii",ap); break;
  280. case(RRRR_EXTEND):
  281. exit_status = set_internalparam_data("di",ap);
  282. break;
  283. case(SSSS_EXTEND):
  284. break;
  285. case(GREV):
  286. exit_status = set_internalparam_data("i",ap);
  287. break;
  288. default:
  289. sprintf(errstr,"Unknown process in set_legal_internalparam_structure()\n");
  290. return(PROGRAM_ERROR);
  291. }
  292. return(exit_status);
  293. }
  294. /********************************************************************************************/
  295. /********************************** FORMERLY IN specialin.c *********************************/
  296. /********************************************************************************************/
  297. /********************** READ_SPECIAL_DATA ************************/
  298. int read_special_data(char *str,dataptr dz)
  299. {
  300. /*int exit_status = FINISHED;*/
  301. aplptr ap = dz->application;
  302. switch(ap->special_data) {
  303. case(GRAIN_REORDER_STRING): return get_reorder_sequence(str,dz);
  304. case(GRAIN_PITCH_RATIOS):
  305. case(GRAIN_TIME_RATIOS): return get_grain_ratios((int)ap->special_data,str,dz);
  306. case(GRAIN_TWO_RATIOS): return get_two_grain_ratios(str,dz);
  307. case(GRAIN_TIMINGS): return get_grain_synctimes(str,dz);
  308. break;
  309. default:
  310. sprintf(errstr,"Unknown special_data type: read_special_data()\n");
  311. return(PROGRAM_ERROR);
  312. }
  313. return(FINISHED); /* NOTREACHED */
  314. }
  315. /********************************* GET_REORDER_SEQUENCE ***************************/
  316. int get_reorder_sequence(char *str,dataptr dz)
  317. {
  318. char minval = 'z' + 1;
  319. char maxval = -1;
  320. char nextval, separator = ':';
  321. int OK = FALSE;
  322. int n, setcnt = strlen(str);
  323. for(n=0;n<setcnt;n++) {
  324. if(str[n]==':' || str[n]=='-') {
  325. separator = str[n];
  326. OK = TRUE;
  327. break;
  328. }
  329. }
  330. if(!OK) {
  331. sprintf(errstr,"Reorder sequence does not contain a separator\n");
  332. return(DATA_ERROR);
  333. }
  334. if(str[setcnt-2] != separator) { /* Check for separator */
  335. sprintf(errstr,"Penultimate character in Reorder sequence should be the separator.\n");
  336. return(DATA_ERROR);
  337. }
  338. str[setcnt-2] = str[setcnt-1]; /* Eliminate separator */
  339. setcnt--;
  340. for(n=0;n<setcnt;n++) { /* check all vals are ascii */
  341. if(!isalpha(str[n])) {
  342. sprintf(errstr,"Reorder sequence contains non-alphabetic characters\n");
  343. return(DATA_ERROR);
  344. }
  345. }
  346. nextval = str[setcnt-1]; /* Store final value as value-to-step-to */
  347. if(islower(nextval)) { /* Standardising on uppercase */
  348. nextval += CAPITALISE;
  349. nextval -= ALPHABASE; /* and converting to numbers starting at zero */
  350. }
  351. dz->iparam[GR_REOCNT] = (int)(setcnt-1);/* Permute-set is all vals except last [which is value to step to] */
  352. for(n=0;n<dz->iparam[GR_REOCNT];n++) {
  353. if(islower(str[n])) /* In the Permute set */
  354. str[n] += CAPITALISE; /* Standardise on uppercase */
  355. str[n] -= ALPHABASE; /* Then convert to numbers starting at zero */
  356. if(str[n] < minval)
  357. minval = str[n]; /* Also finding max and min values */
  358. if(str[n] > maxval)
  359. maxval = str[n];
  360. } /* Check that nextval is an advance through sound */
  361. if((dz->iparam[GR_REOSTEP] = (int)(nextval - str[0])) <= 0) {
  362. sprintf(errstr,"Reorder sequence does not advance (last entry [%c] <= first [%c])\n",
  363. nextval+ALPHABASE,str[0]+ALPHABASE);
  364. return(DATA_ERROR);
  365. } /* Reolen is max no. of grains to store before permute possible */
  366. dz->iparam[GR_REOLEN] = (int)(maxval - minval) + 1;
  367. if((dz->iparray[GR_REOSET] = (int *)malloc(dz->iparam[GR_REOCNT] * sizeof(int)))==NULL) {
  368. sprintf(errstr,"INSUFFICIENT MEMORY to store reorder set.\n");
  369. return(MEMORY_ERROR);
  370. } /* Store the permutation set */
  371. for(n=0;n<dz->iparam[GR_REOCNT];n++) /* Offsetting it so that lowest numeric value = 0 */
  372. dz->iparray[GR_REOSET][n] = (int)(str[n] - minval);
  373. if((dz->extrabuf =
  374. (float **)realloc((char *)dz->extrabuf,(dz->iparam[GR_REOLEN]+SPLBUF_OFFSET) * sizeof(float *)))==NULL) {
  375. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate reorder set.\n");
  376. return(MEMORY_ERROR); /* Create storage location for each stored grain */
  377. } /* [in addition to splice stores] */
  378. if((dz->lparray[GR_ARRAYLEN] = (int *)malloc((dz->iparam[GR_REOLEN]) * sizeof(int)))==NULL) {
  379. sprintf(errstr,"INSUFFICIENT MEMORY to create grain stores.\n");
  380. return(MEMORY_ERROR); /* Create storage location for each stored grain arraylength */
  381. }
  382. if((dz->lparray[GR_THIS_LEN] = (int *)malloc((dz->iparam[GR_REOLEN]) * sizeof(int)))==NULL) {
  383. sprintf(errstr,"INSUFFICIENT MEMORY to store grain lengths.\n");
  384. return(MEMORY_ERROR); /* Create storage location for each stored grain length */
  385. }
  386. for(n=0;n<dz->iparam[GR_REOLEN];n++) {
  387. dz->lparray[GR_ARRAYLEN][n] = NOMINAL_LENGTH;
  388. if((dz->extrabuf[n+SPLBUF_OFFSET] = (float *)malloc(NOMINAL_LENGTH * sizeof(float)))==NULL) {
  389. sprintf(errstr,"INSUFFICIENT MEMORY to create extra grain buffers.\n");
  390. return(MEMORY_ERROR); /* Give each grainstore a nominal length, which will be */
  391. } /* realloced, as required, during permutation process */
  392. }
  393. return(FINISHED);
  394. }
  395. /********************************* GET_GRAIN_RATIOS ***************************/
  396. int get_grain_ratios(int special,char *filename,dataptr dz)
  397. {
  398. FILE *fp;
  399. char temp[200], *p;
  400. double fratio;
  401. int ratiocnt = 0;
  402. /*double max_transpos = GR_MAX_TRANSPOS * SEMITONES_PER_OCTAVE;*/
  403. if((fp = fopen(filename,"r"))==NULL) {
  404. sprintf(errstr,"Cannot open file %s to read data.\n",filename);
  405. return(DATA_ERROR);
  406. }
  407. while(fgets(temp,200,fp)!=NULL) {
  408. p = temp;
  409. if(*p == ';') // Allow comments in file
  410. continue;
  411. while(get_float_from_within_string(&p,&fratio)) {
  412. if(fratio < dz->application->min_special || fratio > dz->application->max_special) {
  413. sprintf(errstr,"Ratio (%lf) out of range (%lf - %lf)\n",
  414. fratio,dz->application->min_special,dz->application->max_special);
  415. return(DATA_ERROR);
  416. }
  417. if(special==GRAIN_PITCH_RATIOS)
  418. fratio = pow(2.0,fratio/SEMITONES_PER_OCTAVE);
  419. if(!ratiocnt)
  420. dz->parray[GR_RATIO] = (double *)malloc((ratiocnt+1)*sizeof(double));
  421. else
  422. dz->parray[GR_RATIO] =(double *)realloc((char *)dz->parray[GR_RATIO],(ratiocnt+1)*sizeof(double));
  423. dz->parray[GR_RATIO][ratiocnt] = fratio;
  424. ratiocnt++;
  425. }
  426. }
  427. if(ratiocnt==0) {
  428. sprintf(errstr,"No data in file '%s'.\n",filename);
  429. return(DATA_ERROR);
  430. }
  431. dz->iparam[GR_RATIOCNT] = ratiocnt;
  432. if(fclose(fp)<0) {
  433. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",filename);
  434. fflush(stdout);
  435. }
  436. return(FINISHED);
  437. }
  438. /********************************* GET_TWO_GRAIN_RATIOS ***************************/
  439. int get_two_grain_ratios(char *filename,dataptr dz)
  440. {
  441. FILE *fp;
  442. char temp[200], *p;
  443. double fratio;
  444. int ratiocnt = 0;
  445. int is_pitch = 1;
  446. /*double max_transpos = GR_MAX_TRANSPOS * SEMITONES_PER_OCTAVE;*/
  447. if((fp = fopen(filename,"r"))==NULL) {
  448. sprintf(errstr,"Cannot open file %s to read data.\n",filename);
  449. return(DATA_ERROR);
  450. }
  451. while(fgets(temp,200,fp)!=NULL) {
  452. p = temp;
  453. if(*p == ';') // Allow comments in file
  454. continue;
  455. while(get_float_from_within_string(&p,&fratio)) {
  456. if(is_pitch) {
  457. if(fratio < dz->application->min_special || fratio > dz->application->max_special) {
  458. sprintf(errstr,"Pitch-ratio (%lf) out of range (%lf - %lf)\n",
  459. fratio,dz->application->min_special,dz->application->max_special);
  460. return(DATA_ERROR);
  461. }
  462. fratio = pow(2.0,fratio/SEMITONES_PER_OCTAVE);
  463. } else {
  464. if(fratio < dz->application->min_special2 || fratio > dz->application->max_special2) {
  465. sprintf(errstr,"Time-ratio (%lf) out of range (%lf - %lf)\n", fratio,
  466. dz->application->min_special2,dz->application->max_special2);
  467. return(DATA_ERROR);
  468. }
  469. }
  470. is_pitch = !is_pitch;
  471. if(!ratiocnt)
  472. dz->parray[GR_RATIO] = (double *)malloc((ratiocnt+1)*sizeof(double));
  473. else
  474. dz->parray[GR_RATIO] =(double *)realloc((char *)dz->parray[GR_RATIO],(ratiocnt+1)*sizeof(double));
  475. dz->parray[GR_RATIO][ratiocnt] = fratio;
  476. ratiocnt++;
  477. }
  478. }
  479. if(ratiocnt==0) {
  480. sprintf(errstr,"No data in file '%s'.\n",filename);
  481. return(DATA_ERROR);
  482. }
  483. if(ODD(ratiocnt)) {
  484. sprintf(errstr,"Pitch and time ratios not paired correctly.\n");
  485. return(DATA_ERROR);
  486. }
  487. dz->iparam[GR_RATIOCNT] = ratiocnt;
  488. if(fclose(fp)<0) {
  489. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",filename);
  490. fflush(stdout);
  491. }
  492. return(FINISHED);
  493. }
  494. /********************************* GET_GRAIN_SYNCTIMES ***************************/
  495. int get_grain_synctimes(char *filename,dataptr dz)
  496. {
  497. FILE *fp;
  498. char temp[200], *p;
  499. double synctime;
  500. int synccnt = 0;
  501. double lasttime = -1.0;
  502. if((fp = fopen(filename,"r"))==NULL) {
  503. sprintf(errstr,"Cannot open file %s to read data.\n",filename);
  504. return(DATA_ERROR);
  505. }
  506. while(fgets(temp,200,fp)!=NULL) {
  507. p = temp;
  508. if(*p == ';') // Allow comments in file
  509. continue;
  510. while(get_float_from_within_string(&p,&synctime)) {
  511. if(synctime < 0.0) {
  512. sprintf(errstr,"Invalid sync time %lf (<0.0)\n",synctime);
  513. return(DATA_ERROR);
  514. }
  515. if(synctime < lasttime) {
  516. sprintf(errstr,"Sync times out of sequence (%lf %lf)\n",lasttime,synctime);
  517. return(DATA_ERROR);
  518. }
  519. lasttime = synctime;
  520. synccnt++;
  521. }
  522. }
  523. if(synccnt==0) {
  524. sprintf(errstr,"No data in file '%s'.\n",filename);
  525. return(DATA_ERROR);
  526. }
  527. if(fseek(fp,0,0)<0) {
  528. sprintf(errstr,"Seek to start failed on file '%s'.\n",filename);
  529. return(SYSTEM_ERROR);
  530. }
  531. if((dz->parray[GR_SYNCTIME] = (double *)malloc(synccnt*sizeof(double)))==NULL) {
  532. sprintf(errstr,"Insufficient memory to store grain sync times.\n");
  533. return(MEMORY_ERROR);
  534. }
  535. synccnt = 0;
  536. while(fgets(temp,200,fp)!=NULL) {
  537. p = temp;
  538. while(get_float_from_within_string(&p,&synctime)) {
  539. dz->parray[GR_SYNCTIME][synccnt] = synctime;
  540. synccnt++;
  541. }
  542. }
  543. if(dz->process == GREV)
  544. dz->itemcnt = (int)synccnt;
  545. else
  546. dz->iparam[GR_SYNCCNT] = (int)synccnt;
  547. if(fclose(fp)<0) {
  548. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",filename);
  549. fflush(stdout);
  550. }
  551. return(FINISHED);
  552. }
  553. /********************************************************************************************/
  554. /********************************** FORMERLY IN preprocess.c ********************************/
  555. /********************************************************************************************/
  556. /****************************** PARAM_PREPROCESS *********************************/
  557. int param_preprocess(dataptr dz)
  558. {
  559. int n, m;
  560. switch(dz->process) {
  561. case(GRAIN_COUNT): case(GRAIN_REPITCH): case(GRAIN_OMIT):
  562. case(GRAIN_TIMEWARP): case(GRAIN_GET): case(GRAIN_RERHYTHM):
  563. case(GRAIN_DUPLICATE): case(GRAIN_ALIGN): case(GRAIN_POSITION):
  564. case(GRAIN_REMOTIF): case(GRAIN_REORDER): case(GRAIN_REVERSE):
  565. case(GRAIN_ASSESS):
  566. return grain_preprocess(GR_GATE,dz);
  567. case(RRRR_EXTEND):
  568. if(dz->mode != 1) {
  569. if(dz->param[RRR_START] >= dz->param[RRR_END]) {
  570. sprintf(errstr,"ERROR: Start and End times for the material to be processed are incompatible.\n");
  571. return(DATA_ERROR);
  572. }
  573. }
  574. if(dz->mode < 2) {
  575. if(dz->brksize[RRR_SLOW]) { // Check data continuity, and find time of end of data, or last 1.0 (no change) value in file
  576. if(dz->brk[RRR_SLOW][1] != 1.0 && dz->vflag[0] == 0) {
  577. fprintf(stderr,"WARNING: First Slowing param not 1.0: will cause discontinuity where iteration starts.\n");
  578. fflush(stdout);
  579. }
  580. if(dz->brk[RRR_SLOW][(dz->brksize[RRR_SLOW] * 2) - 1] != 1.0 && dz->vflag[1] == 0) {
  581. fprintf(stdout,"WARNING: Last Slowing params not 1.0: will cause discontinuity where iteration ends.\n");
  582. fflush(stdout);
  583. }
  584. dz->maxtime = 0.0;
  585. for(n=0,m=1;n <dz->brksize[RRR_SLOW];n++,m+=2) {
  586. if(dz->brk[RRR_SLOW][m] < 1.0) {
  587. sprintf(errstr,"SLOWING PARAMETER CANNOT BE LESS THAN 1.0\n");
  588. return DATA_ERROR;
  589. }
  590. if(dz->brk[RRR_SLOW][m] == 1.0)
  591. dz->maxtime = dz->brk[RRR_SLOW][m-1];
  592. }
  593. if(dz->maxtime == 0.0)
  594. dz->maxtime = dz->brk[RRR_SLOW][(dz->brksize[RRR_SLOW] * 2) - 2];
  595. } else {
  596. if(dz->param[RRR_SLOW] != 1.0) {
  597. fprintf(stdout,"WARNING: SLOWING PARAMETER MUST BE IN A BRKFILE. DEFAULTING TO NO SLOWING.\n");
  598. fflush(stdout);
  599. }
  600. dz->param[RRR_SLOW] = 1.0;
  601. }
  602. if(dz->brksize[RRR_REGU]) {
  603. if(!dz->brksize[RRR_SLOW]) {
  604. sprintf(errstr,"REGULARITY PARAMETER CANNOT BE USED WITHOUT THE SLOWING PARAMETER.\n");
  605. return DATA_ERROR;
  606. }
  607. } else if(dz->param[RRR_REGU] != 0.0) {
  608. sprintf(errstr,"REGULARITY PARAMETER, IF NOT IN A BRKFILE, MUST BE SET TO 0.0\n");
  609. return DATA_ERROR;
  610. }
  611. }
  612. dz->param[RRR_RANGE] = pow(2.0,dz->param[RRR_RANGE]); /* convert transposition range from octaves to ratio */
  613. if(dz->param[RRR_RANGE] > 1.0)
  614. dz->param[RRR_RANGE] = 1.0/dz->param[RRR_RANGE];
  615. break;
  616. case(SSSS_EXTEND):
  617. dz->iparam[SSS_DUR] = (int)round(dz->param[SSS_DUR] * dz->infile->srate) * dz->infile->channels;
  618. dz->iparam[NOISE_MINFRQ] = (int)round((double)dz->infile->srate / (double)dz->param[NOISE_MINFRQ]);
  619. dz->iparam[MIN_NOISLEN] = (int)round(dz->param[MIN_NOISLEN] * MS_TO_SECS * dz->infile->srate) * dz->infile->channels;
  620. dz->iparam[MAX_NOISLEN] = (int)round(dz->param[MIN_NOISLEN] * dz->infile->srate) * dz->infile->channels;
  621. dz->tempsize = dz->iparam[SSS_DUR];
  622. if(!dz->vflag[0])
  623. dz->tempsize += dz->insams[0];
  624. break;
  625. case(GREV):
  626. break;
  627. default:
  628. sprintf(errstr,"PROGRAMMING PROBLEM: Unknown process in param_preprocess()\n");
  629. return(PROGRAM_ERROR);
  630. }
  631. return(FINISHED); /* NOTREACHED */
  632. }
  633. /********************************************************************************************/
  634. /********************************** FORMERLY IN procgrou.c **********************************/
  635. /********************************************************************************************/
  636. /**************************** GROUCHO_PROCESS_FILE ****************************/
  637. int groucho_process_file(dataptr dz) /* FUNCTIONS FOUND IN PROCESS.C */
  638. {
  639. int exit_status = FINISHED;
  640. if(dz->process!=GRAIN_COUNT && dz->process!=GRAIN_ASSESS)
  641. display_virtual_time(0L,dz);
  642. switch(dz->process) {
  643. case(GRAIN_COUNT): case(GRAIN_OMIT): case(GRAIN_DUPLICATE):
  644. case(GRAIN_REORDER): case(GRAIN_REPITCH): case(GRAIN_RERHYTHM):
  645. case(GRAIN_REMOTIF): case(GRAIN_TIMEWARP): case(GRAIN_GET):
  646. case(GRAIN_POSITION):case(GRAIN_REVERSE):
  647. if((exit_status = process_grains(dz))<0)
  648. return(exit_status);
  649. break;
  650. case(GRAIN_ALIGN):
  651. if((exit_status = process_grains(dz))<0) /* Process as GRAIN_GET,but with no text output */
  652. return(exit_status);
  653. if((exit_status = swap_to_otherfile_and_readjust_counters(dz))<0)
  654. return(exit_status);
  655. if((exit_status = zero_sound_buffers(dz))<0)
  656. return(exit_status);
  657. dz->process = GRAIN_POSITION;
  658. if((exit_status = grain_preprocess(GR_GATE2,dz))<0)
  659. return(exit_status);
  660. if((exit_status = process_grains(dz))<0)
  661. return(exit_status);
  662. break;
  663. case(RRRR_EXTEND):
  664. switch(dz->mode) {
  665. case(0):
  666. exit_status = timestretch_iterative(dz);
  667. break;
  668. case(1):
  669. exit_status = timestretch_iterative2(dz);
  670. break;
  671. case(2):
  672. exit_status = timestretch_iterative3(dz);
  673. break;
  674. }
  675. if(exit_status <0)
  676. return(exit_status);
  677. break;
  678. case(SSSS_EXTEND):
  679. if((exit_status = grab_noise_and_expand(dz))<0)
  680. return(exit_status);
  681. break;
  682. case(GRAIN_ASSESS):
  683. return assess_grains(dz);
  684. case(GREV):
  685. return grev(dz);
  686. default:
  687. sprintf(errstr,"Unknown case in process_file()\n");
  688. return(PROGRAM_ERROR);
  689. }
  690. return(FINISHED);
  691. }
  692. /********************************************************************************************/
  693. /********************************** FORMERLY IN pconsistency.c ******************************/
  694. /********************************************************************************************/
  695. /****************************** CHECK_PARAM_VALIDITY_AND_CONSISTENCY *********************************/
  696. int check_param_validity_and_consistency(dataptr dz)
  697. {
  698. int exit_status;
  699. int n;
  700. double brkmax, brkmin, ratio;
  701. handle_pitch_zeros(dz);
  702. switch(dz->process) {
  703. case(GRAIN_OMIT): return check_grain_consistency(dz);
  704. case(GRAIN_ASSESS):
  705. dz->param[GR_GATE] = GR_GATE_DEFAULT;
  706. dz->param[GR_MINTIME] = (GRAIN_SPLICELEN + GRAIN_SAFETY) * MS_TO_SECS * 2.0;
  707. dz->param[GR_WINSIZE] = 50.0;
  708. if((dz->vflag = (char *)malloc(sizeof(char))) == NULL) {
  709. sprintf(errstr,"Insufficient memory for internal flags.\n");
  710. return(MEMORY_ERROR);
  711. }
  712. dz->vflag[0] = 0;
  713. if((dz->brksize = (int *)malloc(4 * sizeof(int))) == NULL) {
  714. sprintf(errstr,"Insufficient memory for internal flags.\n");
  715. return(MEMORY_ERROR);
  716. }
  717. for(n=0;n<4;n++)
  718. dz->brksize[n] = 0;
  719. break;
  720. case(RRRR_EXTEND):
  721. if(dz->mode == 2) {
  722. dz->tempsize = dz->insams[0];
  723. break;
  724. }
  725. if(dz->mode == 1) {
  726. dz->param[RRR_WSIZENU] = LOW_RRR_SIZE/3.0; /* 15 ms/2 = 5ms */
  727. generate_samp_windowsizer(dz);
  728. ratio = dz->param[RRR_GRSIZ] * MS_TO_SECS * dz->param[RRR_GET] * (dz->param[RRR_STRETCH] - 1.0);
  729. } else {
  730. ratio = fabs(dz->param[RRR_END] - dz->param[RRR_START]) * (dz->param[RRR_STRETCH] - 1.0);
  731. }
  732. dz->tempsize = (int)round(dz->infile->srate * ratio);
  733. dz->tempsize *= dz->infile->channels;
  734. dz->tempsize += dz->insams[0];
  735. break;
  736. case(GREV):
  737. dz->iparam[GREV_SAMP_WSIZE] = (int)round(dz->param[GREV_WSIZE] * MS_TO_SECS * (double)dz->infile->srate);
  738. dz->param[GREV_TROFRAC] /= 2.0; /* include averaging factor here */
  739. switch(dz->mode) {
  740. case(GREV_DELETE):
  741. case(GREV_OMIT): /* convert how many to delete, to how many to keep NB GREV_DEL = GREV_KEEP */
  742. if(dz->brksize[GREV_DEL]) {
  743. if((exit_status = get_maxvalue_in_brktable(&brkmax,GREV_DEL,dz)) < 0)
  744. return(exit_status);
  745. } else
  746. brkmax = (double)dz->iparam[GREV_DEL];
  747. if(dz->iparam[GREV_OUTOF] <= (int)round(brkmax)) {
  748. sprintf(errstr,"CANNOT DELETE %d OUT OF %d.\n",(int)round(brkmax),dz->iparam[GREV_OUTOF]);
  749. return(DATA_ERROR);
  750. }
  751. if(dz->brksize[GREV_DEL]) {
  752. for(n = 1; n < dz->brksize[GREV_KEEP];n+=2)
  753. dz->brk[GREV_KEEP][n] = (double)dz->iparam[GREV_OUTOF] - dz->brk[GREV_DEL][n];
  754. } else
  755. dz->iparam[GREV_KEEP] = dz->iparam[GREV_OUTOF] - dz->iparam[GREV_DEL];
  756. break;
  757. }
  758. switch(dz->mode) {
  759. case(GREV_REVERSE):
  760. case(GREV_OMIT):
  761. case(GREV_PUT):
  762. dz->tempsize = dz->insams[0];
  763. break;
  764. case(GREV_REPEAT):
  765. if(dz->brksize[GREV_REPETS]) {
  766. if((exit_status = get_maxvalue_in_brktable(&brkmax,GREV_REPETS,dz)) < 0)
  767. return(exit_status);
  768. } else
  769. brkmax = (double)dz->iparam[GREV_REPETS];
  770. dz->tempsize = (int)round(dz->insams[0] * brkmax);
  771. break;
  772. case(GREV_DELETE):
  773. if(dz->brksize[GREV_DEL]) {
  774. if((exit_status = get_minvalue_in_brktable(&brkmin,GREV_DEL,dz)) < 0)
  775. return(exit_status);
  776. } else
  777. brkmin = (double)dz->iparam[GREV_DEL];
  778. ratio = (double)dz->iparam[GREV_OUTOF] - brkmin;
  779. ratio /= (double)dz->iparam[GREV_OUTOF];
  780. dz->tempsize = (int)round(dz->insams[0] * ratio);
  781. break;
  782. case(GREV_TSTRETCH):
  783. if(dz->brksize[GREV_TSTR]) { /* actually, the maximum number to DELETE */
  784. if((exit_status = get_maxvalue_in_brktable(&brkmax,GREV_TSTR,dz)) < 0)
  785. return(exit_status);
  786. } else
  787. brkmax = (double)dz->param[GREV_TSTR];
  788. dz->tempsize = (int)round(dz->insams[0] * brkmax);
  789. break;
  790. }
  791. break;
  792. }
  793. return(FINISHED);
  794. }
  795. /***************************** CHECK_GRAIN_CONSISTENCY ******************************/
  796. int check_grain_consistency(dataptr dz)
  797. {
  798. int exit_status;
  799. int maxval;
  800. double dmaxval;
  801. switch(dz->process) {
  802. case(GRAIN_OMIT): /* Check grains-to-KEEP vals against OUT-OF */
  803. if(dz->brksize[GR_KEEP]) {
  804. if((exit_status = get_maxvalue_in_brktable(&dmaxval,GR_KEEP,dz))<0)
  805. return(exit_status);
  806. maxval = round(dmaxval);
  807. } else
  808. maxval = dz->iparam[GR_KEEP];
  809. if(maxval > dz->iparam[GR_OUT_OF]) {
  810. sprintf(errstr,"A value of %d grains-to-keep out of each %d is impossible.\n",maxval,(int)dz->iparam[GR_OUT_OF]);
  811. return(DATA_ERROR);
  812. }
  813. break;
  814. default:
  815. sprintf(errstr,"Unknown case in check_grain_consistency()\n");
  816. return(PROGRAM_ERROR);
  817. }
  818. return(FINISHED);
  819. }
  820. /********************************************************************************************/
  821. /********************************** FORMERLY IN buffers.c ***********************************/
  822. /********************************************************************************************/
  823. /**************************** ALLOCATE_LARGE_BUFFERS ******************************/
  824. int allocate_large_buffers(dataptr dz)
  825. {
  826. switch(dz->process) {
  827. case(GRAIN_COUNT): case(GRAIN_REPITCH): case(GRAIN_OMIT):
  828. case(GRAIN_TIMEWARP): case(GRAIN_GET): case(GRAIN_RERHYTHM):
  829. case(GRAIN_DUPLICATE): case(GRAIN_ALIGN): case(GRAIN_POSITION):
  830. case(GRAIN_REMOTIF): case(GRAIN_REORDER): case(GRAIN_REVERSE):
  831. case(GRAIN_ASSESS):
  832. return create_grain_sndbufs(dz);
  833. case(RRRR_EXTEND):
  834. return create_rrr_sndbufs(dz);
  835. case(SSSS_EXTEND):
  836. return create_ssss_sndbufs(dz);
  837. case(GREV):
  838. return create_sndbufs(dz);
  839. default:
  840. sprintf(errstr,"Unknown program no. in allocate_large_buffers()\n");
  841. return(PROGRAM_ERROR);
  842. }
  843. return(FINISHED); /* NOTREACHED */
  844. }
  845. /*************************** CREATE_GRAIN_SNDBUFS **************************/
  846. int create_grain_sndbufs(dataptr dz)
  847. {
  848. int bigbufsize, zz;
  849. int n;
  850. //TW
  851. // int modulus = FSECSIZE;
  852. int modulus = FSECSIZE * dz->infile->channels;
  853. if(dz->sbufptr == 0 || dz->sampbuf==0) {
  854. sprintf(errstr,"buffer pointers not allocated: create_grain_sndbufs()\n");
  855. return(PROGRAM_ERROR);
  856. }
  857. bigbufsize = (int) (size_t ) Malloc(-1);
  858. dz->buflen = bigbufsize / sizeof(float);
  859. dz->buflen /= dz->bufcnt;
  860. if((zz = (int)round(dz->param[GR_BLEN] * dz->infile->srate)) > dz->buflen)
  861. dz->buflen = zz;
  862. dz->iparam[GR_WSIZE_SAMPS] = 0;
  863. if(dz->vflag[GR_ENVTRACK] && !flteq(dz->param[GR_WINSIZE],0.0)) {
  864. /* may be able to eliminate this ...?? */
  865. establish_grain_envelope_windowsize_in_samps(dz);
  866. modulus = dz->iparam[GR_WSIZE_SAMPS];
  867. }
  868. //TW most importantly, modulo-channels calc now moved AFTER "dz->buflen /= dz->bufcnt"
  869. // so divided-buffers still multiples of chans
  870. if((dz->buflen = (dz->buflen/modulus) * modulus)<=0) {
  871. dz->buflen = modulus;
  872. }
  873. if((dz->bigbuf = (float *)malloc((size_t)(dz->buflen * dz->bufcnt)* sizeof(float))) == NULL) {
  874. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  875. return(MEMORY_ERROR);
  876. }
  877. for(n=0;n<dz->bufcnt;n++)
  878. dz->sbufptr[n] = dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  879. dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  880. return(FINISHED);
  881. }
  882. /*************************** CREATE_SSSS_SNDBUFS **************************/
  883. int create_ssss_sndbufs(dataptr dz)
  884. {
  885. int bigsize, bigbufsize;
  886. if(dz->sbufptr == 0 || dz->sampbuf==0) {
  887. sprintf(errstr,"buffer pointers not allocated: create_grain_sndbufs()\n");
  888. return(PROGRAM_ERROR);
  889. }
  890. bigbufsize = (int)(size_t) Malloc(-1);
  891. dz->buflen = bigbufsize/sizeof(float);
  892. bigbufsize = dz->buflen * sizeof(float);
  893. bigsize = dz->insams[0] * sizeof(float);
  894. if(bigsize < 0) {
  895. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  896. return(MEMORY_ERROR);
  897. }
  898. if(bigbufsize + bigsize < 0) {
  899. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  900. return(MEMORY_ERROR);
  901. }
  902. if((dz->bigbuf = (float *)malloc((size_t)(bigbufsize + bigsize))) == NULL) {
  903. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  904. return(MEMORY_ERROR);
  905. }
  906. dz->sbufptr[0] = dz->sampbuf[0] = dz->bigbuf;
  907. dz->sbufptr[1] = dz->sampbuf[1] = dz->sampbuf[0] + dz->insams[0];
  908. dz->sampbuf[2] = dz->sampbuf[1] + dz->buflen;
  909. return(FINISHED);
  910. }
  911. /*************************** ESTABLISH_GRAIN_ENVELOPE_WINDOWSIZE_IN_SAMPS **************************/
  912. void establish_grain_envelope_windowsize_in_samps(dataptr dz)
  913. {
  914. int seccnt;
  915. dz->iparam[GR_WSIZE_SAMPS] =
  916. (int)round(dz->param[GR_WINSIZE] * (double)(dz->infile->srate * dz->infile->channels));
  917. //TW retain old buffering protocol, for now
  918. if((seccnt = dz->iparam[GR_WSIZE_SAMPS]/F_SECSIZE)*F_SECSIZE != dz->iparam[GR_WSIZE_SAMPS]) {
  919. seccnt++;
  920. dz->iparam[GR_WSIZE_SAMPS] = (int)(seccnt * F_SECSIZE);
  921. }
  922. }
  923. /********************************************************************************************/
  924. /********************************** FORMERLY IN cmdline.c ***********************************/
  925. /********************************************************************************************/
  926. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  927. {
  928. if (!strcmp(prog_identifier_from_cmdline,"count")) dz->process = GRAIN_COUNT;
  929. else if(!strcmp(prog_identifier_from_cmdline,"omit")) dz->process = GRAIN_OMIT;
  930. else if(!strcmp(prog_identifier_from_cmdline,"duplicate")) dz->process = GRAIN_DUPLICATE;
  931. else if(!strcmp(prog_identifier_from_cmdline,"reorder")) dz->process = GRAIN_REORDER;
  932. else if(!strcmp(prog_identifier_from_cmdline,"repitch")) dz->process = GRAIN_REPITCH;
  933. else if(!strcmp(prog_identifier_from_cmdline,"rerhythm")) dz->process = GRAIN_RERHYTHM;
  934. else if(!strcmp(prog_identifier_from_cmdline,"remotif")) dz->process = GRAIN_REMOTIF;
  935. else if(!strcmp(prog_identifier_from_cmdline,"timewarp")) dz->process = GRAIN_TIMEWARP;
  936. else if(!strcmp(prog_identifier_from_cmdline,"find")) dz->process = GRAIN_GET;
  937. else if(!strcmp(prog_identifier_from_cmdline,"reposition")) dz->process = GRAIN_POSITION;
  938. else if(!strcmp(prog_identifier_from_cmdline,"align")) dz->process = GRAIN_ALIGN;
  939. else if(!strcmp(prog_identifier_from_cmdline,"reverse")) dz->process = GRAIN_REVERSE;
  940. else if(!strcmp(prog_identifier_from_cmdline,"assess")) dz->process = GRAIN_ASSESS;
  941. else if(!strcmp(prog_identifier_from_cmdline,"r_extend")) dz->process = RRRR_EXTEND;
  942. else if(!strcmp(prog_identifier_from_cmdline,"noise_extend")) dz->process = SSSS_EXTEND;
  943. else if(!strcmp(prog_identifier_from_cmdline,"grev")) dz->process = GREV;
  944. else {
  945. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  946. return(USAGE_ONLY);
  947. }
  948. return FINISHED;
  949. }
  950. /********************************************************************************************/
  951. /********************************** FORMERLY IN usage.c *************************************/
  952. /********************************************************************************************/
  953. /******************************** USAGE1 ********************************/
  954. int usage1(void)
  955. {
  956. sprintf(errstr,
  957. "\nUSAGE: grain NAME (mode) infile (infile2) outfile parameters:\n"
  958. "\n"
  959. "where NAME can be any one of\n"
  960. "\n"
  961. " GRAIN OPERATIONS ON A SINGLE SOUND FILE\n\n"
  962. "count omit repitch timewarp\n"
  963. "find duplicate rerhythm reverse\n"
  964. "reposition reorder remotif assess\n"
  965. "r_extend grev noise_extend\n"
  966. "\n"
  967. " GRAIN OPERATIONS ON TWO SOUND FILES\n\n"
  968. "align\n"
  969. "\n"
  970. "Type 'grain count' for more info on count option.. ETC.\n");
  971. return(USAGE_ONLY);
  972. }
  973. /******************************** USAGE2 ********************************/
  974. int usage2(char *str)
  975. {
  976. double min_graintime = (GRAIN_SPLICELEN + GRAIN_SAFETY) * MS_TO_SECS * 2.0;
  977. if(!strcmp(str,"count")) {
  978. fprintf(stdout,
  979. "COUNT GRAINS FOUND IN A SOUND (AT GIVEN GATE & MINHOLE VALUES)\n\n"
  980. "USAGE:\n"
  981. "grain count infile [-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  982. "LEN maximum time between grains\n"
  983. "GATE required signal level for grain to be seen: Range 0-1: default 1\n"
  984. "MINHOLE min duration of inter-grain holes:\n"
  985. " Min value (default) %.3lf\n"
  986. "-t Gate level tracks signal level, found with windowsize winsize(msecs)\n"
  987. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  988. "-x ignore the last grain in the source.\n\n"
  989. "Gate may vary over time.\n",
  990. min_graintime);
  991. } else if(!strcmp(str,"omit")) {
  992. fprintf(stdout,
  993. "OMIT A PROPORTION OF GRAINS FROM GRAINY-SOUND\n\n"
  994. "USAGE:\n"
  995. "grain omit inf outf keep out-of [-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  996. "KEEP number of grains to keep from each set of 'out_of' grains\n"
  997. "OUT_OF 'keep' grains retained from start of each set of 'out_of' grains\n"
  998. "LEN maximum time between grains\n"
  999. "GATE required signal level for grain to be seen: Range 0-1: default 1\n"
  1000. "MINHOLE min duration of inter-grain holes:\n"
  1001. " Min value (default) %.3lf\n"
  1002. "-t Gate level tracks signal level, found with windowsize winsize(msecs)\n"
  1003. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1004. "-x ignore the last grain in the source.\n\n"
  1005. "Gate may vary over time.\n"
  1006. "Keep may vary over time, but must not exceed 'out_of'.\n",
  1007. min_graintime);
  1008. } else if(!strcmp(str,"duplicate")) {
  1009. fprintf(stdout,
  1010. "DUPLICATE GRAINS IN GRAINY-SOUND\n\n"
  1011. "USAGE:\n"
  1012. "grain duplicate infil outfil N [-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1013. "N number of repetitions of each grain\n"
  1014. "LEN maximum time between grains\n"
  1015. "GATE required signal level for grain to be seen: Range 0-1: default 1\n"
  1016. "MINHOLE min duration of inter-grain holes:\n"
  1017. " Min value (default) %.3lf\n"
  1018. "-t Gate level tracks signal level, found with windowsize winsize(msecs)\n"
  1019. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1020. "-x ignore the last grain in the source.\n\n"
  1021. "N and Gate may vary over time.\n",
  1022. min_graintime);
  1023. } else if(!strcmp(str,"reorder")) {
  1024. fprintf(stdout,
  1025. "REORDER GRAINS IN GRAINY-SOUND\n\n"
  1026. "USAGE:\n"
  1027. "grain reorder infil outfil code [-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1028. "CODE a string such as 'adb:c' indicating how grains are to be reordered.\n"
  1029. " The example means use grains 1 (a), 4 (d) and 2 (b) in sequence,\n"
  1030. " then begin this grain-jumping pattern again, BUT start at grain3 (c).\n"
  1031. " Continue to advance in this fashion until no grains left in infile.\n"
  1032. " The ':' is obligatory.\n"
  1033. "LEN maximum time between grains\n"
  1034. "GATE required signal level for grain to be seen: Range 0-1: default 1\n"
  1035. "MINHOLE min duration of inter-grain holes:\n"
  1036. " Min value (default) %.3lf\n"
  1037. "-t Gate level tracks signal level, found with windowsize winsize(msecs)\n"
  1038. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1039. "-x ignore the last grain in the source.\n\n"
  1040. "Gate may vary over time.\n",
  1041. min_graintime);
  1042. } else if(!strcmp(str,"repitch")) {
  1043. fprintf(stdout,
  1044. "REPITCH GRAINS IN GRAINY-SOUND\n\n"
  1045. "USAGE:\n"
  1046. "grain repitch mode infile outfile transpfile\n\t\t[-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1047. "MODES are:-\n"
  1048. "(1) Repitch each grain in turn, without repeating any grains:\n"
  1049. " on reaching end of transposition list, cycle back to its start.\n"
  1050. "(2) Play grain at each transposed pitch, before proceeding to next grain.\n\n"
  1051. "TRANSPFILE is a file listing transpositions as (+ve or -ve) semitone shifts.\n"
  1052. " Max transposition is %.0lf octaves up or down\n\n"
  1053. "LEN maximum time between grains\n"
  1054. "GATE required signal level for grain to be seen: Range 0-1:default 1\n"
  1055. "MINHOLE min duration of inter-grain holes:\n"
  1056. " Min value (default) %.3lf\n"
  1057. "-t Gate level tracks signal level,found with windowsize winsize(msecs)\n"
  1058. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1059. "-x ignore the last grain in the source.\n\n"
  1060. "Gate may vary over time.\n",
  1061. GR_MAX_TRANSPOS,min_graintime);
  1062. } else if(!strcmp(str,"rerhythm")) {
  1063. fprintf(stdout,
  1064. "CHANGE RHYTHM OF GRAINS IN GRAINY-SOUND\n\n"
  1065. "USAGE:\n"
  1066. "grain rerhythm mode infile outfile multfile\n\t\t[-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1067. "MODES are:-\n"
  1068. "(1) Lengthen or shorten each grain in turn, without repeating any grains:\n"
  1069. " on reaching end of time-multipliers list, cycle back to its start.\n"
  1070. "(2) Play grain at each specified retiming, before proceeding to next grain.\n\n"
  1071. "MULTFILE is a file listing duration-multipliers to change the duration\n"
  1072. " between one grain onset and the next grain onset.\n"
  1073. " Max multiplier is %.0lf : Min multiplier is %lf\n"
  1074. " if any inter-grain time reduced below MINGRAINTIME (%.3lf)\n"
  1075. " it will be set to MINGRAINTIME.\n\n"
  1076. "LEN maximum time between grains\n"
  1077. "GATE required signal level for grain to be seen: Range 0-1: default 1\n"
  1078. "MINHOLE min duration of inter-grain holes:\n"
  1079. " Min value (default) %.3lf\n"
  1080. "-t Gate level tracks signal level,found with windowsize winsize(msecs)\n"
  1081. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1082. "-x ignore the last grain in the source.\n\n"
  1083. "Gate may vary over time.\n",
  1084. GR_MAX_TSTRETCH,GR_MIN_TSTRETCH,min_graintime,min_graintime);
  1085. } else if(!strcmp(str,"remotif")) {
  1086. fprintf(stdout,
  1087. "CHANGE PITCH AND RHYTHM OF GRAINS IN GRAINY-SOUND\n\n"
  1088. "USAGE:\n"
  1089. "grain remotif mode infile outfile transpmultfile\n\t\t[-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1090. "MODES are:-\n"
  1091. "(1) Transform each grain in turn, without repeating any grains:\n"
  1092. " on reaching end of transpmultfile-data, cycle back to its start.\n"
  1093. "(2) Transform grain in each specified way, before proceeding to next grain.\n\n"
  1094. "TRANSPMULTFILE is a file containing transposition\\time-multiplier PAIRS.\n"
  1095. " Transpositions are (+ve or -ve) semitone shifts.\n"
  1096. " Max transposition is %.0lf octaves up or down.\n"
  1097. " Time-multipliers change the duration\n"
  1098. " between one grain onset and the next grain onset.\n"
  1099. " Max multiplier is %.0lf : Min multiplier is %lf\n"
  1100. " if any inter-grain time reduced below MINGRAINTIME (%.3lf)\n"
  1101. " it will be set to MINGRAINTIME.\n\n"
  1102. "LEN maximum time between grains\n"
  1103. "GATE required signal level for grain to be seen: Range 0-1: default 1\n"
  1104. "MINHOLE min duration of inter-grain holes:\n"
  1105. " Min value (default) %.3lf\n"
  1106. "-t Gate level tracks signal level,found with windowsize winsize(msecs)\n"
  1107. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1108. "-x ignore the last grain in the source.\n\n"
  1109. "Gate may vary over time.\n",
  1110. GR_MAX_TRANSPOS,GR_MAX_TSTRETCH,GR_MIN_TSTRETCH,min_graintime,min_graintime);
  1111. } else if(!strcmp(str,"timewarp")) {
  1112. fprintf(stdout,
  1113. "STRETCH (OR SHRINK) DURATION OF GRAINY-SOUND\n"
  1114. " WITHOUT STRETCHING GRAINS THEMSELVES\n\n"
  1115. "USAGE:\n"
  1116. "grain timewarp infile outfile timestretch-ratio\n\t\t[-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1117. "TIMESTRETCH-RATIO is degree of stretching/shrinking of intergrain time.\n"
  1118. " a value of 2 doubles the intergrain time.\n"
  1119. " a value of .5 halves the intergrain time.\n"
  1120. " Max stretch is %.0lf : Min shrink is %lf\n"
  1121. " if any inter-grain time reduced below MINGRAINTIME (%.3lf)\n"
  1122. " it will be set to MINGRAINTIME.\n\n"
  1123. "LEN maximum time between grains\n"
  1124. "GATE required signal level for grain to be seen: Range 0-1: default 1\n"
  1125. "MINHOLE min duration of inter-grain holes:\n"
  1126. " Min value (default) %.3lf\n"
  1127. "-t Gate level tracks signal level,found with windowsize winsize(msecs)\n"
  1128. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1129. "-x ignore the last grain in the source.\n\n"
  1130. "Timestretch-ratio and Gate may vary over time.\n"
  1131. "Times in brkpnt files refer to INFILE time.\n",
  1132. GR_MAX_TSTRETCH,GR_MIN_TSTRETCH,min_graintime,min_graintime);
  1133. } else if(!strcmp(str,"reverse")) {
  1134. fprintf(stdout,
  1135. "REVERSE ORDER OF GRAINS IN A GRAINY-SOUND\n"
  1136. " WITHOUT REVERSING GRAINS THEMSELVES\n\n"
  1137. "USAGE:\n"
  1138. "grain reverse infil outfil [-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1139. "LEN maximum time between grains\n"
  1140. "GATE required signal level for grain to be seen: Range 0-1: default 1\n"
  1141. "MINHOLE min duration of inter-grain holes:\n"
  1142. " Min value (default) %.3lf\n"
  1143. "-t Gate level tracks signal level,found with windowsize winsize(msecs)\n"
  1144. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1145. "-x ignore the last grain in the source.\n\n"
  1146. "Gate may vary over time.\n",min_graintime);
  1147. } else if(!strcmp(str,"find")) {
  1148. fprintf(stdout,
  1149. "LOCATE TIMINGS OF GRAIN-ONSETS IN A GRAINY-SOUND\n\n"
  1150. "USAGE:\n"
  1151. "grain find infil out-textfil [-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1152. "OUT-TEXTFILE will contain a list of grain-onset timings, in seconds.\n"
  1153. "LEN maximum time between grains\n"
  1154. "GATE min signal level for grain to be seen: Range 0-1: default 1\n"
  1155. "MINHOLE min duration of inter-grain holes:\n"
  1156. " Min value (default) %.3lf\n"
  1157. "-t Gate tracks signal level,found with windowsize winsize(msecs)\n"
  1158. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1159. "-x ignore the last grain in the source.\n\n"
  1160. "Gate may vary over time.\n",
  1161. min_graintime);
  1162. } else if(!strcmp(str,"reposition")) {
  1163. fprintf(stdout,
  1164. "REPOSITION GRAIN-ONSETS IN A GRAINY-SOUND\n\n"
  1165. "USAGE:\n"
  1166. "grain reposition infile outfile timefile offset\n\t\t[-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1167. "TIMEFILE must contain a list of grain-onset timings, in seconds.\n"
  1168. " if any inter-grain time reduced below MINGRAINTIME (%.3lf)\n"
  1169. " it will be set to MINGRAINTIME.\n"
  1170. "OFFSET add this value (secs) to ALL grain timings.\n"
  1171. "LEN maximum time between grains\n"
  1172. "GATE min signal level for grain to be seen: Range 0-1: default 1\n"
  1173. "MINHOLE min duration of inter-grain holes:\n"
  1174. " Min value (default) %.3lf\n"
  1175. "-t Gate level tracks signal level,found with windowsize winsize(msecs)\n"
  1176. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1177. "-x ignore the last grain in the source.\n\n"
  1178. "Gate may vary over time.\n",
  1179. min_graintime,min_graintime);
  1180. } else if(!strcmp(str,"align")) {
  1181. fprintf(stdout,
  1182. "SYNCHRONISE GRAIN-ONSETS IN 2nd GRAINY-SOUND\n"
  1183. " WITH THOSE IN THE 1st.\n"
  1184. "USAGE:\n"
  1185. "grain align infile1 infile2 outfile offset gate2\n\t\t[-blen] [-lgate] [-hminhole] [-twinsize] [-x]\n\n"
  1186. "INFILE1 provides grain-onset times.\n"
  1187. "INFILE2 provides the actual grains to be retimed.\n"
  1188. "OFFSET add this value (secs) to ALL grain timings.\n"
  1189. "GATE2 min signal level to register grain in File2: Range 0-1: default 1\n"
  1190. "LEN maximum time between grains\n"
  1191. "GATE min signal level to register grain in File1: Range 0-1: default 1\n"
  1192. "MINHOLE min duration of inter-grain holes:\n"
  1193. " Min value (default) %.3lf\n"
  1194. "-t Gate level tracks signal level,found with windowsize winsize(msecs)\n"
  1195. " Range(0.0 - infiledur): 0.0 turns off tracking)\n"
  1196. "-x ignore the last grain in the source.\n\n"
  1197. "Gate and Gate2 may vary over time.\n",
  1198. min_graintime);
  1199. } else if(!strcmp(str,"assess")) {
  1200. fprintf(stdout,
  1201. "ASSESS BEST GATE VALUE TO FIND MAXIMUM NUMBER OF GRAINS IN FILE.\n"
  1202. "USAGE: grain assess infile\n\n");
  1203. } else if(!strcmp(str,"r_extend")) {
  1204. fprintf(stdout,
  1205. "TIME-STRETCH' NATURAL ITERATIVE SOUNDS LIKE THE ROLLED 'rrr' IN SPEECH.\n"
  1206. "IN MODE 1 you mark where iterative part of sound is.\n"
  1207. "USAGE: \n"
  1208. "grain r_extend 1 inf outf stt end te pr rep get asc psc rit reg [-s] [-e]\n"
  1209. " STT Time of start of iterated material within source.\n"
  1210. " END Time of end of iterated material within source.\n"
  1211. "\n"
  1212. "IN MODE 2 process attempts to find it, using envelope tracing.\n"
  1213. "USAGE: \n"
  1214. "grain r_extend 2 inf outf gate usz te pr rep get asc psc skp rit reg [-s] [-e]\n"
  1215. " GATE min level in src for env tracing to kick in (0-1).\n"
  1216. " USZ size of unit searched for, in ms (15 for rolled rr).\n"
  1217. " SKP number of found units to skip before processing.\n"
  1218. "\n"
  1219. "TE How much to time-extend the marked (or found) material.\n"
  1220. "PR Guesstimate pitch-range of iteration in src - in octaves (try 1).\n"
  1221. "REP Iterated material extended by reusing individual segments\n"
  1222. " 'repets' is no. of adjacent copies of any seg you allow (try 1-2)\n"
  1223. "GET Guesstimate no. of iterations expected: listen to snd (range 2-1000).\n"
  1224. " In MODE 2, MINIMUM no of repeats you expect to find.\n"
  1225. "ASC Ouput segs made to vary randomly in amplitude. (Range 0-1)\n"
  1226. " Value multiplies orig seg amp by rand val in range 1 to (1-N).\n"
  1227. "PSC Output segs made to vary randomly in pitch (Range 0-24)\n"
  1228. " Val transposes pitch by randval in range -N to +N semitones.\n"
  1229. "RIT Slowing factor (grain-turnover rate slowed by inserting silence).\n"
  1230. " 1.0 = no slowing. Otherwise vals MUST BE IN A BRKPOINT FILE.\n"
  1231. " Brkpoint times refer to time RELATIVE TO START OF ITERATED SEG.\n"
  1232. " When used, number of interations will be extended (or curtailed)\n"
  1233. " to allow the rit(s) to play out.\n"
  1234. "REG Force grain rate to regular. (Only where grains slowed by \"rit\").\n"
  1235. " 1 = completely regular. But only happens once intest grain\n"
  1236. " shorter than average separation caused by \"rit\".\n"
  1237. "-s Don't keep start of sound before the iterative-extension.\n"
  1238. "-e Don't keep end of sound after the iterative-extension.\n"
  1239. "IN MODE 3 Sound is edited into its start, each iterate unit, and its end.\n"
  1240. "using params STT END and PR\n");
  1241. } else if(!strcmp(str,"noise_extend")) {
  1242. fprintf(stdout,
  1243. "FIND AND TIME-STRETCH' NOISE COMPONENT IN SOUND.\n"
  1244. "USAGE: \n"
  1245. "grain noise_extend inf outf duration minfrq mindur maxdur [-x]\n"
  1246. "DURATION Duration of noise (part of) output file.\n"
  1247. "MINFRQ lowest 'frequency' (Hz) acceptable as noise (try %.1f)\n"
  1248. "MINDUR Minimum duration of signal (in ms) acceptable as noise source.\n"
  1249. "MAXDUR Maximum duration of signal (in secs) acceptable as noise source.\n"
  1250. "-x Keep only the extended noise: default keep rest of input src too.\n",NOIS_MIN_FRQ);
  1251. } else if(!strcmp(str,"grev")) {
  1252. fprintf(stdout,
  1253. "FIND AND MANIPULATE 'GRAINS', USING ENVELOPE TROUGHS AND ZERO-CROSSINGS.\n"
  1254. "This process locates elements of sound by searching for troughs in envelope.\n"
  1255. "It doesn't need a clear attack to recognise 'grain' in sound, and is\n"
  1256. "more appropriate for e.g. separating syllables in speech.\n\n"
  1257. "(REVERSE) USAGE: grain grev 1 inf outf wsiz trof gpcnt\n"
  1258. "(REPEAT) USAGE: grain grev 2 inf outf wsiz trof gpcnt repets\n"
  1259. "(DELETE) USAGE: grain grev 3 inf outf wsiz trof gpcnt keep outof\n"
  1260. "(OMIT) USAGE: grain grev 4 inf outf wsiz trof gpcnt keep outof\n"
  1261. "(TIMESTRETCH) USAGE: grain grev 5 inf outf wsiz trof gpcnt tstretch\n"
  1262. "(GET) USAGE: grain grev 6 inf out_timesfile wsiz trof gpcnt\n"
  1263. "(PUT) USAGE: grain grev 7 inf outf in_timesfile wsiz trof gpcnt\n"
  1264. "WSIZ sizeof window in ms, determines size of grains to find.\n"
  1265. "TROF acceptable trough height, relative to adjacent peaks (range >0 - <1)\n"
  1266. "GPCNT number of grains to treat as a unit in the operations.\n"
  1267. "REPETS number of repetitions of each unit.\n"
  1268. "KEEP | OUTOF number of units to keep, e.g. 3 out of 5.\n"
  1269. "TSTRETCH amount to timestretch output (grains NOT streched) (Range .01 - 100).\n"
  1270. "GET gets grain-pos to (text) timesfile: PUT puts grains at times in timesfile\n"
  1271. "DELETE removes specified units: OMIT replaces them with silence,\n"
  1272. "GPCNT, REPETS, KEEP and TSTRETCH can vary over time,\n");
  1273. } else
  1274. fprintf(stdout,"Unknown option '%s'\n",str);
  1275. return(USAGE_ONLY);
  1276. }
  1277. /******************************** USAGE3 ********************************/
  1278. int usage3(char *str1,char *str2)
  1279. {
  1280. if(!strcmp(str1,"count") || !strcmp(str1,"assess")) /*RWD added assess */
  1281. return(CONTINUE);
  1282. else
  1283. sprintf(errstr,"Insufficient parameters on command line.\n");
  1284. return(USAGE_ONLY);
  1285. }
  1286. /******************************** INNER_LOOP (redundant) ********************************/
  1287. int inner_loop
  1288. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  1289. {
  1290. return(FINISHED);
  1291. }
  1292. /*************************** CREATE_RRR_SNDBUFS **************************/
  1293. int create_rrr_sndbufs(dataptr dz)
  1294. {
  1295. int n;
  1296. int bigbufsize, secfactor, seccnt;
  1297. if(dz->sbufptr == 0 || dz->sampbuf==0) {
  1298. sprintf(errstr,"buffer pointers not allocated: create_grain_sndbufs()\n");
  1299. return(PROGRAM_ERROR);
  1300. }
  1301. bigbufsize = (dz->insams[0] + 2) * 2; /* in and out bufs, plus wrap-around point */
  1302. if(dz->mode == 1) {
  1303. secfactor = max((int)(dz->iparam[RRR_SAMP_WSIZENU]),F_SECSIZE) * 2;
  1304. if((seccnt = bigbufsize/secfactor) * secfactor != bigbufsize)
  1305. seccnt++;
  1306. bigbufsize = seccnt * secfactor;
  1307. }
  1308. if(bigbufsize < 0) {
  1309. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  1310. return(MEMORY_ERROR);
  1311. }
  1312. if((dz->bigbuf = (float *) malloc((size_t)(bigbufsize * sizeof(float)))) == NULL) {
  1313. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  1314. return(MEMORY_ERROR);
  1315. }
  1316. bigbufsize /= dz->bufcnt;
  1317. dz->buflen = bigbufsize;
  1318. for(n=0;n<dz->bufcnt;n++)
  1319. dz->sbufptr[n] = dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  1320. dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  1321. /* These are larger than the theoretically possible maximum, i.e. a peak every 2 samples !! */
  1322. if((dz->parray[0] = (double *)malloc(((dz->insams[0]/2) + 2) * sizeof(double)))==NULL) {
  1323. sprintf(errstr,"INSUFFICIENT MEMORY to create peak store.\n");
  1324. return(MEMORY_ERROR);
  1325. }
  1326. if((dz->lparray[0] = (int *)malloc(((dz->insams[0]/2) + 2) * sizeof(int)))==NULL) {
  1327. sprintf(errstr,"INSUFFICIENT MEMORY to create peak store.\n");
  1328. return(MEMORY_ERROR);
  1329. }
  1330. return(FINISHED);
  1331. }
  1332. /**************************** GENERATE_SAMP_WINDOWSIZER *************************
  1333. *
  1334. * unadjusted_envwindow_sampsize too small should NOW be trapped by range settings!!
  1335. */
  1336. int generate_samp_windowsizer(dataptr dz)
  1337. {
  1338. int unadjusted_envwindow_sampsize, j, k;
  1339. int srate = dz->infile->srate;
  1340. dz->iparam[RRR_SAMP_WSIZENU] = (int)(SECSIZE/sizeof(float));
  1341. unadjusted_envwindow_sampsize = round(dz->param[RRR_WSIZENU] * MS_TO_SECS * (double)srate);
  1342. /* FOLLOWING POSSIBLY UNNECESSARY WITH NEW WINSIZE MINIMUM */
  1343. if(unadjusted_envwindow_sampsize < dz->iparam[RRR_SAMP_WSIZENU]) {
  1344. k = dz->iparam[RRR_SAMP_WSIZENU];
  1345. while(unadjusted_envwindow_sampsize<k)
  1346. k /= 2;
  1347. j = k * 2;
  1348. if(j - unadjusted_envwindow_sampsize > k - unadjusted_envwindow_sampsize)
  1349. dz->iparam[RRR_SAMP_WSIZENU] = (int)k;
  1350. else
  1351. dz->iparam[RRR_SAMP_WSIZENU] = (int)j;
  1352. }
  1353. if(unadjusted_envwindow_sampsize > dz->iparam[RRR_SAMP_WSIZENU]) {
  1354. k = round((double)unadjusted_envwindow_sampsize/(double)dz->iparam[RRR_SAMP_WSIZENU]);
  1355. dz->iparam[RRR_SAMP_WSIZENU] = (int)(dz->iparam[RRR_SAMP_WSIZENU] * k);
  1356. }
  1357. return(FINISHED);
  1358. }