Compiler Error - Own Audio Library class

Matthias

New member
Hey,

I am trying to program my own class for the audio library to practice some basics of realtime audio synthesis. For this I had a look at the guidelines for creating your own classes on the Teensy website and used the object template displayed there (just changing the name of the class to "Synth"):

Code:
  #include "core_pins.h"
  #include "AudioStream.h"
  #include "Arduino.h"

class Synth : public AudioStream
{
public:
        Synth() : AudioStream(1, inputQueueArray);
        virtual void update(void);
private:
        audio_block_t *inputQueueArray[1];
};

When I tried to compile the code I unfortunately got several compiler errors. I got rid of the "F_CPU_ACTUAL" was not declared in this scope by browsing the forum and including "core_pins.h" as described in this thread.
However I'm still facing one compiler error I just can't get behind:

[{
"owner": "cpp",
"severity": 8,
"message": "expected '{' at end of input",
"startLineNumber": 8,
"startColumn": 49,
"endLineNumber": 8,
"endColumn": 49
}]

Has anyone an idea on how to solve this? Appreciate your help! :)

Cheers, Matthias
 
Opps, well that's embarrassing! That web page has shown slightly wrong syntax all these years. I've updated it just now.

In C++ the init list syntax can only be used where the constructor is defined (actual code), not where it's merely declared. So if the class is declared in a .h file and the constructor code is not inline in the class declaration, then the initialization list would need to be in the definition with the actual code (presumably the .cpp file).

Normally with the audio library, the constructors for non-hardware classes are both declared+defined with inline code, because they usually don't do much other than initialize member variables. So normally it would look like this in the .h file:

Code:
#include <Arduino.h>
#include <AudioStream.h>

class AudioEffectTemplate : public AudioStream
{
public:
        AudioEffectTemplate() : AudioStream(1, inputQueueArray) {
          // any extra initialization
        }
        virtual void update(void);
private:
        audio_block_t *inputQueueArray[1];
};

But if you wanted to only declare stuff in the .h file (no actual code) and define (actual code) it in the .cpp file, the you would use this in the .h file:

Code:
class AudioEffectTemplate : public AudioStream
{
public:
        AudioEffectTemplate();
        virtual void update(void);
private:
        audio_block_t *inputQueueArray[1];
};

and of course you would put the constructor's actual code (even if only an empty function) in the .cpp file, like this:

Code:
AudioEffectTemplate::AudioEffectTemplate() : AudioStream(1, inputQueueArray)
{
        // any extra initialization (likely more substantial if put into the .cpp file)
}
 
oooh yeah! Thank you very much for the fast reply, now everything works just fine!
Yes, my constructor code is not inline in the class declaration but in the .cpp file just as you assumed. I already thought that I did something wrong with the declaration and definition of the constructor, but I didn't come up with the solution on my own so your explanation is very much appreciated!
 
Back
Top