// demonstrate pulse with slow changes in width | |||||
// demonstrate pulse with slow changes in pulse width | |||||
#include <Audio.h> | #include <Audio.h> | ||||
#include <Wire.h> | #include <Wire.h> | ||||
audioShield.enable(); | audioShield.enable(); | ||||
audioShield.volume(0.45); | audioShield.volume(0.45); | ||||
waveform1.width(0.5); | |||||
waveform1.pulseWidth(0.5); | |||||
waveform1.begin(0.4, 220, WAVEFORM_PULSE); | waveform1.begin(0.4, 220, WAVEFORM_PULSE); | ||||
envelope1.attack(50); | envelope1.attack(50); | ||||
float w; | float w; | ||||
for (uint32_t i =1; i<20; i++) { | for (uint32_t i =1; i<20; i++) { | ||||
w = i / 20.0; | w = i / 20.0; | ||||
waveform1.width(w); | |||||
waveform1.pulseWidth(w); | |||||
envelope1.noteOn(); | envelope1.noteOn(); | ||||
delay(800); | delay(800); | ||||
envelope1.noteOff(); | envelope1.noteOff(); |
</script> | </script> | ||||
<script type="text/x-red" data-help-name="AudioSynthWaveform"> | <script type="text/x-red" data-help-name="AudioSynthWaveform"> | ||||
<h3>Summary</h3> | <h3>Summary</h3> | ||||
<p>Create a waveform: sine, sawtooth, square, or triangle</p> | |||||
<p>Create a waveform: sine, sawtooth, square, triangle, pulse or arbitrary.</p> | |||||
<h3>Audio Connections</h3> | <h3>Audio Connections</h3> | ||||
<table class=doc align=center cellpadding=3> | <table class=doc align=center cellpadding=3> | ||||
<tr class=top><th>Port</th><th>Purpose</th></tr> | <tr class=top><th>Port</th><th>Purpose</th></tr> | ||||
<a href="http://www.pjrc.com/teensy/td_libs_AudioProcessorUsage.html" target="_blank">AudioNoInterrupts()</a> | <a href="http://www.pjrc.com/teensy/td_libs_AudioProcessorUsage.html" target="_blank">AudioNoInterrupts()</a> | ||||
should be used to guarantee all new settings take effect together. | should be used to guarantee all new settings take effect together. | ||||
</p> | </p> | ||||
<p class=func><span class=keyword>pulseWidth</span>(amount);</p> | |||||
<p class=desc>Change the width (duty cycle) of the pulse.</p> | |||||
<p class=func><span class=keyword>arbitraryWaveform</span>(array, maxFreq);</p> | <p class=func><span class=keyword>arbitraryWaveform</span>(array, maxFreq);</p> | ||||
<p class=desc> | <p class=desc> | ||||
Configure the waveform to be used with WAVEFORM_ARBITRARY. Array | Configure the waveform to be used with WAVEFORM_ARBITRARY. Array | ||||
<li><span class=literal>WAVEFORM_SQUARE</span></li> | <li><span class=literal>WAVEFORM_SQUARE</span></li> | ||||
<li><span class=literal>WAVEFORM_TRIANGLE</span></li> | <li><span class=literal>WAVEFORM_TRIANGLE</span></li> | ||||
<li><span class=literal>WAVEFORM_ARBITRARY</span></li> | <li><span class=literal>WAVEFORM_ARBITRARY</span></li> | ||||
<li><span class=literal>WAVEFORM_PULSE</span></li> | |||||
</ul> | </ul> | ||||
</p> | </p> | ||||
</script> | </script> |
Audio KEYWORD2 | |||||
Audio KEYWORD2 | |||||
AudioConnection KEYWORD2 | AudioConnection KEYWORD2 | ||||
AudioInputI2S KEYWORD2 | AudioInputI2S KEYWORD2 | ||||
AudioOutputI2S KEYWORD2 | AudioOutputI2S KEYWORD2 | ||||
phase KEYWORD2 | phase KEYWORD2 | ||||
amplitude KEYWORD2 | amplitude KEYWORD2 | ||||
offset KEYWORD2 | offset KEYWORD2 | ||||
pulseWidth KEYWORD2 | |||||
resonance KEYWORD2 | resonance KEYWORD2 | ||||
octaveControl KEYWORD2 | octaveControl KEYWORD2 | ||||
averageTogether KEYWORD2 | averageTogether KEYWORD2 | ||||
WAVEFORM_SQUARE LITERAL1 | WAVEFORM_SQUARE LITERAL1 | ||||
WAVEFORM_TRIANGLE LITERAL1 | WAVEFORM_TRIANGLE LITERAL1 | ||||
WAVEFORM_ARBITRARY LITERAL1 | WAVEFORM_ARBITRARY LITERAL1 | ||||
WAVEFORM_PULSE LITERAL1 | |||||
else if (n > 1.0) n = 1.0; | else if (n > 1.0) n = 1.0; | ||||
tone_offset = n * 32767.0; | tone_offset = n * 32767.0; | ||||
} | } | ||||
void width(float n) { // 0.0 to 1.0 | |||||
void pulseWidth(float n) { // 0.0 to 1.0 | |||||
if (n < 0) n = 0; | if (n < 0) n = 0; | ||||
else if (n > 1.0) n = 1.0; | else if (n > 1.0) n = 1.0; | ||||
tone_width = n * 0x7fffffffLL; | tone_width = n * 0x7fffffffLL; | ||||
// width is stored as the equivalent phase | |||||
// pulse width is stored as the equivalent phase | |||||
} | } | ||||
void begin(short t_type) { | void begin(short t_type) { | ||||
tone_phase = 0; | tone_phase = 0; |