Csound Csound-dev Csound-tekno Search About

[Csnd] python l to array or ftable

Date2020-04-08 19:01
FromStefan Thomas
Subject[Csnd] python l to array or ftable
Dear community,
is it possible to transfer the content of a list that I've produced with python, to an ftable or an array in csound?
For example
aList = list(range(1,6))
I know, off course, that I could do this easily with csound itself, but I feel more comfortable with the python language and I would like to transfer more complicated things from python to csound.
Thanks for Your help.
Stefan
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

Date2020-04-08 19:18
FromRichard Knight
SubjectRe: [Csnd] python l to array or ftable

hi

Unfortunately the python opcodes that return data to Csound (pyeval) appear to only be able to pass floats (so no array). The only way I could think of is a bit hacky but works like the following. The length of the list is obtained first and then loop through and obtain the value for the relevant index using individual pyeval statements. This could be done in a similar way at k-rate with the relevant pyeval opcodes changed, etc:

pyinit
pyruni "the_list = range(1,6)"

instr 1
    ilength pyevali "float(len(the_list))" ; has to be float or raises error
    iresult[] init ilength
    index = 0
    while (index < ilength) do
        iresult[index] = pyevali(sprintf("float(the_list[%d])", index)) ; has to be float or raises error
        index += 1
    od
    printarray iresult
endin

 

On Wed, 8 Apr 2020 20:01:48 +0200, Stefan Thomas wrote:

Dear community,
is it possible to transfer the content of a list that I've produced with python, to an ftable or an array in csound?
For example
aList = list(range(1,6))
I know, off course, that I could do this easily with csound itself, but I feel more comfortable with the python language and I would like to transfer more complicated things from python to csound.
Thanks for Your help.
Stefan
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

Date2020-04-08 19:53
Fromfra
SubjectRe: [Csnd] python l to array or ftable
If you use python as scripting language then you can use ctcsound

python binding to csound (i guess distribuited with Csound).


https://csound.com/docs/ctcsound/

https://csound.com/docs/ctcsound/ctcsound-API.html


I'm not sure if an example as you need is present but you can also look at

https://github.com/csound/csoundAPI_examples


Hope to be right and helpful :)

ciao,

francesco.

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

Date2020-04-08 20:05
Fromfra
SubjectRe: [Csnd] python l to array or ftable
and here:

https://github.com/csound/ctcsound/blob/master/cookbook/08-ctcsoundAPIExamples.ipynb

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

Date2020-04-08 20:36
FromStefan Thomas
SubjectRe: [Csnd] python l to array or ftable
Dear Richard,
thanks for Your help!
It worked perfectly for me.I will have a look for ctcsound as well.
All the best,
Stefan

Am Mi., 8. Apr. 2020 um 20:18 Uhr schrieb Richard Knight <richard@1bpm.net>:

hi

Unfortunately the python opcodes that return data to Csound (pyeval) appear to only be able to pass floats (so no array). The only way I could think of is a bit hacky but works like the following. The length of the list is obtained first and then loop through and obtain the value for the relevant index using individual pyeval statements. This could be done in a similar way at k-rate with the relevant pyeval opcodes changed, etc:

pyinit
pyruni "the_list = range(1,6)"

instr 1
    ilength pyevali "float(len(the_list))" ; has to be float or raises error
    iresult[] init ilength
    index = 0
    while (index < ilength) do
        iresult[index] = pyevali(sprintf("float(the_list[%d])", index)) ; has to be float or raises error
        index += 1
    od
    printarray iresult
endin

 

On Wed, 8 Apr 2020 20:01:48 +0200, Stefan Thomas wrote:

Dear community,
is it possible to transfer the content of a list that I've produced with python, to an ftable or an array in csound?
For example
aList = list(range(1,6))
I know, off course, that I could do this easily with csound itself, but I feel more comfortable with the python language and I would like to transfer more complicated things from python to csound.
Thanks for Your help.
Stefan
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

Date2020-04-08 21:38
FromRichard Knight
SubjectRe: [Csnd] python l to array or ftable

No problem -not sure if there are any ideal ways of non-primitive type data interchange with the API (& ctcsound) but here are two ideas:

If the list won't change you could just generate the actual csd string with it in, eg:
    the_list = range(1,6)
    csd = ".... idata[] fillarray " + str(the_list) + " ... "
and python's default string conversion puts it in [ ] so that would be quite simple.


Something else that came to mind which would be more powerful and you could use in performance time (although this example would need some tweaks depending on your situation for k-rate usage) would be to pass the array elements as p fields from ctcsound with CsoundPerformanceThread.putMessage and then have an instrument that parses an arbitrary number of p-fields, so in python you would do something like this:

pt = ctcsound.CsoundPerformanceThread( .... )

the_list = range(1,6)
pt.inputMessage("i1 0 1 " + " ".join([str(x) for x in the_list]))  # list comprehension needed to format as string

... and then have instr 1 as this or similar which uses pcount and pindex to find out how many p-fields it has been given (this will give warnings, ie 'instr 1 uses 3 p-fields but is given n':

instr 1
    index = 0
    imax = pcount()
    idata[] init imax - 3
    
    while (index + 3 < imax) do
        idata[index] = pindex(index + 4)
        index += 1
    od
    printarray idata
endin

hope that helps!

On Wed, 8 Apr 2020 21:36:16 +0200, Stefan Thomas wrote:

Dear Richard,
thanks for Your help!
It worked perfectly for me.I will have a look for ctcsound as well.
All the best,
Stefan

Am Mi., 8. Apr. 2020 um 20:18 Uhr schrieb Richard Knight <richard@1bpm.net>:

hi

Unfortunately the python opcodes that return data to Csound (pyeval) appear to only be able to pass floats (so no array). The only way I could think of is a bit hacky but works like the following. The length of the list is obtained first and then loop through and obtain the value for the relevant index using individual pyeval statements. This could be done in a similar way at k-rate with the relevant pyeval opcodes changed, etc:

pyinit
pyruni "the_list = range(1,6)"

instr 1
    ilength pyevali "float(len(the_list))" ; has to be float or raises error
    iresult[] init ilength
    index = 0
    while (index < ilength) do
        iresult[index] = pyevali(sprintf("float(the_list[%d])", index)) ; has to be float or raises error
        index += 1
    od
    printarray iresult
endin

 

On Wed, 8 Apr 2020 20:01:48 +0200, Stefan Thomas wrote:

Dear community,
is it possible to transfer the content of a list that I've produced with python, to an ftable or an array in csound?
For example
aList = list(range(1,6))
I know, off course, that I could do this easily with csound itself, but I feel more comfortable with the python language and I would like to transfer more complicated things from python to csound.
Thanks for Your help.
Stefan
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

Date2020-04-08 21:57
FromRichard Knight
SubjectRe: [Csnd] python l to array or ftable

... also I think with the api/ctcsound there is a way to recompile the orc without affecting performance, but not sure how to hand.

something else I though of, which I use in some situations is the built-in udp server - not sure if there is a way in the api to achieve the same, but if you start csound with --port it opens a udp listener to which orchestra code can be sent, eg:
    csound --port=9999 blank.csd

then you could sent an array definition to that, eg if you use this in python
    socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    the_list = range(1,6)
    target_socket.sendto("gidata[] fillarray " + str(the_list), ("127.0.0.1", 9999))

it will create the list as a global array gidata during performance.

these are just ideas, there may be a better way to achieve what you want to do, I'd be interested to hear any other approaches...

On Wed, 8 Apr 2020 21:38:01 +0100, Richard Knight wrote:

No problem -not sure if there are any ideal ways of non-primitive type data interchange with the API (& ctcsound) but here are two ideas:

If the list won't change you could just generate the actual csd string with it in, eg:
    the_list = range(1,6)
    csd = ".... idata[] fillarray " + str(the_list) + " ... "
and python's default string conversion puts it in [ ] so that would be quite simple.


Something else that came to mind which would be more powerful and you could use in performance time (although this example would need some tweaks depending on your situation for k-rate usage) would be to pass the array elements as p fields from ctcsound with CsoundPerformanceThread.putMessage and then have an instrument that parses an arbitrary number of p-fields, so in python you would do something like this:

pt = ctcsound.CsoundPerformanceThread( .... )

the_list = range(1,6)
pt.inputMessage("i1 0 1 " + " ".join([str(x) for x in the_list]))  # list comprehension needed to format as string

... and then have instr 1 as this or similar which uses pcount and pindex to find out how many p-fields it has been given (this will give warnings, ie 'instr 1 uses 3 p-fields but is given n':

instr 1
    index = 0
    imax = pcount()
    idata[] init imax - 3
    
    while (index + 3 < imax) do
        idata[index] = pindex(index + 4)
        index += 1
    od
    printarray idata
endin

hope that helps!

On Wed, 8 Apr 2020 21:36:16 +0200, Stefan Thomas wrote:

Dear Richard,
thanks for Your help!
It worked perfectly for me.I will have a look for ctcsound as well.
All the best,
Stefan

Am Mi., 8. Apr. 2020 um 20:18 Uhr schrieb Richard Knight <richard@1bpm.net>:

hi

Unfortunately the python opcodes that return data to Csound (pyeval) appear to only be able to pass floats (so no array). The only way I could think of is a bit hacky but works like the following. The length of the list is obtained first and then loop through and obtain the value for the relevant index using individual pyeval statements. This could be done in a similar way at k-rate with the relevant pyeval opcodes changed, etc:

pyinit
pyruni "the_list = range(1,6)"

instr 1
    ilength pyevali "float(len(the_list))" ; has to be float or raises error
    iresult[] init ilength
    index = 0
    while (index < ilength) do
        iresult[index] = pyevali(sprintf("float(the_list[%d])", index)) ; has to be float or raises error
        index += 1
    od
    printarray iresult
endin

 

On Wed, 8 Apr 2020 20:01:48 +0200, Stefan Thomas wrote:

Dear community,
is it possible to transfer the content of a list that I've produced with python, to an ftable or an array in csound?
For example
aList = list(range(1,6))
I know, off course, that I could do this easily with csound itself, but I feel more comfortable with the python language and I would like to transfer more complicated things from python to csound.
Thanks for Your help.
Stefan
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


Date2020-04-08 22:00
FromRichard Knight
SubjectRe: [Csnd] python l to array or ftable

ah sorry when I have used str(the_list) that won't actually work, I forgot fillarray doesn't take the surrounding square brackets so you'd have to " ".join([str(x) for x in the_list]) or similar.

On Wed, 8 Apr 2020 21:57:49 +0100, Richard Knight wrote:

... also I think with the api/ctcsound there is a way to recompile the orc without affecting performance, but not sure how to hand.

something else I though of, which I use in some situations is the built-in udp server - not sure if there is a way in the api to achieve the same, but if you start csound with --port it opens a udp listener to which orchestra code can be sent, eg:
    csound --port=9999 blank.csd

then you could sent an array definition to that, eg if you use this in python
    socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    the_list = range(1,6)
    target_socket.sendto("gidata[] fillarray " + str(the_list), ("127.0.0.1", 9999))

it will create the list as a global array gidata during performance.

these are just ideas, there may be a better way to achieve what you want to do, I'd be interested to hear any other approaches...

On Wed, 8 Apr 2020 21:38:01 +0100, Richard Knight wrote:

No problem -not sure if there are any ideal ways of non-primitive type data interchange with the API (& ctcsound) but here are two ideas:

If the list won't change you could just generate the actual csd string with it in, eg:
    the_list = range(1,6)
    csd = ".... idata[] fillarray " + str(the_list) + " ... "
and python's default string conversion puts it in [ ] so that would be quite simple.


Something else that came to mind which would be more powerful and you could use in performance time (although this example would need some tweaks depending on your situation for k-rate usage) would be to pass the array elements as p fields from ctcsound with CsoundPerformanceThread.putMessage and then have an instrument that parses an arbitrary number of p-fields, so in python you would do something like this:

pt = ctcsound.CsoundPerformanceThread( .... )

the_list = range(1,6)
pt.inputMessage("i1 0 1 " + " ".join([str(x) for x in the_list]))  # list comprehension needed to format as string

... and then have instr 1 as this or similar which uses pcount and pindex to find out how many p-fields it has been given (this will give warnings, ie 'instr 1 uses 3 p-fields but is given n':

instr 1
    index = 0
    imax = pcount()
    idata[] init imax - 3
    
    while (index + 3 < imax) do
        idata[index] = pindex(index + 4)
        index += 1
    od
    printarray idata
endin

hope that helps!

On Wed, 8 Apr 2020 21:36:16 +0200, Stefan Thomas wrote:

Dear Richard,
thanks for Your help!
It worked perfectly for me.I will have a look for ctcsound as well.
All the best,
Stefan

Am Mi., 8. Apr. 2020 um 20:18 Uhr schrieb Richard Knight <richard@1bpm.net>:

hi

Unfortunately the python opcodes that return data to Csound (pyeval) appear to only be able to pass floats (so no array). The only way I could think of is a bit hacky but works like the following. The length of the list is obtained first and then loop through and obtain the value for the relevant index using individual pyeval statements. This could be done in a similar way at k-rate with the relevant pyeval opcodes changed, etc:

pyinit
pyruni "the_list = range(1,6)"

instr 1
    ilength pyevali "float(len(the_list))" ; has to be float or raises error
    iresult[] init ilength
    index = 0
    while (index < ilength) do
        iresult[index] = pyevali(sprintf("float(the_list[%d])", index)) ; has to be float or raises error
        index += 1
    od
    printarray iresult
endin

 

On Wed, 8 Apr 2020 20:01:48 +0200, Stefan Thomas wrote:

Dear community,
is it possible to transfer the content of a list that I've produced with python, to an ftable or an array in csound?
For example
aList = list(range(1,6))
I know, off course, that I could do this easily with csound itself, but I feel more comfortable with the python language and I would like to transfer more complicated things from python to csound.
Thanks for Your help.
Stefan
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

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


Date2020-04-09 09:28
Fromjoachim heintz
SubjectRe: [Csnd] python l to array or ftable
in addition to the other suggestions:
if you really only want to do something in python, and import the result 
as ftable or array in csound, you can simply write it to a file in 
python, and read the file into a function table with GEN23 (or with 
readf if this is more appropriate).



On 08/04/2020 21:36, Stefan Thomas wrote:
> Dear Richard,
> thanks for Your help!
> It worked perfectly for me.I will have a look for ctcsound as well.
> All the best,
> Stefan
> 
> Am Mi., 8. Apr. 2020 um 20:18 Uhr schrieb Richard Knight 
> >:
> 
>     __
> 
>     hi
> 
>     Unfortunately the python opcodes that return data to Csound (pyeval)
>     appear to only be able to pass floats (so no array). The only way I
>     could think of is a bit hacky but works like the following. The
>     length of the list is obtained first and then loop through and
>     obtain the value for the relevant index using individual pyeval
>     statements. This could be done in a similar way at k-rate with the
>     relevant pyeval opcodes changed, etc:
> 
>     pyinit
>     pyruni "the_list = range(1,6)"
> 
>     instr 1
>          ilength pyevali "float(len(the_list))" ; has to be float or
>     raises error
>          iresult[] init ilength
>          index = 0
>          while (index < ilength) do
>              iresult[index] = pyevali(sprintf("float(the_list[%d])",
>     index)) ; has to be float or raises error
>              index += 1
>          od
>          printarray iresult
>     endin
> 
>     On Wed, 8 Apr 2020 20:01:48 +0200, Stefan Thomas wrote:
> 
>>     Dear community,
>>     is it possible to transfer the content of a list that I've
>>     produced with python, to an ftable or an array in csound?
>>     For example
>>     aList = list(range(1,6))
>>     I know, off course, that I could do this easily with csound
>>     itself, but I feel more comfortable with the python language and I
>>     would like to transfer more complicated things from python to csound.
>>     Thanks for Your help.
>>     Stefan
>>     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

Date2020-04-09 18:35
FromVictor Lazzarini
SubjectRe: [Csnd] [EXTERNAL] [Csnd] python l to array or ftable
It’s dead easy with ctcsound. You just copy a Numpy array into a function table and
from there you can copy it into an array if you need, or just use it directly.
Here’s an example, where I create an arbitrary impulse response and then
copy it into Csound for convolution

# impulse response (trivial)
h = np.zeros(128)
h[0]  = 1
h[127] = 1
N = h.size

# synthesis
csound = cs.Csound()
csound.setOption('-odac')
duration = 4
csound.compileOrc('''
// empty table
imp = ftgen(1,0,%d,7,0,%d,0) 
instr 1
 asig = diskin:a("fox.wav")
 aout = dconv(asig,ftlen(p4),p4)
   out(aout)
endin
schedule(1,0,%d,imp)
''' % (N,N,duration))
sr=csound.sr()
ksmps=csound.ksmps()
t=np.arange(0,int(sr*duration))
csound.start()
# copy into table 1
csound.tableCopyIn(1,h)
for i in t[::ksmps]:
    csound.performKsmps()

========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 8 Apr 2020, at 20:36, Stefan Thomas  wrote:
> 
> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> Dear Richard, 
> thanks for Your help!
> It worked perfectly for me.I will have a look for ctcsound as well.
> All the best,
> Stefan
> 
> Am Mi., 8. Apr. 2020 um 20:18 Uhr schrieb Richard Knight :
> hi
> 
> Unfortunately the python opcodes that return data to Csound (pyeval) appear to only be able to pass floats (so no array). The only way I could think of is a bit hacky but works like the following. The length of the list is obtained first and then loop through and obtain the value for the relevant index using individual pyeval statements. This could be done in a similar way at k-rate with the relevant pyeval opcodes changed, etc:
> 
> pyinit
> pyruni "the_list = range(1,6)"
> 
> instr 1
>     ilength pyevali "float(len(the_list))" ; has to be float or raises error
>     iresult[] init ilength
>     index = 0
>     while (index < ilength) do
>         iresult[index] = pyevali(sprintf("float(the_list[%d])", index)) ; has to be float or raises error
>         index += 1
>     od
>     printarray iresult
> endin
> 
>  
> On Wed, 8 Apr 2020 20:01:48 +0200, Stefan Thomas wrote:
> 
>> Dear community,
>> is it possible to transfer the content of a list that I've produced with python, to an ftable or an array in csound?
>> For example
>> aList = list(range(1,6))
>> I know, off course, that I could do this easily with csound itself, but I feel more comfortable with the python language and I would like to transfer more complicated things from python to csound.
>> Thanks for Your help.
>> Stefan
>> 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