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.

106 satır
3.3KB

  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. #ifndef synth_waveform_h_
  27. #define synth_waveform_h_
  28. #include "AudioStream.h"
  29. #include "arm_math.h"
  30. #define AUDIO_SAMPLE_RATE_ROUNDED (44118)
  31. #define DELAY_PASSTHRU -1
  32. #define TONE_TYPE_SINE 0
  33. #define TONE_TYPE_SAWTOOTH 1
  34. #define TONE_TYPE_SQUARE 2
  35. #define TONE_TYPE_TRIANGLE 3
  36. class AudioSynthWaveform :
  37. public AudioStream
  38. {
  39. public:
  40. AudioSynthWaveform(void) :
  41. AudioStream(0,NULL),
  42. tone_freq(0), tone_phase(0), tone_incr(0), tone_type(0),
  43. ramp_down(0), ramp_up(0), ramp_length(0)
  44. {
  45. }
  46. void frequency(int t_hi)
  47. {
  48. tone_incr = (0x80000000LL*t_hi)/AUDIO_SAMPLE_RATE_EXACT;
  49. }
  50. // If ramp_length is non-zero this will set up
  51. // either a rmap up or a ramp down when a wave
  52. // first starts or when the amplitude is set
  53. // back to zero.
  54. // Note that if the ramp_length is N, the generated
  55. // wave will be N samples longer than when it is not
  56. // ramp
  57. void amplitude(float n) { // 0 to 1.0
  58. if (n < 0) n = 0;
  59. else if (n > 1.0) n = 1.0;
  60. // Ramp code
  61. if(tone_amp && (n == 0)) {
  62. ramp_down = ramp_length;
  63. ramp_up = 0;
  64. last_tone_amp = tone_amp;
  65. }
  66. else if((tone_amp == 0) && n) {
  67. ramp_up = ramp_length;
  68. ramp_down = 0;
  69. // reset the phase when the amplitude was zero
  70. // and has now been increased. Note that this
  71. // happens even if the wave is not ramped
  72. // so that the signal starts at zero
  73. tone_phase = 0;
  74. }
  75. // set new magnitude
  76. tone_amp = n * 32767.0;
  77. }
  78. boolean begin(float t_amp,int t_hi,short t_type);
  79. virtual void update(void);
  80. void set_ramp_length(int16_t r_length);
  81. private:
  82. short tone_amp;
  83. short last_tone_amp;
  84. short tone_freq;
  85. uint32_t tone_phase;
  86. // volatile prevents the compiler optimizing out the frequency function
  87. volatile uint32_t tone_incr;
  88. short tone_type;
  89. uint32_t ramp_down;
  90. uint32_t ramp_up;
  91. uint16_t ramp_length;
  92. };
  93. #endif