IFD:EAI SoS21/course material/Session 6: Getting Started with Teensyduino: Difference between revisions

From Medien Wiki
Line 34: Line 34:
We want to code a so called envelope follower, it tracks the peaks of our audio signal and gets the rough volume shape. With the help of the volume (~loudness) of our signal we can detect wether there is vibration on our piezo or not. In rough terms we get the volume envelope in two steps:
We want to code a so called envelope follower, it tracks the peaks of our audio signal and gets the rough volume shape. With the help of the volume (~loudness) of our signal we can detect wether there is vibration on our piezo or not. In rough terms we get the volume envelope in two steps:


# get the maximum (peak) of the signal samples seen so far (this is also called a peak hold circuit [[File:peak_hold.png|800px]]
# Get the maximum (peak) of the signal samples seen so far (this is also called a peak hold circuit [[File:peak_hold.png|800px]]
# multiply this maximum with a decay factor (<1) for every new sample, or sample block (envelope follower) [[File:envelope_follower.png|800px]]
# Multiply this maximum with a decay factor (<1) for every new sample, or sample block (envelope follower) [[File:envelope_follower.png|800px]]


To get a bit more accuracy you might want to get the absolute value of our signal before tracking the maximum.
To get a bit more accuracy you might want to get the absolute value of our signal before tracking the maximum.


With the help of the [https://www.pjrc.com/teensy/td_libs_AudioNewObjects.html teensy documentation] and [[:File:envelope_follower_template.zip|my empty template file]] you should be able to grasp how this works. Basically you write your own teensy audio object by including a C++ class that is structured as documented in the link above. This class will receive a pointer to the audio input stream coming from the piezo and can do some work on it. If you have questions about the template code please ask on our [https://signal.group/#CjQKIGr9R1a7Znwe1Ca0pmbx3rvHHzDaS1c_LnmgwwVk1KoSEhB6aHUiokel3vtGVoTErtfB signal group]!
With the help of the [https://www.pjrc.com/teensy/td_libs_AudioNewObjects.html teensy documentation] and [[:File:envelope_follower_template.zip|my empty template file]] you should be able to grasp how this works. Basically you write your own teensy audio object by including a C++ class that is structured as documented in the link above. This class will receive a pointer to the audio input stream coming from the piezo and can do some work on it. If you have questions about the template code please ask on our [https://signal.group/#CjQKIGr9R1a7Znwe1Ca0pmbx3rvHHzDaS1c_LnmgwwVk1KoSEhB6aHUiokel3vtGVoTErtfB signal group]!

Revision as of 17:40, 25 May 2021

After you soldered your headers and mics, it's time to try and get some sound in and output from your teensy devices! Here is a short how-to, partially summarizing what we did in the last online session.

  1. Installl Arduino IDE and Teensyduino: https://www.pjrc.com/teensy/td_download.html
  2. Open and program the example patch
    opening the example test patch
  3. Connect some headphones on the audio shield's headphone out and listen! You should be able to hear a sine wave switching amplitudes.

    If you are having problems connecting your teensies :( read through the following sections:
  4. Now we want to listen to the actual piezo disc sounds. Check out the teensy web ide patches: https://www.pjrc.com/teensy/gui/
    • add the following nodes from the gui:
      1. from "input" add "i2s"
      2. from "output" add "i2s"
      3. from "control" add "sgtl5000"
        Audio System Design Tool "Audio through patch"
  5. Click the export button to generate the setup code for your teensy and paste it in your Arduino IDE patch!
  6. In the setup routine of the arduino IDE you need code to choose the microphone port and audio levels. It should look more or less like this:
    void setup() {
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(8);
      // Enable the audio shield and set the output volume.
     
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
      sgtl5000_1.micGain(36); //from 0dB 40dB
      sgtl5000_1.volume(0.8); // from 0.0 to 1.0 // 0.8 standard
    }
  7. Fingers crossed, press upload and compile the patch!


Now put some headphones on the teensy headphone connector and hopefully listen to some interesting piezo disc microphonic sounds (this is where the word micro-phone really comes from, it's analogous to microscope, actually).

Homework: Coding an Envelope follower

If you were successful with setting up the teensy and piezo microphone, you can make a step further and learn how to wrestle with audio data directly on the teensy!

We want to code a so called envelope follower, it tracks the peaks of our audio signal and gets the rough volume shape. With the help of the volume (~loudness) of our signal we can detect wether there is vibration on our piezo or not. In rough terms we get the volume envelope in two steps:

  1. Get the maximum (peak) of the signal samples seen so far (this is also called a peak hold circuit Peak hold.png
  2. Multiply this maximum with a decay factor (<1) for every new sample, or sample block (envelope follower) Envelope follower.png

To get a bit more accuracy you might want to get the absolute value of our signal before tracking the maximum.

With the help of the teensy documentation and my empty template file you should be able to grasp how this works. Basically you write your own teensy audio object by including a C++ class that is structured as documented in the link above. This class will receive a pointer to the audio input stream coming from the piezo and can do some work on it. If you have questions about the template code please ask on our signal group!