Csound Csound-dev Csound-tekno Search About

[Csnd] running python with maintop() inside csound

Date2017-12-17 07:01
Fromalexmitchellmus
Subject[Csnd] running python with maintop() inside csound
I'm experimenting with using Python as a way to generate GUI's, however I am
unable to get mainloop() running without blocking Csound execution?

Here is my test code:




; Select audio/midi flags here according to platform
-odac    ;;;realtime audio out
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o oscils.wav -W ;;; for file output any platform



sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1


pyinit

pyruni {{
import sys

if not hasattr(sys, 'argv'):
   sys.argv = ['']

import Tkinter as t

root = t.Tk()
T = t.Text(root, height=3, width=30)
T.pack()
T.insert(t.END, "This window has to be closed\\nin order for Csound
to\\ngenerate sound.")
T.mainloop()
}}


instr 1

iflg = p4
asig oscils .7, 220, 0, iflg
     outs asig, asig

endin






i 1 0 2 0
i 1 3 2 2	;double precision
e






--
Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html

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

Date2017-12-18 00:00
FromRoger Kelly
SubjectRe: [Csnd] running python with maintop() inside csound
I am not exactly sure what your app does, but you definitely will block the Csound thread like this.

You should look at the Python 'threading" module or "process".  It might help here.  You would have to spin up your Mainloop in a Python process likely.

On Sun, Dec 17, 2017 at 1:01 AM, alexmitchellmus <alex.w.mitchell@gmail.com> wrote:
I'm experimenting with using Python as a way to generate GUI's, however I am
unable to get mainloop() running without blocking Csound execution?

Here is my test code:


<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-odac    ;;;realtime audio out
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o oscils.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1


pyinit

pyruni {{
import sys

if not hasattr(sys, 'argv'):
   sys.argv = ['']

import Tkinter as t

root = t.Tk()
T = t.Text(root, height=3, width=30)
T.pack()
T.insert(t.END, "This window has to be closed\\nin order for Csound
to\\ngenerate sound.")
T.mainloop()
}}


instr 1

iflg = p4
asig oscils .7, 220, 0, iflg
     outs asig, asig

endin



</CsInstruments>
<CsScore>

i 1 0 2 0
i 1 3 2 2       ;double precision
e
</CsScore>
</CsoundSynthesizer>




--
Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html

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

Date2017-12-18 01:00
FromMichael Gogins
SubjectRe: [Csnd] running python with maintop() inside csound
There are examples to look at like
https://github.com/csound/csound/blob/develop/examples/python/drone.py
(requires python-tk and tix).

Regards,
Mike

-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com


On Sun, Dec 17, 2017 at 7:00 PM, Roger Kelly  wrote:
> I am not exactly sure what your app does, but you definitely will block the
> Csound thread like this.
>
> You should look at the Python 'threading" module or "process".  It might
> help here.  You would have to spin up your Mainloop in a Python process
> likely.
>
> On Sun, Dec 17, 2017 at 1:01 AM, alexmitchellmus 
> wrote:
>>
>> I'm experimenting with using Python as a way to generate GUI's, however I
>> am
>> unable to get mainloop() running without blocking Csound execution?
>>
>> Here is my test code:
>>
>>
>> 
>> 
>> ; Select audio/midi flags here according to platform
>> -odac    ;;;realtime audio out
>> ;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
>> ; For Non-realtime ouput leave only the line below:
>> ; -o oscils.wav -W ;;; for file output any platform
>> 
>> 
>>
>> sr = 44100
>> ksmps = 32
>> nchnls = 2
>> 0dbfs  = 1
>>
>>
>> pyinit
>>
>> pyruni {{
>> import sys
>>
>> if not hasattr(sys, 'argv'):
>>    sys.argv = ['']
>>
>> import Tkinter as t
>>
>> root = t.Tk()
>> T = t.Text(root, height=3, width=30)
>> T.pack()
>> T.insert(t.END, "This window has to be closed\\nin order for Csound
>> to\\ngenerate sound.")
>> T.mainloop()
>> }}
>>
>>
>> instr 1
>>
>> iflg = p4
>> asig oscils .7, 220, 0, iflg
>>      outs asig, asig
>>
>> endin
>>
>>
>>
>> 
>> 
>>
>> i 1 0 2 0
>> i 1 3 2 2       ;double precision
>> e
>> 
>> 
>>
>>
>>
>>
>> --
>> Sent from:
>> http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>>
>> 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

Date2017-12-18 01:52
Fromalexmitchellmus
SubjectRe: [Csnd] running python with maintop() inside csound
Thanks for suggestions. I understand that python can call the Csound API, but
wanted to see if running a python GUI from within csound was possible.



--
Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html

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

Date2017-12-18 03:45
FromMichael Gogins
SubjectRe: [Csnd] running python with maintop() inside csound
It MIGHT be possible but instead of calling the main loop you should
try calling some sort of message dispatch function every kperiod. In
other words Csound has become the main thread. To receive and dispatch
GUI messages you will have to do what the GUI main loop usually does.
I think in Tikinter it would be something like "root.update()." This
should happen on some sort of Csound timer opcode, you should
experiment to see what is the proper interval and if there are any
other problems.

Hope this helps,
Mike

-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com


On Sun, Dec 17, 2017 at 8:52 PM, alexmitchellmus
 wrote:
> Thanks for suggestions. I understand that python can call the Csound API, but
> wanted to see if running a python GUI from within csound was possible.
>
>
>
> --
> Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>
> 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

Date2017-12-18 03:45
FromMichael Gogins
SubjectRe: [Csnd] running python with maintop() inside csound
Probably you want to define an instrument that is "always on" to run
the Tkinter dispatching.

-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com


On Sun, Dec 17, 2017 at 10:45 PM, Michael Gogins
 wrote:
> It MIGHT be possible but instead of calling the main loop you should
> try calling some sort of message dispatch function every kperiod. In
> other words Csound has become the main thread. To receive and dispatch
> GUI messages you will have to do what the GUI main loop usually does.
> I think in Tikinter it would be something like "root.update()." This
> should happen on some sort of Csound timer opcode, you should
> experiment to see what is the proper interval and if there are any
> other problems.
>
> Hope this helps,
> Mike
>
> -----------------------------------------------------
> Michael Gogins
> Irreducible Productions
> http://michaelgogins.tumblr.com
> Michael dot Gogins at gmail dot com
>
>
> On Sun, Dec 17, 2017 at 8:52 PM, alexmitchellmus
>  wrote:
>> Thanks for suggestions. I understand that python can call the Csound API, but
>> wanted to see if running a python GUI from within csound was possible.
>>
>>
>>
>> --
>> Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>>
>> 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

Date2017-12-18 08:29
Fromalexmitchellmus
SubjectRe: [Csnd] running python with maintop() inside csound
Thanks Michael,

I will give that a try. I'm not a huge user of Tkinter, (however much less
1990's OOP than WXWidgets) but was interested in seeing how this could work
in Csound (as a CSD- not as a PY).



--
Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html

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

Date2017-12-18 17:05
FromMichael Gogins
SubjectRe: [Csnd] running python with maintop() inside csound
The same principle would apply in wx. Instead of calling MainLoop, set up an alwayson instrument and in it, set up a timer that repeatedly calls wx.

Not tested, but something like this. In your always on instrument set up your own "event loop" by executing this Python:

        self.evtloop = wx.GUIEventLoop()
        wx.EventLoop.SetActive(self.evtloop)
        self.run = True

Then in your Csound timer block execute this python periodically to handle GUI events:

       if self.run == True:
            while self.evtloop.Pending(): # if there is at least one event to be processed
                self.evtloop.Dispatch() # process one event
            else: # no UI events
                self.ticks += 1
                if self.ticks >= self.idle_ticks:
                    self.evtloop.ProcessIdle()
                    self.ticks = 0



On Dec 18, 2017 3:29 AM, "alexmitchellmus" <alex.w.mitchell@gmail.com> wrote:
Thanks Michael,

I will give that a try. I'm not a huge user of Tkinter, (however much less
1990's OOP than WXWidgets) but was interested in seeing how this could work
in Csound (as a CSD- not as a PY).



--
Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html

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