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.

132 line
3.7KB

  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 <Arduino.h>
  29. #include "AudioStream.h"
  30. #include "arm_math.h"
  31. // waveforms.c
  32. extern "C" {
  33. extern const int16_t AudioWaveformSine[257];
  34. }
  35. #define WAVEFORM_SINE 0
  36. #define WAVEFORM_SAWTOOTH 1
  37. #define WAVEFORM_SQUARE 2
  38. #define WAVEFORM_TRIANGLE 3
  39. #define WAVEFORM_ARBITRARY 4
  40. #define WAVEFORM_PULSE 5
  41. #define WAVEFORM_SAWTOOTH_REVERSE 6
  42. #define WAVEFORM_SAMPLE_HOLD 7
  43. #define WAVEFORM_TRIANGLE_VARIABLE 8
  44. class AudioSynthWaveform : public AudioStream
  45. {
  46. public:
  47. AudioSynthWaveform(void) : AudioStream(0,NULL),
  48. phase_accumulator(0), phase_increment(0), phase_offset(0),
  49. magnitude(0), pulse_width(0x40000000),
  50. arbdata(NULL), sample(0), tone_type(WAVEFORM_SINE),
  51. tone_offset(0) {
  52. }
  53. void frequency(float freq) {
  54. if (freq < 0.0) {
  55. freq = 0.0;
  56. } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2) {
  57. freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  58. }
  59. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  60. //tone_incr = (t_freq * (0x80000000LL/AUDIO_SAMPLE_RATE_EXACT)) + 0.5;
  61. }
  62. void phase(float angle) {
  63. if (angle < 0.0) {
  64. angle = 0.0;
  65. } else if (angle > 360.0) {
  66. angle = angle - 360.0;
  67. if (angle >= 360.0) return;
  68. }
  69. phase_offset = angle * (4294967296.0 / 360.0);
  70. }
  71. void amplitude(float n) { // 0 to 1.0
  72. if (n < 0) {
  73. n = 0;
  74. } else if (n > 1.0) {
  75. n = 1.0;
  76. }
  77. magnitude = n * 65536.0;
  78. }
  79. void offset(float n) {
  80. if (n < -1.0) {
  81. n = -1.0;
  82. } else if (n > 1.0) {
  83. n = 1.0;
  84. }
  85. tone_offset = n * 32767.0;
  86. }
  87. void pulseWidth(float n) { // 0.0 to 1.0
  88. if (n < 0) {
  89. n = 0;
  90. } else if (n > 1.0) {
  91. n = 1.0;
  92. }
  93. pulse_width = n * 4294967296.0;
  94. }
  95. void begin(short t_type) {
  96. phase_offset = 0;
  97. tone_type = t_type;
  98. }
  99. void begin(float t_amp, float t_freq, short t_type) {
  100. amplitude(t_amp);
  101. frequency(t_freq);
  102. phase_offset = 0;
  103. tone_type = t_type;
  104. }
  105. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  106. arbdata = data;
  107. }
  108. virtual void update(void);
  109. private:
  110. uint32_t phase_accumulator;
  111. uint32_t phase_increment;
  112. uint32_t phase_offset;
  113. int32_t magnitude;
  114. uint32_t pulse_width;
  115. const int16_t *arbdata;
  116. int16_t sample; // for WAVEFORM_SAMPLE_HOLD
  117. short tone_type;
  118. int16_t tone_offset;
  119. };
  120. #endif