[Csnd] Naive question about the API

Is it possible to set global variables from the API as an alternative to channels? And if so, how do these methods compare in terms of performance?

  • Dave

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Rationale: I just ported one of my Nebulae customer instruments to work with docB’s excellent VCV Rack Csound module. It occurred to me that the Rack module could be a great way to prototype/debug Nebulae code, because it’s difficult to do this in the Nebulae environment. The Rack modules uses channels cor controls/inputs vs Nebulae which uses globals. It wou;d be cool to be able to write the same (or more similar) code for both environments.

I think you can use csoundCompileOrc to create globals (or add instruments, gens) on the fly.
But other instruments that use those globals should be created later otherwise you will have a runtime error.

You could use either of these two functions
https://github.com/csound/csound/blob/master/include/csound.h#L2420-L2431

csoundQueryGlobalVariable and csoundQueryGlobalVariableNoCheck
I assume you can just set a new float to the pointer returned.

Thanks Hlöðver, that looks like exactly what I’d need.

Any thoughts about the performance difference of using globals vs channels, if any?

I think only Victor would know. I don’t think it matters, if it’s k-rate, I assume it will be 1 performance cycle for the new value to be picked up.

Those API functions are not used to set Csound global variables, they are used to set C variables/memory in the Csound object. Csound code does not have access to them.

As Giovanni said, the only way to set global variables is via compilation, or through Csound
events (using pfields to instruments), or try chnexport

https://csound.com/docs/manual/chnexport.html

You can also use function tables to pass data from and to a host.

Prof. Victor Lazzarini
Maynooth University
Ireland

Aha, thanks for the clarification, Victor. And thanks Giovanni!

My default for getting control signals into and out of Csound from host software is chnexport. In the Csound orchestra:

gk_Guitar_midi_dynamic_range chnexport “gk_Guitar_midi_dynamic_range”, 3 ; 127
gk_Guitar_midi_dynamic_range init 30
gk_Guitar_level chnexport “gk_Guitar_level”, 3
gk_Guitar_level init 0
gk_Guitar_space_left_to_right chnexport “gk_Guitar_space_left_to_right”, 3
gk_Guitar_space_left_to_right init .5

In the instrument, I simply refer to these global variables. All variables are prefixed with type and instrument name to avoid name collisions.

In the host software (HTML5) there is a kind of generic thing that depends on the global variable, the control channel, and the HTML input element all having exactly the same name:

('input').on('input', async function(event) { var slider_value = parseFloat(event.target.value); if (csound) { csound.SetControlChannel([event.target.id](http://event.target.id), slider_value); } var output_selector = '#' + [event.target.id](http://event.target.id) + '_output'; (output_selector).val(slider_value);
});

I haven’t tried sending control signals from Csound to the host software.

Regards,
Mike

Thanks Mike, I like that and may adapt my code to use the technique.