You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.1KB

  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. #include "synth_dc.h"
  27. void AudioSynthWaveformDc::update(void)
  28. {
  29. audio_block_t *block;
  30. uint32_t *p, *end, val;
  31. int32_t count, t1, t2, t3, t4;
  32. block = allocate();
  33. if (!block) return;
  34. p = (uint32_t *)(block->data);
  35. end = p + AUDIO_BLOCK_SAMPLES/2;
  36. if (state == 0) {
  37. // steady DC output, simply fill the buffer with fixed value
  38. val = pack_16t_16t(magnitude, magnitude);
  39. do {
  40. *p++ = val;
  41. *p++ = val;
  42. *p++ = val;
  43. *p++ = val;
  44. *p++ = val;
  45. *p++ = val;
  46. *p++ = val;
  47. *p++ = val;
  48. } while (p < end);
  49. } else {
  50. // transitioning to a new DC level
  51. //count = (target - magnitude) / increment;
  52. count = substract_int32_then_divide_int32(target, magnitude, increment);
  53. if (count >= 128) {
  54. // this update will not reach the target
  55. do {
  56. magnitude += increment;
  57. t1 = magnitude;
  58. magnitude += increment;
  59. t1 = pack_16t_16t(magnitude, t1);
  60. magnitude += increment;
  61. t2 = magnitude;
  62. magnitude += increment;
  63. t2 = pack_16t_16t(magnitude, t2);
  64. magnitude += increment;
  65. t3 = magnitude;
  66. magnitude += increment;
  67. t3 = pack_16t_16t(magnitude, t3);
  68. magnitude += increment;
  69. t4 = magnitude;
  70. magnitude += increment;
  71. t4 = pack_16t_16t(magnitude, t4);
  72. *p++ = t1;
  73. *p++ = t2;
  74. *p++ = t3;
  75. *p++ = t4;
  76. } while (p < end);
  77. } else {
  78. // this update reaches the target
  79. while (count >= 2) {
  80. count -= 2;
  81. magnitude += increment;
  82. t1 = magnitude;
  83. magnitude += increment;
  84. t1 = pack_16t_16t(magnitude, t1);
  85. *p++ = t1;
  86. }
  87. if (count) {
  88. t1 = pack_16t_16t(target, magnitude + increment);
  89. *p++ = t1;
  90. }
  91. magnitude = target;
  92. state = 0;
  93. val = pack_16t_16t(magnitude, magnitude);
  94. while (p < end) {
  95. *p++ = val;
  96. }
  97. }
  98. }
  99. transmit(block);
  100. release(block);
  101. }