distortf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <modeno.h>
  28. #include <arrays.h>
  29. #include <distort.h>
  30. #include <cdpmain.h>
  31. #include <sfsys.h>
  32. #include <osbind.h>
  33. #define FCYCBUF (2)
  34. static void do_fractal(int grouplen,int current_buf,dataptr dz);
  35. static void do_fractal_crosbuf(int grouplen,int bufcros_cycle,int current_buf,dataptr dz);
  36. static int get_scaled_halfcycle(int n,int *cnt,float **buf,int *bufpos,int *grouplen,int *current_buf,
  37. int *is_bufcros,int *bufcros_cycle,int prescale_param,int *last_total_samps,dataptr dz);
  38. static int change_buf_marking_crossing_cycle_no(int n,float **buf,int *is_bufcross,int *bufcros_cycle,int *current_buf,dataptr dz);
  39. /**************************** DISTORTF ******************************
  40. *
  41. * Distort file by adding smaller scale perturbations modelled on large.
  42. */
  43. int distortf(int *bufpos,int phase,int *last_total_samps,int *current_buf,dataptr dz)
  44. {
  45. int exit_status;
  46. int grouplen = 0;
  47. int cnt = 0, n;
  48. int firstime = 1;
  49. float *buf = dz->sampbuf[*current_buf];
  50. int bufcros_cycle = 0;
  51. int is_bufcros = 0;
  52. double thistime = (double)(dz->total_samps_written + *bufpos)/(double)(dz->infile->srate);
  53. if((exit_status = read_values_from_all_existing_brktables(thistime,dz))<0)
  54. return(exit_status);
  55. switch(phase) {
  56. case(1):
  57. for(n=0;n<dz->iparam[DISTORTF_SCALE];n++) {
  58. dz->lparray[DISTORTF_STARTCYC][n] = *bufpos;
  59. while(buf[*bufpos]>=0.0) {
  60. if((exit_status = get_scaled_halfcycle(n,&cnt,&buf,bufpos,&grouplen,current_buf,&is_bufcros,&bufcros_cycle,DISTORTF_PRESCALE,last_total_samps,dz))!=CONTINUE)
  61. return(exit_status);
  62. }
  63. while(buf[*bufpos]<=0.0) {
  64. if((exit_status = get_scaled_halfcycle(n,&cnt,&buf,bufpos,&grouplen,current_buf,&is_bufcros,&bufcros_cycle,DISTORTF_PRESCALE,last_total_samps,dz))!=CONTINUE)
  65. return(exit_status);
  66. }
  67. if(is_bufcros && firstime) {
  68. dz->lparray[DISTORTF_CYCLEN][n] = dz->buflen - dz->lparray[DISTORTF_STARTCYC][n] + *bufpos;
  69. firstime = 0;
  70. } else
  71. dz->lparray[DISTORTF_CYCLEN][n] = *bufpos - dz->lparray[DISTORTF_STARTCYC][n];
  72. }
  73. break;
  74. case(-1):
  75. for(n=0;n<dz->iparam[DISTORTF_SCALE];n++) {
  76. dz->lparray[DISTORTF_STARTCYC][n] = *bufpos;
  77. while(buf[*bufpos]<=0.0) {
  78. if((exit_status = get_scaled_halfcycle(n,&cnt,&buf,bufpos,&grouplen,current_buf,&is_bufcros,&bufcros_cycle,DISTORTF_PRESCALE,last_total_samps,dz))!=CONTINUE)
  79. return(exit_status);
  80. }
  81. while(buf[*bufpos]>=0.0) {
  82. if((exit_status = get_scaled_halfcycle(n,&cnt,&buf,bufpos,&grouplen,current_buf,&is_bufcros,&bufcros_cycle,DISTORTF_PRESCALE,last_total_samps,dz))!=CONTINUE)
  83. return(exit_status);
  84. }
  85. if(is_bufcros && firstime) {
  86. dz->lparray[DISTORTF_CYCLEN][n] = dz->buflen - dz->lparray[DISTORTF_STARTCYC][n] + *bufpos;
  87. firstime = 0;
  88. } else
  89. dz->lparray[DISTORTF_CYCLEN][n] = *bufpos - dz->lparray[DISTORTF_STARTCYC][n];
  90. }
  91. break;
  92. }
  93. dz->lparray[DISTORTF_STARTCYC][dz->iparam[DISTORTF_SCALE]] = *bufpos;
  94. if(is_bufcros) {
  95. do_fractal_crosbuf(grouplen,bufcros_cycle,*current_buf,dz);
  96. if((exit_status = write_samps(dz->sampbuf[!(*current_buf)],dz->buflen,dz))<0)
  97. return(exit_status);
  98. is_bufcros = 0;
  99. } else
  100. do_fractal(grouplen,*current_buf,dz);
  101. return(CONTINUE);
  102. }
  103. /***************************** DO_FRACTAL *******************************/
  104. void do_fractal(int grouplen,int current_buf,dataptr dz)
  105. {
  106. int n, k, i, index;
  107. double ratio;
  108. float *buf = dz->sampbuf[current_buf];
  109. for(n=0;n<dz->iparam[DISTORTF_SCALE];n++) {
  110. ratio = (double)grouplen/(double)dz->lparray[DISTORTF_CYCLEN][n];
  111. k = 0;
  112. i = dz->lparray[DISTORTF_STARTCYC][n];
  113. while(i < dz->lparray[DISTORTF_STARTCYC][n+1]) {
  114. index = round(ratio * (double)k++);
  115. buf[i++] += (float)(dz->param[DISTORTF_AMPFACT] * dz->sampbuf[FCYCBUF][index]);
  116. }
  117. }
  118. }
  119. /*************************** DO_FRACTAL_CROSBUF ***************************/
  120. void do_fractal_crosbuf(int grouplen,int bufcros_cycle,int current_buf,dataptr dz)
  121. {
  122. float *buf = dz->sampbuf[!current_buf];
  123. int n, k, i, index;
  124. double ratio;
  125. for(n=0;n<bufcros_cycle;n++) {
  126. ratio = (double)grouplen/(double)dz->lparray[DISTORTF_CYCLEN][n];
  127. k = 0;
  128. i = dz->lparray[DISTORTF_STARTCYC][n];
  129. while(i < dz->lparray[DISTORTF_STARTCYC][n+1]) {
  130. index = round(ratio * (double)k++);
  131. buf[i++] += (float)(dz->param[DISTORTF_AMPFACT] * dz->sampbuf[FCYCBUF][index]);
  132. }
  133. }
  134. ratio = (double)grouplen/(double)dz->lparray[DISTORTF_CYCLEN][n];
  135. k = 0;
  136. i = dz->lparray[DISTORTF_STARTCYC][n];
  137. while(i < dz->buflen) {
  138. index = round(ratio * (double)k++);
  139. buf[i++] += (float)(dz->param[DISTORTF_AMPFACT] * dz->sampbuf[FCYCBUF][index]);
  140. }
  141. buf = dz->sampbuf[current_buf];
  142. i = 0;
  143. while(i < dz->lparray[DISTORTF_STARTCYC][n+1]) {
  144. index = round(ratio * (double)k++);
  145. buf[i++] += (float)(dz->param[DISTORTF_AMPFACT] * dz->sampbuf[FCYCBUF][index]);
  146. }
  147. for(n=bufcros_cycle+1;n<dz->iparam[DISTORTF_SCALE];n++) {
  148. ratio = (double)grouplen/(double)dz->lparray[DISTORTF_CYCLEN][n];
  149. k = 0;
  150. i = dz->lparray[DISTORTF_STARTCYC][n];
  151. while(i < dz->lparray[DISTORTF_STARTCYC][n+1]) {
  152. index = round(ratio * (double)k++);
  153. buf[i++] += (float)(dz->param[DISTORTF_AMPFACT] * dz->sampbuf[FCYCBUF][index]);
  154. }
  155. }
  156. }
  157. /**************************** GET_SCALED_HALFCYCLE **************************/
  158. int get_scaled_halfcycle(int n,int *cnt,float **buf,int *bufpos,int *grouplen,int *current_buf,int *is_bufcros,int *bufcros_cycle,
  159. int prescale_param,int *last_total_samps,dataptr dz)
  160. {
  161. int exit_status;
  162. if(++(*cnt) >= dz->iparam[DISTORTF_SCALE]) {
  163. dz->sampbuf[FCYCBUF][*grouplen] = (*buf)[*bufpos];
  164. if(++(*grouplen) >dz->iparam[DISTORTF_CYCBUFLEN]) {
  165. cop_out(*bufpos,*grouplen,*last_total_samps,dz);
  166. return FINISHED;
  167. }
  168. *cnt = 0;
  169. }
  170. if(++(*bufpos) >= dz->ssampsread) {
  171. *last_total_samps = dz->total_samps_read;
  172. if((exit_status = change_buf_marking_crossing_cycle_no(n,buf,is_bufcros,bufcros_cycle,current_buf,dz))!=CONTINUE)
  173. return(exit_status);
  174. if(dz->vflag[IS_PRESCALED])
  175. prescale(*current_buf,prescale_param,dz);
  176. *bufpos = 0;
  177. }
  178. return(CONTINUE);
  179. }
  180. /****************************** CHANGE_BUF_MARKING_CROSSING_CYCLE_NO ****************************
  181. *
  182. * (1) 'bufcros_cycle' marks the cycle-no which crosses between buffers.
  183. */
  184. int change_buf_marking_crossing_cycle_no(int n,float **buf,int *is_bufcros,int *bufcros_cycle,int *current_buf,dataptr dz)
  185. {
  186. int exit_status;
  187. if(dz->samps_left <= 0)
  188. return(FINISHED);
  189. if(*is_bufcros==TRUE) {
  190. sprintf(errstr,"cycle_search exceeds buffer size.\n");
  191. return(GOAL_FAILED);
  192. }
  193. *current_buf = !(*current_buf);
  194. *buf = dz->sampbuf[*current_buf];
  195. *bufcros_cycle = n;
  196. *is_bufcros = 1;
  197. if((exit_status = read_samps(*buf,dz))<0)
  198. return(exit_status);
  199. return(CONTINUE);
  200. }