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.

389 rindas
10KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2018, 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. #include <Arduino.h>
  27. #include "synth_waveform.h"
  28. #include "arm_math.h"
  29. #include "utility/dspinst.h"
  30. void AudioSynthWaveform::update(void)
  31. {
  32. audio_block_t *block;
  33. int16_t *bp, *end;
  34. int32_t val1, val2;
  35. int16_t magnitude15;
  36. uint32_t i, ph, index, index2, scale;
  37. const uint32_t inc = phase_increment;
  38. ph = phase_accumulator + phase_offset;
  39. if (magnitude == 0) {
  40. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  41. return;
  42. }
  43. block = allocate();
  44. if (!block) {
  45. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  46. return;
  47. }
  48. bp = block->data;
  49. switch(tone_type) {
  50. case WAVEFORM_SINE:
  51. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  52. index = ph >> 24;
  53. val1 = AudioWaveformSine[index];
  54. val2 = AudioWaveformSine[index+1];
  55. scale = (ph >> 8) & 0xFFFF;
  56. val2 *= scale;
  57. val1 *= 0x10000 - scale;
  58. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  59. ph += inc;
  60. }
  61. break;
  62. case WAVEFORM_ARBITRARY:
  63. if (!arbdata) {
  64. release(block);
  65. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  66. return;
  67. }
  68. // len = 256
  69. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  70. index = ph >> 24;
  71. index2 = index + 1;
  72. if (index2 >= 256) index2 = 0;
  73. val1 = *(arbdata + index);
  74. val2 = *(arbdata + index2);
  75. scale = (ph >> 8) & 0xFFFF;
  76. val2 *= scale;
  77. val1 *= 0x10000 - scale;
  78. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  79. ph += inc;
  80. }
  81. break;
  82. case WAVEFORM_SQUARE:
  83. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  84. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  85. if (ph & 0x80000000) {
  86. *bp++ = -magnitude15;
  87. } else {
  88. *bp++ = magnitude15;
  89. }
  90. ph += inc;
  91. }
  92. break;
  93. case WAVEFORM_SAWTOOTH:
  94. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  95. *bp++ = signed_multiply_32x16t(magnitude, ph);
  96. ph += inc;
  97. }
  98. break;
  99. case WAVEFORM_SAWTOOTH_REVERSE:
  100. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  101. *bp++ = signed_multiply_32x16t(0xFFFFFFFFu - magnitude, ph);
  102. ph += inc;
  103. }
  104. break;
  105. case WAVEFORM_TRIANGLE:
  106. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  107. uint32_t phtop = ph >> 30;
  108. if (phtop == 1 || phtop == 2) {
  109. *bp++ = ((0xFFFF - (ph >> 15)) * magnitude) >> 16;
  110. } else {
  111. *bp++ = ((ph >> 15) * magnitude) >> 16;
  112. }
  113. ph += inc;
  114. }
  115. break;
  116. case WAVEFORM_TRIANGLE_VARIABLE:
  117. do {
  118. uint32_t rise = 0xFFFFFFFF / (pulse_width >> 16);
  119. uint32_t fall = 0xFFFFFFFF / (0xFFFF - (pulse_width >> 16));
  120. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  121. if (ph < pulse_width/2) {
  122. uint32_t n = (ph >> 16) * rise;
  123. *bp++ = ((n >> 16) * magnitude) >> 16;
  124. } else if (ph < 0xFFFFFFFF - pulse_width/2) {
  125. uint32_t n = 0x7FFFFFFF - (((ph - pulse_width/2) >> 16) * fall);
  126. *bp++ = ((n >> 16) * magnitude) >> 16;
  127. } else {
  128. uint32_t n = ((ph + pulse_width/2) >> 16) * rise + 0x80000000;
  129. *bp++ = ((n >> 16) * magnitude) >> 16;
  130. }
  131. ph += inc;
  132. }
  133. } while (0);
  134. break;
  135. case WAVEFORM_PULSE:
  136. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  137. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  138. if (ph < pulse_width) {
  139. *bp++ = magnitude15;
  140. } else {
  141. *bp++ = -magnitude15;
  142. }
  143. ph += inc;
  144. }
  145. break;
  146. case WAVEFORM_SAMPLE_HOLD:
  147. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  148. *bp++ = sample;
  149. uint32_t newph = ph + inc;
  150. if (newph < ph) {
  151. sample = random(magnitude) - (magnitude >> 1);
  152. }
  153. ph = newph;
  154. }
  155. break;
  156. }
  157. phase_accumulator = ph - phase_offset;
  158. if (tone_offset) {
  159. bp = block->data;
  160. end = bp + AUDIO_BLOCK_SAMPLES;
  161. do {
  162. val1 = *bp;
  163. *bp++ = signed_saturate_rshift(val1 + tone_offset, 16, 0);
  164. } while (bp < end);
  165. }
  166. transmit(block, 0);
  167. release(block);
  168. }
  169. //--------------------------------------------------------------------------------
  170. void AudioSynthWaveformModulated::update(void)
  171. {
  172. audio_block_t *block, *moddata, *shapedata;
  173. int16_t *bp, *end;
  174. int32_t val1, val2;
  175. int16_t magnitude15;
  176. uint32_t i, ph, index, index2, scale, priorphase;
  177. const uint32_t inc = phase_increment;
  178. moddata = receiveReadOnly(0);
  179. shapedata = receiveReadOnly(1);
  180. ph = phase_accumulator;
  181. priorphase = phasedata[AUDIO_BLOCK_SAMPLES-1];
  182. if (moddata && modulation_type == 0) {
  183. // Frequency Modulation
  184. bp = moddata->data;
  185. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  186. int32_t n = (*bp++) * modulation_factor; // n is # of octaves to mod
  187. int32_t ipart = n >> 27; // 4 integer bits
  188. n &= 0x7FFFFFF; // 27 fractional bits
  189. #ifdef IMPROVE_EXPONENTIAL_ACCURACY
  190. // exp2 polynomial suggested by Stefan Stenzel on "music-dsp"
  191. // mail list, Wed, 3 Sep 2014 10:08:55 +0200
  192. int32_t x = n << 3;
  193. n = multiply_accumulate_32x32_rshift32_rounded(536870912, x, 1494202713);
  194. int32_t sq = multiply_32x32_rshift32_rounded(x, x);
  195. n = multiply_accumulate_32x32_rshift32_rounded(n, sq, 1934101615);
  196. n = n + (multiply_32x32_rshift32_rounded(sq,
  197. multiply_32x32_rshift32_rounded(x, 1358044250)) << 1);
  198. #else
  199. // exp2 algorithm by Laurent de Soras
  200. // http://www.musicdsp.org/showone.php?id=106
  201. n = (n + 134217728) << 3;
  202. n = multiply_32x32_rshift32_rounded(n, n);
  203. n = multiply_32x32_rshift32_rounded(n, 715827883) << 3;
  204. n = n + 715827882;
  205. #endif
  206. uint32_t scale = n >> (14 - ipart);
  207. uint32_t phinc = ((uint64_t)inc * scale) >> 16; // TODO: saturate 31 bits??
  208. ph += phinc;
  209. phasedata[i] = ph;
  210. }
  211. release(moddata);
  212. } else if (moddata) {
  213. // Phase Modulation
  214. // TODO.....
  215. release(moddata);
  216. } else {
  217. // No Modulation Input
  218. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  219. phasedata[i] = ph;
  220. ph += inc;
  221. }
  222. }
  223. phase_accumulator = ph;
  224. if (magnitude == 0) {
  225. if (shapedata) release(shapedata);
  226. return;
  227. }
  228. block = allocate();
  229. if (!block) {
  230. if (shapedata) release(shapedata);
  231. return;
  232. }
  233. bp = block->data;
  234. switch(tone_type) {
  235. case WAVEFORM_SINE:
  236. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  237. ph = phasedata[i];
  238. index = ph >> 24;
  239. val1 = AudioWaveformSine[index];
  240. val2 = AudioWaveformSine[index+1];
  241. scale = (ph >> 8) & 0xFFFF;
  242. val2 *= scale;
  243. val1 *= 0x10000 - scale;
  244. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  245. }
  246. break;
  247. case WAVEFORM_ARBITRARY:
  248. if (!arbdata) {
  249. release(block);
  250. if (shapedata) release(shapedata);
  251. return;
  252. }
  253. // len = 256
  254. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  255. ph = phasedata[i];
  256. index = ph >> 24;
  257. index2 = index + 1;
  258. if (index2 >= 256) index2 = 0;
  259. val1 = *(arbdata + index);
  260. val2 = *(arbdata + index2);
  261. scale = (ph >> 8) & 0xFFFF;
  262. val2 *= scale;
  263. val1 *= 0x10000 - scale;
  264. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  265. }
  266. break;
  267. case WAVEFORM_PULSE:
  268. if (shapedata) {
  269. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  270. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  271. uint32_t width = ((shapedata->data[i] + 0x8000) & 0xFFFF) << 16;
  272. if (phasedata[i] < width) {
  273. *bp++ = magnitude15;
  274. } else {
  275. *bp++ = -magnitude15;
  276. }
  277. }
  278. break;
  279. } // else fall through to orginary square without shape modulation
  280. case WAVEFORM_SQUARE:
  281. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  282. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  283. if (phasedata[i] & 0x80000000) {
  284. *bp++ = -magnitude15;
  285. } else {
  286. *bp++ = magnitude15;
  287. }
  288. }
  289. break;
  290. case WAVEFORM_SAWTOOTH:
  291. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  292. *bp++ = signed_multiply_32x16t(magnitude, phasedata[i]);
  293. }
  294. break;
  295. case WAVEFORM_SAWTOOTH_REVERSE:
  296. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  297. *bp++ = signed_multiply_32x16t(0xFFFFFFFFu - magnitude, phasedata[i]);
  298. }
  299. break;
  300. case WAVEFORM_TRIANGLE_VARIABLE:
  301. if (shapedata) {
  302. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  303. uint32_t width = (shapedata->data[i] + 0x8000) & 0xFFFF;
  304. uint32_t rise = 0xFFFFFFFF / width;
  305. uint32_t fall = 0xFFFFFFFF / (0xFFFF - width);
  306. width = width << 15;
  307. uint32_t n;
  308. ph = phasedata[i];
  309. if (ph < width) {
  310. n = (ph >> 16) * rise;
  311. *bp++ = ((n >> 16) * magnitude) >> 16;
  312. } else if (ph < 0xFFFFFFFF - width) {
  313. n = 0x7FFFFFFF - (((ph - width) >> 16) * fall);
  314. *bp++ = ((n >> 16) * magnitude) >> 16;
  315. } else {
  316. n = ((ph + width) >> 16) * rise + 0x80000000;
  317. *bp++ = ((n >> 16) * magnitude) >> 16;
  318. }
  319. ph += inc;
  320. }
  321. break;
  322. } // else fall through to orginary triangle without shape modulation
  323. case WAVEFORM_TRIANGLE:
  324. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  325. ph = phasedata[i];
  326. uint32_t phtop = ph >> 30;
  327. if (phtop == 1 || phtop == 2) {
  328. *bp++ = ((0xFFFF - (ph >> 15)) * magnitude) >> 16;
  329. } else {
  330. *bp++ = ((ph >> 15) * magnitude) >> 16;
  331. }
  332. }
  333. break;
  334. case WAVEFORM_SAMPLE_HOLD:
  335. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  336. ph = phasedata[i];
  337. if (ph < priorphase) {
  338. sample = random(magnitude) - (magnitude >> 1);
  339. }
  340. priorphase = ph;
  341. *bp++ = sample;
  342. }
  343. break;
  344. }
  345. if (tone_offset) {
  346. bp = block->data;
  347. end = bp + AUDIO_BLOCK_SAMPLES;
  348. do {
  349. val1 = *bp;
  350. *bp++ = signed_saturate_rshift(val1 + tone_offset, 16, 0);
  351. } while (bp < end);
  352. }
  353. if (shapedata) release(shapedata);
  354. transmit(block, 0);
  355. release(block);
  356. }