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.

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