您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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