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.

72 lines
2.4KB

  1. /*
  2. * Waveshaper for Teensy 3.X audio
  3. *
  4. * Copyright (c) 2017 Damien Clarke, http://damienclarke.me
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "effect_waveshaper.h"
  25. void AudioEffectWaveshaper::shape(int16_t* waveshape, int length)
  26. {
  27. // length must be bigger than 1 and equal to a power of two + 1
  28. // anything else means we don't continue
  29. if(!waveshape || length < 2 || length > 32769 || ((length - 1) & (length - 2))) return;
  30. this->waveshape = waveshape;
  31. // set lerpshift to the number of bits to shift while interpolating
  32. // to cover the entire waveshape over a uint16_t input range
  33. int index = length - 1;
  34. lerpshift = 16;
  35. while (index >>= 1) --lerpshift;
  36. }
  37. void AudioEffectWaveshaper::update(void)
  38. {
  39. if(!waveshape) return;
  40. audio_block_t *block;
  41. block = receiveWritable();
  42. if (!block) return;
  43. // performance testing...
  44. // unsigned long mcs = micros();
  45. uint16_t x, xa;
  46. int16_t i, ya, yb;
  47. for (i = 0; i < AUDIO_BLOCK_SAMPLES; i++) {
  48. // bring int16_t data into uint16_t range
  49. x = block->data[i] + 32768;
  50. // lerp waveshape (from http://coranac.com/tonc/text/fixed.htm)
  51. xa = x >> lerpshift;
  52. ya = waveshape[xa];
  53. yb = waveshape[xa + 1];
  54. block->data[i] = ya + ((yb - ya) * (x - (xa << lerpshift)) >> lerpshift);
  55. }
  56. // log performance test without compensating for rollover...
  57. // Serial.print("(micros)");
  58. // Serial.println(micros() - mcs);
  59. transmit(block);
  60. release(block);
  61. }