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.

synth_dc.cpp 3.2KB

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