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.

synth_tonesweep.cpp 3.4KB

10 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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::play(float 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_amp > 1)return false;
  44. if(t_lo < 1)return false;
  45. if(t_hi < 1)return false;
  46. if(t_hi >= 44100/2)return false;
  47. if(t_lo >= 44100/2)return false;
  48. if(t_time < 0)return false;
  49. tone_lo = t_lo;
  50. tone_hi = t_hi;
  51. tone_phase = 0;
  52. tone_amp = t_amp * 32767.0;
  53. tone_tmp = tone_hi - tone_lo;
  54. tone_sign = 1;
  55. tone_freq = tone_lo*0x100000000LL;
  56. if(tone_tmp < 0) {
  57. tone_sign = -1;
  58. tone_tmp = -tone_tmp;
  59. }
  60. tone_tmp = tone_tmp/t_time/44100.;
  61. tone_incr = (tone_tmp * 0x100000000LL);
  62. sweep_busy = 1;
  63. return(true);
  64. }
  65. unsigned char AudioSynthToneSweep::isPlaying(void)
  66. {
  67. return(sweep_busy);
  68. }
  69. void AudioSynthToneSweep::update(void)
  70. {
  71. audio_block_t *block;
  72. short *bp;
  73. int i;
  74. if(!sweep_busy)return;
  75. // L E F T C H A N N E L O N L Y
  76. block = allocate();
  77. if(block) {
  78. bp = block->data;
  79. // Generate the sweep
  80. for(i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  81. *bp++ = (short)(( (short)(arm_sin_q31((uint32_t)((tone_phase >> 15)&0x7fffffff))>>16) *tone_amp) >> 16);
  82. uint64_t tone_tmp = (0x400000000000LL * (int)((tone_freq >> 32)&0x7fffffff))/44100;
  83. tone_phase += tone_tmp;
  84. if(tone_phase & 0x800000000000LL)tone_phase &= 0x7fffffffffffLL;
  85. if(tone_sign > 0) {
  86. if((tone_freq >> 32) > tone_hi) {
  87. sweep_busy = 0;
  88. break;
  89. }
  90. tone_freq += tone_incr;
  91. } else {
  92. if((tone_freq >> 32) < tone_hi) {
  93. sweep_busy = 0;
  94. break;
  95. }
  96. tone_freq -= tone_incr;
  97. }
  98. }
  99. while(i < AUDIO_BLOCK_SAMPLES) {
  100. *bp++ = 0;
  101. i++;
  102. }
  103. //b_count++;
  104. // send the samples to the left channel
  105. transmit(block,0);
  106. release(block);
  107. }
  108. }