You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 10 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Pete (El Supremo)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "effect_chorus.h"
  23. /******************************************************************/
  24. // A u d i o E f f e c t C h o r u s
  25. // Written by Pete (El Supremo) Jan 2014
  26. // 140529 - change to handle mono stream - change modify() to voices()
  27. // 140219 - correct storage class (not static)
  28. boolean AudioEffectChorus::begin(short *delayline,int d_length,int n_chorus)
  29. {
  30. Serial.print("AudioEffectChorus.begin(Chorus delay line length = ");
  31. Serial.print(d_length);
  32. Serial.print(", n_chorus = ");
  33. Serial.print(n_chorus);
  34. Serial.println(")");
  35. l_delayline = NULL;
  36. delay_length = 0;
  37. l_circ_idx = 0;
  38. if(delayline == NULL) {
  39. return(false);
  40. }
  41. if(d_length < 10) {
  42. return(false);
  43. }
  44. if(n_chorus < 1) {
  45. return(false);
  46. }
  47. l_delayline = delayline;
  48. delay_length = d_length/2;
  49. num_chorus = n_chorus;
  50. return(true);
  51. }
  52. void AudioEffectChorus::voices(int n_chorus)
  53. {
  54. num_chorus = n_chorus;
  55. }
  56. //int last_idx = 0;
  57. void AudioEffectChorus::update(void)
  58. {
  59. audio_block_t *block;
  60. short *bp;
  61. int sum;
  62. int c_idx;
  63. if(l_delayline == NULL)return;
  64. // do passthru
  65. // It stores the unmodified data in the delay line so that
  66. // it isn't as likely to click
  67. if(num_chorus < 1) {
  68. // Just passthrough
  69. block = receiveWritable(0);
  70. if(block) {
  71. bp = block->data;
  72. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  73. l_circ_idx++;
  74. if(l_circ_idx >= delay_length) {
  75. l_circ_idx = 0;
  76. }
  77. l_delayline[l_circ_idx] = *bp++;
  78. }
  79. transmit(block,0);
  80. release(block);
  81. }
  82. }
  83. // L E F T C H A N N E L
  84. block = receiveWritable(0);
  85. if(block) {
  86. bp = block->data;
  87. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  88. l_circ_idx++;
  89. if(l_circ_idx >= delay_length) {
  90. l_circ_idx = 0;
  91. }
  92. l_delayline[l_circ_idx] = *bp;
  93. sum = 0;
  94. c_idx = l_circ_idx;
  95. for(int k = 0; k < num_chorus; k++) {
  96. sum += l_delayline[c_idx];
  97. if(num_chorus > 1)c_idx -= delay_length/(num_chorus - 1) - 1;
  98. if(c_idx < 0) {
  99. c_idx += delay_length;
  100. }
  101. }
  102. *bp++ = sum/num_chorus;
  103. }
  104. // transmit the block
  105. transmit(block,0);
  106. release(block);
  107. }
  108. }