Sine wave phase question

Status
Not open for further replies.

DCL

Member
Hi,
I am totally new to Teensy and Arduino.
I started an experiment using multiple sine waves stacked to do sort of additive synthesis using Teensy 3.2/ Audio Shield Rev. C/Arduino 1.8.11/ Teensyduino 1.50
I used the audio system design tool to set up my project. My multiple sine waves frequencies are controlled by one Analog pin 3. It works just fine.

My question is in regards to the "phase" function that is available for the sine wave. As soon as I add that function for a given sine wave, the frequency of that sine wave is no longer changing with the others.
In this code I am trying to control the phase of sine1, sine3, 5, 7, 9, 11, with a separate knob. I think the phase may be changing, but the frequency is stuck at one pitch.

Code:
#include <Bounce.h>

Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15);  // 15 = 15 ms debounce time
Bounce button2 = Bounce(2, 15);


///////////////////////////////////
// copy the Design Tool code here
///////////////////////////////////
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveformSine   sine4;          //xy=227,314
AudioSynthWaveformSine   sine1;          //xy=228,196
AudioSynthWaveformSine   sine2;          //xy=228,235
AudioSynthWaveformSine   sine3;          //xy=228,275
AudioSynthWaveformSine   sine5;          //xy=228,368
AudioSynthWaveformSine   sine6;          //xy=229,410
AudioSynthWaveformSine   sine7;          //xy=231,450
AudioSynthWaveformSine   sine8;          //xy=234,490
AudioSynthWaveformSine   sine9;          //xy=246,534
AudioSynthWaveformSine   sine10;         //xy=251,573
AudioSynthWaveformSine   sine11;         //xy=261,613
AudioSynthWaveformSine   sine12;         //xy=281,662
AudioSynthWaveformSine   sine13;         //xy=300,711
AudioSynthWaveformSine   sine14;         //xy=302,749
AudioSynthWaveformSine   sine16;         //xy=304,825
AudioSynthWaveformSine   sine15;         //xy=305,787
AudioMixer4              mixer3;         //xy=449,538
AudioMixer4              mixer1;         //xy=471,247
AudioMixer4              mixer2;         //xy=474,423
AudioMixer4              mixer5;         //xy=483,627
AudioMixer4              mixer4;         //xy=637,427
AudioOutputI2S           i2s1;           //xy=828,426
AudioConnection          patchCord1(sine4, 0, mixer1, 3);
AudioConnection          patchCord2(sine1, 0, mixer1, 0);
AudioConnection          patchCord3(sine2, 0, mixer1, 1);
AudioConnection          patchCord4(sine3, 0, mixer1, 2);
AudioConnection          patchCord5(sine5, 0, mixer2, 0);
AudioConnection          patchCord6(sine6, 0, mixer2, 1);
AudioConnection          patchCord7(sine7, 0, mixer2, 2);
AudioConnection          patchCord8(sine8, 0, mixer2, 3);
AudioConnection          patchCord9(sine9, 0, mixer3, 0);
AudioConnection          patchCord10(sine10, 0, mixer3, 1);
AudioConnection          patchCord11(sine11, 0, mixer3, 2);
AudioConnection          patchCord12(sine12, 0, mixer3, 3);
AudioConnection          patchCord13(sine13, 0, mixer5, 0);
AudioConnection          patchCord14(sine14, 0, mixer5, 1);
AudioConnection          patchCord15(sine16, 0, mixer5, 3);
AudioConnection          patchCord16(sine15, 0, mixer5, 2);
AudioConnection          patchCord17(mixer3, 0, mixer4, 2);
AudioConnection          patchCord18(mixer1, 0, mixer4, 0);
AudioConnection          patchCord19(mixer2, 0, mixer4, 1);
AudioConnection          patchCord20(mixer5, 0, mixer4, 3);
AudioConnection          patchCord21(mixer4, 0, i2s1, 0);
AudioConnection          patchCord22(mixer4, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=657,186
// GUItool: end automatically generated code






void setup() {
  Serial.begin(9600);
  AudioMemory(20);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.32);
  
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);


  mixer4.gain(0, 0.5);
  mixer4.gain(1, 0.5);
  mixer4.gain(2, 0.5);
  mixer4.gain(3, 0.5);
  

}


float Sine01Level = 25;
float Sine02Level = 23;
float Sine03Level = 21;
float Sine04Level = 19;
float Sine05Level = 17;
float Sine06Level = 15;
float Sine07Level = 14;
float Sine08Level = 13;
float Sine09Level = 12;
float Sine10Level = 11;
float Sine11Level = 10;
float Sine12Level = 9;
float Sine13Level = 8;
float Sine14Level = 7;
float Sine15Level = 6;
float Sine16Level = 5;

int mixer1_setting = 0;
int mixer2_setting = 0;
elapsedMillis timeout = 0;
char SinePick = 1;


void loop() {
  button0.update();
  button1.update();
  button2.update();


  // use the knobs to adjust parameters
  float knob1 = (float)analogRead(A1) / 2.85;
  float x = (float)analogRead(A1) / 2047;
  //float knob2 = (float)analogRead(A2) / 2.85;
  float FreqCV = (float)analogRead(A3); /// 1023.0;
  
  float f = (440*exp2f((0.0292969*FreqCV - 69)/3))*200000;

  
  AudioNoInterrupts();
  
  sine1.frequency(f);
  sine1.phase(knob1);
  sine2.frequency(f*(2-x));
  sine3.frequency(f*(3-(2*x)));
  sine3.phase(knob1);
  sine4.frequency(f*(4-(3*x)));
  sine5.frequency(f*(5-(4*x)));
  sine5.phase(knob1);
  sine6.frequency(f*(6-(5*x)));
  sine7.frequency(f*(7-(6*x)));
  sine7.phase(knob1);
  sine8.frequency(f*(8-(7*x)));
  sine9.frequency(f*(9-(8*x)));
  sine9.phase(knob1);
  sine10.frequency(f*(10-(9*x)));
  sine11.frequency(f*(11-(10*x)));
  sine11.phase(knob1);
  sine12.frequency(f*(12-(11*x)));

  sine13.frequency(f*(13-(12*x)));
  sine14.frequency(f*(14-(13*x)));
  sine15.frequency(f*(15-(14*x)));
  sine16.frequency(f*(16-(15*x)));
  

  
  
  AudioInterrupts();



// middle button switch which sine wave is level adjusted
  if (button1.fallingEdge()) {
     SinePick++; 
     Serial.print("Sine"); 
     Serial.println(SinePick, DEC);
     Serial.println(" Level Adjust "); 
  }
  if (SinePick > 16) {SinePick = 1;}


  


  if (SinePick == 1) {
      if (button0.fallingEdge()) {
        if (Sine01Level < 25){
            Sine01Level++;
        }  
        Serial.print("Sine1 Level = ");
        Serial.println(Sine01Level/100, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine01Level > 0){
            Sine01Level--;
        }  
        Serial.print("Sine1 Level = ");
        Serial.println(Sine01Level/100, DEC);
      }
   }

   else if (SinePick == 2) {
      if (button0.fallingEdge()) {
        if (Sine02Level < 25){
            Sine02Level++;
        }  
        Serial.print("Sine2 Level = ");
        Serial.println(Sine02Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine02Level > 0){
            Sine02Level--;
        }  
        Serial.print("Sine2 Level = ");
        Serial.println(Sine02Level, DEC);
      }
   }

   else if (SinePick == 3) {
      if (button0.fallingEdge()) {
        if (Sine03Level < 25){
            Sine03Level++;
        }  
        Serial.print("Sine3 Level = ");
        Serial.println(Sine03Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine03Level > 0){
            Sine03Level--;
        }  
        Serial.print("Sine3 Level = ");
        Serial.println(Sine03Level, DEC);
      }
   }

   else if (SinePick == 4) {
      if (button0.fallingEdge()) {
        if (Sine04Level < 25){
            Sine04Level++;
        }  
        Serial.print("Sine4 Level = ");
        Serial.println(Sine04Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine04Level > 0){
            Sine04Level--;
        }  
        Serial.print("Sine4 Level = ");
        Serial.println(Sine04Level, DEC);
      }
   }

  else if (SinePick == 5) {
      if (button0.fallingEdge()) {
        if (Sine05Level < 25){
            Sine05Level++;
        }  
        Serial.print("Sine5 Level = ");
        Serial.println(Sine05Level/100, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine05Level > 0){
            Sine05Level--;
        }  
        Serial.print("Sine5 Level = ");
        Serial.println(Sine05Level/100, DEC);
      }
   }

   else if (SinePick == 6) {
      if (button0.fallingEdge()) {
        if (Sine06Level < 25){
            Sine06Level++;
        }  
        Serial.print("Sine6 Level = ");
        Serial.println(Sine06Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine06Level > 0){
            Sine06Level--;
        }  
        Serial.print("Sine6 Level = ");
        Serial.println(Sine06Level, DEC);
      }
   }

   else if (SinePick == 7) {
      if (button0.fallingEdge()) {
        if (Sine07Level < 25){
            Sine07Level++;
        }  
        Serial.print("Sine7 Level = ");
        Serial.println(Sine07Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine07Level > 0){
            Sine07Level--;
        }  
        Serial.print("Sine7 Level = ");
        Serial.println(Sine07Level, DEC);
      }
   }

   else if (SinePick == 8) {
      if (button0.fallingEdge()) {
        if (Sine08Level < 25){
            Sine08Level++;
        }  
        Serial.print("Sine8 Level = ");
        Serial.println(Sine08Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine08Level > 0){
            Sine08Level--;
        }  
        Serial.print("Sine8 Level = ");
        Serial.println(Sine08Level, DEC);
      }
   }

     else if (SinePick == 9) {
      if (button0.fallingEdge()) {
        if (Sine09Level < 25){
            Sine09Level++;
        }  
        Serial.print("Sine9 Level = ");
        Serial.println(Sine09Level/100, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine09Level > 0){
            Sine09Level--;
        }  
        Serial.print("Sine9 Level = ");
        Serial.println(Sine09Level/100, DEC);
      }
   }

   else if (SinePick == 10) {
      if (button0.fallingEdge()) {
        if (Sine10Level < 25){
            Sine10Level++;
        }  
        Serial.print("Sine10 Level = ");
        Serial.println(Sine10Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine10Level > 0){
            Sine10Level--;
        }  
        Serial.print("Sine10 Level = ");
        Serial.println(Sine10Level, DEC);
      }
   }

   else if (SinePick == 11) {
      if (button0.fallingEdge()) {
        if (Sine11Level < 25){
            Sine11Level++;
        }  
        Serial.print("Sine11 Level = ");
        Serial.println(Sine11Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine11Level > 0){
            Sine11Level--;
        }  
        Serial.print("Sine11 Level = ");
        Serial.println(Sine11Level, DEC);
      }
   }

   else if (SinePick == 12) {
      if (button0.fallingEdge()) {
        if (Sine12Level < 25){
            Sine12Level++;
        }  
        Serial.print("Sine12 Level = ");
        Serial.println(Sine12Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine12Level > 0){
            Sine12Level--;
        }  
        Serial.print("Sine12 Level = ");
        Serial.println(Sine12Level, DEC);
      }
   }

     else if (SinePick == 13) {
      if (button0.fallingEdge()) {
        if (Sine13Level < 25){
            Sine13Level++;
        }  
        Serial.print("Sine13 Level = ");
        Serial.println(Sine13Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine13Level > 0){
            Sine13Level--;
        }  
        Serial.print("Sine13 Level = ");
        Serial.println(Sine13Level, DEC);
      }
   }
      else if (SinePick == 14) {
      if (button0.fallingEdge()) {
        if (Sine14Level < 25){
            Sine14Level++;
        }  
        Serial.print("Sine14 Level = ");
        Serial.println(Sine14Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine14Level > 0){
            Sine14Level--;
        }  
        Serial.print("Sine14 Level = ");
        Serial.println(Sine14Level, DEC);
      }
   }
      else if (SinePick == 15) {
      if (button0.fallingEdge()) {
        if (Sine15Level < 25){
            Sine15Level++;
        }  
        Serial.print("Sine15 Level = ");
        Serial.println(Sine15Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine15Level > 0){
            Sine15Level--;
        }  
        Serial.print("Sine15 Level = ");
        Serial.println(Sine15Level, DEC);
      }
   }
      else if (SinePick == 16) {
      if (button0.fallingEdge()) {
        if (Sine16Level < 25){
            Sine16Level++;
        }  
        Serial.print("Sine16 Level = ");
        Serial.println(Sine16Level, DEC);
      }
      if (button2.fallingEdge()) {
        if (Sine16Level > 0){
            Sine16Level--;
        }  
        Serial.print("Sine16 Level = ");
        Serial.println(Sine16Level, DEC);
      }
   }
  
  mixer1.gain(0, Sine01Level/100);
  mixer1.gain(1, Sine02Level/100);
  mixer1.gain(2, Sine03Level/100);
  mixer1.gain(3, Sine04Level/100);
  mixer2.gain(0, Sine05Level/100);
  mixer2.gain(1, Sine06Level/100);
  mixer2.gain(2, Sine07Level/100);
  mixer2.gain(3, Sine08Level/100);
  mixer3.gain(0, Sine09Level/100);
  mixer3.gain(1, Sine10Level/100);
  mixer3.gain(2, Sine11Level/100);
  mixer3.gain(3, Sine12Level/100);
  mixer5.gain(0, Sine13Level/100);
  mixer5.gain(1, Sine14Level/100);
  mixer5.gain(2, Sine15Level/100);
  mixer5.gain(3, Sine16Level/100);
}
 
Status
Not open for further replies.
Back
Top