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

2200 行
53KB

  1. #include "Audio.h"
  2. #include "arm_math.h"
  3. static arm_cfft_radix4_instance_q15 fft_inst;
  4. void AudioAnalyzeFFT256::init(void)
  5. {
  6. // TODO: replace this with static const version
  7. arm_cfft_radix4_init_q15(&fft_inst, 256, 0, 1);
  8. //for (int i=0; i<2048; i++) {
  9. //buffer[i] = i * 3;
  10. //}
  11. //__disable_irq();
  12. //ARM_DEMCR |= ARM_DEMCR_TRCENA;
  13. //ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  14. //uint32_t n = ARM_DWT_CYCCNT;
  15. //arm_cfft_radix2_q15(&fft_inst, buffer);
  16. //n = ARM_DWT_CYCCNT - n;
  17. //__enable_irq();
  18. //cycles = n;
  19. //arm_cmplx_mag_q15(buffer, buffer, 512);
  20. // each audio block is 278525 cycles @ 96 MHz
  21. // 256 point fft2 takes 65408 cycles
  22. // 256 point fft4 takes 49108 cycles
  23. // 128 point cmag takes 10999 cycles
  24. // 1024 point fft2 takes 125948 cycles
  25. // 1024 point fft4 takes 125840 cycles
  26. // 512 point cmag takes 43764 cycles
  27. //state = 0;
  28. //outputflag = false;
  29. }
  30. static void copy_to_fft_buffer(void *destination, const void *source)
  31. {
  32. const int16_t *src = (const int16_t *)source;
  33. int16_t *dst = (int16_t *)destination;
  34. // TODO: optimize this
  35. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  36. *dst++ = *src++; // real
  37. *dst++ = 0; // imaginary
  38. }
  39. }
  40. // computes limit((val >> rshift), 2**bits)
  41. static inline int32_t signed_saturate_rshift(int32_t val, int bits, int rshift) __attribute__((always_inline));
  42. static inline int32_t signed_saturate_rshift(int32_t val, int bits, int rshift)
  43. {
  44. int32_t out;
  45. asm volatile("ssat %0, %1, %2, asr %3" : "=r" (out) : "I" (bits), "r" (val), "I" (rshift));
  46. return out;
  47. }
  48. static void apply_window_to_fft_buffer(void *buffer, const void *window)
  49. {
  50. int16_t *buf = (int16_t *)buffer;
  51. const int16_t *win = (int16_t *)window;;
  52. for (int i=0; i < 256; i++) {
  53. int32_t val = *buf * *win++;
  54. //*buf = signed_saturate_rshift(val, 16, 15);
  55. *buf = val >> 15;
  56. buf += 2;
  57. }
  58. }
  59. void AudioAnalyzeFFT256::update(void)
  60. {
  61. audio_block_t *block;
  62. block = receiveReadOnly();
  63. if (!block) return;
  64. if (!prevblock) {
  65. prevblock = block;
  66. return;
  67. }
  68. copy_to_fft_buffer(buffer, prevblock->data);
  69. copy_to_fft_buffer(buffer+256, block->data);
  70. //window = AudioWindowBlackmanNuttall256;
  71. //window = NULL;
  72. if (window) apply_window_to_fft_buffer(buffer, window);
  73. arm_cfft_radix4_q15(&fft_inst, buffer);
  74. // TODO: is this averaging correct? G. Heinzel's paper says we're
  75. // supposed to average the magnitude squared, then do the square
  76. // root at the end after dividing by naverage.
  77. arm_cmplx_mag_q15(buffer, buffer, 128);
  78. if (count == 0) {
  79. for (int i=0; i < 128; i++) {
  80. output[i] = buffer[i];
  81. }
  82. } else {
  83. for (int i=0; i < 128; i++) {
  84. output[i] += buffer[i];
  85. }
  86. }
  87. if (++count == naverage) {
  88. count = 0;
  89. for (int i=0; i < 128; i++) {
  90. output[i] /= naverage;
  91. }
  92. outputflag = true;
  93. }
  94. release(prevblock);
  95. prevblock = block;
  96. #if 0
  97. block = receiveReadOnly();
  98. if (state == 0) {
  99. //Serial.print("0");
  100. if (block == NULL) return;
  101. copy_to_fft_buffer(buffer, block->data);
  102. state = 1;
  103. } else if (state == 1) {
  104. //Serial.print("1");
  105. if (block == NULL) return;
  106. copy_to_fft_buffer(buffer+256, block->data);
  107. arm_cfft_radix4_q15(&fft_inst, buffer);
  108. state = 2;
  109. } else {
  110. //Serial.print("2");
  111. arm_cmplx_mag_q15(buffer, output, 128);
  112. outputflag = true;
  113. if (block == NULL) return;
  114. copy_to_fft_buffer(buffer, block->data);
  115. state = 1;
  116. }
  117. release(block);
  118. #endif
  119. }
  120. #ifdef ORIGINAL_AUDIOSYNTHWAVEFORM
  121. /******************************************************************/
  122. // PAH - add ramp-up and ramp-down to the onset of the wave
  123. // the length is specified in samples
  124. void AudioSynthWaveform::set_ramp_length(uint16_t r_length)
  125. {
  126. if(r_length < 0) {
  127. ramp_length = 0;
  128. return;
  129. }
  130. // Don't set the ramp length longer than about 4 milliseconds
  131. if(r_length > 44*4) {
  132. ramp_length = 44*4;
  133. return;
  134. }
  135. ramp_length = r_length;
  136. }
  137. void AudioSynthWaveform::update(void)
  138. {
  139. audio_block_t *block;
  140. uint32_t i, ph, inc, index, scale;
  141. int32_t val1, val2, val3;
  142. //Serial.println("AudioSynthWaveform::update");
  143. if (((magnitude > 0) || ramp_down) && (block = allocate()) != NULL) {
  144. ph = phase;
  145. inc = phase_increment;
  146. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  147. index = ph >> 24;
  148. val1 = wavetable[index];
  149. val2 = wavetable[index+1];
  150. scale = (ph >> 8) & 0xFFFF;
  151. val2 *= scale;
  152. val1 *= 0xFFFF - scale;
  153. val3 = (val1 + val2) >> 16;
  154. // The value of ramp_up is always initialized to RAMP_LENGTH and then is
  155. // decremented each time through here until it reaches zero.
  156. // The value of ramp_up is used to generate a Q15 fraction which varies
  157. // from [0 - 1), and multiplies this by the current sample
  158. if(ramp_up) {
  159. // ramp up to the new magnitude
  160. // ramp_mag is the Q15 representation of the fraction
  161. // Since ramp_up can't be zero, this cannot generate +1
  162. ramp_mag = ((ramp_length-ramp_up)<<15)/ramp_length;
  163. ramp_up--;
  164. block->data[i] = (val3 * ((ramp_mag * magnitude)>>15)) >> 15;
  165. } else if(ramp_down) {
  166. // ramp down to zero from the last magnitude
  167. // The value of ramp_down is always initialized to RAMP_LENGTH and then is
  168. // decremented each time through here until it reaches zero.
  169. // The value of ramp_down is used to generate a Q15 fraction which varies
  170. // from (1 - 0], and multiplies this by the current sample
  171. // avoid RAMP_LENGTH/RAMP_LENGTH because Q15 format
  172. // cannot represent +1
  173. ramp_mag = ((ramp_down - 1)<<15)/ramp_length;
  174. ramp_down--;
  175. block->data[i] = (val3 * ((ramp_mag * last_magnitude)>>15)) >> 15;
  176. } else {
  177. block->data[i] = (val3 * magnitude) >> 15;
  178. }
  179. //Serial.print(block->data[i]);
  180. //Serial.print(", ");
  181. //if ((i % 12) == 11) Serial.println();
  182. ph += inc;
  183. }
  184. //Serial.println();
  185. phase = ph;
  186. transmit(block);
  187. release(block);
  188. } else {
  189. // is this numerical overflow ok?
  190. phase += phase_increment * AUDIO_BLOCK_SAMPLES;
  191. }
  192. }
  193. #else
  194. /******************************************************************/
  195. // PAH - add ramp-up and ramp-down to the onset of the wave
  196. // the length is specified in samples
  197. void AudioSynthWaveform::set_ramp_length(uint16_t r_length)
  198. {
  199. if(r_length < 0) {
  200. ramp_length = 0;
  201. return;
  202. }
  203. // Don't set the ramp length longer than about 4 milliseconds
  204. if(r_length > 44*4) {
  205. ramp_length = 44*4;
  206. return;
  207. }
  208. ramp_length = r_length;
  209. }
  210. boolean AudioSynthWaveform::begin(float t_amp,int t_hi,short type)
  211. {
  212. tone_type = type;
  213. // tone_amp = t_amp;
  214. amplitude(t_amp);
  215. tone_freq = t_hi;
  216. if(t_hi < 1)return false;
  217. if(t_hi >= AUDIO_SAMPLE_RATE_EXACT/2)return false;
  218. tone_phase = 0;
  219. tone_incr = (0x100000000LL*t_hi)/AUDIO_SAMPLE_RATE_EXACT;
  220. if(0) {
  221. Serial.print("AudioSynthWaveform.begin(tone_amp = ");
  222. Serial.print(t_amp);
  223. Serial.print(", tone_hi = ");
  224. Serial.print(t_hi);
  225. Serial.print(", tone_incr = ");
  226. Serial.print(tone_incr,HEX);
  227. // Serial.print(", tone_hi = ");
  228. // Serial.print(t_hi);
  229. Serial.println(")");
  230. }
  231. return(true);
  232. }
  233. void AudioSynthWaveform::update(void)
  234. {
  235. audio_block_t *block;
  236. short *bp;
  237. // temporary for ramp in sine
  238. uint32_t ramp_mag;
  239. // temporaries for TRIANGLE
  240. uint32_t mag;
  241. short tmp_amp;
  242. if(tone_freq == 0)return;
  243. // L E F T C H A N N E L O N L Y
  244. block = allocate();
  245. if(block) {
  246. bp = block->data;
  247. switch(tone_type) {
  248. case TONE_TYPE_SINE:
  249. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  250. // The value of ramp_up is always initialized to RAMP_LENGTH and then is
  251. // decremented each time through here until it reaches zero.
  252. // The value of ramp_up is used to generate a Q15 fraction which varies
  253. // from [0 - 1), and multiplies this by the current sample
  254. if(ramp_up) {
  255. // ramp up to the new magnitude
  256. // ramp_mag is the Q15 representation of the fraction
  257. // Since ramp_up can't be zero, this cannot generate +1
  258. ramp_mag = ((ramp_length-ramp_up)<<15)/ramp_length;
  259. ramp_up--;
  260. // adjust tone_phase to Q15 format and then adjust the result
  261. // of the multiplication
  262. *bp = (short)((arm_sin_q15(tone_phase>>17) * tone_amp) >> 15);
  263. *bp++ = (*bp * ramp_mag)>>15;
  264. }
  265. else if(ramp_down) {
  266. // ramp down to zero from the last magnitude
  267. // The value of ramp_down is always initialized to RAMP_LENGTH and then is
  268. // decremented each time through here until it reaches zero.
  269. // The value of ramp_down is used to generate a Q15 fraction which varies
  270. // from (1 - 0], and multiplies this by the current sample
  271. // avoid RAMP_LENGTH/RAMP_LENGTH because Q15 format
  272. // cannot represent +1
  273. ramp_mag = ((ramp_down - 1)<<15)/ramp_length;
  274. ramp_down--;
  275. // adjust tone_phase to Q15 format and then adjust the result
  276. // of the multiplication
  277. *bp = (short)((arm_sin_q15(tone_phase>>17) * last_tone_amp) >> 15);
  278. *bp++ = (*bp * ramp_mag)>>15;
  279. } else {
  280. // adjust tone_phase to Q15 format and then adjust the result
  281. // of the multiplication
  282. *bp++ = (short)((arm_sin_q15(tone_phase>>17) * tone_amp) >> 15);
  283. }
  284. // phase and incr are both unsigned 32-bit fractions
  285. tone_phase += tone_incr;
  286. }
  287. break;
  288. case TONE_TYPE_SQUARE:
  289. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  290. if(tone_phase & 0x80000000)*bp++ = -tone_amp;
  291. else *bp++ = tone_amp;
  292. // phase and incr are both unsigned 32-bit fractions
  293. tone_phase += tone_incr;
  294. }
  295. break;
  296. case TONE_TYPE_SAWTOOTH:
  297. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  298. *bp = ((short)(tone_phase>>16)*tone_amp) >> 15;
  299. bp++;
  300. // phase and incr are both unsigned 32-bit fractions
  301. tone_phase += tone_incr;
  302. }
  303. break;
  304. case TONE_TYPE_TRIANGLE:
  305. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  306. if(tone_phase & 0x80000000) {
  307. // negative half-cycle
  308. tmp_amp = -tone_amp;
  309. }
  310. else {
  311. // positive half-cycle
  312. tmp_amp = tone_amp;
  313. }
  314. mag = tone_phase << 2;
  315. // Determine which quadrant
  316. if(tone_phase & 0x40000000) {
  317. // negate the magnitude
  318. mag = ~mag + 1;
  319. }
  320. *bp++ = ((short)(mag>>17)*tmp_amp) >> 15;
  321. tone_phase += tone_incr;
  322. }
  323. break;
  324. }
  325. // send the samples to the left channel
  326. transmit(block,0);
  327. release(block);
  328. }
  329. }
  330. #endif
  331. #if 0
  332. void AudioSineWaveMod::frequency(float f)
  333. {
  334. if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || f < 0.0) return;
  335. phase_increment = (f / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  336. }
  337. void AudioSineWaveMod::update(void)
  338. {
  339. audio_block_t *block, *modinput;
  340. uint32_t i, ph, inc, index, scale;
  341. int32_t val1, val2;
  342. //Serial.println("AudioSineWave::update");
  343. modinput = receiveReadOnly();
  344. ph = phase;
  345. inc = phase_increment;
  346. block = allocate();
  347. if (!block) {
  348. // unable to allocate memory, so we'll send nothing
  349. if (modinput) {
  350. // but if we got modulation data, update the phase
  351. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  352. ph += inc + modinput->data[i] * modulation_factor;
  353. }
  354. release(modinput);
  355. } else {
  356. ph += phase_increment * AUDIO_BLOCK_SAMPLES;
  357. }
  358. phase = ph;
  359. return;
  360. }
  361. if (modinput) {
  362. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  363. index = ph >> 24;
  364. val1 = sine_table[index];
  365. val2 = sine_table[index+1];
  366. scale = (ph >> 8) & 0xFFFF;
  367. val2 *= scale;
  368. val1 *= 0xFFFF - scale;
  369. block->data[i] = (val1 + val2) >> 16;
  370. //Serial.print(block->data[i]);
  371. //Serial.print(", ");
  372. //if ((i % 12) == 11) Serial.println();
  373. ph += inc + modinput->data[i] * modulation_factor;
  374. }
  375. release(modinput);
  376. } else {
  377. ph = phase;
  378. inc = phase_increment;
  379. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  380. index = ph >> 24;
  381. val1 = sine_table[index];
  382. val2 = sine_table[index+1];
  383. scale = (ph >> 8) & 0xFFFF;
  384. val2 *= scale;
  385. val1 *= 0xFFFF - scale;
  386. block->data[i] = (val1 + val2) >> 16;
  387. //Serial.print(block->data[i]);
  388. //Serial.print(", ");
  389. //if ((i % 12) == 11) Serial.println();
  390. ph += inc;
  391. }
  392. }
  393. phase = ph;
  394. transmit(block);
  395. release(block);
  396. }
  397. #endif
  398. /******************************************************************/
  399. void AudioPrint::update(void)
  400. {
  401. audio_block_t *block;
  402. uint32_t i;
  403. Serial.println("AudioPrint::update");
  404. Serial.println(name);
  405. block = receiveReadOnly();
  406. if (block) {
  407. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  408. Serial.print(block->data[i]);
  409. Serial.print(", ");
  410. if ((i % 12) == 11) Serial.println();
  411. }
  412. Serial.println();
  413. release(block);
  414. }
  415. }
  416. /******************************************************************/
  417. #define STATE_DIRECT_8BIT_MONO 0 // playing mono at native sample rate
  418. #define STATE_DIRECT_8BIT_STEREO 1 // playing stereo at native sample rate
  419. #define STATE_DIRECT_16BIT_MONO 2 // playing mono at native sample rate
  420. #define STATE_DIRECT_16BIT_STEREO 3 // playing stereo at native sample rate
  421. #define STATE_CONVERT_8BIT_MONO 4 // playing mono, converting sample rate
  422. #define STATE_CONVERT_8BIT_STEREO 5 // playing stereo, converting sample rate
  423. #define STATE_CONVERT_16BIT_MONO 6 // playing mono, converting sample rate
  424. #define STATE_CONVERT_16BIT_STEREO 7 // playing stereo, converting sample rate
  425. #define STATE_PARSE1 8 // looking for 20 byte ID header
  426. #define STATE_PARSE2 9 // looking for 16 byte format header
  427. #define STATE_PARSE3 10 // looking for 8 byte data header
  428. #define STATE_PARSE4 11 // ignoring unknown chunk
  429. #define STATE_STOP 12
  430. void AudioPlaySDcardWAV::begin(void)
  431. {
  432. state = STATE_STOP;
  433. state_play = STATE_STOP;
  434. data_length = 0;
  435. if (block_left) {
  436. release(block_left);
  437. block_left = NULL;
  438. }
  439. if (block_right) {
  440. release(block_right);
  441. block_right = NULL;
  442. }
  443. }
  444. bool AudioPlaySDcardWAV::play(const char *filename)
  445. {
  446. stop();
  447. wavfile = SD.open(filename);
  448. if (!wavfile) return false;
  449. buffer_remaining = 0;
  450. state_play = STATE_STOP;
  451. data_length = 0;
  452. state = STATE_PARSE1;
  453. return true;
  454. }
  455. void AudioPlaySDcardWAV::stop(void)
  456. {
  457. __disable_irq();
  458. if (state != STATE_STOP) {
  459. audio_block_t *b1 = block_left;
  460. block_left = NULL;
  461. audio_block_t *b2 = block_right;
  462. block_right = NULL;
  463. state = STATE_STOP;
  464. __enable_irq();
  465. if (b1) release(b1);
  466. if (b2) release(b2);
  467. wavfile.close();
  468. } else {
  469. __enable_irq();
  470. }
  471. }
  472. bool AudioPlaySDcardWAV::start(void)
  473. {
  474. __disable_irq();
  475. if (state == STATE_STOP) {
  476. if (state_play == STATE_STOP) {
  477. __enable_irq();
  478. return false;
  479. }
  480. state = state_play;
  481. }
  482. __enable_irq();
  483. return true;
  484. }
  485. void AudioPlaySDcardWAV::update(void)
  486. {
  487. // only update if we're playing
  488. if (state == STATE_STOP) return;
  489. // allocate the audio blocks to transmit
  490. block_left = allocate();
  491. if (block_left == NULL) return;
  492. if (state < 8 && (state & 1) == 1) {
  493. // if we're playing stereo, allocate another
  494. // block for the right channel output
  495. block_right = allocate();
  496. if (block_right == NULL) {
  497. release(block_left);
  498. return;
  499. }
  500. } else {
  501. // if we're playing mono or just parsing
  502. // the WAV file header, no right-side block
  503. block_right = NULL;
  504. }
  505. block_offset = 0;
  506. //Serial.println("update");
  507. // is there buffered data?
  508. if (buffer_remaining > 0) {
  509. // we have buffered data
  510. if (consume()) return; // it was enough to transmit audio
  511. }
  512. // we only get to this point when buffer[512] is empty
  513. if (state != STATE_STOP && wavfile.available()) {
  514. // we can read more data from the file...
  515. buffer_remaining = wavfile.read(buffer, 512);
  516. if (consume()) {
  517. // good, it resulted in audio transmit
  518. return;
  519. } else {
  520. // not good, no audio was transmitted
  521. buffer_remaining = 0;
  522. if (block_left) {
  523. release(block_left);
  524. block_left = NULL;
  525. }
  526. if (block_right) {
  527. release(block_right);
  528. block_right = NULL;
  529. }
  530. // if we're still playing, well, there's going to
  531. // be a gap in output, but we can't keep burning
  532. // time trying to read more data. Hopefully things
  533. // will go better next time?
  534. if (state != STATE_STOP) return;
  535. }
  536. }
  537. // end of file reached or other reason to stop
  538. wavfile.close();
  539. if (block_left) {
  540. release(block_left);
  541. block_left = NULL;
  542. }
  543. if (block_right) {
  544. release(block_right);
  545. block_right = NULL;
  546. }
  547. state_play = STATE_STOP;
  548. state = STATE_STOP;
  549. }
  550. // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
  551. // Consume already buffered data. Returns true if audio transmitted.
  552. bool AudioPlaySDcardWAV::consume(void)
  553. {
  554. uint32_t len, size;
  555. uint8_t lsb, msb;
  556. const uint8_t *p;
  557. size = buffer_remaining;
  558. p = buffer + 512 - size;
  559. start:
  560. if (size == 0) return false;
  561. //Serial.print("AudioPlaySDcardWAV write, size = ");
  562. //Serial.print(size);
  563. //Serial.print(", data_length = ");
  564. //Serial.print(data_length);
  565. //Serial.print(", state = ");
  566. //Serial.println(state);
  567. switch (state) {
  568. // parse wav file header, is this really a .wav file?
  569. case STATE_PARSE1:
  570. len = 20 - data_length;
  571. if (size < len) len = size;
  572. memcpy((uint8_t *)header + data_length, p, len);
  573. data_length += len;
  574. if (data_length < 20) return false;
  575. // parse the header...
  576. if (header[0] == 0x46464952 && header[2] == 0x45564157
  577. && header[3] == 0x20746D66 && header[4] == 16) {
  578. //Serial.println("header ok");
  579. state = STATE_PARSE2;
  580. p += len;
  581. size -= len;
  582. data_length = 0;
  583. goto start;
  584. }
  585. //Serial.println("unknown WAV header");
  586. break;
  587. // check & extract key audio parameters
  588. case STATE_PARSE2:
  589. len = 16 - data_length;
  590. if (size < len) len = size;
  591. memcpy((uint8_t *)header + data_length, p, len);
  592. data_length += len;
  593. if (data_length < 16) return false;
  594. if (parse_format()) {
  595. //Serial.println("audio format ok");
  596. p += len;
  597. size -= len;
  598. data_length = 0;
  599. state = STATE_PARSE3;
  600. goto start;
  601. }
  602. //Serial.println("unknown audio format");
  603. break;
  604. // find the data chunk
  605. case STATE_PARSE3:
  606. len = 8 - data_length;
  607. if (size < len) len = size;
  608. memcpy((uint8_t *)header + data_length, p, len);
  609. data_length += len;
  610. if (data_length < 8) return false;
  611. //Serial.print("chunk id = ");
  612. //Serial.print(header[0], HEX);
  613. //Serial.print(", length = ");
  614. //Serial.println(header[1]);
  615. p += len;
  616. size -= len;
  617. data_length = header[1];
  618. if (header[0] == 0x61746164) {
  619. //Serial.println("found data chunk");
  620. // TODO: verify offset in file is an even number
  621. // as required by WAV format. abort if odd. Code
  622. // below will depend upon this and fail if not even.
  623. leftover_bytes = 0;
  624. state = state_play;
  625. if (state & 1) {
  626. // if we're going to start stereo
  627. // better allocate another output block
  628. block_right = allocate();
  629. if (!block_right) return false;
  630. }
  631. } else {
  632. state = STATE_PARSE4;
  633. }
  634. goto start;
  635. // ignore any extra unknown chunks (title & artist info)
  636. case STATE_PARSE4:
  637. if (size < data_length) {
  638. data_length -= size;
  639. return false;
  640. }
  641. p += data_length;
  642. size -= data_length;
  643. data_length = 0;
  644. state = STATE_PARSE3;
  645. goto start;
  646. // playing mono at native sample rate
  647. case STATE_DIRECT_8BIT_MONO:
  648. return false;
  649. // playing stereo at native sample rate
  650. case STATE_DIRECT_8BIT_STEREO:
  651. return false;
  652. // playing mono at native sample rate
  653. case STATE_DIRECT_16BIT_MONO:
  654. if (size > data_length) size = data_length;
  655. data_length -= size;
  656. while (1) {
  657. lsb = *p++;
  658. msb = *p++;
  659. size -= 2;
  660. block_left->data[block_offset++] = (msb << 8) | lsb;
  661. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  662. transmit(block_left, 0);
  663. transmit(block_left, 1);
  664. //Serial1.print('%');
  665. //delayMicroseconds(90);
  666. release(block_left);
  667. block_left = NULL;
  668. data_length += size;
  669. buffer_remaining = size;
  670. if (block_right) release(block_right);
  671. return true;
  672. }
  673. if (size == 0) {
  674. if (data_length == 0) break;
  675. return false;
  676. }
  677. }
  678. // end of file reached
  679. if (block_offset > 0) {
  680. // TODO: fill remainder of last block with zero and transmit
  681. }
  682. state = STATE_STOP;
  683. return false;
  684. // playing stereo at native sample rate
  685. case STATE_DIRECT_16BIT_STEREO:
  686. if (size > data_length) size = data_length;
  687. data_length -= size;
  688. if (leftover_bytes) {
  689. block_left->data[block_offset] = header[0];
  690. goto right16;
  691. }
  692. while (1) {
  693. lsb = *p++;
  694. msb = *p++;
  695. size -= 2;
  696. if (size == 0) {
  697. if (data_length == 0) break;
  698. header[0] = (msb << 8) | lsb;
  699. leftover_bytes = 2;
  700. return false;
  701. }
  702. block_left->data[block_offset] = (msb << 8) | lsb;
  703. right16:
  704. lsb = *p++;
  705. msb = *p++;
  706. size -= 2;
  707. block_right->data[block_offset++] = (msb << 8) | lsb;
  708. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  709. transmit(block_left, 0);
  710. release(block_left);
  711. block_left = NULL;
  712. transmit(block_right, 1);
  713. release(block_right);
  714. block_right = NULL;
  715. data_length += size;
  716. buffer_remaining = size;
  717. return true;
  718. }
  719. if (size == 0) {
  720. if (data_length == 0) break;
  721. leftover_bytes = 0;
  722. return false;
  723. }
  724. }
  725. // end of file reached
  726. if (block_offset > 0) {
  727. // TODO: fill remainder of last block with zero and transmit
  728. }
  729. state = STATE_STOP;
  730. return false;
  731. // playing mono, converting sample rate
  732. case STATE_CONVERT_8BIT_MONO :
  733. return false;
  734. // playing stereo, converting sample rate
  735. case STATE_CONVERT_8BIT_STEREO:
  736. return false;
  737. // playing mono, converting sample rate
  738. case STATE_CONVERT_16BIT_MONO:
  739. return false;
  740. // playing stereo, converting sample rate
  741. case STATE_CONVERT_16BIT_STEREO:
  742. return false;
  743. // ignore any extra data after playing
  744. // or anything following any error
  745. case STATE_STOP:
  746. return false;
  747. // this is not supposed to happen!
  748. //default:
  749. //Serial.println("AudioPlaySDcardWAV, unknown state");
  750. }
  751. state_play = STATE_STOP;
  752. state = STATE_STOP;
  753. return false;
  754. }
  755. /*
  756. 00000000 52494646 66EA6903 57415645 666D7420 RIFFf.i.WAVEfmt
  757. 00000010 10000000 01000200 44AC0000 10B10200 ........D.......
  758. 00000020 04001000 4C495354 3A000000 494E464F ....LIST:...INFO
  759. 00000030 494E414D 14000000 49205761 6E742054 INAM....I Want T
  760. 00000040 6F20436F 6D65204F 76657200 49415254 o Come Over.IART
  761. 00000050 12000000 4D656C69 73736120 45746865 ....Melissa Ethe
  762. 00000060 72696467 65006461 746100EA 69030100 ridge.data..i...
  763. 00000070 FEFF0300 FCFF0400 FDFF0200 0000FEFF ................
  764. 00000080 0300FDFF 0200FFFF 00000100 FEFF0300 ................
  765. 00000090 FDFF0300 FDFF0200 FFFF0100 0000FFFF ................
  766. */
  767. // SD library on Teensy3 at 96 MHz
  768. // 256 byte chunks, speed is 443272 bytes/sec
  769. // 512 byte chunks, speed is 468023 bytes/sec
  770. bool AudioPlaySDcardWAV::parse_format(void)
  771. {
  772. uint8_t num = 0;
  773. uint16_t format;
  774. uint16_t channels;
  775. uint32_t rate;
  776. uint16_t bits;
  777. format = header[0];
  778. //Serial.print(" format = ");
  779. //Serial.println(format);
  780. if (format != 1) return false;
  781. channels = header[0] >> 16;
  782. //Serial.print(" channels = ");
  783. //Serial.println(channels);
  784. if (channels == 1) {
  785. } else if (channels == 2) {
  786. num = 1;
  787. } else {
  788. return false;
  789. }
  790. bits = header[3] >> 16;
  791. //Serial.print(" bits = ");
  792. //Serial.println(bits);
  793. if (bits == 8) {
  794. } else if (bits == 16) {
  795. num |= 2;
  796. } else {
  797. return false;
  798. }
  799. rate = header[1];
  800. //Serial.print(" rate = ");
  801. //Serial.println(rate);
  802. if (rate == AUDIO_SAMPLE_RATE) {
  803. } else if (rate >= 8000 && rate <= 48000) {
  804. num |= 4;
  805. } else {
  806. return false;
  807. }
  808. // we're not checking the byte rate and block align fields
  809. // if they're not the expected values, all we could do is
  810. // return false. Do any real wav files have unexpected
  811. // values in these other fields?
  812. state_play = num;
  813. return true;
  814. }
  815. /******************************************************************/
  816. void AudioPlaySDcardRAW::begin(void)
  817. {
  818. playing = false;
  819. if (block) {
  820. release(block);
  821. block = NULL;
  822. }
  823. }
  824. bool AudioPlaySDcardRAW::play(const char *filename)
  825. {
  826. stop();
  827. rawfile = SD.open(filename);
  828. if (!rawfile) {
  829. Serial.println("unable to open file");
  830. return false;
  831. }
  832. Serial.println("able to open file");
  833. playing = true;
  834. return true;
  835. }
  836. void AudioPlaySDcardRAW::stop(void)
  837. {
  838. __disable_irq();
  839. if (playing) {
  840. playing = false;
  841. __enable_irq();
  842. rawfile.close();
  843. } else {
  844. __enable_irq();
  845. }
  846. }
  847. void AudioPlaySDcardRAW::update(void)
  848. {
  849. unsigned int i, n;
  850. // only update if we're playing
  851. if (!playing) return;
  852. // allocate the audio blocks to transmit
  853. block = allocate();
  854. if (block == NULL) return;
  855. if (rawfile.available()) {
  856. // we can read more data from the file...
  857. n = rawfile.read(block->data, AUDIO_BLOCK_SAMPLES*2);
  858. for (i=n/2; i < AUDIO_BLOCK_SAMPLES; i++) {
  859. block->data[i] = 0;
  860. }
  861. transmit(block);
  862. release(block);
  863. } else {
  864. rawfile.close();
  865. playing = false;
  866. }
  867. }
  868. /******************************************************************/
  869. void AudioPlayMemory::play(const unsigned int *data)
  870. {
  871. uint32_t format;
  872. playing = 0;
  873. prior = 0;
  874. format = *data++;
  875. next = data;
  876. length = format & 0xFFFFFF;
  877. playing = format >> 24;
  878. }
  879. void AudioPlayMemory::stop(void)
  880. {
  881. playing = 0;
  882. }
  883. extern "C" {
  884. extern const int16_t ulaw_decode_table[256];
  885. };
  886. void AudioPlayMemory::update(void)
  887. {
  888. audio_block_t *block;
  889. const unsigned int *in;
  890. int16_t *out;
  891. uint32_t tmp32, consumed;
  892. int16_t s0, s1, s2, s3, s4;
  893. int i;
  894. if (!playing) return;
  895. block = allocate();
  896. if (block == NULL) return;
  897. //Serial.write('.');
  898. out = block->data;
  899. in = next;
  900. s0 = prior;
  901. switch (playing) {
  902. case 0x01: // u-law encoded, 44100 Hz
  903. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 4) {
  904. tmp32 = *in++;
  905. *out++ = ulaw_decode_table[(tmp32 >> 0) & 255];
  906. *out++ = ulaw_decode_table[(tmp32 >> 8) & 255];
  907. *out++ = ulaw_decode_table[(tmp32 >> 16) & 255];
  908. *out++ = ulaw_decode_table[(tmp32 >> 24) & 255];
  909. }
  910. consumed = 128;
  911. break;
  912. case 0x81: // 16 bit PCM, 44100 Hz
  913. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 2) {
  914. tmp32 = *in++;
  915. *out++ = (int16_t)(tmp32 & 65535);
  916. *out++ = (int16_t)(tmp32 >> 16);
  917. }
  918. consumed = 128;
  919. break;
  920. case 0x02: // u-law encoded, 22050 Hz
  921. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 8) {
  922. tmp32 = *in++;
  923. s1 = ulaw_decode_table[(tmp32 >> 0) & 255];
  924. s2 = ulaw_decode_table[(tmp32 >> 8) & 255];
  925. s3 = ulaw_decode_table[(tmp32 >> 16) & 255];
  926. s4 = ulaw_decode_table[(tmp32 >> 24) & 255];
  927. *out++ = (s0 + s1) >> 1;
  928. *out++ = s1;
  929. *out++ = (s1 + s2) >> 1;
  930. *out++ = s2;
  931. *out++ = (s2 + s3) >> 1;
  932. *out++ = s3;
  933. *out++ = (s3 + s4) >> 1;
  934. *out++ = s4;
  935. s0 = s4;
  936. }
  937. consumed = 64;
  938. break;
  939. case 0x82: // 16 bits PCM, 22050 Hz
  940. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 4) {
  941. tmp32 = *in++;
  942. s1 = (int16_t)(tmp32 & 65535);
  943. s2 = (int16_t)(tmp32 >> 16);
  944. *out++ = (s0 + s1) >> 1;
  945. *out++ = s1;
  946. *out++ = (s1 + s2) >> 1;
  947. *out++ = s2;
  948. s0 = s2;
  949. }
  950. consumed = 64;
  951. break;
  952. case 0x03: // u-law encoded, 11025 Hz
  953. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 16) {
  954. tmp32 = *in++;
  955. s1 = ulaw_decode_table[(tmp32 >> 0) & 255];
  956. s2 = ulaw_decode_table[(tmp32 >> 8) & 255];
  957. s3 = ulaw_decode_table[(tmp32 >> 16) & 255];
  958. s4 = ulaw_decode_table[(tmp32 >> 24) & 255];
  959. *out++ = (s0 * 3 + s1) >> 2;
  960. *out++ = (s0 + s1) >> 1;
  961. *out++ = (s0 + s1 * 3) >> 2;
  962. *out++ = s1;
  963. *out++ = (s1 * 3 + s2) >> 2;
  964. *out++ = (s1 + s2) >> 1;
  965. *out++ = (s1 + s2 * 3) >> 2;
  966. *out++ = s2;
  967. *out++ = (s2 * 3 + s3) >> 2;
  968. *out++ = (s2 + s3) >> 1;
  969. *out++ = (s2 + s3 * 3) >> 2;
  970. *out++ = s3;
  971. *out++ = (s3 * 3 + s4) >> 2;
  972. *out++ = (s3 + s4) >> 1;
  973. *out++ = (s3 + s4 * 3) >> 2;
  974. *out++ = s4;
  975. s0 = s4;
  976. }
  977. consumed = 32;
  978. break;
  979. case 0x83: // 16 bit PCM, 11025 Hz
  980. for (i=0; i < AUDIO_BLOCK_SAMPLES; i += 8) {
  981. tmp32 = *in++;
  982. s1 = (int16_t)(tmp32 & 65535);
  983. s2 = (int16_t)(tmp32 >> 16);
  984. *out++ = (s0 * 3 + s1) >> 2;
  985. *out++ = (s0 + s1) >> 1;
  986. *out++ = (s0 + s1 * 3) >> 2;
  987. *out++ = s1;
  988. *out++ = (s1 * 3 + s2) >> 2;
  989. *out++ = (s1 + s2) >> 1;
  990. *out++ = (s1 + s2 * 3) >> 2;
  991. *out++ = s2;
  992. s0 = s2;
  993. }
  994. consumed = 32;
  995. break;
  996. default:
  997. release(block);
  998. playing = 0;
  999. return;
  1000. }
  1001. prior = s0;
  1002. next = in;
  1003. if (length > consumed) {
  1004. length -= consumed;
  1005. } else {
  1006. playing = 0;
  1007. }
  1008. transmit(block);
  1009. release(block);
  1010. }
  1011. /******************************************************************/
  1012. // computes ((a[31:0] * b[15:0]) >> 16)
  1013. static inline int32_t signed_multiply_32x16b(int32_t a, uint32_t b) __attribute__((always_inline));
  1014. static inline int32_t signed_multiply_32x16b(int32_t a, uint32_t b)
  1015. {
  1016. int32_t out;
  1017. asm volatile("smulwb %0, %1, %2" : "=r" (out) : "r" (a), "r" (b));
  1018. return out;
  1019. }
  1020. // computes ((a[31:0] * b[31:16]) >> 16)
  1021. static inline int32_t signed_multiply_32x16t(int32_t a, uint32_t b) __attribute__((always_inline));
  1022. static inline int32_t signed_multiply_32x16t(int32_t a, uint32_t b)
  1023. {
  1024. int32_t out;
  1025. asm volatile("smulwt %0, %1, %2" : "=r" (out) : "r" (a), "r" (b));
  1026. return out;
  1027. }
  1028. // computes (((int64_t)a[31:0] * (int64_t)b[31:0]) >> 32)
  1029. static inline int32_t multiply_32x32_rshift32(int32_t a, int32_t b) __attribute__((always_inline));
  1030. static inline int32_t multiply_32x32_rshift32(int32_t a, int32_t b)
  1031. {
  1032. int32_t out;
  1033. asm volatile("smmul %0, %1, %2" : "=r" (out) : "r" (a), "r" (b));
  1034. return out;
  1035. }
  1036. // computes (((int64_t)a[31:0] * (int64_t)b[31:0] + 0x8000000) >> 32)
  1037. static inline int32_t multiply_32x32_rshift32_rounded(int32_t a, int32_t b) __attribute__((always_inline));
  1038. static inline int32_t multiply_32x32_rshift32_rounded(int32_t a, int32_t b)
  1039. {
  1040. int32_t out;
  1041. asm volatile("smmulr %0, %1, %2" : "=r" (out) : "r" (a), "r" (b));
  1042. return out;
  1043. }
  1044. // computes sum + (((int64_t)a[31:0] * (int64_t)b[31:0] + 0x8000000) >> 32)
  1045. static inline int32_t multiply_accumulate_32x32_rshift32_rounded(int32_t sum, int32_t a, int32_t b) __attribute__((always_inline));
  1046. static inline int32_t multiply_accumulate_32x32_rshift32_rounded(int32_t sum, int32_t a, int32_t b)
  1047. {
  1048. int32_t out;
  1049. asm volatile("smmlar %0, %2, %3, %1" : "=r" (out) : "r" (sum), "r" (a), "r" (b));
  1050. return out;
  1051. }
  1052. // computes sum - (((int64_t)a[31:0] * (int64_t)b[31:0] + 0x8000000) >> 32)
  1053. static inline int32_t multiply_subtract_32x32_rshift32_rounded(int32_t sum, int32_t a, int32_t b) __attribute__((always_inline));
  1054. static inline int32_t multiply_subtract_32x32_rshift32_rounded(int32_t sum, int32_t a, int32_t b)
  1055. {
  1056. int32_t out;
  1057. asm volatile("smmlsr %0, %2, %3, %1" : "=r" (out) : "r" (sum), "r" (a), "r" (b));
  1058. return out;
  1059. }
  1060. // computes ((a[15:0] << 16) | b[15:0])
  1061. static inline uint32_t pack_16x16(int32_t a, int32_t b) __attribute__((always_inline));
  1062. static inline uint32_t pack_16x16(int32_t a, int32_t b)
  1063. {
  1064. int32_t out;
  1065. asm volatile("pkhbt %0, %1, %2, lsl #16" : "=r" (out) : "r" (b), "r" (a));
  1066. return out;
  1067. }
  1068. // computes (((a[31:16] + b[31:16]) << 16) | (a[15:0 + b[15:0]))
  1069. static inline uint32_t signed_add_16_and_16(uint32_t a, uint32_t b) __attribute__((always_inline));
  1070. static inline uint32_t signed_add_16_and_16(uint32_t a, uint32_t b)
  1071. {
  1072. int32_t out;
  1073. asm volatile("qadd16 %0, %1, %2" : "=r" (out) : "r" (a), "r" (b));
  1074. return out;
  1075. }
  1076. // computes (sum + ((a[31:0] * b[15:0]) >> 16))
  1077. static inline int32_t signed_multiply_accumulate_32x16b(int32_t sum, int32_t a, uint32_t b)
  1078. {
  1079. int32_t out;
  1080. asm volatile("smlawb %0, %2, %3, %1" : "=r" (out) : "r" (sum), "r" (a), "r" (b));
  1081. return out;
  1082. }
  1083. // computes (sum + ((a[31:0] * b[31:16]) >> 16))
  1084. static inline int32_t signed_multiply_accumulate_32x16t(int32_t sum, int32_t a, uint32_t b)
  1085. {
  1086. int32_t out;
  1087. asm volatile("smlawt %0, %2, %3, %1" : "=r" (out) : "r" (sum), "r" (a), "r" (b));
  1088. return out;
  1089. }
  1090. // computes logical and, forces compiler to allocate register and use single cycle instruction
  1091. static inline uint32_t logical_and(uint32_t a, uint32_t b)
  1092. {
  1093. asm volatile("and %0, %1" : "+r" (a) : "r" (b));
  1094. return a;
  1095. }
  1096. void applyGain(int16_t *data, int32_t mult)
  1097. {
  1098. uint32_t *p = (uint32_t *)data;
  1099. const uint32_t *end = (uint32_t *)(data + AUDIO_BLOCK_SAMPLES);
  1100. do {
  1101. uint32_t tmp32 = *p; // read 2 samples from *data
  1102. int32_t val1 = signed_multiply_32x16b(mult, tmp32);
  1103. int32_t val2 = signed_multiply_32x16t(mult, tmp32);
  1104. val1 = signed_saturate_rshift(val1, 16, 0);
  1105. val2 = signed_saturate_rshift(val2, 16, 0);
  1106. *p++ = pack_16x16(val2, val1);
  1107. } while (p < end);
  1108. }
  1109. // page 133
  1110. void applyGainThenAdd(int16_t *data, const int16_t *in, int32_t mult)
  1111. {
  1112. uint32_t *dst = (uint32_t *)data;
  1113. const uint32_t *src = (uint32_t *)in;
  1114. const uint32_t *end = (uint32_t *)(data + AUDIO_BLOCK_SAMPLES);
  1115. if (mult == 65536) {
  1116. do {
  1117. uint32_t tmp32 = *dst;
  1118. *dst++ = signed_add_16_and_16(tmp32, *src++);
  1119. tmp32 = *dst;
  1120. *dst++ = signed_add_16_and_16(tmp32, *src++);
  1121. } while (dst < end);
  1122. } else {
  1123. do {
  1124. uint32_t tmp32 = *src++; // read 2 samples from *data
  1125. int32_t val1 = signed_multiply_32x16b(mult, tmp32);
  1126. int32_t val2 = signed_multiply_32x16t(mult, tmp32);
  1127. val1 = signed_saturate_rshift(val1, 16, 0);
  1128. val2 = signed_saturate_rshift(val2, 16, 0);
  1129. tmp32 = pack_16x16(val2, val1);
  1130. uint32_t tmp32b = *dst;
  1131. *dst++ = signed_add_16_and_16(tmp32, tmp32b);
  1132. } while (dst < end);
  1133. }
  1134. }
  1135. void AudioMixer4::update(void)
  1136. {
  1137. audio_block_t *in, *out=NULL;
  1138. unsigned int channel;
  1139. for (channel=0; channel < 4; channel++) {
  1140. if (!out) {
  1141. out = receiveWritable(channel);
  1142. if (out) {
  1143. int32_t mult = multiplier[channel];
  1144. if (mult != 65536) applyGain(out->data, mult);
  1145. }
  1146. } else {
  1147. in = receiveReadOnly(channel);
  1148. if (in) {
  1149. applyGainThenAdd(out->data, in->data, multiplier[channel]);
  1150. release(in);
  1151. }
  1152. }
  1153. }
  1154. if (out) {
  1155. transmit(out);
  1156. release(out);
  1157. }
  1158. }
  1159. /******************************************************************/
  1160. void AudioFilterBiquad::update(void)
  1161. {
  1162. audio_block_t *block;
  1163. int32_t a0, a1, a2, b1, b2, sum;
  1164. uint32_t in2, out2, aprev, bprev, flag;
  1165. uint32_t *data, *end;
  1166. int32_t *state;
  1167. block = receiveWritable();
  1168. if (!block) return;
  1169. data = (uint32_t *)(block->data);
  1170. end = data + AUDIO_BLOCK_SAMPLES/2;
  1171. state = (int32_t *)definition;
  1172. do {
  1173. a0 = *state++;
  1174. a1 = *state++;
  1175. a2 = *state++;
  1176. b1 = *state++;
  1177. b2 = *state++;
  1178. aprev = *state++;
  1179. bprev = *state++;
  1180. sum = *state & 0x3FFF;
  1181. do {
  1182. in2 = *data;
  1183. sum = signed_multiply_accumulate_32x16b(sum, a0, in2);
  1184. sum = signed_multiply_accumulate_32x16t(sum, a1, aprev);
  1185. sum = signed_multiply_accumulate_32x16b(sum, a2, aprev);
  1186. sum = signed_multiply_accumulate_32x16t(sum, b1, bprev);
  1187. sum = signed_multiply_accumulate_32x16b(sum, b2, bprev);
  1188. out2 = (uint32_t)sum >> 14;
  1189. sum &= 0x3FFF;
  1190. sum = signed_multiply_accumulate_32x16t(sum, a0, in2);
  1191. sum = signed_multiply_accumulate_32x16b(sum, a1, in2);
  1192. sum = signed_multiply_accumulate_32x16t(sum, a2, aprev);
  1193. sum = signed_multiply_accumulate_32x16b(sum, b1, out2);
  1194. sum = signed_multiply_accumulate_32x16t(sum, b2, bprev);
  1195. aprev = in2;
  1196. bprev = pack_16x16(sum >> 14, out2);
  1197. sum &= 0x3FFF;
  1198. aprev = in2;
  1199. *data++ = bprev;
  1200. } while (data < end);
  1201. flag = *state & 0x80000000;
  1202. *state++ = sum | flag;
  1203. *(state-2) = bprev;
  1204. *(state-3) = aprev;
  1205. } while (flag);
  1206. transmit(block);
  1207. release(block);
  1208. }
  1209. void AudioFilterBiquad::updateCoefs(int *source, bool doReset)
  1210. {
  1211. int32_t *dest=(int32_t *)definition;
  1212. int32_t *src=(int32_t *)source;
  1213. __disable_irq();
  1214. for(uint8_t index=0;index<5;index++)
  1215. {
  1216. *dest++=*src++;
  1217. }
  1218. if(doReset)
  1219. {
  1220. *dest++=0;
  1221. *dest++=0;
  1222. *dest++=0;
  1223. }
  1224. __enable_irq();
  1225. }
  1226. void AudioFilterBiquad::updateCoefs(int *source)
  1227. {
  1228. updateCoefs(source,false);
  1229. }
  1230. /******************************************************************/
  1231. extern "C" {
  1232. extern const int16_t fader_table[256];
  1233. };
  1234. void AudioEffectFade::update(void)
  1235. {
  1236. audio_block_t *block;
  1237. uint32_t i, pos, inc, index, scale;
  1238. int32_t val1, val2, val, sample;
  1239. uint8_t dir;
  1240. pos = position;
  1241. if (pos == 0) {
  1242. // output is silent
  1243. block = receiveReadOnly();
  1244. if (block) release(block);
  1245. return;
  1246. } else if (pos == 0xFFFFFFFF) {
  1247. // output is 100%
  1248. block = receiveReadOnly();
  1249. if (!block) return;
  1250. transmit(block);
  1251. release(block);
  1252. return;
  1253. }
  1254. block = receiveWritable();
  1255. if (!block) return;
  1256. inc = rate;
  1257. dir = direction;
  1258. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  1259. index = pos >> 24;
  1260. val1 = fader_table[index];
  1261. val2 = fader_table[index+1];
  1262. scale = (pos >> 8) & 0xFFFF;
  1263. val2 *= scale;
  1264. val1 *= 0x10000 - scale;
  1265. val = (val1 + val2) >> 16;
  1266. sample = block->data[i];
  1267. sample = (sample * val) >> 15;
  1268. block->data[i] = sample;
  1269. if (dir > 0) {
  1270. // output is increasing
  1271. if (inc < 0xFFFFFFFF - pos) pos += inc;
  1272. else pos = 0xFFFFFFFF;
  1273. } else {
  1274. // output is decreasing
  1275. if (inc < pos) pos -= inc;
  1276. else pos = 0;
  1277. }
  1278. }
  1279. position = pos;
  1280. transmit(block);
  1281. release(block);
  1282. }
  1283. void AudioEffectFade::fadeBegin(uint32_t newrate, uint8_t dir)
  1284. {
  1285. __disable_irq();
  1286. uint32_t pos = position;
  1287. if (pos == 0) position = 1;
  1288. else if (pos == 0xFFFFFFFF) position = 0xFFFFFFFE;
  1289. rate = newrate;
  1290. direction = dir;
  1291. __enable_irq();
  1292. }
  1293. /******************************************************************/
  1294. static inline int32_t multiply_32x32_rshift30(int32_t a, int32_t b) __attribute__((always_inline));
  1295. static inline int32_t multiply_32x32_rshift30(int32_t a, int32_t b)
  1296. {
  1297. return ((int64_t)a * (int64_t)b) >> 30;
  1298. }
  1299. //#define TONE_DETECT_FAST
  1300. void AudioAnalyzeToneDetect::update(void)
  1301. {
  1302. audio_block_t *block;
  1303. int32_t q0, q1, q2, coef;
  1304. const int16_t *p, *end;
  1305. uint16_t n;
  1306. block = receiveReadOnly();
  1307. if (!block) return;
  1308. if (!enabled) {
  1309. release(block);
  1310. return;
  1311. }
  1312. p = block->data;
  1313. end = p + AUDIO_BLOCK_SAMPLES;
  1314. n = count;
  1315. coef = coefficient;
  1316. q1 = s1;
  1317. q2 = s2;
  1318. do {
  1319. // the Goertzel algorithm is kinda magical ;-)
  1320. #ifdef TONE_DETECT_FAST
  1321. q0 = (*p++) + (multiply_32x32_rshift32_rounded(coef, q1) << 2) - q2;
  1322. #else
  1323. q0 = (*p++) + multiply_32x32_rshift30(coef, q1) - q2;
  1324. // TODO: is this only 1 cycle slower? if so, always use it
  1325. #endif
  1326. q2 = q1;
  1327. q1 = q0;
  1328. if (--n == 0) {
  1329. out1 = q1;
  1330. out2 = q2;
  1331. q1 = 0; // TODO: does clearing these help or hinder?
  1332. q2 = 0;
  1333. new_output = true;
  1334. n = length;
  1335. }
  1336. } while (p < end);
  1337. count = n;
  1338. s1 = q1;
  1339. s2 = q2;
  1340. release(block);
  1341. }
  1342. void AudioAnalyzeToneDetect::set_params(int32_t coef, uint16_t cycles, uint16_t len)
  1343. {
  1344. __disable_irq();
  1345. coefficient = coef;
  1346. ncycles = cycles;
  1347. length = len;
  1348. count = len;
  1349. s1 = 0;
  1350. s2 = 0;
  1351. enabled = true;
  1352. __enable_irq();
  1353. //Serial.printf("Tone: coef=%d, ncycles=%d, length=%d\n", coefficient, ncycles, length);
  1354. }
  1355. float AudioAnalyzeToneDetect::read(void)
  1356. {
  1357. int32_t coef, q1, q2, power;
  1358. uint16_t len;
  1359. __disable_irq();
  1360. coef = coefficient;
  1361. q1 = out1;
  1362. q2 = out2;
  1363. len = length;
  1364. __enable_irq();
  1365. #ifdef TONE_DETECT_FAST
  1366. power = multiply_32x32_rshift32_rounded(q2, q2);
  1367. power = multiply_accumulate_32x32_rshift32_rounded(power, q1, q1);
  1368. power = multiply_subtract_32x32_rshift32_rounded(power,
  1369. multiply_32x32_rshift30(q1, q2), coef);
  1370. power <<= 4;
  1371. #else
  1372. int64_t power64;
  1373. power64 = (int64_t)q2 * (int64_t)q2;
  1374. power64 += (int64_t)q1 * (int64_t)q1;
  1375. power64 -= (((int64_t)q1 * (int64_t)q2) >> 30) * (int64_t)coef;
  1376. power = power64 >> 28;
  1377. #endif
  1378. return sqrtf((float)power) / (float)len;
  1379. }
  1380. AudioAnalyzeToneDetect::operator bool()
  1381. {
  1382. int32_t coef, q1, q2, power, trigger;
  1383. uint16_t len;
  1384. __disable_irq();
  1385. coef = coefficient;
  1386. q1 = out1;
  1387. q2 = out2;
  1388. len = length;
  1389. __enable_irq();
  1390. #ifdef TONE_DETECT_FAST
  1391. power = multiply_32x32_rshift32_rounded(q2, q2);
  1392. power = multiply_accumulate_32x32_rshift32_rounded(power, q1, q1);
  1393. power = multiply_subtract_32x32_rshift32_rounded(power,
  1394. multiply_32x32_rshift30(q1, q2), coef);
  1395. power <<= 4;
  1396. #else
  1397. int64_t power64;
  1398. power64 = (int64_t)q2 * (int64_t)q2;
  1399. power64 += (int64_t)q1 * (int64_t)q1;
  1400. power64 -= (((int64_t)q1 * (int64_t)q2) >> 30) * (int64_t)coef;
  1401. power = power64 >> 28;
  1402. #endif
  1403. trigger = (uint32_t)len * thresh;
  1404. trigger = multiply_32x32_rshift32(trigger, trigger);
  1405. Serial.printf("bool: power=%d, trig=%d\n", power, trigger);
  1406. return (power >= trigger);
  1407. }
  1408. /******************************************************************/
  1409. void AudioFilterFIR::begin(short *cp,int n_coeffs)
  1410. {
  1411. // pointer to coefficients
  1412. coeff_p = cp;
  1413. // Initialize FIR instances for the left and right channels
  1414. if(coeff_p && (coeff_p != FIR_PASSTHRU)) {
  1415. arm_fir_init_q15(&l_fir_inst, n_coeffs, coeff_p, &l_StateQ15[0], AUDIO_BLOCK_SAMPLES);
  1416. arm_fir_init_q15(&r_fir_inst, n_coeffs, coeff_p, &r_StateQ15[0], AUDIO_BLOCK_SAMPLES);
  1417. }
  1418. }
  1419. // This has the same effect as begin(NULL,0);
  1420. void AudioFilterFIR::stop(void)
  1421. {
  1422. coeff_p = NULL;
  1423. }
  1424. void AudioFilterFIR::update(void)
  1425. {
  1426. audio_block_t *block,*b_new;
  1427. // If there's no coefficient table, give up.
  1428. if(coeff_p == NULL)return;
  1429. // do passthru
  1430. if(coeff_p == FIR_PASSTHRU) {
  1431. // Just passthrough
  1432. block = receiveWritable(0);
  1433. if(block) {
  1434. transmit(block,0);
  1435. release(block);
  1436. }
  1437. block = receiveWritable(1);
  1438. if(block) {
  1439. transmit(block,1);
  1440. release(block);
  1441. }
  1442. return;
  1443. }
  1444. // Left Channel
  1445. block = receiveWritable(0);
  1446. // get a block for the FIR output
  1447. b_new = allocate();
  1448. if(block && b_new) {
  1449. arm_fir_q15(&l_fir_inst, (q15_t *)block->data, (q15_t *)b_new->data, AUDIO_BLOCK_SAMPLES);
  1450. // send the FIR output to the left channel
  1451. transmit(b_new,0);
  1452. }
  1453. if(block)release(block);
  1454. if(b_new)release(b_new);
  1455. // Right Channel
  1456. block = receiveWritable(1);
  1457. b_new = allocate();
  1458. if(block && b_new) {
  1459. arm_fir_q15(&r_fir_inst, (q15_t *)block->data, (q15_t *)b_new->data, AUDIO_BLOCK_SAMPLES);
  1460. transmit(b_new,1);
  1461. }
  1462. if(block)release(block);
  1463. if(b_new)release(b_new);
  1464. }
  1465. /******************************************************************/
  1466. // A u d i o E f f e c t F l a n g e
  1467. // Written by Pete (El Supremo) Jan 2014
  1468. // 140207 - fix calculation of delay_rate_incr which is expressed as
  1469. // a fraction of 2*PI
  1470. // 140207 - cosmetic fix to begin()
  1471. // circular addressing indices for left and right channels
  1472. short AudioEffectFlange::l_circ_idx;
  1473. short AudioEffectFlange::r_circ_idx;
  1474. short * AudioEffectFlange::l_delayline = NULL;
  1475. short * AudioEffectFlange::r_delayline = NULL;
  1476. // User-supplied offset for the delayed sample
  1477. // but start with passthru
  1478. int AudioEffectFlange::delay_offset_idx = DELAY_PASSTHRU;
  1479. int AudioEffectFlange::delay_length;
  1480. int AudioEffectFlange::delay_depth;
  1481. int AudioEffectFlange::delay_rate_incr;
  1482. unsigned int AudioEffectFlange::l_delay_rate_index;
  1483. unsigned int AudioEffectFlange::r_delay_rate_index;
  1484. // fails if the user provides unreasonable values but will
  1485. // coerce them and go ahead anyway. e.g. if the delay offset
  1486. // is >= CHORUS_DELAY_LENGTH, the code will force it to
  1487. // CHORUS_DELAY_LENGTH-1 and return false.
  1488. // delay_rate is the rate (in Hz) of the sine wave modulation
  1489. // delay_depth is the maximum variation around delay_offset
  1490. // i.e. the total offset is delay_offset + delay_depth * sin(delay_rate)
  1491. boolean AudioEffectFlange::begin(short *delayline,int d_length,int delay_offset,int d_depth,float delay_rate)
  1492. {
  1493. boolean all_ok = true;
  1494. if(0) {
  1495. Serial.print("AudioEffectFlange.begin(offset = ");
  1496. Serial.print(delay_offset);
  1497. Serial.print(", depth = ");
  1498. Serial.print(d_depth);
  1499. Serial.print(", rate = ");
  1500. Serial.print(delay_rate,3);
  1501. Serial.println(")");
  1502. Serial.print(" FLANGE_DELAY_LENGTH = ");
  1503. Serial.println(d_length);
  1504. }
  1505. delay_length = d_length/2;
  1506. l_delayline = delayline;
  1507. r_delayline = delayline + delay_length;
  1508. delay_depth = d_depth;
  1509. // initial index
  1510. l_delay_rate_index = 0;
  1511. r_delay_rate_index = 0;
  1512. l_circ_idx = 0;
  1513. r_circ_idx = 0;
  1514. delay_rate_incr = delay_rate/44100.*2147483648.;
  1515. //Serial.println(delay_rate_incr,HEX);
  1516. delay_offset_idx = delay_offset;
  1517. // Allow the passthru code to go through
  1518. if(delay_offset_idx < -1) {
  1519. delay_offset_idx = 0;
  1520. all_ok = false;
  1521. }
  1522. if(delay_offset_idx >= delay_length) {
  1523. delay_offset_idx = delay_length - 1;
  1524. all_ok = false;
  1525. }
  1526. return(all_ok);
  1527. }
  1528. boolean AudioEffectFlange::modify(int delay_offset,int d_depth,float delay_rate)
  1529. {
  1530. boolean all_ok = true;
  1531. delay_depth = d_depth;
  1532. delay_rate_incr = delay_rate/44100.*2147483648.;
  1533. delay_offset_idx = delay_offset;
  1534. // Allow the passthru code to go through
  1535. if(delay_offset_idx < -1) {
  1536. delay_offset_idx = 0;
  1537. all_ok = false;
  1538. }
  1539. if(delay_offset_idx >= delay_length) {
  1540. delay_offset_idx = delay_length - 1;
  1541. all_ok = false;
  1542. }
  1543. l_delay_rate_index = 0;
  1544. r_delay_rate_index = 0;
  1545. l_circ_idx = 0;
  1546. r_circ_idx = 0;
  1547. return(all_ok);
  1548. }
  1549. void AudioEffectFlange::update(void)
  1550. {
  1551. audio_block_t *block;
  1552. int idx;
  1553. short *bp;
  1554. short frac;
  1555. int idx1;
  1556. if(l_delayline == NULL)return;
  1557. if(r_delayline == NULL)return;
  1558. // do passthru
  1559. if(delay_offset_idx == DELAY_PASSTHRU) {
  1560. // Just passthrough
  1561. block = receiveWritable(0);
  1562. if(block) {
  1563. bp = block->data;
  1564. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1565. l_circ_idx++;
  1566. if(l_circ_idx >= delay_length) {
  1567. l_circ_idx = 0;
  1568. }
  1569. l_delayline[l_circ_idx] = *bp++;
  1570. }
  1571. transmit(block,0);
  1572. release(block);
  1573. }
  1574. block = receiveWritable(1);
  1575. if(block) {
  1576. bp = block->data;
  1577. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1578. r_circ_idx++;
  1579. if(r_circ_idx >= delay_length) {
  1580. r_circ_idx = 0;
  1581. }
  1582. r_delayline[r_circ_idx] = *bp++;
  1583. }
  1584. transmit(block,1);
  1585. release(block);
  1586. }
  1587. return;
  1588. }
  1589. // L E F T C H A N N E L
  1590. block = receiveWritable(0);
  1591. if(block) {
  1592. bp = block->data;
  1593. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1594. l_circ_idx++;
  1595. if(l_circ_idx >= delay_length) {
  1596. l_circ_idx = 0;
  1597. }
  1598. l_delayline[l_circ_idx] = *bp;
  1599. idx = arm_sin_q15( (q15_t)((l_delay_rate_index >> 16) & 0x7fff));
  1600. idx = (idx * delay_depth) >> 15;
  1601. //Serial.println(idx);
  1602. idx = l_circ_idx - (delay_offset_idx + idx);
  1603. if(idx < 0) {
  1604. idx += delay_length;
  1605. }
  1606. if(idx >= delay_length) {
  1607. idx -= delay_length;
  1608. }
  1609. if(frac < 0)
  1610. idx1 = idx - 1;
  1611. else
  1612. idx1 = idx + 1;
  1613. if(idx1 < 0) {
  1614. idx1 += delay_length;
  1615. }
  1616. if(idx1 >= delay_length) {
  1617. idx1 -= delay_length;
  1618. }
  1619. frac = (l_delay_rate_index >> 1) &0x7fff;
  1620. frac = (( (int)(l_delayline[idx1] - l_delayline[idx])*frac) >> 15);
  1621. *bp++ = (l_delayline[l_circ_idx]
  1622. + l_delayline[idx] + frac
  1623. )/2;
  1624. l_delay_rate_index += delay_rate_incr;
  1625. if(l_delay_rate_index & 0x80000000) {
  1626. l_delay_rate_index &= 0x7fffffff;
  1627. }
  1628. }
  1629. // send the effect output to the left channel
  1630. transmit(block,0);
  1631. release(block);
  1632. }
  1633. // R I G H T C H A N N E L
  1634. block = receiveWritable(1);
  1635. if(block) {
  1636. bp = block->data;
  1637. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1638. r_circ_idx++;
  1639. if(r_circ_idx >= delay_length) {
  1640. r_circ_idx = 0;
  1641. }
  1642. r_delayline[r_circ_idx] = *bp;
  1643. idx = arm_sin_q15( (q15_t)((r_delay_rate_index >> 16)&0x7fff));
  1644. idx = (idx * delay_depth) >> 15;
  1645. idx = r_circ_idx - (delay_offset_idx + idx);
  1646. if(idx < 0) {
  1647. idx += delay_length;
  1648. }
  1649. if(idx >= delay_length) {
  1650. idx -= delay_length;
  1651. }
  1652. if(frac < 0)
  1653. idx1 = idx - 1;
  1654. else
  1655. idx1 = idx + 1;
  1656. if(idx1 < 0) {
  1657. idx1 += delay_length;
  1658. }
  1659. if(idx1 >= delay_length) {
  1660. idx1 -= delay_length;
  1661. }
  1662. frac = (r_delay_rate_index >> 1) &0x7fff;
  1663. frac = (( (int)(r_delayline[idx1] - r_delayline[idx])*frac) >> 15);
  1664. *bp++ = (r_delayline[r_circ_idx]
  1665. + r_delayline[idx] + frac
  1666. )/2;
  1667. r_delay_rate_index += delay_rate_incr;
  1668. if(r_delay_rate_index & 0x80000000) {
  1669. r_delay_rate_index &= 0x7fffffff;
  1670. }
  1671. }
  1672. // send the effect output to the right channel
  1673. transmit(block,1);
  1674. release(block);
  1675. }
  1676. }
  1677. /******************************************************************/
  1678. // A u d i o E f f e c t C h o r u s
  1679. // Written by Pete (El Supremo) Jan 2014
  1680. // circular addressing indices for left and right channels
  1681. short AudioEffectChorus::l_circ_idx;
  1682. short AudioEffectChorus::r_circ_idx;
  1683. short * AudioEffectChorus::l_delayline = NULL;
  1684. short * AudioEffectChorus::r_delayline = NULL;
  1685. int AudioEffectChorus::delay_length;
  1686. // An initial value of zero indicates passthru
  1687. int AudioEffectChorus::num_chorus = 0;
  1688. // All three must be valid.
  1689. boolean AudioEffectChorus::begin(short *delayline,int d_length,int n_chorus)
  1690. {
  1691. Serial.print("AudioEffectChorus.begin(Chorus delay line length = ");
  1692. Serial.print(d_length);
  1693. Serial.print(", n_chorus = ");
  1694. Serial.print(n_chorus);
  1695. Serial.println(")");
  1696. l_delayline = NULL;
  1697. r_delayline = NULL;
  1698. delay_length = 0;
  1699. l_circ_idx = 0;
  1700. r_circ_idx = 0;
  1701. if(delayline == NULL) {
  1702. return(false);
  1703. }
  1704. if(d_length < 10) {
  1705. return(false);
  1706. }
  1707. if(n_chorus < 1) {
  1708. return(false);
  1709. }
  1710. l_delayline = delayline;
  1711. r_delayline = delayline + d_length/2;
  1712. delay_length = d_length/2;
  1713. num_chorus = n_chorus;
  1714. return(true);
  1715. }
  1716. // This has the same effect as begin(NULL,0);
  1717. void AudioEffectChorus::stop(void)
  1718. {
  1719. }
  1720. void AudioEffectChorus::modify(int n_chorus)
  1721. {
  1722. num_chorus = n_chorus;
  1723. }
  1724. int iabs(int x)
  1725. {
  1726. if(x < 0)return(-x);
  1727. return(x);
  1728. }
  1729. //static int d_count = 0;
  1730. int last_idx = 0;
  1731. void AudioEffectChorus::update(void)
  1732. {
  1733. audio_block_t *block;
  1734. short *bp;
  1735. int sum;
  1736. int c_idx;
  1737. if(l_delayline == NULL)return;
  1738. if(r_delayline == NULL)return;
  1739. // do passthru
  1740. // It stores the unmodified data in the delay line so that
  1741. // it isn't as likely to click
  1742. if(num_chorus < 1) {
  1743. // Just passthrough
  1744. block = receiveWritable(0);
  1745. if(block) {
  1746. bp = block->data;
  1747. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1748. l_circ_idx++;
  1749. if(l_circ_idx >= delay_length) {
  1750. l_circ_idx = 0;
  1751. }
  1752. l_delayline[l_circ_idx] = *bp++;
  1753. }
  1754. transmit(block,0);
  1755. release(block);
  1756. }
  1757. block = receiveWritable(1);
  1758. if(block) {
  1759. bp = block->data;
  1760. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1761. r_circ_idx++;
  1762. if(r_circ_idx >= delay_length) {
  1763. r_circ_idx = 0;
  1764. }
  1765. r_delayline[r_circ_idx] = *bp++;
  1766. }
  1767. transmit(block,1);
  1768. release(block);
  1769. }
  1770. return;
  1771. }
  1772. // L E F T C H A N N E L
  1773. block = receiveWritable(0);
  1774. if(block) {
  1775. bp = block->data;
  1776. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1777. l_circ_idx++;
  1778. if(l_circ_idx >= delay_length) {
  1779. l_circ_idx = 0;
  1780. }
  1781. l_delayline[l_circ_idx] = *bp;
  1782. sum = 0;
  1783. c_idx = l_circ_idx;
  1784. for(int k = 0; k < num_chorus; k++) {
  1785. sum += l_delayline[c_idx];
  1786. if(num_chorus > 1)c_idx -= delay_length/(num_chorus - 1) - 1;
  1787. if(c_idx < 0) {
  1788. c_idx += delay_length;
  1789. }
  1790. }
  1791. *bp++ = sum/num_chorus;
  1792. }
  1793. // send the effect output to the left channel
  1794. transmit(block,0);
  1795. release(block);
  1796. }
  1797. // R I G H T C H A N N E L
  1798. block = receiveWritable(1);
  1799. if(block) {
  1800. bp = block->data;
  1801. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1802. r_circ_idx++;
  1803. if(r_circ_idx >= delay_length) {
  1804. r_circ_idx = 0;
  1805. }
  1806. r_delayline[r_circ_idx] = *bp;
  1807. sum = 0;
  1808. c_idx = r_circ_idx;
  1809. for(int k = 0; k < num_chorus; k++) {
  1810. sum += r_delayline[c_idx];
  1811. if(num_chorus > 1)c_idx -= delay_length/(num_chorus - 1) - 1;
  1812. if(c_idx < 0) {
  1813. c_idx += delay_length;
  1814. }
  1815. }
  1816. *bp++ = sum/num_chorus;
  1817. }
  1818. // send the effect output to the left channel
  1819. transmit(block,1);
  1820. release(block);
  1821. }
  1822. }
  1823. /******************************************************************/
  1824. // A u d i o T o n e S w e e p
  1825. // Written by Pete (El Supremo) Feb 2014
  1826. boolean AudioToneSweep::begin(short t_amp,int t_lo,int t_hi,float t_time)
  1827. {
  1828. double tone_tmp;
  1829. if(0) {
  1830. Serial.print("AudioToneSweep.begin(tone_amp = ");
  1831. Serial.print(t_amp);
  1832. Serial.print(", tone_lo = ");
  1833. Serial.print(t_lo);
  1834. Serial.print(", tone_hi = ");
  1835. Serial.print(t_hi);
  1836. Serial.print(", tone_time = ");
  1837. Serial.print(t_time,1);
  1838. Serial.println(")");
  1839. }
  1840. tone_amp = 0;
  1841. if(t_amp < 0)return false;
  1842. if(t_lo < 1)return false;
  1843. if(t_hi < 1)return false;
  1844. if(t_hi >= 44100/2)return false;
  1845. if(t_lo >= 44100/2)return false;
  1846. if(t_time < 0)return false;
  1847. tone_lo = t_lo;
  1848. tone_hi = t_hi;
  1849. tone_phase = 0;
  1850. tone_amp = t_amp;
  1851. // Limit the output amplitude to prevent aliasing
  1852. // until I can figure out why this "overtops"
  1853. // above 29000.
  1854. if(tone_amp > 29000)tone_amp = 29000;
  1855. tone_tmp = tone_hi - tone_lo;
  1856. tone_sign = 1;
  1857. tone_freq = tone_lo*0x100000000LL;
  1858. if(tone_tmp < 0) {
  1859. tone_sign = -1;
  1860. tone_tmp = -tone_tmp;
  1861. }
  1862. tone_tmp = tone_tmp/t_time/44100.;
  1863. tone_incr = (tone_tmp * 0x100000000LL);
  1864. sweep_busy = 1;
  1865. return(true);
  1866. }
  1867. unsigned char AudioToneSweep::busy(void)
  1868. {
  1869. return(sweep_busy);
  1870. }
  1871. int b_count = 0;
  1872. void AudioToneSweep::update(void)
  1873. {
  1874. audio_block_t *block;
  1875. short *bp;
  1876. int i;
  1877. if(!sweep_busy)return;
  1878. // L E F T C H A N N E L O N L Y
  1879. block = allocate();
  1880. if(block) {
  1881. bp = block->data;
  1882. // Generate the sweep
  1883. for(i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  1884. *bp++ = (short)(( (short)(arm_sin_q31((uint32_t)((tone_phase >> 15)&0x7fffffff))>>16) *tone_amp) >> 16);
  1885. uint64_t tone_tmp = (0x400000000000LL * (int)((tone_freq >> 32)&0x7fffffff))/44100;
  1886. tone_phase += tone_tmp;
  1887. if(tone_phase & 0x800000000000LL)tone_phase &= 0x7fffffffffffLL;
  1888. if(tone_sign > 0) {
  1889. if((tone_freq >> 32) > tone_hi) {
  1890. sweep_busy = 0;
  1891. break;
  1892. }
  1893. tone_freq += tone_incr;
  1894. } else {
  1895. if((tone_freq >> 32) < tone_hi) {
  1896. sweep_busy = 0;
  1897. break;
  1898. }
  1899. tone_freq -= tone_incr;
  1900. }
  1901. }
  1902. while(i < AUDIO_BLOCK_SAMPLES) {
  1903. *bp++ = 0;
  1904. i++;
  1905. }
  1906. b_count++;
  1907. // send the samples to the left channel
  1908. transmit(block,0);
  1909. release(block);
  1910. }
  1911. }