// demonstrate pulse with slow changes in width | |||||
#include <Audio.h> | |||||
#include <Wire.h> | |||||
#include <SPI.h> | |||||
#include <SD.h> | |||||
// GUItool: begin automatically generated code | |||||
AudioSynthWaveform waveform1; //xy=188,240 | |||||
AudioEffectEnvelope envelope1; //xy=371,237 | |||||
AudioOutputI2S i2s1; //xy=565,241 | |||||
AudioConnection patchCord1(waveform1, envelope1); | |||||
AudioConnection patchCord2(envelope1, 0, i2s1, 0); | |||||
AudioConnection patchCord3(envelope1, 0, i2s1, 1); | |||||
AudioControlSGTL5000 audioShield; //xy=586,175 | |||||
// GUItool: end automatically generated code | |||||
void setup(void) | |||||
{ | |||||
// Set up | |||||
AudioMemory(8); | |||||
audioShield.enable(); | |||||
audioShield.volume(0.45); | |||||
waveform1.width(0.5); | |||||
waveform1.begin(0.4, 220, WAVEFORM_PULSE); | |||||
envelope1.attack(50); | |||||
envelope1.decay(50); | |||||
envelope1.release(250); | |||||
} | |||||
void loop() { | |||||
float w; | |||||
for (uint32_t i =1; i<20; i++) { | |||||
w = i / 20.0; | |||||
waveform1.width(w); | |||||
envelope1.noteOn(); | |||||
delay(800); | |||||
envelope1.noteOff(); | |||||
delay(600); | |||||
} | |||||
} | |||||
tone_phase += 2*tone_incr; | tone_phase += 2*tone_incr; | ||||
} | } | ||||
break; | break; | ||||
case WAVEFORM_PULSE: | |||||
for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) { | |||||
if(tone_phase < tone_width)*bp++ = -tone_amp; | |||||
else *bp++ = tone_amp; | |||||
tone_phase += tone_incr; | |||||
} | |||||
break; | |||||
} | } | ||||
if (tone_offset) { | if (tone_offset) { | ||||
bp = block->data; | bp = block->data; |
#define WAVEFORM_SQUARE 2 | #define WAVEFORM_SQUARE 2 | ||||
#define WAVEFORM_TRIANGLE 3 | #define WAVEFORM_TRIANGLE 3 | ||||
#define WAVEFORM_ARBITRARY 4 | #define WAVEFORM_ARBITRARY 4 | ||||
#define WAVEFORM_PULSE 5 | |||||
// todo: remove these... | // todo: remove these... | ||||
#define TONE_TYPE_SINE 0 | #define TONE_TYPE_SINE 0 | ||||
AudioSynthWaveform(void) : | AudioSynthWaveform(void) : | ||||
AudioStream(0,NULL), | AudioStream(0,NULL), | ||||
tone_phase(0), tone_incr(0), tone_type(0), | tone_phase(0), tone_incr(0), tone_type(0), | ||||
tone_offset(0), arbdata(NULL) | |||||
tone_offset(0), tone_width(0.25), arbdata(NULL) | |||||
{ | { | ||||
} | } | ||||
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 | |||||
if (n < 0) n = 0; | |||||
else if (n > 1.0) n = 1.0; | |||||
tone_width = n * 0x7fffffffLL; | |||||
// width is stored as the equivalent phase | |||||
} | |||||
void begin(short t_type) { | void begin(short t_type) { | ||||
tone_phase = 0; | tone_phase = 0; | ||||
tone_type = t_type; | tone_type = t_type; | ||||
short tone_amp; | short tone_amp; | ||||
short tone_freq; | short tone_freq; | ||||
uint32_t tone_phase; | uint32_t tone_phase; | ||||
uint32_t tone_width; | |||||
// volatile prevents the compiler optimizing out the frequency function | // volatile prevents the compiler optimizing out the frequency function | ||||
volatile uint32_t tone_incr; | volatile uint32_t tone_incr; | ||||
short tone_type; | short tone_type; |