PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

150 lines
5.7KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, 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. // http://stenzel.waldorfmusic.de/post/pink/
  27. // https://github.com/Stenzel/newshadeofpink
  28. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  29. // New Shade of Pink
  30. // (c) 2014 Stefan Stenzel
  31. // stefan at waldorfmusic.de
  32. //
  33. // Terms of use:
  34. // Use for any purpose. If used in a commercial product, you should give me one.
  35. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  36. #include <Arduino.h>
  37. #include "synth_pinknoise.h"
  38. int16_t AudioSynthNoisePink::instance_cnt = 0;
  39. // Let preprocessor and compiler calculate two lookup tables for 12-tap FIR Filter
  40. // with these coefficients: 1.190566, 0.162580, 0.002208, 0.025475, -0.001522,
  41. // 0.007322, 0.001774, 0.004529, -0.001561, 0.000776, -0.000486, 0.002017
  42. #define Fn(cf,m,shift) (2048*cf*(2*((m)>>shift&1)-1))
  43. #define FA(n) (int32_t)(Fn(1.190566,n,0)+Fn(0.162580,n,1)+Fn(0.002208,n,2)+\
  44. Fn(0.025475,n,3)+Fn(-0.001522,n,4)+Fn(0.007322,n,5))
  45. #define FB(n) (int32_t)(Fn(0.001774,n,0)+Fn(0.004529,n,1)+Fn(-0.001561,n,2)+\
  46. Fn(0.000776,n,3)+Fn(-0.000486,n,4)+Fn(0.002017,n,5))
  47. #define FA8(n) FA(n),FA(n+1),FA(n+2),FA(n+3),FA(n+4),FA(n+5),FA(n+6),FA(n+7)
  48. #define FB8(n) FB(n),FB(n+1),FB(n+2),FB(n+3),FB(n+4),FB(n+5),FB(n+6),FB(n+7)
  49. const int32_t AudioSynthNoisePink::pfira[64] = // 1st FIR lookup table
  50. {FA8(0),FA8(8),FA8(16),FA8(24),FA8(32),FA8(40),FA8(48),FA8(56)};
  51. const int32_t AudioSynthNoisePink::pfirb[64] = // 2nd FIR lookup table
  52. {FB8(0),FB8(8),FB8(16),FB8(24),FB8(32),FB8(40),FB8(48),FB8(56)};
  53. // bitreversed lookup table
  54. #define PM16(n) n,0x80,0x40,0x80,0x20,0x80,0x40,0x80,0x10,0x80,0x40,0x80,0x20,0x80,0x40,0x80
  55. const uint8_t AudioSynthNoisePink::pnmask[256] = {
  56. PM16(0),PM16(8),PM16(4),PM16(8),PM16(2),PM16(8),PM16(4),PM16(8),
  57. PM16(1),PM16(8),PM16(4),PM16(8),PM16(2),PM16(8),PM16(4),PM16(8)
  58. };
  59. #define PINT(bitmask, out) /* macro for processing: */\
  60. bit = lfsr >> 31; /* spill random to all bits */\
  61. dec &= ~bitmask; /* blank old decrement bit */\
  62. lfsr <<= 1; /* shift lfsr */\
  63. dec |= inc & bitmask; /* copy increment to decrement bit */\
  64. inc ^= bit & bitmask; /* new random bit */\
  65. accu += inc - dec; /* integrate */\
  66. lfsr ^= bit & taps; /* update lfsr */\
  67. out = accu + /* save output */\
  68. pfira[lfsr & 0x3F] + /* add 1st half precalculated FIR */\
  69. pfirb[lfsr >> 6 & 0x3F] /* add 2nd half, also correts bias */
  70. void AudioSynthNoisePink::update(void)
  71. {
  72. audio_block_t *block;
  73. uint32_t *p, *end;
  74. int32_t n1, n2;
  75. int32_t gain;
  76. int32_t inc, dec, accu, bit, lfsr;
  77. int32_t taps;
  78. gain = level;
  79. if (gain == 0) return;
  80. block = allocate();
  81. if (!block) return;
  82. p = (uint32_t *)(block->data);
  83. end = p + AUDIO_BLOCK_SAMPLES/2;
  84. taps = 0x46000001;
  85. inc = pinc;
  86. dec = pdec;
  87. accu = paccu;
  88. lfsr = plfsr;
  89. do {
  90. int32_t mask = pnmask[pncnt++];
  91. PINT(mask, n1);
  92. n1 = signed_multiply_32x16b(gain, n1);
  93. PINT(0x0800, n2);
  94. n2 = signed_multiply_32x16b(gain, n2);
  95. *p++ = pack_16b_16b(n2, n1);
  96. PINT(0x0400, n1);
  97. n1 = signed_multiply_32x16b(gain, n1);
  98. PINT(0x0800, n2);
  99. n2 = signed_multiply_32x16b(gain, n2);
  100. *p++ = pack_16b_16b(n2, n1);
  101. PINT(0x0200, n1);
  102. n1 = signed_multiply_32x16b(gain, n1);
  103. PINT(0x0800, n2);
  104. n2 = signed_multiply_32x16b(gain, n2);
  105. *p++ = pack_16b_16b(n2, n1);
  106. PINT(0x0400, n1);
  107. n1 = signed_multiply_32x16b(gain, n1);
  108. PINT(0x0800, n2);
  109. n2 = signed_multiply_32x16b(gain, n2);
  110. *p++ = pack_16b_16b(n2, n1);
  111. PINT(0x0100, n1);
  112. n1 = signed_multiply_32x16b(gain, n1);
  113. PINT(0x0800, n2);
  114. n2 = signed_multiply_32x16b(gain, n2);
  115. *p++ = pack_16b_16b(n2, n1);
  116. PINT(0x0400, n1);
  117. n1 = signed_multiply_32x16b(gain, n1);
  118. PINT(0x0800, n2);
  119. n2 = signed_multiply_32x16b(gain, n2);
  120. *p++ = pack_16b_16b(n2, n1);
  121. PINT(0x0200, n1);
  122. n1 = signed_multiply_32x16b(gain, n1);
  123. PINT(0x0800, n2);
  124. n2 = signed_multiply_32x16b(gain, n2);
  125. *p++ = pack_16b_16b(n2, n1);
  126. PINT(0x0400, n1);
  127. n1 = signed_multiply_32x16b(gain, n1);
  128. PINT(0x0800, n2);
  129. n2 = signed_multiply_32x16b(gain, n2);
  130. *p++ = pack_16b_16b(n2, n1);
  131. } while (p < end);
  132. pinc = inc;
  133. pdec = dec;
  134. paccu = accu;
  135. plfsr = lfsr;
  136. transmit(block);
  137. release(block);
  138. }