721
edits
No edit summary |
|||
(32 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
#*# from "output" add "i2s" | #*# from "output" add "i2s" | ||
#*# from "control" add "sgtl5000" [[File:teensy_webinterface.png|thumb|690px|Audio System Design Tool "Audio through patch"]] | #*# from "control" add "sgtl5000" [[File:teensy_webinterface.png|thumb|690px|Audio System Design Tool "Audio through patch"]] | ||
# Click the export button to generate the setup code for your teensy and paste it in your Arduino IDE patch! | # Click the export button to generate the setup code for your teensy and paste it in your Arduino IDE patch, just before the setup routine! You should get output similar to the following block, paste it at the beginning of your patch.<syntaxhighlight lang="c++"> | ||
#include <Audio.h> | |||
#include <Wire.h> | |||
#include <SPI.h> | |||
#include <SD.h> | |||
#include <SerialFlash.h> | |||
// GUItool: begin automatically generated code | |||
AudioInputI2S i2s1; //xy=200,69 | |||
AudioOutputI2S i2s2; //xy=365,94 | |||
AudioConnection patchCord1(i2s1, 0, i2s2, 0); | |||
AudioConnection patchCord2(i2s1, 1, i2s2, 1); | |||
AudioControlSGTL5000 sgtl5000_1; //xy=302,184 | |||
// GUItool: end automatically generated code | |||
</syntaxhighlight> | |||
# 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:<syntaxhighlight lang="c++"> | # 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:<syntaxhighlight lang="c++"> | ||
void setup() { | void setup() { | ||
Line 17: | Line 31: | ||
AudioMemory(8); | AudioMemory(8); | ||
// Enable the audio shield and set the output volume. | // Enable the audio shield and set the output volume. | ||
sgtl5000_1.enable(); | sgtl5000_1.enable(); | ||
sgtl5000_1.inputSelect(AUDIO_INPUT_MIC); | sgtl5000_1.inputSelect(AUDIO_INPUT_MIC); | ||
sgtl5000_1.micGain(36); //from 0dB 40dB | sgtl5000_1.micGain(36); //from 0dB 40dB | ||
sgtl5000_1.volume(0.8); // from 0.0 to 1.0 // 0.8 standard | sgtl5000_1.volume(0.8); // from 0.0 to 1.0 // 0.8 standard | ||
} | |||
</syntaxhighlight>Note that you really need to declare the loop() function, otherwise your code doesn't compile!<syntaxhighlight lang="c++"> | |||
void loop(){ | |||
// nothing to be done here | |||
// it just needs to be declared to compile correctly :) | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# Fingers | # Fingers crossed, press upload and compile the patch! | ||
Line 31: | Line 50: | ||
==Homework: Coding an Envelope follower== | ==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! | 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! | ||
With the help of the [https://www.pjrc.com/teensy/td_libs_AudioNewObjects.html teensy documentation] and my | |||
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]] | |||
# 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 getPeakOfBlock(float* block, int n_smps){ | |||
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]; | |||
} | |||
} | |||
return 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.] | |||
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 to write your own code for the teensy audio library. 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. Look at my blank example file to see how to access the audio samples from the piezo. If you have questions about the template code please ask on our [https://signal.group/#CjQKIGr9R1a7Znwe1Ca0pmbx3rvHHzDaS1c_LnmgwwVk1KoSEhB6aHUiokel3vtGVoTErtfB signal group]! | |||
As a last step, print out the calculated envelope on your serial plotter to get a picture like this: | |||
[[File:serial_plotter.png|800px|piezo envelope signal in serial plotter]] | |||
Send me your code and a picture like this (hopefully) until Sunday! | |||
Happy Coding!!! |