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.

110 lines
2.5KB

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