Csound Csound-dev Csound-tekno Search About

[Csnd] ctcsound and matplotlib

Date2019-07-24 13:24
Fromjoachim heintz
Subject[Csnd] ctcsound and matplotlib
hi all -

i am working with ctcsound in jupyter notebooks.  i would like to 
display csound channel values in a plot in the notebook (inline if 
possible).  as a very simple example:

	kVal line 0, 1, 1

i would like to show this kVal in a way that it draws in real-time in 
one second a line from 0 to 1 in a xy plot.

i thought it might be easy with the matplotlib.animation module, but i 
cannot find the point where to start.  perhaps anyone already did 
something similar, or has a tip?

thanks in advance -

	joachim

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

Date2019-07-28 17:24
FromGuillermo Senna
SubjectRe: [Csnd] ctcsound and matplotlib
Hi Joachim,

Were you able to do this?

Cheers.


On 24/7/19 09:24, joachim heintz wrote:
> hi all -
>
> i am working with ctcsound in jupyter notebooks.  i would like to
> display csound channel values in a plot in the notebook (inline if
> possible).  as a very simple example:
>
>     kVal line 0, 1, 1
>
> i would like to show this kVal in a way that it draws in real-time in
> one second a line from 0 to 1 in a xy plot.
>
> i thought it might be easy with the matplotlib.animation module, but i
> cannot find the point where to start.  perhaps anyone already did
> something similar, or has a tip?
>
> thanks in advance -
>
>     joachim
>
> 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

Date2019-07-28 18:46
Fromjoachim heintz
SubjectRe: [Csnd] ctcsound and matplotlib
hi guillermo -

not yet.  today i came across this:
https://pythonprogramming.net/live-graphs-matplotlib-tutorial/

and i try to modify the code so that i can use a csound signal in the 
animation function.  this should work, i think, but i am still fiddling 
around ...

cheers -
	j



On 28/07/19 18:24, Guillermo Senna wrote:
> Hi Joachim,
>
> Were you able to do this?
>
> Cheers.
>
>
> On 24/7/19 09:24, joachim heintz wrote:
>> hi all -
>>
>> i am working with ctcsound in jupyter notebooks.  i would like to
>> display csound channel values in a plot in the notebook (inline if
>> possible).  as a very simple example:
>>
>>     kVal line 0, 1, 1
>>
>> i would like to show this kVal in a way that it draws in real-time in
>> one second a line from 0 to 1 in a xy plot.
>>
>> i thought it might be easy with the matplotlib.animation module, but i
>> cannot find the point where to start.  perhaps anyone already did
>> something similar, or has a tip?
>>
>> thanks in advance -
>>
>>     joachim
>>
>> 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

Date2019-07-29 07:36
FromOeyvind Brandtsegg
SubjectRe: [Csnd] ctcsound and matplotlib
Hi,
sorry for the late reply.
Yes, I did some similar things.

Way back I had a waveform plotter for Improsculpt (https://sourceforge.net/projects/improsculpt/),
but this used the wx gui library and it can be done much simpler now.
This was integrated with realtime data from Csound.

Last year, I did some waveform animations for  a class, using matplotlib and animation. Very similar to the last link you sent, Joachim.
Although I have not integrated the animation with realtime data from csound, I would hope this to be realtively straightforward.
The basic premise is that you have a dataset (of static size) that will be displayed. For each frame in the animation, you update the data contents, and show the new set.
A very simple example of a moving pulse is copied in below. 
Come to think if a caveat: I think maybe the animation.FuncAnimation() renders the whole thing before displaying, so you would not be able to intervene with realtime data in the most basic way of using this. But I think there are some tips here https://stackoverflow.com/questions/34376656/matplotlib-create-real-time-animated-graph that would allow realtime updates of the data.

All best
Oeyvind

Here's my simplest version:

***
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.1)
#y = np.random.rand(len(x))
y = np.zeros(len(x))
y[int(len(x)/2)] = 1
line, = ax.plot(x, y)


def init():
    line.set_ydata([np.nan] * len(x))
    return line,

def animate(i):
    # this basically just updates the data, replace it wwith something useful....
    ndices = range(len(x)-1)
    ndices.reverse()
    for ndx in ndices:
        y[ndx+1] = y[ndx]
    y[0] = y[-1]
    # ... and set the data here
    line.set_ydata(y)  
    return line,

ani = animation.FuncAnimation(
    fig, animate, init_func=init, interval=8, blit=True, save_count=300)

#from matplotlib.animation import FFMpegWriter
#writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
#ani.save("movie.mp4", writer=writer)

plt.show()
***


søn. 28. jul. 2019 kl. 19:46 skrev joachim heintz <jh@joachimheintz.de>:
hi guillermo -

not yet.  today i came across this:
https://pythonprogramming.net/live-graphs-matplotlib-tutorial/

and i try to modify the code so that i can use a csound signal in the
animation function.  this should work, i think, but i am still fiddling
around ...

cheers -
        j



On 28/07/19 18:24, Guillermo Senna wrote:
> Hi Joachim,
>
> Were you able to do this?
>
> Cheers.
>
>
> On 24/7/19 09:24, joachim heintz wrote:
>> hi all -
>>
>> i am working with ctcsound in jupyter notebooks.  i would like to
>> display csound channel values in a plot in the notebook (inline if
>> possible).  as a very simple example:
>>
>>     kVal line 0, 1, 1
>>
>> i would like to show this kVal in a way that it draws in real-time in
>> one second a line from 0 to 1 in a xy plot.
>>
>> i thought it might be easy with the matplotlib.animation module, but i
>> cannot find the point where to start.  perhaps anyone already did
>> something similar, or has a tip?
>>
>> thanks in advance -
>>
>>     joachim
>>
>> 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

Date2019-07-29 08:27
FromAndreas Bergsland
SubjectRe: [Csnd] ctcsound and matplotlib

Hi Øyvind

I used a module called drawnow with matplotlib and numpy in a simple realtime plotter I made some years ago. It is not very effective, however, and struggles with higher update rates.

Best,

Andreas

 

 

/*

Plot - Plots one or two(optional) values on a two axes plot

 

DEPENDENCIES

Requires the python modules matplotlib, numpy and drawnow

 

DESCRIPTION

Plot - Plots one or two(optional) values on a two axes plot with user defined range

 

SYNTAX

kdummyoutput Plot                        iupdaterate, imin1, imax1, kval1 [,iplot2, imin2, imax2, kval2]

 

INITIALIZATION

iupdaterate - the rate with which the plot is updated. Defaults to 20.

imin1     - minimum for value 1

imin2 - maximum for value 1

iplot2 - set >= 0 to use second plot (optional)

imin2 - minimum for value 2, defaults to 0 (optional)

imax2 - maximum for value 2, defaults to 1 (optional)

 

PERFORMANCE

kval1 - first value to plot

kval2 - second value to plot (optional)

kout - dummy output (same as kval1)

 

CREDITS

Andreas Bergsland 2015

*/

opcode Plot, k, iiikoopO

                                iupdaterate, imin1, imax1, kval1, iplot2, imin2, imax2, kval2          xin

 

pyinit

pyassigni              "min1", imin1

pyassigni              "max1", imax1

pyassigni              "min2", imin2

pyassigni              "max2", imax2

pyassigni              "plot2", iplot2

 

if             iupdaterate <= 0               then

                iupdaterate = 20

endif

 

pyruni   {{

import numpy

import matplotlib.pyplot as plt

from drawnow import *

 

data1=[]                               #make lists

data2=[]

cnt=0

plt.ion()                #tell matplotlib to operate in interactive mode

 

def PlotFigure():

    plt.ylim(min1,max1)

    plt.grid(True)

    plt.plot(data1, 'ro-')

    if plot2 > 0:

        plt2=plt.twinx()

        plt.ylim(min2,max2)

        plt2.plot(data2,'b^-')

}}

 

pyassign               "val1", kval1

pyassign               "val2", kval2

; Update frequency for plot

ktrig                       metro   iupdaterate

; Update sequence

pyrunt  ktrig,      {{

data1.append(val1)

data2.append(val2)

drawnow(PlotFigure, 'ro-')

cnt = cnt + 1

if (cnt>50):

    data1.pop(0)

    data2.pop(0)

}}

xout       kval1      ; Dummy output

endop

 

From: A discussion list for users of Csound <CSOUND@LISTSERV.HEANET.IE> on behalf of "oyvind.brandtsegg@ntnu.no" <oyvind.brandtsegg@NTNU.NO>
Reply-To: A discussion list for users of Csound <CSOUND@LISTSERV.HEANET.IE>
Date: Monday, 29 July 2019 at 08:37
To: "CSOUND@LISTSERV.HEANET.IE" <CSOUND@LISTSERV.HEANET.IE>
Subject: Re: [Csnd] ctcsound and matplotlib

 

Hi,

sorry for the late reply.

Yes, I did some similar things.

 

Way back I had a waveform plotter for Improsculpt (https://sourceforge.net/projects/improsculpt/),

but this used the wx gui library and it can be done much simpler now.

This was integrated with realtime data from Csound.

 

Last year, I did some waveform animations for  a class, using matplotlib and animation. Very similar to the last link you sent, Joachim.

Although I have not integrated the animation with realtime data from csound, I would hope this to be realtively straightforward.

The basic premise is that you have a dataset (of static size) that will be displayed. For each frame in the animation, you update the data contents, and show the new set.

A very simple example of a moving pulse is copied in below. 

Come to think if a caveat: I think maybe the animation.FuncAnimation() renders the whole thing before displaying, so you would not be able to intervene with realtime data in the most basic way of using this. But I think there are some tips here https://stackoverflow.com/questions/34376656/matplotlib-create-real-time-animated-graph that would allow realtime updates of the data.

 

All best

Oeyvind

 

Here's my simplest version:

 

***

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.1)
#y = np.random.rand(len(x))
y = np.zeros(len(x))
y[int(len(x)/2)] = 1
line, = ax.plot(x, y)


def init():
    line.set_ydata([np.nan] * len(x))
    return line,

def animate(i):
    # this basically just updates the data, replace it wwith something useful....

    ndices = range(len(x)-1)
    ndices.reverse()
    for ndx in ndices:
        y[ndx+1] = y[ndx]
    y[0] = y[-1]

    # ... and set the data here
    line.set_ydata(y)  
    return line,

ani = animation.FuncAnimation(
    fig, animate, init_func=init, interval=8, blit=True, save_count=300)

#from matplotlib.animation import FFMpegWriter
#writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
#ani.save("movie.mp4", writer=writer)

plt.show()

***

 

 

søn. 28. jul. 2019 kl. 19:46 skrev joachim heintz <jh@joachimheintz.de>:

hi guillermo -

not yet.  today i came across this:
https://pythonprogramming.net/live-graphs-matplotlib-tutorial/

and i try to modify the code so that i can use a csound signal in the
animation function.  this should work, i think, but i am still fiddling
around ...

cheers -
        j



On 28/07/19 18:24, Guillermo Senna wrote:
> Hi Joachim,
>
> Were you able to do this?
>
> Cheers.
>
>
> On 24/7/19 09:24, joachim heintz wrote:
>> hi all -
>>
>> i am working with ctcsound in jupyter notebooks.  i would like to
>> display csound channel values in a plot in the notebook (inline if
>> possible).  as a very simple example:
>>
>>     kVal line 0, 1, 1
>>
>> i would like to show this kVal in a way that it draws in real-time in
>> one second a line from 0 to 1 in a xy plot.
>>
>> i thought it might be easy with the matplotlib.animation module, but i
>> cannot find the point where to start.  perhaps anyone already did
>> something similar, or has a tip?
>>
>> thanks in advance -
>>
>>     joachim
>>
>> 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


Date2019-07-29 09:00
FromEduardo Moguillansky
SubjectRe: [Csnd] ctcsound and matplotlib
matplotlib is very inefficient for interactive plotting, in my experience. I would recommend Pyqtplot for that.

On 29.07.19 09:27, Andreas Bergsland wrote:

Hi Øyvind

I used a module called drawnow with matplotlib and numpy in a simple realtime plotter I made some years ago. It is not very effective, however, and struggles with higher update rates.

Best,

Andreas

 

 

/*

Plot - Plots one or two(optional) values on a two axes plot

 

DEPENDENCIES

Requires the python modules matplotlib, numpy and drawnow

 

DESCRIPTION

Plot - Plots one or two(optional) values on a two axes plot with user defined range

 

SYNTAX

kdummyoutput Plot                        iupdaterate, imin1, imax1, kval1 [,iplot2, imin2, imax2, kval2]

 

INITIALIZATION

iupdaterate - the rate with which the plot is updated. Defaults to 20.

imin1     - minimum for value 1

imin2 - maximum for value 1

iplot2 - set >= 0 to use second plot (optional)

imin2 - minimum for value 2, defaults to 0 (optional)

imax2 - maximum for value 2, defaults to 1 (optional)

 

PERFORMANCE

kval1 - first value to plot

kval2 - second value to plot (optional)

kout - dummy output (same as kval1)

 

CREDITS

Andreas Bergsland 2015

*/

opcode Plot, k, iiikoopO

                                iupdaterate, imin1, imax1, kval1, iplot2, imin2, imax2, kval2          xin

 

pyinit

pyassigni              "min1", imin1

pyassigni              "max1", imax1

pyassigni              "min2", imin2

pyassigni              "max2", imax2

pyassigni              "plot2", iplot2

 

if             iupdaterate <= 0               then

                iupdaterate = 20

endif

 

pyruni   {{

import numpy

import matplotlib.pyplot as plt

from drawnow import *

 

data1=[]                               #make lists

data2=[]

cnt=0

plt.ion()                #tell matplotlib to operate in interactive mode

 

def PlotFigure():

    plt.ylim(min1,max1)

    plt.grid(True)

    plt.plot(data1, 'ro-')

    if plot2 > 0:

        plt2=plt.twinx()

        plt.ylim(min2,max2)

        plt2.plot(data2,'b^-')

}}

 

pyassign               "val1", kval1

pyassign               "val2", kval2

; Update frequency for plot

ktrig                       metro   iupdaterate

; Update sequence

pyrunt  ktrig,      {{

data1.append(val1)

data2.append(val2)

drawnow(PlotFigure, 'ro-')

cnt = cnt + 1

if (cnt>50):

    data1.pop(0)

    data2.pop(0)

}}

xout       kval1      ; Dummy output

endop

 

From: A discussion list for users of Csound <CSOUND@LISTSERV.HEANET.IE> on behalf of "oyvind.brandtsegg@ntnu.no" <oyvind.brandtsegg@NTNU.NO>
Reply-To: A discussion list for users of Csound <CSOUND@LISTSERV.HEANET.IE>
Date: Monday, 29 July 2019 at 08:37
To: "CSOUND@LISTSERV.HEANET.IE" <CSOUND@LISTSERV.HEANET.IE>
Subject: Re: [Csnd] ctcsound and matplotlib

 

Hi,

sorry for the late reply.

Yes, I did some similar things.

 

Way back I had a waveform plotter for Improsculpt (https://sourceforge.net/projects/improsculpt/),

but this used the wx gui library and it can be done much simpler now.

This was integrated with realtime data from Csound.

 

Last year, I did some waveform animations for  a class, using matplotlib and animation. Very similar to the last link you sent, Joachim.

Although I have not integrated the animation with realtime data from csound, I would hope this to be realtively straightforward.

The basic premise is that you have a dataset (of static size) that will be displayed. For each frame in the animation, you update the data contents, and show the new set.

A very simple example of a moving pulse is copied in below. 

Come to think if a caveat: I think maybe the animation.FuncAnimation() renders the whole thing before displaying, so you would not be able to intervene with realtime data in the most basic way of using this. But I think there are some tips here https://stackoverflow.com/questions/34376656/matplotlib-create-real-time-animated-graph that would allow realtime updates of the data.

 

All best

Oeyvind

 

Here's my simplest version:

 

***

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.1)
#y = np.random.rand(len(x))
y = np.zeros(len(x))
y[int(len(x)/2)] = 1
line, = ax.plot(x, y)


def init():
    line.set_ydata([np.nan] * len(x))
    return line,

def animate(i):
    # this basically just updates the data, replace it wwith something useful....

    ndices = range(len(x)-1)
    ndices.reverse()
    for ndx in ndices:
        y[ndx+1] = y[ndx]
    y[0] = y[-1]

    # ... and set the data here
    line.set_ydata(y)  
    return line,

ani = animation.FuncAnimation(
    fig, animate, init_func=init, interval=8, blit=True, save_count=300)

#from matplotlib.animation import FFMpegWriter
#writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
#ani.save("movie.mp4", writer=writer)

plt.show()

***

 

 

søn. 28. jul. 2019 kl. 19:46 skrev joachim heintz <jh@joachimheintz.de>:

hi guillermo -

not yet.  today i came across this:
https://pythonprogramming.net/live-graphs-matplotlib-tutorial/

and i try to modify the code so that i can use a csound signal in the
animation function.  this should work, i think, but i am still fiddling
around ...

cheers -
        j



On 28/07/19 18:24, Guillermo Senna wrote:
> Hi Joachim,
>
> Were you able to do this?
>
> Cheers.
>
>
> On 24/7/19 09:24, joachim heintz wrote:
>> hi all -
>>
>> i am working with ctcsound in jupyter notebooks.  i would like to
>> display csound channel values in a plot in the notebook (inline if
>> possible).  as a very simple example:
>>
>>     kVal line 0, 1, 1
>>
>> i would like to show this kVal in a way that it draws in real-time in
>> one second a line from 0 to 1 in a xy plot.
>>
>> i thought it might be easy with the matplotlib.animation module, but i
>> cannot find the point where to start.  perhaps anyone already did
>> something similar, or has a tip?
>>
>> thanks in advance -
>>
>>     joachim
>>
>> 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

Date2019-08-02 10:58
Fromjoachim heintz
SubjectRe: [Csnd] ctcsound and matplotlib
thanks to all for the help and suggestions.  i did some steps and feel 
that i am not far from getting where i want to be.  actually this is the 
question now:

when i run csound in a performance loop, and get control values from 
csound like:

while not cs.performBuffer():
     val = cs.controlChannel('val')[0]

how can i update this variable (val) outside the while loop (because 
outside the while loop i have the matplotlib figure and the animation)?

or is there another way  to receive control values from csound in python?

the jupyter notebook python code is below; thanks for any help -

	joachim


%matplotlib

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

import ctcsound as csound

orc = '''
seed 0
ksmps = 128
instr 1
  kVal randomi 0, 1, 1, 3
  chnset kVal, "val"
  printk 1, kVal
endin
'''
sco = "i1 0 10\n"

cs = csound.Csound()
cs.setOption('-n')
cs.setOption('-b1024')
cs.compileOrc(orc)
cs.readScore(sco)
cs.start()
while not cs.performBuffer():
     val = cs.controlChannel('val')[0]

#hot can i update val OUTSIDE the while loop?
#here it only catches one value
print(val)

fig, ax = plt.subplots()
ax.set(xlim=(0,10), ylim=(0,1))
line, = ax.plot([], [], lw=2)

def animate(i):
     fps = 10
     x = np.linspace(0, i/fps, i)
     y = val
     line.set_data(x, y)

anim = animation.FuncAnimation(fig, animate, interval=100)

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

Date2019-08-02 14:52
FromOeyvind Brandtsegg
SubjectRe: [Csnd] ctcsound and matplotlib
Aha, yes,
the problem is that the animation is its own loop, so it looks to me like Csound will run until done, then the animation will start?
I think it would be better to run the animation and Csound in separate threads in Python. Then you could send values from the Csound thread to the animation thread via signaling, global variables, or even OSC.
Here's an introduction to threading in Python

 

fre. 2. aug. 2019 kl. 11:58 skrev joachim heintz <jh@joachimheintz.de>:
thanks to all for the help and suggestions.  i did some steps and feel
that i am not far from getting where i want to be.  actually this is the
question now:

when i run csound in a performance loop, and get control values from
csound like:

while not cs.performBuffer():
     val = cs.controlChannel('val')[0]

how can i update this variable (val) outside the while loop (because
outside the while loop i have the matplotlib figure and the animation)?

or is there another way  to receive control values from csound in python?

the jupyter notebook python code is below; thanks for any help -

        joachim


%matplotlib

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

import ctcsound as csound

orc = '''
seed 0
ksmps = 128
instr 1
  kVal randomi 0, 1, 1, 3
  chnset kVal, "val"
  printk 1, kVal
endin
'''
sco = "i1 0 10\n"

cs = csound.Csound()
cs.setOption('-n')
cs.setOption('-b1024')
cs.compileOrc(orc)
cs.readScore(sco)
cs.start()
while not cs.performBuffer():
     val = cs.controlChannel('val')[0]

#hot can i update val OUTSIDE the while loop?
#here it only catches one value
print(val)

fig, ax = plt.subplots()
ax.set(xlim=(0,10), ylim=(0,1))
line, = ax.plot([], [], lw=2)

def animate(i):
     fps = 10
     x = np.linspace(0, i/fps, i)
     y = val
     line.set_data(x, y)

anim = animation.FuncAnimation(fig, animate, interval=100)

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

Date2019-08-03 11:06
Fromjoachim heintz
SubjectRe: [Csnd] ctcsound and matplotlib
yes, oeyvind; you hit the point.  thanks -- i was thinking in the wrong 
directory.

actually this threading can very nicely be done as francois showed in 
this notebook:
https://github.com/fggp/ctcsound/blob/master/cookbook/03-threading.ipynb

below is the basic code -- more will follow i think.

	joachim


%matplotlib

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

import ctcsound as csound

orc = '''
ksmps = 128
instr 1
  kVal randomi 0, 1, 1, 3
  chnset kVal, "val"
endin
'''
sco = "i1 0 10\n"

cs = csound.Csound()
cs.setOption('-odac')
cs.compileOrc(orc)
cs.readScore(sco)
cs.start()

pt = csound.CsoundPerformanceThread(cs.csound())
pt.play()

fig, ax = plt.subplots()
ax.set(xlim=(0,10), ylim=(0,1))
line, = ax.plot([], [], lw=2)

def animate(i, x=[], y=[]):
     fps = 10
     x.append(i/fps)
     y.append(cs.controlChannel('val')[0])
     line.set_data(x, y)

anim = animation.FuncAnimation(fig, animate, interval=100)

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

Date2019-08-03 16:58
FromGuillermo Senna
SubjectRe: [Csnd] ctcsound and matplotlib
Nice!

How about:

%matplotlib

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

import ctcsound as csound

orc = '''
ksmps = 128
instr 1
 kVal randomi 0, 1, 1, 3
 chnset kVal, "val"
endin
'''
sco = "i1 0 10\n"

cs = csound.Csound()
cs.setOption('-odac')
cs.compileOrc(orc)
cs.readScore(sco)
cs.start()

pt = csound.CsoundPerformanceThread(cs.csound())
pt.play()

fig, ax = plt.subplots()
ax.set(xlim=(0,0.001), ylim=(0,1))
line, = ax.plot([], [], lw=2)

def animate(i, x=[], y=[]):
    fps = 10
    ax.set_xlim(0, i/fps)
    x.append(i/fps)
    y.append(cs.controlChannel('val')[0])
    line.set_data(x, y)

anim = animation.FuncAnimation(fig, animate, interval=100, frames=100,
repeat=False)



On 3/8/19 07:06, joachim heintz wrote:
> yes, oeyvind; you hit the point.  thanks -- i was thinking in the
> wrong directory.
>
> actually this threading can very nicely be done as francois showed in
> this notebook:
> https://github.com/fggp/ctcsound/blob/master/cookbook/03-threading.ipynb
>
> below is the basic code -- more will follow i think.
>
>     joachim
>
>
> %matplotlib
>
> import numpy as np
> from matplotlib import pyplot as plt
> from matplotlib import animation
>
> import ctcsound as csound
>
> orc = '''
> ksmps = 128
> instr 1
>  kVal randomi 0, 1, 1, 3
>  chnset kVal, "val"
> endin
> '''
> sco = "i1 0 10\n"
>
> cs = csound.Csound()
> cs.setOption('-odac')
> cs.compileOrc(orc)
> cs.readScore(sco)
> cs.start()
>
> pt = csound.CsoundPerformanceThread(cs.csound())
> pt.play()
>
> fig, ax = plt.subplots()
> ax.set(xlim=(0,10), ylim=(0,1))
> line, = ax.plot([], [], lw=2)
>
> def animate(i, x=[], y=[]):
>     fps = 10
>     x.append(i/fps)
>     y.append(cs.controlChannel('val')[0])
>     line.set_data(x, y)
>
> anim = animation.FuncAnimation(fig, animate, interval=100)
>
> 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

Date2019-08-03 19:03
Fromjoachim heintz
SubjectRe: [Csnd] ctcsound and matplotlib
cool!  i like this way of backwards freezing.  if the performance is not 
too long, this is a very nice way of showing what happened.

i did some other plots which allow endless watching.  the basic one is 
below.  and i am happy to see that this works very well even with some 
plots together and some real sounding csds.  it was worth trying.

	j


import ctcsound as csound
from matplotlib import pyplot as plt
from matplotlib import animation

orc = '''
ksmps = 128
seed 0
instr 1
  kVal randomi 0, 1, 1, 3
  chnset kVal, "val"
endin
'''
sco = "i1 0.2 99999\n"

#plot and animation settings
xlim=(0,5)
ylim=(0,1)
tmint = 100 #time interval in ms
cschn = 'val' #csound channel name

cs = csound.Csound()
cs.setOption('-odac')
cs.compileOrc(orc)
cs.readScore(sco)
cs.start()

pt = csound.CsoundPerformanceThread(cs.csound())
pt.play()

fig, ax = plt.subplots()
ax.set(xlim=xlim, ylim=ylim)
line, = ax.plot([], [], lw=2)
fps = 1000/tmint
xrange = xlim[1] - xlim[0]
xshow = 4/5
xclear = 1-xshow

def animate(i, x=[], y=[]):
     x.append(i/fps)
     y.append(cs.controlChannel(cschn)[0])
     line.set_data(x, y)
     if i > fps*xrange*xshow:
         ax.set_xlim(i/fps-xrange*xshow,i/fps+xrange*xclear)

anim = animation.FuncAnimation(fig, animate, interval=tmint)

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

Date2019-08-04 10:24
FromTarmo Johannes
SubjectRe: [Csnd] ctcsound and matplotlib
Hi,

Thanks for the clear and clean example!

Not knowing anything about matplotlib, is it easy to use it to display a function table? Or use values from 2 or 3 dimentional array?

Thanks,
Tarmo

L, 3. august 2019 21:03 joachim heintz <jh@joachimheintz.de> kirjutas:
cool!  i like this way of backwards freezing.  if the performance is not
too long, this is a very nice way of showing what happened.

i did some other plots which allow endless watching.  the basic one is
below.  and i am happy to see that this works very well even with some
plots together and some real sounding csds.  it was worth trying.

        j


import ctcsound as csound
from matplotlib import pyplot as plt
from matplotlib import animation

orc = '''
ksmps = 128
seed 0
instr 1
  kVal randomi 0, 1, 1, 3
  chnset kVal, "val"
endin
'''
sco = "i1 0.2 99999\n"

#plot and animation settings
xlim=(0,5)
ylim=(0,1)
tmint = 100 #time interval in ms
cschn = 'val' #csound channel name

cs = csound.Csound()
cs.setOption('-odac')
cs.compileOrc(orc)
cs.readScore(sco)
cs.start()

pt = csound.CsoundPerformanceThread(cs.csound())
pt.play()

fig, ax = plt.subplots()
ax.set(xlim=xlim, ylim=ylim)
line, = ax.plot([], [], lw=2)
fps = 1000/tmint
xrange = xlim[1] - xlim[0]
xshow = 4/5
xclear = 1-xshow

def animate(i, x=[], y=[]):
     x.append(i/fps)
     y.append(cs.controlChannel(cschn)[0])
     line.set_data(x, y)
     if i > fps*xrange*xshow:
         ax.set_xlim(i/fps-xrange*xshow,i/fps+xrange*xclear)

anim = animation.FuncAnimation(fig, animate, interval=tmint)

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

Date2019-08-04 10:41
Fromjoachim heintz
SubjectRe: [Csnd] ctcsound and matplotlib
hi tarmo -

yes, andrés cabrera used the matplotlib for showing tables in his 
iCsound.  françois pinot did a port to ctcsound and explained here:
https://github.com/fggp/ctcsound/blob/master/cookbook/07-icsound.ipynb

i wrote a notebook which is now here (as i just see):
https://github.com/fggp/ctcsound/blob/master/cookbook/09-showing-kvals.ipynb

ciao -
	joachim



On 04/08/19 11:24, Tarmo Johannes wrote:
> Hi,
>
> Thanks for the clear and clean example!
>
> Not knowing anything about matplotlib, is it easy to use it to display a
> function table? Or use values from 2 or 3 dimentional array?
>
> Thanks,
> Tarmo
>
> L, 3. august 2019 21:03 joachim heintz  > kirjutas:
>
>     cool!  i like this way of backwards freezing.  if the performance is
>     not
>     too long, this is a very nice way of showing what happened.
>
>     i did some other plots which allow endless watching.  the basic one is
>     below.  and i am happy to see that this works very well even with some
>     plots together and some real sounding csds.  it was worth trying.
>
>             j
>
>
>     import ctcsound as csound
>     from matplotlib import pyplot as plt
>     from matplotlib import animation
>
>     orc = '''
>     ksmps = 128
>     seed 0
>     instr 1
>       kVal randomi 0, 1, 1, 3
>       chnset kVal, "val"
>     endin
>     '''
>     sco = "i1 0.2 99999\n"
>
>     #plot and animation settings
>     xlim=(0,5)
>     ylim=(0,1)
>     tmint = 100 #time interval in ms
>     cschn = 'val' #csound channel name
>
>     cs = csound.Csound()
>     cs.setOption('-odac')
>     cs.compileOrc(orc)
>     cs.readScore(sco)
>     cs.start()
>
>     pt = csound.CsoundPerformanceThread(cs.csound())
>     pt.play()
>
>     fig, ax = plt.subplots()
>     ax.set(xlim=xlim, ylim=ylim)
>     line, = ax.plot([], [], lw=2)
>     fps = 1000/tmint
>     xrange = xlim[1] - xlim[0]
>     xshow = 4/5
>     xclear = 1-xshow
>
>     def animate(i, x=[], y=[]):
>          x.append(i/fps)
>          y.append(cs.controlChannel(cschn)[0])
>          line.set_data(x, y)
>          if i > fps*xrange*xshow:
>              ax.set_xlim(i/fps-xrange*xshow,i/fps+xrange*xclear)
>
>     anim = animation.FuncAnimation(fig, animate, interval=tmint)
>
>     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

Date2019-08-04 11:33
FromTarmo Johannes
SubjectRe: [Csnd] ctcsound and matplotlib
Thanks for the links! Exactly what I was interested in!
Tarmo

P, 4. august 2019 12:41 joachim heintz <jh@joachimheintz.de> kirjutas:
hi tarmo -

yes, andrés cabrera used the matplotlib for showing tables in his
iCsound.  françois pinot did a port to ctcsound and explained here:
https://github.com/fggp/ctcsound/blob/master/cookbook/07-icsound.ipynb

i wrote a notebook which is now here (as i just see):
https://github.com/fggp/ctcsound/blob/master/cookbook/09-showing-kvals.ipynb

ciao -
        joachim



On 04/08/19 11:24, Tarmo Johannes wrote:
> Hi,
>
> Thanks for the clear and clean example!
>
> Not knowing anything about matplotlib, is it easy to use it to display a
> function table? Or use values from 2 or 3 dimentional array?
>
> Thanks,
> Tarmo
>
> L, 3. august 2019 21:03 joachim heintz <jh@joachimheintz.de
> <mailto:jh@joachimheintz.de>> kirjutas:
>
>     cool!  i like this way of backwards freezing.  if the performance is
>     not
>     too long, this is a very nice way of showing what happened.
>
>     i did some other plots which allow endless watching.  the basic one is
>     below.  and i am happy to see that this works very well even with some
>     plots together and some real sounding csds.  it was worth trying.
>
>             j
>
>
>     import ctcsound as csound
>     from matplotlib import pyplot as plt
>     from matplotlib import animation
>
>     orc = '''
>     ksmps = 128
>     seed 0
>     instr 1
>       kVal randomi 0, 1, 1, 3
>       chnset kVal, "val"
>     endin
>     '''
>     sco = "i1 0.2 99999\n"
>
>     #plot and animation settings
>     xlim=(0,5)
>     ylim=(0,1)
>     tmint = 100 #time interval in ms
>     cschn = 'val' #csound channel name
>
>     cs = csound.Csound()
>     cs.setOption('-odac')
>     cs.compileOrc(orc)
>     cs.readScore(sco)
>     cs.start()
>
>     pt = csound.CsoundPerformanceThread(cs.csound())
>     pt.play()
>
>     fig, ax = plt.subplots()
>     ax.set(xlim=xlim, ylim=ylim)
>     line, = ax.plot([], [], lw=2)
>     fps = 1000/tmint
>     xrange = xlim[1] - xlim[0]
>     xshow = 4/5
>     xclear = 1-xshow
>
>     def animate(i, x=[], y=[]):
>          x.append(i/fps)
>          y.append(cs.controlChannel(cschn)[0])
>          line.set_data(x, y)
>          if i > fps*xrange*xshow:
>              ax.set_xlim(i/fps-xrange*xshow,i/fps+xrange*xclear)
>
>     anim = animation.FuncAnimation(fig, animate, interval=tmint)
>
>     Csound mailing list
>     Csound@listserv.heanet.ie <mailto: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
> <mailto: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