132 lines
3.4KB

  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 <Arduino.h>
  23. #include "effect_chorus.h"
  24. /******************************************************************/
  25. // A u d i o E f f e c t C h o r u s
  26. // Written by Pete (El Supremo) Jan 2014
  27. // 140529 - change to handle mono stream - change modify() to voices()
  28. // 140219 - correct storage class (not static)
  29. boolean AudioEffectChorus::begin(short *delayline,int d_length,int n_chorus)
  30. {
  31. #if 0
  32. Serial.print("AudioEffectChorus.begin(Chorus delay line length = ");
  33. Serial.print(d_length);
  34. Serial.print(", n_chorus = ");
  35. Serial.print(n_chorus);
  36. Serial.println(")");
  37. #endif
  38. l_delayline = NULL;
  39. delay_length = 0;
  40. l_circ_idx = 0;
  41. if(delayline == NULL) {
  42. return(false);
  43. }
  44. if(d_length < 10) {
  45. return(false);
  46. }
  47. if(n_chorus < 1) {
  48. return(false);
  49. }
  50. l_delayline = delayline;
  51. delay_length = d_length/2;
  52. num_chorus = n_chorus;
  53. return(true);
  54. }
  55. void AudioEffectChorus::voices(int n_chorus)
  56. {
  57. num_chorus = n_chorus;
  58. }
  59. //int last_idx = 0;
  60. void AudioEffectChorus::update(void)
  61. {
  62. audio_block_t *block;
  63. short *bp;
  64. int sum;
  65. int c_idx;
  66. if(l_delayline == NULL)return;
  67. // do passthru
  68. // It stores the unmodified data in the delay line so that
  69. // it isn't as likely to click
  70. if(num_chorus <= 1) {
  71. // Just passthrough
  72. block = receiveWritable(0);
  73. if(block) {
  74. bp = block->data;
  75. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  76. l_circ_idx++;
  77. if(l_circ_idx >= delay_length) {
  78. l_circ_idx = 0;
  79. }
  80. l_delayline[l_circ_idx] = *bp++;
  81. }
  82. transmit(block,0);
  83. release(block);
  84. }
  85. return;
  86. }
  87. // L E F T C H A N N E L
  88. block = receiveWritable(0);
  89. if(block) {
  90. bp = block->data;
  91. uint32_t tmp = delay_length/(num_chorus - 1) - 1;
  92. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  93. l_circ_idx++;
  94. if(l_circ_idx >= delay_length) {
  95. l_circ_idx = 0;
  96. }
  97. l_delayline[l_circ_idx] = *bp;
  98. sum = 0;
  99. c_idx = l_circ_idx;
  100. for(int k = 0; k < num_chorus; k++) {
  101. sum += l_delayline[c_idx];
  102. if(num_chorus > 1)c_idx -= tmp;
  103. if(c_idx < 0) {
  104. c_idx += delay_length;
  105. }
  106. }
  107. *bp++ = sum/num_chorus;
  108. }
  109. // transmit the block
  110. transmit(block,0);
  111. release(block);
  112. }
  113. }