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.

660 rindas
18KB

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