Using Audio objects within class

Status
Not open for further replies.

Kees

New member
Hi All,


I am playing around with declaring audio objects in a seperate class. In my case, i'm attempting to combine two oscillators in a single voice class that connects to the mixer and audioOutput in the main sketch.

Results have been mixed; i am able to connect a single Audio object from a class to an object in the main sketch like so;
View attachment objecttest.inoView attachment Voice.h


However, when i want to add something with an audioConnection in my class, the Teensy stalls right after being reprogrammed.

View attachment objecttest.inoView attachment Voice.h

Am i trying something impossible here, or simply overlooking a neat solution?:)

It would be very neat for my synth design, if multiple voices could be declared as classes instead of in the main sketch.

I hope i have made my problem clear, and would like to thank in advance for all the valuable information that can be found in this place!

Best regards,

Kees
 
I don't think Audio objects can be instantiated at run time. You need to design your audio "pipeline" (objects and connections) and declare all the objects/connections when you initialize. If you declare more later, they have no connection to the output.

However, you can have wrapper classes that accept a pointer to an audio object when they are instantiated, and have the wrapper objects control that audio object via the pointer. So let's say you set up 4 ToneSweep objects, and output into a mixer. That would allow you to instantiate up to 4 different Voice objects, each controlling one of the ToneSweep objects. Does that help?

Here is an example of what i did. I wanted a generic Effect class so i could have a sequencer control multiple effects. The sequencer interacts with everything as the parent class Effect, but the different effect types have additional methods for setup, etc. Below are the base Effect class and the EffectDrum subclass. Note all of these use the audio pointer.

Code:
//Base Effect Class
class Effect
{
  public:
    Effect(AudioStream *streamP);
    virtual void soundOn();
    virtual void soundOff();
    void setId(int id);
    int effectType;
  protected:
    int effectId;
  private:
    AudioStream *aStreamPointer;
};

class EffectDrum: public Effect
{
  public:
    EffectDrum(AudioSynthSimpleDrum *drumP);
    void setParams(float freq, int duration, float mod);
    void setFreq(float freq);
    void setDuration(int duration);
    virtual void soundOn();
    // no override for soundOff because drum has a duration
  private:
    AudioSynthSimpleDrum *drumPointer;  
};

Effect::Effect(AudioStream *streamP)
{
  aStreamPointer = streamP;
}
// blank methods, override these
void Effect::soundOn(){ 
  //Serial.println("Effect.soundOn");
}
void Effect::soundOff(){ 
  //Serial.println("Effect.soundOff");
}
void Effect::setId(int id) { effectId = id; }

// Drum
EffectDrum::EffectDrum(AudioSynthSimpleDrum *drumP) 
  : Effect(drumP)
{
  drumPointer = drumP;
  effectType = EFFECT_DRUM;
}
void EffectDrum::setParams(float freq, int duration, float mod)
{
  (*drumPointer).frequency(freq);
  (*drumPointer).length(duration);
  (*drumPointer).pitchMod(mod);
}
void EffectDrum::setFreq(float freq)
{
  (*drumPointer).frequency(freq);
}
void EffectDrum::setDuration(int duration)
{
  (*drumPointer).length(duration);
}
void EffectDrum::soundOn()
{
  (*drumPointer).noteOn();
  //Serial.print(" *");
  //SSerial.print(effectId);
}

then i initialize them like:
Code:
// from audio design tool:
..(other objects/connections)
AudioSynthSimpleDrum     drum1;          //xy=199,324
..(more objects/connections)
...
EffectDrum eDrum1(&drum1);
...
eDrum1.setParams(50, 150, 0.5);

and trigger by calling the soundOn() method
 
Status
Not open for further replies.
Back
Top