Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

synth_sine.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_sine.h"
  27. #include "utility/dspinst.h"
  28. // data_waveforms.c
  29. extern "C" {
  30. extern const int16_t AudioWaveformSine[257];
  31. }
  32. void AudioSynthWaveformSine::update(void)
  33. {
  34. audio_block_t *block;
  35. uint32_t i, ph, inc, index, scale;
  36. int32_t val1, val2;
  37. if (magnitude) {
  38. block = allocate();
  39. if (block) {
  40. ph = phase_accumulator;
  41. inc = phase_increment;
  42. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  43. index = ph >> 24;
  44. val1 = AudioWaveformSine[index];
  45. val2 = AudioWaveformSine[index+1];
  46. scale = (ph >> 8) & 0xFFFF;
  47. val2 *= scale;
  48. val1 *= 0xFFFF - scale;
  49. //block->data[i] = (((val1 + val2) >> 16) * magnitude) >> 16;
  50. block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude);
  51. ph += inc;
  52. }
  53. phase_accumulator = ph;
  54. transmit(block);
  55. release(block);
  56. return;
  57. }
  58. }
  59. phase_accumulator += phase_increment * AUDIO_BLOCK_SAMPLES;
  60. }
  61. void AudioSynthWaveformSineModulated::update(void)
  62. {
  63. audio_block_t *block, *modinput;
  64. uint32_t i, ph, inc, index, scale;
  65. int32_t val1, val2;
  66. int16_t mod;
  67. modinput = receiveReadOnly();
  68. ph = phase_accumulator;
  69. inc = phase_increment;
  70. block = allocate();
  71. if (!block) {
  72. // unable to allocate memory, so we'll send nothing
  73. if (modinput) {
  74. // but if we got modulation data, update the phase
  75. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  76. mod = modinput->data[i];
  77. ph += inc + (multiply_32x32_rshift32(inc, mod << 16) << 1);
  78. }
  79. release(modinput);
  80. } else {
  81. ph += phase_increment * AUDIO_BLOCK_SAMPLES;
  82. }
  83. phase_accumulator = ph;
  84. return;
  85. }
  86. if (modinput) {
  87. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  88. index = ph >> 24;
  89. val1 = AudioWaveformSine[index];
  90. val2 = AudioWaveformSine[index+1];
  91. scale = (ph >> 8) & 0xFFFF;
  92. val2 *= scale;
  93. val1 *= 0xFFFF - scale;
  94. //block->data[i] = (((val1 + val2) >> 16) * magnitude) >> 16;
  95. block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude);
  96. // -32768 = no phase increment
  97. // 32767 = double phase increment
  98. mod = modinput->data[i];
  99. ph += inc + (multiply_32x32_rshift32(inc, mod << 16) << 1);
  100. //ph += inc + (((int64_t)inc * (mod << 16)) >> 31);
  101. }
  102. release(modinput);
  103. } else {
  104. ph = phase_accumulator;
  105. inc = phase_increment;
  106. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  107. index = ph >> 24;
  108. val1 = AudioWaveformSine[index];
  109. val2 = AudioWaveformSine[index+1];
  110. scale = (ph >> 8) & 0xFFFF;
  111. val2 *= scale;
  112. val1 *= 0xFFFF - scale;
  113. block->data[i] = (val1 + val2) >> 16;
  114. ph += inc;
  115. }
  116. }
  117. phase_accumulator = ph;
  118. transmit(block);
  119. release(block);
  120. }