Csound Csound-dev Csound-tekno Search About

[Csnd] Check existence of instruments

Date2021-03-02 09:21
FromJana Hübenthal
Subject[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
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2021-03-02 09:56
FromRory Walsh
SubjectRe: [Csnd] Check existence of instruments
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>

 

On Tue, 2 Mar 2021 at 09:22, Jana Hübenthal <jana@ebene11.de> wrote:
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
        https://github.com/csound/csound/issues
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 https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2021-03-02 10:09
FromJana Hübenthal
SubjectRe: [Csnd] Check existence of instruments
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  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.
> 
> 
> 
> 
> 
> -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
> 
> 
> 
> 
> 
> 
> On Tue, 2 Mar 2021 at 09:22, Jana Hübenthal  wrote:
> > 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
> >  https://github.com/csound/csound/issues
> >  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 https://github.com/csound/csound/issues 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
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2021-03-03 01:12
FromRichard Knight
SubjectRe: [Csnd] Check existence of instruments
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
(http://csound.1045644.n5.nabble.com/Csnd-dev-API-function-to-find-an-instrument-instance-td5767985.html)
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

On 2021-03-02 10:09, Jana Hübenthal wrote:
> 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  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.
>> 
>> 
>> 
>> 
>> 
>> -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
>> 
>> 
>> 
>> 
>> 
>> 
>> On Tue, 2 Mar 2021 at 09:22, Jana Hübenthal  wrote:
>> > 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
>> >  https://github.com/csound/csound/issues
>> >  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 
>> https://github.com/csound/csound/issues 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
>         https://github.com/csound/csound/issues
> 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
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here