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.

synth_sine.cpp 4.3KB

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