dzsetup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /* floatsam version */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <structures.h>
  25. #include <tkglobals.h>
  26. #include <globcon.h>
  27. #include <cdpmain.h>
  28. #include <formants.h>
  29. #include <speccon.h>
  30. #include <modicon.h>
  31. #include <txtucon.h>
  32. #include <memory.h> /*RWD*/
  33. #include <string.h>
  34. #include <sfsys.h>
  35. static int establish_infile_and_outfile_property_structs(dataptr dz);
  36. static void pointer_initialisation(dataptr dz);
  37. static void initialise_file_property_struct(fileptr fp);
  38. static void free_tex_structure(dataptr dz);
  39. static void delete_motifs_here_and_beyond(motifptr startmtf);
  40. static void free_application_structure_and_related_brktables(dataptr dz);
  41. static void nullify_duplicate_wordptrs(dataptr dz);
  42. /**************************** ESTABLISH_DATASTRUCTURE ************************/
  43. int establish_datastructure(dataptr *dz)
  44. {
  45. int exit_status;
  46. if((*dz = (dataptr)malloc(sizeof(struct datalist)))==NULL) {
  47. sprintf(errstr,"INSUFFICIENT MEMORY for main data structure\n");
  48. return(MEMORY_ERROR);
  49. }
  50. pointer_initialisation(*dz);
  51. if((exit_status = establish_infile_and_outfile_property_structs(*dz))<0)
  52. return(exit_status);
  53. return(FINISHED);
  54. }
  55. /******************** ESTABLISH_INFILE_AND_OUTFILE_PROPERTY_STRUCTS *************************/
  56. int establish_infile_and_outfile_property_structs(dataptr dz)
  57. {
  58. if((dz->infile = (fileptr)malloc(sizeof(struct fileprops)))==NULL) {
  59. sprintf(errstr,"INSUFICIENT MEMORY for infile_property_struct\n");
  60. return(MEMORY_ERROR);
  61. }
  62. if((dz->otherfile = (fileptr)malloc(sizeof(struct fileprops)))==NULL) {
  63. sprintf(errstr,"INSUFICIENT MEMORY for otherfile_property_struct\n");
  64. return(MEMORY_ERROR);
  65. }
  66. if((dz->outfile = (fileptr)malloc(sizeof(struct fileprops)))==NULL) {
  67. sprintf(errstr,"INSUFICIENT MEMORY for outfile_property_struct\n");
  68. return(MEMORY_ERROR);
  69. }
  70. initialise_file_property_struct(dz->infile);
  71. initialise_file_property_struct(dz->otherfile);
  72. initialise_file_property_struct(dz->outfile);
  73. /*RWD.7.98 */
  74. if((dz->outfilename = (char *) malloc(_MAX_PATH)) ==NULL){
  75. sprintf(errstr,"INSUFFICIENT MEMORY for outfile name\n");
  76. return MEMORY_ERROR;
  77. }
  78. return(FINISHED);
  79. }
  80. /******************************** POINTER_INITIALISATION *******************************/
  81. void pointer_initialisation(dataptr dz)
  82. {
  83. //RWD.10.98
  84. char *clipstring = NULL;
  85. memset((char *)dz,0,sizeof(struct datalist)); /* THIS INITIALISES EVERYTHING ELSE TO ZERO!!! */
  86. dz->process = -1;
  87. dz->maxmode = -1;
  88. dz->mode = -1;
  89. /* PROCESS TYPES */
  90. dz->input_data_type = -2;
  91. dz->outfiletype = -2;
  92. dz->process_type = -2;
  93. /* OTHER FILE PONTERS */
  94. dz->ofd = -1;
  95. dz->other_file = -1;
  96. dz->extra_word = -1;
  97. dz->extrabrkno = -1;
  98. dz->deal_with_chan_data = RECTIFY_CHANNEL_FRQ_ORDER;
  99. dz->is_sharp = EIGHTH_TONE; /* default */
  100. dz->is_flat = 1.0/EIGHTH_TONE; /* default */
  101. /*RWD.7.98 */
  102. dz->outfilename = NULL;
  103. //RWD.10.98
  104. dz->floatsam_output = 0;
  105. dz->peak_fval = 1.0; //only report if overrange
  106. dz->true_outfile_stype = SAMP_SHORT;
  107. dz->clip_floatsams = 1;
  108. clipstring = getenv("CDP_NOCLIP_FLOATS");
  109. if(clipstring != NULL)
  110. if((strcmp(clipstring,"") != 0) && (atoi(clipstring) != 0))
  111. dz->clip_floatsams = 0;
  112. dz->outpeaks = dz->otherpeaks = NULL;
  113. dz->outpeakpos = dz->otherpeakpos = NULL;
  114. dz->outchans = dz->otheroutchans = 0;
  115. dz->needpeaks = dz->needotherpeaks = 0;
  116. }
  117. /******************************** INITIALISE_FILE_PROPERTY_STRUCT *************************/
  118. void initialise_file_property_struct(fileptr fp)
  119. {
  120. fp->filetype = -1;
  121. fp->srate = 0;
  122. fp->stype = -1;
  123. fp->origstype = -1;
  124. fp->origrate = 0;
  125. fp->channels = 0;
  126. fp->origchans = 0;
  127. fp->specenvcnt = 0;
  128. fp->Mlen = 0;
  129. fp->Dfac = 0;
  130. fp->arate = 0.0f;
  131. fp->window_size = 0.0f;
  132. }
  133. /******************************* SUPERFREE ******************************/
  134. int superfree(dataptr dz)
  135. {
  136. int n;
  137. if(dz!=NULL) {
  138. if(dz->tex != NULL)
  139. free_tex_structure(dz);
  140. if(dz->infile!=NULL)
  141. free(dz->infile);
  142. if(dz->outfile!=NULL)
  143. free(dz->outfile);
  144. if(dz->fp!=NULL)
  145. fclose(dz->fp);
  146. if(dz->is_int!=NULL)
  147. free(dz->is_int);
  148. if(dz->no_brk!=NULL)
  149. free(dz->no_brk);
  150. if(dz->is_active!=NULL)
  151. free(dz->is_active);
  152. if(dz->bigbuf!=NULL)
  153. free(dz->bigbuf);
  154. if(dz->bigfbuf!=NULL)
  155. free(dz->bigfbuf);
  156. if(dz->amp!=NULL)
  157. free(dz->amp);
  158. if(dz->freq!=NULL)
  159. free(dz->freq);
  160. if(dz->sbufptr!=NULL)
  161. free(dz->sbufptr);
  162. if(dz->sampbuf!=NULL)
  163. free(dz->sampbuf);
  164. if(dz->flbufptr!=NULL)
  165. free(dz->flbufptr);
  166. if(dz->windowbuf!=NULL)
  167. free(dz->windowbuf);
  168. if(dz->ifd!=NULL) {
  169. free(dz->ifd);
  170. dz->ifd = NULL; /*RWD Nov 2011 jic */
  171. }
  172. if(dz->insams!=NULL)
  173. free(dz->insams);
  174. if(dz->application!=NULL)
  175. free_application_structure_and_related_brktables(dz);
  176. if((dz->extrabrkno >= 0) && (dz->brk !=NULL) && (dz->brk[dz->extrabrkno] !=NULL))
  177. free(dz->brk[dz->extrabrkno]);
  178. if(dz->brk!=NULL)
  179. free(dz->brk);
  180. if(dz->brkptr!=NULL)
  181. free(dz->brkptr);
  182. if(dz->brksize!=NULL)
  183. free(dz->brksize);
  184. if(dz->lastind!=NULL)
  185. free(dz->lastind);
  186. if(dz->lastval!=NULL)
  187. free(dz->lastval);
  188. if(dz->brkinit!=NULL)
  189. free(dz->brkinit);
  190. if(dz->firstval!=NULL)
  191. free(dz->firstval);
  192. if(dz->param!=NULL)
  193. free(dz->param);
  194. if(dz->iparam!=NULL)
  195. free(dz->iparam);
  196. if(dz->vflag!=NULL)
  197. free(dz->vflag);
  198. if((dz->array_cnt > 0) && (dz->parray!=NULL)) {
  199. for(n = 0;n< dz->array_cnt; n++) {
  200. if(dz->parray[n]!=NULL)
  201. free(dz->parray[n]);
  202. }
  203. }
  204. if((dz->iarray_cnt > 0) && (dz->iparray!=NULL)) {
  205. for(n = 0;n< dz->iarray_cnt; n++) {
  206. if(dz->iparray[n]!=NULL)
  207. free(dz->iparray[n]);
  208. }
  209. }
  210. if((dz->larray_cnt > 0) && (dz->lparray!=NULL)) {
  211. for(n = 0;n< dz->larray_cnt; n++) {
  212. if(dz->lparray[n]!=NULL)
  213. free(dz->lparray[n]);
  214. }
  215. }
  216. /* RWD 4:2002 lfarray shadows lparray for sumbix syncatt and distort*/
  217. if((dz->larray_cnt > 0) && (dz->lfarray!=NULL)) {
  218. for(n = 0;n< dz->larray_cnt; n++) {
  219. if(dz->lfarray[n]!=NULL)
  220. free(dz->lfarray[n]);
  221. }
  222. }
  223. if(dz->lfarray != NULL)
  224. free(dz->lfarray);
  225. if(dz->parray!=NULL)
  226. free(dz->parray);
  227. if(dz->iparray!=NULL)
  228. free(dz->iparray);
  229. if(dz->lparray!=NULL)
  230. free(dz->lparray);
  231. if(dz->sndbuf!=NULL)
  232. free(dz->sndbuf);
  233. if(dz->fptr!=NULL)
  234. free(dz->fptr);
  235. if(dz->ptr!=NULL)
  236. free(dz->ptr);
  237. if(dz->specenvfrq!=NULL)
  238. free(dz->specenvfrq);
  239. if(dz->specenvpch!=NULL)
  240. free(dz->specenvpch);
  241. if(dz->specenvamp!=NULL)
  242. free(dz->specenvamp);
  243. if(dz->specenvtop!=NULL)
  244. free(dz->specenvtop);
  245. if(dz->specenvamp2!=NULL)
  246. free(dz->specenvamp2);
  247. if(dz->pitches!=NULL)
  248. free(dz->pitches);
  249. if(dz->transpos!=NULL)
  250. free(dz->transpos);
  251. if(dz->pitches2!=NULL)
  252. free(dz->pitches2);
  253. if(dz->transpos2!=NULL)
  254. free(dz->transpos2);
  255. if(dz->frq_template!=NULL)
  256. free(dz->frq_template);
  257. if(dz->fsampbuf!=NULL)
  258. free(dz->fsampbuf);
  259. if(dz->filtpeak!=NULL)
  260. free(dz->filtpeak);
  261. if(dz->fbandtop!=NULL)
  262. free(dz->fbandtop);
  263. if(dz->fbandbot!=NULL) free(dz->fbandbot);
  264. if(dz->peakno!=NULL)
  265. free(dz->peakno);
  266. if(dz->lastpeakno!=NULL)
  267. free(dz->lastpeakno);
  268. if(dz->band!=NULL)
  269. free(dz->band);
  270. if(dz->temp!=NULL)
  271. free(dz->temp);
  272. if(dz->origenv!=NULL)
  273. free(dz->origenv);
  274. if(dz->env!=NULL)
  275. free(dz->env);
  276. if(dz->rampbrk!=NULL)
  277. free(dz->rampbrk);
  278. if(dz->valstor!=NULL) {
  279. for(n=0;n<dz->linecnt;n++) {
  280. if(dz->valstor[n]!=NULL)
  281. free(dz->valstor[n]);
  282. }
  283. free(dz->valstor);
  284. }
  285. if(dz->buflist!=NULL) {
  286. for(n=0;n<dz->bufcnt;n++) {
  287. if(dz->buflist[n]!=NULL)
  288. free(dz->buflist[n]);
  289. }
  290. free(dz->buflist);
  291. }
  292. if(dz->wordstor!=NULL)
  293. free_wordstors(dz);
  294. if(dz->wordcnt!=NULL)
  295. free(dz->wordcnt);
  296. if(dz->act!=NULL)
  297. free(dz->act);
  298. if(dz->activebuf!=NULL)
  299. free(dz->activebuf);
  300. if(dz->activebuf_ptr!=NULL)
  301. free(dz->activebuf_ptr);
  302. if(dz->outpeaks)
  303. free(dz->outpeaks);
  304. if(dz->otherpeaks)
  305. free(dz->otherpeaks);
  306. if(dz->outpeakpos)
  307. free(dz->outpeakpos);
  308. if(dz->otherpeakpos)
  309. free(dz->otherpeakpos);
  310. free(dz);
  311. }
  312. return(FINISHED);
  313. }
  314. /**************************** FREE_TEX_STRUCTURE *******************************/
  315. void free_tex_structure(dataptr dz)
  316. {
  317. int n;
  318. if(dz->tex->motifhead !=NULL)
  319. delete_motifs_here_and_beyond(dz->tex->motifhead);
  320. if(dz->tex->insnd !=NULL) {
  321. for(n=0;n<dz->infilecnt;n++) {
  322. if(((dz->tex->insnd)[n])->buffer != NULL)
  323. free(((dz->tex->insnd)[n])->buffer);
  324. free((dz->tex->insnd)[n]);
  325. }
  326. free(dz->tex->insnd);
  327. }
  328. if(dz->tex->phrase !=NULL)
  329. free(dz->tex->phrase);
  330. if(dz->tex->perm !=NULL) {
  331. for(n=0;n<PERMCNT;n++) {
  332. if(dz->tex->perm[n] != NULL)
  333. free(dz->tex->perm[n]);
  334. }
  335. free(dz->tex->perm);
  336. }
  337. free(dz->tex);
  338. }
  339. /********************** DELETE_MOTIFS_HERE_AND_BEYOND *******************************/
  340. void delete_motifs_here_and_beyond(motifptr startmtf)
  341. {
  342. motifptr thismotif = startmtf;
  343. if(thismotif==NULL)
  344. return;
  345. while(thismotif->next!=NULL)
  346. thismotif=thismotif->next;
  347. while(thismotif!=startmtf) {
  348. delete_notes_here_and_beyond(thismotif->firstnote);
  349. thismotif=thismotif->last;
  350. free(thismotif->next);
  351. }
  352. if(startmtf->last!=NULL)
  353. startmtf->last->next = NULL;
  354. free(startmtf);
  355. }
  356. /********************** DELETE_NOTES_HERE_AND_BEYOND *******************************/
  357. void delete_notes_here_and_beyond(noteptr startnote)
  358. {
  359. noteptr thisnote = startnote;
  360. if(thisnote==NULL)
  361. return;
  362. while(thisnote->next!=NULL)
  363. thisnote=thisnote->next;
  364. while(thisnote!=startnote) {
  365. thisnote=thisnote->last;
  366. free(thisnote->next);
  367. }
  368. if(startnote->last!=NULL)
  369. startnote->last->next = NULL;
  370. free(startnote);
  371. }
  372. /********************** FREE_APPLICATION_STRUCTURE_AND_RELATED_BRKTABLES *******************************/
  373. void free_application_structure_and_related_brktables(dataptr dz)
  374. {
  375. int n;
  376. aplptr ap = dz->application;
  377. if((ap->total_input_param_cnt > 0) && (dz->brk!=NULL)) {
  378. for(n = 0;n< ap->total_input_param_cnt; n++) {
  379. if(dz->brk[n]!=NULL)
  380. free(dz->brk[n]);
  381. }
  382. }
  383. if(ap->param_list!=NULL) free(ap->param_list);
  384. if(ap->option_list!=NULL) free(ap->option_list);
  385. if(ap->variant_list!=NULL) free(ap->variant_list);
  386. if(ap->internal_param_list!=NULL) free(ap->internal_param_list);
  387. if(ap->option_flags!=NULL) free(ap->option_flags);
  388. if(ap->variant_flags!=NULL) free(ap->variant_flags);
  389. if(ap->lo!=NULL) free(ap->lo);
  390. if(ap->hi!=NULL) free(ap->hi);
  391. free(dz->application);
  392. }
  393. /********************** FREE_WORDSTORS *******************************/
  394. void free_wordstors(dataptr dz)
  395. {
  396. int n;
  397. nullify_duplicate_wordptrs(dz);
  398. for(n=0;n<dz->all_words;n++) {
  399. if(dz->wordstor[n]!=NULL)
  400. free(dz->wordstor[n]);
  401. }
  402. free(dz->wordstor);
  403. }
  404. /***************************** NULLIFY_DUPLICATE_WORDPTRS **************************/
  405. void nullify_duplicate_wordptrs(dataptr dz)
  406. {
  407. int n, m;
  408. for(n=0;n<dz->all_words-1;n++) {
  409. if(dz->wordstor[n]!= NULL) {
  410. for(m=n+1;m<dz->all_words;m++) {
  411. if(dz->wordstor[m]!=NULL) {
  412. if(dz->wordstor[m]==dz->wordstor[n])
  413. dz->wordstor[m] = NULL;
  414. }
  415. }
  416. }
  417. }
  418. }