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

Thread: Generating sine wave from teensy 3.1 board

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

    Generating sine wave from teensy 3.1 board

    Hi,

    I'm currently trying to generate a pure tone of 1kHz from the teensy board itself. However, although the program can be uploaded into the board, there's no sound output from the audio output. Did i define any part wrong?

    Code:
    #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);
    
    
    
    AudioSynthWaveformSine   sine1; //generate sine wave
    
    
    // Use the fast FIR filter for left and right channels
    AudioFilterFIR       myFilterL1;
    AudioFilterFIR       myFilterR1;
    AudioFilterFIR       myFilterL2;
    AudioFilterFIR       myFilterR2;
    AudioFilterFIR       myFilterL3;
    AudioFilterFIR       myFilterR3;
    AudioFilterFIR       myFilterL4;
    AudioFilterFIR       myFilterR4;
    AudioFilterFIR       myFilterL5;
    AudioFilterFIR       myFilterR5;
    AudioMixer4          mixerL1;
    AudioMixer4          mixerR1;
    AudioMixer4          mixerL2;
    AudioMixer4          mixerR2;
    AudioMixer4          mixerLeft;
    AudioMixer4          mixerRight;
    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, myFilterL1, 0);
    AudioConnection c2(sine1, 0, myFilterR1, 0);
    AudioConnection c3(sine1, 0, myFilterL2, 0);
    AudioConnection c4(sine1, 0, myFilterR2, 0);
    AudioConnection c5(sine1, 0, myFilterL3, 0);
    AudioConnection c6(sine1, 0, myFilterR3, 0);
    AudioConnection c7(sine1, 0, myFilterL4, 0);
    AudioConnection c8(sine1, 0, myFilterR4, 0);
    AudioConnection c9(sine1, 0, myFilterL5, 0);
    AudioConnection c10(sine1, 0, myFilterR5, 0);
    // Route the output of the filters to their respective channels
    AudioConnection c11(myFilterL1, 0, mixerL1, 0);
    AudioConnection c12(myFilterR1, 0, mixerR1, 0);
    AudioConnection c13(myFilterL2, 0, mixerL1, 1);
    AudioConnection c14(myFilterR2, 0, mixerR1, 1);
    AudioConnection c15(myFilterL3, 0, mixerL1, 2);
    AudioConnection c16(myFilterR3, 0, mixerR1, 2);
    AudioConnection c17(myFilterL4, 0, mixerL1, 3);
    AudioConnection c18(myFilterR4, 0, mixerR1, 3);
    
    AudioConnection c19(myFilterL5, 0, mixerL2, 0);
    AudioConnection c20(myFilterR5, 0, mixerR2, 0);
    
    AudioConnection c21(mixerL1, 0, mixerLeft, 0);
    AudioConnection c22(mixerR1, 0, mixerRight, 0);
    AudioConnection c23(mixerL2, 0, mixerLeft, 1);
    AudioConnection c24(mixerR2, 0, mixerRight, 1);
    
    AudioConnection c25(mixerLeft, 0, audioOutput, 0);
    AudioConnection c26(mixerRight, 0, audioOutput, 1);
    
    
    AudioControlSGTL5000     sgtl5000_1; 
    
    
    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
      {hrtf_l_0, 100},    
      {hrtf_r_0, 100},  
      {hrtf_l_45, 100},    
      {hrtf_r_45, 100},    
      {hrtf_l_80, 100},    
      {hrtf_r_80, 100},      
    
     
      {NULL,      0}
    };
    
    
    void setup() {
      
      Serial.begin(9600);
      //while (!Serial) ;
      delay(3000);
    
    
      // It doesn't work properly with any less than 8
      AudioMemory(20);
      
       
      // enable the audio shield
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.8);
      
       // Create a synthetic sine wave, for testing
      // To use this, edit the connections above
      sine1.amplitude(0.8);
      sine1.frequency(1000.000);
      
      mixerL1.gain(0, 1);
      mixerR1.gain(0, 1);
      mixerL1.gain(1, 1);
      mixerR1.gain(1, 1);
      mixerL1.gain(2, 1);
      mixerR1.gain(2, 1);
      mixerL1.gain(3, 1);
      mixerR1.gain(3, 1);
      
      mixerL2.gain(0, 1);
      mixerR2.gain(0, 1);
      
      mixerLeft.gain(0, 1);
      mixerLeft.gain(1, 1);
      mixerLeft.gain(2, 1);
      mixerRight.gain(0, 1);
      mixerRight.gain(1, 1);
      mixerRight.gain(2, 1);
    
    
    
    
    
        // Warn that the passthru pin is grounded
      if(!digitalRead(PASSTHRU_PIN)) {
        Serial.print("PASSTHRU_PIN (");
        Serial.print(PASSTHRU_PIN);
        Serial.println(") is grounded");
      }
    
      // 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
        myFilterL1.begin(fir_list[0].coeffs,fir_list[0].num_coeffs);
        myFilterR1.begin(fir_list[0].coeffs,fir_list[0].num_coeffs);
        myFilterL2.begin(fir_list[1].coeffs,fir_list[1].num_coeffs);
        myFilterR2.begin(fir_list[1].coeffs,fir_list[1].num_coeffs);
        myFilterL3.begin(fir_list[2].coeffs,fir_list[1].num_coeffs);
        myFilterR3.begin(fir_list[3].coeffs,fir_list[1].num_coeffs);
        myFilterL4.begin(fir_list[4].coeffs,fir_list[1].num_coeffs);
        myFilterR4.begin(fir_list[5].coeffs,fir_list[1].num_coeffs);
        myFilterL5.begin(fir_list[6].coeffs,fir_list[1].num_coeffs);
        myFilterR5.begin(fir_list[7].coeffs,fir_list[1].num_coeffs);
       
    
      Serial.println("setup done");
    }
    
    // index of current filter when passthrough is selected
    int old_idx = -1;
    int inData = 0;
    
    // audio volume
    int volume = 0;
    unsigned long last_time = millis();
    
    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);
      }
    
          
     }

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,857
    Have you tried simply routing the sine object to the output, without all those filters? Or have you tried running examples like SamplePlayer, to verify your hardware is working when you run known-good code?

  3. #3
    Junior Member
    Join Date
    Jan 2015
    Posts
    11
    Ya. The program seems to be working after restarting the computer. However, when testing with pure tone, I observed that when I increase the gain from the program under the mixer gain, there would be a decrease of dB in the SPL meter. Is there any fault in the program as i did not change any thing in the program.

Posting Permissions

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