You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.2KB

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