distflt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 is_filtered_out(int cyclecnt,dataptr dz);
  34. static int copy_cycle_to_output(float *b,int endpos,int startpos,int *obufpos,dataptr dz);
  35. static int copy_crosbuf_cycle_to_output(int endpos,int startpos,int current_buf,int *obufpos,dataptr dz);
  36. /************************** DISTORT_FLT **************************/
  37. int distort_flt(int *current_buf,int initial_phase,int *obufpos,int *current_pos_in_buf,dataptr dz)
  38. {
  39. int exit_status;
  40. register int i = *current_pos_in_buf;
  41. float *b = dz->sampbuf[*current_buf];
  42. int cyclelen;
  43. int cyclestart = *current_pos_in_buf;
  44. int buffer_overrun = FALSE;
  45. switch(initial_phase) {
  46. case(1):
  47. while(b[i]>=0) {
  48. if(++i >= dz->ssampsread) {
  49. if((exit_status = change_buf(current_buf,&buffer_overrun,&b,dz))!=CONTINUE) {
  50. *current_pos_in_buf = i;
  51. return(exit_status);
  52. }
  53. i = 0;
  54. }
  55. }
  56. while(b[i]<=0) {
  57. if(++i >= dz->ssampsread) {
  58. if((exit_status = change_buf(current_buf,&buffer_overrun,&b,dz))!=CONTINUE) {
  59. *current_pos_in_buf = i;
  60. return(exit_status);
  61. }
  62. i = 0;
  63. }
  64. }
  65. break;
  66. case(-1):
  67. while(b[i]<=0) {
  68. if(++i >= dz->ssampsread) {
  69. if((exit_status = change_buf(current_buf,&buffer_overrun,&b,dz))!=CONTINUE) {
  70. *current_pos_in_buf = i;
  71. return(exit_status);
  72. }
  73. i = 0;
  74. }
  75. }
  76. while(b[i]>=0) {
  77. if(++i >= dz->ssampsread) {
  78. if((exit_status = change_buf(current_buf,&buffer_overrun,&b,dz))!=CONTINUE) {
  79. *current_pos_in_buf = i;
  80. return(exit_status);
  81. }
  82. i = 0;
  83. }
  84. }
  85. break;
  86. }
  87. cyclelen = i - cyclestart;
  88. if(buffer_overrun==TRUE)
  89. cyclelen += dz->buflen;
  90. exit_status = is_filtered_out(cyclelen,dz);
  91. switch(exit_status) {
  92. case(TRUE): /* ignore this cycle */
  93. break;
  94. case(FALSE): /* keep this cycle */
  95. if(buffer_overrun==TRUE)
  96. exit_status = copy_crosbuf_cycle_to_output(i,cyclestart,*current_buf,obufpos,dz);
  97. else
  98. exit_status = copy_cycle_to_output(b,i,cyclestart,obufpos,dz);
  99. break;
  100. }
  101. if(exit_status<0)
  102. return(exit_status);
  103. *current_pos_in_buf = i;
  104. return(CONTINUE);
  105. }
  106. /****************************** IS_FILTERED_OUT ****************************/
  107. int is_filtered_out(int cyclelen,dataptr dz)
  108. {
  109. switch(dz->mode) {
  110. case(DISTFLT_HIPASS):
  111. if(cyclelen>dz->iparam[DISTFLT_LOFRQ_CYCLELEN])
  112. return(TRUE);
  113. break;
  114. case(DISTFLT_LOPASS):
  115. if(cyclelen<dz->iparam[DISTFLT_HIFRQ_CYCLELEN])
  116. return(TRUE);
  117. break;
  118. case(DISTFLT_BANDPASS):
  119. if(cyclelen<dz->iparam[DISTFLT_HIFRQ_CYCLELEN] || cyclelen>dz->iparam[DISTFLT_LOFRQ_CYCLELEN])
  120. return(TRUE);
  121. break;
  122. default:
  123. sprintf(errstr,"Unknown mode in is_filtered_out()\n");
  124. return(PROGRAM_ERROR);
  125. }
  126. return(FALSE);
  127. }
  128. /**************************** COPY_CYCLE_TO_OUTPUT *****************************/
  129. int copy_cycle_to_output(float *b,int endpos,int startpos,int *obufpos,dataptr dz)
  130. {
  131. int exit_status;
  132. while(startpos < endpos) {
  133. if((exit_status = output_val(b[startpos++],obufpos,dz))<0)
  134. return(exit_status);
  135. }
  136. return(FINISHED);
  137. }
  138. /************************* COPY_CROSBUF_CYCLE_TO_OUTPUT ****************************/
  139. int copy_crosbuf_cycle_to_output(int endpos,int startpos,int current_buf,int *obufpos,dataptr dz)
  140. {
  141. int exit_status;
  142. float *b = dz->sampbuf[!current_buf];
  143. while(startpos < dz->buflen) {
  144. if((exit_status = output_val(b[startpos++],obufpos,dz))<0)
  145. return(exit_status);
  146. }
  147. b = dz->sampbuf[current_buf];
  148. startpos = 0;
  149. while(startpos < endpos) {
  150. if((exit_status = output_val(b[startpos++],obufpos,dz))<0)
  151. return(exit_status);
  152. }
  153. return(FINISHED);
  154. }