distorth.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 HCYCBUF (2)
  34. static int get_halfcycle(float **buf,int *bufpos,int *cyclelen,int *current_buf,int *is_bufcros,
  35. int prescale_param,int *last_total_samps,dataptr dz);
  36. static void do_harmonic(float *buf,int endsamp,int cyclestart,int cyclelen,dataptr dz);
  37. static void do_harmonic_crosbuf(int endsamp,int cyclestart,int cyclelen,int current_buf,dataptr dz);
  38. /**************************** DISTORTH ******************************
  39. *
  40. * Distort file by adding harmonics over each wavecycle.
  41. */
  42. int distorth(int *bufpos,int phase,int *last_total_samps,int *current_buf,dataptr dz)
  43. {
  44. int exit_status;
  45. int cyclelen = 0;
  46. float *buf = dz->sampbuf[*current_buf];
  47. int is_bufcros = 0;
  48. int cyclestart = *bufpos;
  49. switch(phase) {
  50. case(1):
  51. while(buf[*bufpos]>=0) {
  52. if((exit_status = get_halfcycle(&buf,bufpos,&cyclelen,current_buf,&is_bufcros,
  53. DISTORTH_PRESCALE,last_total_samps,dz))!=CONTINUE)
  54. return(exit_status);
  55. }
  56. while(buf[*bufpos]<=0) {
  57. if((exit_status = get_halfcycle(&buf,bufpos,&cyclelen,current_buf,&is_bufcros,
  58. DISTORTH_PRESCALE,last_total_samps,dz))!=CONTINUE)
  59. return(exit_status);
  60. }
  61. break;
  62. case(-1):
  63. while(buf[*bufpos]<=0) {
  64. if((exit_status = get_halfcycle(&buf,bufpos,&cyclelen,current_buf,&is_bufcros,
  65. DISTORTH_PRESCALE,last_total_samps,dz))!=CONTINUE)
  66. return(exit_status);
  67. }
  68. while(buf[*bufpos]>=0) {
  69. if((exit_status = get_halfcycle(&buf,bufpos,&cyclelen,current_buf,&is_bufcros,
  70. DISTORTH_PRESCALE,last_total_samps,dz))!=CONTINUE)
  71. return(exit_status);
  72. }
  73. break;
  74. }
  75. if(is_bufcros) {
  76. do_harmonic_crosbuf(*bufpos,cyclestart,cyclelen,*current_buf,dz);
  77. if((exit_status = write_samps(dz->sampbuf[!(*current_buf)],dz->buflen,dz))<0)
  78. return(exit_status);
  79. is_bufcros = 0;
  80. } else
  81. do_harmonic(buf,*bufpos,cyclestart,cyclelen,dz);
  82. return(CONTINUE);
  83. }
  84. /**************************** GET_HALFCYCLE **************************/
  85. int get_halfcycle(float **buf,int *bufpos,int *cyclelen,int *current_buf,int *is_bufcros,
  86. int prescale_param,int *last_total_samps,dataptr dz)
  87. {
  88. int exit_status;
  89. dz->sampbuf[HCYCBUF][*cyclelen] = (*buf)[*bufpos];
  90. if(++(*cyclelen) >dz->iparam[DISTORTH_CYCBUFLEN]) {
  91. cop_out(*bufpos,*cyclelen,*last_total_samps,dz);
  92. return FINISHED;
  93. }
  94. if(++(*bufpos) >= dz->ssampsread) {
  95. *last_total_samps = dz->total_samps_read;
  96. if((exit_status = change_buf(current_buf,is_bufcros,buf,dz))!=CONTINUE)
  97. return(exit_status);
  98. if(dz->vflag[IS_PRESCALED])
  99. prescale(*current_buf,prescale_param,dz);
  100. *bufpos = 0;
  101. }
  102. return(CONTINUE);
  103. }
  104. /*************************** DO_HARMONIC ***************************/
  105. void do_harmonic(float *buf,int endsamp,int cyclestart,int cyclelen,dataptr dz)
  106. {
  107. //TW UPDATE
  108. // int n, foldover = 0;
  109. int n;
  110. //TW REVISION (avoid warnings)
  111. double val;
  112. register int k, i;
  113. for(n=0;n<dz->iparam[DISTORTH_HCNT];n++) {
  114. k = cyclestart;
  115. i = dz->iparray[DISTORTH_HNO][n];
  116. //TW UPDATE
  117. if(i >= cyclelen/4) /* Foldover */
  118. continue;
  119. while(k < endsamp) {
  120. /*RWD added cast: but always get the warning with +=... */
  121. // buf[k++] += (float) /*round*/(dz->sampbuf[HCYCBUF][i] * dz->parray[DISTORTH_AMP][n]);
  122. //TW AVOID WARNING
  123. val = buf[k] + (dz->sampbuf[HCYCBUF][i] * dz->parray[DISTORTH_AMP][n]);
  124. buf[k++] = (float)val;
  125. i += dz->iparray[DISTORTH_HNO][n];
  126. //TW UPDATE
  127. while(i >= cyclelen)
  128. i -= cyclelen;
  129. //TW UPDATE
  130. }
  131. }
  132. }
  133. /************************* DO_HARMONIC_CROSBUF *************************/
  134. void do_harmonic_crosbuf(int endsamp,int cyclestart,int cyclelen,int current_buf,dataptr dz)
  135. {
  136. float *buf;
  137. //TW UPDATE
  138. // int n, foldover = 0;
  139. int n;
  140. //TW ADDITION (avoid warnings)
  141. double val;
  142. register int k, i;
  143. for(n=0;n<dz->iparam[DISTORTH_HCNT];n++) {
  144. k = cyclestart;
  145. i = dz->iparray[DISTORTH_HNO][n];
  146. //TW UPDATE
  147. if(i >= cyclelen/4) /* Foldover */
  148. continue;
  149. buf = dz->sampbuf[!current_buf];
  150. while(k < dz->buflen) {
  151. /*RWD added cast */
  152. // buf[k++] += (float) /*round*/(dz->sampbuf[HCYCBUF][i] * dz->parray[DISTORTH_AMP][n]);
  153. //TW REVISION (avoid warnings)
  154. val = buf[k] + (dz->sampbuf[HCYCBUF][i] * dz->parray[DISTORTH_AMP][n]);
  155. buf[k++] = (float)val;
  156. i += dz->iparray[DISTORTH_HNO][n];
  157. //TW UPDATE
  158. // while(i >= cyclelen) {
  159. while(i >= cyclelen)
  160. i -= cyclelen;
  161. // if(dz->iparam[FOLDOVER_WARNING]==FALSE && foldover==1) {
  162. // fprintf(stdout,"\nWARNING: FOLDOVER\n");
  163. // fflush(stdout);
  164. // dz->iparam[FOLDOVER_WARNING] = TRUE;
  165. // }
  166. // foldover++;
  167. // }
  168. // foldover = 0;
  169. }
  170. buf = dz->sampbuf[current_buf];
  171. k = 0;
  172. while(k < endsamp) {
  173. /*RWD added cast */
  174. // buf[k++] += (float)/* round*/(dz->sampbuf[HCYCBUF][i] * dz->parray[DISTORTH_AMP][n]);
  175. //TW REVISION (avoid warnings)
  176. val = buf[k] + (dz->sampbuf[HCYCBUF][i] * dz->parray[DISTORTH_AMP][n]);
  177. buf[k++] = (float)val;
  178. i += dz->iparray[DISTORTH_HNO][n];
  179. //TW UPDATE
  180. // while(i >= cyclelen) {
  181. while(i >= cyclelen)
  182. i -= cyclelen;
  183. // if(dz->iparam[FOLDOVER_WARNING]==FALSE && foldover==1) {
  184. // fprintf(stdout,"WARNING : FOLDOVER\n");
  185. // fflush(stdout);
  186. // dz->iparam[FOLDOVER_WARNING] = TRUE;
  187. // }
  188. // foldover++;
  189. // }
  190. // foldover = 0;
  191. }
  192. }
  193. }