Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

133 lines
3.7KB

  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. // TODO: this object should be renamed to AudioSynthToneSweep
  25. /******************************************************************/
  26. // A u d i o T o n e S w e e p
  27. // Written by Pete (El Supremo) Feb 2014
  28. boolean AudioSynthToneSweep::begin(short t_amp,int t_lo,int t_hi,float t_time)
  29. {
  30. double 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_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;
  53. // Limit the output amplitude to prevent aliasing
  54. // until I can figure out why this "overtops"
  55. // above 29000.
  56. if(tone_amp > 29000)tone_amp = 29000;
  57. tone_tmp = tone_hi - tone_lo;
  58. tone_sign = 1;
  59. tone_freq = tone_lo*0x100000000LL;
  60. if(tone_tmp < 0) {
  61. tone_sign = -1;
  62. tone_tmp = -tone_tmp;
  63. }
  64. tone_tmp = tone_tmp/t_time/44100.;
  65. tone_incr = (tone_tmp * 0x100000000LL);
  66. sweep_busy = 1;
  67. return(true);
  68. }
  69. unsigned char AudioSynthToneSweep::busy(void)
  70. {
  71. return(sweep_busy);
  72. }
  73. static int b_count = 0; // TODO: what's this for?
  74. void AudioSynthToneSweep::update(void)
  75. {
  76. audio_block_t *block;
  77. short *bp;
  78. int i;
  79. if(!sweep_busy)return;
  80. // L E F T C H A N N E L O N L Y
  81. block = allocate();
  82. if(block) {
  83. bp = block->data;
  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) >> 16);
  87. uint64_t tone_tmp = (0x400000000000LL * (int)((tone_freq >> 32)&0x7fffffff))/44100;
  88. tone_phase += tone_tmp;
  89. if(tone_phase & 0x800000000000LL)tone_phase &= 0x7fffffffffffLL;
  90. if(tone_sign > 0) {
  91. if((tone_freq >> 32) > tone_hi) {
  92. sweep_busy = 0;
  93. break;
  94. }
  95. tone_freq += tone_incr;
  96. } else {
  97. if((tone_freq >> 32) < tone_hi) {
  98. sweep_busy = 0;
  99. break;
  100. }
  101. tone_freq -= tone_incr;
  102. }
  103. }
  104. while(i < AUDIO_BLOCK_SAMPLES) {
  105. *bp++ = 0;
  106. i++;
  107. }
  108. b_count++;
  109. // send the samples to the left channel
  110. transmit(block,0);
  111. release(block);
  112. }
  113. }