Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

153 lines
4.5KB

  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 0
  34. // TODO: write this in assembly....
  35. static inline int32_t substract_32_then_divide(int32_t a, int32_t b, int32_t c) __attribute__((always_inline, unused));
  36. static inline int32_t substract_32_then_divide(int32_t a, int32_t b, int32_t c)
  37. {
  38. int32_t diff = substract_32_saturate(a, b);
  39. // if only C language provided a way to test Q status bit....
  40. if (diff != 0x7FFFFFFF && diff != 0x80000000) {
  41. return diff / c;
  42. } else {
  43. diff = substract_32_saturate(a/2, b/2);
  44. if (c == 1 || c == -1) c *= 2; // <-- horrible, incorrect hack
  45. return (diff / c) * 2;
  46. }
  47. }
  48. #else
  49. // compute (a - b) / c ... handling 32 bit interger overflow without slow 64 bit math
  50. static inline int32_t substract_int32_then_divide_int32(int32_t a, int32_t b, int32_t c) __attribute__((always_inline, unused));
  51. static inline int32_t substract_int32_then_divide_int32(int32_t a, int32_t b, int32_t c)
  52. {
  53. uint32_t diff;
  54. uint8_t negative;
  55. if (a >= 0) {
  56. if (b >= 0) {
  57. if (a >= b) {
  58. diff = a - b;
  59. negative = 0;
  60. } else {
  61. diff = b - a;
  62. negative = 1;
  63. }
  64. } else {
  65. diff = a + (b * -1); // assumes 0x80000000 * -1 == 0x80000000
  66. negative = 0;
  67. }
  68. } else {
  69. if (b >= 0) {
  70. diff = (a * -1) + b; // assumes 0x80000000 * -1 == 0x80000000
  71. negative = 1;
  72. } else {
  73. if (a >= b) {
  74. diff = a - b;
  75. negative = 0;
  76. } else {
  77. diff = b - a;
  78. negative = 1;
  79. }
  80. }
  81. }
  82. if (c >= 0) {
  83. diff = diff / (uint32_t)c;
  84. } else {
  85. diff = diff / (uint32_t)(c * -1);
  86. negative ^= 1;
  87. }
  88. if (negative) {
  89. if (diff > 0x7FFFFFFF) return 0x80000000;
  90. return (int32_t)diff * -1;
  91. } else {
  92. if (diff > 0x7FFFFFFF) return 0x7FFFFFFF;
  93. return (int32_t)diff;
  94. }
  95. }
  96. #endif
  97. class AudioSynthWaveformDc : public AudioStream
  98. {
  99. public:
  100. AudioSynthWaveformDc() : AudioStream(0, NULL), state(0), magnitude(0) {}
  101. // immediately jump to the new DC level
  102. void amplitude(float n) {
  103. if (n > 1.0) n = 1.0;
  104. else if (n < -1.0) n = -1.0;
  105. int32_t m = (int32_t)(n * 2147418112.0);
  106. __disable_irq();
  107. magnitude = m;
  108. state = 0;
  109. __enable_irq();
  110. }
  111. // slowly transition to the new DC level
  112. void amplitude(float n, float milliseconds) {
  113. if (milliseconds <= 0.0) {
  114. amplitude(n);
  115. return;
  116. }
  117. if (n > 1.0) n = 1.0;
  118. else if (n < -1.0) n = -1.0;
  119. int32_t c = (int32_t)(milliseconds*(AUDIO_SAMPLE_RATE_EXACT/1000.0));
  120. if (c == 0) {
  121. amplitude(n);
  122. return;
  123. }
  124. int32_t t = (int32_t)(n * 2147418112.0);
  125. __disable_irq();
  126. target = t;
  127. if (target == magnitude) {
  128. state = 0;
  129. __enable_irq();
  130. return;
  131. }
  132. increment = substract_int32_then_divide_int32(target, magnitude, c);
  133. if (increment == 0) {
  134. increment = (target > magnitude) ? 1 : -1;
  135. }
  136. state = 1;
  137. __enable_irq();
  138. }
  139. virtual void update(void);
  140. private:
  141. uint8_t state; // 0=steady output, 1=transitioning
  142. int32_t magnitude; // current output
  143. int32_t target; // designed output (while transitiong)
  144. int32_t increment; // adjustment per sample (while transitiong)
  145. };
  146. #endif