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

111 行
2.5KB

  1. #include "Audio.h"
  2. #include "arm_math.h"
  3. #include "utility/dspinst.h"
  4. // TODO: this object should be renamed to AudioSynthToneSweep
  5. /******************************************************************/
  6. // A u d i o T o n e S w e e p
  7. // Written by Pete (El Supremo) Feb 2014
  8. boolean AudioToneSweep::begin(short t_amp,int t_lo,int t_hi,float t_time)
  9. {
  10. double tone_tmp;
  11. if(0) {
  12. Serial.print("AudioToneSweep.begin(tone_amp = ");
  13. Serial.print(t_amp);
  14. Serial.print(", tone_lo = ");
  15. Serial.print(t_lo);
  16. Serial.print(", tone_hi = ");
  17. Serial.print(t_hi);
  18. Serial.print(", tone_time = ");
  19. Serial.print(t_time,1);
  20. Serial.println(")");
  21. }
  22. tone_amp = 0;
  23. if(t_amp < 0)return false;
  24. if(t_lo < 1)return false;
  25. if(t_hi < 1)return false;
  26. if(t_hi >= 44100/2)return false;
  27. if(t_lo >= 44100/2)return false;
  28. if(t_time < 0)return false;
  29. tone_lo = t_lo;
  30. tone_hi = t_hi;
  31. tone_phase = 0;
  32. tone_amp = t_amp;
  33. // Limit the output amplitude to prevent aliasing
  34. // until I can figure out why this "overtops"
  35. // above 29000.
  36. if(tone_amp > 29000)tone_amp = 29000;
  37. tone_tmp = tone_hi - tone_lo;
  38. tone_sign = 1;
  39. tone_freq = tone_lo*0x100000000LL;
  40. if(tone_tmp < 0) {
  41. tone_sign = -1;
  42. tone_tmp = -tone_tmp;
  43. }
  44. tone_tmp = tone_tmp/t_time/44100.;
  45. tone_incr = (tone_tmp * 0x100000000LL);
  46. sweep_busy = 1;
  47. return(true);
  48. }
  49. unsigned char AudioToneSweep::busy(void)
  50. {
  51. return(sweep_busy);
  52. }
  53. int b_count = 0;
  54. void AudioToneSweep::update(void)
  55. {
  56. audio_block_t *block;
  57. short *bp;
  58. int i;
  59. if(!sweep_busy)return;
  60. // L E F T C H A N N E L O N L Y
  61. block = allocate();
  62. if(block) {
  63. bp = block->data;
  64. // Generate the sweep
  65. for(i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  66. *bp++ = (short)(( (short)(arm_sin_q31((uint32_t)((tone_phase >> 15)&0x7fffffff))>>16) *tone_amp) >> 16);
  67. uint64_t tone_tmp = (0x400000000000LL * (int)((tone_freq >> 32)&0x7fffffff))/44100;
  68. tone_phase += tone_tmp;
  69. if(tone_phase & 0x800000000000LL)tone_phase &= 0x7fffffffffffLL;
  70. if(tone_sign > 0) {
  71. if((tone_freq >> 32) > tone_hi) {
  72. sweep_busy = 0;
  73. break;
  74. }
  75. tone_freq += tone_incr;
  76. } else {
  77. if((tone_freq >> 32) < tone_hi) {
  78. sweep_busy = 0;
  79. break;
  80. }
  81. tone_freq -= tone_incr;
  82. }
  83. }
  84. while(i < AUDIO_BLOCK_SAMPLES) {
  85. *bp++ = 0;
  86. i++;
  87. }
  88. b_count++;
  89. // send the samples to the left channel
  90. transmit(block,0);
  91. release(block);
  92. }
  93. }