選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

149 行
5.6KB

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