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.

135 lines
4.0 KiB

  1. /* Audio Library for Teensy 3.X
  2. Copyright (c) 2014, Pete (El Supremo)
  3. Copyright (c) 2019, Holger Wirtz
  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. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <Arduino.h>
  21. #include <Audio.h>
  22. #include "arm_math.h"
  23. #include "effect_modulated_delay.h"
  24. #include "config.h"
  25. extern config_t configuration;
  26. /******************************************************************/
  27. // Based on; A u d i o E f f e c t D e l a y
  28. // Written by Pete (El Supremo) Jan 2014
  29. // 140529 - change to handle mono stream - change modify() to voices()
  30. // 140219 - correct storage class (not static)
  31. // 190527 - added modulation input (by Holger Wirtz)
  32. boolean AudioEffectModulatedDelay::begin(short *delayline, uint16_t d_length)
  33. {
  34. #if 0
  35. Serial.print(F("AudioEffectModulatedDelay.begin(modulated-delay line length = "));
  36. Serial.print(d_length);
  37. Serial.println(F(")"));
  38. #endif
  39. _delayline = NULL;
  40. _delay_length = 0;
  41. _cb_index = 0;
  42. _delay_offset = 0;
  43. if (delayline == NULL)
  44. return (false);
  45. if (d_length < 10)
  46. return (false);
  47. _delayline = delayline;
  48. _delay_length = d_length;
  49. memset(_delayline, 0, _delay_length * sizeof(int16_t));
  50. _delay_offset = _delay_length >> 1 ;
  51. return (true);
  52. }
  53. uint16_t AudioEffectModulatedDelay::get_delay_length(void)
  54. {
  55. return (_delay_length);
  56. }
  57. void AudioEffectModulatedDelay::update(void)
  58. {
  59. audio_block_t *block;
  60. audio_block_t *modulation;
  61. if (_delayline == NULL)
  62. return;
  63. block = receiveWritable(0);
  64. modulation = receiveReadOnly(1);
  65. if (block && modulation)
  66. {
  67. int16_t *bp;
  68. int16_t cb_mod_index_neighbor;
  69. float *mp;
  70. float mod_index;
  71. float mod_number;
  72. float mod_fraction;
  73. float modulation_f32[AUDIO_BLOCK_SAMPLES];
  74. bp = block->data;
  75. arm_q15_to_float(modulation->data, modulation_f32, AUDIO_BLOCK_SAMPLES);
  76. mp = modulation_f32;
  77. for (uint16_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
  78. {
  79. // write data into circular buffer (delayline)
  80. if (_cb_index >= _delay_length)
  81. _cb_index = 0;
  82. _delayline[_cb_index] = *bp;
  83. // calculate the modulation-index as a floating point number for interpolation
  84. mod_index = *mp * _delay_offset;
  85. mod_fraction = modff(mod_index, &mod_number); // split float of mod_index into integer (= mod_number) and fraction part
  86. // calculate modulation index into circular buffer
  87. cb_mod_index = _cb_index - (_delay_offset + mod_number);
  88. if (cb_mod_index < 0) // check for negative offsets and correct them
  89. cb_mod_index += _delay_length;
  90. if (cb_mod_index == _delay_length - 1)
  91. cb_mod_index_neighbor = 0;
  92. else
  93. cb_mod_index_neighbor = cb_mod_index + 1;
  94. *bp = round(float(_delayline[cb_mod_index]) * mod_fraction + float(_delayline[cb_mod_index_neighbor]) * (1.0 - mod_fraction));
  95. // push the pointers forward
  96. bp++; // next audio data
  97. mp++; // next modulation data
  98. _cb_index++; // next circular buffer index
  99. }
  100. }
  101. if (modulation)
  102. release(modulation);
  103. if (block)
  104. {
  105. transmit(block, 0);
  106. release(block);
  107. }
  108. }