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

Thread: Audio Tutorial Filter questions

  1. #1
    Junior Member
    Join Date
    Feb 2020
    Posts
    11

    Audio Tutorial Filter questions

    (T4 + Audio shield + mpr121 for buttons + 4 pots. All on strip-board) Heading towards my hearing enhancer....

    A question about filters with part 2-7 of audio tutorial. (I adapted to include volume control code, balance and the mpr121 buttons) As I have 12 buttons to use, I added one more option that sets mixers to a third of each output of the filters (pad_state bit3). Q1. Is this sensible? The output does sound like the original, I think.

    Q2. The pot on knob3 works, but where would be the "normal value"? It goes from freq 90 to around 9000 (sounds about normal at about 450....)
    Q3. The knob3 works, but there is no "patch cord" - is it a virtual one?, i.e. an assumed control patch as the SGTL5000 has no connectors? Forget that! I misread the tutorial guide!! Need to add other input.....

    Q4. Best microphone for things like normal hearing through to bat detection (I believe this is still work in progress on T4?) ? (Definitely another project somewhere there!)

    p.s. Keep safe!

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include "Adafruit_MPR121.h"
    
    // reading of pots for vol and balance as suggested by https://forum.pjrc.com/threads/59943-Teensy-4-0-Audio-Shield-344Hz-buzz-when-using-line-in
    // bits from https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit
    
    /* a=target variable, b=bit number to act upon 0-n */
    #define BIT_SET(a,b) ((a) |= (1ULL<<(b)))
    #define BIT_CLEAR(a,b) ((a) &= ~(1ULL<<(b)))
    #define BIT_FLIP(a,b) ((a) ^= (1ULL<<(b)))
    #define BIT_CHECK(a,b) (!!((a) & (1ULL<<(b))))        // '!!' to make sure this returns 0 or 1
    
    #define BUTTON_PRESS_DELAY 400  // Wait 400ms after each button press
    
    // mixers 1 & 2 used for balance
    // mixers 2 & 3 used for control of output from filters (1&2 in original)
    
    // GUItool: begin automatically generated code
    AudioInputI2S            i2s2;           //xy=164,349
    AudioFilterStateVariable filter1;        //xy=405,265
    AudioFilterStateVariable filter2;        //xy=408,433
    AudioMixer4              mixer4;         //xy=623,454
    AudioMixer4              mixer3;         //xy=624,263
    AudioMixer4              mixer1;         //xy=1116,362
    AudioMixer4              mixer2;         //xy=1118,438
    AudioOutputI2S           i2s1;           //xy=1300,401
    AudioConnection          patchCord1(i2s2, 0, filter1, 0);
    AudioConnection          patchCord2(i2s2, 1, filter2, 0);
    AudioConnection          patchCord3(filter1, 0, mixer3, 0);
    AudioConnection          patchCord4(filter1, 1, mixer3, 1);
    AudioConnection          patchCord5(filter1, 2, mixer3, 2);
    AudioConnection          patchCord6(filter2, 0, mixer4, 0);
    AudioConnection          patchCord7(filter2, 1, mixer4, 1);
    AudioConnection          patchCord8(filter2, 2, mixer4, 2);
    AudioConnection          patchCord9(mixer4, 0, mixer2, 0);
    AudioConnection          patchCord10(mixer3, 0, mixer1, 0);
    AudioConnection          patchCord11(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord12(mixer2, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=1134,557
    // GUItool: end automatically generated code
    
    Adafruit_MPR121 cap = Adafruit_MPR121();
    static uint16_t pad_state = 0;
    static int vol = 0;
    static int bal = 511;
    static float freq;
    static elapsedMillis lastButtonPress;
    
    
    void setup()
    {
        Serial.begin(9600);
    	  AudioMemory(25);
        AudioNoInterrupts();
        sgtl5000_1.enable();
        sgtl5000_1.volume(0.2);
    
      sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
    //  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
    //  sgtl5000_1.micGain(63);
    //  sgtl5000_1.lineInLevel(7,7);
    
      mixer3.gain(0, 0.0);  
      mixer3.gain(1, 1.0);  // default to hearing band-pass signal
      mixer3.gain(2, 0.0);
      mixer3.gain(3, 0.0);
      mixer4.gain(0, 0.0);
      mixer4.gain(1, 1.0);
      mixer4.gain(2, 0.0);
      mixer4.gain(3, 0.0);
      
    // mix 1 and 2 for balance ('cos I have poor hearing in one ear!!) (WOW! Stereo!!)
       mixer1.gain(0, 0.5);
       mixer1.gain(1, 0.0);
       mixer1.gain(2, 0.0);
       mixer1.gain(3, 0.0);
    
       mixer2.gain(0, 0.5);
       mixer2.gain(1, 0.0);
       mixer2.gain(2, 0.0);
       mixer2.gain(3, 0.0);
    
        if(!cap.begin(0x5A))
        {
            Serial.println("MPR121 not found!");
            while(1);
        }
        AudioInterrupts();
    }
    
    
    void loop()
    {
      check_pad();
      check_volume();
      check_balance();
      set_mixers();
      set_freq();
    // Serial.print("pad_state = ");
    // Serial.println(pad_state);
      Serial.print("frequency = ");
      Serial.println(freq);
    
      delay(100);
    }
    
    
    void set_freq(){
      // read the knob and adjust the filter frequency
      int knob = analogRead(A3);
      // quick and dirty equation for exp scale frequency adjust
      freq =  expf((float)knob / 150.0) * 10.0 + 80.0;
      filter1.frequency(freq);
      filter2.frequency(freq);
    }
    
    void set_mixers() {
      if (BIT_CHECK(pad_state,0)) {
        Serial.println("Low Pass Signal");
        mixer3.gain(0, 1.0);  // hear low-pass signal
        mixer3.gain(1, 0.0);
        mixer3.gain(2, 0.0);
        mixer4.gain(0, 1.0);
        mixer4.gain(1, 0.0);
        mixer4.gain(2, 0.0);
        BIT_CLEAR(pad_state,0);
        delay(50);
      }
      if (BIT_CHECK(pad_state,1)) {
        Serial.println("Band Pass Signal");
        mixer3.gain(0, 0.0);
        mixer3.gain(1, 1.0);  // hear low-pass signal
        mixer3.gain(2, 0.0);
        mixer4.gain(0, 0.0);
        mixer4.gain(1, 1.0);
        mixer4.gain(2, 0.0);
        BIT_CLEAR(pad_state,1);
        delay(50);
      }
      if (BIT_CHECK(pad_state,2)) {
        Serial.println("High Pass Signal");
        mixer3.gain(0, 0.0);
        mixer3.gain(1, 0.0);
        mixer3.gain(2, 1.0);  // hear low-pass signal
        mixer4.gain(0, 0.0);
        mixer4.gain(1, 0.0);
        mixer4.gain(2, 1.0);
        BIT_CLEAR(pad_state,2);
        delay(50);
      }
      if (BIT_CHECK(pad_state,3)) {
        Serial.println("High Pass Signal");
        mixer3.gain(0, 0.33);
        mixer3.gain(1, 0.33);
        mixer3.gain(2, 0.33);  // hear all signal ???? need to read docs...
        mixer4.gain(0, 0.33);
        mixer4.gain(1, 0.33);
        mixer4.gain(2, 0.33);
        BIT_CLEAR(pad_state,3);
        delay(50);
      }
    }
    
    void check_pad()
    {
       uint16_t mpr_vals = cap.touched();
       if(mpr_vals)
       {
            float pad_pressed = log(mpr_vals) / log(2);     // Get position of bit pressed (?? RR)
    //        Serial.print("pad_pressed = ");
    //        Serial.println(pad_pressed);
            int active_pressed = (int) pad_pressed;
            if(lastButtonPress > BUTTON_PRESS_DELAY)
            {
    //            Serial.println(active_pressed);
    // toggle bit in pad_state variable
                BIT_FLIP(pad_state,active_pressed);
                lastButtonPress = 0;
           }
       }
    }
    
    void check_volume()
    {
    // get volume from knob1
      int knob1 = analogRead(A1);
      if ((knob1 < vol-9)||(knob1 > vol+9)) {     // allow for a bit of wobble
        vol = knob1;
        //    Serial.print("vol = ");
        //    Serial.println(vol);
        sgtl5000_1.volume(knob1 / 1023.0); //note decimal point on the float constant
      }
    }
    
    void check_balance()
    {
      float gain1;
      float gain2;
    // get balance from knob0
      int knob0 = analogRead(A0);
      if ((knob0 < bal-9)||(knob0 > bal+9)) {     // allow for a bit of wobble
        bal = knob0;
    //        Serial.print("bal = ");
    //        Serial.println(bal);
        gain1 = bal / 1023.0;
    //        Serial.print("gain1 = ");
    //        Serial.print(gain1);
    //        Serial.print("   ");
        gain2 = (1023 - bal) / 1023.0;   //note decimal point on the float constant
    //        Serial.print("gain2 = ");
    //        Serial.println(gain2);
        mixer1.gain(0, gain2);         //left right swapped....
        mixer2.gain(0, gain1);
      }
    }
    Last edited by richrarobi; 03-17-2020 at 09:01 PM. Reason: reread

Posting Permissions

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