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.

synth_waveform.h 3.6KB

10 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. class AudioSynthWaveform : public AudioStream
  44. {
  45. public:
  46. AudioSynthWaveform(void) : AudioStream(0,NULL),
  47. phase_accumulator(0), phase_increment(0), phase_offset(0),
  48. magnitude(0), pulse_width(0x40000000),
  49. arbdata(NULL), sample(0), tone_type(WAVEFORM_SINE),
  50. tone_offset(0) {
  51. }
  52. void frequency(float freq) {
  53. if (freq < 0.0) {
  54. freq = 0.0;
  55. } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2) {
  56. freq = AUDIO_SAMPLE_RATE_EXACT / 2;
  57. }
  58. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  59. //tone_incr = (t_freq * (0x80000000LL/AUDIO_SAMPLE_RATE_EXACT)) + 0.5;
  60. }
  61. void phase(float angle) {
  62. if (angle < 0.0) {
  63. angle = 0.0;
  64. } else if (angle > 360.0) {
  65. angle = angle - 360.0;
  66. if (angle >= 360.0) return;
  67. }
  68. phase_offset = angle * (4294967296.0 / 360.0);
  69. }
  70. void amplitude(float n) { // 0 to 1.0
  71. if (n < 0) {
  72. n = 0;
  73. } else if (n > 1.0) {
  74. n = 1.0;
  75. }
  76. magnitude = n * 65536.0;
  77. }
  78. void offset(float n) {
  79. if (n < -1.0) {
  80. n = -1.0;
  81. } else if (n > 1.0) {
  82. n = 1.0;
  83. }
  84. tone_offset = n * 32767.0;
  85. }
  86. void pulseWidth(float n) { // 0.0 to 1.0
  87. if (n < 0) {
  88. n = 0;
  89. } else if (n > 1.0) {
  90. n = 1.0;
  91. }
  92. pulse_width = n * 4294967296.0;
  93. }
  94. void begin(short t_type) {
  95. phase_offset = 0;
  96. tone_type = t_type;
  97. }
  98. void begin(float t_amp, float t_freq, short t_type) {
  99. amplitude(t_amp);
  100. frequency(t_freq);
  101. phase_offset = 0;
  102. tone_type = t_type;
  103. }
  104. void arbitraryWaveform(const int16_t *data, float maxFreq) {
  105. arbdata = data;
  106. }
  107. virtual void update(void);
  108. private:
  109. uint32_t phase_accumulator;
  110. uint32_t phase_increment;
  111. uint32_t phase_offset;
  112. int32_t magnitude;
  113. uint32_t pulse_width;
  114. const int16_t *arbdata;
  115. int16_t sample; // for WAVEFORM_SAMPLE_HOLD
  116. short tone_type;
  117. int16_t tone_offset;
  118. };
  119. #endif