[SOLVED ]How to allocate dynamically AudioInputSource for a mixer

I set the flag to true, tested right now: still the same error :confounded:

Then you are now leaking AudioFormatReaderSource?

Add a class member std::unique_ptr and create your source into there.
The replace transport.setSource(source.release()); with transport.setSource(source.get()); since the transport does not take the ownership of the source.

1 Like

Ok now the error changed with this old friend of us:

    if (oldMasterSource != nullptr)
        oldMasterSource->releaseResources();

Now the code is like:

class mixerChannel : public ProcessorBase
{
public:
    mixerChannel()
    {
        addParameter(volume = new juce::AudioParameterFloat("volume", "Volume", 0.0, 1.0, 0.0));
        addParameter(reproductionState = new juce::AudioParameterChoice("state", "State", states, 0));

        juce::AudioFormatManager formatManager;
        formatManager.registerBasicFormats();

        juce::File audioPath = "C:/Users/renda/Documents/provaJuce/mixerProject/Source/audio";
        juce::Array<juce::File> listOfFiles = audioPath.findChildFiles(2, false);
        int randomInt = juce::Random::getSystemRandom().nextInt(listOfFiles.size() - 1);
        std::unique_ptr<juce::AudioFormatReader> reader(formatManager.createReaderFor(listOfFiles[randomInt]));

        if (reader)
        {   
            sourcePtr.reset(new juce::AudioFormatReaderSource(reader.release(), true));
            sourcePtr.get()->setLooping(true);
            transport.setSource(sourcePtr.get()),
            transport.start();
        }
    }
//================================================================//
    std::unique_ptr<juce::AudioFormatReaderSource> sourcePtr;
}

So far so good, now add the destructor back and remove the source from transport. Either with transport.setSource(nullptr) or something else thats suggested by the docks, if nullptr is not allowed.

You can use the operator overloading from unique ptr
sourcePtr->setLooping(true);
It’s semantically the same

1 Like

Ok, after the modification of the destructor method, I obtained the following error

*** Leaked objects detected: 1 instance(s) of class WavAudioFormatReader

I tried to modify the code by implementing as many unique_ptr as possible:

class mixerChannel : public ProcessorBase
{
public:
    mixerChannel()
    {
        addParameter(volume = new juce::AudioParameterFloat("volume", "Volume", 0.0, 1.0, 0.0));
        addParameter(reproductionState = new juce::AudioParameterChoice("state", "State", states, 0));

        formatReaderPtr.reset(new juce::AudioFormatManager());
        formatReaderPtr->registerBasicFormats();

        juce::File audioPath = "C:/Users/renda/Documents/provaJuce/mixerProject/Source/audio";
        juce::Array<juce::File> listOfFiles = audioPath.findChildFiles(2, false);
        int randomInt = juce::Random::getSystemRandom().nextInt(listOfFiles.size() - 1);
        readerPtr.reset(formatReaderPtr->createReaderFor(listOfFiles[randomInt]));

        if (readerPtr)
        {   
            sourcePtr.reset(new juce::AudioFormatReaderSource(readerPtr.release(), false));
            sourcePtr->setLooping(true);

            transportPtr.reset(new juce::AudioTransportSource());
            transportPtr->setSource(sourcePtr.get());
            transportPtr->start();
        }
    }

You changed it back to false :open_mouth: :smiley:

1 Like

Ooooouch, sorry! I turned the flag to true, now the program exits with no errors!!!

I implemented this logic even with MIDI classes and other nodes in the AudioProcessorGraph and everything seems perfect!

Thank you so much Rincewind, you made my day

1 Like