distorta.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. static int write_cycles(int *obufpos,int cyc_average,dataptr dz);
  34. static int distorta_func_crosbuf(int,int,int *,int,dataptr);
  35. static int distorta_func(int current_buf,int cyc_average,int *obufpos,dataptr dz);
  36. static int get_cyc_average(int *cyc_average,dataptr dz);
  37. /**************************** DISTORT_AVG ******************************
  38. *
  39. * This works on groups of N input COMPLETE CYCLES, where the output
  40. * group is not necessarily the same length as the input group.
  41. *
  42. * It requires a global current_pos_in_buf & dz->iparam[DISTORTA_OUTCNT].
  43. *
  44. * change_buff now uses cycleno_in_group_at_bufcros to MARK the cycle number at which
  45. * the buff-boundary is crossed.
  46. */
  47. int distort_avg(int *current_buf,int initial_phase,int *obufpos,int *current_pos_in_buf,int *cnt,dataptr dz)
  48. {
  49. int exit_status;
  50. register int i = *current_pos_in_buf;
  51. register int n;
  52. int cyc_average;
  53. float *b = dz->sampbuf[*current_buf];
  54. int cycleno_in_group_at_bufcros = -1;
  55. for(n=0;n<dz->iparam[DISTORTA_CYCLECNT];n++) {
  56. dz->lparray[DISTORTA_STARTCYC][n] = i;
  57. dz->lparray[DISTORTA_CYCLEN][n] = 0;
  58. switch(initial_phase) {
  59. case(1):
  60. while(b[i]>=0) {
  61. dz->lparray[DISTORTA_CYCLEN][n]++;
  62. if(++i >= dz->ssampsread) {
  63. if((exit_status = change_buff(&b,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  64. return(exit_status);
  65. cycleno_in_group_at_bufcros = n;
  66. i = 0;
  67. }
  68. }
  69. while(b[i]<=0) {
  70. dz->lparray[DISTORTA_CYCLEN][n]++;
  71. if(++i >= dz->ssampsread) {
  72. if((exit_status = change_buff(&b,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  73. return(exit_status);
  74. cycleno_in_group_at_bufcros = n;
  75. i = 0;
  76. }
  77. }
  78. break;
  79. case(-1):
  80. while(b[i]<=0) {
  81. dz->lparray[DISTORTA_CYCLEN][n]++;
  82. if(++i >= dz->ssampsread) {
  83. if((exit_status = change_buff(&b,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  84. return(exit_status);
  85. cycleno_in_group_at_bufcros = n;
  86. i = 0;
  87. }
  88. }
  89. while(b[i]>=0) {
  90. dz->lparray[DISTORTA_CYCLEN][n]++;
  91. if(++i >= dz->ssampsread) {
  92. if((exit_status = change_buff(&b,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  93. return(exit_status);
  94. cycleno_in_group_at_bufcros = n;
  95. i = 0;
  96. }
  97. }
  98. break;
  99. }
  100. }
  101. if((exit_status = get_cyc_average(&cyc_average,dz))<0)
  102. return(exit_status);
  103. if(cycleno_in_group_at_bufcros < 0) /* cycle set does not cross between buffers */
  104. exit_status = distorta_func(*current_buf,cyc_average,obufpos,dz);
  105. else
  106. exit_status = distorta_func_crosbuf(cyc_average,*current_buf,obufpos,cycleno_in_group_at_bufcros,dz);
  107. if(exit_status < 0)
  108. return(exit_status);
  109. *current_pos_in_buf = i;
  110. (*cnt)++;
  111. return(CONTINUE);
  112. }
  113. /****************************** CHANGE_BUFF *****************************/
  114. int change_buff(float **b,int *cycleno_in_group_at_bufcros,int *current_buf,dataptr dz)
  115. {
  116. int exit_status;
  117. if(dz->samps_left <= 0)
  118. return(FINISHED);
  119. if(*cycleno_in_group_at_bufcros >= 0) {
  120. sprintf(errstr,"cycle_search exceeds buffer size\n");
  121. return(GOAL_FAILED);
  122. }
  123. *current_buf = !(*current_buf);
  124. *b = dz->sampbuf[*current_buf];
  125. if((exit_status = read_samps(*b,dz))<0)
  126. return(exit_status);
  127. return(CONTINUE);
  128. }
  129. /*************************** GET_CYC_AVERAGE ***************************/
  130. int get_cyc_average(int *cyc_average,dataptr dz)
  131. {
  132. register int n;
  133. int k = 0;
  134. for(n=0;n<dz->iparam[DISTORTA_CYCLECNT];n++)
  135. k += dz->lparray[DISTORTA_CYCLEN][n];
  136. k = round((double)k/(double)dz->iparam[DISTORTA_CYCLECNT]);
  137. if(k > dz->iparam[DISTORTA_CYCBUFLEN]) {
  138. sprintf(errstr,"wavelength exceeds maximum size\n");
  139. return(GOAL_FAILED);
  140. }
  141. *cyc_average = k;
  142. return(FINISHED);
  143. }
  144. /************************* DISTORT_FUNC ***************************/
  145. int distorta_func(int current_buf,int cyc_average,int *obufpos,dataptr dz)
  146. {
  147. register int n, m;
  148. int cycindex;
  149. double sum, zorg;
  150. int j = 0;
  151. float *b = dz->sampbuf[current_buf];
  152. for(m=0;m<cyc_average;m++) {
  153. sum = 0.0;
  154. zorg = (double)m/(double)cyc_average;
  155. for(n=0;n<dz->iparam[DISTORTA_CYCLECNT];n++) {
  156. cycindex = round(zorg * (double)dz->lparray[DISTORTA_CYCLEN][n]);
  157. sum += b[dz->lparray[DISTORTA_STARTCYC][n] + cycindex];
  158. }
  159. /*RWD added cast */
  160. dz->sampbuf[3][j++] = (float) /*round*/(sum/(double)dz->iparam[DISTORTA_CYCLECNT]);
  161. }
  162. return write_cycles(obufpos,cyc_average,dz);
  163. }
  164. /*************************** DISTORT_FUNC_CROSBUF ***********************/
  165. int distorta_func_crosbuf(int cyc_average,int current_buf,int *obufpos,int cycleno_in_group_at_bufcros,dataptr dz)
  166. {
  167. register int n, m, k;
  168. int cycindex;
  169. double sum, zorg;
  170. int j = 0;
  171. float *b;
  172. for(m=0;m<cyc_average;m++) {
  173. sum = 0.0;
  174. b = dz->sampbuf[!current_buf]; /* CYCLES BEFORE CROSSOVER CYCLE */
  175. zorg = (double)m/(double)cyc_average;
  176. for(n=0;n<cycleno_in_group_at_bufcros;n++) {
  177. cycindex = round(zorg * (double)dz->lparray[DISTORTA_CYCLEN][n]);
  178. k = dz->lparray[DISTORTA_STARTCYC][n] + cycindex;
  179. sum += b[k];
  180. } /* CROSSOVER CYCLE */
  181. cycindex = round(zorg * (double)dz->lparray[DISTORTA_CYCLEN][n]);
  182. k = dz->lparray[DISTORTA_STARTCYC][n] + cycindex;
  183. if(k >= dz->buflen) {
  184. b = dz->sampbuf[current_buf];
  185. k -= dz->buflen;
  186. }
  187. sum += b[k];
  188. b = dz->sampbuf[current_buf]; /* CYCLES AFTER CROSSOVER CYCLE */
  189. for(n=cycleno_in_group_at_bufcros+1;n<dz->iparam[DISTORTA_CYCLECNT];n++) {
  190. cycindex = round(zorg * (double)dz->lparray[DISTORTA_CYCLEN][n]);
  191. k = dz->lparray[DISTORTA_STARTCYC][n] + cycindex;
  192. sum += b[k];
  193. }
  194. /*RWD added cast */
  195. dz->sampbuf[3][j++] = (float) /*round*/(sum/(double)dz->iparam[DISTORTA_CYCLECNT]);
  196. }
  197. return write_cycles(obufpos,cyc_average,dz);
  198. }
  199. /************************* WRITE_CYCLES **************************/
  200. int write_cycles(int *obufpos,int cyc_average,dataptr dz)
  201. {
  202. int exit_status;
  203. register int k = *obufpos, n, m, j;
  204. for(m=0;m<dz->iparam[DISTORTA_CYCLECNT];m++) {
  205. j = 0;
  206. for(n=0;n<cyc_average;n++) {
  207. dz->sampbuf[2][k++] = dz->sampbuf[3][j++];
  208. if(k >= dz->buflen) {
  209. if((exit_status = write_samps(dz->sampbuf[2],dz->buflen,dz))<0)
  210. return(exit_status);
  211. k = 0;
  212. }
  213. }
  214. }
  215. *obufpos = k;
  216. return(FINISHED);
  217. }