You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

synth_waveform.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. void new_step_check_square (uint32_t new_phase, int i) ;
  70. void new_step_check_pulse (uint32_t new_phase, uint32_t pulse_width, int i) ;
  71. void new_step_check_saw (uint32_t new_phase, int i) ;
  72. uint32_t phase_word ;
  73. int32_t dc_offset ;
  74. step_state states [32] ; // circular buffer of active steps
  75. int newptr ; // buffer pointers into states, AND'd with PTRMASK to keep in buffer range.
  76. int delptr ;
  77. int32_t cyclic[16] ; // circular buffer of output samples
  78. bool pulse_state ;
  79. uint32_t sampled_width ;
  80. };
  81. class AudioSynthWaveform : public AudioStream
  82. {
  83. public:
  84. AudioSynthWaveform(void) : AudioStream(0,NULL),
  85. phase_accumulator(0), phase_increment(0), phase_offset(0),
  86. magnitude(0), pulse_width(0x40000000),
  87. arbdata(NULL), sample(0), tone_type(WAVEFORM_SINE),
  88. tone_offset(0) {
  89. }
  90. void frequency(float freq) {
  91. if (freq < 0.0) {
  92. freq = 0.0;
  93. } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2) {
  94. freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  95. }
  96. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  97. if (phase_increment > 0x7FFE0000u) phase_increment = 0x7FFE0000;
  98. ensure_pulse_width_ok () ;
  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. ensure_pulse_width_ok () ;
  133. }
  134. void begin(short t_type) {
  135. phase_offset = 0;
  136. tone_type = t_type;
  137. if (t_type == WAVEFORM_BANDLIMIT_SQUARE)
  138. band_limit_waveform.init_square (phase_increment) ;
  139. else if (t_type == WAVEFORM_BANDLIMIT_PULSE)
  140. {
  141. ensure_pulse_width_ok () ;
  142. band_limit_waveform.init_pulse (phase_increment, pulse_width) ;
  143. }
  144. else if (t_type == WAVEFORM_BANDLIMIT_SAWTOOTH || t_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE)
  145. band_limit_waveform.init_sawtooth (phase_increment) ;
  146. }
  147. void begin(float t_amp, float t_freq, short t_type) {
  148. amplitude(t_amp);
  149. frequency(t_freq);
  150. phase_offset = 0;
  151. begin (t_type);
  152. }
  153. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  154. arbdata = data;
  155. }
  156. virtual void update(void);
  157. private:
  158. void ensure_pulse_width_ok()
  159. {
  160. if (tone_type == WAVEFORM_BANDLIMIT_PULSE)
  161. {
  162. if (pulse_width < phase_increment) // ensure pulse never narrow enough to glitch out of existence.
  163. pulse_width = phase_increment ;
  164. else if (pulse_width > -phase_increment)
  165. pulse_width = -phase_increment;
  166. }
  167. }
  168. uint32_t phase_accumulator;
  169. uint32_t phase_increment;
  170. uint32_t phase_offset;
  171. int32_t magnitude;
  172. uint32_t pulse_width;
  173. const int16_t *arbdata;
  174. int16_t sample; // for WAVEFORM_SAMPLE_HOLD
  175. short tone_type;
  176. int16_t tone_offset;
  177. BandLimitedWaveform band_limit_waveform ;
  178. };
  179. class AudioSynthWaveformModulated : public AudioStream
  180. {
  181. public:
  182. AudioSynthWaveformModulated(void) : AudioStream(2, inputQueueArray),
  183. phase_accumulator(0), phase_increment(0), modulation_factor(32768),
  184. magnitude(0), arbdata(NULL), sample(0), tone_offset(0),
  185. tone_type(WAVEFORM_SINE), modulation_type(0) {
  186. }
  187. void frequency(float freq) {
  188. if (freq < 0.0) {
  189. freq = 0.0;
  190. } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2) {
  191. freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  192. }
  193. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  194. if (phase_increment > 0x7FFE0000u) phase_increment = 0x7FFE0000;
  195. }
  196. void amplitude(float n) { // 0 to 1.0
  197. if (n < 0) {
  198. n = 0;
  199. } else if (n > 1.0) {
  200. n = 1.0;
  201. }
  202. magnitude = n * 65536.0;
  203. }
  204. void offset(float n) {
  205. if (n < -1.0) {
  206. n = -1.0;
  207. } else if (n > 1.0) {
  208. n = 1.0;
  209. }
  210. tone_offset = n * 32767.0;
  211. }
  212. void begin(short t_type) {
  213. tone_type = t_type;
  214. if (t_type == WAVEFORM_BANDLIMIT_SQUARE)
  215. band_limit_waveform.init_square (phase_increment) ;
  216. else if (t_type == WAVEFORM_BANDLIMIT_PULSE)
  217. band_limit_waveform.init_pulse (phase_increment, 0x80000000u) ;
  218. else if (t_type == WAVEFORM_BANDLIMIT_SAWTOOTH || t_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE)
  219. band_limit_waveform.init_sawtooth (phase_increment) ;
  220. }
  221. void begin(float t_amp, float t_freq, short t_type) {
  222. amplitude(t_amp);
  223. frequency(t_freq);
  224. begin (t_type) ;
  225. }
  226. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  227. arbdata = data;
  228. }
  229. void frequencyModulation(float octaves) {
  230. if (octaves > 12.0) {
  231. octaves = 12.0;
  232. } else if (octaves < 0.1) {
  233. octaves = 0.1;
  234. }
  235. modulation_factor = octaves * 4096.0;
  236. modulation_type = 0;
  237. }
  238. void phaseModulation(float degrees) {
  239. if (degrees > 9000.0) {
  240. degrees = 9000.0;
  241. } else if (degrees < 30.0) {
  242. degrees = 30.0;
  243. }
  244. modulation_factor = degrees * (65536.0 / 180.0);
  245. modulation_type = 1;
  246. }
  247. virtual void update(void);
  248. private:
  249. audio_block_t *inputQueueArray[2];
  250. uint32_t phase_accumulator;
  251. uint32_t phase_increment;
  252. uint32_t modulation_factor;
  253. int32_t magnitude;
  254. const int16_t *arbdata;
  255. uint32_t phasedata[AUDIO_BLOCK_SAMPLES];
  256. int16_t sample; // for WAVEFORM_SAMPLE_HOLD
  257. int16_t tone_offset;
  258. uint8_t tone_type;
  259. uint8_t modulation_type;
  260. BandLimitedWaveform band_limit_waveform ;
  261. };
  262. #endif