Kaynağa Gözat

Add simple pulse waveform

pulse plus example
dds
Nantonos 10 yıl önce
ebeveyn
işleme
73bfb6926f
3 değiştirilmiş dosya ile 67 ekleme ve 1 silme
  1. +49
    -0
      examples/Synthesis/pulse/pulse.ino
  2. +9
    -0
      synth_waveform.cpp
  3. +9
    -1
      synth_waveform.h

+ 49
- 0
examples/Synthesis/pulse/pulse.ino Dosyayı Görüntüle

@@ -0,0 +1,49 @@
// 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);
}
}



+ 9
- 0
synth_waveform.cpp Dosyayı Görüntüle

@@ -128,6 +128,15 @@ void AudioSynthWaveform::update(void)
tone_phase += 2*tone_incr;
}
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) {
bp = block->data;

+ 9
- 1
synth_waveform.h Dosyayı Görüntüle

@@ -44,6 +44,7 @@ extern const int16_t AudioWaveformSine[257];
#define WAVEFORM_SQUARE 2
#define WAVEFORM_TRIANGLE 3
#define WAVEFORM_ARBITRARY 4
#define WAVEFORM_PULSE 5

// todo: remove these...
#define TONE_TYPE_SINE 0
@@ -58,7 +59,7 @@ public:
AudioSynthWaveform(void) :
AudioStream(0,NULL),
tone_phase(0), tone_incr(0), tone_type(0),
tone_offset(0), arbdata(NULL)
tone_offset(0), tone_width(0.25), arbdata(NULL)
{
}
@@ -91,6 +92,12 @@ public:
else if (n > 1.0) n = 1.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) {
tone_phase = 0;
tone_type = t_type;
@@ -109,6 +116,7 @@ private:
short tone_amp;
short tone_freq;
uint32_t tone_phase;
uint32_t tone_width;
// volatile prevents the compiler optimizing out the frequency function
volatile uint32_t tone_incr;
short tone_type;

Yükleniyor…
İptal
Kaydet