[Csnd] Create a performance thread in a Lua through the API

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

[Csnd] Create a performance thread in a Lua through the API

Johann Philippe-2
Hello all,

I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
finished, i'm writing the piece of code to connect the program to the Csound
API.
I finally could play my first sine wave on this program yesterday night. The
problem is that the GUI is freezing as long as Csound is playing. The
example i'm based on is the luaffi.lua in csound examples.
I guess it's freezing because there is no performance thread separate from
the main thread.

So i tried to create this performance thread with "csoundCreateThread" from
the API. I could find some examples in C here :
https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
and here : http://write.flossmanuals.net/csound/a-the-csound-api/
I tried to adapt those examples to Lua. But doing this through the foreign
function interface is not that simple for me. I tried a thousand of
possibilities, trying to understand the connection between C and lua ffi.
Yet it's not working now.

So, maybe someone already tried this ?
The main issue is about the arguments passed to csoundCreateThread. The
first needs to be a function right ? But, i think it's just taking a pointer
to a C function for the main performance loop. And i couldn't figure out how
to declare this function in C. Or is it possible to pass a Lua function as
argument ?
And what about the second argument ?
I have to say that i never worked with C programming, so it's quite a
complex world for me.
I guess another solution would be to declare the performance function in a C
library, and to import it with ffi.load, define it with ffi.cdef etc... But
i was wondering if there is a way to do this directly in the lua code.

If someone had a small example about threads in lua Api of Csound, it would
be great.
And i'll let you know as soon as the program is ready !

Thanking you in advance

Johann






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

Csound mailing list
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Steven Yi
Hi Johann,

I think you'll probably want to use the CsoundPerformanceThread.
There's Lua examples for it here:

https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua

This is higher-level than csoundCreateThread and should be already
setup for doing the kind of thing you want. It should correspond to
this C++ class:

https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp

steven
On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
<[hidden email]> wrote:

>
> Hello all,
>
> I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> finished, i'm writing the piece of code to connect the program to the Csound
> API.
> I finally could play my first sine wave on this program yesterday night. The
> problem is that the GUI is freezing as long as Csound is playing. The
> example i'm based on is the luaffi.lua in csound examples.
> I guess it's freezing because there is no performance thread separate from
> the main thread.
>
> So i tried to create this performance thread with "csoundCreateThread" from
> the API. I could find some examples in C here :
> https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> I tried to adapt those examples to Lua. But doing this through the foreign
> function interface is not that simple for me. I tried a thousand of
> possibilities, trying to understand the connection between C and lua ffi.
> Yet it's not working now.
>
> So, maybe someone already tried this ?
> The main issue is about the arguments passed to csoundCreateThread. The
> first needs to be a function right ? But, i think it's just taking a pointer
> to a C function for the main performance loop. And i couldn't figure out how
> to declare this function in C. Or is it possible to pass a Lua function as
> argument ?
> And what about the second argument ?
> I have to say that i never worked with C programming, so it's quite a
> complex world for me.
> I guess another solution would be to declare the performance function in a C
> library, and to import it with ffi.load, define it with ffi.cdef etc... But
> i was wondering if there is a way to do this directly in the lua code.
>
> If someone had a small example about threads in lua Api of Csound, it would
> be great.
> And i'll let you know as soon as the program is ready !
>
> Thanking you in advance
>
> Johann
>
>
>
>
>
>
> --
> Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>
> Csound mailing list
> [hidden email]
> 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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Johann Philippe-2

Hello Steven,

Thank you for the answer.
I'm trying this way, and it doesn't seem to work here.
I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?




ffi.cdef[[
int csoundGetVersion();
int csoundGetAPIVersion();
int csoundInitialize (int *argc, char ***argv, int flags);
void *csoundCreate(void *hostdata);
void csoundDestroy(void *csound);
int csoundSetOption(void *csound, const char *option);
int csoundCompileOrc(void *csound, const char *orc);
int csoundCompileCsd(void *csound, const char *csd_filename);
int csoundReadScore(void *csound, const char *message);
int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
int csoundStart(void *csound);
int csoundPerformKsmps(void *csound);
int csoundPerform(void *csound);
void csoundSetControlChannel(void *csound, const char *name, double value);
int csoundStop(void *csound);
int csoundReset(void *csound);
void csoundGetCurrentThreadId(void *csound);
void csoundPerformanceThread(void *csound);
]]


csoundApi = ffi.load(pathtocsound64)

local argc = ffi.new('int *')
local argv = ffi.new('char ***')

self.result = csoundApi.csoundInitialize(argc, argv, 0);

voidptr = ffi.new('void *')
print 'Creating an instance of Csound...'

self.csound=csoundApi.csoundCreate(voidptr)


csoundApi.csoundSetOption(self.csound, '--sample-accurate')
csoundApi.csoundSetOption(self.csound, '--output=dac')
csoundApi.csoundSetOption(self.csound, '--format=float')
csoundApi.csoundSetOption(self.csound, '--nodisplays')

csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
csoundApi.csoundReadScore(self.csound,tostring(score))
self.result = csoundApi.csoundStart(self.csound)
self.thread=csnd6.csoundPerformanceThread(self.csound)





Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :


Hi Johann,

I think you'll probably want to use the CsoundPerformanceThread.
There's Lua examples for it here:

https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua

This is higher-level than csoundCreateThread and should be already
setup for doing the kind of thing you want. It should correspond to
this C++ class:

https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp

steven
On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
<[hidden email]> wrote:

>
> Hello all,
>
> I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> finished, i'm writing the piece of code to connect the program to the Csound
> API.
> I finally could play my first sine wave on this program yesterday night. The
> problem is that the GUI is freezing as long as Csound is playing. The
> example i'm based on is the luaffi.lua in csound examples.
> I guess it's freezing because there is no performance thread separate from
> the main thread.
>
> So i tried to create this performance thread with "csoundCreateThread" from
> the API. I could find some examples in C here :
> https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> I tried to adapt those examples to Lua. But doing this through the foreign
> function interface is not that simple for me. I tried a thousand of
> possibilities, trying to understand the connection between C and lua ffi.
> Yet it's not working now.
>
> So, maybe someone already tried this ?
> The main issue is about the arguments passed to csoundCreateThread. The
> first needs to be a function right ? But, i think it's just taking a pointer
> to a C function for the main performance loop. And i couldn't figure out how
> to declare this function in C. Or is it possible to pass a Lua function as
> argument ?
> And what about the second argument ?
> I have to say that i never worked with C programming, so it's quite a
> complex world for me.
> I guess another solution would be to declare the performance function in a C
> library, and to import it with ffi.load, define it with ffi.cdef etc... But
> i was wondering if there is a way to do this directly in the lua code.
>
> If someone had a small example about threads in lua Api of Csound, it would
> be great.
> And i'll let you know as soon as the program is ready !
>
> Thanking you in advance
>
> Johann
>
>
>
>
>
>
> --
> Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>
> Csound mailing list
> [hidden email]
> 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
[hidden email]
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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Michael Gogins-2
Lua itself is not thread safe, so the FFI approach is tricky. You can run the Csound thread class from Lua. That is what I would do. 

You could also run the performance thread in Lua, and dispatch windows events every kperiod.

Regards, 
Mike

On Tue, Sep 25, 2018, 08:26 Johann Philippe <[hidden email]> wrote:

Hello Steven,

Thank you for the answer.
I'm trying this way, and it doesn't seem to work here.
I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?




ffi.cdef[[
int csoundGetVersion();
int csoundGetAPIVersion();
int csoundInitialize (int *argc, char ***argv, int flags);
void *csoundCreate(void *hostdata);
void csoundDestroy(void *csound);
int csoundSetOption(void *csound, const char *option);
int csoundCompileOrc(void *csound, const char *orc);
int csoundCompileCsd(void *csound, const char *csd_filename);
int csoundReadScore(void *csound, const char *message);
int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
int csoundStart(void *csound);
int csoundPerformKsmps(void *csound);
int csoundPerform(void *csound);
void csoundSetControlChannel(void *csound, const char *name, double value);
int csoundStop(void *csound);
int csoundReset(void *csound);
void csoundGetCurrentThreadId(void *csound);
void csoundPerformanceThread(void *csound);
]]


csoundApi = ffi.load(pathtocsound64)

local argc = ffi.new('int *')
local argv = ffi.new('char ***')

self.result = csoundApi.csoundInitialize(argc, argv, 0);

voidptr = ffi.new('void *')
print 'Creating an instance of Csound...'

self.csound=csoundApi.csoundCreate(voidptr)


csoundApi.csoundSetOption(self.csound, '--sample-accurate')
csoundApi.csoundSetOption(self.csound, '--output=dac')
csoundApi.csoundSetOption(self.csound, '--format=float')
csoundApi.csoundSetOption(self.csound, '--nodisplays')

csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
csoundApi.csoundReadScore(self.csound,tostring(score))
self.result = csoundApi.csoundStart(self.csound)
self.thread=csnd6.csoundPerformanceThread(self.csound)





Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :


Hi Johann,

I think you'll probably want to use the CsoundPerformanceThread.
There's Lua examples for it here:

https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua

This is higher-level than csoundCreateThread and should be already
setup for doing the kind of thing you want. It should correspond to
this C++ class:

https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp

steven
On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
<[hidden email]> wrote:

>
> Hello all,
>
> I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> finished, i'm writing the piece of code to connect the program to the Csound
> API.
> I finally could play my first sine wave on this program yesterday night. The
> problem is that the GUI is freezing as long as Csound is playing. The
> example i'm based on is the luaffi.lua in csound examples.
> I guess it's freezing because there is no performance thread separate from
> the main thread.
>
> So i tried to create this performance thread with "csoundCreateThread" from
> the API. I could find some examples in C here :
> https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> I tried to adapt those examples to Lua. But doing this through the foreign
> function interface is not that simple for me. I tried a thousand of
> possibilities, trying to understand the connection between C and lua ffi.
> Yet it's not working now.
>
> So, maybe someone already tried this ?
> The main issue is about the arguments passed to csoundCreateThread. The
> first needs to be a function right ? But, i think it's just taking a pointer
> to a C function for the main performance loop. And i couldn't figure out how
> to declare this function in C. Or is it possible to pass a Lua function as
> argument ?
> And what about the second argument ?
> I have to say that i never worked with C programming, so it's quite a
> complex world for me.
> I guess another solution would be to declare the performance function in a C
> library, and to import it with ffi.load, define it with ffi.cdef etc... But
> i was wondering if there is a way to do this directly in the lua code.
>
> If someone had a small example about threads in lua Api of Csound, it would
> be great.
> And i'll let you know as soon as the program is ready !
>
> Thanking you in advance
>
> Johann
>
>
>
>
>
>
> --
> Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>
> Csound mailing list
> [hidden email]
> 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
[hidden email]
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 [hidden email] 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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Johann Philippe-2
I'm not sure to understand. Your first proposition seems interesting, but by "csound thread class", do you mean the c++ class Steven sent earlier ? I'm not sure how to do that with Lua.
Or is it the csoundCreateThread and basic Api functions ?

Thanks in advance

Le mardi 25 septembre 2018 à 14:55:40 UTC+2, Michael Gogins <[hidden email]> a écrit :


Lua itself is not thread safe, so the FFI approach is tricky. You can run the Csound thread class from Lua. That is what I would do. 

You could also run the performance thread in Lua, and dispatch windows events every kperiod.

Regards, 
Mike

On Tue, Sep 25, 2018, 08:26 Johann Philippe <[hidden email]> wrote:

Hello Steven,

Thank you for the answer.
I'm trying this way, and it doesn't seem to work here.
I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?




ffi.cdef[[
int csoundGetVersion();
int csoundGetAPIVersion();
int csoundInitialize (int *argc, char ***argv, int flags);
void *csoundCreate(void *hostdata);
void csoundDestroy(void *csound);
int csoundSetOption(void *csound, const char *option);
int csoundCompileOrc(void *csound, const char *orc);
int csoundCompileCsd(void *csound, const char *csd_filename);
int csoundReadScore(void *csound, const char *message);
int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
int csoundStart(void *csound);
int csoundPerformKsmps(void *csound);
int csoundPerform(void *csound);
void csoundSetControlChannel(void *csound, const char *name, double value);
int csoundStop(void *csound);
int csoundReset(void *csound);
void csoundGetCurrentThreadId(void *csound);
void csoundPerformanceThread(void *csound);
]]


csoundApi = ffi.load(pathtocsound64)

local argc = ffi.new('int *')
local argv = ffi.new('char ***')

self.result = csoundApi.csoundInitialize(argc, argv, 0);

voidptr = ffi.new('void *')
print 'Creating an instance of Csound...'

self.csound=csoundApi.csoundCreate(voidptr)


csoundApi.csoundSetOption(self.csound, '--sample-accurate')
csoundApi.csoundSetOption(self.csound, '--output=dac')
csoundApi.csoundSetOption(self.csound, '--format=float')
csoundApi.csoundSetOption(self.csound, '--nodisplays')

csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
csoundApi.csoundReadScore(self.csound,tostring(score))
self.result = csoundApi.csoundStart(self.csound)
self.thread=csnd6.csoundPerformanceThread(self.csound)





Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :


Hi Johann,

I think you'll probably want to use the CsoundPerformanceThread.
There's Lua examples for it here:

https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua

This is higher-level than csoundCreateThread and should be already
setup for doing the kind of thing you want. It should correspond to
this C++ class:

https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp

steven
On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
<[hidden email]> wrote:

>
> Hello all,
>
> I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> finished, i'm writing the piece of code to connect the program to the Csound
> API.
> I finally could play my first sine wave on this program yesterday night. The
> problem is that the GUI is freezing as long as Csound is playing. The
> example i'm based on is the luaffi.lua in csound examples.
> I guess it's freezing because there is no performance thread separate from
> the main thread.
>
> So i tried to create this performance thread with "csoundCreateThread" from
> the API. I could find some examples in C here :
> https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> I tried to adapt those examples to Lua. But doing this through the foreign
> function interface is not that simple for me. I tried a thousand of
> possibilities, trying to understand the connection between C and lua ffi.
> Yet it's not working now.
>
> So, maybe someone already tried this ?
> The main issue is about the arguments passed to csoundCreateThread. The
> first needs to be a function right ? But, i think it's just taking a pointer
> to a C function for the main performance loop. And i couldn't figure out how
> to declare this function in C. Or is it possible to pass a Lua function as
> argument ?
> And what about the second argument ?
> I have to say that i never worked with C programming, so it's quite a
> complex world for me.
> I guess another solution would be to declare the performance function in a C
> library, and to import it with ffi.load, define it with ffi.cdef etc... But
> i was wondering if there is a way to do this directly in the lua code.
>
> If someone had a small example about threads in lua Api of Csound, it would
> be great.
> And i'll let you know as soon as the program is ready !
>
> Thanking you in advance
>
> Johann
>
>
>
>
>
>
> --
> Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>
> Csound mailing list
> [hidden email]
> 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
[hidden email]
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 [hidden email] 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 [hidden email] 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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Michael Gogins-2
Yes I meant that class. 

If you can write C and C++, write a C language wrapper for the header file only CsoundThreaded class in csound_threaded.hpp, which is a simpler alternative to the class Steven is talking about. Then use Lua FFI to call your C language wrapper. 

Regards, 
Mike

On Tue, Sep 25, 2018, 09:14 Johann Philippe <[hidden email]> wrote:
I'm not sure to understand. Your first proposition seems interesting, but by "csound thread class", do you mean the c++ class Steven sent earlier ? I'm not sure how to do that with Lua.
Or is it the csoundCreateThread and basic Api functions ?

Thanks in advance

Le mardi 25 septembre 2018 à 14:55:40 UTC+2, Michael Gogins <[hidden email]> a écrit :


Lua itself is not thread safe, so the FFI approach is tricky. You can run the Csound thread class from Lua. That is what I would do. 

You could also run the performance thread in Lua, and dispatch windows events every kperiod.

Regards, 
Mike

On Tue, Sep 25, 2018, 08:26 Johann Philippe <[hidden email]> wrote:

Hello Steven,

Thank you for the answer.
I'm trying this way, and it doesn't seem to work here.
I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?




ffi.cdef[[
int csoundGetVersion();
int csoundGetAPIVersion();
int csoundInitialize (int *argc, char ***argv, int flags);
void *csoundCreate(void *hostdata);
void csoundDestroy(void *csound);
int csoundSetOption(void *csound, const char *option);
int csoundCompileOrc(void *csound, const char *orc);
int csoundCompileCsd(void *csound, const char *csd_filename);
int csoundReadScore(void *csound, const char *message);
int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
int csoundStart(void *csound);
int csoundPerformKsmps(void *csound);
int csoundPerform(void *csound);
void csoundSetControlChannel(void *csound, const char *name, double value);
int csoundStop(void *csound);
int csoundReset(void *csound);
void csoundGetCurrentThreadId(void *csound);
void csoundPerformanceThread(void *csound);
]]


csoundApi = ffi.load(pathtocsound64)

local argc = ffi.new('int *')
local argv = ffi.new('char ***')

self.result = csoundApi.csoundInitialize(argc, argv, 0);

voidptr = ffi.new('void *')
print 'Creating an instance of Csound...'

self.csound=csoundApi.csoundCreate(voidptr)


csoundApi.csoundSetOption(self.csound, '--sample-accurate')
csoundApi.csoundSetOption(self.csound, '--output=dac')
csoundApi.csoundSetOption(self.csound, '--format=float')
csoundApi.csoundSetOption(self.csound, '--nodisplays')

csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
csoundApi.csoundReadScore(self.csound,tostring(score))
self.result = csoundApi.csoundStart(self.csound)
self.thread=csnd6.csoundPerformanceThread(self.csound)





Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :


Hi Johann,

I think you'll probably want to use the CsoundPerformanceThread.
There's Lua examples for it here:

https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua

This is higher-level than csoundCreateThread and should be already
setup for doing the kind of thing you want. It should correspond to
this C++ class:

https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp

steven
On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
<[hidden email]> wrote:

>
> Hello all,
>
> I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> finished, i'm writing the piece of code to connect the program to the Csound
> API.
> I finally could play my first sine wave on this program yesterday night. The
> problem is that the GUI is freezing as long as Csound is playing. The
> example i'm based on is the luaffi.lua in csound examples.
> I guess it's freezing because there is no performance thread separate from
> the main thread.
>
> So i tried to create this performance thread with "csoundCreateThread" from
> the API. I could find some examples in C here :
> https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> I tried to adapt those examples to Lua. But doing this through the foreign
> function interface is not that simple for me. I tried a thousand of
> possibilities, trying to understand the connection between C and lua ffi.
> Yet it's not working now.
>
> So, maybe someone already tried this ?
> The main issue is about the arguments passed to csoundCreateThread. The
> first needs to be a function right ? But, i think it's just taking a pointer
> to a C function for the main performance loop. And i couldn't figure out how
> to declare this function in C. Or is it possible to pass a Lua function as
> argument ?
> And what about the second argument ?
> I have to say that i never worked with C programming, so it's quite a
> complex world for me.
> I guess another solution would be to declare the performance function in a C
> library, and to import it with ffi.load, define it with ffi.cdef etc... But
> i was wondering if there is a way to do this directly in the lua code.
>
> If someone had a small example about threads in lua Api of Csound, it would
> be great.
> And i'll let you know as soon as the program is ready !
>
> Thanking you in advance
>
> Johann
>
>
>
>
>
>
> --
> Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>
> Csound mailing list
> [hidden email]
> 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
[hidden email]
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 [hidden email] 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 [hidden email] 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 [hidden email] 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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Steven Yi
In reply to this post by Johann Philippe-2
Hi Johann,

Instead of using libcsound64, try using libcsnd6. The definition for it is here:

https://github.com/csound/csound/blob/develop/interfaces/CMakeLists.txt

that describes which files are used. It's the same library used by the
other interface bindings and adds some C++ classes like the
CsoundPerformanceThread.

If you use this:

https://github.com/csound/csound/blob/develop/interfaces/CppSound.hpp

together with the getCsound() to use the standard API, it might work
for you there.

steven
On Tue, Sep 25, 2018 at 8:26 AM Johann Philippe
<[hidden email]> wrote:

>
>
> Hello Steven,
>
> Thank you for the answer.
> I'm trying this way, and it doesn't seem to work here.
> I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
> Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?
>
>
>
>
> ffi.cdef[[
> int csoundGetVersion();
> int csoundGetAPIVersion();
> int csoundInitialize (int *argc, char ***argv, int flags);
> void *csoundCreate(void *hostdata);
> void csoundDestroy(void *csound);
> int csoundSetOption(void *csound, const char *option);
> int csoundCompileOrc(void *csound, const char *orc);
> int csoundCompileCsd(void *csound, const char *csd_filename);
> int csoundReadScore(void *csound, const char *message);
> int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
> int csoundStart(void *csound);
> int csoundPerformKsmps(void *csound);
> int csoundPerform(void *csound);
> void csoundSetControlChannel(void *csound, const char *name, double value);
> int csoundStop(void *csound);
> int csoundReset(void *csound);
> void csoundGetCurrentThreadId(void *csound);
> void csoundPerformanceThread(void *csound);
> ]]
>
> csoundApi = ffi.load(pathtocsound64)
>
> local argc = ffi.new('int *')
> local argv = ffi.new('char ***')
>
> self.result = csoundApi.csoundInitialize(argc, argv, 0);
>
> voidptr = ffi.new('void *')
> print 'Creating an instance of Csound...'
>
> self.csound=csoundApi.csoundCreate(voidptr)
>
>
> csoundApi.csoundSetOption(self.csound, '--sample-accurate')
> csoundApi.csoundSetOption(self.csound, '--output=dac')
> csoundApi.csoundSetOption(self.csound, '--format=float')
> csoundApi.csoundSetOption(self.csound, '--nodisplays')
>
> csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
> csoundApi.csoundReadScore(self.csound,tostring(score))
> self.result = csoundApi.csoundStart(self.csound)
> self.thread=csnd6.csoundPerformanceThread(self.csound)
>
>
>
>
>
> Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :
>
>
> Hi Johann,
>
> I think you'll probably want to use the CsoundPerformanceThread.
> There's Lua examples for it here:
>
> https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua
>
> This is higher-level than csoundCreateThread and should be already
> setup for doing the kind of thing you want. It should correspond to
> this C++ class:
>
> https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp
>
> steven
> On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
> <[hidden email]> wrote:
> >
> > Hello all,
> >
> > I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> > I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> > finished, i'm writing the piece of code to connect the program to the Csound
> > API.
> > I finally could play my first sine wave on this program yesterday night. The
> > problem is that the GUI is freezing as long as Csound is playing. The
> > example i'm based on is the luaffi.lua in csound examples.
> > I guess it's freezing because there is no performance thread separate from
> > the main thread.
> >
> > So i tried to create this performance thread with "csoundCreateThread" from
> > the API. I could find some examples in C here :
> > https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> > and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> > I tried to adapt those examples to Lua. But doing this through the foreign
> > function interface is not that simple for me. I tried a thousand of
> > possibilities, trying to understand the connection between C and lua ffi.
> > Yet it's not working now.
> >
> > So, maybe someone already tried this ?
> > The main issue is about the arguments passed to csoundCreateThread. The
> > first needs to be a function right ? But, i think it's just taking a pointer
> > to a C function for the main performance loop. And i couldn't figure out how
> > to declare this function in C. Or is it possible to pass a Lua function as
> > argument ?
> > And what about the second argument ?
> > I have to say that i never worked with C programming, so it's quite a
> > complex world for me.
> > I guess another solution would be to declare the performance function in a C
> > library, and to import it with ffi.load, define it with ffi.cdef etc... But
> > i was wondering if there is a way to do this directly in the lua code.
> >
> > If someone had a small example about threads in lua Api of Csound, it would
> > be great.
> > And i'll let you know as soon as the program is ready !
> >
> > Thanking you in advance
> >
> > Johann
> >
> >
> >
> >
> >
> >
> > --
> > Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
> >
> > Csound mailing list
> > [hidden email]
> > 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
> [hidden email]
> 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 [hidden email] 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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Johann Philippe-2
Thanks for the answers.
I'm not sure to understand well - can i do this directly from the ffi ? And, in this case, what should i write in the ffi.cdef ?
I've been trying with definitions in ffi.cdef like this :
ffi.cdef[[
virtual   CSOUND *GetCsound();
]]
and the definition is wrong for luajit


or even like this
ffi.cdef[[
void *GetCsound();
]]
and the definition is right, but the calling of GetCsound is not working then.

I also tried like a billion other things, with no more result.

And i can't do like on the first example (the example here https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua ) because, as I said, no luaCsnd6. And require"csnd6" is not right for luajit (cannot find the specified procedure - i guess luacsnd6 was a specific binding of this library for lua).

Or maybe you're telling me that i should write something in c++ to make the connection ?

Sorry guys, i'm not really aware of those questions... 








Le mardi 25 septembre 2018 à 16:04:22 UTC+2, Steven Yi <[hidden email]> a écrit :


Hi Johann,

Instead of using libcsound64, try using libcsnd6. The definition for it is here:

https://github.com/csound/csound/blob/develop/interfaces/CMakeLists.txt

that describes which files are used. It's the same library used by the
other interface bindings and adds some C++ classes like the
CsoundPerformanceThread.

If you use this:

https://github.com/csound/csound/blob/develop/interfaces/CppSound.hpp

together with the getCsound() to use the standard API, it might work
for you there.

steven
On Tue, Sep 25, 2018 at 8:26 AM Johann Philippe
<[hidden email]> wrote:

>
>
> Hello Steven,
>
> Thank you for the answer.
> I'm trying this way, and it doesn't seem to work here.
> I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
> Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?
>
>
>
>
> ffi.cdef[[
> int csoundGetVersion();
> int csoundGetAPIVersion();
> int csoundInitialize (int *argc, char ***argv, int flags);
> void *csoundCreate(void *hostdata);
> void csoundDestroy(void *csound);
> int csoundSetOption(void *csound, const char *option);
> int csoundCompileOrc(void *csound, const char *orc);
> int csoundCompileCsd(void *csound, const char *csd_filename);
> int csoundReadScore(void *csound, const char *message);
> int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
> int csoundStart(void *csound);
> int csoundPerformKsmps(void *csound);
> int csoundPerform(void *csound);
> void csoundSetControlChannel(void *csound, const char *name, double value);
> int csoundStop(void *csound);
> int csoundReset(void *csound);
> void csoundGetCurrentThreadId(void *csound);
> void csoundPerformanceThread(void *csound);
> ]]
>
> csoundApi = ffi.load(pathtocsound64)
>
> local argc = ffi.new('int *')
> local argv = ffi.new('char ***')
>
> self.result = csoundApi.csoundInitialize(argc, argv, 0);
>
> voidptr = ffi.new('void *')
> print 'Creating an instance of Csound...'
>
> self.csound=csoundApi.csoundCreate(voidptr)
>
>
> csoundApi.csoundSetOption(self.csound, '--sample-accurate')
> csoundApi.csoundSetOption(self.csound, '--output=dac')
> csoundApi.csoundSetOption(self.csound, '--format=float')
> csoundApi.csoundSetOption(self.csound, '--nodisplays')
>
> csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
> csoundApi.csoundReadScore(self.csound,tostring(score))
> self.result = csoundApi.csoundStart(self.csound)
> self.thread=csnd6.csoundPerformanceThread(self.csound)
>
>
>
>
>
> Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :
>
>
> Hi Johann,
>
> I think you'll probably want to use the CsoundPerformanceThread.
> There's Lua examples for it here:
>
> https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua
>
> This is higher-level than csoundCreateThread and should be already
> setup for doing the kind of thing you want. It should correspond to
> this C++ class:
>
> https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp
>
> steven
> On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
> <[hidden email]> wrote:
> >
> > Hello all,
> >
> > I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> > I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> > finished, i'm writing the piece of code to connect the program to the Csound
> > API.
> > I finally could play my first sine wave on this program yesterday night. The
> > problem is that the GUI is freezing as long as Csound is playing. The
> > example i'm based on is the luaffi.lua in csound examples.
> > I guess it's freezing because there is no performance thread separate from
> > the main thread.
> >
> > So i tried to create this performance thread with "csoundCreateThread" from
> > the API. I could find some examples in C here :
> > https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> > and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> > I tried to adapt those examples to Lua. But doing this through the foreign
> > function interface is not that simple for me. I tried a thousand of
> > possibilities, trying to understand the connection between C and lua ffi.
> > Yet it's not working now.
> >
> > So, maybe someone already tried this ?
> > The main issue is about the arguments passed to csoundCreateThread. The
> > first needs to be a function right ? But, i think it's just taking a pointer
> > to a C function for the main performance loop. And i couldn't figure out how
> > to declare this function in C. Or is it possible to pass a Lua function as
> > argument ?
> > And what about the second argument ?
> > I have to say that i never worked with C programming, so it's quite a
> > complex world for me.
> > I guess another solution would be to declare the performance function in a C
> > library, and to import it with ffi.load, define it with ffi.cdef etc... But
> > i was wondering if there is a way to do this directly in the lua code.
> >
> > If someone had a small example about threads in lua Api of Csound, it would
> > be great.
> > And i'll let you know as soon as the program is ready !
> >
> > Thanking you in advance
> >
> > Johann
> >
> >
> >
> >
> >
> >
> > --
> > Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
> >
> > Csound mailing list
> > [hidden email]
> > 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
> [hidden email]
> 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 [hidden email] 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
[hidden email]
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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Michael Gogins-2
OK, I see your problem. You can't use a C++ definition for a Lua cdef
without being a bit of a C++ and Lua expert. That's not I was getting
at.

I was suggesting wrapping these C++ declarations (from csound_theaded.hpp)

CsoundThreaded();
virtual int CsoundThreaded::CompileCsdText(const char *text);
CsoundThreaded:;Start();

in this C++ code that has a C interface:

extern "C" {
  void *CsoundThreaded_Create() {
    return new CsoundThreaded;
  int CsoundThreaded_CompileCsdText(void *csound, const char *text) {
   return (CsoundThreaded *)csound)->CompileCsdText(text);
 }
 int CsoundThreaded_Start(void *csond) {
  return ((CsoundThreaded *)csound)->Start();
}

You would compile this into a little DLL called CsoundThreadedAPI. And
the Lua CDEF would just be:
ffi.cdef[[
 void *CsoundThreaded_Create();
  int CsoundThreaded_CompileCsdText(void *csound, const char *text);
 int CsoundThreaded_Start(void *csound);
]]
local CsoundThreadedAPI = ffi.load('CsoundThreadedAPI()

Other functions such as channels could also be done this way.

So in Lua you would just do this:

local csound = CsoundThreadedAPI.CsoundThreaded_Create()
local result = CsoundThreadedAPI.CsoundThreaded_CompileCsdText(csound, text)
-- Runs in separate C++ thread:
local result = CsoundThreadedAPI.CsoundThreaded_Start(csound)





-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com
On Tue, Sep 25, 2018 at 6:46 PM Johann Philippe
<[hidden email]> wrote:

>
> Thanks for the answers.
> I'm not sure to understand well - can i do this directly from the ffi ? And, in this case, what should i write in the ffi.cdef ?
> I've been trying with definitions in ffi.cdef like this :
> ffi.cdef[[
> virtual   CSOUND *GetCsound();
> ]]
> and the definition is wrong for luajit
>
>
> or even like this
> ffi.cdef[[
> void *GetCsound();
> ]]
> and the definition is right, but the calling of GetCsound is not working then.
>
> I also tried like a billion other things, with no more result.
>
> And i can't do like on the first example (the example here https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua ) because, as I said, no luaCsnd6. And require"csnd6" is not right for luajit (cannot find the specified procedure - i guess luacsnd6 was a specific binding of this library for lua).
>
> Or maybe you're telling me that i should write something in c++ to make the connection ?
>
> Sorry guys, i'm not really aware of those questions...
>
>
>
>
>
>
>
>
> Le mardi 25 septembre 2018 à 16:04:22 UTC+2, Steven Yi <[hidden email]> a écrit :
>
>
> Hi Johann,
>
> Instead of using libcsound64, try using libcsnd6. The definition for it is here:
>
> https://github.com/csound/csound/blob/develop/interfaces/CMakeLists.txt
>
> that describes which files are used. It's the same library used by the
> other interface bindings and adds some C++ classes like the
> CsoundPerformanceThread.
>
> If you use this:
>
> https://github.com/csound/csound/blob/develop/interfaces/CppSound.hpp
>
> together with the getCsound() to use the standard API, it might work
> for you there.
>
> steven
> On Tue, Sep 25, 2018 at 8:26 AM Johann Philippe
> <[hidden email]> wrote:
> >
> >
> > Hello Steven,
> >
> > Thank you for the answer.
> > I'm trying this way, and it doesn't seem to work here.
> > I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
> > Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?
> >
> >
> >
> >
> > ffi.cdef[[
> > int csoundGetVersion();
> > int csoundGetAPIVersion();
> > int csoundInitialize (int *argc, char ***argv, int flags);
> > void *csoundCreate(void *hostdata);
> > void csoundDestroy(void *csound);
> > int csoundSetOption(void *csound, const char *option);
> > int csoundCompileOrc(void *csound, const char *orc);
> > int csoundCompileCsd(void *csound, const char *csd_filename);
> > int csoundReadScore(void *csound, const char *message);
> > int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
> > int csoundStart(void *csound);
> > int csoundPerformKsmps(void *csound);
> > int csoundPerform(void *csound);
> > void csoundSetControlChannel(void *csound, const char *name, double value);
> > int csoundStop(void *csound);
> > int csoundReset(void *csound);
> > void csoundGetCurrentThreadId(void *csound);
> > void csoundPerformanceThread(void *csound);
> > ]]
> >
> > csoundApi = ffi.load(pathtocsound64)
> >
> > local argc = ffi.new('int *')
> > local argv = ffi.new('char ***')
> >
> > self.result = csoundApi.csoundInitialize(argc, argv, 0);
> >
> > voidptr = ffi.new('void *')
> > print 'Creating an instance of Csound...'
> >
> > self.csound=csoundApi.csoundCreate(voidptr)
> >
> >
> > csoundApi.csoundSetOption(self.csound, '--sample-accurate')
> > csoundApi.csoundSetOption(self.csound, '--output=dac')
> > csoundApi.csoundSetOption(self.csound, '--format=float')
> > csoundApi.csoundSetOption(self.csound, '--nodisplays')
> >
> > csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
> > csoundApi.csoundReadScore(self.csound,tostring(score))
> > self.result = csoundApi.csoundStart(self.csound)
> > self.thread=csnd6.csoundPerformanceThread(self.csound)
> >
> >
> >
> >
> >
> > Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :
> >
> >
> > Hi Johann,
> >
> > I think you'll probably want to use the CsoundPerformanceThread.
> > There's Lua examples for it here:
> >
> > https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua
> >
> > This is higher-level than csoundCreateThread and should be already
> > setup for doing the kind of thing you want. It should correspond to
> > this C++ class:
> >
> > https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp
> >
> > steven
> > On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
> > <[hidden email]> wrote:
> > >
> > > Hello all,
> > >
> > > I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> > > I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> > > finished, i'm writing the piece of code to connect the program to the Csound
> > > API.
> > > I finally could play my first sine wave on this program yesterday night. The
> > > problem is that the GUI is freezing as long as Csound is playing. The
> > > example i'm based on is the luaffi.lua in csound examples.
> > > I guess it's freezing because there is no performance thread separate from
> > > the main thread.
> > >
> > > So i tried to create this performance thread with "csoundCreateThread" from
> > > the API. I could find some examples in C here :
> > > https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> > > and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> > > I tried to adapt those examples to Lua. But doing this through the foreign
> > > function interface is not that simple for me. I tried a thousand of
> > > possibilities, trying to understand the connection between C and lua ffi.
> > > Yet it's not working now.
> > >
> > > So, maybe someone already tried this ?
> > > The main issue is about the arguments passed to csoundCreateThread. The
> > > first needs to be a function right ? But, i think it's just taking a pointer
> > > to a C function for the main performance loop. And i couldn't figure out how
> > > to declare this function in C. Or is it possible to pass a Lua function as
> > > argument ?
> > > And what about the second argument ?
> > > I have to say that i never worked with C programming, so it's quite a
> > > complex world for me.
> > > I guess another solution would be to declare the performance function in a C
> > > library, and to import it with ffi.load, define it with ffi.cdef etc... But
> > > i was wondering if there is a way to do this directly in the lua code.
> > >
> > > If someone had a small example about threads in lua Api of Csound, it would
> > > be great.
> > > And i'll let you know as soon as the program is ready !
> > >
> > > Thanking you in advance
> > >
> > > Johann
> > >
> > >
> > >
> > >
> > >
> > >
> > > --
> > > Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
> > >
> > > Csound mailing list
> > > [hidden email]
> > > 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
> > [hidden email]
> > 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 [hidden email] 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
> [hidden email]
> 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 [hidden email] 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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Johann Philippe-2
So if i want to keep moving on this project, i'll have to learn some C++ and C anyway. Good to know.
I'll try that, thank you very much !
Johann


Le mercredi 26 septembre 2018 à 02:09:12 UTC+2, Michael Gogins <[hidden email]> a écrit :


OK, I see your problem. You can't use a C++ definition for a Lua cdef
without being a bit of a C++ and Lua expert. That's not I was getting
at.

I was suggesting wrapping these C++ declarations (from csound_theaded.hpp)

CsoundThreaded();
virtual int CsoundThreaded::CompileCsdText(const char *text);
CsoundThreaded:;Start();

in this C++ code that has a C interface:

extern "C" {
  void *CsoundThreaded_Create() {
    return new CsoundThreaded;
  int CsoundThreaded_CompileCsdText(void *csound, const char *text) {
  return (CsoundThreaded *)csound)->CompileCsdText(text);
}
int CsoundThreaded_Start(void *csond) {
  return ((CsoundThreaded *)csound)->Start();
}

You would compile this into a little DLL called CsoundThreadedAPI. And
the Lua CDEF would just be:
ffi.cdef[[
void *CsoundThreaded_Create();
  int CsoundThreaded_CompileCsdText(void *csound, const char *text);
int CsoundThreaded_Start(void *csound);
]]
local CsoundThreadedAPI = ffi.load('CsoundThreadedAPI()

Other functions such as channels could also be done this way.

So in Lua you would just do this:

local csound = CsoundThreadedAPI.CsoundThreaded_Create()
local result = CsoundThreadedAPI.CsoundThreaded_CompileCsdText(csound, text)
-- Runs in separate C++ thread:
local result = CsoundThreadedAPI.CsoundThreaded_Start(csound)





-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com
On Tue, Sep 25, 2018 at 6:46 PM Johann Philippe
<[hidden email]> wrote:

>
> Thanks for the answers.
> I'm not sure to understand well - can i do this directly from the ffi ? And, in this case, what should i write in the ffi.cdef ?
> I've been trying with definitions in ffi.cdef like this :
> ffi.cdef[[
> virtual  CSOUND *GetCsound();
> ]]
> and the definition is wrong for luajit
>
>
> or even like this
> ffi.cdef[[
> void *GetCsound();
> ]]
> and the definition is right, but the calling of GetCsound is not working then.
>
> I also tried like a billion other things, with no more result.
>
> And i can't do like on the first example (the example here https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua ) because, as I said, no luaCsnd6. And require"csnd6" is not right for luajit (cannot find the specified procedure - i guess luacsnd6 was a specific binding of this library for lua).
>
> Or maybe you're telling me that i should write something in c++ to make the connection ?
>
> Sorry guys, i'm not really aware of those questions...
>
>
>
>
>
>
>
>
> Le mardi 25 septembre 2018 à 16:04:22 UTC+2, Steven Yi <[hidden email]> a écrit :
>
>
> Hi Johann,
>
> Instead of using libcsound64, try using libcsnd6. The definition for it is here:
>
> https://github.com/csound/csound/blob/develop/interfaces/CMakeLists.txt
>
> that describes which files are used. It's the same library used by the
> other interface bindings and adds some C++ classes like the
> CsoundPerformanceThread.
>
> If you use this:
>
> https://github.com/csound/csound/blob/develop/interfaces/CppSound.hpp
>
> together with the getCsound() to use the standard API, it might work
> for you there.
>
> steven
> On Tue, Sep 25, 2018 at 8:26 AM Johann Philippe
> <[hidden email]> wrote:
> >
> >
> > Hello Steven,
> >
> > Thank you for the answer.
> > I'm trying this way, and it doesn't seem to work here.
> > I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
> > Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?
> >
> >
> >
> >
> > ffi.cdef[[
> > int csoundGetVersion();
> > int csoundGetAPIVersion();
> > int csoundInitialize (int *argc, char ***argv, int flags);
> > void *csoundCreate(void *hostdata);
> > void csoundDestroy(void *csound);
> > int csoundSetOption(void *csound, const char *option);
> > int csoundCompileOrc(void *csound, const char *orc);
> > int csoundCompileCsd(void *csound, const char *csd_filename);
> > int csoundReadScore(void *csound, const char *message);
> > int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
> > int csoundStart(void *csound);
> > int csoundPerformKsmps(void *csound);
> > int csoundPerform(void *csound);
> > void csoundSetControlChannel(void *csound, const char *name, double value);
> > int csoundStop(void *csound);
> > int csoundReset(void *csound);
> > void csoundGetCurrentThreadId(void *csound);
> > void csoundPerformanceThread(void *csound);
> > ]]
> >
> > csoundApi = ffi.load(pathtocsound64)
> >
> > local argc = ffi.new('int *')
> > local argv = ffi.new('char ***')
> >
> > self.result = csoundApi.csoundInitialize(argc, argv, 0);
> >
> > voidptr = ffi.new('void *')
> > print 'Creating an instance of Csound...'
> >
> > self.csound=csoundApi.csoundCreate(voidptr)
> >
> >
> > csoundApi.csoundSetOption(self.csound, '--sample-accurate')
> > csoundApi.csoundSetOption(self.csound, '--output=dac')
> > csoundApi.csoundSetOption(self.csound, '--format=float')
> > csoundApi.csoundSetOption(self.csound, '--nodisplays')
> >
> > csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
> > csoundApi.csoundReadScore(self.csound,tostring(score))
> > self.result = csoundApi.csoundStart(self.csound)
> > self.thread=csnd6.csoundPerformanceThread(self.csound)
> >
> >
> >
> >
> >
> > Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :
> >
> >
> > Hi Johann,
> >
> > I think you'll probably want to use the CsoundPerformanceThread.
> > There's Lua examples for it here:
> >
> > https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua
> >
> > This is higher-level than csoundCreateThread and should be already
> > setup for doing the kind of thing you want. It should correspond to
> > this C++ class:
> >
> > https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp
> >
> > steven
> > On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
> > <[hidden email]> wrote:
> > >
> > > Hello all,
> > >
> > > I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> > > I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> > > finished, i'm writing the piece of code to connect the program to the Csound
> > > API.
> > > I finally could play my first sine wave on this program yesterday night. The
> > > problem is that the GUI is freezing as long as Csound is playing. The
> > > example i'm based on is the luaffi.lua in csound examples.
> > > I guess it's freezing because there is no performance thread separate from
> > > the main thread.
> > >
> > > So i tried to create this performance thread with "csoundCreateThread" from
> > > the API. I could find some examples in C here :
> > > https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> > > and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> > > I tried to adapt those examples to Lua. But doing this through the foreign
> > > function interface is not that simple for me. I tried a thousand of
> > > possibilities, trying to understand the connection between C and lua ffi.
> > > Yet it's not working now.
> > >
> > > So, maybe someone already tried this ?
> > > The main issue is about the arguments passed to csoundCreateThread. The
> > > first needs to be a function right ? But, i think it's just taking a pointer
> > > to a C function for the main performance loop. And i couldn't figure out how
> > > to declare this function in C. Or is it possible to pass a Lua function as
> > > argument ?
> > > And what about the second argument ?
> > > I have to say that i never worked with C programming, so it's quite a
> > > complex world for me.
> > > I guess another solution would be to declare the performance function in a C
> > > library, and to import it with ffi.load, define it with ffi.cdef etc... But
> > > i was wondering if there is a way to do this directly in the lua code.
> > >
> > > If someone had a small example about threads in lua Api of Csound, it would
> > > be great.
> > > And i'll let you know as soon as the program is ready !
> > >
> > > Thanking you in advance
> > >
> > > Johann
> > >
> > >
> > >
> > >
> > >
> > >
> > > --
> > > Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
> > >
> > > Csound mailing list
> > > [hidden email]
> > > 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
> > [hidden email]
> > 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 [hidden email] 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
> [hidden email]
> 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 [hidden email] 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
[hidden email]
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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Michael Gogins-2
I can create this code, but it will not be in a release for a while. What platform are you on?

On Wed, Sep 26, 2018, 06:13 Johann Philippe <[hidden email]> wrote:
So if i want to keep moving on this project, i'll have to learn some C++ and C anyway. Good to know.
I'll try that, thank you very much !
Johann


Le mercredi 26 septembre 2018 à 02:09:12 UTC+2, Michael Gogins <[hidden email]> a écrit :


OK, I see your problem. You can't use a C++ definition for a Lua cdef
without being a bit of a C++ and Lua expert. That's not I was getting
at.

I was suggesting wrapping these C++ declarations (from csound_theaded.hpp)

CsoundThreaded();
virtual int CsoundThreaded::CompileCsdText(const char *text);
CsoundThreaded:;Start();

in this C++ code that has a C interface:

extern "C" {
  void *CsoundThreaded_Create() {
    return new CsoundThreaded;
  int CsoundThreaded_CompileCsdText(void *csound, const char *text) {
  return (CsoundThreaded *)csound)->CompileCsdText(text);
}
int CsoundThreaded_Start(void *csond) {
  return ((CsoundThreaded *)csound)->Start();
}

You would compile this into a little DLL called CsoundThreadedAPI. And
the Lua CDEF would just be:
ffi.cdef[[
void *CsoundThreaded_Create();
  int CsoundThreaded_CompileCsdText(void *csound, const char *text);
int CsoundThreaded_Start(void *csound);
]]
local CsoundThreadedAPI = ffi.load('CsoundThreadedAPI()

Other functions such as channels could also be done this way.

So in Lua you would just do this:

local csound = CsoundThreadedAPI.CsoundThreaded_Create()
local result = CsoundThreadedAPI.CsoundThreaded_CompileCsdText(csound, text)
-- Runs in separate C++ thread:
local result = CsoundThreadedAPI.CsoundThreaded_Start(csound)





-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com
On Tue, Sep 25, 2018 at 6:46 PM Johann Philippe
<[hidden email]> wrote:

>
> Thanks for the answers.
> I'm not sure to understand well - can i do this directly from the ffi ? And, in this case, what should i write in the ffi.cdef ?
> I've been trying with definitions in ffi.cdef like this :
> ffi.cdef[[
> virtual  CSOUND *GetCsound();
> ]]
> and the definition is wrong for luajit
>
>
> or even like this
> ffi.cdef[[
> void *GetCsound();
> ]]
> and the definition is right, but the calling of GetCsound is not working then.
>
> I also tried like a billion other things, with no more result.
>
> And i can't do like on the first example (the example here https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua ) because, as I said, no luaCsnd6. And require"csnd6" is not right for luajit (cannot find the specified procedure - i guess luacsnd6 was a specific binding of this library for lua).
>
> Or maybe you're telling me that i should write something in c++ to make the connection ?
>
> Sorry guys, i'm not really aware of those questions...
>
>
>
>
>
>
>
>
> Le mardi 25 septembre 2018 à 16:04:22 UTC+2, Steven Yi <[hidden email]> a écrit :
>
>
> Hi Johann,
>
> Instead of using libcsound64, try using libcsnd6. The definition for it is here:
>
> https://github.com/csound/csound/blob/develop/interfaces/CMakeLists.txt
>
> that describes which files are used. It's the same library used by the
> other interface bindings and adds some C++ classes like the
> CsoundPerformanceThread.
>
> If you use this:
>
> https://github.com/csound/csound/blob/develop/interfaces/CppSound.hpp
>
> together with the getCsound() to use the standard API, it might work
> for you there.
>
> steven
> On Tue, Sep 25, 2018 at 8:26 AM Johann Philippe
> <[hidden email]> wrote:
> >
> >
> > Hello Steven,
> >
> > Thank you for the answer.
> > I'm trying this way, and it doesn't seem to work here.
> > I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
> > Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?
> >
> >
> >
> >
> > ffi.cdef[[
> > int csoundGetVersion();
> > int csoundGetAPIVersion();
> > int csoundInitialize (int *argc, char ***argv, int flags);
> > void *csoundCreate(void *hostdata);
> > void csoundDestroy(void *csound);
> > int csoundSetOption(void *csound, const char *option);
> > int csoundCompileOrc(void *csound, const char *orc);
> > int csoundCompileCsd(void *csound, const char *csd_filename);
> > int csoundReadScore(void *csound, const char *message);
> > int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
> > int csoundStart(void *csound);
> > int csoundPerformKsmps(void *csound);
> > int csoundPerform(void *csound);
> > void csoundSetControlChannel(void *csound, const char *name, double value);
> > int csoundStop(void *csound);
> > int csoundReset(void *csound);
> > void csoundGetCurrentThreadId(void *csound);
> > void csoundPerformanceThread(void *csound);
> > ]]
> >
> > csoundApi = ffi.load(pathtocsound64)
> >
> > local argc = ffi.new('int *')
> > local argv = ffi.new('char ***')
> >
> > self.result = csoundApi.csoundInitialize(argc, argv, 0);
> >
> > voidptr = ffi.new('void *')
> > print 'Creating an instance of Csound...'
> >
> > self.csound=csoundApi.csoundCreate(voidptr)
> >
> >
> > csoundApi.csoundSetOption(self.csound, '--sample-accurate')
> > csoundApi.csoundSetOption(self.csound, '--output=dac')
> > csoundApi.csoundSetOption(self.csound, '--format=float')
> > csoundApi.csoundSetOption(self.csound, '--nodisplays')
> >
> > csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
> > csoundApi.csoundReadScore(self.csound,tostring(score))
> > self.result = csoundApi.csoundStart(self.csound)
> > self.thread=csnd6.csoundPerformanceThread(self.csound)
> >
> >
> >
> >
> >
> > Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :
> >
> >
> > Hi Johann,
> >
> > I think you'll probably want to use the CsoundPerformanceThread.
> > There's Lua examples for it here:
> >
> > https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua
> >
> > This is higher-level than csoundCreateThread and should be already
> > setup for doing the kind of thing you want. It should correspond to
> > this C++ class:
> >
> > https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp
> >
> > steven
> > On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
> > <[hidden email]> wrote:
> > >
> > > Hello all,
> > >
> > > I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> > > I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> > > finished, i'm writing the piece of code to connect the program to the Csound
> > > API.
> > > I finally could play my first sine wave on this program yesterday night. The
> > > problem is that the GUI is freezing as long as Csound is playing. The
> > > example i'm based on is the luaffi.lua in csound examples.
> > > I guess it's freezing because there is no performance thread separate from
> > > the main thread.
> > >
> > > So i tried to create this performance thread with "csoundCreateThread" from
> > > the API. I could find some examples in C here :
> > > https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> > > and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> > > I tried to adapt those examples to Lua. But doing this through the foreign
> > > function interface is not that simple for me. I tried a thousand of
> > > possibilities, trying to understand the connection between C and lua ffi.
> > > Yet it's not working now.
> > >
> > > So, maybe someone already tried this ?
> > > The main issue is about the arguments passed to csoundCreateThread. The
> > > first needs to be a function right ? But, i think it's just taking a pointer
> > > to a C function for the main performance loop. And i couldn't figure out how
> > > to declare this function in C. Or is it possible to pass a Lua function as
> > > argument ?
> > > And what about the second argument ?
> > > I have to say that i never worked with C programming, so it's quite a
> > > complex world for me.
> > > I guess another solution would be to declare the performance function in a C
> > > library, and to import it with ffi.load, define it with ffi.cdef etc... But
> > > i was wondering if there is a way to do this directly in the lua code.
> > >
> > > If someone had a small example about threads in lua Api of Csound, it would
> > > be great.
> > > And i'll let you know as soon as the program is ready !
> > >
> > > Thanking you in advance
> > >
> > > Johann
> > >
> > >
> > >
> > >
> > >
> > >
> > > --
> > > Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
> > >
> > > Csound mailing list
> > > [hidden email]
> > > 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
> > [hidden email]
> > 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 [hidden email] 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
> [hidden email]
> 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 [hidden email] 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
[hidden email]
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 [hidden email] 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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Johann Philippe-2
It's on Windows 10 x64
If i see the code, i think i'll be able to reproduce it/adapt it to a release if necessary.


Le mercredi 26 septembre 2018 à 12:19:36 UTC+2, Michael Gogins <[hidden email]> a écrit :


I can create this code, but it will not be in a release for a while. What platform are you on?

On Wed, Sep 26, 2018, 06:13 Johann Philippe <[hidden email]> wrote:
So if i want to keep moving on this project, i'll have to learn some C++ and C anyway. Good to know.
I'll try that, thank you very much !
Johann


Le mercredi 26 septembre 2018 à 02:09:12 UTC+2, Michael Gogins <[hidden email]> a écrit :


OK, I see your problem. You can't use a C++ definition for a Lua cdef
without being a bit of a C++ and Lua expert. That's not I was getting
at.

I was suggesting wrapping these C++ declarations (from csound_theaded.hpp)

CsoundThreaded();
virtual int CsoundThreaded::CompileCsdText(const char *text);
CsoundThreaded:;Start();

in this C++ code that has a C interface:

extern "C" {
  void *CsoundThreaded_Create() {
    return new CsoundThreaded;
  int CsoundThreaded_CompileCsdText(void *csound, const char *text) {
  return (CsoundThreaded *)csound)->CompileCsdText(text);
}
int CsoundThreaded_Start(void *csond) {
  return ((CsoundThreaded *)csound)->Start();
}

You would compile this into a little DLL called CsoundThreadedAPI. And
the Lua CDEF would just be:
ffi.cdef[[
void *CsoundThreaded_Create();
  int CsoundThreaded_CompileCsdText(void *csound, const char *text);
int CsoundThreaded_Start(void *csound);
]]
local CsoundThreadedAPI = ffi.load('CsoundThreadedAPI()

Other functions such as channels could also be done this way.

So in Lua you would just do this:

local csound = CsoundThreadedAPI.CsoundThreaded_Create()
local result = CsoundThreadedAPI.CsoundThreaded_CompileCsdText(csound, text)
-- Runs in separate C++ thread:
local result = CsoundThreadedAPI.CsoundThreaded_Start(csound)





-----------------------------------------------------
Michael Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com
On Tue, Sep 25, 2018 at 6:46 PM Johann Philippe
<[hidden email]> wrote:

>
> Thanks for the answers.
> I'm not sure to understand well - can i do this directly from the ffi ? And, in this case, what should i write in the ffi.cdef ?
> I've been trying with definitions in ffi.cdef like this :
> ffi.cdef[[
> virtual  CSOUND *GetCsound();
> ]]
> and the definition is wrong for luajit
>
>
> or even like this
> ffi.cdef[[
> void *GetCsound();
> ]]
> and the definition is right, but the calling of GetCsound is not working then.
>
> I also tried like a billion other things, with no more result.
>
> And i can't do like on the first example (the example here https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua ) because, as I said, no luaCsnd6. And require"csnd6" is not right for luajit (cannot find the specified procedure - i guess luacsnd6 was a specific binding of this library for lua).
>
> Or maybe you're telling me that i should write something in c++ to make the connection ?
>
> Sorry guys, i'm not really aware of those questions...
>
>
>
>
>
>
>
>
> Le mardi 25 septembre 2018 à 16:04:22 UTC+2, Steven Yi <[hidden email]> a écrit :
>
>
> Hi Johann,
>
> Instead of using libcsound64, try using libcsnd6. The definition for it is here:
>
> https://github.com/csound/csound/blob/develop/interfaces/CMakeLists.txt
>
> that describes which files are used. It's the same library used by the
> other interface bindings and adds some C++ classes like the
> CsoundPerformanceThread.
>
> If you use this:
>
> https://github.com/csound/csound/blob/develop/interfaces/CppSound.hpp
>
> together with the getCsound() to use the standard API, it might work
> for you there.
>
> steven
> On Tue, Sep 25, 2018 at 8:26 AM Johann Philippe
> <[hidden email]> wrote:
> >
> >
> > Hello Steven,
> >
> > Thank you for the answer.
> > I'm trying this way, and it doesn't seem to work here.
> > I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
> > Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?
> >
> >
> >
> >
> > ffi.cdef[[
> > int csoundGetVersion();
> > int csoundGetAPIVersion();
> > int csoundInitialize (int *argc, char ***argv, int flags);
> > void *csoundCreate(void *hostdata);
> > void csoundDestroy(void *csound);
> > int csoundSetOption(void *csound, const char *option);
> > int csoundCompileOrc(void *csound, const char *orc);
> > int csoundCompileCsd(void *csound, const char *csd_filename);
> > int csoundReadScore(void *csound, const char *message);
> > int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
> > int csoundStart(void *csound);
> > int csoundPerformKsmps(void *csound);
> > int csoundPerform(void *csound);
> > void csoundSetControlChannel(void *csound, const char *name, double value);
> > int csoundStop(void *csound);
> > int csoundReset(void *csound);
> > void csoundGetCurrentThreadId(void *csound);
> > void csoundPerformanceThread(void *csound);
> > ]]
> >
> > csoundApi = ffi.load(pathtocsound64)
> >
> > local argc = ffi.new('int *')
> > local argv = ffi.new('char ***')
> >
> > self.result = csoundApi.csoundInitialize(argc, argv, 0);
> >
> > voidptr = ffi.new('void *')
> > print 'Creating an instance of Csound...'
> >
> > self.csound=csoundApi.csoundCreate(voidptr)
> >
> >
> > csoundApi.csoundSetOption(self.csound, '--sample-accurate')
> > csoundApi.csoundSetOption(self.csound, '--output=dac')
> > csoundApi.csoundSetOption(self.csound, '--format=float')
> > csoundApi.csoundSetOption(self.csound, '--nodisplays')
> >
> > csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
> > csoundApi.csoundReadScore(self.csound,tostring(score))
> > self.result = csoundApi.csoundStart(self.csound)
> > self.thread=csnd6.csoundPerformanceThread(self.csound)
> >
> >
> >
> >
> >
> > Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :
> >
> >
> > Hi Johann,
> >
> > I think you'll probably want to use the CsoundPerformanceThread.
> > There's Lua examples for it here:
> >
> > https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua
> >
> > This is higher-level than csoundCreateThread and should be already
> > setup for doing the kind of thing you want. It should correspond to
> > this C++ class:
> >
> > https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp
> >
> > steven
> > On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
> > <[hidden email]> wrote:
> > >
> > > Hello all,
> > >
> > > I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> > > I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> > > finished, i'm writing the piece of code to connect the program to the Csound
> > > API.
> > > I finally could play my first sine wave on this program yesterday night. The
> > > problem is that the GUI is freezing as long as Csound is playing. The
> > > example i'm based on is the luaffi.lua in csound examples.
> > > I guess it's freezing because there is no performance thread separate from
> > > the main thread.
> > >
> > > So i tried to create this performance thread with "csoundCreateThread" from
> > > the API. I could find some examples in C here :
> > > https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> > > and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> > > I tried to adapt those examples to Lua. But doing this through the foreign
> > > function interface is not that simple for me. I tried a thousand of
> > > possibilities, trying to understand the connection between C and lua ffi.
> > > Yet it's not working now.
> > >
> > > So, maybe someone already tried this ?
> > > The main issue is about the arguments passed to csoundCreateThread. The
> > > first needs to be a function right ? But, i think it's just taking a pointer
> > > to a C function for the main performance loop. And i couldn't figure out how
> > > to declare this function in C. Or is it possible to pass a Lua function as
> > > argument ?
> > > And what about the second argument ?
> > > I have to say that i never worked with C programming, so it's quite a
> > > complex world for me.
> > > I guess another solution would be to declare the performance function in a C
> > > library, and to import it with ffi.load, define it with ffi.cdef etc... But
> > > i was wondering if there is a way to do this directly in the lua code.
> > >
> > > If someone had a small example about threads in lua Api of Csound, it would
> > > be great.
> > > And i'll let you know as soon as the program is ready !
> > >
> > > Thanking you in advance
> > >
> > > Johann
> > >
> > >
> > >
> > >
> > >
> > >
> > > --
> > > Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
> > >
> > > Csound mailing list
> > > [hidden email]
> > > 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
> > [hidden email]
> > 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 [hidden email] 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
> [hidden email]
> 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 [hidden email] 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
[hidden email]
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 [hidden email] 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 [hidden email] 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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Michael Gogins-2
I will do this then.

Regards,
Mike

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

On Wed, Sep 26, 2018 at 7:18 AM Johann Philippe
<[hidden email]> wrote:

>
> It's on Windows 10 x64
> If i see the code, i think i'll be able to reproduce it/adapt it to a release if necessary.
>
>
> Le mercredi 26 septembre 2018 à 12:19:36 UTC+2, Michael Gogins <[hidden email]> a écrit :
>
>
> I can create this code, but it will not be in a release for a while. What platform are you on?
>
> On Wed, Sep 26, 2018, 06:13 Johann Philippe <[hidden email]> wrote:
>
> So if i want to keep moving on this project, i'll have to learn some C++ and C anyway. Good to know.
> I'll try that, thank you very much !
> Johann
>
>
> Le mercredi 26 septembre 2018 à 02:09:12 UTC+2, Michael Gogins <[hidden email]> a écrit :
>
>
> OK, I see your problem. You can't use a C++ definition for a Lua cdef
> without being a bit of a C++ and Lua expert. That's not I was getting
> at.
>
> I was suggesting wrapping these C++ declarations (from csound_theaded.hpp)
>
> CsoundThreaded();
> virtual int CsoundThreaded::CompileCsdText(const char *text);
> CsoundThreaded:;Start();
>
> in this C++ code that has a C interface:
>
> extern "C" {
>   void *CsoundThreaded_Create() {
>     return new CsoundThreaded;
>   int CsoundThreaded_CompileCsdText(void *csound, const char *text) {
>   return (CsoundThreaded *)csound)->CompileCsdText(text);
> }
> int CsoundThreaded_Start(void *csond) {
>   return ((CsoundThreaded *)csound)->Start();
> }
>
> You would compile this into a little DLL called CsoundThreadedAPI. And
> the Lua CDEF would just be:
> ffi.cdef[[
> void *CsoundThreaded_Create();
>   int CsoundThreaded_CompileCsdText(void *csound, const char *text);
> int CsoundThreaded_Start(void *csound);
> ]]
> local CsoundThreadedAPI = ffi.load('CsoundThreadedAPI()
>
> Other functions such as channels could also be done this way.
>
> So in Lua you would just do this:
>
> local csound = CsoundThreadedAPI.CsoundThreaded_Create()
> local result = CsoundThreadedAPI.CsoundThreaded_CompileCsdText(csound, text)
> -- Runs in separate C++ thread:
> local result = CsoundThreadedAPI.CsoundThreaded_Start(csound)
>
>
>
>
>
> -----------------------------------------------------
> Michael Gogins
> Irreducible Productions
> http://michaelgogins.tumblr.com
> Michael dot Gogins at gmail dot com
> On Tue, Sep 25, 2018 at 6:46 PM Johann Philippe
> <[hidden email]> wrote:
> >
> > Thanks for the answers.
> > I'm not sure to understand well - can i do this directly from the ffi ? And, in this case, what should i write in the ffi.cdef ?
> > I've been trying with definitions in ffi.cdef like this :
> > ffi.cdef[[
> > virtual  CSOUND *GetCsound();
> > ]]
> > and the definition is wrong for luajit
> >
> >
> > or even like this
> > ffi.cdef[[
> > void *GetCsound();
> > ]]
> > and the definition is right, but the calling of GetCsound is not working then.
> >
> > I also tried like a billion other things, with no more result.
> >
> > And i can't do like on the first example (the example here https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua ) because, as I said, no luaCsnd6. And require"csnd6" is not right for luajit (cannot find the specified procedure - i guess luacsnd6 was a specific binding of this library for lua).
> >
> > Or maybe you're telling me that i should write something in c++ to make the connection ?
> >
> > Sorry guys, i'm not really aware of those questions...
> >
> >
> >
> >
> >
> >
> >
> >
> > Le mardi 25 septembre 2018 à 16:04:22 UTC+2, Steven Yi <[hidden email]> a écrit :
> >
> >
> > Hi Johann,
> >
> > Instead of using libcsound64, try using libcsnd6. The definition for it is here:
> >
> > https://github.com/csound/csound/blob/develop/interfaces/CMakeLists.txt
> >
> > that describes which files are used. It's the same library used by the
> > other interface bindings and adds some C++ classes like the
> > CsoundPerformanceThread.
> >
> > If you use this:
> >
> > https://github.com/csound/csound/blob/develop/interfaces/CppSound.hpp
> >
> > together with the getCsound() to use the standard API, it might work
> > for you there.
> >
> > steven
> > On Tue, Sep 25, 2018 at 8:26 AM Johann Philippe
> > <[hidden email]> wrote:
> > >
> > >
> > > Hello Steven,
> > >
> > > Thank you for the answer.
> > > I'm trying this way, and it doesn't seem to work here.
> > > I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
> > > Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?
> > >
> > >
> > >
> > >
> > > ffi.cdef[[
> > > int csoundGetVersion();
> > > int csoundGetAPIVersion();
> > > int csoundInitialize (int *argc, char ***argv, int flags);
> > > void *csoundCreate(void *hostdata);
> > > void csoundDestroy(void *csound);
> > > int csoundSetOption(void *csound, const char *option);
> > > int csoundCompileOrc(void *csound, const char *orc);
> > > int csoundCompileCsd(void *csound, const char *csd_filename);
> > > int csoundReadScore(void *csound, const char *message);
> > > int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
> > > int csoundStart(void *csound);
> > > int csoundPerformKsmps(void *csound);
> > > int csoundPerform(void *csound);
> > > void csoundSetControlChannel(void *csound, const char *name, double value);
> > > int csoundStop(void *csound);
> > > int csoundReset(void *csound);
> > > void csoundGetCurrentThreadId(void *csound);
> > > void csoundPerformanceThread(void *csound);
> > > ]]
> > >
> > > csoundApi = ffi.load(pathtocsound64)
> > >
> > > local argc = ffi.new('int *')
> > > local argv = ffi.new('char ***')
> > >
> > > self.result = csoundApi.csoundInitialize(argc, argv, 0);
> > >
> > > voidptr = ffi.new('void *')
> > > print 'Creating an instance of Csound...'
> > >
> > > self.csound=csoundApi.csoundCreate(voidptr)
> > >
> > >
> > > csoundApi.csoundSetOption(self.csound, '--sample-accurate')
> > > csoundApi.csoundSetOption(self.csound, '--output=dac')
> > > csoundApi.csoundSetOption(self.csound, '--format=float')
> > > csoundApi.csoundSetOption(self.csound, '--nodisplays')
> > >
> > > csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
> > > csoundApi.csoundReadScore(self.csound,tostring(score))
> > > self.result = csoundApi.csoundStart(self.csound)
> > > self.thread=csnd6.csoundPerformanceThread(self.csound)
> > >
> > >
> > >
> > >
> > >
> > > Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :
> > >
> > >
> > > Hi Johann,
> > >
> > > I think you'll probably want to use the CsoundPerformanceThread.
> > > There's Lua examples for it here:
> > >
> > > https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua
> > >
> > > This is higher-level than csoundCreateThread and should be already
> > > setup for doing the kind of thing you want. It should correspond to
> > > this C++ class:
> > >
> > > https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp
> > >
> > > steven
> > > On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
> > > <[hidden email]> wrote:
> > > >
> > > > Hello all,
> > > >
> > > > I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> > > > I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> > > > finished, i'm writing the piece of code to connect the program to the Csound
> > > > API.
> > > > I finally could play my first sine wave on this program yesterday night. The
> > > > problem is that the GUI is freezing as long as Csound is playing. The
> > > > example i'm based on is the luaffi.lua in csound examples.
> > > > I guess it's freezing because there is no performance thread separate from
> > > > the main thread.
> > > >
> > > > So i tried to create this performance thread with "csoundCreateThread" from
> > > > the API. I could find some examples in C here :
> > > > https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> > > > and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> > > > I tried to adapt those examples to Lua. But doing this through the foreign
> > > > function interface is not that simple for me. I tried a thousand of
> > > > possibilities, trying to understand the connection between C and lua ffi.
> > > > Yet it's not working now.
> > > >
> > > > So, maybe someone already tried this ?
> > > > The main issue is about the arguments passed to csoundCreateThread. The
> > > > first needs to be a function right ? But, i think it's just taking a pointer
> > > > to a C function for the main performance loop. And i couldn't figure out how
> > > > to declare this function in C. Or is it possible to pass a Lua function as
> > > > argument ?
> > > > And what about the second argument ?
> > > > I have to say that i never worked with C programming, so it's quite a
> > > > complex world for me.
> > > > I guess another solution would be to declare the performance function in a C
> > > > library, and to import it with ffi.load, define it with ffi.cdef etc... But
> > > > i was wondering if there is a way to do this directly in the lua code.
> > > >
> > > > If someone had a small example about threads in lua Api of Csound, it would
> > > > be great.
> > > > And i'll let you know as soon as the program is ready !
> > > >
> > > > Thanking you in advance
> > > >
> > > > Johann
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
> > > >
> > > > Csound mailing list
> > > > [hidden email]
> > > > 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
> > > [hidden email]
> > > 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 [hidden email] 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
> > [hidden email]
> > 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 [hidden email] 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
> [hidden email]
> 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 [hidden email] 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 [hidden email] 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 [hidden email] 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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] Create a performance thread in a Lua through the API

Johann Philippe-2
Well, i'm trying to learn a bit of C++ to see how i could create this small .dll.
I don't really see the architecture of the code to write here :

The following quote : it's like a header definition right ?
Should i add it to csound_threaded.hpp (in the CsoundThreaded class maybe) or just create another header .hpp ? or .h ? Or just another class ?
"
    CsoundThreaded();
    virtual int CsoundThreaded::CompileCsdText(const char *text);
    CsoundThreaded::Start();
"

And the following one should be the .cpp file i guess - but what should i put in the main function then ?

extern "C" {
  void *CsoundThreaded_Create() {
    return new CsoundThreaded;
  int CsoundThreaded_CompileCsdText(void *csound, const char *text) {
  return (CsoundThreaded *)csound)->CompileCsdText(text);
}
int CsoundThreaded_Start(void *csond) {
  return ((CsoundThreaded *)csound)->Start();
}

Thanks.
Le mercredi 26 septembre 2018 à 14:26:42 UTC+2, Michael Gogins <[hidden email]> a écrit :


I will do this then.

Regards,
Mike

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

On Wed, Sep 26, 2018 at 7:18 AM Johann Philippe
<[hidden email]> wrote:

>
> It's on Windows 10 x64
> If i see the code, i think i'll be able to reproduce it/adapt it to a release if necessary.
>
>
> Le mercredi 26 septembre 2018 à 12:19:36 UTC+2, Michael Gogins <[hidden email]> a écrit :
>
>
> I can create this code, but it will not be in a release for a while. What platform are you on?
>
> On Wed, Sep 26, 2018, 06:13 Johann Philippe <[hidden email]> wrote:
>
> So if i want to keep moving on this project, i'll have to learn some C++ and C anyway. Good to know.
> I'll try that, thank you very much !
> Johann
>
>
> Le mercredi 26 septembre 2018 à 02:09:12 UTC+2, Michael Gogins <[hidden email]> a écrit :
>
>
> OK, I see your problem. You can't use a C++ definition for a Lua cdef
> without being a bit of a C++ and Lua expert. That's not I was getting
> at.
>
> I was suggesting wrapping these C++ declarations (from csound_theaded.hpp)
>
> CsoundThreaded();
> virtual int CsoundThreaded::CompileCsdText(const char *text);
> CsoundThreaded:;Start();
>
> in this C++ code that has a C interface:
>
> extern "C" {
>  void *CsoundThreaded_Create() {
>    return new CsoundThreaded;
>  int CsoundThreaded_CompileCsdText(void *csound, const char *text) {
>  return (CsoundThreaded *)csound)->CompileCsdText(text);
> }
> int CsoundThreaded_Start(void *csond) {
>  return ((CsoundThreaded *)csound)->Start();
> }
>
> You would compile this into a little DLL called CsoundThreadedAPI. And
> the Lua CDEF would just be:
> ffi.cdef[[
> void *CsoundThreaded_Create();
>  int CsoundThreaded_CompileCsdText(void *csound, const char *text);
> int CsoundThreaded_Start(void *csound);
> ]]
> local CsoundThreadedAPI = ffi.load('CsoundThreadedAPI()
>
> Other functions such as channels could also be done this way.
>
> So in Lua you would just do this:
>
> local csound = CsoundThreadedAPI.CsoundThreaded_Create()
> local result = CsoundThreadedAPI.CsoundThreaded_CompileCsdText(csound, text)
> -- Runs in separate C++ thread:
> local result = CsoundThreadedAPI.CsoundThreaded_Start(csound)
>
>
>
>
>
> -----------------------------------------------------
> Michael Gogins
> Irreducible Productions
> http://michaelgogins.tumblr.com
> Michael dot Gogins at gmail dot com
> On Tue, Sep 25, 2018 at 6:46 PM Johann Philippe
> <[hidden email]> wrote:
> >
> > Thanks for the answers.
> > I'm not sure to understand well - can i do this directly from the ffi ? And, in this case, what should i write in the ffi.cdef ?
> > I've been trying with definitions in ffi.cdef like this :
> > ffi.cdef[[
> > virtual  CSOUND *GetCsound();
> > ]]
> > and the definition is wrong for luajit
> >
> >
> > or even like this
> > ffi.cdef[[
> > void *GetCsound();
> > ]]
> > and the definition is right, but the calling of GetCsound is not working then.
> >
> > I also tried like a billion other things, with no more result.
> >
> > And i can't do like on the first example (the example here https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua ) because, as I said, no luaCsnd6. And require"csnd6" is not right for luajit (cannot find the specified procedure - i guess luacsnd6 was a specific binding of this library for lua).
> >
> > Or maybe you're telling me that i should write something in c++ to make the connection ?
> >
> > Sorry guys, i'm not really aware of those questions...
> >
> >
> >
> >
> >
> >
> >
> >
> > Le mardi 25 septembre 2018 à 16:04:22 UTC+2, Steven Yi <[hidden email]> a écrit :
> >
> >
> > Hi Johann,
> >
> > Instead of using libcsound64, try using libcsnd6. The definition for it is here:
> >
> > https://github.com/csound/csound/blob/develop/interfaces/CMakeLists.txt
> >
> > that describes which files are used. It's the same library used by the
> > other interface bindings and adds some C++ classes like the
> > CsoundPerformanceThread.
> >
> > If you use this:
> >
> > https://github.com/csound/csound/blob/develop/interfaces/CppSound.hpp
> >
> > together with the getCsound() to use the standard API, it might work
> > for you there.
> >
> > steven
> > On Tue, Sep 25, 2018 at 8:26 AM Johann Philippe
> > <[hidden email]> wrote:
> > >
> > >
> > > Hello Steven,
> > >
> > > Thank you for the answer.
> > > I'm trying this way, and it doesn't seem to work here.
> > > I'm not using "luacsnd6" (i didn't get this .dll in the csound 6.10 distribution), so i'm using the ffi method instead. So, i tried like with the following code, and it returns "cannot resolve symbol 'csoundPerformanceThread': The specified procedure could not be found."
> > > Any idea of what is not working ? Maybe i need to load a specific library to use this CsoundPerformanceThread ?
> > >
> > >
> > >
> > >
> > > ffi.cdef[[
> > > int csoundGetVersion();
> > > int csoundGetAPIVersion();
> > > int csoundInitialize (int *argc, char ***argv, int flags);
> > > void *csoundCreate(void *hostdata);
> > > void csoundDestroy(void *csound);
> > > int csoundSetOption(void *csound, const char *option);
> > > int csoundCompileOrc(void *csound, const char *orc);
> > > int csoundCompileCsd(void *csound, const char *csd_filename);
> > > int csoundReadScore(void *csound, const char *message);
> > > int csoundScoreEvent (void *csound, char type, const double *pFields, long numFields);
> > > int csoundStart(void *csound);
> > > int csoundPerformKsmps(void *csound);
> > > int csoundPerform(void *csound);
> > > void csoundSetControlChannel(void *csound, const char *name, double value);
> > > int csoundStop(void *csound);
> > > int csoundReset(void *csound);
> > > void csoundGetCurrentThreadId(void *csound);
> > > void csoundPerformanceThread(void *csound);
> > > ]]
> > >
> > > csoundApi = ffi.load(pathtocsound64)
> > >
> > > local argc = ffi.new('int *')
> > > local argv = ffi.new('char ***')
> > >
> > > self.result = csoundApi.csoundInitialize(argc, argv, 0);
> > >
> > > voidptr = ffi.new('void *')
> > > print 'Creating an instance of Csound...'
> > >
> > > self.csound=csoundApi.csoundCreate(voidptr)
> > >
> > >
> > > csoundApi.csoundSetOption(self.csound, '--sample-accurate')
> > > csoundApi.csoundSetOption(self.csound, '--output=dac')
> > > csoundApi.csoundSetOption(self.csound, '--format=float')
> > > csoundApi.csoundSetOption(self.csound, '--nodisplays')
> > >
> > > csoundApi.csoundCompileOrc(self.csound,tostring(orchestra))
> > > csoundApi.csoundReadScore(self.csound,tostring(score))
> > > self.result = csoundApi.csoundStart(self.csound)
> > > self.thread=csnd6.csoundPerformanceThread(self.csound)
> > >
> > >
> > >
> > >
> > >
> > > Le mardi 25 septembre 2018 à 01:19:01 UTC+2, Steven Yi <[hidden email]> a écrit :
> > >
> > >
> > > Hi Johann,
> > >
> > > I think you'll probably want to use the CsoundPerformanceThread.
> > > There's Lua examples for it here:
> > >
> > > https://github.com/csound/csoundAPI_examples/blob/master/lua/example4.lua
> > >
> > > This is higher-level than csoundCreateThread and should be already
> > > setup for doing the kind of thing you want. It should correspond to
> > > this C++ class:
> > >
> > > https://github.com/csound/csound/blob/develop/interfaces/csPerfThread.hpp
> > >
> > > steven
> > > On Mon, Sep 24, 2018 at 6:25 PM Johann Philippe
> > > <[hidden email]> wrote:
> > > >
> > > > Hello all,
> > > >
> > > > I'm currently working on a program with Lua, IUP (for the GUI) and Csound.
> > > > I'm working on windows 10 x64 with csound 6.10.  Now that the GUI is almost
> > > > finished, i'm writing the piece of code to connect the program to the Csound
> > > > API.
> > > > I finally could play my first sine wave on this program yesterday night. The
> > > > problem is that the GUI is freezing as long as Csound is playing. The
> > > > example i'm based on is the luaffi.lua in csound examples.
> > > > I guess it's freezing because there is no performance thread separate from
> > > > the main thread.
> > > >
> > > > So i tried to create this performance thread with "csoundCreateThread" from
> > > > the API. I could find some examples in C here :
> > > > https://github.com/csound/csoundAPI_examples/blob/master/c/example4.c
> > > > and here : http://write.flossmanuals.net/csound/a-the-csound-api/
> > > > I tried to adapt those examples to Lua. But doing this through the foreign
> > > > function interface is not that simple for me. I tried a thousand of
> > > > possibilities, trying to understand the connection between C and lua ffi.
> > > > Yet it's not working now.
> > > >
> > > > So, maybe someone already tried this ?
> > > > The main issue is about the arguments passed to csoundCreateThread. The
> > > > first needs to be a function right ? But, i think it's just taking a pointer
> > > > to a C function for the main performance loop. And i couldn't figure out how
> > > > to declare this function in C. Or is it possible to pass a Lua function as
> > > > argument ?
> > > > And what about the second argument ?
> > > > I have to say that i never worked with C programming, so it's quite a
> > > > complex world for me.
> > > > I guess another solution would be to declare the performance function in a C
> > > > library, and to import it with ffi.load, define it with ffi.cdef etc... But
> > > > i was wondering if there is a way to do this directly in the lua code.
> > > >
> > > > If someone had a small example about threads in lua Api of Csound, it would
> > > > be great.
> > > > And i'll let you know as soon as the program is ready !
> > > >
> > > > Thanking you in advance
> > > >
> > > > Johann
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
> > > >
> > > > Csound mailing list
> > > > [hidden email]
> > > > 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
> > > [hidden email]
> > > 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 [hidden email] 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
> > [hidden email]
> > 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 [hidden email] 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
> [hidden email]
> 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 [hidden email] 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 [hidden email] 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 [hidden email] 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
[hidden email]
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 [hidden email] 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