Cabbage Logo
Back to Cabbage Site

Retrieve sr for opcode

Hi,
I’m currently working on a custom opcode and I need to retireve the sample rate for the processing section of the opcode.
I’ve been trying to use
p->samplerate = csoundGetSr(csound);
To store the value of sr into my opcodes data structure and I’ve included the csdl.h, csound.h, and csoundCore.h headers and I keep getting an error saying the function call is an implicit function call. Am I calling the incorrect headers or is there a better way to retrieve the sr value?

By sr I mean in a csound file in the instruments section where you’d declare sr = 44100 for example.

Any help would be greatly appreciated!

Are you using the opcode_sdk? Is this relevant?

Btw, I don’t think you should need to include csoundCore.h or csound.h at all.

Okay so how would I implement that? Would I declare it in the opcodes data structure first:
typedef structure _newopc{
MYFLT *sr;
} newopc;

and then again at initialization:

int new_opc_init(CSOUND *csound, newopc *p){
p->sr = csound->GetSr(csound);
return OK;
}

and then declare a new variable and assign it to the dereferenced sr:

int new_opc_process_audio(CSOUND *csound, newopc *p){
double sr = *p->sr;
return OK;
}

Or would I just declare it once at initialization as its written in your reply and then dereference it into a new variable within the processing section like:

nt new_opc_init(CSOUND *csound, newopc *p){
MYFLT sr = csound->GetSr(csound);
return OK;
}

int new_opc_process_audio(CSOUND *csound, newopc *p){
double sr = *p->sr;
return OK;
}

I’d say that’s up to you, but I can’t imagine the call to GetSr() is very expensive. If you don’t want to create a new SR member in your opcode struct you can probably just call GetSr() in your processing function.

Okay, I decided to just call GetSr() in the processing function.
My opcode was running fine beforehand now it crasheds csoundqt when I try to run it.

I think it’s because I have the output counter set to the sample rate instead of the ksmps variable:

for (int d = 0; d < samplerate; d++) {
//lots of maths and stuff
out[d]= ampcompensation*temp;
}
return OK;
}
and instead maybe I need to nest some for conditions somehow so that it’s performing the samplerate dependent calculations but sending those to out in batches of ksmps amount??

I am so confusion

I just swapped out sample rate for ksmps in my output counter and it indeed does work without crashing csoundqt now. It makes noise, just not to frequency input the way I intended, but I started another post to address those issues :skull: