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