Csound Csound-dev Csound-tekno Search About

[Csnd] How can Csound yield the processor for a coroutine in Lua?

Date2013-07-23 14:23
FromMark Brophy
Subject[Csnd] How can Csound yield the processor for a coroutine in Lua?
I want to run a coroutine in Lua to process the user interface and another to generate sound. How can I direct the sound generator to periodically yield the processor?

Date2013-07-23 14:35
FromMichael Gogins
SubjectRe: [Csnd] How can Csound yield the processor for a coroutine in Lua?
There are many ways to deal with this.

If you are running Lua inside of Csound using the Lua opcodes, you do not need to deal with this, Csound is managing the sound generation threads.

If you are running Lua outside of Csound, then if you use performKsmps you do not need to deal with this, you do your GUI stuff and when you are done you call performKsmps and Csound will manage the audio generation threads. No coroutines, but possibly Csound will use multiple threads for audio.

As you probably know, coroutines are not threads, and two coroutines will not execute at the same time, so there is no advantage to using coroutines in this context that I am aware of.

If you truly need to run audio generation at the same time you are handling GUI events, use the csPerfThread class. This will run audio in a separate thread (managed form inside Csound) and so anything you do in Lua will indeed be handled in a separate thread. The csPerfThread  class (might be called CsoundPerformanceThread or something) manages the synchronization of data and processing for you. I think this is the way to go, but then, I have not tried it in Lua. It works fine in Python, though.

What is the context here? What are you trying to do?

Hope this helps,
Mike


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


On Tue, Jul 23, 2013 at 9:23 AM, Mark Brophy <mark@brophyworld.com> wrote:
I want to run a coroutine in Lua to process the user interface and another to generate sound. How can I direct the sound generator to periodically yield the processor?


Date2013-07-23 15:03
Fromfrancesco
Subject[Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.

Date2013-07-23 15:15
FromMichael Gogins
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
I do not usually use a GUI library with Lua. If I do, it is OpenGL using the ufo distribution.

I use Lua both inside Csound, with the Lua opcodes, mostly for score generation. I use the Lua opcodes mostly on Android.

I also use Lua outside Csound, also mostly for score generation. I use both the SWIG generated wrappers and the FFI interface to csound.h. 

There are examples for all of these uses. The FFI example is http://sourceforge.net/p/csound/csound6-git/ci/master/tree/examples/lua/csound_ffi.lua. You'd change the name of the Csound library to be correct for your platform.

Hope this helps,
Mike


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


On Tue, Jul 23, 2013 at 10:03 AM, francesco <ilterzouomo@fastwebnet.it> wrote:
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"




Date2013-07-23 17:51
FromMark Brophy
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
I'm trying to use LuaJIT and wxLua outside Csound to generate events in realtime with the Csound API and the FFI example Michael published and the CsoundPerformanceThread that you published.

On Tue, Jul 23, 2013 at 8:03 AM, francesco <ilterzouomo@fastwebnet.it> wrote:
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"




Date2013-07-23 18:31
FromMichael Gogins
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
It's not going to be easy to use CsoundPerformanceThread with FFI, but I'll look into it. You could easily do it just with csoundPerformKsmps however. If your wLua GUI is not doing too much, you won't notice the difference compared to threading.

Regards,
Mike


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


On Tue, Jul 23, 2013 at 12:51 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'm trying to use LuaJIT and wxLua outside Csound to generate events in realtime with the Csound API and the FFI example Michael published and the CsoundPerformanceThread that you published.


On Tue, Jul 23, 2013 at 8:03 AM, francesco <ilterzouomo@fastwebnet.it> wrote:
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"





Date2013-07-23 19:49
Fromfrancesco
Subject[Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
i use lua script with iup GUI (i also used wxLua and ltcltk).
I think Mr. Gogins is right, it's easy to do GUI for Csound if 
not too much complex, using the API, and it's working very well
(i.e. efficiently) in my opinion.
But i'm only a hobbyst like programmer (and Csounder).
LuaJIT ffi is great but need a better programming knowledge than mine!
But i was able to do some work using ffi and portsf (a library for
reading/writing
soundfiles that come with Audio Programming Book).
And that feels me like a real programmer, so it must be easy.

Hoping to see soon some of Your works.

ciao,
francesco.





--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725956.html
Sent from the Csound - General mailing list archive at Nabble.com.

Date2013-07-23 20:13
FromMichael Gogins
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
Because Csound comes with libsndfile, you can use FFI to read and write soundfiles using libsndfile. Then you do not need portsf. Of course, I do know know which soundfile library is best for you.

Regards,
Mike


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


On Tue, Jul 23, 2013 at 2:49 PM, francesco <ilterzouomo@fastwebnet.it> wrote:
i use lua script with iup GUI (i also used wxLua and ltcltk).
I think Mr. Gogins is right, it's easy to do GUI for Csound if
not too much complex, using the API, and it's working very well
(i.e. efficiently) in my opinion.
But i'm only a hobbyst like programmer (and Csounder).
LuaJIT ffi is great but need a better programming knowledge than mine!
But i was able to do some work using ffi and portsf (a library for
reading/writing
soundfiles that come with Audio Programming Book).
And that feels me like a real programmer, so it must be easy.

Hoping to see soon some of Your works.

ciao,
francesco.





--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725956.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"




Date2013-07-23 20:34
Fromfrancesco
Subject[Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
Yes You are right,
but i would need a more simple library for testing the use of FFI
and seemed to me that portsf (by Mr. Dobson) was perfect for my
need.

Here some FFI library (there is also libsndfile):
http://wiki.luajit.org/FFI-Bindings

Thanks,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725958.html
Sent from the Csound - General mailing list archive at Nabble.com.

Date2013-07-23 23:30
FromMichael Gogins
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
I have checked, and SWIG has produced a Lua wrapper for CsoundPerformanceThread. I haven't tried it, though.

Regards,
Mike


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


On Tue, Jul 23, 2013 at 3:34 PM, francesco <ilterzouomo@fastwebnet.it> wrote:
Yes You are right,
but i would need a more simple library for testing the use of FFI
and seemed to me that portsf (by Mr. Dobson) was perfect for my
need.

Here some FFI library (there is also libsndfile):
http://wiki.luajit.org/FFI-Bindings

Thanks,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725958.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"




Date2013-08-20 17:17
FromMark Brophy
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
I'd like to request a feature: a demo of csoundPerformKsmps in Lua that uses wxLua, lqt, or ltcltk to create a button that when pressed plays a 220 Hz sine using csoundScoreEvent. It would also be useful to add a second button that plays a 330 Hz sine wave so that it can be verified that both sounds can be played simultaneously. The demo would be useful for cross-platform GUI development, including Android and possibly iOS.



On Tue, Jul 23, 2013 at 11:31 AM, Michael Gogins <michael.gogins@gmail.com> wrote:
It's not going to be easy to use CsoundPerformanceThread with FFI, but I'll look into it. You could easily do it just with csoundPerformKsmps however. If your wLua GUI is not doing too much, you won't notice the difference compared to threading.

Regards,
Mike


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


On Tue, Jul 23, 2013 at 12:51 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'm trying to use LuaJIT and wxLua outside Csound to generate events in realtime with the Csound API and the FFI example Michael published and the CsoundPerformanceThread that you published.


On Tue, Jul 23, 2013 at 8:03 AM, francesco <ilterzouomo@fastwebnet.it> wrote:
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"






Date2013-08-20 20:09
FromMichael Gogins
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
I may provide such a demo. In the meantime, there are two basic choices. Either Csound runs in its own thread, using the CsoundPerformanceThread class, or it runs in the main GUI thread. If Csound runs in the main GUI thread, then you need to create a your own main event loop, which will replace the regular main event loop. First, the event loop will call csoundPerformKsmps() (if Csound is in fact running). Then, the GUI event queue will be polled for pending events, which will be handled if present. Then back to the top of the event queue. This is essentially what using a coroutine would do. In effect, Csound yields at the end of every kperiod.

I'm not sure whether threads or running in the main event loop is better, for your purposes. Maybe you should try both. T

he risk of using an event loop is too much stuff happening when it comes time to handle the GUI event queue, so that there is an audio dropout. I found that in fact this usually isn't a problem, but using threads ought to provide finer granularity and better sharing of work.  

The problem with using threads is that the Csound thread can't call safely into the main app's Lua runtime. So if you use, for example, the Lua opcodes, first you have to create a Lua state for your app, and then Csound will create a second Lua state for the Lua opcodes, and you will have to manage communications between these states.

If you use LuaJIT, then you can use Csound itself to write your entire app. This is actually a third basic choice. You can start up your GUI in the Csound orchestra by running Lua code inside a Lua opcode. In this case you would have a Csound instrument, always on, that would manage event dispatching for the GUI. This would essentially be doing the same job as the FLrun opcode does for the FLTK widgets.

A question for you: Are you using plain Lua, or LuaJIT? I found that LuaJIT is a drop-in replacement for Lua in wxLua. 

Please note, the Lua opcodes and LuaJIT already exist in the Csound6 Android app.

Hope this helps,
Mike


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


On Tue, Aug 20, 2013 at 12:17 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'd like to request a feature: a demo of csoundPerformKsmps in Lua that uses wxLua, lqt, or ltcltk to create a button that when pressed plays a 220 Hz sine using csoundScoreEvent. It would also be useful to add a second button that plays a 330 Hz sine wave so that it can be verified that both sounds can be played simultaneously. The demo would be useful for cross-platform GUI development, including Android and possibly iOS.



On Tue, Jul 23, 2013 at 11:31 AM, Michael Gogins <michael.gogins@gmail.com> wrote:
It's not going to be easy to use CsoundPerformanceThread with FFI, but I'll look into it. You could easily do it just with csoundPerformKsmps however. If your wLua GUI is not doing too much, you won't notice the difference compared to threading.

Regards,
Mike


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


On Tue, Jul 23, 2013 at 12:51 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'm trying to use LuaJIT and wxLua outside Csound to generate events in realtime with the Csound API and the FFI example Michael published and the CsoundPerformanceThread that you published.


On Tue, Jul 23, 2013 at 8:03 AM, francesco <ilterzouomo@fastwebnet.it> wrote:
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"







Date2013-08-21 01:31
FromMark Brophy
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
I'm using the LuaJIT that comes with the Windows version of Csound6 which I hope to port to other platforms. I haven't figured out how to replace the main GUI event thread with a different event loop. I'd like to skip the threads and use one of the other two methods.


On Tue, Aug 20, 2013 at 1:09 PM, Michael Gogins <michael.gogins@gmail.com> wrote:
I may provide such a demo. In the meantime, there are two basic choices. Either Csound runs in its own thread, using the CsoundPerformanceThread class, or it runs in the main GUI thread. If Csound runs in the main GUI thread, then you need to create a your own main event loop, which will replace the regular main event loop. First, the event loop will call csoundPerformKsmps() (if Csound is in fact running). Then, the GUI event queue will be polled for pending events, which will be handled if present. Then back to the top of the event queue. This is essentially what using a coroutine would do. In effect, Csound yields at the end of every kperiod.

I'm not sure whether threads or running in the main event loop is better, for your purposes. Maybe you should try both. T

he risk of using an event loop is too much stuff happening when it comes time to handle the GUI event queue, so that there is an audio dropout. I found that in fact this usually isn't a problem, but using threads ought to provide finer granularity and better sharing of work.  

The problem with using threads is that the Csound thread can't call safely into the main app's Lua runtime. So if you use, for example, the Lua opcodes, first you have to create a Lua state for your app, and then Csound will create a second Lua state for the Lua opcodes, and you will have to manage communications between these states.

If you use LuaJIT, then you can use Csound itself to write your entire app. This is actually a third basic choice. You can start up your GUI in the Csound orchestra by running Lua code inside a Lua opcode. In this case you would have a Csound instrument, always on, that would manage event dispatching for the GUI. This would essentially be doing the same job as the FLrun opcode does for the FLTK widgets.

A question for you: Are you using plain Lua, or LuaJIT? I found that LuaJIT is a drop-in replacement for Lua in wxLua. 

Please note, the Lua opcodes and LuaJIT already exist in the Csound6 Android app.

Hope this helps,
Mike


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


On Tue, Aug 20, 2013 at 12:17 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'd like to request a feature: a demo of csoundPerformKsmps in Lua that uses wxLua, lqt, or ltcltk to create a button that when pressed plays a 220 Hz sine using csoundScoreEvent. It would also be useful to add a second button that plays a 330 Hz sine wave so that it can be verified that both sounds can be played simultaneously. The demo would be useful for cross-platform GUI development, including Android and possibly iOS.



On Tue, Jul 23, 2013 at 11:31 AM, Michael Gogins <michael.gogins@gmail.com> wrote:
It's not going to be easy to use CsoundPerformanceThread with FFI, but I'll look into it. You could easily do it just with csoundPerformKsmps however. If your wLua GUI is not doing too much, you won't notice the difference compared to threading.

Regards,
Mike


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


On Tue, Jul 23, 2013 at 12:51 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'm trying to use LuaJIT and wxLua outside Csound to generate events in realtime with the Csound API and the FFI example Michael published and the CsoundPerformanceThread that you published.


On Tue, Jul 23, 2013 at 8:03 AM, francesco <ilterzouomo@fastwebnet.it> wrote:
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"








Date2013-08-21 02:13
FromMichael Gogins
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
Derive from wxApp and override the main loop method. In C++ like this, but you would do it in Lua:

MyClass : public wxApp;

MyClass::MainLoop()
{
while(running) {
  // If
  if (csoundIsRunning) {
    csoundPerformKsmps(csound);
  }
  while(Pending() {
    Dispatch();
}


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


On Tue, Aug 20, 2013 at 8:31 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'm using the LuaJIT that comes with the Windows version of Csound6 which I hope to port to other platforms. I haven't figured out how to replace the main GUI event thread with a different event loop. I'd like to skip the threads and use one of the other two methods.


On Tue, Aug 20, 2013 at 1:09 PM, Michael Gogins <michael.gogins@gmail.com> wrote:
I may provide such a demo. In the meantime, there are two basic choices. Either Csound runs in its own thread, using the CsoundPerformanceThread class, or it runs in the main GUI thread. If Csound runs in the main GUI thread, then you need to create a your own main event loop, which will replace the regular main event loop. First, the event loop will call csoundPerformKsmps() (if Csound is in fact running). Then, the GUI event queue will be polled for pending events, which will be handled if present. Then back to the top of the event queue. This is essentially what using a coroutine would do. In effect, Csound yields at the end of every kperiod.

I'm not sure whether threads or running in the main event loop is better, for your purposes. Maybe you should try both. T

he risk of using an event loop is too much stuff happening when it comes time to handle the GUI event queue, so that there is an audio dropout. I found that in fact this usually isn't a problem, but using threads ought to provide finer granularity and better sharing of work.  

The problem with using threads is that the Csound thread can't call safely into the main app's Lua runtime. So if you use, for example, the Lua opcodes, first you have to create a Lua state for your app, and then Csound will create a second Lua state for the Lua opcodes, and you will have to manage communications between these states.

If you use LuaJIT, then you can use Csound itself to write your entire app. This is actually a third basic choice. You can start up your GUI in the Csound orchestra by running Lua code inside a Lua opcode. In this case you would have a Csound instrument, always on, that would manage event dispatching for the GUI. This would essentially be doing the same job as the FLrun opcode does for the FLTK widgets.

A question for you: Are you using plain Lua, or LuaJIT? I found that LuaJIT is a drop-in replacement for Lua in wxLua. 

Please note, the Lua opcodes and LuaJIT already exist in the Csound6 Android app.

Hope this helps,
Mike


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


On Tue, Aug 20, 2013 at 12:17 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'd like to request a feature: a demo of csoundPerformKsmps in Lua that uses wxLua, lqt, or ltcltk to create a button that when pressed plays a 220 Hz sine using csoundScoreEvent. It would also be useful to add a second button that plays a 330 Hz sine wave so that it can be verified that both sounds can be played simultaneously. The demo would be useful for cross-platform GUI development, including Android and possibly iOS.



On Tue, Jul 23, 2013 at 11:31 AM, Michael Gogins <michael.gogins@gmail.com> wrote:
It's not going to be easy to use CsoundPerformanceThread with FFI, but I'll look into it. You could easily do it just with csoundPerformKsmps however. If your wLua GUI is not doing too much, you won't notice the difference compared to threading.

Regards,
Mike


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


On Tue, Jul 23, 2013 at 12:51 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'm trying to use LuaJIT and wxLua outside Csound to generate events in realtime with the Csound API and the FFI example Michael published and the CsoundPerformanceThread that you published.


On Tue, Jul 23, 2013 at 8:03 AM, francesco <ilterzouomo@fastwebnet.it> wrote:
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"









Date2013-08-21 16:58
FromMark Brophy
SubjectRe: [Csnd] Re: How can Csound yield the processor for a coroutine in Lua?
If I'm using Lua from inside Csound via an opcode, do I need to define the following FFI functions?
ffi.cdef[[
int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
int csoundStart(void *csound);
int csoundPerformKsmps(void *csound);
int csoundPerform(void *csound);
int csoundInitialize (int *argc, char ***argv, int flags);
]]



On Tue, Aug 20, 2013 at 7:13 PM, Michael Gogins <michael.gogins@gmail.com> wrote:
Derive from wxApp and override the main loop method. In C++ like this, but you would do it in Lua:

MyClass : public wxApp;

MyClass::MainLoop()
{
while(running) {
  // If
  if (csoundIsRunning) {
    csoundPerformKsmps(csound);
  }
  while(Pending() {
    Dispatch();
}


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


On Tue, Aug 20, 2013 at 8:31 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'm using the LuaJIT that comes with the Windows version of Csound6 which I hope to port to other platforms. I haven't figured out how to replace the main GUI event thread with a different event loop. I'd like to skip the threads and use one of the other two methods.


On Tue, Aug 20, 2013 at 1:09 PM, Michael Gogins <michael.gogins@gmail.com> wrote:
I may provide such a demo. In the meantime, there are two basic choices. Either Csound runs in its own thread, using the CsoundPerformanceThread class, or it runs in the main GUI thread. If Csound runs in the main GUI thread, then you need to create a your own main event loop, which will replace the regular main event loop. First, the event loop will call csoundPerformKsmps() (if Csound is in fact running). Then, the GUI event queue will be polled for pending events, which will be handled if present. Then back to the top of the event queue. This is essentially what using a coroutine would do. In effect, Csound yields at the end of every kperiod.

I'm not sure whether threads or running in the main event loop is better, for your purposes. Maybe you should try both. T

he risk of using an event loop is too much stuff happening when it comes time to handle the GUI event queue, so that there is an audio dropout. I found that in fact this usually isn't a problem, but using threads ought to provide finer granularity and better sharing of work.  

The problem with using threads is that the Csound thread can't call safely into the main app's Lua runtime. So if you use, for example, the Lua opcodes, first you have to create a Lua state for your app, and then Csound will create a second Lua state for the Lua opcodes, and you will have to manage communications between these states.

If you use LuaJIT, then you can use Csound itself to write your entire app. This is actually a third basic choice. You can start up your GUI in the Csound orchestra by running Lua code inside a Lua opcode. In this case you would have a Csound instrument, always on, that would manage event dispatching for the GUI. This would essentially be doing the same job as the FLrun opcode does for the FLTK widgets.

A question for you: Are you using plain Lua, or LuaJIT? I found that LuaJIT is a drop-in replacement for Lua in wxLua. 

Please note, the Lua opcodes and LuaJIT already exist in the Csound6 Android app.

Hope this helps,
Mike


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


On Tue, Aug 20, 2013 at 12:17 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'd like to request a feature: a demo of csoundPerformKsmps in Lua that uses wxLua, lqt, or ltcltk to create a button that when pressed plays a 220 Hz sine using csoundScoreEvent. It would also be useful to add a second button that plays a 330 Hz sine wave so that it can be verified that both sounds can be played simultaneously. The demo would be useful for cross-platform GUI development, including Android and possibly iOS.



On Tue, Jul 23, 2013 at 11:31 AM, Michael Gogins <michael.gogins@gmail.com> wrote:
It's not going to be easy to use CsoundPerformanceThread with FFI, but I'll look into it. You could easily do it just with csoundPerformKsmps however. If your wLua GUI is not doing too much, you won't notice the difference compared to threading.

Regards,
Mike


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


On Tue, Jul 23, 2013 at 12:51 PM, Mark Brophy <mark@brophyworld.com> wrote:
I'm trying to use LuaJIT and wxLua outside Csound to generate events in realtime with the Csound API and the FFI example Michael published and the CsoundPerformanceThread that you published.


On Tue, Jul 23, 2013 at 8:03 AM, francesco <ilterzouomo@fastwebnet.it> wrote:
Hi,
for my personal curiosity may i ask how are You using Lua?
And what GUI library?

Thank You,
ciao,
francesco.




--
View this message in context: http://csound.1045644.n5.nabble.com/How-can-Csound-yield-the-processor-for-a-coroutine-in-Lua-tp5725940p5725945.html
Sent from the Csound - General mailing list archive at Nabble.com.


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"