Sound2Light auf RGB(W)-LED-Leisten
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_wrappers.c 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * vim: sw=2 ts=2 expandtab
  3. *
  4. * THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
  5. * <cfr34k@tkolb.de> wrote this file. As long as you retain this notice you can
  6. * do whatever you want with this stuff. If we meet some day, and you think
  7. * this stuff is worth it, you can buy me a pizza in return. - Thomas Kolb
  8. */
  9. #include <semaphore.h>
  10. #include <lua.h>
  11. #include <lualib.h>
  12. #include <lauxlib.h>
  13. #include "fft.h"
  14. #include "config.h"
  15. #include "lua_utils.h"
  16. #include "lua_wrappers.h"
  17. extern sem_t fftSemaphore;
  18. extern double fft[BLOCK_LEN];
  19. extern sample signal[BLOCK_LEN];
  20. extern double rms;
  21. static int l_get_energy_in_band(lua_State *L) {
  22. int lowerFreq, higherFreq;
  23. luaL_checktype(L, 1, LUA_TNUMBER);
  24. lowerFreq = lua_tointeger(L, 1);
  25. luaL_checktype(L, 2, LUA_TNUMBER);
  26. higherFreq = lua_tointeger(L, 2);
  27. sem_wait(&fftSemaphore);
  28. lua_pushnumber(L, get_energy_in_band(fft, lowerFreq, higherFreq));
  29. sem_post(&fftSemaphore);
  30. return 1; // number of arguments
  31. }
  32. static int l_get_fft(lua_State *L) {
  33. sem_wait(&fftSemaphore);
  34. lua_pushdoublearray(L, fft, BLOCK_LEN);
  35. sem_post(&fftSemaphore);
  36. return 1; // number of return values
  37. }
  38. static int l_get_signal(lua_State *L) {
  39. sem_wait(&fftSemaphore);
  40. lua_pushsamplearray(L, signal, BLOCK_LEN);
  41. sem_post(&fftSemaphore);
  42. return 1; // number of return values
  43. }
  44. static int l_get_rms(lua_State *L) {
  45. sem_wait(&fftSemaphore);
  46. lua_pushnumber(L, rms);
  47. sem_post(&fftSemaphore);
  48. return 1; // number of return values
  49. }
  50. void lua_register_funcs(lua_State *L) {
  51. lua_register(L, "get_energy_in_band", l_get_energy_in_band);
  52. lua_register(L, "get_fft", l_get_fft);
  53. lua_register(L, "get_signal", l_get_signal);
  54. lua_register(L, "get_rms", l_get_rms);
  55. }