ERIS CORE
eris_synth_waveform.h
Go to the documentation of this file.
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 
27 #ifndef eris_synth_waveform_h_
28 #define eris_synth_waveform_h_
29 #include <Arduino.h>
30 #include "eris_waveshapes.h"
31 #include "synth_waveform.h"
32 #include "AudioStream.h"
33 #include "arm_math.h"
34 
35 
36 // waveforms.c
37 extern "C" {
38 extern const int16_t AudioWaveformSine[257];
39 }
40 
42 {
43 public:
46  magnitude(0), pulse_width(0x40000000),
47  arbdata(NULL), sample(0), tone_type(WAVEFORM_SINE),
48  tone_offset(0) {
49  }
50 
51  void frequency(float freq) {
52  if (freq < 0.0f) {
53  freq = 0.0;
54  } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2.0f) {
55  freq = AUDIO_SAMPLE_RATE_EXACT / 2.0f;
56  }
57  phase_increment = freq * (4294967296.0f / AUDIO_SAMPLE_RATE_EXACT);
58  if (phase_increment > 0x7FFE0000u) phase_increment = 0x7FFE0000;
59  }
60  void phase(float angle) {
61  if (angle < 0.0f) {
62  angle = 0.0;
63  } else if (angle > 360.0f) {
64  angle = angle - 360.0f;
65  if (angle >= 360.0f) return;
66  }
67  phase_offset = angle * (float)(4294967296.0 / 360.0);
68  }
69  //get phase added for Eris Core
70  float getPhase() {
71  return phase_offset/(4294967296.0 / 360.0);
72  }
73  void amplitude(float n) { // 0 to 1.0
74  if (n < 0) {
75  n = 0;
76  } else if (n > 1.0f) {
77  n = 1.0;
78  }
79  magnitude = n * 65536.0f;
80  }
81  void offset(float n) {
82  if (n < -1.0f) {
83  n = -1.0f;
84  } else if (n > 1.0f) {
85  n = 1.0f;
86  }
87  tone_offset = n * 32767.0f;
88  }
89  void pulseWidth(float n) { // 0.0 to 1.0
90  if (n < 0) {
91  n = 0;
92  } else if (n > 1.0f) {
93  n = 1.0f;
94  }
95  pulse_width = n * 4294967296.0f;
96  }
97  void begin(short t_type) {
98  phase_offset = 0;
99  tone_type = t_type;
100  if (t_type == WAVEFORM_BANDLIMIT_SQUARE)
101  band_limit_waveform.init_square (phase_increment) ;
102  else if (t_type == WAVEFORM_BANDLIMIT_PULSE)
104  else if (t_type == WAVEFORM_BANDLIMIT_SAWTOOTH || t_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE)
105  band_limit_waveform.init_sawtooth (phase_increment) ;
106  }
107  void begin(float t_amp, float t_freq, short t_type) {
108  amplitude(t_amp);
109  frequency(t_freq);
110  phase_offset = 0;
111  begin (t_type);
112  }
113  //added for Eris Core
114  void arbitraryProgram(uint8_t program);
115  void arbitraryWaveform(const int16_t *data, float maxFreq) {
116  arbdata = data;
117  }
118  virtual void update(void);
119 
120 private:
122  uint32_t phase_increment;
123  uint32_t phase_offset;
124  int32_t magnitude;
125  uint32_t pulse_width;
126  const int16_t *arbdata;
127  int16_t sample; // for WAVEFORM_SAMPLE_HOLD
128  short tone_type;
129  int16_t tone_offset;
130  BandLimitedWaveform band_limit_waveform ;
131 };
132 
133 
135 {
136 public:
139  magnitude(0), arbdata(NULL), sample(0), tone_offset(0),
140  tone_type(WAVEFORM_SINE), modulation_type(0) {
141  }
142 
143  void frequency(float freq) {
144  if (freq < 0.0f) {
145  freq = 0.0;
146  } else if (freq > AUDIO_SAMPLE_RATE_EXACT / 2.0f) {
147  freq = AUDIO_SAMPLE_RATE_EXACT / 2.0f;
148  }
149  phase_increment = freq * (4294967296.0f / AUDIO_SAMPLE_RATE_EXACT);
150  if (phase_increment > 0x7FFE0000u) phase_increment = 0x7FFE0000;
151  }
152  void amplitude(float n) { // 0 to 1.0
153  if (n < 0) {
154  n = 0;
155  } else if (n > 1.0f) {
156  n = 1.0f;
157  }
158  magnitude = n * 65536.0f;
159  }
160  void offset(float n) {
161  if (n < -1.0f) {
162  n = -1.0f;
163  } else if (n > 1.0f) {
164  n = 1.0f;
165  }
166  tone_offset = n * 32767.0f;
167  }
168  void begin(short t_type) {
169  tone_type = t_type;
170  if (t_type == WAVEFORM_BANDLIMIT_SQUARE)
171  band_limit_waveform.init_square (phase_increment) ;
172  else if (t_type == WAVEFORM_BANDLIMIT_PULSE)
173  band_limit_waveform.init_pulse (phase_increment, 0x80000000u) ;
174  else if (t_type == WAVEFORM_BANDLIMIT_SAWTOOTH || t_type == WAVEFORM_BANDLIMIT_SAWTOOTH_REVERSE)
175  band_limit_waveform.init_sawtooth (phase_increment) ;
176  }
177  void begin(float t_amp, float t_freq, short t_type) {
178  amplitude(t_amp);
179  frequency(t_freq);
180  begin (t_type) ;
181  }
182  //added for Eris Core
183  void arbitraryProgram(uint8_t program);
184  void arbitraryWaveform(const int16_t *data, float maxFreq) {
185  arbdata = data;
186  }
187  void frequencyModulation(float octaves) {
188  if (octaves > 12.0f) {
189  octaves = 12.0f;
190  } else if (octaves < 0.1f) {
191  octaves = 0.1f;
192  }
193  modulation_factor = octaves * 4096.0f;
194  modulation_type = 0;
195  }
196  void phaseModulation(float degrees) {
197  if (degrees > 9000.0f) {
198  degrees = 9000.0f;
199  } else if (degrees < 30.0f) {
200  degrees = 30.0f;
201  }
202  modulation_factor = degrees * (float)(65536.0 / 180.0);
203  modulation_type = 1;
204  }
205  virtual void update(void);
206 
207 private:
210  uint32_t phase_increment;
212  int32_t magnitude;
213  const int16_t *arbdata;
214  uint32_t phasedata[AUDIO_BLOCK_SAMPLES];
215  int16_t sample; // for WAVEFORM_SAMPLE_HOLD
216  int16_t tone_offset;
217  uint8_t tone_type;
219  BandLimitedWaveform band_limit_waveform ;
220 };
221 
222 
223 #endif
void begin(float t_amp, float t_freq, short t_type)
void arbitraryWaveform(const int16_t *data, float maxFreq)
void arbitraryProgram(uint8_t program)
BandLimitedWaveform band_limit_waveform
void frequencyModulation(float octaves)
uint32_t phasedata[AUDIO_BLOCK_SAMPLES]
void arbitraryWaveform(const int16_t *data, float maxFreq)
BandLimitedWaveform band_limit_waveform
void begin(float t_amp, float t_freq, short t_type)
void frequency(float freq)
void begin(short t_type)
void arbitraryProgram(uint8_t program)
virtual void update(void)
void phase(float angle)
const int16_t AudioWaveformSine[257]