distorts.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 do_shuffle(float *inbuf,int *obufpos,dataptr dz);
  34. static int do_shuffle_crosbuf(int current_buf,int *obufpos,int cycleno_in_group_at_bufcros,dataptr dz);
  35. /**************************** DISTORT_SHUF ******************************
  36. *
  37. * Distort file by shuffling GROUPS of cycles.
  38. */
  39. int distort_shuf
  40. (int *current_buf,int initial_phase,int *obufpos,int *current_pos_in_buf,int *cnt,dataptr dz)
  41. {
  42. int exit_status;
  43. register int i = *current_pos_in_buf;
  44. register int n;
  45. int cycleno_in_group_at_bufcros = -1;
  46. int cyclecnt = dz->iparam[DISTORTS_CYCLECNT] * dz->iparam[DISTORTS_DMNCNT];
  47. float *inbuf = dz->sampbuf[*current_buf];
  48. for(n=0;n<cyclecnt;n++) {
  49. dz->lparray[DISTORTS_STARTCYC][n] = i;
  50. switch(initial_phase) {
  51. case(1):
  52. while(inbuf[i]>=0.0) {
  53. if(++i >= dz->ssampsread) {
  54. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  55. return(exit_status);
  56. cycleno_in_group_at_bufcros = n;
  57. i = 0;
  58. }
  59. }
  60. while(inbuf[i]<=0) {
  61. if(++i >= dz->ssampsread) {
  62. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  63. return(exit_status);
  64. cycleno_in_group_at_bufcros = n;
  65. i = 0;
  66. }
  67. }
  68. break;
  69. case(-1):
  70. while(inbuf[i]<=0) {
  71. if(++i >= dz->ssampsread) {
  72. if((exit_status = change_buff(&inbuf,&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. while(inbuf[i]>=0) {
  79. if(++i >= dz->ssampsread) {
  80. if((exit_status = change_buff(&inbuf,&cycleno_in_group_at_bufcros,current_buf,dz))!=CONTINUE)
  81. return(exit_status);
  82. cycleno_in_group_at_bufcros = n;
  83. i = 0;
  84. }
  85. }
  86. break;
  87. }
  88. }
  89. dz->lparray[DISTORTS_STARTCYC][n] = i;
  90. if(cycleno_in_group_at_bufcros >= 0)
  91. exit_status = do_shuffle_crosbuf(*current_buf,obufpos,cycleno_in_group_at_bufcros,dz);
  92. else
  93. exit_status = do_shuffle(inbuf,obufpos,dz);
  94. if(exit_status<0)
  95. return(exit_status);
  96. *current_pos_in_buf = i;
  97. (*cnt)++;
  98. return(CONTINUE);
  99. }
  100. /*************************** DO_SHUFFLE **************************/
  101. int do_shuffle(float *inbuf,int *obufpos,dataptr dz)
  102. {
  103. /*RWD need this! */
  104. int exit_status;
  105. float *outbuf = dz->sampbuf[2];
  106. register int outbufpos = *obufpos, k, m, n;
  107. int grpbase, thiscycle;
  108. for(n=0;n<dz->iparam[DISTORTS_IMGCNT];n++) {
  109. grpbase = dz->iparray[DISTORTS_MAP][n] * dz->iparam[DISTORTS_CYCLECNT];
  110. for(m=0;m<dz->iparam[DISTORTS_CYCLECNT];m++) {
  111. thiscycle = grpbase + m;
  112. k = dz->lparray[DISTORTS_STARTCYC][thiscycle];
  113. while(k < dz->lparray[DISTORTS_STARTCYC][thiscycle+1]) {
  114. outbuf[outbufpos] = inbuf[k++];
  115. if(++outbufpos >= dz->buflen) {
  116. /* RWD 4:2002 added exit_status check */
  117. exit_status = write_samps(outbuf,dz->buflen,dz);
  118. if(exit_status < 0)
  119. return exit_status;
  120. outbufpos = 0;
  121. }
  122. }
  123. }
  124. }
  125. *obufpos = outbufpos;
  126. return(FINISHED);
  127. }
  128. /********************** DO_SHUFFLE_CROSBUF *************************/
  129. int do_shuffle_crosbuf(int current_buf,int *obufpos,int cycleno_in_group_at_bufcros,dataptr dz)
  130. {
  131. int exit_status; /*RWD 4:2002 */
  132. float *outbuf = dz->sampbuf[2];
  133. register int k, m, n, outbufpos = *obufpos;
  134. int grpbase, thiscycle;
  135. float *inbuf;
  136. for(n=0;n<dz->iparam[DISTORTS_IMGCNT];n++) {
  137. grpbase = dz->iparray[DISTORTS_MAP][n] * dz->iparam[DISTORTS_CYCLECNT];
  138. for(m=0;m<dz->iparam[DISTORTS_CYCLECNT];m++) {
  139. thiscycle = grpbase + m;
  140. k = dz->lparray[DISTORTS_STARTCYC][thiscycle];
  141. if(thiscycle == cycleno_in_group_at_bufcros) {
  142. inbuf= dz->sampbuf[!current_buf];
  143. while(k < dz->buflen) {
  144. outbuf[outbufpos] = inbuf[k++];
  145. if(++outbufpos >= dz->buflen) {
  146. /* RWD 4:2002 added error check */
  147. exit_status = write_samps(outbuf,dz->buflen,dz);
  148. if(exit_status < 0)
  149. return exit_status;
  150. outbufpos = 0;
  151. }
  152. }
  153. inbuf = dz->sampbuf[current_buf];
  154. k = 0;
  155. } else {
  156. if(thiscycle < cycleno_in_group_at_bufcros)
  157. inbuf = dz->sampbuf[!current_buf];
  158. else
  159. inbuf = dz->sampbuf[current_buf];
  160. }
  161. while(k < dz->lparray[DISTORTS_STARTCYC][thiscycle+1]) {
  162. outbuf[outbufpos] = inbuf[k++];
  163. if(++outbufpos >= dz->buflen) {
  164. /*RWD as above */
  165. exit_status = write_samps(outbuf,dz->buflen,dz);
  166. if(exit_status < 0)
  167. return exit_status;
  168. outbufpos = 0;
  169. }
  170. }
  171. }
  172. }
  173. *obufpos = outbufpos;
  174. return(FINISHED);
  175. }