musiclight2/lua_wrappers.c
Thomas Kolb d4a2e7ef4c Added lua scripting capabilities
- basic configuration is now done via a lua script
- all the animation is generated by a lua script (see pulsetunnel.lua
  and vumeter.lua for examples)
- basic calculations (FFT, RMS) are done in C and accessible on demand
  from the lua scripts
2012-07-28 01:30:20 +02:00

74 lines
1.7 KiB
C

/*
* vim: sw=2 ts=2 expandtab
*
* THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
* <cfr34k@tkolb.de> wrote this file. As long as you retain this notice you can
* do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a pizza in return. - Thomas Kolb
*/
#include <semaphore.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "fft.h"
#include "config.h"
#include "lua_utils.h"
#include "lua_wrappers.h"
extern sem_t fftSemaphore;
extern double fft[BLOCK_LEN];
extern sample signal[BLOCK_LEN];
extern double rms;
static int l_get_energy_in_band(lua_State *L) {
int lowerFreq, higherFreq;
luaL_checktype(L, 1, LUA_TNUMBER);
lowerFreq = lua_tointeger(L, 1);
luaL_checktype(L, 2, LUA_TNUMBER);
higherFreq = lua_tointeger(L, 2);
sem_wait(&fftSemaphore);
lua_pushnumber(L, get_energy_in_band(fft, lowerFreq, higherFreq));
sem_post(&fftSemaphore);
return 1; // number of arguments
}
static int l_get_fft(lua_State *L) {
sem_wait(&fftSemaphore);
lua_pushdoublearray(L, fft, BLOCK_LEN);
sem_post(&fftSemaphore);
return 1; // number of return values
}
static int l_get_signal(lua_State *L) {
sem_wait(&fftSemaphore);
lua_pushsamplearray(L, signal, BLOCK_LEN);
sem_post(&fftSemaphore);
return 1; // number of return values
}
static int l_get_rms(lua_State *L) {
sem_wait(&fftSemaphore);
lua_pushnumber(L, rms);
sem_post(&fftSemaphore);
return 1; // number of return values
}
void lua_register_funcs(lua_State *L) {
lua_register(L, "get_energy_in_band", l_get_energy_in_band);
lua_register(L, "get_fft", l_get_fft);
lua_register(L, "get_signal", l_get_signal);
lua_register(L, "get_rms", l_get_rms);
}