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.

преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "synth_waveform.h"
  27. #include "arm_math.h"
  28. #include "utility/dspinst.h"
  29. /******************************************************************/
  30. // PAH 140415 - change sin to use Paul's interpolation which is much
  31. // faster than arm's sin function
  32. // PAH 140316 - fix calculation of sample (amplitude error)
  33. // PAH 140314 - change t_hi from int to float
  34. boolean AudioSynthWaveform::begin(float t_amp,float t_hi,short type)
  35. {
  36. tone_type = type;
  37. amplitude(t_amp);
  38. tone_freq = t_hi > 0.0;
  39. if(t_hi <= 0.0)return false;
  40. if(t_hi >= AUDIO_SAMPLE_RATE_EXACT/2)return false;
  41. tone_phase = 0;
  42. frequency(t_hi);
  43. if(0) {
  44. Serial.print("AudioSynthWaveform.begin(tone_amp = ");
  45. Serial.print(t_amp);
  46. Serial.print(", tone_hi = ");
  47. Serial.print(t_hi);
  48. Serial.print(", tone_incr = ");
  49. Serial.print(tone_incr,HEX);
  50. // Serial.print(", tone_hi = ");
  51. // Serial.print(t_hi);
  52. Serial.println(")");
  53. }
  54. return(true);
  55. }
  56. void AudioSynthWaveform::update(void)
  57. {
  58. audio_block_t *block;
  59. short *bp;
  60. int32_t val1, val2, val3;
  61. uint32_t index, scale;
  62. // temporaries for TRIANGLE
  63. uint32_t mag;
  64. short tmp_amp;
  65. if(tone_freq == 0)return;
  66. block = allocate();
  67. if(block) {
  68. bp = block->data;
  69. switch(tone_type) {
  70. case TONE_TYPE_SINE:
  71. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  72. // Calculate interpolated sin
  73. index = tone_phase >> 23;
  74. val1 = AudioWaveformSine[index];
  75. val2 = AudioWaveformSine[index+1];
  76. scale = (tone_phase >> 7) & 0xFFFF;
  77. val2 *= scale;
  78. val1 *= 0xFFFF - scale;
  79. val3 = (val1 + val2) >> 16;
  80. *bp++ = (short)((val3 * tone_amp) >> 15);
  81. // phase and incr are both unsigned 32-bit fractions
  82. tone_phase += tone_incr;
  83. // If tone_phase has overflowed, truncate the top bit
  84. if(tone_phase & 0x80000000)tone_phase &= 0x7fffffff;
  85. }
  86. break;
  87. case TONE_TYPE_SQUARE:
  88. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  89. if(tone_phase & 0x40000000)*bp++ = -tone_amp;
  90. else *bp++ = tone_amp;
  91. // phase and incr are both unsigned 32-bit fractions
  92. tone_phase += tone_incr;
  93. }
  94. break;
  95. case TONE_TYPE_SAWTOOTH:
  96. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  97. *bp++ = ((short)(tone_phase>>15)*tone_amp) >> 15;
  98. // phase and incr are both unsigned 32-bit fractions
  99. tone_phase += tone_incr;
  100. }
  101. break;
  102. case TONE_TYPE_TRIANGLE:
  103. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  104. if(tone_phase & 0x80000000) {
  105. // negative half-cycle
  106. tmp_amp = -tone_amp;
  107. }
  108. else {
  109. // positive half-cycle
  110. tmp_amp = tone_amp;
  111. }
  112. mag = tone_phase << 2;
  113. // Determine which quadrant
  114. if(tone_phase & 0x40000000) {
  115. // negate the magnitude
  116. mag = ~mag + 1;
  117. }
  118. *bp++ = ((short)(mag>>17)*tmp_amp) >> 15;
  119. tone_phase += 2*tone_incr;
  120. }
  121. break;
  122. }
  123. // send the samples to the left channel
  124. transmit(block,0);
  125. release(block);
  126. }
  127. }