Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

132 linhas
4.1KB

  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. #ifndef synth_sine_h_
  27. #define synth_sine_h_
  28. #include "AudioStream.h"
  29. #include "arm_math.h"
  30. // TODO: investigate making a high resolution sine wave
  31. // using Taylor series expansion.
  32. // http://www.musicdsp.org/showone.php?id=13
  33. class AudioSynthWaveformSine : public AudioStream
  34. {
  35. public:
  36. AudioSynthWaveformSine() : AudioStream(0, NULL), magnitude(16384) {}
  37. void frequency(float freq) {
  38. if (freq < 0.0) freq = 0.0;
  39. else if (freq > AUDIO_SAMPLE_RATE_EXACT/2) freq = AUDIO_SAMPLE_RATE_EXACT/2;
  40. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  41. }
  42. void phase(float angle) {
  43. if (angle < 0.0) angle = 0.0;
  44. else if (angle > 360.0) {
  45. angle = angle - 360.0;
  46. if (angle >= 360.0) return;
  47. }
  48. phase_accumulator = angle * (4294967296.0 / 360.0);
  49. }
  50. void amplitude(float n) {
  51. if (n < 0) n = 0;
  52. else if (n > 1.0) n = 1.0;
  53. magnitude = n * 65536.0;
  54. }
  55. virtual void update(void);
  56. private:
  57. uint32_t phase_accumulator;
  58. uint32_t phase_increment;
  59. int32_t magnitude;
  60. };
  61. class AudioSynthWaveformSineHires : public AudioStream
  62. {
  63. public:
  64. AudioSynthWaveformSineHires() : AudioStream(0, NULL), magnitude(16384) {}
  65. void frequency(float freq) {
  66. if (freq < 0.0) freq = 0.0;
  67. else if (freq > AUDIO_SAMPLE_RATE_EXACT/2) freq = AUDIO_SAMPLE_RATE_EXACT/2;
  68. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  69. }
  70. void phase(float angle) {
  71. if (angle < 0.0) angle = 0.0;
  72. else if (angle > 360.0) {
  73. angle = angle - 360.0;
  74. if (angle >= 360.0) return;
  75. }
  76. phase_accumulator = angle * (4294967296.0 / 360.0);
  77. }
  78. void amplitude(float n) {
  79. if (n < 0) n = 0;
  80. else if (n > 1.0) n = 1.0;
  81. magnitude = n * 65536.0;
  82. }
  83. virtual void update(void);
  84. private:
  85. uint32_t phase_accumulator;
  86. uint32_t phase_increment;
  87. int32_t magnitude;
  88. };
  89. class AudioSynthWaveformSineModulated : public AudioStream
  90. {
  91. public:
  92. AudioSynthWaveformSineModulated() : AudioStream(1, inputQueueArray), magnitude(16384) {}
  93. // maximum unmodulated carrier frequency is 11025 Hz
  94. // input = +1.0 doubles carrier
  95. // input = -1.0 DC output
  96. void frequency(float freq) {
  97. if (freq < 0.0) freq = 0.0;
  98. else if (freq > AUDIO_SAMPLE_RATE_EXACT/4) freq = AUDIO_SAMPLE_RATE_EXACT/4;
  99. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  100. }
  101. void phase(float angle) {
  102. if (angle < 0.0) angle = 0.0;
  103. else if (angle > 360.0) {
  104. angle = angle - 360.0;
  105. if (angle >= 360.0) return;
  106. }
  107. phase_accumulator = angle * (4294967296.0 / 360.0);
  108. }
  109. void amplitude(float n) {
  110. if (n < 0) n = 0;
  111. else if (n > 1.0) n = 1.0;
  112. magnitude = n * 65536.0;
  113. }
  114. virtual void update(void);
  115. private:
  116. uint32_t phase_accumulator;
  117. uint32_t phase_increment;
  118. audio_block_t *inputQueueArray[1];
  119. int32_t magnitude;
  120. };
  121. #endif