<commit_before>/*
 * Copyright (c) 2015 <NAME> (<EMAIL>)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <api_time.h>
#include <api_atom.h>
#include <api_forge.h>

#include <timely.h>

static void
_ltimeresponder_cb(timely_t *timely, int64_t frames, LV2_URID type,
	void *data)
{
	lua_State *L = data;

	if(lua_geti(L, 5, type) != LUA_TNIL) // uservalue[type]
	{
		lua_pushvalue(L, 5); // uservalue
		lua_pushinteger(L, frames); // frames
		lua_pushvalue(L, 4); // data

		if(type == TIMELY_URI_BAR_BEAT(timely))
		{
			lua_pushnumber(L, TIMELY_BAR_BEAT_RAW(timely));
		}
		else if(type == TIMELY_URI_BAR(timely))
		{
			lua_pushinteger(L, TIMELY_BAR(timely));
		}
		else if(type == TIMELY_URI_BEAT_UNIT(timely))
		{
			lua_pushinteger(L, TIMELY_BEAT_UNIT(timely));
		}
		else if(type == TIMELY_URI_BEATS_PER_BAR(timely))
		{
			lua_pushnumber(L, TIMELY_BEATS_PER_BAR(timely));
		}
		else if(type == TIMELY_URI_BEATS_PER_MINUTE(timely))
		{
			lua_pushnumber(L, TIMELY_BEATS_PER_MINUTE(timely));
		}
		else if(type == TIMELY_URI_FRAME(timely))
		{
			lua_pushinteger(L, TIMELY_FRAME(timely));
		}
		else if(type == TIMELY_URI_FRAMES_PER_SECOND(timely))
		{
			lua_pushnumber(L, TIMELY_FRAMES_PER_SECOND(timely));
		}
		else if(type == TIMELY_URI_SPEED(timely))
		{
			lua_pushnumber(L, TIMELY_SPEED(timely));
		}
		else
		{
			lua_pushnil(L);
		}

		lua_call(L, 4, 0);
	}
	else
		lua_pop(L, 1); // nil
}

static int
_ltimeresponder__call(lua_State *L)
{
	//moony_t *moony = lua_touserdata(L, lua_upvalueindex(1));

	lua_settop(L, 5); // discard superfluous arguments
	// 1: self
	// 2: from
	// 3: to
	// 4: data aka forge
	// 5: atom || nil
	
	timely_t *timely = lua_touserdata(L, 1);
	int64_t from = luaL_checkinteger(L, 2);
	int64_t to = luaL_checkinteger(L, 3);
	latom_t *latom = NULL;
	if(luaL_testudata(L, 5, "latom"))
		latom = lua_touserdata(L, 5);
	lua_pop(L, 1); // atom

	lua_getuservalue(L, 1); // 5: uservalue
	const int handled = timely_advance(timely, latom ? (const LV2_Atom_Object *)latom->atom : NULL, from, to); //FIXME use body

	lua_pushboolean(L, handled); // handled vs not handled
	return 1;
}

int
_ltimeresponder_apply(lua_State *L)
{
	// 1: self
	// 2: atom
	
	lua_pushinteger(L, 0);
	lua_insert(L, 2); // from

	lua_pushinteger(L, 0);
	lua_insert(L, 3); // to

	lua_pushnil(L);
	lua_insert(L, 4); // data aka forge<commit_msg>use atom object body and timely_advance_body.<commit_after>/*
 * Copyright (c) 2015 <NAME> (<EMAIL>)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <api_time.h>
#include <api_atom.h>
#include <api_forge.h>

#include <timely.h>

static void
_ltimeresponder_cb(timely_t *timely, int64_t frames, LV2_URID type,
	void *data)
{
	lua_State *L = data;

	if(lua_geti(L, 5, type) != LUA_TNIL) // uservalue[type]
	{
		lua_pushvalue(L, 5); // uservalue
		lua_pushinteger(L, frames); // frames
		lua_pushvalue(L, 4); // data

		if(type == TIMELY_URI_BAR_BEAT(timely))
		{
			lua_pushnumber(L, TIMELY_BAR_BEAT_RAW(timely));
		}
		else if(type == TIMELY_URI_BAR(timely))
		{
			lua_pushinteger(L, TIMELY_BAR(timely));
		}
		else if(type == TIMELY_URI_BEAT_UNIT(timely))
		{
			lua_pushinteger(L, TIMELY_BEAT_UNIT(timely));
		}
		else if(type == TIMELY_URI_BEATS_PER_BAR(timely))
		{
			lua_pushnumber(L, TIMELY_BEATS_PER_BAR(timely));
		}
		else if(type == TIMELY_URI_BEATS_PER_MINUTE(timely))
		{
			lua_pushnumber(L, TIMELY_BEATS_PER_MINUTE(timely));
		}
		else if(type == TIMELY_URI_FRAME(timely))
		{
			lua_pushinteger(L, TIMELY_FRAME(timely));
		}
		else if(type == TIMELY_URI_FRAMES_PER_SECOND(timely))
		{
			lua_pushnumber(L, TIMELY_FRAMES_PER_SECOND(timely));
		}
		else if(type == TIMELY_URI_SPEED(timely))
		{
			lua_pushnumber(L, TIMELY_SPEED(timely));
		}
		else
		{
			lua_pushnil(L);
		}

		lua_call(L, 4, 0);
	}
	else
		lua_pop(L, 1); // nil
}

static int
_ltimeresponder__call(lua_State *L)
{
	//moony_t *moony = lua_touserdata(L, lua_upvalueindex(1));

	lua_settop(L, 5); // discard superfluous arguments
	// 1: self
	// 2: from
	// 3: to
	// 4: data aka forge
	// 5: atom || nil
	
	timely_t *timely = lua_touserdata(L, 1);
	int64_t from = luaL_checkinteger(L, 2);
	int64_t to = luaL_checkinteger(L, 3);
	latom_t *latom = NULL;
	if(luaL_testudata(L, 5, "latom"))
		latom = lua_touserdata(L, 5);
	lua_pop(L, 1); // atom

	lua_getuservalue(L, 1); // 5: uservalue
	const int handled = latom
		?  timely_advance_body(timely, latom->atom->size, latom->atom->type, latom->body.obj, from, to)
		:  timely_advance_body(timely, 0, 0, NULL, from, to);

	lua_pushboolean(L, handled); // handled vs not handled
	return 1;
}

int
_ltimeresponder_apply(lua_State *L)
{
	// 1: self
	// 2: atom
	
	lua_pushinteger(L, 0);
	lua_insert(L, 2); // from

	lua_pushinteger(L, 0);
	lua_insert(L, 3); // to

	lua_pushnil(L);
	lua_insert(L, 4); // data aka forge
{'fraction_non_alphanumeric': 0.13174872665534804, 'fraction_numerical': 0.016808149405772497, 'mean_word_length': 4.078448275862069, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4907258', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2333, 'n_words': 554}