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

Thread: Audio Sample Rate: how to change from 44100 Hz (44117.65 Hz) to 48000?

  1. #1
    Member Revalogics's Avatar
    Join Date
    Dec 2016
    Location
    Philippines
    Posts
    51

    Audio Sample Rate: how to change from 44100 Hz (44117.65 Hz) to 48000?

    So I have made a sketch for Teensy 3.6 I have that uses a single ADC as an input and USB as an output with nothing connected between them. I tested it using Windows and it worked OK, aside from faint RF noises I'm hearing so I put a 40nF ceramic capacitor between ADC input and AGND. I tried it on Linux (Ubuntu 16.04) using JACK audio connection toolkit, a realtime low-latency audio driver, and it worked OK for the first few seconds and gets noisy and noisy as time goes by and after a while, noise comes down gradually, going to clean audio again. Maybe it is caused by the inaccuracy of the sample rate?

    I choose 48000 Hz because clock frequencies in Teensy can be divided and will arrive at 48000 accurately.

    Anyone knows how to change the sample rate, especially for ADC, USB, and any other peripherals involved?

    Code:
    #include <Audio.h>
    
    AudioInputAnalog adc1;
    AudioOutputUSB usb1;
    AudioConnection patchCord1(adc1, 0, usb1, 0);
    AudioConnection patchCord2(adc1, 0, usb1, 1);
    
    elapsedMillis fader13timer2;
    byte fader13timer = 0;
    byte fader13PWM = 0;
    boolean dir = 0;
    
    void setup() {
      AudioMemory(16);
      pinMode(13, OUTPUT);
      digitalWrite(13, HIGH);
    }
    
    void loop() {
      fader13();
    }
    
    void fader13() {
      if(fader13timer == 0) digitalWrite(13, 1);
      if(fader13timer >= fader13PWM) digitalWrite(13, 0);
      if(fader13timer2 >= 5) {
        fader13timer2 = 0;
        if(dir) {
          fader13PWM++;
          if(fader13PWM == 255) dir = 0;
        }
        if(!dir) {
          fader13PWM--;
          if(fader13PWM == 0) dir = 1;
        }
      }
      fader13timer++;
    }

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    824
    See this thread
    My code in message #44 in that thread has the setI2SFreq function (by @FrankB) which you can use to set the sampling frequency.
    I think it is the current version. In theory all you need is that function and then call it in setup().
    Code:
      setI2SFreq(48000);
    Pete

  3. #3
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    843
    Quote Originally Posted by Revalogics View Post
    I choose 48000 Hz because clock frequencies in Teensy can be divided and will arrive at 48000 accurately.

    Anyone knows how to change the sample rate, especially for ADC, USB, and any other peripherals involved?
    Though I haven't tried it you would need to change the PDB MOD register value in pdb.h (PDB_PERIOD). If you running at 48MHz bus then use (1000-1), there are probably many other things to change that I don't know about either especially with the USB audio interface.

  4. #4
    Senior Member Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    3,935
    Yes, thats mentioned (and a piece of code is there, too) in the thread el-supremo mentioned.

Posting Permissions

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