@@ -16,9 +16,6 @@ LiveProgrammableDSP::LiveProgrammableDSP() : treeState(*this, nullptr, "Paramete
1616 compileSucessfully = 0 ;
1717 codehandleInit = 0 ;
1818 codehandleProcess = 0 ;
19- actualLuaVM = 0 ;
20- luaStructRef = LUA_NOREF;
21- luaProcessRef = LUA_NOREF;
2219 NSEEL_start ();
2320 vm = NSEEL_VM_alloc(); // create virtual machine
2421 vmFs = NSEEL_VM_regvar(vm, " srate" );
@@ -96,14 +93,6 @@ LiveProgrammableDSP::~LiveProgrammableDSP()
9693 NSEEL_VM_free (vm);
9794 }
9895 NSEEL_quit ();
99- if (actualLuaVM)
100- {
101- if (luaProcessRef != LUA_NOREF)
102- luaL_unref (actualLuaVM, LUA_REGISTRYINDEX, luaProcessRef);
103- if (luaStructRef != LUA_NOREF)
104- luaL_unref (actualLuaVM, LUA_REGISTRYINDEX, luaStructRef);
105- lua_close (actualLuaVM);
106- }
10796}
10897AudioProcessorValueTreeState::ParameterLayout LiveProgrammableDSP::createParameterLayout ()
10998{
@@ -124,26 +113,6 @@ void LiveProgrammableDSP::prepareToPlay(double sampleRate, int samplesPerBlock)
124113 ScopedLock lock (criticalSection);
125114 NSEEL_code_execute (codehandleInit);
126115 }
127- if (compileSucessfully == 2 )
128- {
129- ScopedLock lock (criticalSection);
130- int top = lua_gettop (actualLuaVM);
131- int retType = lua_getglobal (actualLuaVM, " init" ); // function to be called
132- lua_pushnumber (actualLuaVM, fs); // push 1st argument
133- // do the call (1 arguments, 1 table)
134- lua_call (actualLuaVM, 1 , LUA_MULTRET);
135- // check the return value
136- if (lua_gettop (actualLuaVM) - top) {
137- // store userdata to a pointer
138- luaStructRef = luaL_ref (actualLuaVM, LUA_REGISTRYINDEX);
139- }
140- else
141- {
142- char erMsg[70 ] = " Lua: Fail to get reference while re-set sample rate, stop compiling\n " ;
143- EEL_STRING_STDOUT_WRITE (erMsg, 69 );
144- lua_close (actualLuaVM);
145- }
146- }
147116}
148117const String LiveProgrammableDSP::getName () const
149118{
@@ -203,6 +172,7 @@ void LiveProgrammableDSP::processBlock(AudioBuffer<float>& buffer, MidiBuffer&)
203172{
204173 // number of samples per buffer
205174 const int n = buffer.getNumSamples ();
175+ const int pcmChannels = 6 ;
206176 // input channels
207177 const float *inputs[6 ] = { buffer.getReadPointer (0 ), buffer.getReadPointer (1 ), buffer.getReadPointer (2 ), buffer.getReadPointer (3 ), buffer.getReadPointer (4 ), buffer.getReadPointer (5 ) };
208178 // output channels
@@ -227,30 +197,6 @@ void LiveProgrammableDSP::processBlock(AudioBuffer<float>& buffer, MidiBuffer&)
227197 outputs[5 ][i] = (float )*input6;
228198 }
229199 }
230- if (compileSucessfully == 2 )
231- {
232- ScopedLock lock (criticalSection);
233- for (int i = 0 ; i < n; i++)
234- {
235- lua_rawgeti (actualLuaVM, LUA_REGISTRYINDEX, luaProcessRef);
236- lua_rawgeti (actualLuaVM, LUA_REGISTRYINDEX, luaStructRef);
237- // Push variable to stack
238- lua_pushnumber (actualLuaVM, inputs[0 ][i]);
239- lua_pushnumber (actualLuaVM, inputs[1 ][i]);
240- lua_pushnumber (actualLuaVM, inputs[2 ][i]);
241- lua_pushnumber (actualLuaVM, inputs[3 ][i]);
242- lua_pushnumber (actualLuaVM, inputs[4 ][i]);
243- lua_pushnumber (actualLuaVM, inputs[5 ][i]);
244- lua_call (actualLuaVM, 7 , 6 );
245- outputs[0 ][i] = (float )lua_tonumber (actualLuaVM, -6 );
246- outputs[1 ][i] = (float )lua_tonumber (actualLuaVM, -5 );
247- outputs[2 ][i] = (float )lua_tonumber (actualLuaVM, -4 );
248- outputs[3 ][i] = (float )lua_tonumber (actualLuaVM, -3 );
249- outputs[4 ][i] = (float )lua_tonumber (actualLuaVM, -2 );
250- outputs[5 ][i] = (float )lua_tonumber (actualLuaVM, -1 );
251- lua_pop (actualLuaVM, 6 );
252- }
253- }
254200}
255201bool LiveProgrammableDSP::hasEditor () const
256202{
@@ -350,177 +296,11 @@ int LiveProgrammableDSP::compileEEL(const char *eelCode, size_t strLen)
350296 free (codeTextProcess);
351297 return 1 ;
352298}
353- int LiveProgrammableDSP::validateLua (const char *luaCode, size_t strLen)
354- {
355- lua_State *L = luaL_newstate (); // create state
356- luaL_openlibs (L); // open standard libraries
357- lua_settop (L, 0 );
358- char *concatedStr = (char *)malloc (strLen + 256 );
359- int outLen = stbsp_snprintf (concatedStr, strLen + 256 , " %s\n process(init(%lf), %lf, %lf)" , luaCode, nseel_int_rand (192000.0 ), nseel_int_rand (1.0 ), nseel_int_rand (1.0 ));
360- int err = luaL_loadbuffer (L, concatedStr, outLen, " LiveProg" );
361- free (concatedStr);
362- EEL_STRING_STDOUT_WRITE (" Lua: Test run\n " , 14 );
363- if (err)
364- {
365- char erMsg[256 ] = { 0 };
366- int msgLen = stbsp_snprintf (erMsg, 256 , " Lua error %d\n %s\n " , err, lua_tostring (L, -1 ));
367- EEL_STRING_STDOUT_WRITE (erMsg, msgLen);
368- lua_pop (L, 1 ); // pop error message from the stack
369- lua_close (L);
370- return -1 ;
371- }
372- err = lua_pcall (L, 0 , 0 , 0 );
373- if (err)
374- {
375- char erMsg[256 ] = { 0 };
376- int msgLen = stbsp_snprintf (erMsg, 256 , " Lua error %d\n %s\n " , err, lua_tostring (L, -1 ));
377- EEL_STRING_STDOUT_WRITE (erMsg, msgLen);
378- lua_pop (L, 1 ); // pop error message from the stack
379- lua_close (L);
380- return -1 ;
381- }
382- // Table return
383- int userDataRef = LUA_NOREF;
384- int top = lua_gettop (L);
385- int retType = lua_getglobal (L, " init" ); // function to be called
386- lua_pushnumber (L, fs); // push 1st argument
387- // do the call (1 arguments, 1 table)
388- if (lua_pcall (L, 1 , LUA_MULTRET, 0 ) != 0 )
389- {
390- char erMsg[256 ] = { 0 };
391- int msgLen = stbsp_snprintf (erMsg, 256 , " Lua error %d\n error running function 'init': %s\n " , err, lua_tostring (L, -1 ));
392- EEL_STRING_STDOUT_WRITE (erMsg, msgLen);
393- lua_pop (L, 1 ); // pop error message from the stack
394- lua_close (L);
395- return 1 ;
396- }
397- int retClass = lua_istable (L, -1 );
398- if (!retClass)
399- {
400- char erMsg[55 ] = " Lua: init function must return table, stop compiling\n " ;
401- EEL_STRING_STDOUT_WRITE (erMsg, 54 );
402- lua_close (L);
403- return 2 ;
404- }
405- // check the return value
406- if (lua_gettop (L) - top) {
407- // store userdata to a pointer
408- userDataRef = luaL_ref (L, LUA_REGISTRYINDEX);
409- }
410- else
411- {
412- char erMsg[45 ] = " Lua: Fail to get reference, stop compiling\n " ;
413- EEL_STRING_STDOUT_WRITE (erMsg, 44 );
414- lua_close (L);
415- return 3 ;
416- }
417- lua_getglobal (L, " process" ); // function to be called
418- int process = luaL_ref (L, LUA_REGISTRYINDEX);
419- if (userDataRef != LUA_NOREF && userDataRef != LUA_REFNIL)
420- {
421- lua_rawgeti (L, LUA_REGISTRYINDEX, process);
422- lua_rawgeti (L, LUA_REGISTRYINDEX, userDataRef);
423- // Push variable to stack
424- lua_pushnumber (L, 0.5 );
425- lua_pushnumber (L, -0.15 );
426- if (lua_pcall (L, 3 , 2 , 0 ) != 0 )
427- {
428- char erMsg[256 ] = { 0 };
429- int msgLen = stbsp_snprintf (erMsg, 256 , " Lua error %d\n error running function 'process': %s\n " , err, lua_tostring (L, -1 ));
430- EEL_STRING_STDOUT_WRITE (erMsg, msgLen);
431- lua_pop (L, 1 ); // pop error message from the stack
432- lua_close (L);
433- return 1 ;
434- }
435- double y1 = lua_tonumber (L, -2 );
436- double y2 = lua_tonumber (L, -1 );
437- lua_pop (L, 2 );
438- }
439- else
440- {
441- char erMsg[42 ] = " Lua: Reference go wrong, stop compiling\n " ;
442- EEL_STRING_STDOUT_WRITE (erMsg, 41 );
443- }
444- luaL_unref (L, LUA_REGISTRYINDEX, process);
445- luaL_unref (L, LUA_REGISTRYINDEX, userDataRef);
446- lua_close (L);
447- return LUA_OK;
448- }
449- void LiveProgrammableDSP::LoadLuaCode (const char *luaCode, size_t strLen)
450- {
451- ScopedLock lock (criticalSection);
452- compileSucessfully = 0 ;
453- int i;
454- if (actualLuaVM)
455- {
456- if (luaProcessRef != LUA_NOREF)
457- luaL_unref (actualLuaVM, LUA_REGISTRYINDEX, luaProcessRef);
458- luaProcessRef = LUA_NOREF;
459- if (luaStructRef != LUA_NOREF)
460- luaL_unref (actualLuaVM, LUA_REGISTRYINDEX, luaStructRef);
461- luaStructRef = LUA_NOREF;
462- lua_close (actualLuaVM);
463- actualLuaVM = 0 ;
464- }
465- actualLuaVM = luaL_newstate (); // create state
466- luaL_openlibs (actualLuaVM); // open standard libraries
467- lua_settop (actualLuaVM, 0 );
468- int err = luaL_loadbuffer (actualLuaVM, luaCode, strLen, " LiveProg" );
469- if (err)
470- {
471- char erMsg[256 ] = { 0 };
472- int msgLen = stbsp_snprintf (erMsg, 256 , " Lua error %d\n %s\n Fatal code loading error after validation\n " , err, lua_tostring (actualLuaVM, -1 ));
473- EEL_STRING_STDOUT_WRITE (erMsg, msgLen);
474- lua_pop (actualLuaVM, 1 ); // pop error message from the stack
475- lua_close (actualLuaVM);
476- return ;
477- }
478- lua_call (actualLuaVM, 0 , 0 );
479- int top = lua_gettop (actualLuaVM);
480- int retType = lua_getglobal (actualLuaVM, " init" ); // function to be called
481- lua_pushnumber (actualLuaVM, fs); // push 1st argument
482- // do the call (1 arguments, 1 table)
483- lua_call (actualLuaVM, 1 , LUA_MULTRET);
484- // check the return value
485- if (lua_gettop (actualLuaVM) - top) {
486- // store userdata to a pointer
487- luaStructRef = luaL_ref (actualLuaVM, LUA_REGISTRYINDEX);
488- }
489- else
490- {
491- char erMsg[105 ] = " Lua: Fail to get reference after validation, stop compiling\n Fatal get reference error after validation\n " ;
492- EEL_STRING_STDOUT_WRITE (erMsg, 104 );
493- lua_close (actualLuaVM);
494- return ;
495- }
496- lua_getglobal (actualLuaVM, " process" ); // function to be called
497- luaProcessRef = luaL_ref (actualLuaVM, LUA_REGISTRYINDEX);
498- compileSucessfully = 2 ;
499- EEL_STRING_STDOUT_WRITE (" Lua: Code looks OK\n " , 16 );
500- }
501- int LiveProgrammableDSP::compileLua (const char *luaCode, size_t strLen)
502- {
503- EEL_STRING_STDOUT_WRITE (" Code text is not EEL, compiling code as Lua\n " , 45 );
504- int ret = validateLua (luaCode, strLen);
505- if (ret != LUA_OK)
506- {
507- EEL_STRING_STDOUT_WRITE (" Lua compilation failed\n " , 24 );
508- compileSucessfully = 0 ;
509- return 0 ;
510- }
511- else
512- {
513- LoadLuaCode (luaCode, strLen);
514- return 1 ;
515- }
516- }
517299void LiveProgrammableDSP::playButtonClicked (String codetext)
518300{
519301 int strLen = codetext.length ();
520302 const char *textbuf = codetext.toRawUTF8 ();
521303 int eelSuccess = compileEEL (textbuf, strLen);
522- if (!eelSuccess)
523- int luaSuccess = compileLua (textbuf, strLen);
524304}
525305AudioProcessor* JUCE_CALLTYPE createPluginFilter ()
526306{
0 commit comments