[Csnd] Odd string problem

Hi,
I am trying to parse comma separated numbers in a string (as to send them one by one to another process). Something odd is happening so that my instrument works correctly the first time it starts, but then when it is started again the first item of the string is not converted to a number.

It might look like some k-rate string opcodes somehow retain their state from previous initializations (??)

The csd below shows the problem. Running instr 1 should print the numbers in the string one by one. This happens correctly the first time the instrument runs, but it only prints 2 and 3 the second time the instrument runs.

As a test I have recreated the same procedure with i-time string opcodes in instr 2. This works every time. Sadly, I do need to make it work at k-rate…

All best
Øyvind

-n

instr 1
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
kcomma strindexk Snumbers, “,”
while kcomma > -1 do
Sval strsubk Snumbers, 0, kcomma
kval strtodk Sval
printk2 kval, 10
Snumbers strsubk Snumbers, kcomma+1, -1
kcomma strindexk Snumbers, “,”
od
endin

instr 2
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
icomma strindex Snumbers, “,”
while icomma > -1 do
Sval strsub Snumbers, 0, icomma
ival strtod Sval
print ival
Snumbers strsub Snumbers, icomma+1, -1
icomma strindex Snumbers, “,”
od
endin

i1 0 .1 i1 1 .1

;i2 2 .1
;i2 3 .1

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

Browsing through the list of opcodes (csound -z), I found string2array that seems very useful in this context.
I can’t find a manual page for it. Can I expect it to still be included in future versions of Csound?

ons. 15. des. 2021 kl. 11:14 skrev Oeyvind Brandtsegg <obrandts@gmail.com>:

Hi,
I am trying to parse comma separated numbers in a string (as to send them one by one to another process). Something odd is happening so that my instrument works correctly the first time it starts, but then when it is started again the first item of the string is not converted to a number.

It might look like some k-rate string opcodes somehow retain their state from previous initializations (??)

The csd below shows the problem. Running instr 1 should print the numbers in the string one by one. This happens correctly the first time the instrument runs, but it only prints 2 and 3 the second time the instrument runs.

As a test I have recreated the same procedure with i-time string opcodes in instr 2. This works every time. Sadly, I do need to make it work at k-rate…

All best
Øyvind

-n

instr 1
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
kcomma strindexk Snumbers, “,”
while kcomma > -1 do
Sval strsubk Snumbers, 0, kcomma
kval strtodk Sval
printk2 kval, 10
Snumbers strsubk Snumbers, kcomma+1, -1
kcomma strindexk Snumbers, “,”
od
endin

instr 2
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
icomma strindex Snumbers, “,”
while icomma > -1 do
Sval strsub Snumbers, 0, icomma
ival strtod Sval
print ival
Snumbers strsub Snumbers, icomma+1, -1
icomma strindex Snumbers, “,”
od
endin

i1 0 .1 i1 1 .1

;i2 2 .1
;i2 3 .1

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

Found it in “what’s new 6.16”, wonderful!

ons. 15. des. 2021 kl. 14:46 skrev Oeyvind Brandtsegg <obrandts@gmail.com>:

Browsing through the list of opcodes (csound -z), I found string2array that seems very useful in this context.
I can’t find a manual page for it. Can I expect it to still be included in future versions of Csound?

ons. 15. des. 2021 kl. 11:14 skrev Oeyvind Brandtsegg <obrandts@gmail.com>:

Hi,
I am trying to parse comma separated numbers in a string (as to send them one by one to another process). Something odd is happening so that my instrument works correctly the first time it starts, but then when it is started again the first item of the string is not converted to a number.

It might look like some k-rate string opcodes somehow retain their state from previous initializations (??)

The csd below shows the problem. Running instr 1 should print the numbers in the string one by one. This happens correctly the first time the instrument runs, but it only prints 2 and 3 the second time the instrument runs.

As a test I have recreated the same procedure with i-time string opcodes in instr 2. This works every time. Sadly, I do need to make it work at k-rate…

All best
Øyvind

-n

instr 1
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
kcomma strindexk Snumbers, “,”
while kcomma > -1 do
Sval strsubk Snumbers, 0, kcomma
kval strtodk Sval
printk2 kval, 10
Snumbers strsubk Snumbers, kcomma+1, -1
kcomma strindexk Snumbers, “,”
od
endin

instr 2
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
icomma strindex Snumbers, “,”
while icomma > -1 do
Sval strsub Snumbers, 0, icomma
ival strtod Sval
print ival
Snumbers strsub Snumbers, icomma+1, -1
icomma strindex Snumbers, “,”
od
endin

i1 0 .1 i1 1 .1

;i2 2 .1
;i2 3 .1

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

hi oeyvind -

i think this is expected behaviour. i wrote about it in
https://flossmanual.csound.com/csound-language/initialization-and-performance-pass#k-values-and-initialization-in-multiple-triggered-instruments

or is this a different case?

cheers -
  joachim

Hi Joachim
I think it is a different case. The issue is that the instrument produce different output the second time it is initialized. I would not expect such behavior in any case where all variables are local to the instrument instance.

All best
Øyvind

ons. 15. des. 2021, 4:31 p.m. skrev joachim heintz <jh@joachimheintz.de>:

hi oeyvind -

i think this is expected behaviour. i wrote about it in
https://flossmanual.csound.com/csound-language/initialization-and-performance-pass#k-values-and-initialization-in-multiple-triggered-instruments

or is this a different case?

cheers -
joachim

Hi,
I am trying to parse comma separated numbers in a string (as to send
them one by one to another process). Something odd is happening so that
my instrument works correctly the first time it starts, but then when it
is started again the first item of the string is not converted to a number.

It might look like some k-rate string opcodes somehow retain their state
from previous initializations (??)

The csd below shows the problem. Running instr 1 should print the
numbers in the string one by one. This happens correctly the first time
the instrument runs, but it only prints 2 and 3 the second time the
instrument runs.

As a test I have recreated the same procedure with i-time string opcodes
in instr 2. This works every time. Sadly, I do need to make it work at
k-rate…

All best
Øyvind

-n

instr 1
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
kcomma strindexk Snumbers, “,”
while kcomma > -1 do
Sval strsubk Snumbers, 0, kcomma
kval strtodk Sval
printk2 kval, 10
Snumbers strsubk Snumbers, kcomma+1, -1
kcomma strindexk Snumbers, “,”
od
endin

instr 2
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
icomma strindex Snumbers, “,”
while icomma > -1 do
Sval strsub Snumbers, 0, icomma
ival strtod Sval
print ival
Snumbers strsub Snumbers, icomma+1, -1
icomma strindex Snumbers, “,”
od
endin

i1 0 .1 i1 1 .1

;i2 2 .1
;i2 3 .1

Csound mailing list Csound@listserv.heanet.ie
mailto:[Csound@listserv.heanet.ie](mailto:Csound@listserv.heanet.ie)
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
<https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND> Send bugs reports to
https://github.com/csound/csound/issues
<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

Oh I see now what you meant. The link sent me to the top of the page first, so I checked again scrolling down to the section about multiple passes. But is still don’t see how it applies to the code I sent. Could you be so kind to explain it in terms of my example code?

ons. 15. des. 2021, 6:09 p.m. skrev Oeyvind Brandtsegg <obrandts@gmail.com>:

Hi Joachim
I think it is a different case. The issue is that the instrument produce different output the second time it is initialized. I would not expect such behavior in any case where all variables are local to the instrument instance.

All best
Øyvind

ons. 15. des. 2021, 4:31 p.m. skrev joachim heintz <jh@joachimheintz.de>:

hi oeyvind -

i think this is expected behaviour. i wrote about it in
https://flossmanual.csound.com/csound-language/initialization-and-performance-pass#k-values-and-initialization-in-multiple-triggered-instruments

or is this a different case?

cheers -
joachim

Hi,
I am trying to parse comma separated numbers in a string (as to send
them one by one to another process). Something odd is happening so that
my instrument works correctly the first time it starts, but then when it
is started again the first item of the string is not converted to a number.

It might look like some k-rate string opcodes somehow retain their state
from previous initializations (??)

The csd below shows the problem. Running instr 1 should print the
numbers in the string one by one. This happens correctly the first time
the instrument runs, but it only prints 2 and 3 the second time the
instrument runs.

As a test I have recreated the same procedure with i-time string opcodes
in instr 2. This works every time. Sadly, I do need to make it work at
k-rate…

All best
Øyvind

-n

instr 1
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
kcomma strindexk Snumbers, “,”
while kcomma > -1 do
Sval strsubk Snumbers, 0, kcomma
kval strtodk Sval
printk2 kval, 10
Snumbers strsubk Snumbers, kcomma+1, -1
kcomma strindexk Snumbers, “,”
od
endin

instr 2
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
icomma strindex Snumbers, “,”
while icomma > -1 do
Sval strsub Snumbers, 0, icomma
ival strtod Sval
print ival
Snumbers strsub Snumbers, icomma+1, -1
icomma strindex Snumbers, “,”
od
endin

i1 0 .1 i1 1 .1

;i2 2 .1
;i2 3 .1

Csound mailing list Csound@listserv.heanet.ie
mailto:[Csound@listserv.heanet.ie](mailto:Csound@listserv.heanet.ie)
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
<https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND> Send bugs reports to
https://github.com/csound/csound/issues
<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

hi oeyvind -

you are right; this is not the same as the case which i thought it was. (i had no time to look closer this afternoon and just reacted to your desription.)

now i had a closer look and i think the problem is that in your code you mix i- and k-rate. when you go consequently to k-rate, it works for me:

instr 1
   Snumbers strcpyk "1,2,3"
   Snumbers strcatk Snumbers, ","
   kcomma strindexk Snumbers, ","
   while kcomma > -1 do
     Sval strsubk Snumbers, 0, kcomma
     kval strtodk Sval
     printk2 kval, 10
     Snumbers strsubk Snumbers, kcomma+1, -1
     kcomma strindexk Snumbers, ","
   od
   turnoff
endin

ciao -
  joachim

Hi Øyvind,

As far as I can tell, something *very* peculiar is happening!
Maybe I'm getting lost, but it looks as if there are two simultaneous
versions of Snumbers, one for i-time and one at k-time!
I aded some printf's and printf_i's to the code, and I don't
really understand the output:

instr 1
  Snumbers = "1,2,3"
  printf_i "Snumbers at init='%s'\n", 1, Snumbers
  printf "Snumbers at k-time='%s'\n", 1, Snumbers
  Snumbers strcat Snumbers, ","
  printf_i "Snumbers at init after strcat='%s'\n", 1, Snumbers
  printf "Snumbers at k-time after strcat='%s'\n", 1, Snumbers
  kcomma strindexk Snumbers, ","
  printf_i "At init: Snumbers='%s', kcomma=%d\n", 1, Snumbers, kcomma
  while kcomma > -1 do
    Sval strsubk Snumbers, 0, kcomma
    kval strtodk Sval
    printk2 kval, 10
    Snumbers strsubk Snumbers, kcomma+1, -1
    kcomma strindexk Snumbers, ","
  od
endin

which for the second run yields:

...
Snumbers at init='1,2,3'
Snumbers at init after strcat='1,2,3,'
At init: Snumbers='1,2,3,', kcomma=1
Snumbers at k-time='2,3,'
Snumbers at k-time after strcat='2,3,'
...

which looks as if Snumbers is being set correctly initially,
but *then* being reset to the value of Snumbers from the
previous run! Maybe I'm being stupid, but I can't make sense
of this!

  -- Pete --

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

Oh, good catch. Thanks for looking into the details of my code. And thanks for the workaround.
op, if I need to insert an i-rate string into this, I could make that work with
‘’’
Sinput = “1,2,3”
Snumbers strcpyk Sinput

… and so on, rest of the instr like you wrote it
‘’’

It looks like this is an oddity that could trip up many users (don’t you think?). Especially since the relation between i- and k-rate for string variables seems to work differently than one would experience with regular numeric i- and k-variables. I have posted an issue at github about it, and hope the community agrees that it would be more clear if this oddity did not occur.

Thanks and best
Øyvind

ons. 15. des. 2021 kl. 22:17 skrev joachim heintz <jh@joachimheintz.de>:

hi oeyvind -

you are right; this is not the same as the case which i thought it was.
(i had no time to look closer this afternoon and just reacted to your
desription.)

now i had a closer look and i think the problem is that in your code you
mix i- and k-rate. when you go consequently to k-rate, it works for me:

instr 1
Snumbers strcpyk “1,2,3”
Snumbers strcatk Snumbers, “,”
kcomma strindexk Snumbers, “,”
while kcomma > -1 do
Sval strsubk Snumbers, 0, kcomma
kval strtodk Sval
printk2 kval, 10
Snumbers strsubk Snumbers, kcomma+1, -1
kcomma strindexk Snumbers, “,”
od
turnoff
endin

ciao -
joachim

Oh I see now what you meant. The link sent me to the top of the page
first, so I checked again scrolling down to the section about multiple
passes. But is still don’t see how it applies to the code I sent. Could
you be so kind to explain it in terms of my example code?

ons. 15. des. 2021, 6:09 p.m. skrev Oeyvind Brandtsegg
<obrandts@gmail.com mailto:[obrandts@gmail.com](mailto:obrandts@gmail.com)>:

Hi Joachim
I think it is a different case. The issue is that the instrument
produce different output the second time it is initialized. I would
not expect such behavior in any case where all variables are local
to the instrument instance.

All best
Øyvind

ons. 15. des. 2021, 4:31 p.m. skrev joachim heintz
<jh@joachimheintz.de mailto:[jh@joachimheintz.de](mailto:jh@joachimheintz.de)>:

hi oeyvind -

i think this is expected behaviour. i wrote about it in
https://flossmanual.csound.com/csound-language/initialization-and-performance-pass#k-values-and-initialization-in-multiple-triggered-instruments
<https://flossmanual.csound.com/csound-language/initialization-and-performance-pass#k-values-and-initialization-in-multiple-triggered-instruments>

or is this a different case?

cheers -
joachim

Hi,
I am trying to parse comma separated numbers in a string (as
to send
them one by one to another process). Something odd is
happening so that
my instrument works correctly the first time it starts, but
then when it
is started again the first item of the string is not
converted to a number.

It might look like some k-rate string opcodes somehow retain
their state
from previous initializations (??)

The csd below shows the problem. Running instr 1 should print
the
numbers in the string one by one. This happens correctly the
first time
the instrument runs, but it only prints 2 and 3 the second
time the
instrument runs.

As a test I have recreated the same procedure with i-time
string opcodes
in instr 2. This works every time. Sadly, I do need to make
it work at
k-rate…

All best
Øyvind

-n

instr 1
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
kcomma strindexk Snumbers, “,”
while kcomma > -1 do
Sval strsubk Snumbers, 0, kcomma
kval strtodk Sval
printk2 kval, 10
Snumbers strsubk Snumbers, kcomma+1, -1
kcomma strindexk Snumbers, “,”
od
endin

instr 2
Snumbers = “1,2,3”
Snumbers strcat Snumbers, “,”
icomma strindex Snumbers, “,”
while icomma > -1 do
Sval strsub Snumbers, 0, icomma
ival strtod Sval
print ival
Snumbers strsub Snumbers, icomma+1, -1
icomma strindex Snumbers, “,”
od
endin

i1 0 .1 i1 1 .1

;i2 2 .1
;i2 3 .1

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

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

Csound mailing list Csound@listserv.heanet.ie
mailto:[Csound@listserv.heanet.ie](mailto:Csound@listserv.heanet.ie)
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
<https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND> Send bugs reports to
https://github.com/csound/csound/issues
<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

yes i think, too =)