Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

synth_pwm.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2017, 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_pwm.h"
  28. #include "utility/dspinst.h"
  29. #if defined(KINETISK)
  30. void AudioSynthWaveformPWM::update(void)
  31. {
  32. audio_block_t *block, *modinput;
  33. uint32_t i;
  34. int32_t out;
  35. modinput = receiveReadOnly();
  36. if (magnitude == 0) {
  37. if (modinput) release(modinput);
  38. return;
  39. }
  40. block = allocate();
  41. if (!block) {
  42. // unable to allocate memory, so we'll send nothing
  43. if (modinput) release(modinput);
  44. return;
  45. }
  46. if (modinput) {
  47. const uint32_t _duration = duration;
  48. uint32_t _elapsed = elapsed;
  49. int32_t _magnitude = magnitude;
  50. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  51. _elapsed += 65536;
  52. int32_t in = modinput->data[i];
  53. if (_magnitude < 0) in = -in;
  54. uint32_t dur = ((uint64_t)(in + 32768) * _duration) >> 15;
  55. if (_elapsed < dur) {
  56. out = _magnitude;
  57. } else {
  58. int32_t e = _elapsed - dur;
  59. signed_saturate_rshift(e, 17, 0);
  60. if (e < 0) e = 0;
  61. _elapsed = e;
  62. // elapsed must be 0 to 65535
  63. // magnitude must be -32767 to +32767
  64. out = _magnitude - ((_magnitude * _elapsed) >> 15);
  65. _magnitude = -_magnitude;
  66. }
  67. block->data[i] = out;
  68. }
  69. elapsed = _elapsed;
  70. magnitude = _magnitude;
  71. release(modinput);
  72. } else {
  73. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  74. elapsed += 65536;
  75. if (elapsed < duration) {
  76. out = magnitude;
  77. } else {
  78. elapsed -= duration;
  79. // elapsed must be 0 to 65535
  80. // magnitude must be -32767 to +32767
  81. out = magnitude - ((magnitude * elapsed) >> 15);
  82. magnitude = -magnitude;
  83. }
  84. block->data[i] = out;
  85. }
  86. }
  87. transmit(block);
  88. release(block);
  89. }
  90. #elif defined(KINETISL)
  91. void AudioSynthWaveformPWM::update(void)
  92. {
  93. audio_block_t *block = receiveReadOnly();
  94. if (block) release(block);
  95. }
  96. #endif