Audio Lib inits in other than main.cpp

Status
Not open for further replies.

Robbert

Active member
I've used the audio library before and had trouble when pasting the init code in another .hpp file, other than main.cpp

By init code I mean the code the audio library generates:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s2;           //xy=128,351
AudioSynthWaveformSine   sine1;          //xy=221,562
AudioMixer4              mixer1;         //xy=282,369
AudioSynthWaveformSineModulated sine_fm1;       //xy=409,553
AudioAnalyzePeak         peak1;          //xy=410,336
AudioEffectDelay         delay1;         //xy=603,418
AudioEffectFreeverb      freeverb1;      //xy=720,280
AudioMixer4              mixer2;         //xy=918,300
AudioOutputI2S           i2s1;           //xy=1102,294
AudioOutputUSB           usb1;           //xy=1108,353
AudioConnection          patchCord1(i2s2, 0, mixer1, 0);
AudioConnection          patchCord2(i2s2, 1, mixer1, 1);
AudioConnection          patchCord3(sine1, delay1);
AudioConnection          patchCord4(mixer1, peak1);
AudioConnection          patchCord5(sine_fm1, delay1);
AudioConnection          patchCord6(delay1, 0, freeverb1, 0);
AudioConnection          patchCord7(delay1, 0, mixer2, 1);
AudioConnection          patchCord8(freeverb1, 0, mixer2, 0);
AudioConnection          patchCord9(mixer2, 0, i2s1, 0);
AudioConnection          patchCord10(mixer2, 0, i2s1, 1);
AudioConnection          patchCord11(mixer2, 0, usb1, 0);
AudioConnection          patchCord12(mixer2, 0, usb1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=136,157
// GUItool: end automatically generated code

I'd rather put it in a separate audioController.hpp file and use it in audioController.cpp as to organise my project well.
Last time I've fixed this by passing pointers of all audio instances into the constructor of the audioController.
Now before starting to work on the audio part of this rather large project I wonder if anyone else has come across this and maybe has a solution for it.
Thanks in advance :)
 
If audioController.h has the code that you posted, you should be able to include it just fine from main.h or main.cpp for example.

You shouldn't need to make pointers to something just because it's been moved into a different header file.
 
@wcalvert that's what I would expect but it doesn't seem to work.
Here's my code;

audioController.hpp
Code:
#pragma once

#include <Arduino.h>

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

class AudioController {
    public:
        AudioController();
        void testSignal();
    private:
        // GUItool: begin automatically generated code
        AudioSynthWaveformSine   sine1;          //xy=264,299
        AudioMixer4              mixer1;         //xy=469,316
        AudioOutputI2S           i2s1;           //xy=643,316
        AudioControlSGTL5000     sgtl5000_1;     //xy=136,157
        // GUItool: end automatically generated code
};

audioController.cpp
Code:
#include "audioController.hpp"

AudioController::AudioController() {
    AudioConnection          patchCord1(sine1, 0, mixer1, 0);
    AudioConnection          patchCord2(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord3(mixer1, 0, i2s1, 1);
    
    AudioMemory(10);
    sgtl5000_1.enable();
    sgtl5000_1.volume(0.75);
    sine1.amplitude(1.0);
    sine1.frequency(500);
    mixer1.gain(0,1.0);
}

in main.cpp this class is included as follows;
Code:
#include "audioController.hpp"
AudioController audioController;

This is just some audio code to test and not the final code. I've had it run when everything is in main.cpp, so it should just give a test-tone sine.
 
FIXED;

So I've just fixed it by just putting everything inside audioController.cpp as follows;

Code:
#include "audioController.hpp"

    // GUItool: begin automatically generated code     
    AudioSynthWaveformSine   sine1;          //xy=264,299
    AudioMixer4              mixer1;         //xy=469,316
    AudioOutputI2S           i2s1;           //xy=643,316
    AudioControlSGTL5000     sgtl5000_1;     //xy=136,157
    AudioConnection          patchCord1(sine1, 0, mixer1, 0);
    AudioConnection          patchCord2(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord3(mixer1, 0, i2s1, 1);
    // GUItool: end automatically generated code

AudioController::AudioController() {
    AudioMemory(10);
    sgtl5000_1.enable();
    sgtl5000_1.volume(0.75);
    sine1.amplitude(1.0);
    sine1.frequency(500);
    mixer1.gain(0,1.0);
}

It seems like odd practice to me to init those objects in the .cpp file and outside any functions. So if anyone has something wise to say about that please explain :D
 
One minor suggestion.. when asking for help, please tell us if you're having a compile time problem (and if so, include the error message) or having a runtime problem (and be really specific about the problem).

The way you posted it before, the patchCords are going out of scope since they are declared in the constructor. You need to move the patchCords to be private members so they stick around.
 
Status
Not open for further replies.
Back
Top