No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

synth_sine.cpp 3.9KB

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