[Csnd] Check existence of instruments

Hi,

currently, I'm experimenting a little bit with firing events from the computer keyboard (using the sensekey & event opcodes). As I'm using the key code / ASCII value directly as instrument number, it is possible to generate "i" events to nonexisting instruments by hitting the wrong keys (then resulting in a "Cannot Find Instrument" message).
Is there any way to check in advance whether a given instrument number is defined? a kind of "active" opcode returning -1 if the queried instrument does not exist - or something in that way.

Best,
Jana

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

I thought it might be possible using nstrstr but that gives a segfault if it doesn’t find the instrument. You could map the ascii key values to an array of valid instruments. Start by setting all array values to a default instrument that doesn’t do anything, but exists all the same. Then reassign the keys you want to valid instrument. I’ve attached a brief example. Maybe it helps.

-odac ; Initialize the global variables. ksmps = 32 nchnls = 2 0dbfs = 1

giKeys[] init 127

instr INIT_ARRAY
iIndex = 0
while iIndex < 127 do
giKeys[iIndex] = 99; 99 is the default instrument, it doesn’t do anything
iIndex += 1
od
endin

schedule(“INIT_ARRAY”, 0, 0.1)

instr 1
;overwrite key instruments
giKeys[97] = 10 ; instrument ten plays when a is pressed
giKeys[115] = 11 ; instrument ten plays when s is pressed
;etc…

kKey, kKeyDown sensekey

if changed(kKeyDown) == 1 then
if kKeyDown == 1 then
event “i”, giKeys[kKey], 0, 1
endif
endif

endin

instr 10
kEnv madsr .1, .2, .4, .5
a1 oscili kEnv, 200
outs a1, a1
endin

instr 11
kEnv madsr .1, .2, .4, .5
a1 oscili kEnv, 300
outs a1, a1
endin

instr 99
;does nothing but prevent invalid instrument messages…
endin

i1 1 z

Dear Rory,

thanks a lot. Sure, I could check/decode it on my own. But in combination with CTRL/Alt/Shift, there are many keycodes to handle, not to mention multibyte sequences (like the cursor keys), which I combine to a unique single code to make 'em usable. So I thought there would be an elegant way. Yes, there seem to be some segmentation fault issues in the instrument number/name management. Just realized that...

Best,
Jana

Rory Walsh <rorywalsh@ear.ie> hat am 02.03.2021 10:56 geschrieben:

I thought it might be possible using nstrstr but that gives a segfault if it doesn't find the instrument. You could map the ascii key values to an array of valid instruments. Start by setting all array values to a default instrument that doesn't do anything, but exists all the same. Then reassign the keys you want to valid instrument. I've attached a brief example. Maybe it helps.

<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>
; Initialize the global variables.
ksmps = 32
nchnls = 2
0dbfs = 1

giKeys[] init 127

instr INIT_ARRAY
iIndex = 0
while iIndex < 127 do
giKeys[iIndex] = 99; 99 is the default instrument, it doesn't do anything
iIndex += 1
od
endin

schedule("INIT_ARRAY", 0, 0.1)

instr 1
;overwrite key instruments
giKeys[97] = 10 ; instrument ten plays when a is pressed
giKeys[115] = 11 ; instrument ten plays when s is pressed
;etc....

kKey, kKeyDown sensekey

if changed(kKeyDown) == 1 then
if kKeyDown == 1 then
event "i", giKeys[kKey], 0, 1
endif
endif

endin

instr 10
kEnv madsr .1, .2, .4, .5
a1 oscili kEnv, 200
outs a1, a1
endin

instr 11
kEnv madsr .1, .2, .4, .5
a1 oscili kEnv, 300
outs a1, a1
endin

instr 99
;does nothing but prevent invalid instrument messages..
endin

</CsInstruments>
<CsScore>
i1 1 z
</CsScore>
</CsoundSynthesizer>

> Hi,
>
> currently, I'm experimenting a little bit with firing events from the computer keyboard (using the sensekey & event opcodes). As I'm using the key code / ASCII value directly as instrument number, it is possible to generate "i" events to nonexisting instruments by hitting the wrong keys (then resulting in a "Cannot Find Instrument" message).
> Is there any way to check in advance whether a given instrument number is defined? a kind of "active" opcode returning -1 if the queried instrument does not exist - or something in that way.
>
> Best,
> Jana
>
> Csound mailing list
> Csound@listserv.heanet.ie
> LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
> Send bugs reports to
> Issues · csound/csound · GitHub
> Discussions of bugs and features can be posted here
>
Csound mailing list Csound@listserv.heanet.ie LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE Send bugs reports to Issues · csound/csound · GitHub Discussions of bugs and features can be posted here

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

Hi
I was quite interested so had a dig and found that in 2019 a way to check for instruments existing was introduced to the API
(Csound - Dev - [Csnd-dev] API function to find an instrument instance)
though no opcodes use it that I could see.

If you're determined, I've put together a basic plugin opcode called nstrexists which returns 0/1 depending if an instrument number exists in the orchestra. It's here:
http://git.1bpm.net/csound-nstrexists/commit/

Only tested on linux though so far.

all the best
RK