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

105 行
3.1KB

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