Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

synth_sine.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #if defined(KINETISK)
  50. block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude);
  51. #elif defined(KINETISL)
  52. block->data[i] = (((val1 + val2) >> 16) * magnitude) >> 16;
  53. #endif
  54. ph += inc;
  55. }
  56. phase_accumulator = ph;
  57. transmit(block);
  58. release(block);
  59. return;
  60. }
  61. }
  62. phase_accumulator += phase_increment * AUDIO_BLOCK_SAMPLES;
  63. }
  64. #if defined(KINETISK)
  65. void AudioSynthWaveformSineModulated::update(void)
  66. {
  67. audio_block_t *block, *modinput;
  68. uint32_t i, ph, inc, index, scale;
  69. int32_t val1, val2;
  70. int16_t mod;
  71. modinput = receiveReadOnly();
  72. ph = phase_accumulator;
  73. inc = phase_increment;
  74. block = allocate();
  75. if (!block) {
  76. // unable to allocate memory, so we'll send nothing
  77. if (modinput) {
  78. // but if we got modulation data, update the phase
  79. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  80. mod = modinput->data[i];
  81. ph += inc + (multiply_32x32_rshift32(inc, mod << 16) << 1);
  82. }
  83. release(modinput);
  84. } else {
  85. ph += phase_increment * AUDIO_BLOCK_SAMPLES;
  86. }
  87. phase_accumulator = ph;
  88. return;
  89. }
  90. if (modinput) {
  91. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  92. index = ph >> 24;
  93. val1 = AudioWaveformSine[index];
  94. val2 = AudioWaveformSine[index+1];
  95. scale = (ph >> 8) & 0xFFFF;
  96. val2 *= scale;
  97. val1 *= 0xFFFF - scale;
  98. //block->data[i] = (((val1 + val2) >> 16) * magnitude) >> 16;
  99. block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude);
  100. // -32768 = no phase increment
  101. // 32767 = double phase increment
  102. mod = modinput->data[i];
  103. ph += inc + (multiply_32x32_rshift32(inc, mod << 16) << 1);
  104. //ph += inc + (((int64_t)inc * (mod << 16)) >> 31);
  105. }
  106. release(modinput);
  107. } else {
  108. ph = phase_accumulator;
  109. inc = phase_increment;
  110. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  111. index = ph >> 24;
  112. val1 = AudioWaveformSine[index];
  113. val2 = AudioWaveformSine[index+1];
  114. scale = (ph >> 8) & 0xFFFF;
  115. val2 *= scale;
  116. val1 *= 0xFFFF - scale;
  117. block->data[i] = (val1 + val2) >> 16;
  118. ph += inc;
  119. }
  120. }
  121. phase_accumulator = ph;
  122. transmit(block);
  123. release(block);
  124. }
  125. #elif defined(KINETISL)
  126. void AudioSynthWaveformSineModulated::update(void)
  127. {
  128. audio_block_t *block;
  129. block = receiveReadOnly();
  130. if (block) release(block);
  131. }
  132. #endif