Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

689 lines
19KB

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