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.

89 lines
2.6KB

  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 "synth_karplusstrong.h"
  27. static uint32_t pseudorand(uint32_t lo)
  28. {
  29. uint32_t hi;
  30. hi = multiply_16bx16t(16807, lo); // 16807 * (lo >> 16)
  31. lo = 16807 * (lo & 0xFFFF);
  32. lo += (hi & 0x7FFF) << 16;
  33. lo += hi >> 15;
  34. lo = (lo & 0x7FFFFFFF) + (lo >> 31);
  35. return lo;
  36. }
  37. void AudioSynthKarplusStrong::update(void)
  38. {
  39. audio_block_t *block;
  40. if (state == 0) return;
  41. if (state == 1) {
  42. uint32_t lo = seed;
  43. for (int i=0; i < bufferLen; i++) {
  44. lo = pseudorand(lo);
  45. buffer[i] = signed_multiply_32x16b(magnitude, lo);
  46. }
  47. seed = lo;
  48. state = 2;
  49. }
  50. block = allocate();
  51. if (!block) {
  52. state = 0;
  53. return;
  54. }
  55. int16_t prior;
  56. if (bufferIndex > 0) {
  57. prior = buffer[bufferIndex - 1];
  58. } else {
  59. prior = buffer[bufferLen - 1];
  60. }
  61. int16_t *data = block->data;
  62. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  63. int16_t in = buffer[bufferIndex];
  64. //int16_t out = (in * 32604 + prior * 32604) >> 16;
  65. int16_t out = (in * 32686 + prior * 32686) >> 16;
  66. //int16_t out = (in * 32768 + prior * 32768) >> 16;
  67. *data++ = out;
  68. buffer[bufferIndex] = out;
  69. prior = in;
  70. if (++bufferIndex >= bufferLen) bufferIndex = 0;
  71. }
  72. transmit(block);
  73. release(block);
  74. }
  75. uint32_t AudioSynthKarplusStrong::seed = 1;