Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 WAVEFORM_SINE 0
  36. #define WAVEFORM_SAWTOOTH 1
  37. #define WAVEFORM_SQUARE 2
  38. #define WAVEFORM_TRIANGLE 3
  39. #define WAVEFORM_ARBITRARY 4
  40. #define WAVEFORM_PULSE 5
  41. #define WAVEFORM_SAWTOOTH_REVERSE 6
  42. #define WAVEFORM_SAMPLE_HOLD 7
  43. #define WAVEFORM_TRIANGLE_VARIABLE 8
  44. class AudioSynthWaveform : public AudioStream
  45. {
  46. public:
  47. AudioSynthWaveform(void) : AudioStream(0,NULL),
  48. phase_accumulator(0), phase_increment(0), phase_offset(0),
  49. magnitude(0), pulse_width(0x40000000),
  50. arbdata(NULL), sample(0), tone_type(WAVEFORM_SINE),
  51. tone_offset(0) {
  52. }
  53. void frequency(float freq) {
  54. if (freq < 0.0) {
  55. freq = 0.0;
  56. } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2) {
  57. freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  58. }
  59. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  60. if (phase_increment > 0x7FFE0000u) phase_increment = 0x7FFE0000;
  61. }
  62. void phase(float angle) {
  63. if (angle < 0.0) {
  64. angle = 0.0;
  65. } else if (angle > 360.0) {
  66. angle = angle - 360.0;
  67. if (angle >= 360.0) return;
  68. }
  69. phase_offset = angle * (4294967296.0 / 360.0);
  70. }
  71. void amplitude(float n) { // 0 to 1.0
  72. if (n < 0) {
  73. n = 0;
  74. } else if (n > 1.0) {
  75. n = 1.0;
  76. }
  77. magnitude = n * 65536.0;
  78. }
  79. void offset(float n) {
  80. if (n < -1.0) {
  81. n = -1.0;
  82. } else if (n > 1.0) {
  83. n = 1.0;
  84. }
  85. tone_offset = n * 32767.0;
  86. }
  87. void pulseWidth(float n) { // 0.0 to 1.0
  88. if (n < 0) {
  89. n = 0;
  90. } else if (n > 1.0) {
  91. n = 1.0;
  92. }
  93. pulse_width = n * 4294967296.0;
  94. }
  95. void begin(short t_type) {
  96. phase_offset = 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. phase_offset = 0;
  103. tone_type = t_type;
  104. }
  105. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  106. arbdata = data;
  107. }
  108. virtual void update(void);
  109. private:
  110. uint32_t phase_accumulator;
  111. uint32_t phase_increment;
  112. uint32_t phase_offset;
  113. int32_t magnitude;
  114. uint32_t pulse_width;
  115. const int16_t *arbdata;
  116. int16_t sample; // for WAVEFORM_SAMPLE_HOLD
  117. short tone_type;
  118. int16_t tone_offset;
  119. };
  120. class AudioSynthWaveformModulated : public AudioStream
  121. {
  122. public:
  123. AudioSynthWaveformModulated(void) : AudioStream(2, inputQueueArray),
  124. phase_accumulator(0), phase_increment(0), modulation_factor(32768),
  125. magnitude(0), arbdata(NULL), sample(0), tone_offset(0),
  126. tone_type(WAVEFORM_SINE), modulation_type(0) {
  127. }
  128. void frequency(float freq) {
  129. if (freq < 0.0) {
  130. freq = 0.0;
  131. } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2) {
  132. freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  133. }
  134. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  135. if (phase_increment > 0x7FFE0000u) phase_increment = 0x7FFE0000;
  136. }
  137. void amplitude(float n) { // 0 to 1.0
  138. if (n < 0) {
  139. n = 0;
  140. } else if (n > 1.0) {
  141. n = 1.0;
  142. }
  143. magnitude = n * 65536.0;
  144. }
  145. void offset(float n) {
  146. if (n < -1.0) {
  147. n = -1.0;
  148. } else if (n > 1.0) {
  149. n = 1.0;
  150. }
  151. tone_offset = n * 32767.0;
  152. }
  153. void begin(short t_type) {
  154. tone_type = t_type;
  155. }
  156. void begin(float t_amp, float t_freq, short t_type) {
  157. amplitude(t_amp);
  158. frequency(t_freq);
  159. tone_type = t_type;
  160. }
  161. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  162. arbdata = data;
  163. }
  164. void frequencyModulation(float octaves) {
  165. if (octaves > 12.0) {
  166. octaves = 12.0;
  167. } else if (octaves < 0.1) {
  168. octaves = 0.1;
  169. }
  170. modulation_factor = octaves * 4096.0;
  171. modulation_type = 0;
  172. }
  173. void phaseModulation(float degrees) {
  174. if (degrees > 9000.0) {
  175. degrees = 9000.0;
  176. } else if (degrees < 30.0) {
  177. degrees = 30.0;
  178. }
  179. modulation_factor = degrees * (65536.0 / 180.0);
  180. modulation_type = 1;
  181. }
  182. virtual void update(void);
  183. private:
  184. audio_block_t *inputQueueArray[2];
  185. uint32_t phase_accumulator;
  186. uint32_t phase_increment;
  187. uint32_t modulation_factor;
  188. int32_t magnitude;
  189. const int16_t *arbdata;
  190. uint32_t phasedata[AUDIO_BLOCK_SAMPLES];
  191. int16_t sample; // for WAVEFORM_SAMPLE_HOLD
  192. int16_t tone_offset;
  193. uint8_t tone_type;
  194. uint8_t modulation_type;
  195. };
  196. #endif