musiclight2/lua_wrappers.c

74 rivejä
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);
}