Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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