cdparse_other.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  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. /* floats version */
  22. /* RECEIVES FROM TK
  23. *
  24. * Name of input file.
  25. *
  26. * SENDS TO TK
  27. *
  28. * (1) Various file constants, which TK needs to STORE, to pass back at subsequent stage.
  29. */
  30. /**** FEB 1999 MODS
  31. *
  32. * Force cdparams() output to be printed to stdout on a single line, with items separated by
  33. * single spaces, forming a valid TK list
  34. *
  35. * Recognise Float-sndfiles.
  36. */
  37. #include <stdio.h>
  38. #include <osbind.h>
  39. #include <math.h>
  40. #include <float.h>
  41. #include <cdplib.h>
  42. #include <structures.h>
  43. #include <cdpstate.h>
  44. #include <tkglobals.h>
  45. #include <filetype.h>
  46. #include <globcon.h>
  47. #include <headread.h>
  48. #include <speccon.h>
  49. #include <processno.h>
  50. #include <stdlib.h>
  51. #include <sfsys.h>
  52. #include <ctype.h>
  53. #include <limits.h>
  54. #include <string.h>
  55. /* NEW CODE June 1st 2000 */
  56. #ifndef CDP97
  57. #define CDP97 //RWD.6.98 need this!
  58. #endif
  59. #ifdef _DEBUG
  60. #include <assert.h>
  61. #endif
  62. void maxsamp(int ifd,double *maxamp,double *maxloc,int *maxrep);
  63. /* END OF NEW CODE */
  64. #define POSSIBLE (TRUE)
  65. char errstr[400];
  66. static void initialise_fileptr(infileptr inputfile);
  67. static int check_for_data_in_file(char *filename,infileptr inputfile);
  68. static int set_wlength_and_check_data(char *filename,int ifd,infileptr inputfile);
  69. static int check_pitch_or_transpos_binary_data(infileptr inputfile,char *filename,int ifd);
  70. static int megaparsec(char *filename,FILE *fp,infileptr inputfile);
  71. static int initial_parse_textfile(char *filename,FILE *fp,int *linelist,
  72. int *has_comments,int *dB_notation,int *total_wordcnt,
  73. int *numcnt,int *linecnt,int *max_line_word_cnt,int *min_line_word_cnt);
  74. static void initial_textfile_check(int dB_notation,int *sndlist,int *mixlist,int *brklist,int *numlist,
  75. int numcnt,int total_wordcnt,int max_line_word_cnt,int in_line_word_cnt);
  76. static int valid_start_of_mixfile_line(int k,int *chans,int lineno,char **wordstor,int *srate,
  77. int *mixlist,infileptr inputfile,char *filename,double *dur);
  78. #ifdef IS_CDPARSE
  79. static int is_valid_end_of_mixfile_line(int j,int chans,int lineno,char **wordstor,int *wordcnt);
  80. #else
  81. static int is_valid_end_of_mixfile_line(int j,int chans,int lineno,char **wordstor,int *wordcnt,char *filename);
  82. #endif
  83. static int confirm_true_stereo(int *chans,int *is_left,int *is_right,int *is_central,char *panword);
  84. static int is_a_possible_mixfile(char **wordstor,int *wordcnt,int linecnt,
  85. int *mixlist,int *srate,infileptr inputfile, char *filename,double *dur);
  86. static int OK_level(char *str);
  87. static int is_a_dB_value(char *str);
  88. static int get_leveldb_frame(char *str,double *val);
  89. static int is_a_valid_sndfile_for_mixfile(char *filename,char *sndfilename,int *srate,
  90. int chans,int linecnt,infileptr inputfile,double time,double *dur);
  91. static int check_for_brklist_and_count_numbers(char **wordstor,int *wordcnt,
  92. int linecnt,int *brklist,infileptr inputfile,char *filename);
  93. static int check_for_a_linelist(int line_word_cnt,int *linelist,char **wordstor,infileptr inputfile);
  94. static int store_wordlist_without_comments
  95. (FILE *fp,char *temp,int total_wordcnt,char **wordstor,int *wordcnt,infileptr inputfile);
  96. /* NEW CODE */
  97. static int assign_type(int sndlist,int numlist,int brklist,int mixlist,int synclist,int linelist,
  98. int extended_brkfile,infileptr inputfile);
  99. /* NEW CODE */
  100. static int check_for_sndlist(char **wordstor,int *wordcnt,int linecnt,int *sndlist);
  101. static int check_for_a_synclist(int *synclist,char **wordstor,int *wordcnt,infileptr inputfile,char *filename);
  102. static int is_a_valid_sndfile_for_sync(char *filename,char *sndfilename,int *srate,int linecnt,infileptr inputfile);
  103. static int set_sndlist_flags(infileptr inputfile,char **wordstor);
  104. static int assign_brkfile_type(infileptr inputfile,int linelist);
  105. #ifdef NO_DUPLICATE_SNDFILES
  106. #ifdef IS_CDPARSE
  107. static int duplicated_filenames(int linecnt,char **wordstor,int *wordcnt);
  108. #else
  109. static int duplicated_filenames(int linecnt,char **wordstor,int *wordcnt,char *filename);
  110. #endif
  111. #endif
  112. static int check_for_an_extended_brkfile
  113. (int *extended_brkfile,char **wordstor,int *wordcnt,int linecnt,int total_words,infileptr inputfile);
  114. static int extract_value_pair
  115. (int *n,double *time, double *val,double *maxval,double *minval,double *lasttime, char **wordstor);
  116. static void utol(char *str);
  117. extern int smpflteq(double a,double b);
  118. int do_exit(int exit_status);
  119. /* RWD TODO: use sndsystem */
  120. #ifdef IS_CDPARSE
  121. static int nuheadread(infileptr inputfile,int ifd,char *filename,double *maxamp,double *maxloc, int *maxrep,int getmax);
  122. #endif
  123. static int parse_a_textfile(char *filename,infileptr inputfile);
  124. static int copy_to_infileptr(infileptr i, fileptr f);
  125. /***************************** MAIN **************************/
  126. #ifdef IS_CDPARSE
  127. extern int sloom = 1;
  128. extern int sloombatch = 0;
  129. int main(int argc,char *argv[])
  130. {
  131. char *filename;
  132. infileptr inputfile;
  133. char temp[2000], *p; /*** Added FEB 1999 *****/
  134. int getmaxinfo = 1;
  135. #else
  136. int cdparse(char *filename,infileptr inputfile)
  137. {
  138. int getmaxinfo = 0;
  139. #endif
  140. int exit_status;
  141. int ifd;
  142. double maxamp = 0.0, maxloc = -1.0;
  143. int maxrep = 0;
  144. int getmax = 0;
  145. #ifdef IS_CDPARSE
  146. /* NEW CODE : June 1st 2000 */
  147. if(argc!=3) {
  148. sprintf(errstr,"Incorrect call to cdparse()\n");
  149. exit_status = FAILED;
  150. return do_exit(exit_status);
  151. }
  152. if(sscanf(argv[2],"%d",&getmax)!=1) {
  153. sprintf(errstr,"Incorrect call to cdparse()\n");
  154. exit_status = FAILED;
  155. return do_exit(exit_status);
  156. }
  157. filename = argv[1];
  158. if(file_has_invalid_startchar(filename)) {
  159. sprintf(errstr, "Filename %s has invalid starting character(s)\n",filename);
  160. exit_status = FAILED;
  161. return do_exit(exit_status);
  162. }
  163. if(file_has_invalid_extension(filename)) {
  164. sprintf(errstr, "Filename %s has invalid extension for CDP work\n",filename);
  165. exit_status = FAILED;
  166. return do_exit(exit_status);
  167. }
  168. if((inputfile = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  169. sprintf(errstr,"Insufficient memory for parsing file %s.\n",filename);
  170. exit_status = MEMORY_ERROR;
  171. return do_exit(exit_status);
  172. }
  173. #endif
  174. initialise_fileptr(inputfile);
  175. if((ifd = sndopenEx(filename,0,CDP_OPEN_RDONLY)) >= 0) {
  176. if((exit_status = readhead(inputfile,ifd,filename,&maxamp,&maxloc,&maxrep,getmax,getmaxinfo))<0) {
  177. if(sndcloseEx(ifd)<0) {
  178. sprintf(errstr, "Failed to close file %s\n",filename);
  179. exit_status = SYSTEM_ERROR;
  180. return do_exit(exit_status);
  181. }
  182. return do_exit(exit_status);
  183. }
  184. /* TEST */
  185. fprintf(stdout,"INFO: 3333 inputfile->filetype = %d\n",inputfile->filetype);
  186. fflush(stdout);
  187. /* TEST */
  188. if(getmaxinfo && getmax && (maxrep < 0)) { /* if maxsamp required & not in header, get it by searching file */
  189. //TW only get maxsamp if it's a soundfile
  190. if(inputfile->filetype == SNDFILE) {
  191. maxsamp(ifd,&maxamp,&maxloc,&maxrep); /*RWD TODO */
  192. sndseekEx(ifd,0,0); /* RWD TODO */
  193. }
  194. }
  195. if((inputfile->insams = sndsizeEx(ifd))<0) {
  196. sprintf(errstr, "Can't read size of input file %s\n",filename);
  197. exit_status = DATA_ERROR;
  198. if(sndcloseEx(ifd)<0) {
  199. sprintf(errstr, "Failed to close file %s: %s\n",filename,sferrstr());
  200. exit_status = SYSTEM_ERROR;
  201. }
  202. return do_exit(exit_status);
  203. }
  204. if((exit_status = check_for_data_in_file(filename,inputfile))<0) {
  205. if(sndcloseEx(ifd)<0) {
  206. sprintf(errstr, "Failed to close file %s\n",filename);
  207. exit_status = SYSTEM_ERROR;
  208. }
  209. return do_exit(exit_status);
  210. }
  211. switch(inputfile->filetype) {
  212. case(ANALFILE):
  213. case(PITCHFILE):
  214. case(TRANSPOSFILE):
  215. case(FORMANTFILE):
  216. inputfile->nyquist = inputfile->origrate/2.0;
  217. inputfile->frametime = (float)(1.0/inputfile->arate);
  218. /*inputfile->insams = inputfile->infilesize/sizeof(float); */
  219. if((exit_status = set_wlength_and_check_data(filename,ifd,inputfile))<0) {
  220. if(sndcloseEx(ifd)<0) {
  221. sprintf(errstr, "Failed to close file %s\n",filename);
  222. exit_status = SYSTEM_ERROR;
  223. }
  224. return do_exit(exit_status);
  225. }
  226. inputfile->duration = inputfile->wlength * inputfile->frametime;
  227. if(inputfile->duration <= FLTERR) {
  228. sprintf(errstr, "file %s has ZERO duration : ignoring it\n",filename);
  229. exit_status = DATA_ERROR;
  230. return do_exit(exit_status);
  231. }
  232. break;
  233. case(ENVFILE):
  234. if(sndgetprop(ifd,"window size",(char *)&inputfile->window_size,sizeof(float)) < 0) {
  235. sprintf(errstr, "Cannot read window size for file %s\n",filename);
  236. exit_status = DATA_ERROR;
  237. return do_exit(exit_status);
  238. }
  239. inputfile->duration = inputfile->window_size * inputfile->insams * MS_TO_SECS;
  240. if(inputfile->duration <= FLTERR) {
  241. sprintf(errstr, "file %s has zero duration : ignoring it\n",filename);
  242. exit_status = DATA_ERROR;
  243. return do_exit(exit_status);
  244. }
  245. break;
  246. case(SNDFILE):
  247. case(FLT_SNDFILE): /*RWD 2001 added - needed? */
  248. /*inputfile->insams = inputfile->infilesize/sizeof(short); */
  249. inputfile->nyquist = (double)inputfile->srate/2.0;
  250. inputfile->duration = (double)(inputfile->insams/inputfile->channels)/(double)(inputfile->srate);
  251. if(inputfile->duration <= FLTERR) {
  252. sprintf(errstr, "file %s has zero duration : ignoring it\n",filename);
  253. exit_status = DATA_ERROR;
  254. return do_exit(exit_status);
  255. }
  256. //RWD.7.99 need to reset this to FLT_SNDFILE if stype==FLOAT ?
  257. break;
  258. default:
  259. exit_status = DATA_ERROR;
  260. sprintf(errstr,"Unknown type of file: file %s\n",filename);
  261. if(sndcloseEx(ifd)<0) {
  262. sprintf(errstr, "Failed to close file %s\n",filename);
  263. exit_status = SYSTEM_ERROR;
  264. }
  265. return do_exit(exit_status);
  266. }
  267. if(sndcloseEx(ifd)<0) {
  268. sprintf(errstr, "Failed to close file %s\n",filename);
  269. exit_status = SYSTEM_ERROR;
  270. return do_exit(exit_status);
  271. }
  272. } else {
  273. if((exit_status = parse_a_textfile(filename,inputfile)) < 0)
  274. return(exit_status);
  275. }
  276. #ifdef IS_CDPARSE
  277. p = temp;
  278. sprintf(p,"%d ",inputfile->filetype); /* 0 */
  279. p = temp + strlen(temp);
  280. //TW CORRECTED, FOR COMPATIBILITY WITH tkinput: first value OUTPUT is NOW in samples (it was, infilesize)
  281. // sprintf(p,"%ld ", inputfile->infilesize /* 1 */ p = temp + strlen(temp);
  282. sprintf(p,"%ld ", inputfile->insams); /* 1 */ p = temp + strlen(temp);
  283. sprintf(p,"%ld ", inputfile->insams); /* 2 */ p = temp + strlen(temp);
  284. sprintf(p,"%ld ", inputfile->srate); /* 3 */ p = temp + strlen(temp);
  285. sprintf(p,"%ld ", inputfile->channels); /* 4 */ p = temp + strlen(temp);
  286. sprintf(p,"%ld ", inputfile->wanted); /* 5 */ p = temp + strlen(temp);
  287. sprintf(p,"%ld ", inputfile->wlength); /* 6 */ p = temp + strlen(temp);
  288. sprintf(p,"%ld ", inputfile->linecnt); /* 7 */ p = temp + strlen(temp);
  289. sprintf(p,"%f ", inputfile->arate); /* 8 */ p = temp + strlen(temp);
  290. sprintf(p,"%.12f ",inputfile->frametime); /* 9 */ p = temp + strlen(temp);
  291. sprintf(p,"%lf ", inputfile->nyquist); /* 10 */ p = temp + strlen(temp);
  292. sprintf(p,"%lf ", inputfile->duration); /* 11 */ p = temp + strlen(temp);
  293. sprintf(p,"%ld ", inputfile->stype); /* 12 */ p = temp + strlen(temp);
  294. sprintf(p,"%ld ", inputfile->origstype); /* 13 */ p = temp + strlen(temp);
  295. sprintf(p,"%ld ", inputfile->origrate); /* 14 */ p = temp + strlen(temp);
  296. sprintf(p,"%d ", inputfile->Mlen); /* 15 */ p = temp + strlen(temp);
  297. sprintf(p,"%d ", inputfile->Dfac); /* 16 */ p = temp + strlen(temp);
  298. sprintf(p,"%ld ", inputfile->origchans); /* 17 */ p = temp + strlen(temp);
  299. sprintf(p,"%d ", inputfile->specenvcnt); /* 18 */ p = temp + strlen(temp);
  300. sprintf(p,"%d ", inputfile->out_chans); /* 19 */ p = temp + strlen(temp);
  301. //TW REVISED
  302. // sprintf(p,"%ld ", inputfile->descriptor_bytes); /* 20 * p = temp + strlen(temp); /*RWD TODO : ??? */
  303. sprintf(p,"%ld ", inputfile->descriptor_samps); /* 20 */ p = temp + strlen(temp);
  304. sprintf(p,"%d ", inputfile->is_transpos); /* 21 */ p = temp + strlen(temp);
  305. sprintf(p,"%d ", inputfile->could_be_transpos); /* 22 */ p = temp + strlen(temp);
  306. sprintf(p,"%d ", inputfile->could_be_pitch); /* 23 */ p = temp + strlen(temp);
  307. sprintf(p,"%d ", inputfile->different_srates); /* 24 */ p = temp + strlen(temp);
  308. sprintf(p,"%d ", inputfile->duplicate_snds); /* 25 */ p = temp + strlen(temp);
  309. sprintf(p,"%ld ", inputfile->brksize); /* 26 */ p = temp + strlen(temp);
  310. sprintf(p,"%ld ", inputfile->numsize); /* 27 */ p = temp + strlen(temp);
  311. sprintf(p,"%ld ", inputfile->all_words); /* 28 */ p = temp + strlen(temp);
  312. sprintf(p,"%f ", inputfile->window_size); /* 29 */ p = temp + strlen(temp);
  313. sprintf(p,"%lf ", inputfile->minbrk); /* 30 */ p = temp + strlen(temp);
  314. sprintf(p,"%lf ", inputfile->maxbrk); /* 31 */ p = temp + strlen(temp);
  315. sprintf(p,"%lf ", inputfile->minnum); /* 32 */ p = temp + strlen(temp);
  316. sprintf(p,"%lf ", inputfile->maxnum); /* 33 */ p = temp + strlen(temp);
  317. //TW maxsamp is now float value
  318. sprintf(p,"%lf ", maxamp); /* 34 value of maxsamp */ p = temp + strlen(temp);
  319. sprintf(p,"%ld ", (int)round(maxloc)); /* 35 location maxsamp */ p = temp + strlen(temp);
  320. sprintf(p,"%ld\n", maxrep); /* 36 no of repets, [old usage, -1 flagged no maxval] */
  321. fprintf(stdout,"%s",temp);
  322. fprintf(stdout,"END\n");
  323. fflush(stdout);
  324. return(SUCCEEDED);
  325. #else
  326. return(FINISHED);
  327. #endif
  328. }
  329. /********************************** CHECK_FOR_DATA_IN_FILE ************************************/
  330. int check_for_data_in_file(char *filename,infileptr inputfile)
  331. {
  332. switch(inputfile->filetype) {
  333. case(FORMANTFILE):
  334. /*if(inputfile->infilesize <= (int)(DESCRIPTOR_DATA_BLOKS * inputfile->specenvcnt * sizeof(float))) */
  335. if(inputfile->insams <= (int)(DESCRIPTOR_DATA_BLOKS * inputfile->specenvcnt)) {
  336. sprintf(errstr, "FILE %s contains no data.\n",filename);
  337. return(DATA_ERROR);
  338. }
  339. break;
  340. default:
  341. if(inputfile->insams <= 0L) {
  342. sprintf(errstr, "FILE %s contains no data.\n",filename);
  343. return(DATA_ERROR);
  344. }
  345. break;
  346. }
  347. return(FINISHED);
  348. }
  349. /************************* INITIALISE_FILEPTR *************************/
  350. void initialise_fileptr(infileptr inputfile)
  351. {
  352. inputfile->filetype = 0;
  353. /*inputfile->infilesize = 0;*/
  354. inputfile->insams = 0;
  355. inputfile->srate = 0;
  356. inputfile->channels = 0;
  357. inputfile->stype = 0;
  358. inputfile->origstype = 0;
  359. inputfile->origrate = 0;
  360. inputfile->Mlen = 0;
  361. inputfile->Dfac = 0;
  362. inputfile->origchans = 0;
  363. inputfile->specenvcnt = 0;
  364. inputfile->wanted = 0;
  365. inputfile->wlength = 0;
  366. inputfile->out_chans = 0;
  367. inputfile->descriptor_samps = 0;
  368. inputfile->is_transpos = FALSE;
  369. inputfile->could_be_transpos = FALSE;
  370. inputfile->could_be_pitch = FALSE;
  371. inputfile->different_srates = FALSE;
  372. inputfile->duplicate_snds = FALSE;
  373. inputfile->brksize = 0;
  374. inputfile->numsize = 0;
  375. inputfile->linecnt = 0;
  376. inputfile->all_words = 0;
  377. inputfile->arate = 0.0f;
  378. inputfile->frametime = 0.0f;
  379. inputfile->window_size = 0.0f;
  380. inputfile->nyquist = 0.0;
  381. inputfile->duration = 0.0;
  382. inputfile->minbrk = 0.0;
  383. inputfile->maxbrk = 0.0;
  384. inputfile->minnum = 0.0;
  385. inputfile->maxnum = 0.0;
  386. }
  387. /***************************** SET_WLENGTH_AND_CHECK_DATA *******************************/
  388. int set_wlength_and_check_data(char *filename,int ifd,infileptr inputfile)
  389. {
  390. int exit_status;
  391. switch(inputfile->filetype) {
  392. case(ANALFILE):
  393. inputfile->wanted = inputfile->channels;
  394. inputfile->wlength = inputfile->insams/inputfile->wanted;
  395. break;
  396. case(PITCHFILE):
  397. case(TRANSPOSFILE):
  398. inputfile->wlength = inputfile->insams;
  399. inputfile->wanted = 1; /* notional: to avoid error calls with bad command lines */
  400. if((exit_status = check_pitch_or_transpos_binary_data(inputfile,filename,ifd))<0)
  401. return(exit_status);
  402. break;
  403. case(FORMANTFILE):
  404. inputfile->wlength = (inputfile->insams/inputfile->specenvcnt) - DESCRIPTOR_DATA_BLOKS;
  405. inputfile->descriptor_samps = inputfile->specenvcnt * DESCRIPTOR_DATA_BLOKS;
  406. inputfile->wanted = inputfile->origchans; /* temporary val for initialise_specenv */
  407. /* working val set in ...chunklen...() */
  408. break;
  409. default:
  410. sprintf(errstr,"Invalid case: set_wlength_and_check_data()\n",inputfile->filetype);
  411. return(PROGRAM_ERROR);
  412. }
  413. return(FINISHED);
  414. }
  415. /********************* CHECK_PITCH_OR_TRANSPOS_BINARY_DATA *************************/
  416. int check_pitch_or_transpos_binary_data(infileptr inputfile,char *filename,int ifd)
  417. {
  418. int samps_read;
  419. int samps_to_read = inputfile->insams;
  420. int n;
  421. float *thisdata;
  422. if((thisdata = (float *)malloc((size_t)(samps_to_read * sizeof(float))))==NULL) {
  423. sprintf(errstr,"INSUFFICIENT MEMORY for pitch_or_transpos_binary_data of file %s\n",filename);
  424. return(MEMORY_ERROR);
  425. }
  426. samps_read = fgetfbufEx(thisdata, samps_to_read,ifd,0);
  427. if(samps_read!= inputfile->insams) {
  428. /*RWD.6.98 lets discriminate!*/
  429. if(samps_read < 0){
  430. sprintf(errstr,"Failed to read infile data.\n");
  431. return SYSTEM_ERROR;
  432. }
  433. sprintf(errstr,"Problem reading binary pitch or transposition data in file %s.\n",filename);
  434. return(PROGRAM_ERROR);
  435. }
  436. switch(inputfile->filetype) {
  437. case(PITCHFILE):
  438. for(n=0;n<inputfile->insams;n++) {
  439. //TW BRACKETED OUT: NEW CODE DEALS WITH UNPITCHED, AND SILENT WINDOWS
  440. /*
  441. if(thisdata[n] < FLTERR) {
  442. sprintf(errstr,"File %s contains unpitched windows: cannot proceed.\n",filename);
  443. return(DATA_ERROR);
  444. }
  445. */
  446. if(thisdata[n] >= inputfile->nyquist) {
  447. sprintf(errstr,"File %s contains pitches beyond nyquist: cannot proceed.\n",filename);
  448. return(DATA_ERROR);
  449. }
  450. }
  451. break;
  452. case(TRANSPOSFILE):
  453. for(n=0;n<inputfile->insams;n++) {
  454. if(thisdata[n] < MIN_TRANSPOS || thisdata[n] >= MAX_TRANSPOS) {
  455. sprintf(errstr,"Transposition data item %d (%f):file %s: out of range (%lf to %lf)\n",
  456. n+1,thisdata[n],filename,MIN_TRANSPOS,MAX_TRANSPOS);
  457. return(DATA_ERROR);
  458. }
  459. }
  460. inputfile->is_transpos = TRUE;
  461. break;
  462. }
  463. free(thisdata);
  464. return(FINISHED);
  465. }
  466. /************************************* MEGAPARSEC ******************************/
  467. int megaparsec(char *filename,FILE *fp,infileptr inputfile)
  468. {
  469. int exit_status;
  470. /* NEW CODE */
  471. int extended_brkfile = POSSIBLE;
  472. /* NEW CODE */
  473. int sndlist = POSSIBLE;
  474. int numlist = POSSIBLE;
  475. int brklist = POSSIBLE;
  476. int mixlist = POSSIBLE;
  477. int linelist = POSSIBLE;
  478. int synclist = POSSIBLE;
  479. int has_comments = FALSE;
  480. int dB_notation = FALSE;
  481. char temp[200];
  482. char **wordstor;
  483. int *wordcnt;
  484. int numcnt = 0, linecnt = 0, total_wordcnt = 0, max_line_word_cnt = 0, min_line_word_cnt, n, srate;
  485. double dur = 0.0;
  486. if((exit_status = initial_parse_textfile(filename,fp,&linelist,&has_comments,&dB_notation,
  487. &total_wordcnt,&numcnt,&linecnt,&max_line_word_cnt,&min_line_word_cnt))<0)
  488. return(exit_status);
  489. initial_textfile_check(dB_notation,&sndlist,&mixlist,&brklist,
  490. &numlist,numcnt,total_wordcnt,max_line_word_cnt,min_line_word_cnt);
  491. if((wordstor = (char **)malloc(total_wordcnt * sizeof(char *)))==NULL) {
  492. sprintf(errstr,"INSUFFICIENT MEMORY for wordstor: file %s\n",filename);
  493. return(MEMORY_ERROR);
  494. }
  495. for(n=0;n<total_wordcnt;n++) /* initialise, for testing and safe freeing */
  496. wordstor[n] = NULL;
  497. inputfile->all_words = total_wordcnt;
  498. if((wordcnt = (int *)malloc(linecnt * sizeof(int)))==NULL) {
  499. sprintf(errstr,"INSUFFICIENT MEMORY for wordcnt: file %s\n",filename);
  500. return(MEMORY_ERROR);
  501. }
  502. for(n=0;n<linecnt;n++) /* initialise, for later testing */
  503. wordcnt[n] = 0;
  504. if(fseek(fp,0,0)<0) {
  505. sprintf(errstr,"fseek failed in megaparsec(): 1\n");
  506. return(PROGRAM_ERROR);
  507. }
  508. if((exit_status = store_wordlist_without_comments(fp,temp,inputfile->all_words,wordstor,wordcnt,inputfile))<0)
  509. return(exit_status);
  510. if(sndlist==POSSIBLE) {
  511. if((exit_status = check_for_sndlist(wordstor,wordcnt,linecnt,&sndlist))<0)
  512. return(exit_status);
  513. } else if(numlist==POSSIBLE) {
  514. if((exit_status = check_for_brklist_and_count_numbers(wordstor,wordcnt,linecnt,&brklist,inputfile,filename))<0)
  515. return(exit_status);
  516. }
  517. if(sndlist==POSSIBLE || numlist==POSSIBLE)
  518. mixlist = FALSE;
  519. if(mixlist==POSSIBLE) {
  520. if((exit_status = is_a_possible_mixfile(wordstor,wordcnt,linecnt,&mixlist,&srate,inputfile,filename,&dur))<0)
  521. return(exit_status);
  522. if(mixlist==FALSE)
  523. inputfile->out_chans = 0;
  524. else {
  525. inputfile->srate = srate;
  526. inputfile->duration = dur;
  527. synclist = FALSE;
  528. brklist = FALSE;
  529. sndlist = FALSE;
  530. extended_brkfile = FALSE;
  531. }
  532. }
  533. if(linelist==POSSIBLE) {
  534. if((exit_status = check_for_a_linelist(max_line_word_cnt,&linelist,wordstor,inputfile))<0)
  535. return(exit_status);
  536. }
  537. if(sndlist==POSSIBLE) {
  538. if((exit_status = set_sndlist_flags(inputfile,wordstor))<0)
  539. return(exit_status);
  540. if(inputfile->different_srates
  541. #ifdef NO_DUPLICATE_SNDFILES
  542. || inputfile->duplicate_snds
  543. #endif
  544. )
  545. synclist = FALSE;
  546. } else if(synclist==POSSIBLE) {
  547. if((exit_status = check_for_a_synclist(&synclist,wordstor,wordcnt,inputfile,filename))<0)
  548. return(exit_status);
  549. }
  550. //TW REMOVED COMMENTS
  551. if(extended_brkfile==POSSIBLE) {
  552. if((exit_status =
  553. check_for_an_extended_brkfile(&extended_brkfile,wordstor,wordcnt,linecnt,total_wordcnt,inputfile))<0)
  554. return(exit_status);
  555. }
  556. if((inputfile->filetype = assign_type(sndlist,numlist,brklist,mixlist,synclist,linelist,extended_brkfile,inputfile))<0)
  557. return(inputfile->filetype);
  558. return(FINISHED);
  559. }
  560. /************************************* INITIAL_PARSE_TEXTFILE ******************************/
  561. int initial_parse_textfile
  562. (char *filename,FILE *fp,int *linelist,int *has_comments,int *dB_notation,
  563. int *total_wordcnt,int *numcnt,int *linecnt,int *max_line_word_cnt,int *min_line_word_cnt)
  564. {
  565. char temp[200], *p, *q, c;
  566. double val;
  567. int line_num_cnt = 0;
  568. int last_line_word_cnt = 0, last_line_num_cnt = 0;
  569. int line_word_cnt = 0;
  570. *max_line_word_cnt = 0;
  571. *min_line_word_cnt = LONG_MAX;
  572. while((c = (char)fgetc(fp))!=EOF) {
  573. if(!isalnum(c) && !ispunct(c) && !isspace(c)) {
  574. sprintf(errstr,"%s is not a valid CDP file\n",filename);
  575. return(DATA_ERROR);
  576. }
  577. }
  578. if(fseek(fp,0,0) != 0) {
  579. sprintf(errstr,"Cannot rewind to start of file %s\n",filename);
  580. return(DATA_ERROR);
  581. }
  582. while(fgets(temp,200,fp)!=NULL) {
  583. p = temp;
  584. if(is_a_comment(p)) {
  585. *has_comments = TRUE;
  586. continue;
  587. } else if(is_an_empty_line(p))
  588. continue;
  589. line_word_cnt = 0;
  590. line_num_cnt = 0;
  591. while(get_word_from_string(&p,&q)) {
  592. line_word_cnt++;
  593. if(is_a_dB_value(q)) {
  594. *dB_notation = TRUE;
  595. (*numcnt)++;
  596. line_num_cnt++;
  597. } else if(get_float_from_within_string(&q,&val)) {
  598. (*numcnt)++;
  599. line_num_cnt++;
  600. }
  601. }
  602. if(*linecnt) {
  603. if(line_word_cnt != last_line_word_cnt
  604. || line_num_cnt != last_line_num_cnt)
  605. *linelist = FALSE;
  606. }
  607. *max_line_word_cnt = max(*max_line_word_cnt,line_word_cnt);
  608. *min_line_word_cnt = min(*min_line_word_cnt,line_word_cnt);
  609. last_line_word_cnt = line_word_cnt;
  610. last_line_num_cnt = line_num_cnt;
  611. *total_wordcnt += line_word_cnt;
  612. (*linecnt)++;
  613. }
  614. if(*total_wordcnt==0) {
  615. sprintf(errstr,"Cannot get any data from file %s\n",filename);
  616. return(DATA_ERROR);
  617. }
  618. return(FINISHED);
  619. }
  620. /************************************* INITIAL_TEXTFILE_CHECK ******************************/
  621. void initial_textfile_check(int dB_notation,int *sndlist,int *mixlist,int *brklist,int *numlist,
  622. int numcnt,int wordcnt,int max_line_word_cnt,int min_line_word_cnt)
  623. {
  624. if((numcnt < wordcnt) || dB_notation) {
  625. *numlist = FALSE;
  626. *brklist = FALSE;
  627. }
  628. if(numcnt == wordcnt) {
  629. *mixlist = FALSE;
  630. *sndlist = FALSE;
  631. }
  632. if(ODD(numcnt))
  633. *brklist = FALSE;
  634. if(numcnt>0)
  635. *sndlist = FALSE;
  636. else
  637. *mixlist = FALSE;
  638. if(min_line_word_cnt<MIX_MINLINE || max_line_word_cnt>MIX_MAXLINE)
  639. *mixlist = FALSE;
  640. }
  641. /************************************* VALID_START_OF_MIXFILE_LINE ******************************/
  642. #ifdef IS_CDPARSE
  643. int valid_start_of_mixfile_line
  644. (int k,int *chans,int lineno,char **wordstor,int *srate,int *mixlist,infileptr inputfile,char *filename,double *dur)
  645. {
  646. int exit_status;
  647. double time;
  648. char *sndfilename;
  649. sndfilename = wordstor[k++];
  650. if(sscanf(wordstor[k++],"%lf",&time)!=1) {
  651. *mixlist = FALSE;
  652. return(FINISHED);
  653. }
  654. if(time<0.0) {
  655. *mixlist = FALSE;
  656. return(FINISHED);
  657. }
  658. if(sscanf(wordstor[k++],"%d",chans)!=1) {
  659. *mixlist = FALSE;
  660. return(FINISHED);
  661. }
  662. if(*chans < MONO || *chans > STEREO) {
  663. *mixlist = FALSE;
  664. return(FINISHED);
  665. }
  666. if((exit_status = is_a_valid_sndfile_for_mixfile(filename,sndfilename,srate,*chans,lineno,inputfile,time,dur))<0)
  667. return(exit_status);
  668. if(exit_status == FALSE) {
  669. *mixlist = FALSE;
  670. return(FINISHED);
  671. }
  672. if(!OK_level(wordstor[k])) { /* must be numeric, and witihin range */
  673. *mixlist = FALSE;
  674. return(FINISHED);
  675. }
  676. return(FINISHED);
  677. }
  678. #else
  679. int valid_start_of_mixfile_line
  680. (int k,int *chans,int lineno,char **wordstor,int *srate,int *mixlist,infileptr inputfile,char *filename,double *dur)
  681. {
  682. int exit_status;
  683. double time;
  684. char *sndfilename;
  685. sndfilename = wordstor[k++];
  686. if(sscanf(wordstor[k++],"%lf",&time)!=1) {
  687. if(lineno>0) {
  688. fprintf(stdout,"WARNING: If %s is a mixfile: Invalid or missing time val on line %d\n",filename,lineno+1);
  689. fflush(stdout);
  690. }
  691. *mixlist = FALSE;
  692. return(FINISHED);
  693. }
  694. if(time<0.0) {
  695. if(lineno>0) {
  696. fprintf(stdout,"WARNING: If %s is a mixfile: starttime less than zero [%lf]: line %d\n",
  697. filename,time,lineno+1);
  698. fflush(stdout);
  699. }
  700. *mixlist = FALSE;
  701. return(FINISHED);
  702. }
  703. if(sscanf(wordstor[k++],"%d",chans)!=1) {
  704. if(lineno>0) {
  705. fprintf(stdout,"WARNING: If %s is a mixfile: Invalid or missing channel val on line %d\n",
  706. filename,lineno+1);
  707. fflush(stdout);
  708. }
  709. *mixlist = FALSE;
  710. return(FINISHED);
  711. }
  712. if(*chans < MONO || *chans > STEREO) {
  713. if(lineno>0) {
  714. fprintf(stdout,"WARNING: If %s is a mixfile: Invalid channel-cnt on line %d\n",filename,lineno+1);
  715. fflush(stdout);
  716. }
  717. *mixlist = FALSE;
  718. return(FINISHED);
  719. }
  720. if((exit_status = is_a_valid_sndfile_for_mixfile(filename,sndfilename,srate,*chans,lineno,inputfile,time,dur))<0)
  721. return(exit_status);
  722. if(exit_status == FALSE) {
  723. *mixlist = FALSE;
  724. return(FINISHED);
  725. }
  726. if(!OK_level(wordstor[k])) { /* must be numeric, and witihin range */
  727. if(lineno>0) {
  728. fprintf(stdout,"WARNING: If %s is a mixfile: Invalid [channel 1] level val on line %d\n",
  729. filename,lineno+1);
  730. fflush(stdout);
  731. }
  732. *mixlist = FALSE;
  733. return(FINISHED);
  734. }
  735. return(FINISHED);
  736. }
  737. #endif
  738. /************************************* IS_VALID_END_OF_MIXFILE_LINE ******************************/
  739. #ifdef IS_CDPARSE
  740. int is_valid_end_of_mixfile_line(int j,int chans,int lineno,char **wordstor,int *wordcnt)
  741. {
  742. double pan;
  743. //TW UPDATE: AVOID WARNING
  744. int got_numeric_panval = sscanf(wordstor[j],"%lf",&pan);
  745. if(strcmp(wordstor[j],"C") && strcmp(wordstor[j],"L") && strcmp(wordstor[j],"R")
  746. && got_numeric_panval!=1)
  747. return(FALSE);
  748. if(got_numeric_panval && (pan < MINPAN || pan >MAXPAN))
  749. return(FALSE);
  750. if(chans==MONO) {
  751. if(wordcnt[lineno]>MIX_MIDLINE) {
  752. return(FALSE);
  753. }
  754. return(TRUE);
  755. } else {
  756. if(wordcnt[lineno]<MIX_MAXLINE) {
  757. return(FALSE);
  758. }
  759. }
  760. j++;
  761. if(!OK_level(wordstor[j++])) /* level must be numeric or dB & in range */
  762. return(FALSE);
  763. got_numeric_panval = 0;
  764. //TW UPDATE: AVOID WARNING
  765. got_numeric_panval = sscanf(wordstor[j],"%lf",&pan);
  766. if(strcmp(wordstor[j],"C") && strcmp(wordstor[j],"L") && strcmp(wordstor[j],"R")
  767. && (got_numeric_panval !=1)) /* pan must be C,R,L or numeric */
  768. return(FALSE);
  769. if(got_numeric_panval && (pan < MINPAN || pan > MAXPAN)) {
  770. return(FALSE);
  771. }
  772. return(TRUE);
  773. }
  774. #else
  775. int is_valid_end_of_mixfile_line(int j,int chans,int lineno,char **wordstor,int *wordcnt,char *filename)
  776. {
  777. double pan;
  778. //TW UPDATE: AVOID WARNING
  779. int got_numeric_panval = sscanf(wordstor[j],"%lf",&pan);
  780. if(strcmp(wordstor[j],"C") && strcmp(wordstor[j],"L") && strcmp(wordstor[j],"R")
  781. && got_numeric_panval!=1) {
  782. if(lineno>0) {
  783. fprintf(stdout,"WARNING: If %s is a mixfile: Invalid (1st) pan val, line %d\n",filename,lineno+1);
  784. fflush(stdout);
  785. }
  786. return(FALSE);
  787. }
  788. if(got_numeric_panval && (pan < MINPAN || pan >MAXPAN)) {
  789. if(lineno>0) {
  790. fprintf(stdout,"WARNING: If %s is a mixfile: Invalid (1st) pan val [%lf], line %d\n",
  791. filename,pan,lineno+1);
  792. fflush(stdout);
  793. }
  794. return(FALSE);
  795. }
  796. if(chans==MONO) {
  797. if(wordcnt[lineno]>MIX_MIDLINE) {
  798. if(lineno>0) {
  799. fprintf(stdout,"WARNING: If %s is a mixfile: Too many words on line %d\n",filename,lineno+1);
  800. fflush(stdout);
  801. }
  802. return(FALSE);
  803. }
  804. return(TRUE);
  805. } else {
  806. if(wordcnt[lineno]<MIX_MAXLINE) {
  807. if(lineno>0) {
  808. fprintf(stdout,"WARNING: If %s is a mixfile: Too few entries for stereo file on line %d\n",
  809. filename,lineno+1);
  810. fflush(stdout);
  811. }
  812. return(FALSE);
  813. }
  814. }
  815. j++;
  816. if(!OK_level(wordstor[j++])) { /* level must be numeric or dB & in range */
  817. if(lineno>0) {
  818. fprintf(stdout,"WARNING: If %s is a mixfile: Missing level for chan2 on line %d\n",filename,lineno+1);
  819. fflush(stdout);
  820. }
  821. return(FALSE);
  822. }
  823. got_numeric_panval = 0;
  824. //TW UPDATE: AVOID WARNING
  825. got_numeric_panval = sscanf(wordstor[j],"%lf",&pan);
  826. if(strcmp(wordstor[j],"C") && strcmp(wordstor[j],"L") && strcmp(wordstor[j],"R")
  827. && (got_numeric_panval !=1)) { /* pan must be C,R,L or numeric */
  828. if(lineno>0) {
  829. fprintf(stdout,"WARNING: If %s is a mixfile: Missing chan2 pan val on line %d\n",
  830. filename,lineno+1);
  831. fflush(stdout);
  832. }
  833. return(FALSE);
  834. }
  835. if(got_numeric_panval && (pan < MINPAN || pan > MAXPAN)) {
  836. if(lineno>0) {
  837. fprintf(stdout,"WARNING: If %s is a mixfile: Invalid 2nd pan val [%lf], line %d\n",
  838. filename,pan,lineno+1);
  839. fflush(stdout);
  840. }
  841. return(FALSE);
  842. }
  843. return(TRUE);
  844. }
  845. #endif
  846. /************************************* CONFIRM_TRUE_STEREO ******************************/
  847. int confirm_true_stereo(int *chans,int *is_left,int *is_right,int *is_central,char *panword)
  848. {
  849. double pan;
  850. if(!strcmp(panword,"L")) {
  851. *is_left = TRUE; /* panned hard Left */
  852. *chans = MONO;
  853. } else if(!strcmp(panword,"R")) {
  854. *is_right = TRUE; /* panned hard Right */
  855. *chans = MONO;
  856. } else if(strcmp(panword,"C")) {
  857. if(sscanf(panword,"%lf",&pan)!=1) {
  858. sprintf(errstr,"Parsing error 2: confirm_true_stereo()\n");
  859. return(PROGRAM_ERROR);
  860. }
  861. if(pan <= -1.0) {
  862. *is_left = TRUE; /* panned hard Left */
  863. *chans = MONO;
  864. } else if(pan >= 1.0) {
  865. *is_right = TRUE; /* panned hard Right */
  866. *chans = MONO;
  867. }
  868. // TW REMOVED COMMENTS
  869. else
  870. *chans = STEREO;
  871. }
  872. else
  873. *is_central = TRUE;
  874. return(FINISHED);
  875. }
  876. /**************** IS_A_POSSIBLE_MIXFILE **********************/
  877. int is_a_possible_mixfile(char **wordstor,int *wordcnt,int linecnt,int *mixlist,
  878. int *srate,infileptr inputfile,char *filename,double *dur)
  879. {
  880. int exit_status;
  881. int is_left = FALSE, is_right = FALSE, is_central = FALSE;
  882. int /*cnt = 0,*/ chans;
  883. int k = 0, j, lineno;
  884. if(linecnt > SF_MAXFILES-1) {
  885. #ifndef IS_CDPARSE
  886. fprintf(stdout,"WARNING: If %s is a mixfile: cannot mix more than %d files in one mix\n",
  887. filename,SF_MAXFILES-1);
  888. fflush(stdout);
  889. #endif
  890. *mixlist = FALSE;
  891. return(FINISHED);
  892. }
  893. for(lineno=0;lineno<linecnt;lineno++) {
  894. if((exit_status = valid_start_of_mixfile_line(k,&chans,lineno,wordstor,srate,mixlist,inputfile,filename,dur))<0)
  895. return(exit_status);
  896. if(*mixlist==FALSE)
  897. return(FINISHED);
  898. if(wordcnt[lineno]>MIX_MINLINE) {
  899. j = k + MIX_MINLINE;
  900. #ifdef IS_CDPARSE
  901. if(!is_valid_end_of_mixfile_line(j,chans,lineno,wordstor,wordcnt)) {
  902. #else
  903. if(!is_valid_end_of_mixfile_line(j,chans,lineno,wordstor,wordcnt,filename)) {
  904. #endif
  905. *mixlist = FALSE;
  906. return(FINISHED);
  907. }
  908. if(wordcnt[lineno]==MIX_MIDLINE && chans==MONO) {
  909. if((exit_status = confirm_true_stereo(&chans,&is_left,&is_right,&is_central,wordstor[j]))<0)
  910. return(exit_status);
  911. }
  912. } else
  913. is_central = TRUE;
  914. inputfile->out_chans = max(inputfile->out_chans,chans);
  915. k += wordcnt[lineno];
  916. }
  917. #ifdef NO_DUPLICATE_SNDFILES
  918. #ifdef IS_CDPARSE
  919. if(duplicated_filenames(linecnt,wordstor,wordcnt))
  920. #else
  921. if(duplicated_filenames(linecnt,wordstor,wordcnt,filename))
  922. #endif
  923. {
  924. *mixlist = FALSE;
  925. return(FINISHED);
  926. }
  927. #endif
  928. if(inputfile->out_chans==MONO) {
  929. if((is_left && is_right)
  930. || (is_left && is_central)
  931. || (is_right && is_central))
  932. inputfile->out_chans = STEREO;
  933. }
  934. if(inputfile->out_chans < MONO) {
  935. sprintf(errstr,"Impossible out_chans value\n");
  936. return(PROGRAM_ERROR);
  937. }
  938. return(FINISHED);
  939. }
  940. /******************* DUPLICATED_FILENAMES **************************/
  941. #ifdef IS_CDPARSE
  942. int duplicated_filenames(int linecnt,char **wordstor,int *wordcnt)
  943. {
  944. int n, m;
  945. int j, k = 0;
  946. for(n=0;n<linecnt-1;n++) {
  947. j = k;
  948. for(m=n+1;m<linecnt;m++) {
  949. j += wordcnt[m-1];
  950. if(!strcmp(wordstor[k],wordstor[j]))
  951. return(TRUE);
  952. }
  953. k += wordcnt[n];
  954. }
  955. return(FALSE);
  956. }
  957. #else
  958. int duplicated_filenames(int linecnt,char **wordstor,int *wordcnt,char *filename)
  959. {
  960. int n, m;
  961. int j, k = 0;
  962. for(n=0;n<linecnt-1;n++) {
  963. j = k;
  964. for(m=n+1;m<linecnt;m++) {
  965. j += wordcnt[m-1];
  966. if(!strcmp(wordstor[k],wordstor[j])) {
  967. #ifndef IS_CDPARSE
  968. fprintf(stdout,"WARNING: If %s is a mix or syncfile: filename duplicates found (not allowed).\n",filename);
  969. fflush(stdout);
  970. #endif
  971. return(TRUE);
  972. }
  973. }
  974. k += wordcnt[n];
  975. }
  976. return(FALSE);
  977. }
  978. #endif
  979. /*************** OK_LEVEL ******************/
  980. int OK_level(char *str)
  981. {
  982. int exit_status = TRUE;
  983. double level;
  984. if(is_a_dB_value(str))
  985. exit_status = get_leveldb_frame(str,&level);
  986. else {
  987. if(sscanf(str,"%lf",&level)!=1)
  988. exit_status = FALSE;
  989. }
  990. if(exit_status==FALSE || level < 0.0)
  991. return(FALSE);
  992. return(TRUE);
  993. }
  994. /************************************* IS_A_DB_VALUE ******************************/
  995. int is_a_dB_value(char *str)
  996. {
  997. int decimal_point_cnt = 0;
  998. int has_digits = FALSE;
  999. double val;
  1000. char *p, *end_of_number;
  1001. p = str + strlen(str) - 2;
  1002. if(strcmp(p,"dB") && strcmp(p,"DB") && strcmp(p,"db"))
  1003. return(FALSE);
  1004. if(p==str)
  1005. return(FALSE);
  1006. end_of_number = p;
  1007. p = str;
  1008. switch(*p) {
  1009. case('-'): break;
  1010. case('.'): decimal_point_cnt=1; break;
  1011. default:
  1012. if(!isdigit(*p))
  1013. return(FALSE);
  1014. has_digits = TRUE;
  1015. break;
  1016. }
  1017. p++;
  1018. while(p!=end_of_number) {
  1019. if(isdigit(*p))
  1020. has_digits = TRUE;
  1021. else if(*p == '.') {
  1022. if(++decimal_point_cnt>1)
  1023. return(FALSE);
  1024. } else
  1025. return(FALSE);
  1026. p++;
  1027. }
  1028. if(!has_digits || sscanf(str,"%lf",&val)!=1)
  1029. return(FALSE);
  1030. return(TRUE);
  1031. }
  1032. /**************************** GET_LEVELDB_FRAME ********************************/
  1033. int get_leveldb_frame(char *str,double *val)
  1034. {
  1035. int is_neg = 0;
  1036. if(sscanf(str,"%lf",val)!=1)
  1037. return(FALSE);
  1038. *val = max(*val,MIN_DB_ON_16_BIT);
  1039. if(flteq(*val,0.0)) {
  1040. *val = 1.0;
  1041. return(TRUE);
  1042. }
  1043. if(*val<0.0) {
  1044. *val = -(*val);
  1045. is_neg = 1;
  1046. }
  1047. *val /= 20.0;
  1048. *val = pow(10.0,*val);
  1049. if(is_neg)
  1050. *val = 1.0/(*val);
  1051. return(TRUE);
  1052. }
  1053. /***************************** IS_A_VALID_SNDFILE_FOR_MIXFILE **************************/
  1054. int is_a_valid_sndfile_for_mixfile
  1055. (char *filename,char *sndfilename,int *srate,int chans,int linecnt,infileptr inputfile,double time,double *dur)
  1056. {
  1057. int exit_status;
  1058. /*int minsize = 0L;*/
  1059. double thisdur;
  1060. int ifd;
  1061. double maxamp, maxloc;
  1062. int maxrep;
  1063. #ifdef IS_CDPARSE
  1064. int getmaxinfo = 1;
  1065. #else
  1066. int getmaxinfo = 0;
  1067. #endif
  1068. char *p = strrchr(sndfilename, '.');
  1069. if(p == NULL) {
  1070. #ifndef IS_CDPARSE
  1071. fprintf(stdout,"WARNING: If %s is a mixfile: uses %s which has no sndfile extension\n",filename,sndfilename);
  1072. fflush(stdout);
  1073. return(FALSE);
  1074. #endif
  1075. }
  1076. p++;
  1077. #ifdef NOTDEF
  1078. /*RWD 4:2002 this is silly! sfsys accepts aiff files, so cdparse certainly can! */
  1079. if(_stricmp(p,(getenv("CDP_SOUND_EXT")))) {
  1080. #ifndef IS_CDPARSE
  1081. fprintf(stdout,"WARNING: If %s is a mixfile: uses %s which has an invalid sndfile extension : %s is not %s\n",filename,sndfilename,p,getenv("CDP_SOUND_EXT"));
  1082. fflush(stdout);
  1083. return(FALSE);
  1084. #endif
  1085. }
  1086. #endif
  1087. if((ifd = sndopenEx(sndfilename,0,CDP_OPEN_RDONLY)) < 0) {
  1088. #ifndef IS_CDPARSE
  1089. fprintf(stdout,"WARNING: If %s is a mixfile: uses %s which is not a sndfile\n",filename,sndfilename);
  1090. fflush(stdout);
  1091. #endif
  1092. return(FALSE);
  1093. }
  1094. if((exit_status = readhead(inputfile,ifd,sndfilename,&maxamp,&maxloc,&maxrep,0,getmaxinfo))<0) { /* READ THE FILE HEADER */
  1095. if(sndcloseEx(ifd)<0)
  1096. sprintf(errstr,"Failed to close file %s when checking file %s\n",sndfilename,filename);
  1097. return(exit_status);
  1098. }
  1099. exit_status = TRUE;
  1100. if(inputfile->filetype!=SNDFILE) {
  1101. #ifndef IS_CDPARSE
  1102. fprintf(stdout,"WARNING: If %s is a mixfile: uses %s which is not a sndfile\n",filename,sndfilename);
  1103. fflush(stdout);
  1104. #endif
  1105. exit_status=FALSE;
  1106. } else if(inputfile->channels != chans) {
  1107. if(chans==MONO)
  1108. #ifndef IS_CDPARSE
  1109. fprintf(stdout,"WARNING: If %s is a mixfile: %s is not a mono sndfile\n",filename,sndfilename);
  1110. else if(chans==STEREO) /*RWD 4:2001 added test */
  1111. fprintf(stdout,"WARNING: If %s is a mixfile: %s is not a stereo sndfile\n",filename,sndfilename);
  1112. fflush(stdout);
  1113. #endif
  1114. exit_status=FALSE;
  1115. } else if((inputfile->insams = sndsizeEx(ifd))<0) { /* FIND SIZE OF FILE */
  1116. sprintf(errstr, "Can't read size of input file %s: used in file %s\n",sndfilename,filename);
  1117. exit_status=PROGRAM_ERROR;
  1118. } else if(inputfile->insams <= 0L) {
  1119. #ifndef IS_CDPARSE
  1120. fprintf(stdout,"WARNING: If %s is a mixfile: file %s contains no data.\n",filename,sndfilename);
  1121. fflush(stdout);
  1122. #endif
  1123. exit_status=FALSE;
  1124. } else if (linecnt==0) {
  1125. *srate = inputfile->srate;
  1126. } else if(inputfile->srate != *srate) {
  1127. #ifndef IS_CDPARSE
  1128. fprintf(stdout,"WARNING: If %s is a mixfile: Incompatible sample-rate in file %s\n",filename,sndfilename);
  1129. fflush(stdout);
  1130. #endif
  1131. exit_status=FALSE;
  1132. }
  1133. #ifdef NOTDEF /* RWD 4:2001 */
  1134. switch(inputfile->filetype) {
  1135. case(SNDFILE): inputfile->insams = inputfile->infilesize/sizeof(short); break;
  1136. case(FLT_SNDFILE): inputfile->insams = inputfile->infilesize/sizeof(float); break;
  1137. }
  1138. #endif
  1139. #ifdef _DEBUG
  1140. assert(inputfile->insams > 0);
  1141. #endif
  1142. thisdur = time + (inputfile->insams/inputfile->channels/(double)inputfile->srate);
  1143. *dur = max(*dur,thisdur);
  1144. // inputfile->infilesize = 0;
  1145. inputfile->insams = 0;
  1146. inputfile->filetype = 0;
  1147. inputfile->srate = 0;
  1148. inputfile->channels = 0;
  1149. inputfile->stype = 0;
  1150. if(sndcloseEx(ifd)<0) {
  1151. sprintf(errstr,"Failed to close file %s while checking file %s\n",sndfilename,filename);
  1152. exit_status=SYSTEM_ERROR;
  1153. }
  1154. return(exit_status);
  1155. }
  1156. /*************************** CHECK_FOR_BRKLIST_AND_STORE_NUMBERS ***********************/
  1157. int check_for_brklist_and_count_numbers
  1158. (char **wordstor,int *wordcnt,int linecnt,int *brklist,infileptr inputfile,char *filename)
  1159. {
  1160. double p, lasttime = 0.0;
  1161. int istime = TRUE;
  1162. int lineno, word_in_line, dcount = 0, k = 0;
  1163. for(lineno=0;lineno<linecnt;lineno++) {
  1164. for(word_in_line = 0;word_in_line < wordcnt[lineno];word_in_line++) {
  1165. if(sscanf(wordstor[k],"%lf",&p)!=1) {
  1166. sprintf(errstr,"scan anomaly: check_for_brklist_and_count_numbers()\n");
  1167. return(PROGRAM_ERROR);
  1168. }
  1169. if(istime) {
  1170. if(*brklist==POSSIBLE) {
  1171. if(k==0) {
  1172. if(p < 0.0)
  1173. *brklist = FALSE;
  1174. } else if(p <= lasttime)
  1175. *brklist = FALSE;
  1176. lasttime = p;
  1177. }
  1178. if(k==0) {
  1179. inputfile->maxnum = p;
  1180. inputfile->minnum = p;
  1181. } else {
  1182. inputfile->maxnum = max(p,inputfile->maxnum);
  1183. inputfile->minnum = min(p,inputfile->minnum);
  1184. }
  1185. } else {
  1186. if(k==1) {
  1187. inputfile->maxbrk = p;
  1188. inputfile->minbrk = p;
  1189. } else {
  1190. inputfile->maxbrk = max(p,inputfile->maxbrk);
  1191. inputfile->minbrk = min(p,inputfile->minbrk);
  1192. }
  1193. inputfile->maxnum = max(p,inputfile->maxnum);
  1194. inputfile->minnum = min(p,inputfile->minnum);
  1195. }
  1196. istime = !istime;
  1197. k++;
  1198. }
  1199. }
  1200. if(k == 0) {
  1201. sprintf(errstr,"No numeric data found in file %s\n",filename);
  1202. return(DATA_ERROR);
  1203. }
  1204. if(*brklist==POSSIBLE) {
  1205. if(k < 4) /* Must be at least 2 point in a brkfile */
  1206. *brklist = FALSE;
  1207. }
  1208. if(*brklist==POSSIBLE) {
  1209. if(((dcount = k/2) * 2) != k)
  1210. *brklist = FALSE;
  1211. }
  1212. if(*brklist==POSSIBLE) {
  1213. inputfile->brksize = dcount;
  1214. inputfile->duration = lasttime;
  1215. }
  1216. else
  1217. inputfile->brksize = 0;
  1218. inputfile->numsize = k;
  1219. return(FINISHED);
  1220. }
  1221. /*************************** CHECK_FOR_A_LINELIST ***********************/
  1222. #define OTHER (0)
  1223. #define NUMERICAL (1)
  1224. #define DBLIKE (2)
  1225. int check_for_a_linelist(int line_word_cnt,int *linelist,char **wordstor,infileptr inputfile)
  1226. {
  1227. int linecnt = 0, n, m;
  1228. int total_wordcnt = 0;
  1229. //TW AGREED REMOVAL of 'OK'
  1230. int k;
  1231. double val;
  1232. char *wordtype[2], *p;
  1233. if((wordtype[0] = (char *)malloc(line_word_cnt * sizeof(char)))==NULL) {
  1234. sprintf(errstr,"INSUFFICIENT MEMORY for wordtype[0]\n");
  1235. return(MEMORY_ERROR);
  1236. }
  1237. if((wordtype[1] = (char *)malloc(line_word_cnt * sizeof(char)))==NULL) {
  1238. sprintf(errstr,"INSUFFICIENT MEMORY for wordtype[1]\n");
  1239. free(wordtype[0]);
  1240. return(MEMORY_ERROR);
  1241. }
  1242. for(n=0;n<inputfile->linecnt;n++) {
  1243. if(n==0) k = 0;
  1244. else k = 1;
  1245. for(m=0;m<line_word_cnt;m++) {
  1246. p = wordstor[total_wordcnt];
  1247. if(is_a_dB_value(wordstor[total_wordcnt]))
  1248. wordtype[k][m] = (char)DBLIKE;
  1249. else if(get_float_from_within_string(&p,&val))
  1250. wordtype[k][m] = (char)NUMERICAL;
  1251. else
  1252. wordtype[k][m] = (char)OTHER;
  1253. total_wordcnt++;
  1254. }
  1255. if(linecnt>0) {
  1256. for(m=0;m<line_word_cnt;m++) {
  1257. if(wordtype[0][m] != wordtype[1][m]) {
  1258. *linelist = FALSE;
  1259. break;
  1260. }
  1261. }
  1262. }
  1263. if(!(*linelist))
  1264. break;
  1265. linecnt++;
  1266. }
  1267. free(wordtype[0]);
  1268. free(wordtype[1]);
  1269. return(FINISHED);
  1270. }
  1271. /***************************** CHECK_FOR_SNDLIST **************************/
  1272. int check_for_sndlist(char **wordstor,int *wordcnt,int linecnt,int *sndlist)
  1273. {
  1274. char *filename;
  1275. //TW AGREED VARIABLE REMOVED total_wordcnt
  1276. int ifd;
  1277. int k = 0, lineno, word_in_line;
  1278. //RWD.9.98
  1279. SFPROPS props;
  1280. for(lineno=0;lineno<linecnt;lineno++) {
  1281. for(word_in_line=0;word_in_line<wordcnt[lineno];word_in_line++) {
  1282. filename = wordstor[k];
  1283. if((ifd = sndopenEx(filename,0,CDP_OPEN_RDONLY)) < 0) {
  1284. *sndlist = FALSE;
  1285. return(FINISHED);
  1286. }
  1287. //RWD added 9.98
  1288. /* TEST THIS !!! */
  1289. if(!snd_headread(ifd,&props)){
  1290. sprintf(errstr,"Cannot read soundfile header: %s\n",filename);
  1291. sndcloseEx(ifd);
  1292. return(SYSTEM_ERROR);
  1293. }
  1294. if(!(props.type==wt_wave)){
  1295. *sndlist = FALSE;
  1296. sprintf(errstr,"soundfile %s is not a wave file\n",filename);
  1297. sndcloseEx(ifd);
  1298. return FINISHED;
  1299. }
  1300. if(sndcloseEx(ifd)<0) {
  1301. sprintf(errstr,"Cannot close sndfile %s: check_for_sndlist()\n",filename);
  1302. return(SYSTEM_ERROR);
  1303. }
  1304. k++;
  1305. }
  1306. }
  1307. return(FINISHED);
  1308. }
  1309. /***************************** STORE_WORDLIST_WITHOUT_COMMENTS **************************/
  1310. int store_wordlist_without_comments(FILE *fp,char *temp,int total_wordcnt,char **wordstor,int *wordcnt,infileptr inputfile)
  1311. {
  1312. char *p , *q;
  1313. int this_wordcnt = 0;
  1314. int wordcnt_in_line;
  1315. int linecnt = 0;
  1316. while(fgets(temp,200,fp)!=NULL) {
  1317. p = temp;
  1318. if(is_an_empty_line_or_a_comment(p))
  1319. continue;
  1320. wordcnt_in_line = 0;
  1321. while(get_word_from_string(&p,&q)) {
  1322. if((wordstor[this_wordcnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  1323. sprintf(errstr,"INSUFFICIENT MEMORY for word %ld\n",this_wordcnt+1);
  1324. return(MEMORY_ERROR);
  1325. }
  1326. strcpy(wordstor[this_wordcnt],q);
  1327. if(++this_wordcnt > total_wordcnt) {
  1328. sprintf(errstr,"Error in word accounting: store_wordlist_without_comments()\n");
  1329. return(PROGRAM_ERROR);
  1330. }
  1331. wordcnt_in_line++;
  1332. }
  1333. wordcnt[linecnt] = wordcnt_in_line;
  1334. linecnt++;
  1335. }
  1336. inputfile->linecnt = linecnt;
  1337. return(FINISHED);
  1338. }
  1339. /***************************** ASSIGN_TYPE **************************/
  1340. //TW REMOVED OLD COMMENT LINES
  1341. int assign_type(int sndlist,int numlist,int brklist,int mixlist,int synclist,int linelist,int extended_brkfile,infileptr inputfile)
  1342. {
  1343. //TW AGREED WARNING SHOULD BE MOVED
  1344. if(sndlist==POSSIBLE) {
  1345. if(synclist==POSSIBLE) {
  1346. if(linelist==POSSIBLE)
  1347. return SNDLIST_OR_SYNCLIST_LINELIST_OR_WORDLIST;
  1348. else
  1349. return SNDLIST_OR_SYNCLIST_OR_WORDLIST;
  1350. } else {
  1351. if(linelist==POSSIBLE)
  1352. return SNDLIST_OR_LINELIST_OR_WORDLIST;
  1353. else
  1354. return SNDLIST_OR_WORDLIST;
  1355. }
  1356. } else if(numlist==POSSIBLE) {
  1357. if(brklist==POSSIBLE)
  1358. return assign_brkfile_type(inputfile,linelist);
  1359. else {
  1360. if(linelist==POSSIBLE)
  1361. return NUMLIST_OR_LINELIST_OR_WORDLIST;
  1362. else
  1363. return NUMLIST_OR_WORDLIST;
  1364. }
  1365. } else if(mixlist==POSSIBLE) {
  1366. if(linelist==POSSIBLE)
  1367. return MIXLIST_OR_LINELIST_OR_WORDLIST;
  1368. else
  1369. return MIXLIST_OR_WORDLIST;
  1370. } else if(synclist==POSSIBLE) {
  1371. if(linelist==POSSIBLE)
  1372. return SYNCLIST_OR_LINELIST_OR_WORDLIST;
  1373. else
  1374. return SYNCLIST_OR_WORDLIST;
  1375. } else if(linelist==POSSIBLE)
  1376. return LINELIST_OR_WORDLIST;
  1377. //RWD.8.98 do this test here!
  1378. if(extended_brkfile==POSSIBLE) {
  1379. fprintf(stderr,"WARNING:Extended format brkfiles not yet supported by commandline programs.\n");
  1380. }
  1381. return WORDLIST;
  1382. }
  1383. /****************************************** CHECK_FOR_A_SYNCLIST ****************************************/
  1384. int check_for_a_synclist(int *synclist,char **wordstor,int *wordcnt,infileptr inputfile,char *filename)
  1385. {
  1386. int exit_status;
  1387. int n;
  1388. int total_words = 0;
  1389. int srate = 0;
  1390. double starttime, endtime;
  1391. for(n=0;n<inputfile->linecnt;n++) { /* Must be 1 or 3 words on each line */
  1392. if(wordcnt[n]!=1 && wordcnt[n]!=3) {
  1393. *synclist = FALSE;
  1394. return(FINISHED);
  1395. }
  1396. }
  1397. for(n=0;n<inputfile->linecnt;n++) { /* Every line must start with a sndfilename */
  1398. if((exit_status = is_a_valid_sndfile_for_sync(filename,wordstor[total_words],&srate,n,inputfile))<0)
  1399. return(exit_status);
  1400. if(exit_status == FALSE) {
  1401. *synclist = FALSE;
  1402. return(FINISHED);
  1403. }
  1404. total_words += wordcnt[n];
  1405. }
  1406. total_words = 0;
  1407. for(n=0;n<inputfile->linecnt;n++) { /* Any other values must be numbers */
  1408. if(wordcnt[n] > 1) {
  1409. if(sscanf(wordstor[total_words+1],"%lf\n",&starttime)!=1) {
  1410. *synclist = FALSE;
  1411. return(FINISHED);
  1412. }
  1413. else if(sscanf(wordstor[total_words+2],"%lf\n",&endtime)!=1) {
  1414. *synclist = FALSE;
  1415. return(FINISHED);
  1416. } else if(starttime < 0.0 || endtime < 0.0) {
  1417. #ifndef IS_CDPARSE
  1418. fprintf(stdout,"WARNING: If %s is a synclist, it contains negative times\n",filename);
  1419. fflush(stdout);
  1420. #endif
  1421. *synclist = FALSE;
  1422. return(FINISHED);
  1423. } else if(starttime > endtime) {
  1424. #ifndef IS_CDPARSE
  1425. fprintf(stdout,"WARNING: If %s is a synclist, starttime > endtime in line %d\n",filename,n+1);
  1426. fflush(stdout);
  1427. #endif
  1428. *synclist = FALSE;
  1429. return(FINISHED);
  1430. }
  1431. }
  1432. total_words += wordcnt[n];
  1433. }
  1434. #ifdef NO_DUPLICATE_SNDFILES
  1435. #ifdef CDPARSE
  1436. if(duplicated_filenames(inputfile->linecnt,wordstor,wordcnt))
  1437. #else
  1438. if(duplicated_filenames(inputfile->linecnt,wordstor,wordcnt,filename))
  1439. #endif
  1440. return(FALSE);
  1441. #endif
  1442. inputfile->srate = srate;
  1443. return(FINISHED);
  1444. }
  1445. /***************************** IS_A_VALID_SNDFILE_FOR_SYNC **************************/
  1446. int is_a_valid_sndfile_for_sync(char *filename,char *sndfilename,int *srate,int linecnt,infileptr inputfile)
  1447. {
  1448. int exit_status;
  1449. //TW AGREED REMOVAL minsize
  1450. int ifd;
  1451. double maxamp, maxloc;
  1452. int maxrep;
  1453. #ifdef IS_CDPARSE
  1454. int getmaxinfo = 1;
  1455. #else
  1456. int getmaxinfo = 0;
  1457. #endif
  1458. if((ifd = sndopenEx(sndfilename,0,CDP_OPEN_RDONLY)) < 0)
  1459. return(FALSE);
  1460. if((exit_status = readhead(inputfile,ifd,sndfilename,&maxamp,&maxloc,&maxrep,0,getmaxinfo))<0) { /* READ THE FILE HEADER */
  1461. if(sndcloseEx(ifd)<0) {
  1462. sprintf(errstr,"Failed to close sndfile %s while checking file %s\n",sndfilename,filename);
  1463. exit_status = SYSTEM_ERROR;
  1464. }
  1465. return(exit_status);
  1466. }
  1467. exit_status = TRUE;
  1468. if(inputfile->filetype!=SNDFILE) {
  1469. #ifndef IS_CDPARSE
  1470. fprintf(stdout,"WARNING: If %s is a syncfile: %s is not a sndfile.\n",filename,sndfilename);
  1471. fflush(stdout);
  1472. #endif
  1473. exit_status=FALSE;
  1474. } else if((inputfile->insams = sndsizeEx(ifd))<0) { /* FIND SIZE OF FILE */
  1475. sprintf(errstr, "Can't read size of input file %s: while checking file %s\n",sndfilename,filename);
  1476. exit_status = SYSTEM_ERROR; /*RWD 4.2001 was PROGRAM_ERROR */
  1477. } else if(inputfile->insams == 0L) /*RWD 4.2001 was <= */
  1478. exit_status=FALSE;
  1479. else if (linecnt==0)
  1480. *srate = inputfile->srate;
  1481. else if(inputfile->srate != *srate) {
  1482. #ifndef IS_CDPARSE
  1483. fprintf(stdout,"WARNING: If %s is a syncfile: Incompatible sample-rate in file %s\n",filename,sndfilename);
  1484. fflush(stdout);
  1485. #endif
  1486. exit_status=FALSE;
  1487. }
  1488. // inputfile->infilesize = 0;
  1489. inputfile->insams = 0;
  1490. inputfile->filetype = 0;
  1491. inputfile->srate = 0;
  1492. inputfile->channels = 0;
  1493. inputfile->stype = 0;
  1494. if(sndcloseEx(ifd)<0) {
  1495. sprintf(errstr,"Failed to close sndfile %s while testing file %s\n",sndfilename,filename);
  1496. return(SYSTEM_ERROR);
  1497. }
  1498. return(exit_status);
  1499. }
  1500. /***************************** SET_SNDLIST_FLAGS **************************/
  1501. int set_sndlist_flags(infileptr inputfile,char **wordstor)
  1502. {
  1503. int exit_status;
  1504. int srate = 0, wordcnt, m;
  1505. char *filename;
  1506. int ifd;
  1507. double maxamp, maxloc;
  1508. int maxrep;
  1509. #ifdef IS_CDPARSE
  1510. int getmaxinfo = 1;
  1511. #else
  1512. int getmaxinfo = 0;
  1513. #endif
  1514. for(wordcnt=0;wordcnt<inputfile->all_words;wordcnt++) {
  1515. filename = wordstor[wordcnt];
  1516. if(inputfile->different_srates == FALSE) {
  1517. if((ifd = sndopenEx(filename,0,CDP_OPEN_RDONLY)) < 0) {
  1518. #ifdef _DEBUG
  1519. sprintf(errstr,"Anomaly reopening sndfile %s: set_sndfilelist_flags()\n",filename);
  1520. #else
  1521. sprintf(errstr,"Anomaly reopening sndfile %s: %s\n",filename,rsferrstr);
  1522. #endif
  1523. return(SYSTEM_ERROR);
  1524. }
  1525. if((exit_status = readhead(inputfile,ifd,filename,&maxamp,&maxloc,&maxrep,0,getmaxinfo))<0) { /* READ THE FILE HEADER */
  1526. sprintf(errstr,"Anomaly Rereading header of sndfile %s: set_sndfilelist_flags()\n",filename);
  1527. if(sndcloseEx(ifd) < 0)
  1528. sprintf(errstr, "Can't close soundfile %s: set_sndlist_flags()\n",filename);
  1529. return(SYSTEM_ERROR);
  1530. }
  1531. if(wordcnt==0)
  1532. srate = inputfile->srate;
  1533. else if(inputfile->srate != srate) {
  1534. inputfile->different_srates = TRUE;
  1535. inputfile->srate = 0;
  1536. }
  1537. // inputfile->infilesize = 0;
  1538. inputfile->insams = 0;
  1539. inputfile->filetype = 0;
  1540. inputfile->channels = 0;
  1541. inputfile->stype = 0;
  1542. if(sndcloseEx(ifd) < 0) {
  1543. sprintf(errstr, "Can't close soundfile %s: set_sndlist_flags()\n",filename);
  1544. return(SYSTEM_ERROR);
  1545. }
  1546. }
  1547. }
  1548. for(wordcnt=0;wordcnt<inputfile->all_words;wordcnt++) {
  1549. for(m=wordcnt+1;m<inputfile->all_words;m++) {
  1550. if(!strcmp(wordstor[wordcnt],wordstor[m])) {
  1551. inputfile->duplicate_snds = TRUE;
  1552. break;
  1553. }
  1554. }
  1555. if(inputfile->duplicate_snds)
  1556. break;
  1557. }
  1558. return(FINISHED);
  1559. }
  1560. /****************************************** ASSIGN_BRKFILE_TYPE ****************************************/
  1561. int assign_brkfile_type(infileptr inputfile,int linelist)
  1562. {
  1563. int normalised_brkfile = FALSE;
  1564. int dB_brkfile = FALSE;
  1565. int pitch_brkfile = FALSE;
  1566. int transpos_brkfile = FALSE;
  1567. int positive_brkfile = FALSE;
  1568. if(inputfile->minbrk >= 0.0)
  1569. positive_brkfile = TRUE;
  1570. if(inputfile->maxbrk <= 1.0 && inputfile->minbrk >= 0.0)
  1571. normalised_brkfile = TRUE;
  1572. else if (inputfile->maxbrk <= DEFAULT_NYQUIST && inputfile->minbrk >= -1.0) {
  1573. inputfile->could_be_pitch = TRUE;
  1574. pitch_brkfile = TRUE;
  1575. }
  1576. if(inputfile->maxbrk <= 0.0 && inputfile->minbrk >= MIN_DB_ON_16_BIT)
  1577. dB_brkfile = TRUE;
  1578. if(inputfile->maxbrk <= MAX_TRANSPOS && inputfile->minbrk >= MIN_TRANSPOS) {
  1579. inputfile->could_be_transpos = TRUE;
  1580. transpos_brkfile = TRUE;
  1581. }
  1582. if(transpos_brkfile) {
  1583. if(normalised_brkfile) {
  1584. if(linelist==POSSIBLE)
  1585. return TRANSPOS_OR_NORMD_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1586. else
  1587. return TRANSPOS_OR_NORMD_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1588. } else if(pitch_brkfile) {
  1589. if(linelist==POSSIBLE)
  1590. return TRANSPOS_OR_PITCH_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1591. else
  1592. return TRANSPOS_OR_PITCH_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1593. } else {
  1594. if(linelist==POSSIBLE)
  1595. return TRANSPOS_OR_UNRANGED_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1596. else
  1597. return TRANSPOS_OR_UNRANGED_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1598. }
  1599. } else {
  1600. if(normalised_brkfile) {
  1601. if(linelist==POSSIBLE)
  1602. return NORMD_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1603. else
  1604. return NORMD_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1605. } else if(dB_brkfile) {
  1606. if(linelist==POSSIBLE)
  1607. return DB_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1608. else
  1609. return DB_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1610. } else if(pitch_brkfile) {
  1611. if(linelist==POSSIBLE) {
  1612. if(positive_brkfile)
  1613. return PITCH_POSITIVE_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1614. else
  1615. return PITCH_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1616. } else {
  1617. if(positive_brkfile)
  1618. return PITCH_POSITIVE_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1619. else
  1620. return PITCH_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1621. }
  1622. } else if(positive_brkfile) {
  1623. if(linelist==POSSIBLE)
  1624. return POSITIVE_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1625. else
  1626. return POSITIVE_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1627. } else {
  1628. if(linelist==POSSIBLE)
  1629. return UNRANGED_BRKFILE_OR_NUMLIST_OR_LINELIST_OR_WORDLIST;
  1630. else
  1631. return UNRANGED_BRKFILE_OR_NUMLIST_OR_WORDLIST;
  1632. }
  1633. }
  1634. }
  1635. #define MIN_BRKSLOPE (1.0)
  1636. #define MAX_BRKSLOPE (6.0)
  1637. /************************************ CHECK_FOR_AN_EXTENDED_BRKFILE *********************************/
  1638. int check_for_an_extended_brkfile
  1639. (int *extended_brkfile,char **wordstor,int *wordcnt,int linecnt,int total_words,infileptr inputfile)
  1640. {
  1641. int wordno = 0, lineno;
  1642. int is_extended_format = FALSE;
  1643. double lasttime = -1.0, time, val;
  1644. double maxval = -(double)(MAXRANGE+1);
  1645. double minval = (double)(MAXRANGE+1);
  1646. if(wordcnt[0]!=2) {
  1647. *extended_brkfile = FALSE;
  1648. return(FINISHED);
  1649. }
  1650. if(!extract_value_pair(&wordno,&time,&val,&maxval,&minval,&lasttime,wordstor)) {
  1651. *extended_brkfile = FALSE;
  1652. return(FINISHED);
  1653. }
  1654. if(time!=0.0) {
  1655. *extended_brkfile = FALSE;
  1656. return(FINISHED);
  1657. }
  1658. for(lineno=1;lineno<linecnt;lineno++) {
  1659. if(wordno+2 > total_words) {
  1660. *extended_brkfile = FALSE;
  1661. return(FINISHED);
  1662. }
  1663. if(wordcnt[lineno]<2 || wordcnt[lineno]>4) {
  1664. *extended_brkfile = FALSE;
  1665. return(FINISHED);
  1666. }
  1667. if(!extract_value_pair(&wordno,&time,&val,&maxval,&minval,&lasttime,wordstor)) {
  1668. *extended_brkfile = FALSE;
  1669. return(FINISHED);
  1670. }
  1671. if(wordcnt[lineno]>2) {
  1672. if(wordno >=total_words) {
  1673. sprintf(errstr,"Failure in word-counting logic 2: check_for_an_extended_brkfile()\n");
  1674. return(PROGRAM_ERROR);
  1675. }
  1676. utol(wordstor[wordno]);
  1677. if(strcmp(wordstor[wordno],"lin")
  1678. && strcmp(wordstor[wordno],"exp")
  1679. && strcmp(wordstor[wordno],"log")) {
  1680. *extended_brkfile = FALSE;
  1681. return(FINISHED);
  1682. }
  1683. wordno++;
  1684. is_extended_format = TRUE;
  1685. }
  1686. if(wordcnt[lineno]>3) {
  1687. if(wordno >=total_words) {
  1688. sprintf(errstr,"Failure in word-counting logic 3: check_for_an_extended_brkfile()\n");
  1689. return(PROGRAM_ERROR);
  1690. }
  1691. if(sscanf(wordstor[wordno],"%lf",&val)!=1
  1692. || val < MIN_BRKSLOPE || val > MAX_BRKSLOPE) {
  1693. *extended_brkfile = FALSE;
  1694. return(FINISHED);
  1695. }
  1696. wordno++;
  1697. }
  1698. }
  1699. if(is_extended_format) {
  1700. inputfile->maxbrk = maxval;
  1701. inputfile->minbrk = minval;
  1702. } else
  1703. *extended_brkfile = FALSE;
  1704. return(FINISHED);
  1705. }
  1706. /****************************************** EXTRACT_VALUE_PAIR ****************************************/
  1707. int extract_value_pair(int *n,double *time, double *val,double *maxval,double *minval, double *lasttime, char **wordstor)
  1708. {
  1709. if(sscanf(wordstor[*n],"%lf",time)!=1 || sscanf(wordstor[(*n)+1],"%lf",val)!=1)
  1710. return(FALSE);
  1711. //TW
  1712. // if(*time <= *lasttime || *val > (double)MAXSAMP || *val < -(double)MAXSAMP)
  1713. if(*time <= *lasttime || *val > (double)MAXRANGE || *val < -(double)MAXRANGE)
  1714. return(FALSE);
  1715. *lasttime = *time;
  1716. *n += 2;
  1717. *maxval = max(*maxval,*val);
  1718. *minval = min(*minval,*val);
  1719. return(TRUE);
  1720. }
  1721. /****************************************** UTOL ****************************************/
  1722. #define UPPER_TO_LOWER_CASE (32)
  1723. void utol(char *str)
  1724. {
  1725. int n, k = strlen(str);
  1726. for(n=0;n<k;n++) {
  1727. if(str[n] >= 'A' && str[n] <= 'Z')
  1728. str[n] += UPPER_TO_LOWER_CASE;
  1729. }
  1730. }
  1731. /* NEW CODE : June 1st : 2000 */
  1732. /****************************************** MAXSAMP ****************************************/
  1733. //TW Rewritten: floating point data only is retained:
  1734. //This version cannot write maxsamp info to header
  1735. void maxsamp(int ifd,double *maxamp,double *maxloc,int *maxrep)
  1736. {
  1737. int got, totalsamps = 0, samps, i, maxxloc;
  1738. double maxdamp = 0.0;
  1739. float *bigbuf, *fbuf;
  1740. int repeats = 0; /* counts how many times the max repeats */
  1741. int bufsize;
  1742. *maxrep = -1; /* flags that maxamp and maxloc are not found */
  1743. *maxamp = 0.0;
  1744. *maxloc = 0.0;
  1745. if((bufsize = (int)(long) Malloc(-1)) < sizeof(float))
  1746. return;
  1747. /*bufsize = (bufsize/SECSIZE)*SECSIZE; */ /* round to sector boundary */
  1748. bufsize /= sizeof(float);
  1749. if((bigbuf = (float *)malloc(bufsize * sizeof(float)))==NULL)
  1750. return;
  1751. fbuf = (float *)bigbuf;
  1752. while((got = fgetfbufEx(bigbuf,bufsize,ifd,0)) > 0 ) {
  1753. samps = got;
  1754. for( i=0 ; i<samps ; i++ ) {
  1755. if(smpflteq(fbuf[i],maxdamp) || smpflteq(-fbuf[i],maxdamp))
  1756. repeats++;
  1757. else if(fbuf[i] >= 0.0) {
  1758. if(fbuf[i] > maxdamp) {
  1759. maxdamp = fbuf[i];
  1760. *maxloc = (double)(totalsamps + i);
  1761. repeats = 1;
  1762. }
  1763. } else {
  1764. if (-fbuf[i] > maxdamp) {
  1765. maxdamp = -fbuf[i];
  1766. *maxloc = (double)(totalsamps + i);
  1767. repeats = 1;
  1768. }
  1769. }
  1770. }
  1771. totalsamps += got;
  1772. }
  1773. if( got < 0 ) {
  1774. free(bigbuf);
  1775. return;
  1776. }
  1777. if(totalsamps > 0) {
  1778. if(repeats <= 0) {
  1779. Mfree(bigbuf);
  1780. return;
  1781. }
  1782. *maxamp = maxdamp; /* return info to SoundLoom */
  1783. maxxloc = (int)round(*maxloc);
  1784. *maxrep = repeats;
  1785. }
  1786. Mfree(bigbuf);
  1787. }
  1788. /************************************** DO_EXIT ************************************/
  1789. int do_exit(int exit_status) {
  1790. #ifdef IS_CDPARSE
  1791. if(exit_status == DATA_ERROR)
  1792. fprintf(stdout,"WARNING: %s",errstr); /* Sound Loom responds to message headers */
  1793. else /* and differently to different message headers */
  1794. fprintf(stdout,"ERROR: %s",errstr); /* but ignores the return value */
  1795. fflush(stdout);
  1796. #endif
  1797. return(exit_status); /* cmdline responds to value of 'exit_status' */
  1798. }
  1799. /***************************** PARSE_A_TEXTFILE *****************************/
  1800. int parse_a_textfile(char *filename,infileptr inputfile)
  1801. {
  1802. int exit_status;
  1803. FILE *fp;
  1804. if((fp = fopen(filename,"r"))==NULL) {
  1805. sprintf(errstr, "Can't open file %s to read data.\n",filename);
  1806. exit_status = DATA_ERROR;
  1807. return do_exit(exit_status);
  1808. }
  1809. if(file_has_reserved_extension(filename)) {
  1810. sprintf(errstr, "File %s either has unredable header, or is a textfile with a CDP reserved extension.\n",filename);
  1811. //TW ADDED: ESSENTIAL TO CLOSE FILE or NOTIFY SOUND LOOM OF FAILURE TO CLOSE: for Sound Loom INTEGRITY
  1812. if(fclose(fp)<0) {
  1813. sprintf(errstr, "Failed to close file %s\n",filename);
  1814. exit_status = SYSTEM_ERROR;
  1815. return do_exit(exit_status);
  1816. }
  1817. exit_status = DATA_ERROR;
  1818. return do_exit(exit_status);
  1819. }
  1820. if((exit_status = megaparsec(filename,fp,inputfile))<0) {
  1821. if(fclose(fp)<0) {
  1822. sprintf(errstr, "Failed to close file %s\n",filename);
  1823. exit_status = SYSTEM_ERROR;
  1824. }
  1825. return do_exit(exit_status);
  1826. }
  1827. if(fclose(fp)<0) {
  1828. sprintf(errstr, "Failed to close file %s\n",filename);
  1829. exit_status = SYSTEM_ERROR;
  1830. return do_exit(exit_status);
  1831. }
  1832. return FINISHED;
  1833. }