PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

synth_karplusstrong.cpp 2.7KB

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