distortr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 void reverse_cycles(int startpos,int current_pos,int current_buf,dataptr dz);
  34. //TW UPDATE
  35. //static void reverse_cycles_crosbuf(int cyclestart,int current_pos,int current_buf,dataptr dz);
  36. static int reverse_cycles_crosbuf(int cyclestart,int current_pos,int current_buf,dataptr dz);
  37. /************************** DISTORT_REV **************************/
  38. int distort_rev(int *current_buf,int initial_phase,int *current_pos,int *buffer_overrun,int *cnt,dataptr dz)
  39. {
  40. int exit_status;
  41. int cyclestart = *current_pos;
  42. float *b = dz->sampbuf[*current_buf];
  43. if((exit_status = get_full_cycle(b,buffer_overrun,current_buf,initial_phase,current_pos,DISTORTR_CYCLECNT,dz))!=CONTINUE)
  44. return(exit_status);
  45. //TW UPDATE
  46. // if(*buffer_overrun)
  47. // reverse_cycles_crosbuf(cyclestart,*current_pos,*current_buf,dz);
  48. // else
  49. if(*buffer_overrun) {
  50. if((exit_status = reverse_cycles_crosbuf(cyclestart,*current_pos,*current_buf,dz))<0)
  51. return(exit_status);
  52. } else
  53. reverse_cycles(cyclestart,*current_pos,*current_buf,dz);
  54. (*cnt)++;
  55. return(CONTINUE);
  56. }
  57. /**************************** REVERSE_CYCLES ***************************/
  58. void reverse_cycles(int startpos,int current_pos,int current_buf,dataptr dz)
  59. {
  60. register int j, endpos = current_pos-1;
  61. int midpoint = (current_pos - startpos)/2;
  62. float *buf = dz->sampbuf[current_buf];
  63. float dummy;
  64. for(j=0;j<midpoint;j++) {
  65. dummy = buf[startpos];
  66. buf[startpos++] = buf[endpos];
  67. buf[endpos--] = dummy;
  68. }
  69. }
  70. /******************** REVERSE_CYCLES_CROSBUF ****************/
  71. //TW UPDATE
  72. //void reverse_cycles_crosbuf(int cyclestart,int current_pos,int current_buf,dataptr dz)
  73. int reverse_cycles_crosbuf(int cyclestart,int current_pos,int current_buf,dataptr dz)
  74. {
  75. //TW UPDATE
  76. // int exit_status;
  77. float *this_buf = dz->sampbuf[current_buf];
  78. float *previous_buf = dz->sampbuf[!current_buf];
  79. int samps_in_thisbuf = current_pos;
  80. int samps_in_previous_buf = dz->buflen - cyclestart;
  81. int midpoint = (samps_in_thisbuf + samps_in_previous_buf)/2;
  82. int min_samps_in_one_of_bufs = min(samps_in_previous_buf,samps_in_thisbuf);
  83. register int j, startpos = cyclestart, endpos = current_pos - 1;
  84. float dummy;
  85. for(j=0;j<min_samps_in_one_of_bufs;j++) {
  86. dummy = previous_buf[startpos];
  87. previous_buf[startpos++] = this_buf[endpos];
  88. this_buf[endpos--] = dummy;
  89. }
  90. if(samps_in_previous_buf>samps_in_thisbuf) {
  91. endpos = dz->buflen - 1;
  92. for(j=min_samps_in_one_of_bufs;j<midpoint;j++) {
  93. dummy = previous_buf[startpos];
  94. previous_buf[startpos++] = previous_buf[endpos];
  95. previous_buf[endpos--] = dummy;
  96. }
  97. } else {
  98. startpos = 0;
  99. for(j=min_samps_in_one_of_bufs;j<midpoint;j++) {
  100. dummy = this_buf[startpos];
  101. this_buf[startpos++] = this_buf[endpos];
  102. this_buf[endpos--] = dummy;
  103. }
  104. }
  105. return write_samps(dz->sampbuf[!current_buf],dz->buflen,dz);
  106. }