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

Thread: Playing a tone with FIR filter

  1. #1
    Junior Member
    Join Date
    Jan 2015
    Posts
    11

    Playing a tone with FIR filter

    Hi there,

    I using teensy 3.1 with the audio shield. I'm trying to play a frequency tone with a fir filter. I've tried the examples on FIR filter. However, the example uses audio input which is for microphone for line input. I've tried editing the audio input part to

    Code:
    AudioSynthWaveformSine  sine1; 
    AudioEffectEnvelope    envelope1;
    // Use the fast FIR filter for left and right channels
    AudioFilterFIR       myFilterL(true);
    AudioFilterFIR       myFilterR(true);
    AudioOutputI2S       audioOutput;        // audio shield: headphones & line-out
    
    // Create Audio connections between the components
    // Route audio into the left and right filters
    AudioConnection c1(audioInput, 0, myFilterL, 0);
    AudioConnection c2(audioInput, 1, myFilterR, 0);
    // Route the output of the filters to their respective channels
    AudioConnection c3(myFilterL, 0, audioOutput, 0);
    AudioConnection c4(myFilterR, 0, audioOutput, 1);
    AudioControlSGTL5000 audioShield;
    But I can't compile it. It shows a few errors:
    This report would have more information with
    "Show verbose output during compilation"
    enabled in File > Preferences.
    Arduino: 1.0.6 + Td: 1.20 (Windows NT (unknown)), Board: "Teensy 3.1"
    Filter_FIR:49: error: no matching function for call to 'AudioFilterFIR::AudioFilterFIR(int)'
    Filter_FIR.ino:49:36: note: candidates are:
    In file included from C:\Program Files (x86)\Arduino\libraries\Audio/Audio.h:75:0,
    from Filter_FIR.ino:24:
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:38:2: note: AudioFilterFIR::AudioFilterFIR()
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:38:2: note: candidate expects 0 arguments, 1 provided
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:35:7: note: constexpr AudioFilterFIR::AudioFilterFIR(const AudioFilterFIR&)
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:35:7: note: no known conversion for argument 1 from 'int' to 'const AudioFilterFIR&'
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:35:7: note: constexpr AudioFilterFIR::AudioFilterFIR(AudioFilterFIR&&)
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:35:7: note: no known conversion for argument 1 from 'int' to 'AudioFilterFIR&&'
    Filter_FIR:50: error: no matching function for call to 'AudioFilterFIR::AudioFilterFIR(int)'
    Filter_FIR.ino:50:36: note: candidates are:
    In file included from C:\Program Files (x86)\Arduino\libraries\Audio/Audio.h:75:0,
    from Filter_FIR.ino:24:
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:38:2: note: AudioFilterFIR::AudioFilterFIR()
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:38:2: note: candidate expects 0 arguments, 1 provided
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:35:7: note: constexpr AudioFilterFIR::AudioFilterFIR(const AudioFilterFIR&)
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:35:7: note: no known conversion for argument 1 from 'int' to 'const AudioFilterFIR&'
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:35:7: note: constexpr AudioFilterFIR::AudioFilterFIR(AudioFilterFIR&&)
    C:\Program Files (x86)\Arduino\libraries\Audio/filter_fir.h:35:7: note: no known conversion for argument 1 from 'int' to 'AudioFilterFIR&&'

    Is there any other ways to input a frequency tone into a FIR filter?

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Location
    Boston, MA, USA
    Posts
    1,111
    Always post complete source code & details to reproduce any issue!

  3. #3
    Junior Member
    Join Date
    Jan 2015
    Posts
    11
    I've tried editing some parts. The error message is gone and the program can be compile. But there is no sound output also.

    Code:
    /*
    c
      - released
    b
    - Use FIR filters with fast_fft option
    
    The audio board uses the following pins.
     6 - MEMCS
     7 - MOSI
     9 - BCLK
    10 - SDCS
    11 - MCLK
    12 - MISO
    13 - RX
    14 - SCLK
    15 - VOL
    18 - SDA
    19 - SCL
    22 - TX
    23 - LRCLK
    */
    
    //#include <arm_math.h>
    #include <Audio.h>
    #include <Wire.h>
    #include <SD.h>
    #include <SPI.h>
    #include <Bounce.h>
    #include "filters.h"
    
    // If this pin is grounded the FIR filter is turned off
    // which just passes the audio sraight through
    // Don't use any of the pins listed above
    #define PASSTHRU_PIN 1
    // If this pin goes low the next FIR filter in the list
    // is switched in.
    #define FILTER_PIN 0
    
    // debounce the passthru and filter switching pins
    Bounce b_passthru = Bounce(PASSTHRU_PIN,15);
    Bounce b_filter   = Bounce(FILTER_PIN,15);
    
    //const int myInput = AUDIO_INPUT_MIC;
    const int myInput = AUDIO_INPUT_LINEIN;
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformSine   sine1;          //xy=125,221
    //AudioSynthNoisePink      pink1;          //xy=133,121
    //AudioEffectEnvelope      envelope1;      //xy=298,133
    AudioEffectEnvelope      envelope2;      //xy=302,197
    AudioAnalyzeFFT256       fft256_1;       //xy=304,272
    AudioMixer4              mixer1;         //xy=486,163
    AudioOutputI2S           i2s1;           //xy=640,161
    AudioConnection          patchCord1(sine1, envelope2);
    AudioConnection          patchCord2(sine1, fft256_1);
    //AudioConnection          patchCord3(pink1, envelope1);
    //AudioConnection          patchCord4(envelope1, 0, mixer1, 0);
    //AudioConnection          patchCord5(envelope2, 0, mixer1, 1);
    //AudioConnection          patchCord6(mixer1, 0, i2s1, 0);
    //AudioConnection          patchCord7(mixer1, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=517,297
    // GUItool: end automatically generated code
    
    AudioFilterFIR      myFilterL;
    AudioFilterFIR      myFilterR;
    AudioOutputI2S      audioOutput;        // audio shield: headphones & line-out
    
    // Create Audio connections between the components
    // Route audio into the left and right filters
    AudioConnection c1(sine1, 0, myFilterL, 0);
    AudioConnection c2(sine1, 1, myFilterR, 0);
    // Route the output of the filters to their respective channels
    AudioConnection c3(myFilterL, 0, audioOutput, 0);
    AudioConnection c4(myFilterR, 0, audioOutput, 1);
    AudioControlSGTL5000 audioShield;
    
    
    struct fir_filter {
      short *coeffs;
      short num_coeffs;
    };
    
    // index of current filter. Start with the low pass.
    int fir_idx = 0;
    struct fir_filter fir_list[] = {
      {low_pass , 100},    // low pass with cutoff at 1kHz and -60dB at 2kHz
      {band_pass, 100},    // bandpass 1200Hz - 1700Hz
      {NULL,      0}
    };
    
    
    void setup() {
      Serial.begin(9600);
      delay(300);
    
      pinMode(PASSTHRU_PIN, INPUT_PULLUP);
      pinMode(FILTER_PIN, INPUT_PULLUP);
    
      // allocate memory for the audio library
      AudioMemory(8);
    
      audioShield.enable();
      audioShield.inputSelect(myInput);
      audioShield.volume(0.3);
      
      // Warn that the passthru pin is grounded
      if(!digitalRead(PASSTHRU_PIN)) {
        Serial.print("PASSTHRU_PIN (");
        Serial.print(PASSTHRU_PIN);
        Serial.println(") is grounded");
        
       // give the audio library some memory.  We'll be able
      // to see how much it actually uses, which can be used
      // to reduce this to the minimum necessary.
      AudioMemory(20);
    
      // enable the audio shield
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.1);   
      
      // create a simple bass note using a sine wave
      sine1.frequency(1000);
      sine1.amplitude(0.6);
      envelope2.attack(6.5);
      envelope2.hold(60);
      envelope2.decay(70);
      envelope2.sustain(0);
      
      // add both the note together, so we can hear them
      mixer1.gain(0, 0.5);
      mixer1.gain(1, 0.5);
      }
    
      // Warn that the filter pin is grounded
      if(!digitalRead(FILTER_PIN)) {
        Serial.print("FILTER_PIN (");
        Serial.print(FILTER_PIN);
        Serial.println(") is grounded");
      }  
      // Initialize the filter
      myFilterL.begin(fir_list[0].coeffs, fir_list[0].num_coeffs);
      myFilterR.begin(fir_list[0].coeffs, fir_list[0].num_coeffs);
      Serial.println("setup done");
    }
    
    // index of current filter when passthrough is selected
    int old_idx = -1;
    
    // audio volume
    int volume = 0;
    unsigned long last_time = millis();
    
    
    int count = 0;
    int speed = 60;
    
    void loop()
    {
      // Volume control
      int n = analogRead(15);
      if (n != volume) {
        volume = n;
        //uncomment this line if your audio shield has the volume pot
        //audioShield.volume((float)n / 1023);
      }
      
      // update the two buttons
      b_passthru.update();
      b_filter.update();
      
      // If the passthru button is pushed, save the current
      // filter index and then switch the filter to passthru
      if (b_passthru.fallingEdge()) {
        old_idx = fir_idx;
        myFilterL.begin(FIR_PASSTHRU, 0);
        myFilterR.begin(FIR_PASSTHRU, 0);
      }
      
      // If passthru button is released, restore previous filter
      if (b_passthru.risingEdge()) {
        if(old_idx != -1) {
          myFilterL.begin(fir_list[fir_idx].coeffs, fir_list[fir_idx].num_coeffs);
          myFilterR.begin(fir_list[fir_idx].coeffs, fir_list[fir_idx].num_coeffs);
        }
        old_idx = -1;
      }
      
      // switch to next filter in the list
      if (b_filter.fallingEdge()) {
        fir_idx++;
        if (fir_list[fir_idx].num_coeffs == 0) fir_idx = 0;
        myFilterL.begin(fir_list[fir_idx].coeffs, fir_list[fir_idx].num_coeffs);
        myFilterR.begin(fir_list[fir_idx].coeffs, fir_list[fir_idx].num_coeffs);
      }
    
      // print information about resource usage
      // Proc = 18 (18),  Mem = 4 (5)
      if (millis() - last_time >= 2500) {
        Serial.print("Proc = ");
        Serial.print(AudioProcessorUsage());
        Serial.print(" (");    
        Serial.print(AudioProcessorUsageMax());
        Serial.print("),  Mem = ");
        Serial.print(AudioMemoryUsage());
        Serial.print(" (");    
        Serial.print(AudioMemoryUsageMax());
        Serial.println(")");
        last_time = millis();
      }
    
    
    }

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Location
    Boston, MA, USA
    Posts
    1,111
    The connections between the envelope and the mixer, and the mixer and the output, are commented out so there will not be any sound.

  5. #5
    Junior Member
    Join Date
    Jan 2015
    Posts
    11
    I've edited the part that you mentioned. However, there is still no audio output.

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SD.h>
    #include <SPI.h>
    #include <Bounce.h>
    #include "filters.h"
    
    // If this pin is grounded the FIR filter is turned off
    // which just passes the audio sraight through
    // Don't use any of the pins listed above
    #define PASSTHRU_PIN 1
    // If this pin goes low the next FIR filter in the list
    // is switched in.
    #define FILTER_PIN 0
    
    // debounce the passthru and filter switching pins
    Bounce b_passthru = Bounce(PASSTHRU_PIN,15);
    Bounce b_filter   = Bounce(FILTER_PIN,15);
    
    //const int myInput = AUDIO_INPUT_MIC;
    const int myInput = AUDIO_INPUT_LINEIN;
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformSine   sine1;          //xy=125,221
    //AudioSynthNoisePink      pink1;          //xy=133,121
    //AudioEffectEnvelope      envelope1;      //xy=298,133
    AudioEffectEnvelope      envelope2;      //xy=302,197
    AudioAnalyzeFFT256       fft256_1;       //xy=304,272
    AudioMixer4              mixer1;         //xy=486,163
    AudioOutputI2S           i2s1;           //xy=640,161
    AudioConnection          patchCord1(sine1, envelope2);
    AudioConnection          patchCord2(sine1, fft256_1);
    //AudioConnection          patchCord3(pink1, envelope1);
    //AudioConnection          patchCord4(envelope1, 0, mixer1, 0);
    AudioConnection          patchCord5(envelope2, 0, mixer1, 1);
    AudioConnection          patchCord6(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord7(mixer1, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=517,297
    // GUItool: end automatically generated code
    
    AudioFilterFIR      myFilterL;
    AudioFilterFIR      myFilterR;
    AudioOutputI2S      audioOutput;        // audio shield: headphones & line-out
    
    // Create Audio connections between the components
    // Route audio into the left and right filters
    AudioConnection c1(sine1, 0, myFilterL, 0);
    AudioConnection c2(sine1, 1, myFilterR, 0);
    // Route the output of the filters to their respective channels
    AudioConnection c3(myFilterL, 0, audioOutput, 0);
    AudioConnection c4(myFilterR, 0, audioOutput, 1);
    AudioControlSGTL5000 audioShield;
    
    
    struct fir_filter {
      short *coeffs;
      short num_coeffs;
    };
    
    // index of current filter. Start with the low pass.
    int fir_idx = 0;
    struct fir_filter fir_list[] = {
      {low_pass , 100},    // low pass with cutoff at 1kHz and -60dB at 2kHz
      {band_pass, 100},    // bandpass 1200Hz - 1700Hz
      {NULL,      0}
    };
    
    
    void setup() {
      Serial.begin(9600);
      delay(300);
    
      pinMode(PASSTHRU_PIN, INPUT_PULLUP);
      pinMode(FILTER_PIN, INPUT_PULLUP);
    
      // allocate memory for the audio library
      AudioMemory(8);
    
      audioShield.enable();
      audioShield.inputSelect(myInput);
      audioShield.volume(0.5);
      
      // Warn that the passthru pin is grounded
      if(!digitalRead(PASSTHRU_PIN)) {
        Serial.print("PASSTHRU_PIN (");
        Serial.print(PASSTHRU_PIN);
        Serial.println(") is grounded");
        
       // give the audio library some memory.  We'll be able
      // to see how much it actually uses, which can be used
      // to reduce this to the minimum necessary.
      AudioMemory(20);
    
      // enable the audio shield
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.1);   
      
      // create a simple bass note using a sine wave
      sine1.frequency(500);
      sine1.amplitude(0.6);
      envelope2.attack(6.5);
      envelope2.hold(60);
      envelope2.decay(70);
      envelope2.sustain(0);
      
      // add both the note together, so we can hear them
      mixer1.gain(0, 2);
      mixer1.gain(1, 2);
      }
    
      // Warn that the filter pin is grounded
      if(!digitalRead(FILTER_PIN)) {
        Serial.print("FILTER_PIN (");
        Serial.print(FILTER_PIN);
        Serial.println(") is grounded");
      }  
      // Initialize the filter
      myFilterL.begin(fir_list[0].coeffs, fir_list[0].num_coeffs);
      myFilterR.begin(fir_list[0].coeffs, fir_list[0].num_coeffs);
      Serial.println("setup done");
    }
    
    // index of current filter when passthrough is selected
    int old_idx = -1;
    
    // audio volume
    int volume = 0;
    unsigned long last_time = millis();
    
    
    int count = 0;
    int speed = 60;
    
    void loop()
    {
      // Volume control
      int n = analogRead(15);
      if (n != volume) {
        volume = n;
        //uncomment this line if your audio shield has the volume pot
        //audioShield.volume((float)n / 1023);
      }
      
      // update the two buttons
      b_passthru.update();
      b_filter.update();
      
      // If the passthru button is pushed, save the current
      // filter index and then switch the filter to passthru
      if (b_passthru.fallingEdge()) {
        old_idx = fir_idx;
        myFilterL.begin(FIR_PASSTHRU, 0);
        myFilterR.begin(FIR_PASSTHRU, 0);
      }
      
      // If passthru button is released, restore previous filter
      if (b_passthru.risingEdge()) {
        if(old_idx != -1) {
          myFilterL.begin(fir_list[fir_idx].coeffs, fir_list[fir_idx].num_coeffs);
          myFilterR.begin(fir_list[fir_idx].coeffs, fir_list[fir_idx].num_coeffs);
        }
        old_idx = -1;
      }
      
      // switch to next filter in the list
      if (b_filter.fallingEdge()) {
        fir_idx++;
        if (fir_list[fir_idx].num_coeffs == 0) fir_idx = 0;
        myFilterL.begin(fir_list[fir_idx].coeffs, fir_list[fir_idx].num_coeffs);
        myFilterR.begin(fir_list[fir_idx].coeffs, fir_list[fir_idx].num_coeffs);
      }
    
      // print information about resource usage
      // Proc = 18 (18),  Mem = 4 (5)
      if (millis() - last_time >= 2500) {
        Serial.print("Proc = ");
        Serial.print(AudioProcessorUsage());
        Serial.print(" (");    
        Serial.print(AudioProcessorUsageMax());
        Serial.print("),  Mem = ");
        Serial.print(AudioMemoryUsage());
        Serial.print(" (");    
        Serial.print(AudioMemoryUsageMax());
        Serial.println(")");
        last_time = millis();
      }
    
    
    }

  6. #6
    Senior Member
    Join Date
    Nov 2012
    Location
    Boston, MA, USA
    Posts
    1,111
    Are you really using two audio shields? You have two AudioOutputI2S objects, one called i2s1 and another called audioOutput. Also two control objects, audioShield and sgtl5000_1. Suggest you create the audio graph you actually want to use (in the GUI tool) and then paste that in, in place of the section that is there now. Then check that any code you have copied from examples is calling the object by the same name and edit if not.

Posting Permissions

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