Saturday, July 19, 2008
Synthesis ToolKit plus lua
//The compiled file is available here
// stk with lua plugins
// questions comments idea and bug fixes (exc) can be directed here bbs
//This is a proof of concept and still buggy
//but it does compile in visual c++ 2008 express
//lua version is 5.1.0 (I think
//requires lua and The Synthesis ToolKit ( stk ) to compile
#include
#include
#include "FileWvOut.h"
#include "WvOut.h"
//#include FileWvOut.h"
//#include
#include
#include
//input and output is different since visual c++ 2003 very annoying
#include
using namespace std;
extern "C"
{
#include
}
typedef struct luaMemFile
{
const char *text;
size_t size;
} luaMemFile;
//const char *readMemFile(
static const char *readMemFile(lua_State *, void *ud, size_t *size)
{
// Convert the ud pointer (UserData) to a pointer of our structure
luaMemFile *luaMF = (luaMemFile *) ud;
// Are we done?
if(luaMF->size == 0)
return NULL;
// Read everything at once
// And set size to zero to tell the next call we're done
*size = luaMF->size;
luaMF->size = 0;
// Return a pointer to the readed text
return luaMF->text;
}
inline LUA_API lua_Number lua_popnumber(lua_State *L)
{
register lua_Number tmp = lua_tonumber(L, lua_gettop(L));
lua_pop(L, 1);
return tmp;
}
int main(int argc, char* const argv[])
{
char* waveoutName = argv[1]; //output file
char* luaName = argv[2]; //lua file
int timetinloop = atof( argv[3] );
Stk::setSampleRate( 44100.0 ); //global sample rate
FileWvOut output;
//Open a 16 bit, one channel WAV formatted output file
output.openFile( waveoutName, 1,FileWrite::FILE_WAV, Stk::STK_SINT16 );
//char *testscript = loadthefile here
// Initialize LUA
lua_State *L;// = lua_open(); this may work again when the files are included
//
luaMemFile luaMF;
// Load the command and try to execute it...
ifstream in(luaName);
cout << luaMF.text;
luaMF.size = strlen(luaMF.text);
if(lua_load(L, readMemFile, &luaMF, "test function") == 0)
{
// Execute the loaded command...
// The function takes 0 parameters and will always return for ( int i=0; i<44100; i++) {
for ( int i=0; i<44100; i++) { //time is 44100 per second
//StkFloat y = sin( 2 * PI * 440.0 * i / 44100.0 );
lua_pcall(L, 0, 1, 0);
//output.tick( y ); 1 result this is the output function rewrite
{
// There was no error
// Let's get the result from the stack
lua_Number result = lua_tonumber(L, lua_gettop(L));
// Some output
char tmp[64];}}
//wsprintf(tmp, "The result was : %u!", (int) result);
//MessageBox(HWND_DESKTOP, testscript, tmp, MB_OK);
}
// else
// {
// Some output
//MessageBox(HWND_DESKTOP, testscript, "lua_call error!", MB_OK);
// }
// Clean-up the stack
//lua_pop(L, 1);
// }
// else
// {
// There was a lua_load error...
// Pop the error value from the stack
// lua_pop(L, 1);
// Some output
//MessageBox(HWND_DESKTOP, testscript, "lua_load error!", MB_OK);
// }
// Verify the stack and clean-up the LUA state
_ASSERT(lua_gettop(L) == 0);
lua_close(L);
output.closeFile();
return 0;
}
// stk with lua plugins
// questions comments idea and bug fixes (exc) can be directed here bbs
//This is a proof of concept and still buggy
//but it does compile in visual c++ 2008 express
//lua version is 5.1.0 (I think
//requires lua and The Synthesis ToolKit ( stk ) to compile
#include
#include
#include "FileWvOut.h"
#include "WvOut.h"
//#include FileWvOut.h"
//#include
#include
#include
//input and output is different since visual c++ 2003 very annoying
#include
using namespace std;
extern "C"
{
#include
}
typedef struct luaMemFile
{
const char *text;
size_t size;
} luaMemFile;
//const char *readMemFile(
static const char *readMemFile(lua_State *, void *ud, size_t *size)
{
// Convert the ud pointer (UserData) to a pointer of our structure
luaMemFile *luaMF = (luaMemFile *) ud;
// Are we done?
if(luaMF->size == 0)
return NULL;
// Read everything at once
// And set size to zero to tell the next call we're done
*size = luaMF->size;
luaMF->size = 0;
// Return a pointer to the readed text
return luaMF->text;
}
inline LUA_API lua_Number lua_popnumber(lua_State *L)
{
register lua_Number tmp = lua_tonumber(L, lua_gettop(L));
lua_pop(L, 1);
return tmp;
}
int main(int argc, char* const argv[])
{
char* waveoutName = argv[1]; //output file
char* luaName = argv[2]; //lua file
int timetinloop = atof( argv[3] );
Stk::setSampleRate( 44100.0 ); //global sample rate
FileWvOut output;
//Open a 16 bit, one channel WAV formatted output file
output.openFile( waveoutName, 1,FileWrite::FILE_WAV, Stk::STK_SINT16 );
//char *testscript = loadthefile here
// Initialize LUA
lua_State *L;// = lua_open(); this may work again when the files are included
//
luaMemFile luaMF;
// Load the command and try to execute it...
ifstream in(luaName);
cout << luaMF.text;
luaMF.size = strlen(luaMF.text);
if(lua_load(L, readMemFile, &luaMF, "test function") == 0)
{
// Execute the loaded command...
// The function takes 0 parameters and will always return for ( int i=0; i<44100; i++) {
for ( int i=0; i<44100; i++) { //time is 44100 per second
//StkFloat y = sin( 2 * PI * 440.0 * i / 44100.0 );
lua_pcall(L, 0, 1, 0);
//output.tick( y ); 1 result this is the output function rewrite
{
// There was no error
// Let's get the result from the stack
lua_Number result = lua_tonumber(L, lua_gettop(L));
// Some output
char tmp[64];}}
//wsprintf(tmp, "The result was : %u!", (int) result);
//MessageBox(HWND_DESKTOP, testscript, tmp, MB_OK);
}
// else
// {
// Some output
//MessageBox(HWND_DESKTOP, testscript, "lua_call error!", MB_OK);
// }
// Clean-up the stack
//lua_pop(L, 1);
// }
// else
// {
// There was a lua_load error...
// Pop the error value from the stack
// lua_pop(L, 1);
// Some output
//MessageBox(HWND_DESKTOP, testscript, "lua_load error!", MB_OK);
// }
// Verify the stack and clean-up the LUA state
_ASSERT(lua_gettop(L) == 0);
lua_close(L);
output.closeFile();
return 0;
}
Labels: c++, lua, stk, Synthesis ToolKit, visual c++