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

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #define WAVEFORM_BANDLIMIT_SAWTOOTH 9
  45. #define WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE 10
  46. #define WAVEFORM_BANDLIMIT_SQUARE 11
  47. #define WAVEFORM_BANDLIMIT_PULSE 12
  48. typedef struct step_state
  49. {
  50. int offset ;
  51. bool positive ;
  52. } step_state ;
  53. class BandLimitedWaveform
  54. {
  55. public:
  56. BandLimitedWaveform (void) ;
  57. int16_t generate_sawtooth (uint32_t new_phase, int i) ;
  58. int16_t generate_square (uint32_t new_phase, int i) ;
  59. int16_t generate_pulse (uint32_t new_phase, uint32_t pulse_width, int i) ;
  60. void init_sawtooth (uint32_t freq_word) ;
  61. void init_square (uint32_t freq_word) ;
  62. void init_pulse (uint32_t freq_word, uint32_t pulse_width) ;
  63. private:
  64. int32_t lookup (int offset) ;
  65. void insert_step (int offset, bool rising, int i) ;
  66. int32_t process_step (int i) ;
  67. int32_t process_active_steps (uint32_t new_phase) ;
  68. int32_t process_active_steps_saw (uint32_t new_phase) ;
  69. int32_t process_active_steps_pulse (uint32_t new_phase, uint32_t pulse_width) ;
  70. void new_step_check_square (uint32_t new_phase, int i) ;
  71. void new_step_check_pulse (uint32_t new_phase, uint32_t pulse_width, int i) ;
  72. void new_step_check_saw (uint32_t new_phase, int i) ;
  73. uint32_t phase_word ;
  74. int32_t dc_offset ;
  75. step_state states [32] ; // circular buffer of active steps
  76. int newptr ; // buffer pointers into states, AND'd with PTRMASK to keep in buffer range.
  77. int delptr ;
  78. int32_t cyclic[16] ; // circular buffer of output samples
  79. bool pulse_state ;
  80. uint32_t sampled_width ; // pulse width is sampled once per waveform
  81. };
  82. class AudioSynthWaveform : public AudioStream
  83. {
  84. public:
  85. AudioSynthWaveform(void) : AudioStream(0,NULL),
  86. phase_accumulator(0), phase_increment(0), phase_offset(0),
  87. magnitude(0), pulse_width(0x40000000),
  88. arbdata(NULL), sample(0), tone_type(WAVEFORM_SINE),
  89. tone_offset(0) {
  90. }
  91. void frequency(float freq) {
  92. if (freq < 0.0) {
  93. freq = 0.0;
  94. } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2) {
  95. freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  96. }
  97. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  98. if (phase_increment > 0x7FFE0000u) phase_increment = 0x7FFE0000;
  99. }
  100. void phase(float angle) {
  101. if (angle < 0.0) {
  102. angle = 0.0;
  103. } else if (angle > 360.0) {
  104. angle = angle - 360.0;
  105. if (angle >= 360.0) return;
  106. }
  107. phase_offset = angle * (4294967296.0 / 360.0);
  108. }
  109. void amplitude(float n) { // 0 to 1.0
  110. if (n < 0) {
  111. n = 0;
  112. } else if (n > 1.0) {
  113. n = 1.0;
  114. }
  115. magnitude = n * 65536.0;
  116. }
  117. void offset(float n) {
  118. if (n < -1.0) {
  119. n = -1.0;
  120. } else if (n > 1.0) {
  121. n = 1.0;
  122. }
  123. tone_offset = n * 32767.0;
  124. }
  125. void pulseWidth(float n) { // 0.0 to 1.0
  126. if (n < 0) {
  127. n = 0;
  128. } else if (n > 1.0) {
  129. n = 1.0;
  130. }
  131. pulse_width = n * 4294967296.0;
  132. }
  133. void begin(short t_type) {
  134. phase_offset = 0;
  135. tone_type = t_type;
  136. if (t_type == WAVEFORM_BANDLIMIT_SQUARE)
  137. band_limit_waveform.init_square (phase_increment) ;
  138. else if (t_type == WAVEFORM_BANDLIMIT_PULSE)
  139. band_limit_waveform.init_pulse (phase_increment, pulse_width) ;
  140. else if (t_type == WAVEFORM_BANDLIMIT_SAWTOOTH || t_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE)
  141. band_limit_waveform.init_sawtooth (phase_increment) ;
  142. }
  143. void begin(float t_amp, float t_freq, short t_type) {
  144. amplitude(t_amp);
  145. frequency(t_freq);
  146. phase_offset = 0;
  147. begin (t_type);
  148. }
  149. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  150. arbdata = data;
  151. }
  152. virtual void update(void);
  153. private:
  154. uint32_t phase_accumulator;
  155. uint32_t phase_increment;
  156. uint32_t phase_offset;
  157. int32_t magnitude;
  158. uint32_t pulse_width;
  159. const int16_t *arbdata;
  160. int16_t sample; // for WAVEFORM_SAMPLE_HOLD
  161. short tone_type;
  162. int16_t tone_offset;
  163. BandLimitedWaveform band_limit_waveform ;
  164. };
  165. class AudioSynthWaveformModulated : public AudioStream
  166. {
  167. public:
  168. AudioSynthWaveformModulated(void) : AudioStream(2, inputQueueArray),
  169. phase_accumulator(0), phase_increment(0), modulation_factor(32768),
  170. magnitude(0), arbdata(NULL), sample(0), tone_offset(0),
  171. tone_type(WAVEFORM_SINE), modulation_type(0) {
  172. }
  173. void frequency(float freq) {
  174. if (freq < 0.0) {
  175. freq = 0.0;
  176. } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2) {
  177. freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  178. }
  179. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  180. if (phase_increment > 0x7FFE0000u) phase_increment = 0x7FFE0000;
  181. }
  182. void amplitude(float n) { // 0 to 1.0
  183. if (n < 0) {
  184. n = 0;
  185. } else if (n > 1.0) {
  186. n = 1.0;
  187. }
  188. magnitude = n * 65536.0;
  189. }
  190. void offset(float n) {
  191. if (n < -1.0) {
  192. n = -1.0;
  193. } else if (n > 1.0) {
  194. n = 1.0;
  195. }
  196. tone_offset = n * 32767.0;
  197. }
  198. void begin(short t_type) {
  199. tone_type = t_type;
  200. if (t_type == WAVEFORM_BANDLIMIT_SQUARE)
  201. band_limit_waveform.init_square (phase_increment) ;
  202. else if (t_type == WAVEFORM_BANDLIMIT_PULSE)
  203. band_limit_waveform.init_pulse (phase_increment, 0x80000000u) ;
  204. else if (t_type == WAVEFORM_BANDLIMIT_SAWTOOTH || t_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE)
  205. band_limit_waveform.init_sawtooth (phase_increment) ;
  206. }
  207. void begin(float t_amp, float t_freq, short t_type) {
  208. amplitude(t_amp);
  209. frequency(t_freq);
  210. begin (t_type) ;
  211. }
  212. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  213. arbdata = data;
  214. }
  215. void frequencyModulation(float octaves) {
  216. if (octaves > 12.0) {
  217. octaves = 12.0;
  218. } else if (octaves < 0.1) {
  219. octaves = 0.1;
  220. }
  221. modulation_factor = octaves * 4096.0;
  222. modulation_type = 0;
  223. }
  224. void phaseModulation(float degrees) {
  225. if (degrees > 9000.0) {
  226. degrees = 9000.0;
  227. } else if (degrees < 30.0) {
  228. degrees = 30.0;
  229. }
  230. modulation_factor = degrees * (65536.0 / 180.0);
  231. modulation_type = 1;
  232. }
  233. virtual void update(void);
  234. private:
  235. audio_block_t *inputQueueArray[2];
  236. uint32_t phase_accumulator;
  237. uint32_t phase_increment;
  238. uint32_t modulation_factor;
  239. int32_t magnitude;
  240. const int16_t *arbdata;
  241. uint32_t phasedata[AUDIO_BLOCK_SAMPLES];
  242. int16_t sample; // for WAVEFORM_SAMPLE_HOLD
  243. int16_t tone_offset;
  244. uint8_t tone_type;
  245. uint8_t modulation_type;
  246. BandLimitedWaveform band_limit_waveform ;
  247. };
  248. #endif