A lil' help, please

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

A lil' help, please

Post by 7feet »

Iwant to solve the communities audio problems, but I can't.

Fazek noted my mixer script that I'd posted as the most complicated thing here. Probably, I have a Much Better Version that's not quite so dunderheaded. You learn software design pretty fast when you're beat upside the head.

But what I need, really directly, is some assistance in linking LUA to outside DLL's. .SO's, whatever they use on Macs (never touch 'em). I'll use an example, and if you can help. YEAH!

My mixer script is pretty massive, and I've tried a lot of data structures to hold it (also just processing direct from files). It all sucks. The biggest problem is the fact that Lua, at it's core, does all math at double precosion float. The audio stuff is all a lot of short (2 byte) signed data, mad fast in C (or Pascal, which I thought of using, since I useta do a lot in that)I thought that beeing able to write a little pass a Lua string and do integer math on it cross platform library shouldn't be too hard to write.

Aw, hell. I've spent probably 2 solid days worth of my life trying to figure this out. C++ is, as the saying goes, greek to me. I'll learn how to code in it if I absolutely have to,but I don't think I'll ever like it. Whatever.

What I need is a neat way to pass a big Lua string (which would be the audio data) or several, to an external which can just do the giant piles of integer math which should take no time at all. Maybe I'm wierd, but I'd rather have a bridge between Lua and C, and then do everything that supposed to happen in the the in assembler code. I'm actually much more comfortable in that, though I'd prefer something that wasn't quite as nuts'n'bolts. But if I could work out a byte string/integermath/bytestring bit , as a lib accesible to Lua, I thinkl I could do most of the audio stuff thats been asked for thr last yearandahalf. And be happy to.

In th end, you have to pass it as Userdata, but I am 100% dumbfounded. I think I've read a good 50 of the Lua-Users wiki the last 3 weeks, and I still feel like I'm a stone cold idjit. Really, help me. I want to do cool stuff but I am at a stone cold impasse.
User avatar
Fazek
Posts: 246
Joined: Thu Apr 13, 2006 1:37 pm
Location: Hungary
Contact:

Post by Fazek »

This is the Lua script to call a usual lua support library. The library table will be in the libraryname variable. You can see the difference in the syntax of Lua 5.0 and 5.1.

Code: Select all

if (string.find(_VERSION,"5.0",1,true)) then
	local call= loadlib("./libraryname.so","luaopen_libraryname")
	libraryname= call()
else
	require("libraryname")
end
local doc= libraryname:Functionname(arglist)
These libraries have special Lua-compatible functions. A Lua callable C function looks like this example (it creates a new UserData value and passes it to the caller Lua program)

Code: Select all

typedef struct {
       FunctionData *doc;
  } FunctionUserData;

LUA_API int Functionname(lua_State* L) {
	FunctionUserData *d;
	FunctionData *doc= malloc(sizeof(FunctionData));

	if (!doc) return 0;  /* returns no value (=nil) */

        d= (FunctionUserData *)
	  lua_newuserdata(L, sizeof(FunctionUserData));

       /* the userdata is on the top of the stack */

	if (!d) { free(doc); return 0; }

       /* ---- fill the doc structure here ---- */

        d->doc= doc;

        /* get the prepared metatable from the registry and attach it to the UserData */

	luaL_getmetatable(L, "function_metatable");
	lua_setmetatable(L, -2);
	return 1;  /* one returned value (the UserData) on the stack */
}
When you open the library from Lua, its opening function (luaopen_libraryname) registers this function in the library's metatable, so you can call it from Lua. It is possible to create separated metatables and use them with the created tables, userdatums etc. (like function_metatable in this example). You can put the new functions into these metatables too. These C programs are using the functions of the lualib and lauxlib libraries.

To open a general purpose library and use it in Lua, you need an interface library with Lua compatible functions. There are many of them (see the www.lua.org website) but I think you can find a general purpose solution (I hope somebody already made it) since it is possible to write an interface library to call any function of any libraries (with a simple prototyping syntax).

In the Moho's case, for example a Layer is a UserData value and its functions, like Name() are in the Layer's metatable. The UserData's metatable contains special purpose functions like __gc() when the garbage collection wants to delete it, and __index() and __newindex() to handle its member variables, if neccessary.

Unfortunately the loadlib() (and of course require(), Moho's Lua is 5.0) are disabled in my Moho Linux version. So I cannot call any library from Moho.

I don't know what about AS, I'm still waiting for the Linux version.
- - - Fazek
myles
Posts: 821
Joined: Sat Aug 21, 2004 3:32 am
Location: Australia, Victoria, Morwell
Contact:

Post by myles »

Further to Fazek's notes:

Step-by-step instructions here for making a DLL callable from Lua.
Pros: works with Anime Studio Pro (Windows), relatively easy.
Cons: uses a compiler/IDE that is only free for non-commercial usage, doesn't talk about passing data at all.

Put the resulting .dll in same directory as main Anime Pro executable, just call loadlib with .dll filename and no path.

Better:
Example code which includes passing data at http://lua-users.org/wiki/CreatingBinar ... ionModules on the Lua users Wiki.
These instructions are for the free Borland C (and C++) compiler. Only shows how to implement a single function call. Works okay with Anime Studio Pro.
I used lua5_0_bc55.tar.gz from http://luaforge.net/frs/?group_id=110
Works with Anime Studio Pro, my commandline was finally:

Code: Select all

E:\junk>bcc32 -IE:\junk\lua5\include -LE:\junk\lua5\lib\bcc55 -w -tWD -u -DLUA_API=__declspec(dllimport) lua5.lib lualib5.lib msgbox.c
More documentation for specific data types in the main Lua reference manual - note that this is the 5.1 manual, as Fazek notes Anime Studio uses Lua 5.0.

Lots more C API documentation in Programming in Lua, see Part IV.

Regards, Myles - not a C programmer.
"Quote me as saying I was mis-quoted."
-- Groucho Marx
Post Reply