No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

152 líneas
4.4KB

  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. static inline int32_t substract_int32_then_divide_int32(int32_t a, int32_t b, int32_t c) __attribute__((always_inline, unused));
  50. static inline int32_t substract_int32_then_divide_int32(int32_t a, int32_t b, int32_t c)
  51. {
  52. uint32_t diff;
  53. uint8_t negative;
  54. if (a >= 0) {
  55. if (b >= 0) {
  56. if (a >= b) {
  57. diff = a - b;
  58. negative = 0;
  59. } else {
  60. diff = b - a;
  61. negative = 1;
  62. }
  63. } else {
  64. diff = a + (b * -1); // assumes 0x80000000 * -1 == 0x80000000
  65. negative = 0;
  66. }
  67. } else {
  68. if (b >= 0) {
  69. diff = (a * -1) + b; // assumes 0x80000000 * -1 == 0x80000000
  70. negative = 1;
  71. } else {
  72. if (a >= b) {
  73. diff = a - b;
  74. negative = 0;
  75. } else {
  76. diff = b - a;
  77. negative = 1;
  78. }
  79. }
  80. }
  81. if (c >= 0) {
  82. diff = diff / (uint32_t)c;
  83. } else {
  84. diff = diff / (uint32_t)(c * -1);
  85. negative ^= 1;
  86. }
  87. if (negative) {
  88. if (diff > 0x7FFFFFFF) return 0x80000000;
  89. return (int32_t)diff * -1;
  90. } else {
  91. if (diff > 0x7FFFFFFF) return 0x7FFFFFFF;
  92. return (int32_t)diff;
  93. }
  94. }
  95. #endif
  96. class AudioSynthWaveformDc : public AudioStream
  97. {
  98. public:
  99. AudioSynthWaveformDc() : AudioStream(0, NULL), state(0), magnitude(0) {}
  100. // immediately jump to the new DC level
  101. void amplitude(float n) {
  102. if (n > 1.0) n = 1.0;
  103. else if (n < -1.0) n = -1.0;
  104. int32_t m = (int32_t)(n * 2147418112.0);
  105. __disable_irq();
  106. magnitude = m;
  107. state = 0;
  108. __enable_irq();
  109. }
  110. // slowly transition to the new DC level
  111. void amplitude(float n, float milliseconds) {
  112. if (milliseconds <= 0.0) {
  113. amplitude(n);
  114. return;
  115. }
  116. if (n > 1.0) n = 1.0;
  117. else if (n < -1.0) n = -1.0;
  118. int32_t c = (int32_t)(milliseconds*(AUDIO_SAMPLE_RATE_EXACT/1000.0));
  119. if (c == 0) {
  120. amplitude(n);
  121. return;
  122. }
  123. int32_t t = (int32_t)(n * 2147418112.0);
  124. __disable_irq();
  125. target = t;
  126. if (target == magnitude) {
  127. state = 0;
  128. __enable_irq();
  129. return;
  130. }
  131. increment = substract_int32_then_divide_int32(target, magnitude, c);
  132. if (increment == 0) {
  133. increment = (target > magnitude) ? 1 : -1;
  134. }
  135. state = 1;
  136. __enable_irq();
  137. }
  138. virtual void update(void);
  139. private:
  140. uint8_t state; // 0=steady output, 1=transitioning
  141. int32_t magnitude; // current output
  142. int32_t target; // designed output (while transitiong)
  143. int32_t increment; // adjustment per sample (while transitiong)
  144. };
  145. #endif