How to set a variable that can hold any audio filter object?

commi

Member
Hi,

I’m not very good in C++, so I got stuck on this for too long:

I’m trying to write a code that would change the patchCord on the fly.
I managed to make a sample code for that, while using a specific audio filter in each patchCord.connect command.
Now I wish to get the name of the audio filter from a variable, and I’m stuck there.
The “filter” can be from all of the possible audio filters types, so how do I define this variable?


Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveformSine   sine1;          //xy=158,74
AudioEffectEnvelope      envelope1;      //xy=232,156
AudioOutputI2S           i2s1;           //xy=611,61
AudioConnection          patchCord1(sine1, envelope1);
AudioConnection          patchCord2(envelope1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=195,272
// GUItool: end automatically generated code

void setup() {
  AudioMemory(120);
  sgtl5000_1.enable();

  // connectiong to specific filter works:
  patchCord2.disconnect();
  patchCord2.connect(sine1, 0, i2s1, 1); 

  // but how to use a variable to make the connection?
  AudioStream filter;
  // ^^^ which type I should use?
  
  filter = sine1;
  patchCord2.disconnect();
  patchCord2.connect(filter, 0, i2s1, 1); 

  delay (2000);
  filter = envelope1;
  patchCord2.disconnect();
  patchCord2.connect(filter, 0, i2s1, 1); 

}

void loop() {
  // put your main code here, to run repeatedly:
}
 
Something like this:
Code:
#include <Audio.h>

// GUItool: begin automatically generated code
AudioSynthWaveformSine   sine1;          //xy=158,74
AudioEffectEnvelope      envelope1;      //xy=232,156
AudioOutputI2S           i2s1;           //xy=611,61
AudioConnection          patchCord1(sine1, envelope1);
AudioConnection          patchCord2(envelope1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=195,272
// GUItool: end automatically generated code

void setup() 
{
  AudioMemory(120);
  sgtl5000_1.enable();

  // connectiong to specific filter works:
  patchCord2.disconnect();
  patchCord2.connect(sine1, 0, i2s1, 1); 

  // how to use a pointer variable to make the connection:
  AudioStream* filter;
  // using a pointer to the base class
  
  filter = &sine1;
  patchCord2.disconnect();
  patchCord2.connect(*filter, 0, i2s1, 1); 

  delay (2000);
  filter = &envelope1;
  patchCord2.disconnect();
  patchCord2.connect(*filter, 0, i2s1, 1); 
}

void loop() 
{
  
}
Note that the "filter" pointer contains no information as to the (derived) type of object it's pointing at: it's up to you to write code to remember that information and use it to cast the pointer to the right type if you want to. Something like:
Code:
((AudioSynthWaveformSine*) filter)->amplitude(0.5);
...which of course will go horribly wrong if you last pointed filter at envelope1
 
Thanks!

I would only use this kind of pointer for the patchCord. Real fiter manupulation will be done
direclty with the original object of the filter.
 
Hi, i'm interessed in creating an audio matrix in my TDM system,
is it possible to declare a patchCord without initializing it with all the variables,only for using it
to make patches on the fly? :
fake: AudioConnection patchCord1("",0,"",0);
 
I'm thinking about a "BLANK filter",to make a patch bay matrix,instead of using the teensy audio editor,
i could compile with only declare the patchCord : patchCord1();
but after i could't make a connection because it's not initialized in the pre-setup.
Anyone to see how to make this BLANK initialization?
 
Sorry but i have error,i declare in the pre setup: AudioConnection myNewPatchCord();
in the setup: myNewPatchCord.connect(sine2,0,tdm2,0);
that gives this error:
request for member 'connect' in 'myNewPatchCord', which is of non-class type 'AudioConnection()
same error with disconnect...
 
Alright,i made a very simple sketch to illustrate the error:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputTDM            tdm1;           //xy=55,1362.7408447265625
AudioInputTDMB           tdmB1;          //xy=55,1622.7408447265625
AudioSynthWaveformSine   sine1;          //xy=290,1316.7408447265625
AudioSynthWaveformSine   sine2;          //xy=292,1583.7408447265625
AudioOutputTDM           tdm2;           //xy=604,1365.7408447265625
AudioOutputTDMB          tdmB2;          //xy=603,1621.7408447265625

AudioConnection myNewPatchCord();

AudioControlCS42448      cs42448_2;      //xy=298,1730.7408447265625
AudioControlCS42448      cs42448_1;      //xy=303,1454.7408447265625
// GUItool: end automatically generated code


void setup() {
  Serial.begin(38400);
  Wire.begin();
 
cs42448_1.setAddress(0x48);
cs42448_1.enable();
 
cs42448_2.setAddress(0x4A);
cs42448_2.enable();

 AudioMemory(1000);

  if (cs42448_1.enable() && cs42448_1.volume(0.8)) {
    Serial.println("configured CS42448_1");
  } else {
    Serial.println("failed to config CS42448_1");
  }
 
    //TCA9548A(0);                                                                                                                       
  if (cs42448_2.enable() && cs42448_2.volume(0.8)) {
    Serial.println("configured CS42448_2");
  } else {
    Serial.println("failed to config CS42448_2");
  }

cs42448_1.inputLevel(0.5f);//very sensible !!
cs42448_2.inputLevel(0.5f);


//myNewPatchCord.disconnect();
myNewPatchCord.connect(sine2,0,tdmB2,0);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Same error as above,i certainly missed something but it's the first time i use connection stuff
 
I noticed it was not possible to patch directly an input to an output with this method,no sound and the display return itself to the principal menu
after a couple of seconds
for example:

Code:
//before in the pre-setup:
AudioConnection          patchInput1L;
AudioConnection          patchInput1R;

//then in the setup:
patchInput1L.disconnect();
patchInput1R.disconnect();
patchInput1L.connect(tdm1,0,tdm2,4);
patchInput1R.connect(tdm1,2,tdm2,6);

Maybe i'm wrong but do i need to use a "between filter" like an amp to realize the connection?
 
It is definitely possible to patch an input straight to an output. Your code fragment should work, so the code you haven't posted must be causing the issue. Please ... if you have an issue, spend the time to craft and post a minimal complete sketch which anyone can at least compile in the Arduino IDE, even if they don't have the hardware to run it. You may find the problem in the course of doing so, but if not it will surely speed up getting a useful response (though maybe not in 24 minutes every time :giggle: )
 
Well,here is a simple sketch:

Code:
                                                                                                                                                                                                                     //0x38  (RA8875,FT6206,MAX98390)

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <RA8875.h>
#include <Adafruit_FT6206.h>


// The FT6206 uses hardware I2C1 (SCL 17/SDA 16)
Adafruit_FT6206 ts = Adafruit_FT6206();


#define RA8875_CS         36
#define RA8875_RESET      255                 
#define RA8875_INT        41//22


RA8875 tft = RA8875(RA8875_CS, RA8875_RESET);


// GUItool: begin automatically generated code
AudioInputTDM            tdm1;           //xy=55,698
AudioInputTDMB           tdmB1;          //xy=57,989
AudioSynthWaveformSine   sine1;          //xy=395,7421
AudioSynthWaveformSine   sine2;          //xy=489,1141
AudioOutputTDM           tdm2;           //xy=984,681
AudioOutputTDMB          tdmB2;          //xy=989,991

AudioConnection          patchCord1;
AudioConnection          patchCord2;

AudioControlCS42448      cs42448_2;      //xy=487,1197
AudioControlCS42448      cs42448_1;      //xy=559,799
// GUItool: end automatically generated code


void setup() {
 
  Serial.begin(38400);
 

  Wire.begin();
 
cs42448_1.setAddress(0x48);
cs42448_1.enable();
 
cs42448_2.setAddress(0x4A);
cs42448_2.enable();


  //  begin display: Choose from: RA8875_480x272, RA8875_800x480, RA8875_800x480ALT, Adafruit_480x272, Adafruit_800x480
  tft.begin(RA8875_800x480);
    

   if (!ts.begin(40)) {
    Serial.println("Unable to start touchscreen.");
  }
  else {
    Serial.println("Touchscreen started.");
  }
 
   tft.setRotation(0);
  tft.fillWindow(RA8875_BLACK);//fill window black
  //tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
  tft.setFontScale(1); 
      
 
  AudioMemory(1000);

  if (cs42448_1.enable() && cs42448_1.volume(0.8)) {
    Serial.println("configured CS42448_1");
  } else {
    Serial.println("failed to config CS42448_1");
  }
 
                                                                                                                          
  if (cs42448_2.enable() && cs42448_2.volume(0.8)) {
    Serial.println("configured CS42448_2");
  } else {
    Serial.println("failed to config CS42448_2");
  }

cs42448_1.inputLevel(0.5f);
cs42448_2.inputLevel(0.5f);

 
   sine1.frequency(1200);
   sine1.amplitude(0.3);

   sine2.frequency(1200);
   sine2.amplitude(0.3);        
 
 patchCord1.disconnect();
 patchCord1.connect(tdm1, 0, tdm2, 0);
      //patchCord1.connect(sine1, 0, tdm2, 0);
 patchCord2.disconnect();
 patchCord2.connect(tdm1, 2, tdm2, 2);
      //patchCord2.connect(sine1, 0, tdm2, 2);          

  }

void loop(){
 
}

I can affirm the sine generator works,but not the input,no sound and with some clicks at some time
 
I've just pushed a bug-fix to the multi-TDM branch. Please give this a go and see if it's working for you. I adapted my test setup from your posted code - the display stuff seemed irrelevant and I don't have one anyway, and I re-assigned some inputs and outputs to fit better with my hardware setup. Note that I've physically wired tdm2 0+2 to tdmB1 0+2. Adapted code is:
C++:
#include <Audio.h>

// GUItool: begin automatically generated code
AudioInputTDM            tdm1;           //xy=55,698
AudioInputTDMB           tdmB1;          //xy=57,989
AudioSynthWaveformSine   sine1;          //xy=395,7421
AudioSynthWaveformSine   sine2;          //xy=489,1141
AudioOutputTDM           tdm2;           //xy=984,681
AudioOutputTDMB          tdmB2;          //xy=989,991

AudioConnection          patchCord1;
AudioConnection          patchCord2;
AudioConnection          patchCord3;
AudioConnection          patchCord4;
AudioConnection          patchCord5;
AudioConnection          patchCord6;

AudioControlCS42448      cs42448_2;      //xy=487,1197
AudioControlCS42448      cs42448_1;      //xy=559,799
// GUItool: end automatically generated code


void setup()
{
 
  Serial.begin(38400);
 
  cs42448_1.setAddress(0x48);
  cs42448_1.enable();
  
  cs42448_2.setAddress(0x4A);
  cs42448_2.enable();
 
  AudioMemory(100); // this is enough: max is 896

  if (cs42448_1.enable() && cs42448_1.volume(0.8))
    Serial.println("configured CS42448_1");
  else
    Serial.println("failed to config CS42448_1");
 
                                                                                                                          
  if (cs42448_2.enable() && cs42448_2.volume(0.8))
    Serial.println("configured CS42448_2");
  else
    Serial.println("failed to config CS42448_2");

  cs42448_1.inputLevel(0.5f);
  cs42448_2.inputLevel(0.5f);

 
  sine1.frequency(800);
  sine1.amplitude(0.3);
 
  sine2.frequency(1200);
  sine2.amplitude(0.3);       
 
  //patchCord1.disconnect(); // don't need this
  patchCord1.connect(tdmB1, 0, tdmB2, 10);
  patchCord3.connect(sine1, 0, tdmB2, 2);
  patchCord5.connect(sine1, 0, tdm2, 0); // physically wired to tdmB1.0
 
  //patchCord2.disconnect(); // don't need this
  patchCord2.connect(tdmB1, 2, tdmB2, 14);
  patchCord4.connect(sine2, 0, tdmB2, 6);         
  patchCord6.connect(sine2, 0, tdm2, 2); // physically wired to tdmB1.2         
}

void loop()
{}
 
@h4yn0nnym0u5e,hello good news,all your patching are working now :)
but do we still need to use disconnect before connect to another new choice,i said that because your examples
are at startup,and so after?....
 
Yes, if you want to form a different connection using patchcord that’s already connected, you first have to disconnect it. As you’d created them in a disconnected state, there was no need to disconnect again. But it’s not an error and won’t cause a problem if you do so.
 
Back
Top