Why can't I intstantiate a new AudioEffectWaveshaper

I'm trying to code an effect chain (programmable by user) so I need to be able to instantiate different effects and connections. For now I've been able to code a delay and a reverb effect, but when I got to add a WaveShaper I'm running into a runtime error.

The thing is, if I use it like as follows it works good:

Code:
AudioEffectWaveshaper waveshaper;
EffectWaveshaper::EffectWaveshaper() {
    // ... 

    this->effectLeft = &waveshaper;

    // ...
}


But if I instntiate it using new, the program breaks:
Code:
EffectWaveshaper::EffectWaveshaper() {
    // ... 

    this->effectLeft = new AudioEffectWaveshaper();

    // ...
}


Any ideas?

Thanks :)
 
Yup. The constructor for the object is badly written, and fails to initialise the waveshape pointer to a NULL value. If the object is statically declared then it's guaranteed to be initialised to zero, but using new just allocates a chunk of heap, possibly full of junk, so on the first updated the junk pointer is used with less-than-hilarious consequences.

You could edit the class definition in AudioEffectWaveshaper.h thus:
Code:
  public:
    AudioEffectWaveshaper(void): AudioStream(1, inputQueueArray)[COLOR="#FF0000"], waveshape(nullptr)[/COLOR] {}
    ~AudioEffectWaveshaper();

Note that the audio library as shipped with current Teensyduino does not support deleting any audio object.
 
Ok! Thank you for your response, I was going crazy looking at my code ��

Why Teensy doesn't support deleting audio objects? Is it just because its not coded or is there something else going on?
 
I would like to help with that, but I've tried before to understand the Audio library, but I don't even know where to start.
The truth is that I've been using it a lot, and someday I would like to deep into it to understand it more. If you have any link where I can read and help introduce me into it, I would really appreciate it, and maybe some day I can contribute
 
Indeed, the original design goal was for all instances to be static. The main issue with deleting instances is the input queue can "own" audio blocks at the end of an update, which then get used on the next update. Hardware input and output instances also have internal ownership of blocks. So do some effects, like delay and FFT.

I must admit, several times I've merged people's contributions to the audio library without as much review as I really should have done. Waveshaper is one of those cases. More recently, I've been waiting to merge most contributions, because I just don't have time to properly review while PJRC is swamped with a lot of urgent business matters (which has been pretty much non-stop since the pandemic followed by component shortages).
 
The best introduction is probably at https://www.pjrc.com/teensy/td_libs_Audio.html, but then it’s mostly a case of looking at the existing objects, figuring out how a simple one works, and having a go at modifying it or whatever.

My dynamic mods have been stable for a while: I think a few people have made use of them, and I’ve not got a lot of bug reports so hopefully they’re pretty much OK. I tried to make sure all the “internal ownership” issues got addressed, as mentioned by Paul, though I’d never assert it’s bug-free! And of course the waveshaper bug only shows up when dynamically created, which hasn’t historically been a thing, so it’s easy to miss.

The beauty of the open source model is that we can fix these things … and the frustration is that it can then take a while for the fixes to get merged … because of urgent business matters, and making a living so Paul can eat … who knew?! :eek:
 
Back
Top