You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.9KB

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