/*
\\DS1821\Datas\ELECTRONIQUE\TEENSY\Essais pgm\sketch_FIR_002\sketch_FIR_002.ino
*/
/*
Dans cette verion 002 on rajoute les lignes telles que définies dans la vidéo:
https://www.youtube.com/watch?v=uf0KLkB93eY
qui montre:
-comment rentrer les coefficients du filtre,
-comment initialiser les objets créés par l'Audio Design Tool
-comment créer le void setup,
*/
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>
#include <filter_fir.h>
//This section has been copied from the Teensy FIR example
// 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 //donc si je comprends bien il faut mettre la pin 1 à +3.3V ??
// If this pin goes low the next FIR filter in the list
// is switched in.
#define FILTER_PIN 0 //if I understand correctly, the input of the Filter is pin 0
/*
These lines are followed by two "debounce" instrutions that are unknown to me,
// seem to manage the buttons, that I don't use in my project, so I remove the "bounce" lines
*/
// GUItool: begin automatically generated code
AsyncAudioInputSPDIF3 spdif_async1; //xy=145.3333282470703,121.33333587646484
AudioFilterFIR fir1; //xy=353.25,121.25
AudioOutputI2S i2s1; //xy=569.25,126.25
AudioConnection patchCord1(spdif_async1, 0, fir1, 0);
AudioConnection patchCord2(fir1, 0, i2s1, 0);
AudioControlSGTL5000 sgtl5000_1; //xy=360.25,265.25
// GUItool: end automatically generated code
//LPF Coefficients provided by T-Filter
// I got only 5 coefficients, that is not enough, but I don't know why !?!
// The declaration below seems similar to the "struct" declaration used in the Teensy FIR Filter example
// but the "const" approach has been said to be more optimized than the "struct" approach
const short fir1_Coeffs[5] = { //le nombre de coefficients doit etre > à 4 d'après la doc Teensy.
// il faudra remplacer les 5 coeffs ci-dessous générés par T-Filter, par les 57 coefficients générés par
// arc.id.au et relatifs à un Filtre de Kaiser-Bessel de type Window
(short) (32768 * -0.635640527595689),
(short) (32768 * -0.20976263263201292),
(short) (32768 * 0.6981598830670951),
(short) (32768 * -0.20976263263201292),
(short) (32768 * -0.635640527595689)
};
//const int myInput = AUDIO_INPUT_MIC; // remove from the Teensy FIR example
const int myInput = AUDIO_INPUT_LINEIN; // because my correct input is not MIC
// d'autres exemples utilisent myInput = AUDIO_INPUT_LINEIN;
// avec un diagramme AudioDesignTool dans le domaine digital aussi
// par ex le prog "Invert Phase Audio Signal"
unsigned long last_time = millis(); // copied from the Teensy FIR example
void setup()
{
Serial.begin(9600); //copied from the Teensy FIR Filter example
delay(300); //copied from the Teensy FIR Filter example
pinMode(PASSTHRU_PIN, INPUT_PULLUP); //probably activates the Pull-Up resistor because PASSTHRU_PIN is defined as an input
pinMode(FILTER_PIN, INPUT_PULLUP); //probably activates the Pull-Up resistor because FILTER_PIN is defined as an input
//setup the audio shield
AudioNoInterrupts(); // added to the Teensy FIR example: recommended in the video
// what are the benefit ???
// mentionned at the very beginning
AudioMemory(16); /*
The numberBlocks input specifies how much memory to reserve for audio data.
Each block holds 128 audio samples, or approx 2.9 ms of sound.
*/
// Enable the audio shield. select input. and enable output
sgtl5000_1.enable(); //added: was it missing in the Teensy example
sgtl5000_1.InputSelect(myInput); /*
dois-je mettre spdif_async1 ou bien LINEIN comme dans la video ???
Je pense spdif_async1 puisque c'est le nom donné à AsyncAudioInputSPDIF3
qui remplace le AudioInputI2S de la video
Pas sûr car le pgm "Invert Phase Audio Signal" utilise bien
AUDIO_INPUT_LINEIN, donc je laisse myInput défini à
AUDIO_INPUT_LINEIN
*/
sgtl5000_1.lineInLevel(5);
sgtl5000_1.unmuteheadphone();
sgtl5000_1.volume(0.5); // I have a doubt about the parameter: is it correct ???
fir1.begin(fir1_Coeffs, 5); //initialize the fir filter 'fir1" declared before
AudioInterrupts(); //as recommended in the video
// Warn on the Serial Monitor if the passthru pin is grounded
if(!digitalRead(PASSTHRU_PIN)) {
Serial.print("PASSTHRU_PIN (");
Serial.print(PASSTHRU_PIN);
Serial.println(") is grounded");
}
// Warn if the filter pin is grounded: what for ??
//it is supposed to be the filter input, isn't it ?
if(!digitalRead(FILTER_PIN)) {
Serial.print("FILTER_PIN (");
Serial.print(FILTER_PIN);
Serial.println(") is grounded");
}
//ADDED to the Teensy FIR example (as recommended by the video)
fir1.begin(fir1_Coeffs, 5); //Iniitialize the LPF filter as recommended by the AudioDesignTool
Serial.print("fir1 Filter: begin done"); // displays on the Serial Monitor
Serial.println("setup done");
}
void loop() //la video ne dit pas quoi faire dans cette boucle loop,
//et l'exemple de Teensy pas grand chose !
{
// print information about resource usage
// Proc = 18 (18), Mem = 4 (5)
if (millis() - last_time >= 2500) {
Serial.print("Proc = ");
Serial.print(AudioProcessorUsage());
Serial.print(" (");
Serial.print(AudioProcessorUsageMax());
Serial.print("), Mem = ");
Serial.print(AudioMemoryUsage());
Serial.print(" (");
Serial.print(AudioMemoryUsageMax());
Serial.println(")");
last_time = millis();
}
}