Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

746 lines
21KB

  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. // uncomment for more accurate but more computationally expensive frequency modulation
  31. //#define IMPROVE_EXPONENTIAL_ACCURACY
  32. #define BASE_AMPLITUDE 0x6000 // 0x7fff won't work due to Gibb's phenomenon, so use 3/4 of full range.
  33. void AudioSynthWaveform::update(void)
  34. {
  35. audio_block_t *block;
  36. int16_t *bp, *end;
  37. int32_t val1, val2;
  38. int16_t magnitude15;
  39. uint32_t i, ph, index, index2, scale;
  40. const uint32_t inc = phase_increment;
  41. ph = phase_accumulator + phase_offset;
  42. if (magnitude == 0) {
  43. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  44. return;
  45. }
  46. block = allocate();
  47. if (!block) {
  48. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  49. return;
  50. }
  51. bp = block->data;
  52. switch(tone_type) {
  53. case WAVEFORM_SINE:
  54. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  55. index = ph >> 24;
  56. val1 = AudioWaveformSine[index];
  57. val2 = AudioWaveformSine[index+1];
  58. scale = (ph >> 8) & 0xFFFF;
  59. val2 *= scale;
  60. val1 *= 0x10000 - scale;
  61. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  62. ph += inc;
  63. }
  64. break;
  65. case WAVEFORM_ARBITRARY:
  66. if (!arbdata) {
  67. release(block);
  68. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  69. return;
  70. }
  71. // len = 256
  72. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  73. index = ph >> 24;
  74. index2 = index + 1;
  75. if (index2 >= 256) index2 = 0;
  76. val1 = *(arbdata + index);
  77. val2 = *(arbdata + index2);
  78. scale = (ph >> 8) & 0xFFFF;
  79. val2 *= scale;
  80. val1 *= 0x10000 - scale;
  81. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  82. ph += inc;
  83. }
  84. break;
  85. case WAVEFORM_SQUARE:
  86. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  87. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  88. if (ph & 0x80000000) {
  89. *bp++ = -magnitude15;
  90. } else {
  91. *bp++ = magnitude15;
  92. }
  93. ph += inc;
  94. }
  95. break;
  96. case WAVEFORM_BANDLIMIT_SQUARE:
  97. for (int i = 0 ; i < AUDIO_BLOCK_SAMPLES ; i++)
  98. {
  99. uint32_t new_ph = ph + inc ;
  100. int16_t val = band_limit_waveform.generate_square (new_ph, i) ;
  101. *bp++ = (val * magnitude) >> 16 ;
  102. ph = new_ph ;
  103. }
  104. break;
  105. case WAVEFORM_SAWTOOTH:
  106. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  107. *bp++ = signed_multiply_32x16t(magnitude, ph);
  108. ph += inc;
  109. }
  110. break;
  111. case WAVEFORM_SAWTOOTH_REVERSE:
  112. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  113. *bp++ = signed_multiply_32x16t(0xFFFFFFFFu - magnitude, ph);
  114. ph += inc;
  115. }
  116. break;
  117. case WAVEFORM_BANDLIMIT_SAWTOOTH:
  118. case WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE:
  119. for (i = 0 ; i < AUDIO_BLOCK_SAMPLES; i++)
  120. {
  121. uint32_t new_ph = ph + inc ;
  122. int16_t val = band_limit_waveform.generate_sawtooth (new_ph, i) ;
  123. if (tone_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE)
  124. *bp++ = (val * -magnitude) >> 16 ;
  125. else
  126. *bp++ = (val * magnitude) >> 16 ;
  127. ph = new_ph ;
  128. }
  129. break;
  130. case WAVEFORM_TRIANGLE:
  131. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  132. uint32_t phtop = ph >> 30;
  133. if (phtop == 1 || phtop == 2) {
  134. *bp++ = ((0xFFFF - (ph >> 15)) * magnitude) >> 16;
  135. } else {
  136. *bp++ = (((int32_t)ph >> 15) * magnitude) >> 16;
  137. }
  138. ph += inc;
  139. }
  140. break;
  141. case WAVEFORM_TRIANGLE_VARIABLE:
  142. do {
  143. uint32_t rise = 0xFFFFFFFF / (pulse_width >> 16);
  144. uint32_t fall = 0xFFFFFFFF / (0xFFFF - (pulse_width >> 16));
  145. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  146. if (ph < pulse_width/2) {
  147. uint32_t n = (ph >> 16) * rise;
  148. *bp++ = ((n >> 16) * magnitude) >> 16;
  149. } else if (ph < 0xFFFFFFFF - pulse_width/2) {
  150. uint32_t n = 0x7FFFFFFF - (((ph - pulse_width/2) >> 16) * fall);
  151. *bp++ = (((int32_t)n >> 16) * magnitude) >> 16;
  152. } else {
  153. uint32_t n = ((ph + pulse_width/2) >> 16) * rise + 0x80000000;
  154. *bp++ = (((int32_t)n >> 16) * magnitude) >> 16;
  155. }
  156. ph += inc;
  157. }
  158. } while (0);
  159. break;
  160. case WAVEFORM_PULSE:
  161. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  162. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  163. if (ph < pulse_width) {
  164. *bp++ = magnitude15;
  165. } else {
  166. *bp++ = -magnitude15;
  167. }
  168. ph += inc;
  169. }
  170. break;
  171. case WAVEFORM_BANDLIMIT_PULSE:
  172. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++)
  173. {
  174. int32_t new_ph = ph + inc ;
  175. int32_t val = band_limit_waveform.generate_pulse (new_ph, pulse_width, i) ;
  176. val += BASE_AMPLITUDE/2 - pulse_width / (0x100000000L / BASE_AMPLITUDE) ; // correct DC offset for duty cycle
  177. *bp++ = (int16_t) ((val * magnitude) >> 16) ;
  178. ph = new_ph ;
  179. }
  180. break;
  181. case WAVEFORM_SAMPLE_HOLD:
  182. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  183. *bp++ = sample;
  184. uint32_t newph = ph + inc;
  185. if (newph < ph) {
  186. sample = random(magnitude) - (magnitude >> 1);
  187. }
  188. ph = newph;
  189. }
  190. break;
  191. }
  192. phase_accumulator = ph - phase_offset;
  193. if (tone_offset) {
  194. bp = block->data;
  195. end = bp + AUDIO_BLOCK_SAMPLES;
  196. do {
  197. val1 = *bp;
  198. *bp++ = signed_saturate_rshift(val1 + tone_offset, 16, 0);
  199. } while (bp < end);
  200. }
  201. transmit(block, 0);
  202. release(block);
  203. }
  204. //--------------------------------------------------------------------------------
  205. void AudioSynthWaveformModulated::update(void)
  206. {
  207. audio_block_t *block, *moddata, *shapedata;
  208. int16_t *bp, *end;
  209. int32_t val1, val2;
  210. int16_t magnitude15;
  211. uint32_t i, ph, index, index2, scale, priorphase;
  212. const uint32_t inc = phase_increment;
  213. moddata = receiveReadOnly(0);
  214. shapedata = receiveReadOnly(1);
  215. // Pre-compute the phase angle for every output sample of this update
  216. ph = phase_accumulator;
  217. priorphase = phasedata[AUDIO_BLOCK_SAMPLES-1];
  218. if (moddata && modulation_type == 0) {
  219. // Frequency Modulation
  220. bp = moddata->data;
  221. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  222. int32_t n = (*bp++) * modulation_factor; // n is # of octaves to mod
  223. int32_t ipart = n >> 27; // 4 integer bits
  224. n &= 0x7FFFFFF; // 27 fractional bits
  225. #ifdef IMPROVE_EXPONENTIAL_ACCURACY
  226. // exp2 polynomial suggested by Stefan Stenzel on "music-dsp"
  227. // mail list, Wed, 3 Sep 2014 10:08:55 +0200
  228. int32_t x = n << 3;
  229. n = multiply_accumulate_32x32_rshift32_rounded(536870912, x, 1494202713);
  230. int32_t sq = multiply_32x32_rshift32_rounded(x, x);
  231. n = multiply_accumulate_32x32_rshift32_rounded(n, sq, 1934101615);
  232. n = n + (multiply_32x32_rshift32_rounded(sq,
  233. multiply_32x32_rshift32_rounded(x, 1358044250)) << 1);
  234. n = n << 1;
  235. #else
  236. // exp2 algorithm by Laurent de Soras
  237. // https://www.musicdsp.org/en/latest/Other/106-fast-exp2-approximation.html
  238. n = (n + 134217728) << 3;
  239. n = multiply_32x32_rshift32_rounded(n, n);
  240. n = multiply_32x32_rshift32_rounded(n, 715827883) << 3;
  241. n = n + 715827882;
  242. #endif
  243. uint32_t scale = n >> (14 - ipart);
  244. uint64_t phstep = (uint64_t)inc * scale;
  245. uint32_t phstep_msw = phstep >> 32;
  246. if (phstep_msw < 0x7FFE) {
  247. ph += phstep >> 16;
  248. } else {
  249. ph += 0x7FFE0000;
  250. }
  251. phasedata[i] = ph;
  252. }
  253. release(moddata);
  254. } else if (moddata) {
  255. // Phase Modulation
  256. bp = moddata->data;
  257. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  258. // more than +/- 180 deg shift by 32 bit overflow of "n"
  259. uint32_t n = (uint16_t)(*bp++) * modulation_factor;
  260. phasedata[i] = ph + n;
  261. ph += inc;
  262. }
  263. release(moddata);
  264. } else {
  265. // No Modulation Input
  266. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  267. phasedata[i] = ph;
  268. ph += inc;
  269. }
  270. }
  271. phase_accumulator = ph;
  272. // If the amplitude is zero, no output, but phase still increments properly
  273. if (magnitude == 0) {
  274. if (shapedata) release(shapedata);
  275. return;
  276. }
  277. block = allocate();
  278. if (!block) {
  279. if (shapedata) release(shapedata);
  280. return;
  281. }
  282. bp = block->data;
  283. // Now generate the output samples using the pre-computed phase angles
  284. switch(tone_type) {
  285. case WAVEFORM_SINE:
  286. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  287. ph = phasedata[i];
  288. index = ph >> 24;
  289. val1 = AudioWaveformSine[index];
  290. val2 = AudioWaveformSine[index+1];
  291. scale = (ph >> 8) & 0xFFFF;
  292. val2 *= scale;
  293. val1 *= 0x10000 - scale;
  294. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  295. }
  296. break;
  297. case WAVEFORM_ARBITRARY:
  298. if (!arbdata) {
  299. release(block);
  300. if (shapedata) release(shapedata);
  301. return;
  302. }
  303. // len = 256
  304. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  305. ph = phasedata[i];
  306. index = ph >> 24;
  307. index2 = index + 1;
  308. if (index2 >= 256) index2 = 0;
  309. val1 = *(arbdata + index);
  310. val2 = *(arbdata + index2);
  311. scale = (ph >> 8) & 0xFFFF;
  312. val2 *= scale;
  313. val1 *= 0x10000 - scale;
  314. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  315. }
  316. break;
  317. case WAVEFORM_PULSE:
  318. if (shapedata) {
  319. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  320. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  321. uint32_t width = ((shapedata->data[i] + 0x8000) & 0xFFFF) << 16;
  322. if (phasedata[i] < width) {
  323. *bp++ = magnitude15;
  324. } else {
  325. *bp++ = -magnitude15;
  326. }
  327. }
  328. break;
  329. } // else fall through to orginary square without shape modulation
  330. case WAVEFORM_SQUARE:
  331. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  332. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  333. if (phasedata[i] & 0x80000000) {
  334. *bp++ = -magnitude15;
  335. } else {
  336. *bp++ = magnitude15;
  337. }
  338. }
  339. break;
  340. case WAVEFORM_BANDLIMIT_PULSE:
  341. if (shapedata)
  342. {
  343. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++)
  344. {
  345. uint32_t width = ((shapedata->data[i] + 0x8000) & 0xFFFF) << 16;
  346. int32_t val = band_limit_waveform.generate_pulse (phasedata[i], width, i) ;
  347. val += BASE_AMPLITUDE/2 - width / (0x100000000L / BASE_AMPLITUDE) ; // correct DC offset for duty cycle
  348. *bp++ = (int16_t) ((val * magnitude) >> 16) ;
  349. }
  350. break;
  351. } // else fall through to orginary square without shape modulation
  352. case WAVEFORM_BANDLIMIT_SQUARE:
  353. for (i = 0 ; i < AUDIO_BLOCK_SAMPLES ; i++)
  354. {
  355. int32_t val = band_limit_waveform.generate_square (phasedata[i], i) ;
  356. *bp++ = (int16_t) ((val * magnitude) >> 16) ;
  357. }
  358. break;
  359. case WAVEFORM_SAWTOOTH:
  360. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  361. *bp++ = signed_multiply_32x16t(magnitude, phasedata[i]);
  362. }
  363. break;
  364. case WAVEFORM_SAWTOOTH_REVERSE:
  365. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  366. *bp++ = signed_multiply_32x16t(0xFFFFFFFFu - magnitude, phasedata[i]);
  367. }
  368. break;
  369. case WAVEFORM_BANDLIMIT_SAWTOOTH:
  370. case WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE:
  371. for (i = 0 ; i < AUDIO_BLOCK_SAMPLES ; i++)
  372. {
  373. int16_t val = band_limit_waveform.generate_sawtooth (phasedata[i], i) ;
  374. val = (int16_t) ((val * magnitude) >> 16) ;
  375. *bp++ = tone_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE ? (int16_t) -val : (int16_t) +val ;
  376. }
  377. break;
  378. case WAVEFORM_TRIANGLE_VARIABLE:
  379. if (shapedata) {
  380. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  381. uint32_t width = (shapedata->data[i] + 0x8000) & 0xFFFF;
  382. uint32_t rise = 0xFFFFFFFF / width;
  383. uint32_t fall = 0xFFFFFFFF / (0xFFFF - width);
  384. uint32_t halfwidth = width << 15;
  385. uint32_t n;
  386. ph = phasedata[i];
  387. if (ph < halfwidth) {
  388. n = (ph >> 16) * rise;
  389. *bp++ = ((n >> 16) * magnitude) >> 16;
  390. } else if (ph < 0xFFFFFFFF - halfwidth) {
  391. n = 0x7FFFFFFF - (((ph - halfwidth) >> 16) * fall);
  392. *bp++ = (((int32_t)n >> 16) * magnitude) >> 16;
  393. } else {
  394. n = ((ph + halfwidth) >> 16) * rise + 0x80000000;
  395. *bp++ = (((int32_t)n >> 16) * magnitude) >> 16;
  396. }
  397. ph += inc;
  398. }
  399. break;
  400. } // else fall through to orginary triangle without shape modulation
  401. case WAVEFORM_TRIANGLE:
  402. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  403. ph = phasedata[i];
  404. uint32_t phtop = ph >> 30;
  405. if (phtop == 1 || phtop == 2) {
  406. *bp++ = ((0xFFFF - (ph >> 15)) * magnitude) >> 16;
  407. } else {
  408. *bp++ = (((int32_t)ph >> 15) * magnitude) >> 16;
  409. }
  410. }
  411. break;
  412. case WAVEFORM_SAMPLE_HOLD:
  413. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  414. ph = phasedata[i];
  415. if (ph < priorphase) { // does not work for phase modulation
  416. sample = random(magnitude) - (magnitude >> 1);
  417. }
  418. priorphase = ph;
  419. *bp++ = sample;
  420. }
  421. break;
  422. }
  423. if (tone_offset) {
  424. bp = block->data;
  425. end = bp + AUDIO_BLOCK_SAMPLES;
  426. do {
  427. val1 = *bp;
  428. *bp++ = signed_saturate_rshift(val1 + tone_offset, 16, 0);
  429. } while (bp < end);
  430. }
  431. if (shapedata) release(shapedata);
  432. transmit(block, 0);
  433. release(block);
  434. }
  435. // BandLimitedWaveform
  436. #define SUPPORT_SHIFT 4
  437. #define SUPPORT (1 << SUPPORT_SHIFT)
  438. #define PTRMASK ((2 << SUPPORT_SHIFT) - 1)
  439. #define SCALE 16
  440. #define SCALE_MASK (SCALE-1)
  441. #define N (SCALE * SUPPORT * 2)
  442. #define GUARD_BITS 8
  443. #define GUARD (1 << GUARD_BITS)
  444. #define HALF_GUARD (1 << (GUARD_BITS-1))
  445. #define DEG180 0x80000000u
  446. #define PHASE_SCALE (0x100000000L / (2 * BASE_AMPLITUDE))
  447. extern "C"
  448. {
  449. extern const int16_t step_table [258] ;
  450. }
  451. int32_t BandLimitedWaveform::lookup (int offset)
  452. {
  453. int off = offset >> GUARD_BITS ;
  454. int frac = offset & (GUARD-1) ;
  455. int32_t a, b ;
  456. if (off < N/2) // handle odd symmetry by reflecting table
  457. {
  458. a = step_table [off+1] ;
  459. b = step_table [off+2] ;
  460. }
  461. else
  462. {
  463. a = - step_table [N-off] ;
  464. b = - step_table [N-off-1] ;
  465. }
  466. return BASE_AMPLITUDE + ((frac * b + (GUARD - frac) * a + HALF_GUARD) >> GUARD_BITS) ; // interpolated
  467. }
  468. void BandLimitedWaveform::insert_step (int offset, bool rising, int i)
  469. {
  470. while (offset <= (N/2-SCALE)<<GUARD_BITS)
  471. {
  472. if (offset >= 0)
  473. cyclic [i & 15] += rising ? lookup (offset) : -lookup (offset) ;
  474. offset += SCALE<<GUARD_BITS ;
  475. i ++ ;
  476. }
  477. states[newptr].offset = offset ;
  478. states[newptr].positive = rising ;
  479. newptr = (newptr+1) & PTRMASK ;
  480. }
  481. int32_t BandLimitedWaveform::process_step (int i)
  482. {
  483. int off = states[i].offset ;
  484. bool positive = states[i].positive ;
  485. int32_t entry = lookup (off) ;
  486. off += SCALE<<GUARD_BITS ;
  487. states[i].offset = off ; // update offset in table for next sample
  488. if (off >= N<<GUARD_BITS) // at end of step table we alter dc_offset to extend the step into future
  489. dc_offset += positive ? 2*BASE_AMPLITUDE : -2*BASE_AMPLITUDE ;
  490. return positive ? entry : -entry ;
  491. }
  492. int32_t BandLimitedWaveform::process_active_steps (uint32_t new_phase)
  493. {
  494. int32_t sample = dc_offset ;
  495. int step_count = (newptr - delptr) & PTRMASK ;
  496. if (step_count > 0) // for any steps in-flight we sum in table entry and update its state
  497. {
  498. int i = newptr ;
  499. do
  500. {
  501. i = (i-1) & PTRMASK ;
  502. sample += process_step (i) ;
  503. } while (i != delptr) ;
  504. if (states[delptr].offset >= N<<GUARD_BITS)
  505. delptr = (delptr+1) & PTRMASK ;
  506. }
  507. return sample ;
  508. }
  509. int32_t BandLimitedWaveform::process_active_steps_saw (uint32_t new_phase)
  510. {
  511. int32_t sample = process_active_steps (new_phase) ;
  512. sample += (int16_t) ((((uint64_t)phase_word * (2*BASE_AMPLITUDE)) >> 32) - BASE_AMPLITUDE) ; // generate the sloped part of the wave
  513. if (new_phase < DEG180 && phase_word >= DEG180) // detect wrap around, correct dc offset
  514. dc_offset += 2*BASE_AMPLITUDE ;
  515. return sample ;
  516. }
  517. void BandLimitedWaveform::new_step_check_square (uint32_t new_phase, int i)
  518. {
  519. if (new_phase >= DEG180 && phase_word < DEG180) // detect falling step
  520. {
  521. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (sampled_width - phase_word) / (new_phase - phase_word)) ;
  522. if (offset == SCALE<<GUARD_BITS)
  523. offset -- ;
  524. if (pulse_state) // guard against two falling steps in a row (if pulse width changing for instance)
  525. {
  526. insert_step (- offset, false, i) ;
  527. pulse_state = false ;
  528. }
  529. }
  530. else if (new_phase < DEG180 && phase_word >= DEG180) // detect wrap around, rising step
  531. {
  532. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (- phase_word) / (new_phase - phase_word)) ;
  533. if (offset == SCALE<<GUARD_BITS)
  534. offset -- ;
  535. if (!pulse_state) // guard against two rising steps in a row (if pulse width changing for instance)
  536. {
  537. insert_step (- offset, true, i) ;
  538. pulse_state = true ;
  539. }
  540. }
  541. }
  542. // Checking for new steps for pulse waveform has to deal with changing frequency and pulse width and
  543. // not letting a pulse glitch out of existence as these change across a single period of the waveform
  544. // now we detect the rising edge just like for a square wave and use that to sample the pulse width
  545. // parameter, which then has to be checked against the instantaneous frequency every sample.
  546. void BandLimitedWaveform::new_step_check_pulse (uint32_t new_phase, uint32_t pulse_width, int i)
  547. {
  548. uint32_t phase_advance = new_phase - phase_word ;
  549. // prevent pulses glitching away by enforcing 1 sample minimum pulse width.
  550. if (sampled_width < phase_advance)
  551. sampled_width = phase_advance ;
  552. else if (sampled_width > -phase_advance)
  553. sampled_width = -phase_advance ;
  554. if (new_phase < DEG180 && phase_word >= DEG180) // detect wrap around, rising step
  555. {
  556. // sample the pulse width value so its not changing under our feet later in cycle due to modulation
  557. sampled_width = pulse_width ;
  558. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (- phase_word) / phase_advance) ;
  559. if (offset == SCALE<<GUARD_BITS)
  560. offset -- ;
  561. if (!pulse_state) // guard against two rising steps in a row (if pulse width changing for instance)
  562. {
  563. insert_step (- offset, true, i) ;
  564. pulse_state = true ;
  565. }
  566. }
  567. else if (pulse_state && phase_word < sampled_width && new_phase >= sampled_width) // detect falling step
  568. {
  569. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (sampled_width - phase_word) / phase_advance) ;
  570. if (offset == SCALE<<GUARD_BITS)
  571. offset -- ;
  572. insert_step (- offset, false, i) ;
  573. pulse_state = false ;
  574. }
  575. }
  576. void BandLimitedWaveform::new_step_check_saw (uint32_t new_phase, int i)
  577. {
  578. if (new_phase >= DEG180 && phase_word < DEG180) // detect falling step
  579. {
  580. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (DEG180 - phase_word) / (new_phase - phase_word)) ;
  581. if (offset == SCALE<<GUARD_BITS)
  582. offset -- ;
  583. insert_step (- offset, false, i) ;
  584. }
  585. }
  586. int16_t BandLimitedWaveform::generate_sawtooth (uint32_t new_phase, int i)
  587. {
  588. new_step_check_saw (new_phase, i) ;
  589. int32_t val = process_active_steps_saw (new_phase) ;
  590. int16_t sample = (int16_t) cyclic [i&15] ;
  591. cyclic [i&15] = val ;
  592. phase_word = new_phase ;
  593. return sample ;
  594. }
  595. int16_t BandLimitedWaveform::generate_square (uint32_t new_phase, int i)
  596. {
  597. new_step_check_square (new_phase, i) ;
  598. int32_t val = process_active_steps (new_phase) ;
  599. int16_t sample = (int16_t) cyclic [i&15] ;
  600. cyclic [i&15] = val ;
  601. phase_word = new_phase ;
  602. return sample ;
  603. }
  604. int16_t BandLimitedWaveform::generate_pulse (uint32_t new_phase, uint32_t pulse_width, int i)
  605. {
  606. new_step_check_pulse (new_phase, pulse_width, i) ;
  607. int32_t val = process_active_steps (new_phase) ;
  608. int32_t sample = cyclic [i&15] ;
  609. cyclic [i&15] = val ;
  610. phase_word = new_phase ;
  611. return (int16_t) (sample >> 1) ; // scale down to avoid overflow on narrow pulses, where the DC shift is big
  612. }
  613. void BandLimitedWaveform::init_sawtooth (uint32_t freq_word)
  614. {
  615. phase_word = 0 ;
  616. newptr = 0 ;
  617. delptr = 0 ;
  618. for (int i = 0 ; i < 2*SUPPORT ; i++)
  619. phase_word -= freq_word ;
  620. dc_offset = phase_word < DEG180 ? BASE_AMPLITUDE : -BASE_AMPLITUDE ;
  621. for (int i = 0 ; i < 2*SUPPORT ; i++)
  622. {
  623. uint32_t new_phase = phase_word + freq_word ;
  624. new_step_check_saw (new_phase, i) ;
  625. cyclic [i & 15] = (int16_t) process_active_steps_saw (new_phase) ;
  626. phase_word = new_phase ;
  627. }
  628. }
  629. void BandLimitedWaveform::init_square (uint32_t freq_word)
  630. {
  631. init_pulse (freq_word, DEG180) ;
  632. }
  633. void BandLimitedWaveform::init_pulse (uint32_t freq_word, uint32_t pulse_width)
  634. {
  635. phase_word = 0 ;
  636. sampled_width = pulse_width ;
  637. newptr = 0 ;
  638. delptr = 0 ;
  639. for (int i = 0 ; i < 2*SUPPORT ; i++)
  640. phase_word -= freq_word ;
  641. if (phase_word < pulse_width)
  642. {
  643. dc_offset = BASE_AMPLITUDE ;
  644. pulse_state = true ;
  645. }
  646. else
  647. {
  648. dc_offset = -BASE_AMPLITUDE ;
  649. pulse_state = false ;
  650. }
  651. for (int i = 0 ; i < 2*SUPPORT ; i++)
  652. {
  653. uint32_t new_phase = phase_word + freq_word ;
  654. new_step_check_pulse (new_phase, pulse_width, i) ;
  655. cyclic [i & 15] = (int16_t) process_active_steps (new_phase) ;
  656. phase_word = new_phase ;
  657. }
  658. }
  659. BandLimitedWaveform::BandLimitedWaveform()
  660. {
  661. newptr = 0 ;
  662. delptr = 0 ;
  663. dc_offset = BASE_AMPLITUDE ;
  664. phase_word = 0 ;
  665. }