您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

synth_waveform.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. *bp++ = (int16_t) ((val * magnitude) >> 16) ;
  177. ph = new_ph ;
  178. }
  179. break;
  180. case WAVEFORM_SAMPLE_HOLD:
  181. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  182. *bp++ = sample;
  183. uint32_t newph = ph + inc;
  184. if (newph < ph) {
  185. sample = random(magnitude) - (magnitude >> 1);
  186. }
  187. ph = newph;
  188. }
  189. break;
  190. }
  191. phase_accumulator = ph - phase_offset;
  192. if (tone_offset) {
  193. bp = block->data;
  194. end = bp + AUDIO_BLOCK_SAMPLES;
  195. do {
  196. val1 = *bp;
  197. *bp++ = signed_saturate_rshift(val1 + tone_offset, 16, 0);
  198. } while (bp < end);
  199. }
  200. transmit(block, 0);
  201. release(block);
  202. }
  203. //--------------------------------------------------------------------------------
  204. void AudioSynthWaveformModulated::update(void)
  205. {
  206. audio_block_t *block, *moddata, *shapedata;
  207. int16_t *bp, *end;
  208. int32_t val1, val2;
  209. int16_t magnitude15;
  210. uint32_t i, ph, index, index2, scale, priorphase;
  211. const uint32_t inc = phase_increment;
  212. moddata = receiveReadOnly(0);
  213. shapedata = receiveReadOnly(1);
  214. // Pre-compute the phase angle for every output sample of this update
  215. ph = phase_accumulator;
  216. priorphase = phasedata[AUDIO_BLOCK_SAMPLES-1];
  217. if (moddata && modulation_type == 0) {
  218. // Frequency Modulation
  219. bp = moddata->data;
  220. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  221. int32_t n = (*bp++) * modulation_factor; // n is # of octaves to mod
  222. int32_t ipart = n >> 27; // 4 integer bits
  223. n &= 0x7FFFFFF; // 27 fractional bits
  224. #ifdef IMPROVE_EXPONENTIAL_ACCURACY
  225. // exp2 polynomial suggested by Stefan Stenzel on "music-dsp"
  226. // mail list, Wed, 3 Sep 2014 10:08:55 +0200
  227. int32_t x = n << 3;
  228. n = multiply_accumulate_32x32_rshift32_rounded(536870912, x, 1494202713);
  229. int32_t sq = multiply_32x32_rshift32_rounded(x, x);
  230. n = multiply_accumulate_32x32_rshift32_rounded(n, sq, 1934101615);
  231. n = n + (multiply_32x32_rshift32_rounded(sq,
  232. multiply_32x32_rshift32_rounded(x, 1358044250)) << 1);
  233. n = n << 1;
  234. #else
  235. // exp2 algorithm by Laurent de Soras
  236. // https://www.musicdsp.org/en/latest/Other/106-fast-exp2-approximation.html
  237. n = (n + 134217728) << 3;
  238. n = multiply_32x32_rshift32_rounded(n, n);
  239. n = multiply_32x32_rshift32_rounded(n, 715827883) << 3;
  240. n = n + 715827882;
  241. #endif
  242. uint32_t scale = n >> (14 - ipart);
  243. uint64_t phstep = (uint64_t)inc * scale;
  244. uint32_t phstep_msw = phstep >> 32;
  245. if (phstep_msw < 0x7FFE) {
  246. ph += phstep >> 16;
  247. } else {
  248. ph += 0x7FFE0000;
  249. }
  250. phasedata[i] = ph;
  251. }
  252. release(moddata);
  253. } else if (moddata) {
  254. // Phase Modulation
  255. bp = moddata->data;
  256. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  257. // more than +/- 180 deg shift by 32 bit overflow of "n"
  258. uint32_t n = ((uint32_t)(*bp++)) * modulation_factor;
  259. phasedata[i] = ph + n;
  260. ph += inc;
  261. }
  262. release(moddata);
  263. } else {
  264. // No Modulation Input
  265. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  266. phasedata[i] = ph;
  267. ph += inc;
  268. }
  269. }
  270. phase_accumulator = ph;
  271. // If the amplitude is zero, no output, but phase still increments properly
  272. if (magnitude == 0) {
  273. if (shapedata) release(shapedata);
  274. return;
  275. }
  276. block = allocate();
  277. if (!block) {
  278. if (shapedata) release(shapedata);
  279. return;
  280. }
  281. bp = block->data;
  282. // Now generate the output samples using the pre-computed phase angles
  283. switch(tone_type) {
  284. case WAVEFORM_SINE:
  285. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  286. ph = phasedata[i];
  287. index = ph >> 24;
  288. val1 = AudioWaveformSine[index];
  289. val2 = AudioWaveformSine[index+1];
  290. scale = (ph >> 8) & 0xFFFF;
  291. val2 *= scale;
  292. val1 *= 0x10000 - scale;
  293. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  294. }
  295. break;
  296. case WAVEFORM_ARBITRARY:
  297. if (!arbdata) {
  298. release(block);
  299. if (shapedata) release(shapedata);
  300. return;
  301. }
  302. // len = 256
  303. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  304. ph = phasedata[i];
  305. index = ph >> 24;
  306. index2 = index + 1;
  307. if (index2 >= 256) index2 = 0;
  308. val1 = *(arbdata + index);
  309. val2 = *(arbdata + index2);
  310. scale = (ph >> 8) & 0xFFFF;
  311. val2 *= scale;
  312. val1 *= 0x10000 - scale;
  313. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  314. }
  315. break;
  316. case WAVEFORM_PULSE:
  317. if (shapedata) {
  318. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  319. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  320. uint32_t width = ((shapedata->data[i] + 0x8000) & 0xFFFF) << 16;
  321. if (phasedata[i] < width) {
  322. *bp++ = magnitude15;
  323. } else {
  324. *bp++ = -magnitude15;
  325. }
  326. }
  327. break;
  328. } // else fall through to orginary square without shape modulation
  329. case WAVEFORM_SQUARE:
  330. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  331. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  332. if (phasedata[i] & 0x80000000) {
  333. *bp++ = -magnitude15;
  334. } else {
  335. *bp++ = magnitude15;
  336. }
  337. }
  338. break;
  339. case WAVEFORM_BANDLIMIT_PULSE:
  340. if (shapedata)
  341. {
  342. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++)
  343. {
  344. uint32_t width = ((shapedata->data[i] + 0x8000) & 0xFFFF) << 16;
  345. int32_t val = band_limit_waveform.generate_pulse (phasedata[i], width, i) ;
  346. *bp++ = (int16_t) ((val * magnitude) >> 16) ;
  347. }
  348. break;
  349. } // else fall through to orginary square without shape modulation
  350. case WAVEFORM_BANDLIMIT_SQUARE:
  351. for (i = 0 ; i < AUDIO_BLOCK_SAMPLES ; i++)
  352. {
  353. int32_t val = band_limit_waveform.generate_square (phasedata[i], i) ;
  354. *bp++ = (int16_t) ((val * magnitude) >> 16) ;
  355. }
  356. break;
  357. case WAVEFORM_SAWTOOTH:
  358. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  359. *bp++ = signed_multiply_32x16t(magnitude, phasedata[i]);
  360. }
  361. break;
  362. case WAVEFORM_SAWTOOTH_REVERSE:
  363. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  364. *bp++ = signed_multiply_32x16t(0xFFFFFFFFu - magnitude, phasedata[i]);
  365. }
  366. break;
  367. case WAVEFORM_BANDLIMIT_SAWTOOTH:
  368. case WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE:
  369. for (i = 0 ; i < AUDIO_BLOCK_SAMPLES ; i++)
  370. {
  371. int16_t val = band_limit_waveform.generate_sawtooth (phasedata[i], i) ;
  372. val = (int16_t) ((val * magnitude) >> 16) ;
  373. *bp++ = tone_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE ? (int16_t) -val : (int16_t) +val ;
  374. }
  375. break;
  376. case WAVEFORM_TRIANGLE_VARIABLE:
  377. if (shapedata) {
  378. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  379. uint32_t width = (shapedata->data[i] + 0x8000) & 0xFFFF;
  380. uint32_t rise = 0xFFFFFFFF / width;
  381. uint32_t fall = 0xFFFFFFFF / (0xFFFF - width);
  382. uint32_t halfwidth = width << 15;
  383. uint32_t n;
  384. ph = phasedata[i];
  385. if (ph < halfwidth) {
  386. n = (ph >> 16) * rise;
  387. *bp++ = ((n >> 16) * magnitude) >> 16;
  388. } else if (ph < 0xFFFFFFFF - halfwidth) {
  389. n = 0x7FFFFFFF - (((ph - halfwidth) >> 16) * fall);
  390. *bp++ = (((int32_t)n >> 16) * magnitude) >> 16;
  391. } else {
  392. n = ((ph + halfwidth) >> 16) * rise + 0x80000000;
  393. *bp++ = (((int32_t)n >> 16) * magnitude) >> 16;
  394. }
  395. ph += inc;
  396. }
  397. break;
  398. } // else fall through to orginary triangle without shape modulation
  399. case WAVEFORM_TRIANGLE:
  400. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  401. ph = phasedata[i];
  402. uint32_t phtop = ph >> 30;
  403. if (phtop == 1 || phtop == 2) {
  404. *bp++ = ((0xFFFF - (ph >> 15)) * magnitude) >> 16;
  405. } else {
  406. *bp++ = (((int32_t)ph >> 15) * magnitude) >> 16;
  407. }
  408. }
  409. break;
  410. case WAVEFORM_SAMPLE_HOLD:
  411. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  412. ph = phasedata[i];
  413. if (ph < priorphase) { // does not work for phase modulation
  414. sample = random(magnitude) - (magnitude >> 1);
  415. }
  416. priorphase = ph;
  417. *bp++ = sample;
  418. }
  419. break;
  420. }
  421. if (tone_offset) {
  422. bp = block->data;
  423. end = bp + AUDIO_BLOCK_SAMPLES;
  424. do {
  425. val1 = *bp;
  426. *bp++ = signed_saturate_rshift(val1 + tone_offset, 16, 0);
  427. } while (bp < end);
  428. }
  429. if (shapedata) release(shapedata);
  430. transmit(block, 0);
  431. release(block);
  432. }
  433. // BandLimitedWaveform
  434. #define SUPPORT_SHIFT 4
  435. #define SUPPORT (1 << SUPPORT_SHIFT)
  436. #define PTRMASK ((2 << SUPPORT_SHIFT) - 1)
  437. #define SCALE 16
  438. #define SCALE_MASK (SCALE-1)
  439. #define N (SCALE * SUPPORT * 2)
  440. #define GUARD_BITS 8
  441. #define GUARD (1 << GUARD_BITS)
  442. #define HALF_GUARD (1 << (GUARD_BITS-1))
  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. // create a new step, apply its past waveform into the cyclic sample buffer
  467. // and add a step_state object into active list so it can be added for the future samples
  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. // generate value for current sample from one active step, checking for the
  482. // dc_offset adjustment at the end of the table.
  483. int32_t BandLimitedWaveform::process_step (int i)
  484. {
  485. int off = states[i].offset ;
  486. bool positive = states[i].positive ;
  487. int32_t entry = lookup (off) ;
  488. off += SCALE<<GUARD_BITS ;
  489. states[i].offset = off ; // update offset in table for next sample
  490. if (off >= N<<GUARD_BITS) // at end of step table we alter dc_offset to extend the step into future
  491. dc_offset += positive ? 2*BASE_AMPLITUDE : -2*BASE_AMPLITUDE ;
  492. return positive ? entry : -entry ;
  493. }
  494. // process all active steps for current sample, basically generating the waveform portion
  495. // due only to steps
  496. // square waves use this directly.
  497. int32_t BandLimitedWaveform::process_active_steps (uint32_t new_phase)
  498. {
  499. int32_t sample = dc_offset ;
  500. int step_count = (newptr - delptr) & PTRMASK ;
  501. if (step_count > 0) // for any steps in-flight we sum in table entry and update its state
  502. {
  503. int i = newptr ;
  504. do
  505. {
  506. i = (i-1) & PTRMASK ;
  507. sample += process_step (i) ;
  508. } while (i != delptr) ;
  509. if (states[delptr].offset >= N<<GUARD_BITS) // remove any finished entries from the buffer.
  510. {
  511. delptr = (delptr+1) & PTRMASK ;
  512. // can be upto two steps per sample now for pulses
  513. if (newptr != delptr && states[delptr].offset >= N<<GUARD_BITS)
  514. delptr = (delptr+1) & PTRMASK ;
  515. }
  516. }
  517. return sample ;
  518. }
  519. // for sawtooth need to add in the slope and compensate for all the steps being one way
  520. int32_t BandLimitedWaveform::process_active_steps_saw (uint32_t new_phase)
  521. {
  522. int32_t sample = process_active_steps (new_phase) ;
  523. sample += (int16_t) ((((uint64_t)phase_word * (2*BASE_AMPLITUDE)) >> 32) - BASE_AMPLITUDE) ; // generate the sloped part of the wave
  524. if (new_phase < DEG180 && phase_word >= DEG180) // detect wrap around, correct dc offset
  525. dc_offset += 2*BASE_AMPLITUDE ;
  526. return sample ;
  527. }
  528. // for pulse need to adjust the baseline according to the pulse width to cancel the DC component.
  529. int32_t BandLimitedWaveform::process_active_steps_pulse (uint32_t new_phase, uint32_t pulse_width)
  530. {
  531. int32_t sample = process_active_steps (new_phase) ;
  532. return sample + BASE_AMPLITUDE/2 - pulse_width / (0x80000000u / BASE_AMPLITUDE) ; // correct DC offset for duty cycle
  533. }
  534. // Check for new steps using the phase update for the current sample for a square wave
  535. void BandLimitedWaveform::new_step_check_square (uint32_t new_phase, int i)
  536. {
  537. if (new_phase >= DEG180 && phase_word < DEG180) // detect falling step
  538. {
  539. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (sampled_width - phase_word) / (new_phase - phase_word)) ;
  540. if (offset == SCALE<<GUARD_BITS)
  541. offset -- ;
  542. if (pulse_state) // guard against two falling steps in a row (if pulse width changing for instance)
  543. {
  544. insert_step (- offset, false, i) ;
  545. pulse_state = false ;
  546. }
  547. }
  548. else if (new_phase < DEG180 && phase_word >= DEG180) // detect wrap around, rising step
  549. {
  550. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (- phase_word) / (new_phase - phase_word)) ;
  551. if (offset == SCALE<<GUARD_BITS)
  552. offset -- ;
  553. if (!pulse_state) // guard against two rising steps in a row (if pulse width changing for instance)
  554. {
  555. insert_step (- offset, true, i) ;
  556. pulse_state = true ;
  557. }
  558. }
  559. }
  560. // Checking for new steps for pulse waveform has to deal with changing frequency and pulse width and
  561. // not letting a pulse glitch out of existence as these change across a single period of the waveform
  562. // now we detect the rising edge just like for a square wave and use that to sample the pulse width
  563. // parameter, which then has to be checked against the instantaneous frequency every sample.
  564. void BandLimitedWaveform::new_step_check_pulse (uint32_t new_phase, uint32_t pulse_width, int i)
  565. {
  566. if (pulse_state && phase_word < sampled_width && (new_phase >= sampled_width || new_phase < phase_word)) // falling edge
  567. {
  568. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (sampled_width - phase_word) / (new_phase - phase_word)) ;
  569. if (offset == SCALE<<GUARD_BITS)
  570. offset -- ;
  571. insert_step (- offset, false, i) ;
  572. pulse_state = false ;
  573. }
  574. if ((!pulse_state) && phase_word >= DEG180 && new_phase < DEG180) // detect wrap around, rising step
  575. {
  576. // sample the pulse width value so its not changing under our feet later in cycle due to modulation
  577. sampled_width = pulse_width ;
  578. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (- phase_word) / (new_phase - phase_word)) ;
  579. if (offset == SCALE<<GUARD_BITS)
  580. offset -- ;
  581. insert_step (- offset, true, i) ;
  582. pulse_state = true ;
  583. if (pulse_state && new_phase >= sampled_width) // detect falling step directly after a rising edge
  584. //if (new_phase - sampled_width < DEG180) // detect falling step directly after a rising edge
  585. {
  586. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (sampled_width - phase_word) / (new_phase - phase_word)) ;
  587. if (offset == SCALE<<GUARD_BITS)
  588. offset -- ;
  589. insert_step (- offset, false, i) ;
  590. pulse_state = false ;
  591. }
  592. }
  593. }
  594. // new steps for sawtooth are at 180 degree point, always falling.
  595. void BandLimitedWaveform::new_step_check_saw (uint32_t new_phase, int i)
  596. {
  597. if (new_phase >= DEG180 && phase_word < DEG180) // detect falling step
  598. {
  599. int32_t offset = (int32_t) ((uint64_t) (SCALE<<GUARD_BITS) * (DEG180 - phase_word) / (new_phase - phase_word)) ;
  600. if (offset == SCALE<<GUARD_BITS)
  601. offset -- ;
  602. insert_step (- offset, false, i) ;
  603. }
  604. }
  605. // the generation function pushd new sample into cyclic buffer, having taken out the oldest entry
  606. // to return. The output is thus 16 samples behind, which allows the non-casual step function to
  607. // work in real time.
  608. int16_t BandLimitedWaveform::generate_sawtooth (uint32_t new_phase, int i)
  609. {
  610. new_step_check_saw (new_phase, i) ;
  611. int32_t val = process_active_steps_saw (new_phase) ;
  612. int16_t sample = (int16_t) cyclic [i&15] ;
  613. cyclic [i&15] = val ;
  614. phase_word = new_phase ;
  615. return sample ;
  616. }
  617. int16_t BandLimitedWaveform::generate_square (uint32_t new_phase, int i)
  618. {
  619. new_step_check_square (new_phase, i) ;
  620. int32_t val = process_active_steps (new_phase) ;
  621. int16_t sample = (int16_t) cyclic [i&15] ;
  622. cyclic [i&15] = val ;
  623. phase_word = new_phase ;
  624. return sample ;
  625. }
  626. int16_t BandLimitedWaveform::generate_pulse (uint32_t new_phase, uint32_t pulse_width, int i)
  627. {
  628. new_step_check_pulse (new_phase, pulse_width, i) ;
  629. int32_t val = process_active_steps_pulse (new_phase, pulse_width) ;
  630. int32_t sample = cyclic [i&15] ;
  631. cyclic [i&15] = val ;
  632. phase_word = new_phase ;
  633. return (int16_t) ((sample >> 1) - (sample >> 5)) ; // scale down to avoid overflow on narrow pulses, where the DC shift is big
  634. }
  635. void BandLimitedWaveform::init_sawtooth (uint32_t freq_word)
  636. {
  637. phase_word = 0 ;
  638. newptr = 0 ;
  639. delptr = 0 ;
  640. for (int i = 0 ; i < 2*SUPPORT ; i++)
  641. phase_word -= freq_word ;
  642. dc_offset = phase_word < DEG180 ? BASE_AMPLITUDE : -BASE_AMPLITUDE ;
  643. for (int i = 0 ; i < 2*SUPPORT ; i++)
  644. {
  645. uint32_t new_phase = phase_word + freq_word ;
  646. new_step_check_saw (new_phase, i) ;
  647. cyclic [i & 15] = (int16_t) process_active_steps_saw (new_phase) ;
  648. phase_word = new_phase ;
  649. }
  650. }
  651. void BandLimitedWaveform::init_square (uint32_t freq_word)
  652. {
  653. init_pulse (freq_word, DEG180) ;
  654. }
  655. void BandLimitedWaveform::init_pulse (uint32_t freq_word, uint32_t pulse_width)
  656. {
  657. phase_word = 0 ;
  658. sampled_width = pulse_width ;
  659. newptr = 0 ;
  660. delptr = 0 ;
  661. for (int i = 0 ; i < 2*SUPPORT ; i++)
  662. phase_word -= freq_word ;
  663. if (phase_word < pulse_width)
  664. {
  665. dc_offset = BASE_AMPLITUDE ;
  666. pulse_state = true ;
  667. }
  668. else
  669. {
  670. dc_offset = -BASE_AMPLITUDE ;
  671. pulse_state = false ;
  672. }
  673. for (int i = 0 ; i < 2*SUPPORT ; i++)
  674. {
  675. uint32_t new_phase = phase_word + freq_word ;
  676. new_step_check_pulse (new_phase, pulse_width, i) ;
  677. cyclic [i & 15] = (int16_t) process_active_steps_pulse (new_phase, pulse_width) ;
  678. phase_word = new_phase ;
  679. }
  680. }
  681. BandLimitedWaveform::BandLimitedWaveform()
  682. {
  683. newptr = 0 ;
  684. delptr = 0 ;
  685. dc_offset = BASE_AMPLITUDE ;
  686. phase_word = 0 ;
  687. }