Csound api and CompileOrc()

I’m developing a plugin for Bidule using their sdk that basically is integrating an instance of Csound api, and will be able to compile any valid orc code that is entered into a text box in the gui.

What’s the standard practice to recompiling orc code using the cpp api, when an instance of Csound is already running?

I have set up the initial orc (and sco) code in the init() method in the plugin, that works fine, but when I recompile some (valid) orc code that replaces the same instr 1, I get some nasty, loud glitchy output and the whole instance basically crashes and eventually brings down Bidule.

Is there a special order to doing this within C++?

I’m not using compilestr or any of the Csound opcodes for this - only within C++

This is the general idea:

// gets called when a button is pressed.
CsoundTest::recompileOrc() {
 
    if (_recompileTrig == 1) {
        // csound->Reset(); <- does this need to be called at all?
        _csCompileResult = csound->CompileOrc(templateOrc.c_str());

        if (_csCompileResult == 0) {
            csound->ReadScore("i1 0 4 220");  // <- restarts a new instance of instr 1
        }
        _recompileTrig = 0;
    }
}

thanks!

I’ve found the cleanest way to do this is to destroy Csound and create a new instance of it.

csound = nullptr;
csound = std::make_unique<Csound> ();

It’s not pretty, but do the job. I had issues with some resources not being freed when calling csound->Reset(), which would in turn lead to other issues.

Ah right, brute force! cool will try that.

One thing I did think was, when I recompiled, the orc code was including

"sr=48000\n\
    ksmps=64\n\
    nchnls=2\n\
    0dbfs=1\n\
    chn_k \"phsRate\", 1\n\
    chn_k \"pitch\", 1\n\
    chn_k \"amp\", 1\n\";

each time, rather than just the instr code.

Could that muddle things up?

Thanks

Probably could is that’s what is actually being passed to compileOrc. Sorry, I only noticed this message now. For some reason I don’t get notified of reponses…

Haha is ok! i think there is some notification button somewhere that gets auto-checked when you are the author…i dunno. Notifications sometimes do, sometimes don’t work…

In the end I decided for simplicity to pass the path to a .csd, and compile that instead. The code editing ui facilities within Bidule is somewhat lacking, so prob best to leave that to a dedicated editor for now. But I may circle back to this when I have it all working how I want, but I suspect that is a can of worms.

Cheers

Quick one @rory

By doing csound = nullptr; does that leave any memory leaks/un-deleted resources lying around?

It shouldn’t, as this will call the Csound destructor, which in turn should destroy all resources.

if it’s in a smart pointer, right?

also does there need to be a period of time between destroying the instance of Csound, and then recreating it? probably something in my code, but I get a message from Csound saying: Csound is already started, call csoundReset() before starting again. Makes me think is “it” being called twice… it being csound->Start()?