<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 <stdio.h>
#include <stdlib.h>

#include <timely.h>

#include <lv2/lv2plug.in/ns/ext/log/log.h>

#define TIMELY_PREFIX		"http://open-music-kontrollers.ch/lv2/timely#"
#define TIMELY_TEST_URI	TIMELY_PREFIX"test"

typedef struct _plughandle_t plughandle_t;

struct _plughandle_t {
	LV2_URID_Map *map;
	LV2_Log_Log *log;
	timely_t timely;

	LV2_URID log_trace;

	const LV2_Atom_Sequence *event_in;
};

static int
_log_vprintf(plughandle_t *handle, LV2_URID type, const char *fmt, va_list args)
{
	return handle->log->vprintf(handle->log->handle, type, fmt, args);
}

// non-rt || rt with LV2_LOG__Trace
static int
_log_printf(plughandle_t *handle, LV2_URID type, const char *fmt, ...)
{
  va_list args;
	int ret;

  va_start (args, fmt);
	ret = _log_vprintf(handle, type, fmt, args);
  va_end(args);

	return ret;
}

static void
_timely_cb(timely_t *timely, int64_t frames, LV2_URID type, void *data)
{
	plughandle_t *handle = data;

	const char *uri = NULL;

	if(type == TIMELY_URI_BAR_BEAT(timely))
		uri = LV2_TIME__barBeat;
	else if(type == TIMELY_URI_BAR(timely))
		uri = LV2_TIME__bar;
	else if(type == TIMELY_URI_BEAT_UNIT(timely))
		uri = LV2_TIME__beatUnit;
	else if(type == TIMELY_URI_BEATS_PER_BAR(timely))
		uri = LV2_TIME__beatsPerBar;
	else if(type == TIMELY_URI_BEATS_PER_MINUTE(timely))
		uri = LV2_TIME__beatsPerMinute;
	else if(type == TIMELY_URI_FRAME(timely))
		uri = LV2_TIME__frame;
	else if(type == TIMELY_URI_FRAMES_PER_SECOND(timely))
		uri = LV2_TIME__framesPerSecond;
	else if(type == TIMELY_URI_SPEED(timely))
		uri = LV2_TIME__speed;

	_log_printf(data, handle->log_trace, "%4li %s (%i)", frames, uri, type);
}

static LV2_Handle
instantiate(const LV2_Descriptor* descriptor, double rate,
	const char *bundle_path, const LV2_Feature *const *features)
{
	plughandle_t *handle = calloc(1, sizeof(plughandle_t));
	if(!handle)
		return NULL;

	for(unsigned i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_URID__map))
			handle->map = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->log = features[i]->data;
	}

	if(!handle->map)
	{
		fprintf(stderr,
			"%s: Host does not support urid:map\n", descriptor->URI);
		free(handle);
		return NULL;
	}
	if(!handle->log)
	{
		fprintf(stderr,
			"%s: Host does not support log:log\n", descriptor->URI);
		free(handle);
		return NULL;
	}

	handle->log_trace = handle->map->map(handle->map->handle, LV2_LOG__Trace);

	timely_mask_t mask = TIMELY_MASK_BAR_BEAT
		| TIMELY_MASK_BAR
		| TIMELY_MASK_BEAT_UNIT
		| TIMELY_MASK_BEATS_PER_BAR
		| TIMELY_MASK_BEATS_PER_MINUTE
		//| TIMELY_MASK_FRAME
		| TIMELY_MASK_FRAMES_PER_SECOND
		| TIMELY_MASK_SPEED
		| TIMELY_MASK_BAR_BEAT_WHOLE
		| TIMELY_MASK_BAR_WHOLE;
	timely_init(&handle->timely, handle->map, rate, mask, _timely_cb, handle);

	return handle;
}

static void
connect_port(LV2_Handle instance, uint32_t port, void *data)
{
	plughandle_t *handle = (plughandle_t *)instance;

	switch(port)
	{
		case 0:
			handle->event_in = (const LV2_Atom_Sequence *)data;
			break;
		default:
			break;
	}
}

static void
run(LV2_Handle instance, uint32_t nsamples)
{
	plughandle_t *handle = instance;
	int64_t from = 0;

	LV2_ATOM_SEQUENCE_FOREACH(handle->event_in, ev)
	{
		const int64_t to = ev->time.frames;
		const LV2_Atom_Object *obj = (const LV2_Atom_Object *)&ev->body;

		const int handled = timely_advance(&handle->timely, obj, from, to);
		(void)handled;
		from = to;
	}

	timely_advance(&handle->timely, NULL, from, nsamples);<commit_msg>fix timely range upper bound.<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 <stdio.h>
#include <stdlib.h>

#include <timely.h>

#include <lv2/lv2plug.in/ns/ext/log/log.h>

#define TIMELY_PREFIX		"http://open-music-kontrollers.ch/lv2/timely#"
#define TIMELY_TEST_URI	TIMELY_PREFIX"test"

typedef struct _plughandle_t plughandle_t;

struct _plughandle_t {
	LV2_URID_Map *map;
	LV2_Log_Log *log;
	timely_t timely;

	LV2_URID log_trace;

	const LV2_Atom_Sequence *event_in;
};

static int
_log_vprintf(plughandle_t *handle, LV2_URID type, const char *fmt, va_list args)
{
	return handle->log->vprintf(handle->log->handle, type, fmt, args);
}

// non-rt || rt with LV2_LOG__Trace
static int
_log_printf(plughandle_t *handle, LV2_URID type, const char *fmt, ...)
{
  va_list args;
	int ret;

  va_start (args, fmt);
	ret = _log_vprintf(handle, type, fmt, args);
  va_end(args);

	return ret;
}

static void
_timely_cb(timely_t *timely, int64_t frames, LV2_URID type, void *data)
{
	plughandle_t *handle = data;

	const char *uri = NULL;

	if(type == TIMELY_URI_BAR_BEAT(timely))
		uri = LV2_TIME__barBeat;
	else if(type == TIMELY_URI_BAR(timely))
		uri = LV2_TIME__bar;
	else if(type == TIMELY_URI_BEAT_UNIT(timely))
		uri = LV2_TIME__beatUnit;
	else if(type == TIMELY_URI_BEATS_PER_BAR(timely))
		uri = LV2_TIME__beatsPerBar;
	else if(type == TIMELY_URI_BEATS_PER_MINUTE(timely))
		uri = LV2_TIME__beatsPerMinute;
	else if(type == TIMELY_URI_FRAME(timely))
		uri = LV2_TIME__frame;
	else if(type == TIMELY_URI_FRAMES_PER_SECOND(timely))
		uri = LV2_TIME__framesPerSecond;
	else if(type == TIMELY_URI_SPEED(timely))
		uri = LV2_TIME__speed;

	_log_printf(data, handle->log_trace, "%4li %s (%i)", frames, uri, type);
}

static LV2_Handle
instantiate(const LV2_Descriptor* descriptor, double rate,
	const char *bundle_path, const LV2_Feature *const *features)
{
	plughandle_t *handle = calloc(1, sizeof(plughandle_t));
	if(!handle)
		return NULL;

	for(unsigned i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_URID__map))
			handle->map = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->log = features[i]->data;
	}

	if(!handle->map)
	{
		fprintf(stderr,
			"%s: Host does not support urid:map\n", descriptor->URI);
		free(handle);
		return NULL;
	}
	if(!handle->log)
	{
		fprintf(stderr,
			"%s: Host does not support log:log\n", descriptor->URI);
		free(handle);
		return NULL;
	}

	handle->log_trace = handle->map->map(handle->map->handle, LV2_LOG__Trace);

	timely_mask_t mask = TIMELY_MASK_BAR_BEAT
		| TIMELY_MASK_BAR
		| TIMELY_MASK_BEAT_UNIT
		| TIMELY_MASK_BEATS_PER_BAR
		| TIMELY_MASK_BEATS_PER_MINUTE
		//| TIMELY_MASK_FRAME
		| TIMELY_MASK_FRAMES_PER_SECOND
		| TIMELY_MASK_SPEED
		| TIMELY_MASK_BAR_BEAT_WHOLE
		| TIMELY_MASK_BAR_WHOLE;
	timely_init(&handle->timely, handle->map, rate, mask, _timely_cb, handle);

	return handle;
}

static void
connect_port(LV2_Handle instance, uint32_t port, void *data)
{
	plughandle_t *handle = (plughandle_t *)instance;

	switch(port)
	{
		case 0:
			handle->event_in = (const LV2_Atom_Sequence *)data;
			break;
		default:
			break;
	}
}

static void
run(LV2_Handle instance, uint32_t nsamples)
{
	plughandle_t *handle = instance;
	int64_t from = 0;

	LV2_ATOM_SEQUENCE_FOREACH(handle->event_in, ev)
	{
		const int64_t to = ev->time.frames;
		const LV2_Atom_Object *obj = (const LV2_Atom_Object *)&ev->body;

		const int handled = timely_advance(&handle->timely, obj, from, to);
		(void)handled;
		from = to;
	}

	timely_advance(&handle->timely, NULL, from, nsamples-1);
{'fraction_non_alphanumeric': 0.11862367661212704, 'fraction_numerical': 0.014076034648700674, 'mean_word_length': 4.841883345045678, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 71, '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': '7006422', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3150, 'n_words': 747}