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.

138 lines
4.3KB

  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. #ifndef synth_dc_h_
  27. #define synth_dc_h_
  28. #include "AudioStream.h"
  29. #include "utility/dspinst.h"
  30. // compute (a - b) / c
  31. // handling 32 bit interger overflow at every step
  32. // without resorting to slow 64 bit math
  33. #if defined(KINETISK)
  34. static inline int32_t substract_int32_then_divide_int32(int32_t a, int32_t b, int32_t c) __attribute__((always_inline, unused));
  35. static inline int32_t substract_int32_then_divide_int32(int32_t a, int32_t b, int32_t c)
  36. {
  37. int r;
  38. r = substract_32_saturate(a,b);
  39. if ( !get_q_psr() ) return (r/c);
  40. clr_q_psr();
  41. if ( c==0 ) r=0;
  42. if (__builtin_abs(c)<=1) return r;
  43. return (a/c)-(b/c);
  44. }
  45. #else
  46. // compute (a - b) / c ... handling 32 bit interger overflow without slow 64 bit math
  47. static inline int32_t substract_int32_then_divide_int32(int32_t a, int32_t b, int32_t c) __attribute__((always_inline, unused));
  48. static inline int32_t substract_int32_then_divide_int32(int32_t a, int32_t b, int32_t c)
  49. {
  50. uint32_t diff;
  51. uint8_t negative;
  52. if (a >= 0) {
  53. if (b >= 0) {
  54. return (a - b) / c; // no overflow if both a & b are positive
  55. } else {
  56. diff = a + (b * -1); // assumes 0x80000000 * -1 == 0x80000000
  57. negative = 0;
  58. }
  59. } else {
  60. if (b >= 0) {
  61. diff = (a * -1) + b; // assumes 0x80000000 * -1 == 0x80000000
  62. negative = 1;
  63. } else {
  64. return (a - b) / c; // no overflow if both a & b are negative
  65. }
  66. }
  67. if (c >= 0) {
  68. diff = diff / (uint32_t)c;
  69. } else {
  70. diff = diff / (uint32_t)(c * -1);
  71. negative ^= 1;
  72. }
  73. if (negative) {
  74. if (diff > 0x7FFFFFFF) return 0x80000000;
  75. return (int32_t)diff * -1;
  76. } else {
  77. if (diff > 0x7FFFFFFF) return 0x7FFFFFFF;
  78. return (int32_t)diff;
  79. }
  80. }
  81. #endif
  82. class AudioSynthWaveformDc : public AudioStream
  83. {
  84. public:
  85. AudioSynthWaveformDc() : AudioStream(0, NULL), state(0), magnitude(0) {}
  86. // immediately jump to the new DC level
  87. void amplitude(float n) {
  88. if (n > 1.0) n = 1.0;
  89. else if (n < -1.0) n = -1.0;
  90. int32_t m = (int32_t)(n * 2147418112.0);
  91. __disable_irq();
  92. magnitude = m;
  93. state = 0;
  94. __enable_irq();
  95. }
  96. // slowly transition to the new DC level
  97. void amplitude(float n, float milliseconds) {
  98. if (milliseconds <= 0.0) {
  99. amplitude(n);
  100. return;
  101. }
  102. if (n > 1.0) n = 1.0;
  103. else if (n < -1.0) n = -1.0;
  104. int32_t c = (int32_t)(milliseconds*(AUDIO_SAMPLE_RATE_EXACT/1000.0));
  105. if (c == 0) {
  106. amplitude(n);
  107. return;
  108. }
  109. int32_t t = (int32_t)(n * 2147418112.0);
  110. __disable_irq();
  111. target = t;
  112. if (target == magnitude) {
  113. state = 0;
  114. __enable_irq();
  115. return;
  116. }
  117. increment = substract_int32_then_divide_int32(target, magnitude, c);
  118. if (increment == 0) {
  119. increment = (target > magnitude) ? 1 : -1;
  120. }
  121. state = 1;
  122. __enable_irq();
  123. }
  124. virtual void update(void);
  125. private:
  126. uint8_t state; // 0=steady output, 1=transitioning
  127. int32_t magnitude; // current output
  128. int32_t target; // designed output (while transitiong)
  129. int32_t increment; // adjustment per sample (while transitiong)
  130. };
  131. #endif