lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm sure it is pretty basic and poorly designed, since I'm new to Lua, but I used Ross Bencina's OSCPACK (http://www.audiomulch.com/ ~rossb/code/oscpack/) code in an application and bound it to Lua in about an hour, such that a function is called in Lua with any received OSC message along with type and address info. Actually most of the delay was due to me working on a new McIntel, with little/big endian issues.

Here's the core method FYI:

// inherited method from OscPacketListener, defines how to handle OSC messages void OscReceive :: ProcessMessage(const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint)
{
	try{
		long nargs = m.ArgumentCount();
		osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
		
lua_State * L = GLua::getInstance()->getLua(); // my global ptr to Lua space
		
		// call the 'onosc' function with required arguments
		lua_getglobal(L, "onOSC");		
		
		lua_pushstring(L, m.AddressPattern());
		lua_pushstring(L, m.TypeTags());
		
		for (int i=0; i<nargs; i++)
		{
			std::cout << " ";
			if ((arg)->IsFloat()) {
				lua_pushnumber(L, (double)(arg++)->AsFloat());
				
			} else if ((arg)->IsInt32()) {
				lua_pushnumber(L, (double)(arg++)->AsInt32());
				
			} else if ((arg)->IsChar()) {
				lua_pushnumber(L, (double)(arg++)->AsChar());
				
			} else if ((arg)->IsDouble()) {
				lua_pushnumber(L, (arg++)->AsDouble());
				
			} else if ((arg)->IsString()) {
				lua_pushstring(L, (arg++)->AsString());
				
			} else if ((arg)->IsTimeTag()) {
				lua_pushnumber(L, (double)(arg++)->AsTimeTag());
				
			} else if ((arg)->IsBool()) {
				lua_pushboolean(L, (arg++)->AsBool() ? 1 : 0);
				
			} else if ((arg)->IsNil()) {
				lua_pushnil(L);
				arg++;
				
			} else if ((arg)->IsInt64()) {
				lua_pushnumber(L, (double)(arg++)->AsInt64());
				
			} else if ((arg)->IsSymbol()) {
				lua_pushstring(L, (arg++)->AsSymbol());
				
			} else if ((arg)->IsMidiMessage()) {
				lua_pushnumber(L, (double)(arg++)->AsMidiMessage());
				
			} else {
				lua_pushnil(L);
				arg++;
			}
		}
		
		if (lua_pcall(L, nargs+2, 0, 0) != 0) {	// if there's an error
			ConsoleLua::clua->addLine("error running function 'onosc':");
			ConsoleLua::clua->addLine(lua_tostring(L, -1));			
		}
		
	} catch( osc::Exception& e ){
		// any parsing errors such as unexpected argument types, or
		// missing arguments get thrown as exceptions.
		ConsoleLua::clua->addLine("error receiving OSC:");
		ConsoleLua::clua->addLine(m.AddressPattern());
		ConsoleLua::clua->addLine(e.what());
	}
}


On Apr 18, 2006, at 4:49 PM, Damian Stewart wrote:

hey everyone,

i'm looking at using OSC (www.opensoundcontrol.org) to communicate between a vision-based interface device and an engine running a lua scripting system. the best way to do this would seem to be to implement the OSC server in Lua and have the interface device operate as an OSC client. the interface device is built in C++, and will likely be reused across other projects not running Lua - hence the decision to use OSC.

i'm completely new to the scripting end of Lua (despite having coded assembly-level bindings to enable lua-driven C++ function calls on a PlayStation2, but that's another story, another project, another company, and at least two years ago ;-) - can anyone give me some starting pointers to look at doing this?

cheers
d

--
f r e y
live music with computers
http://www.frey.co.nz