PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

effect_waveshaper.cpp 2.5KB

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <Arduino.h>
  25. #include "effect_waveshaper.h"
  26. AudioEffectWaveshaper::~AudioEffectWaveshaper()
  27. {
  28. if(this->waveshape) {
  29. delete [] this->waveshape;
  30. }
  31. }
  32. void AudioEffectWaveshaper::shape(float* waveshape, int length)
  33. {
  34. // length must be bigger than 1 and equal to a power of two + 1
  35. // anything else means we don't continue
  36. if(!waveshape || length < 2 || length > 32769 || ((length - 1) & (length - 2))) return;
  37. if(this->waveshape) {
  38. delete [] this->waveshape;
  39. }
  40. this->waveshape = new int16_t[length];
  41. for(int i = 0; i < length; i++) {
  42. this->waveshape[i] = 32767 * waveshape[i];
  43. }
  44. // set lerpshift to the number of bits to shift while interpolating
  45. // to cover the entire waveshape over a uint16_t input range
  46. int index = length - 1;
  47. lerpshift = 16;
  48. while (index >>= 1) --lerpshift;
  49. }
  50. void AudioEffectWaveshaper::update(void)
  51. {
  52. if(!waveshape) return;
  53. audio_block_t *block;
  54. block = receiveWritable();
  55. if (!block) return;
  56. uint16_t x, xa;
  57. int16_t i, ya, yb;
  58. for (i = 0; i < AUDIO_BLOCK_SAMPLES; i++) {
  59. // bring int16_t data into uint16_t range
  60. x = block->data[i] + 32768;
  61. // lerp waveshape (from http://coranac.com/tonc/text/fixed.htm)
  62. xa = x >> lerpshift;
  63. ya = waveshape[xa];
  64. yb = waveshape[xa + 1];
  65. block->data[i] = ya + ((yb - ya) * (x - (xa << lerpshift)) >> lerpshift);
  66. }
  67. transmit(block);
  68. release(block);
  69. }