Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

effect_waveshaper.cpp 2.5KB

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