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.

151 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 "Audio.h"
  27. #include "arm_math.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. //Serial.println("AudioSynthWaveformSine::update");
  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;
  55. //Serial.print(block->data[i]);
  56. //Serial.print(", ");
  57. //if ((i % 12) == 11) Serial.println();
  58. ph += inc;
  59. }
  60. //Serial.println();
  61. phase = ph;
  62. transmit(block);
  63. release(block);
  64. return;
  65. }
  66. phase += phase_increment * AUDIO_BLOCK_SAMPLES;
  67. }
  68. void AudioSynthWaveformSineModulated::frequency(float f)
  69. {
  70. if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || 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. //Serial.println("AudioSynthWaveformSineModulated::update");
  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. ph += inc + modinput->data[i] * modulation_factor;
  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;
  106. //Serial.print(block->data[i]);
  107. //Serial.print(", ");
  108. //if ((i % 12) == 11) Serial.println();
  109. ph += inc + modinput->data[i] * modulation_factor;
  110. }
  111. release(modinput);
  112. } else {
  113. ph = phase;
  114. inc = phase_increment;
  115. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  116. index = ph >> 24;
  117. val1 = AudioWaveformSine[index];
  118. val2 = AudioWaveformSine[index+1];
  119. scale = (ph >> 8) & 0xFFFF;
  120. val2 *= scale;
  121. val1 *= 0xFFFF - scale;
  122. block->data[i] = (val1 + val2) >> 16;
  123. //Serial.print(block->data[i]);
  124. //Serial.print(", ");
  125. //if ((i % 12) == 11) Serial.println();
  126. ph += inc;
  127. }
  128. }
  129. phase = ph;
  130. transmit(block);
  131. release(block);
  132. }