synth_tonesweep.cpp 3.6KB

8 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <Arduino.h>
  23. #include "synth_tonesweep.h"
  24. #include "arm_math.h"
  25. /******************************************************************/
  26. // A u d i o S y n t h T o n e S w e e p
  27. // Written by Pete (El Supremo) Feb 2014
  28. boolean AudioSynthToneSweep::play(float t_amp,int t_lo,int t_hi,float t_time)
  29. {
  30. float tone_tmp;
  31. if(0) {
  32. Serial.print("AudioSynthToneSweep.begin(tone_amp = ");
  33. Serial.print(t_amp);
  34. Serial.print(", tone_lo = ");
  35. Serial.print(t_lo);
  36. Serial.print(", tone_hi = ");
  37. Serial.print(t_hi);
  38. Serial.print(", tone_time = ");
  39. Serial.print(t_time,1);
  40. Serial.println(")");
  41. }
  42. tone_amp = 0;
  43. if(t_amp < 0)return false;
  44. if(t_amp > 1)return false;
  45. if(t_lo < 1)return false;
  46. if(t_hi < 1)return false;
  47. if(t_hi >= (int) AUDIO_SAMPLE_RATE_EXACT / 2)return false;
  48. if(t_lo >= (int) AUDIO_SAMPLE_RATE_EXACT / 2)return false;
  49. if(t_time <= 0)return false;
  50. tone_lo = t_lo;
  51. tone_hi = t_hi;
  52. tone_phase = 0;
  53. tone_amp = t_amp * 32767.0;
  54. tone_freq = tone_lo*0x100000000LL;
  55. if (tone_hi >= tone_lo) {
  56. tone_tmp = tone_hi - tone_lo;
  57. tone_sign = 1;
  58. } else {
  59. tone_sign = -1;
  60. tone_tmp = tone_lo - tone_hi;
  61. }
  62. tone_tmp = tone_tmp / t_time / AUDIO_SAMPLE_RATE_EXACT;
  63. tone_incr = (tone_tmp * 0x100000000LL);
  64. sweep_busy = 1;
  65. return(true);
  66. }
  67. unsigned char AudioSynthToneSweep::isPlaying(void)
  68. {
  69. return(sweep_busy);
  70. }
  71. void AudioSynthToneSweep::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. uint32_t tmp = tone_freq >> 32;
  82. uint64_t tone_tmp = (tone_freq << 14) / (int) AUDIO_SAMPLE_RATE_EXACT;
  83. uint64_t incr = (tone_incr << 14) / (int) AUDIO_SAMPLE_RATE_EXACT;
  84. // Generate the sweep
  85. for(i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  86. *bp++ = (short)(( (short)(arm_sin_q31((uint32_t)((tone_phase >> 15)&0x7fffffff))>>16) *tone_amp) >> 15);
  87. tone_phase += tone_tmp;
  88. tone_tmp += incr ;
  89. if(tone_sign > 0) {
  90. if(tmp > tone_hi) {
  91. sweep_busy = 0;
  92. break;
  93. }
  94. tone_freq += tone_incr;
  95. } else {
  96. if(tmp < tone_hi || tone_freq < tone_incr) {
  97. sweep_busy = 0;
  98. break;
  99. }
  100. tone_freq -= tone_incr;
  101. }
  102. }
  103. while(i < AUDIO_BLOCK_SAMPLES) {
  104. *bp++ = 0;
  105. i++;
  106. }
  107. // send the samples to the left channel
  108. transmit(block,0);
  109. release(block);
  110. }
  111. }