Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

synth_tonesweep.cpp 3.6KB

pirms 10 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Pete (El Supremo)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "synth_tonesweep.h"
  23. #include "arm_math.h"
  24. /******************************************************************/
  25. // A u d i o S y n t h T o n e S w e e p
  26. // Written by Pete (El Supremo) Feb 2014
  27. boolean AudioSynthToneSweep::begin(short t_amp,int t_lo,int t_hi,float t_time)
  28. {
  29. double tone_tmp;
  30. if(0) {
  31. Serial.print("AudioSynthToneSweep.begin(tone_amp = ");
  32. Serial.print(t_amp);
  33. Serial.print(", tone_lo = ");
  34. Serial.print(t_lo);
  35. Serial.print(", tone_hi = ");
  36. Serial.print(t_hi);
  37. Serial.print(", tone_time = ");
  38. Serial.print(t_time,1);
  39. Serial.println(")");
  40. }
  41. tone_amp = 0;
  42. if(t_amp < 0)return false;
  43. if(t_lo < 1)return false;
  44. if(t_hi < 1)return false;
  45. if(t_hi >= 44100/2)return false;
  46. if(t_lo >= 44100/2)return false;
  47. if(t_time < 0)return false;
  48. tone_lo = t_lo;
  49. tone_hi = t_hi;
  50. tone_phase = 0;
  51. tone_amp = t_amp;
  52. // Limit the output amplitude to prevent aliasing
  53. // until I can figure out why this "overtops"
  54. // above 29000.
  55. if(tone_amp > 29000)tone_amp = 29000;
  56. tone_tmp = tone_hi - tone_lo;
  57. tone_sign = 1;
  58. tone_freq = tone_lo*0x100000000LL;
  59. if(tone_tmp < 0) {
  60. tone_sign = -1;
  61. tone_tmp = -tone_tmp;
  62. }
  63. tone_tmp = tone_tmp/t_time/44100.;
  64. tone_incr = (tone_tmp * 0x100000000LL);
  65. sweep_busy = 1;
  66. return(true);
  67. }
  68. unsigned char AudioSynthToneSweep::busy(void)
  69. {
  70. return(sweep_busy);
  71. }
  72. void AudioSynthToneSweep::update(void)
  73. {
  74. audio_block_t *block;
  75. short *bp;
  76. int i;
  77. if(!sweep_busy)return;
  78. // L E F T C H A N N E L O N L Y
  79. block = allocate();
  80. if(block) {
  81. bp = block->data;
  82. // Generate the sweep
  83. for(i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  84. *bp++ = (short)(( (short)(arm_sin_q31((uint32_t)((tone_phase >> 15)&0x7fffffff))>>16) *tone_amp) >> 16);
  85. uint64_t tone_tmp = (0x400000000000LL * (int)((tone_freq >> 32)&0x7fffffff))/44100;
  86. tone_phase += tone_tmp;
  87. if(tone_phase & 0x800000000000LL)tone_phase &= 0x7fffffffffffLL;
  88. if(tone_sign > 0) {
  89. if((tone_freq >> 32) > tone_hi) {
  90. sweep_busy = 0;
  91. break;
  92. }
  93. tone_freq += tone_incr;
  94. } else {
  95. if((tone_freq >> 32) < tone_hi) {
  96. sweep_busy = 0;
  97. break;
  98. }
  99. tone_freq -= tone_incr;
  100. }
  101. }
  102. while(i < AUDIO_BLOCK_SAMPLES) {
  103. *bp++ = 0;
  104. i++;
  105. }
  106. //b_count++;
  107. // send the samples to the left channel
  108. transmit(block,0);
  109. release(block);
  110. }
  111. }