Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Modulating frequency with an envelope?

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    12

    Modulating frequency with an envelope?

    Hi,

    As the title says, I'm wondering how to achieve this with the Audio Library. I've tried with the AudioSynthWaveformModulated and the AudioEffectEnvelope, hooking the output of the envelop into the first input of the waveformMod. I then initiated all the waveform and envelope settings, called the "begin" method on the waveform but calling "noteOn" on the envelope does not seem to change the pitch.. it merely is stuck on the pitch it was initiated with.

    Any ideas what I could be doing wrong?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,734
    The envelope needs an input signal. It's an effect, so it doesn't create anything on its own. No input means no output.

    Give it a DC input if you want just the envelope for something else to use.

    Like this...

    Click image for larger version. 

Name:	env.png 
Views:	38 
Size:	10.3 KB 
ID:	15236

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    12
    You're the best Paul! I tried something similar with a sine synth, which gave an odd but interesting result. Didn't notice the DC object before you mentioned it. Thanks!

  4. #4
    Junior Member
    Join Date
    May 2020
    Posts
    9
    Quote Originally Posted by PaulStoffregen View Post
    The envelope needs an input signal. It's an effect, so it doesn't create anything on its own. No input means no output.

    Give it a DC input if you want just the envelope for something else to use.

    Like this...

    Click image for larger version. 

Name:	env.png 
Views:	38 
Size:	10.3 KB 
ID:	15236

    I'm not managing to change the pitch of the waveform with an envelope, can you help me?

    Click image for larger version. 

Name:	Image1.png 
Views:	22 
Size:	28.2 KB 
ID:	21975

    Do I have to put something specific in the code?

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,734
    Quote Originally Posted by Angel View Post
    Do I have to put something specific in the code?
    Yes, you need to configure the modulated waveform to make sound and to respond in the way you want to its input, and you need to cause the other stuff to create that modulated input.

    Here's a quick test program I wrote just now, for the diagram I showed in msg #2. When you run this, you can easily hear it working!

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformDc     dc1;            //xy=131,185
    AudioEffectEnvelope      envelope1;      //xy=283,155
    AudioSynthWaveformModulated waveformMod1;   //xy=385,239
    AudioOutputI2S           i2s1;           //xy=582,240
    AudioConnection          patchCord1(dc1, envelope1);
    AudioConnection          patchCord2(envelope1, 0, waveformMod1, 0);
    AudioConnection          patchCord3(waveformMod1, 0, i2s1, 0);
    AudioConnection          patchCord4(waveformMod1, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=482,303
    // GUItool: end automatically generated code
    
    void setup() {
      AudioMemory(20);
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
      dc1.amplitude(0.95);
      envelope1.delay(0);
      envelope1.attack(200);
      envelope1.hold(200);
      envelope1.decay(200);
      envelope1.sustain(0.4);
      envelope1.release(1500);
      waveformMod1.begin(WAVEFORM_SAWTOOTH);
      waveformMod1.amplitude(0.8);
      waveformMod1.frequency(120);
      waveformMod1.frequencyModulation(4); // 1.0 input increases 3 octaves
    }
    
    void loop() {
      delay(2000);
      envelope1.noteOn();
      delay(1000);
      envelope1.noteOff();
      delay(2000);
    }
    The idea is this oscillator runs at a constant 120 Hz, but quick ramps up by 4 octaves during the attack-hold-decay portion of the envelope. During the sustain phase (before noteOff is called) it remains at 1.6 octaves higher than normal 120 Hz, because the sustain level is has a gain of 0.4, and the DC input is 1.0, and the modulation is 4 octaves for 1.0. When noteOff is called, the release phase happens over 1.5 seconds, so you can hear it gradually return back down to the lower 120 Hz buzzing sound. The process repeats every 5 seconds, which gets a bit old after a few cycles of constant amplitude sawtooth buzzing sound.... but hopefully as you hear the transitions you can see hear how this code is working. Obviously for musically pleasing sounds you'd want something much more dynamic than a constant amplitude waveform!

  6. #6
    Junior Member
    Join Date
    May 2020
    Posts
    9
    thanks paul, now all good: o

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •