Teensy tracked sine wave gliassando.

Status
Not open for further replies.

cfredisded

Active member
Hey all,
Really been enjoying learning 'programming' with the teensy 3.2 + audio board, and I cant wait for some type of real time audio pitch shifting (which I've read is at least on a todo list for the audio library).
I managed to scrounge some code together from examples that tracks the pitch and amplitude of the incoming signal and creates a sine wave that follows. Please excuse the messy state that the code is in... I've comment out a lot of failed attempts at adding different things, rather then deleting them. I'm wondering if i could get some help adding a glissando type of effect to the sine output. So that if the pitch change between the last tracked note and the current tracked note is greater then `5 or so, the sine wave will change at a controlled rate from the previous note to the new note..... if that makes sense... Here's the messy code and appreciate any insight.

Code:
/*
 * A simple hardware test which receives audio from the audio shield
 * Line-In pins and send it to the Line-Out pins and headphone jack.
 *
 * This example code is in the public domain.
 */
 /*
 C     C#    D     Eb    E     F     F#      G      G#      A     Bb    B
 0 16.35 17.32 18.35 19.45 20.60 21.83 23.12 24.50 25.96 27.50 29.14 30.87
 1 32.70 34.65 36.71 38.89 41.20 43.65 46.25 49.00 51.91 55.00 58.27 61.74
 2 65.41 69.30 73.42 77.78 82.41 87.31 92.50 98.00 103.8 110.0 116.5 123.5
 3 130.8 138.6 146.8 155.6 164.8 174.6 185.0 196.0 207.7 220.0 233.1 246.9
 4 261.6 277.2 293.7 311.1 329.6 349.2 370.0 392.0 415.3 440.0 466.2 493.9
 5 523.3 554.4 587.3 622.3 659.3 698.5 740.0 784.0 830.6 880.0 932.3 987.8
 6 1047  1109  1175  1245  1319  1397  1480  1568  1661  1760  1865  1976
 7 2093  2217  2349  2489  2637  2794  2960  3136  3322  3520  3729  3951
 8 4186  4435  4699  4978  5274  5588  5920  6272  6645  7040  7459  7902
 
 Guitar strings are E2=82.41Hz, A2=110Hz, D3=146.8Hz, G3=196Hz, B3=246.9Hz, E4=329.6Hz
 
 Bass strings are (5th string) B0=30.87Hz, (4th string) E1=41.20Hz, A1=55Hz, D2=73.42Hz, G2=98Hz
 
 This example tests the yin algorithm with actual notes from nylon string guitar recorded
 as wav format at 16B @ 44100 samples/sec. Since the decay of the notes will be longer than what
 the teensy can store in flash these notes are truncated to ~120,000B or about 1/2 of the whole
 signal.
 */


#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=311,268
AudioAnalyzeNoteFrequency notefreq1;      //xy=427,457
AudioAnalyzeRMS          rms1;           //xy=442,409
AudioSynthWaveformSineModulated sine_fm1;       //xy=507,315
AudioMixer4              mixer1;         //xy=698,285
AudioOutputI2S           i2s2;           //xy=923,279
AudioConnection          patchCord1(i2s1, 0, sine_fm1, 0);
AudioConnection          patchCord2(i2s1, 0, notefreq1, 0);
AudioConnection          patchCord3(i2s1, 0, mixer1, 0);
AudioConnection          patchCord4(i2s1, 0, rms1, 0);
AudioConnection          patchCord5(sine_fm1, 0, mixer1, 1);
AudioConnection          patchCord6(mixer1, 0, i2s2, 0);
AudioConnection          patchCord7(mixer1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=240,67
// GUItool: end automatically generated code








const int myInput = AUDIO_INPUT_LINEIN; //const int myInput = AUDIO_INPUT_MIC;
float note1;
float note2;
int amp;
int amp1;
int a1history=0;
int arp;
float ampprint;
//int arpamount
///int sinfreq = note;
///int sinamp = .8;


void setup() {
  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(30);
  notefreq1.begin(.10);

  // Enable the audio shield, select input, and enable output
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(myInput);
  sgtl5000_1.volume(0.5);
  //Set sinwave freq and amp variables
  //sine_fm1.amplitude(amp);
 // sine_fm1.frequency(note);
  //set mix gain
  mixer1.gain(0, 0.0);
  mixer1.gain(1, 0.8);
  //arp == 0;
  //a1history = analogRead(A1);

//elapsedMillis volmsec=0;

}

//void loop() {
  // every 50 ms, adjust the volume
  ///  if (volmsec > 50) {
    ///float vol = analogRead(15);
  ///  vol = vol / 1023.0;
  //audioShield.volume(vol); // <-- uncomment if you have the optional
    /// volmsec = 0;               //     volume pot on your audio shield

   //if (peak1.available()) {
    //   float amp = peak1.read();
    //   sine1.amplitude(amp);
      
 //  }

elapsedMillis fps;
//waits 25 mils to read peak

void loop() {
  //  for (arp = 0; arp < 100; arp++) {
  //  
  //  }
  //   for (arp = 100; arp > 0; arp--) {
  //  
  // }
  //int arp = analogRead(A1);
  arp++;

    
  //arpamount = arpamount + arp'

   // if(arp >= 1000) ;
  //  float arp = arp-- ;
if(fps > 25) {
       while (rms1.available()) {
        fps=0;
        float amp = rms1.read()*10 ; ///amp int equals peak1 times 10
        sine_fm1.amplitude(amp);
       //Serial.printf("amplitude: %f", amp);
      // Serial.println(amp);
    
      
            }
    
    
      }
   if (notefreq1.available()) {
        float note1 = notefreq1.read();
        float prob = notefreq1.probability();
        int a1 = analogRead(A1);
       
        sine_fm1.frequency(note1);
        
         //   if (note1<note2){
         //   note1++;
         //   sine_fm1.frequency(note1);
         //   float note2 = notefreq1.read();
         //   } 
         //   
         //   if (note1>note2){
         //   note1--;         
         //   sine_fm1.frequency(note1);
         //   float note1 = notefreq1.read();
         //   } 
        
     
      
        
        Serial.printf("Note: %3.2f | Probability: %.2f\n", note1, prob);
        Serial.print("Knob (pin A1) = ");
        Serial.println(a1);
        Serial.print("note1 = ");
        Serial.println(note1);
        Serial.print("arp value = ");
        Serial.println(arp);
       // Serial.printf("amplitude: %f", amp);
       // Serial.println(amp);
        
  }
//note1=note2;  
if(arp>1000) {
arp=0;}
}

Also if anyone wants to play with the code.. you can get a wacky effect by changing

sine_fm1.frequency(note1);
to
sine_fm1.frequency(note1+arp);
Cheers,
 
I'm wondering if i could get some help adding a glissando type of effect

Maybe try adding ramping control signal to the sine frequency modulation input?

You can get a signal that ramps between levels with the DC object, using a transition time. Or you could feed a constant DC signal into the envelope object, which gives you a "fire and forget" approach with the configured attack/decay (if you set the sustain level to zero), rather than having your code control the moments when the signal changes. Or you could use the waveform object configured as a triangle or sawtooth, using a low frequency and perhaps by setting the phase to a specific point when you want to begin.

If you want to add this signal together with the control input you're getting from the audio shield, just run both into a mixer and let the mixer drive the sine modulation input.
 
Status
Not open for further replies.
Back
Top