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

Thread: Waveform.begin no longer resets the wave?

  1. #1

    Waveform.begin no longer resets the wave?

    Hey All,
    I'm working on a project that has multiple wave forms and I'd like to be able to push a button to reset all of the waveforms to start at once. I remember in June waveform.begin would restart the wave form. I remember because back in June I had the opposite problem ....
    https://forum.pjrc.com/threads/52750...shape?p=181444
    ... and Paul helped me figure out a work around for my amateur code.

    It appears maybe Paul updated the waveform begin to not reset at 0? Is there anyway to reset the waveforms 'phase' back to zero now?


    Here's some example code. Even without the if button pressed statements the waveforms sounds just fine. Yet 6 months ago they would have just kept resetting.
    Code:
    #include <Bounce.h>
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioSynthWaveform       waveform2;      //xy=480,575
    AudioSynthWaveform       waveform1;      //xy=482,630
    AudioMixer4              mixer1;         //xy=751,615
    AudioOutputI2S           i2s1;           //xy=937,614
    AudioConnection          patchCord1(waveform2, 0, mixer1, 0);
    AudioConnection          patchCord2(waveform1, 0, mixer1, 1);
    AudioConnection          patchCord3(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord4(mixer1, 0, i2s1, 1);
    // GUItool: end automatically generated code
    
    
    const int shiftPin = 28;
    Bounce SHIFTbutton = Bounce(shiftPin, 10);  // 10 ms debounce
    const int ratioPin = 27;
    Bounce RATIObutton = Bounce(ratioPin, 10);  // 10 ms debounce
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      AudioMemory(50);
      waveform1.begin(.1 ,2 ,WAVEFORM_SINE);
      waveform2.begin(.1 ,2 ,WAVEFORM_SINE);
      mixer1.gain(0, .25);
      mixer1.gain(1, .25);
      pinMode(shiftPin, INPUT_PULLUP);
      pinMode(ratioPin, INPUT_PULLUP);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      if (SHIFTbutton.update()){
        if (SHIFTbutton.fallingEdge()){
          waveform1.begin(.5 ,40 ,WAVEFORM_SINE);
          waveform2.begin(.5 ,80 ,WAVEFORM_SINE);
          Serial.println("shift pressed");
          
        }
      }
    
    }

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,174
    Try this:

    Code:
      AudioNoInterrupts();
      waveform1.phase(0);
      waveform2.phase(0);
      AudioInterrupts();

  3. #3
    Awesome.... thanks paul! What about with a waveformmod?
    would something like this work?
    Code:
    AudioNoInterrupts();
    waveformMod.phaseModulation(0);
    AudioNoInterrupts();
    the way the info is worded kinda makes it seem like waveformMod.phaseModulation only sets the total amount a signal from 1 to -1 into 'in 0' will affect it. I'll experiment and post back. might have to be something like...

    Code:
    AudioNoInterrupts();
    waveformMod.phaseModulation(0);
    dc1.amplitude(0);
    AudioNoInterrupts();

    It will actually be really nice to be able to change the waveshape without resetting the wave and also have a way to reset the wave when needed.. for instances when multiple shapes need to be suddenly sinked up.

  4. #4
    It's looks like this code doesn't seem to do anything.

    Code:
      AudioNoInterrupts();
      waveform1.phase(0);
      waveform2.phase(0);
      AudioInterrupts();
    Here's where i have it in my full sketch... doenst seem to do anything even when its just running in the void loop.

    Code:
    #include <Bounce.h>
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioSynthWaveform       waveform2;      //xy=577,367
    AudioSynthWaveform       waveform1;      //xy=580,324
    AudioMixer4              mixer1;         //xy=762,364
    AudioOutputI2S           i2s1;           //xy=948,363
    AudioConnection          patchCord1(waveform2, 0, mixer1, 1);
    AudioConnection          patchCord2(waveform1, 0, mixer1, 0);
    AudioConnection          patchCord3(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord4(mixer1, 0, i2s1, 1);
    // GUItool: end automatically generated code
    
    
    const int shiftPin = 28;
    Bounce SHIFTbutton = Bounce(shiftPin, 10);  // 10 ms debounce
    const int ratioPin = 27;
    Bounce RATIObutton = Bounce(ratioPin, 10);  // 10 ms debounce
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      AudioMemory(50);
      waveform1.begin(.25 ,40 ,WAVEFORM_SINE);
      waveform2.begin(.25 ,80 ,WAVEFORM_SINE);
      mixer1.gain(0, .25);
      mixer1.gain(1, .25);
      pinMode(shiftPin, INPUT_PULLUP);
      pinMode(ratioPin, INPUT_PULLUP);
    }
    
    void loop() {
    
      AudioNoInterrupts();
      waveform1.phase(0);
      waveform2.phase(0);
      AudioInterrupts();
    
    
    }
    Any suggestions on how to sync multiple waveforms/waveforms mods to the same starting point?

  5. #5
    Couldn't for the life of me find a way to restart the waveforms phase. But I stumbled upon 'pull request' that I think will be exactly what I need.

    https://github.com/PaulStoffregen/Audio/pull/275

    I just modified my audio library and it works. Something like this..
    Code:
            AudioNoInterrupts();
            waveform1.restart();
            waveform2.restart();
            AudioInterrupts();

  6. #6
    Hey, is this available for waveformMod1? i did tryed
    Code:
    AudioNoInterrupts();
    waveformMod1.phaseModulation(0);
    AudioInterrupts();
    with an external trigger and it's not starting the wave properly, any thoughts?
    Last edited by vincentiuș; 05-15-2019 at 07:18 PM.

  7. #7
    Junior Member
    Join Date
    Apr 2019
    Posts
    12
    Hello, no waveformMod1.phaseModulation(0); just sets the phase modulation range. You need to set the phase_accumulator to zero in synth_waveform.cpp. I added this:

    In synth_waveform.h:

    void sync() {
    syncFlag = 1;
    }

    In synth_waveform.cpp:

    if(syncFlag==1){
    phase_accumulator = 0;
    syncFlag = 0;
    }
    ph = phase_accumulator + phase_offset; //existing code

  8. #8
    Quote Originally Posted by UHF View Post
    Hello, no waveformMod1.phaseModulation(0); just sets the phase modulation range. You need to set the phase_accumulator to zero in synth_waveform.cpp. I added this:

    In synth_waveform.h:

    void sync() {
    syncFlag = 1;
    }

    In synth_waveform.cpp:

    if(syncFlag==1){
    phase_accumulator = 0;
    syncFlag = 0;
    }
    ph = phase_accumulator + phase_offset; //existing code
    working for waveform unmodulated, but i was speaking about waveformMod1 for which fm modulation is available

  9. #9
    Junior Member
    Join Date
    Apr 2019
    Posts
    12
    I stress I've not tried it for the modulated waveform yet (I will do later as I'm planning to implement oscillator hard sync), but it looks like setting phase_accumulator to zero will work here as well. Its working out the wave phase with modulation and then giving it to the wave shape to work out the amplitude. If the accumulator is zero on the next update, then it will do the phase computation again from the start of the cycle.

Posting Permissions

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