Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_waveform_h_
  27. #define synth_waveform_h_
  28. #include "Arduino.h"
  29. #include "AudioStream.h"
  30. #include "arm_math.h"
  31. // waveforms.c
  32. extern "C" {
  33. extern const int16_t AudioWaveformSine[257];
  34. }
  35. #define AUDIO_SAMPLE_RATE_ROUNDED (44118)
  36. #define DELAY_PASSTHRU -1
  37. #define WAVEFORM_SINE 0
  38. #define WAVEFORM_SAWTOOTH 1
  39. #define WAVEFORM_SQUARE 2
  40. #define WAVEFORM_TRIANGLE 3
  41. #define WAVEFORM_ARBITRARY 4
  42. #define WAVEFORM_PULSE 5
  43. #define WAVEFORM_SAWTOOTH_REVERSE 6
  44. #define WAVEFORM_SAMPLE_HOLD 7
  45. // todo: remove these...
  46. #define TONE_TYPE_SINE 0
  47. #define TONE_TYPE_SAWTOOTH 1
  48. #define TONE_TYPE_SQUARE 2
  49. #define TONE_TYPE_TRIANGLE 3
  50. class AudioSynthWaveform :
  51. public AudioStream
  52. {
  53. public:
  54. AudioSynthWaveform(void) :
  55. AudioStream(0,NULL), tone_amp(0), tone_freq(0),
  56. tone_phase(0), tone_width(0.25), tone_incr(0), tone_type(0),
  57. tone_offset(0), arbdata(NULL)
  58. {
  59. }
  60. void frequency(float t_freq) {
  61. if (t_freq < 0.0) t_freq = 0.0;
  62. else if (t_freq > AUDIO_SAMPLE_RATE_EXACT / 2) t_freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  63. tone_incr = (t_freq * (0x80000000LL/AUDIO_SAMPLE_RATE_EXACT)) + 0.5;
  64. }
  65. void phase(float angle) {
  66. if (angle < 0.0) angle = 0.0;
  67. else if (angle > 360.0) {
  68. angle = angle - 360.0;
  69. if (angle >= 360.0) return;
  70. }
  71. tone_phase = angle * (2147483648.0 / 360.0);
  72. }
  73. void amplitude(float n) { // 0 to 1.0
  74. if (n < 0) n = 0;
  75. else if (n > 1.0) n = 1.0;
  76. if ((tone_amp == 0) && n) {
  77. // reset the phase when the amplitude was zero
  78. // and has now been increased.
  79. tone_phase = 0;
  80. }
  81. // set new magnitude
  82. tone_amp = n * 32767.0;
  83. }
  84. void offset(float n) {
  85. if (n < -1.0) n = -1.0;
  86. else if (n > 1.0) n = 1.0;
  87. tone_offset = n * 32767.0;
  88. }
  89. void pulseWidth(float n) { // 0.0 to 1.0
  90. if (n < 0) n = 0;
  91. else if (n > 1.0) n = 1.0;
  92. tone_width = n * 0x7fffffffLL;
  93. // pulse width is stored as the equivalent phase
  94. }
  95. void begin(short t_type) {
  96. tone_phase = 0;
  97. tone_type = t_type;
  98. }
  99. void begin(float t_amp, float t_freq, short t_type) {
  100. amplitude(t_amp);
  101. frequency(t_freq);
  102. begin(t_type);
  103. }
  104. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  105. arbdata = data;
  106. }
  107. virtual void update(void);
  108. private:
  109. short tone_amp;
  110. short tone_freq;
  111. uint32_t tone_phase;
  112. uint32_t tone_width;
  113. // sample for SAMPLE_HOLD
  114. short sample;
  115. // volatile prevents the compiler optimizing out the frequency function
  116. volatile uint32_t tone_incr;
  117. short tone_type;
  118. int16_t tone_offset;
  119. const int16_t *arbdata;
  120. };
  121. #endif