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 or peak detector circuit [[File:peak_hold.png|800px]]
# Get the maximum (peak) of the signal samples seen so far. This is also called a peak hold or peak detector 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]]


This is a code snippet that illustrates how to search for the peak in a block of audio samples:
<syntaxhighlight lang="c++">
float peak=0;
// imagine that the audio block and its length (n_smps) is given to you
for (int i=0; i<n_smps; i++) {
    if (block[i]>peak) {
        // we remember every peak that
        // is greater then we seen so far
        peak = block[i];
        // we write our peak hold directly
        // the audio output
        out_block[i]=peak;
    }
}
</syntaxhighlight>
For our envelope follower, we just decay the peak signal with every new sample by a decay factor that is slightly smaller then 1 (e.g. 0.95).
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]!