選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

122 行
3.5KB

  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. // waveforms.c
  31. extern "C" {
  32. extern const int16_t AudioWaveformSine[257];
  33. }
  34. #define AUDIO_SAMPLE_RATE_ROUNDED (44118)
  35. #define DELAY_PASSTHRU -1
  36. #define WAVEFORM_SINE 0
  37. #define WAVEFORM_SAWTOOTH 1
  38. #define WAVEFORM_SQUARE 2
  39. #define WAVEFORM_TRIANGLE 3
  40. #define WAVEFORM_ARBITRARY 4
  41. // todo: remove these...
  42. #define TONE_TYPE_SINE 0
  43. #define TONE_TYPE_SAWTOOTH 1
  44. #define TONE_TYPE_SQUARE 2
  45. #define TONE_TYPE_TRIANGLE 3
  46. class AudioSynthWaveform :
  47. public AudioStream
  48. {
  49. public:
  50. AudioSynthWaveform(void) :
  51. AudioStream(0,NULL),
  52. tone_phase(0), tone_incr(0), tone_type(0),
  53. tone_offset(0), arbdata(NULL)
  54. {
  55. }
  56. void frequency(float t_freq) {
  57. if (t_freq < 0.0) t_freq = 0.0;
  58. else if (t_freq > AUDIO_SAMPLE_RATE_EXACT / 2) t_freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  59. tone_incr = (t_freq * (0x80000000LL/AUDIO_SAMPLE_RATE_EXACT)) + 0.5;
  60. }
  61. void phase(float angle) {
  62. if (angle < 0.0) angle = 0.0;
  63. else if (angle > 360.0) {
  64. angle = angle - 360.0;
  65. if (angle >= 360.0) return;
  66. }
  67. tone_phase = angle * (2147483648.0 / 360.0);
  68. }
  69. void amplitude(float n) { // 0 to 1.0
  70. if (n < 0) n = 0;
  71. else if (n > 1.0) n = 1.0;
  72. if ((tone_amp == 0) && n) {
  73. // reset the phase when the amplitude was zero
  74. // and has now been increased.
  75. tone_phase = 0;
  76. }
  77. // set new magnitude
  78. tone_amp = n * 32767.0;
  79. }
  80. void offset(float n) {
  81. if (n < -1.0) n = -1.0;
  82. else if (n > 1.0) n = 1.0;
  83. tone_offset = n * 32767.0;
  84. }
  85. void begin(short t_type) {
  86. tone_phase = 0;
  87. tone_type = t_type;
  88. }
  89. void begin(float t_amp, float t_freq, short t_type) {
  90. amplitude(t_amp);
  91. frequency(t_freq);
  92. begin(t_type);
  93. }
  94. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  95. arbdata = data;
  96. }
  97. virtual void update(void);
  98. private:
  99. short tone_amp;
  100. short tone_freq;
  101. uint32_t tone_phase;
  102. // volatile prevents the compiler optimizing out the frequency function
  103. volatile uint32_t tone_incr;
  104. short tone_type;
  105. int16_t tone_offset;
  106. const int16_t *arbdata;
  107. };
  108. #endif