No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

synth_karplusstrong.cpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2016, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "synth_karplusstrong.h"
  28. static uint32_t pseudorand(uint32_t lo)
  29. {
  30. uint32_t hi;
  31. hi = multiply_16bx16t(16807, lo); // 16807 * (lo >> 16)
  32. lo = 16807 * (lo & 0xFFFF);
  33. lo += (hi & 0x7FFF) << 16;
  34. lo += hi >> 15;
  35. lo = (lo & 0x7FFFFFFF) + (lo >> 31);
  36. return lo;
  37. }
  38. void AudioSynthKarplusStrong::update(void)
  39. {
  40. audio_block_t *block;
  41. if (state == 0) return;
  42. if (state == 1) {
  43. uint32_t lo = seed;
  44. for (int i=0; i < bufferLen; i++) {
  45. lo = pseudorand(lo);
  46. buffer[i] = signed_multiply_32x16b(magnitude, lo);
  47. }
  48. seed = lo;
  49. state = 2;
  50. }
  51. block = allocate();
  52. if (!block) {
  53. state = 0;
  54. return;
  55. }
  56. int16_t prior;
  57. if (bufferIndex > 0) {
  58. prior = buffer[bufferIndex - 1];
  59. } else {
  60. prior = buffer[bufferLen - 1];
  61. }
  62. int16_t *data = block->data;
  63. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  64. int16_t in = buffer[bufferIndex];
  65. //int16_t out = (in * 32604 + prior * 32604) >> 16;
  66. int16_t out = (in * 32686 + prior * 32686) >> 16;
  67. //int16_t out = (in * 32768 + prior * 32768) >> 16;
  68. *data++ = out;
  69. buffer[bufferIndex] = out;
  70. prior = in;
  71. if (++bufferIndex >= bufferLen) bufferIndex = 0;
  72. }
  73. transmit(block);
  74. release(block);
  75. }
  76. uint32_t AudioSynthKarplusStrong::seed = 1;