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.

131 line
3.5KB

  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_freq = tone_lo*0x100000000LL;
  54. if (tone_hi >= tone_lo) {
  55. tone_tmp = tone_hi - tone_lo;
  56. tone_sign = 1;
  57. } else {
  58. tone_sign = -1;
  59. tone_tmp = tone_lo - tone_hi;
  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 AudioSynthToneSweep::isPlaying(void)
  67. {
  68. return(sweep_busy);
  69. }
  70. void AudioSynthToneSweep::update(void)
  71. {
  72. audio_block_t *block;
  73. short *bp;
  74. int i;
  75. if(!sweep_busy)return;
  76. // L E F T C H A N N E L O N L Y
  77. block = allocate();
  78. if(block) {
  79. bp = block->data;
  80. // Generate the sweep
  81. for(i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  82. *bp++ = (short)(( (short)(arm_sin_q31((uint32_t)((tone_phase >> 15)&0x7fffffff))>>16) *tone_amp) >> 16);
  83. uint64_t tone_tmp = (0x400000000000LL * (int)((tone_freq >> 32)&0x7fffffff))/44100;
  84. tone_phase += tone_tmp;
  85. if(tone_phase & 0x800000000000LL)tone_phase &= 0x7fffffffffffLL;
  86. if(tone_sign > 0) {
  87. if((tone_freq >> 32) > tone_hi) {
  88. sweep_busy = 0;
  89. break;
  90. }
  91. tone_freq += tone_incr;
  92. } else {
  93. if((tone_freq >> 32) < tone_hi) {
  94. sweep_busy = 0;
  95. break;
  96. }
  97. tone_freq -= tone_incr;
  98. }
  99. }
  100. while(i < AUDIO_BLOCK_SAMPLES) {
  101. *bp++ = 0;
  102. i++;
  103. }
  104. //b_count++;
  105. // send the samples to the left channel
  106. transmit(block,0);
  107. release(block);
  108. }
  109. }