<commit_before>/* 
 * jcom.map
 * External for Jamoma: map input to output: y=f(x)
 * Copyright © 2007
 * 
 * License: This code is licensed under the terms of the GNU LGPL
 * http://www.gnu.org/licenses/lgpl.html 
 */

#include "Jamoma.h"


// Data Structure for this object
typedef struct _map{
	t_object		ob;
	void			*outlet;
	t_symbol		*attr_function;
	double			attr_inputMin;
	double			attr_inputMax;
	double			attr_outputMin;
	double			attr_outputMax;
	double 			a, b;				// Coefficients used for normalizing input
	double			c, d;				// Coefficients used for scaling normalized output
	TTAudioObject	*functionUnit;
	bool			valid;				// true if the functionUnit can be used
} t_map;


// Prototypes for methods
void*		map_new(t_symbol *name, long argc, t_atom *argv);
void		map_free(t_map *obj);
void		map_assist(t_map *obj, void *b, long m, long a, char *s);
void		map_int(t_map *obj, long x);
void		map_float(t_map *obj, double x);
void		map_getFunctions(t_map *x, t_symbol *msg, long argc, t_atom *argv);
void		map_getParameter(t_map *x, t_symbol *msg, long argc, t_atom *argv);
void		map_getFunctionParameters(t_map *x, t_symbol *msg, long argc, t_atom *argv);
void		map_setParameter(t_map *x, t_symbol *msg, long argc, t_atom *argv);
t_max_err	map_setFunction(t_map *obj, void *attr, long argc, t_atom *argv);
t_max_err	map_setInputMin(t_map *obj, void *attr, long argc, t_atom *argv);
t_max_err	map_setInputMax(t_map *obj, void *attr, long argc, t_atom *argv);
void 		map_scaleInput(t_map *obj);
t_max_err	map_setOutputMin(t_map *obj, void *attr, long argc, t_atom *argv);
t_max_err	map_setOutputMax(t_map *obj, void *attr, long argc, t_atom *argv);
void 		map_scaleOutput(t_map *obj);


// Globals
t_class		*map_class;			// Required. Global pointing to this class


/************************************************************************************/
// Main() Function

int JAMOMA_EXPORT_MAXOBJ main(void)
{
	t_class *c;
	
	jamoma_init();
	common_symbols_init();

	// Define our class
	c = class_new("jcom.map",(method)map_new, (method)map_free, sizeof(t_map), (method)0L, A_GIMME, 0);

	// Make methods accessible for our class: 
	class_addmethod(c, (method)map_int,						"int", A_GIMME, 0L);
	class_addmethod(c, (method)map_float,					"float", A_GIMME, 0L);
	class_addmethod(c, (method)map_getFunctions,			"functions.get", A_GIMME, 0);
 	class_addmethod(c, (method)map_getParameter,			"parameter.get", A_GIMME, 0);
	class_addmethod(c, (method)map_getFunctionParameters,	"function.parameters.get", A_GIMME, 0);
 	class_addmethod(c, (method)map_setParameter,			"parameter", A_GIMME, 0);
	class_addmethod(c, (method)map_assist,					"assist", A_CANT, 0L); 
    class_addmethod(c, (method)object_obex_dumpout,			"dumpout", A_CANT,0);

	// ATTRIBUTE: set the function to use
	class_addattr(c, 
		attr_offset_new("function", _sym_symbol, 0,
		(method)0, (method)map_setFunction, calcoffset(t_map, attr_function)));
	// ATTRIBUTE: set the input minimum value
	class_addattr(c,
		attr_offset_new("inputMin", _sym_float64, 0,
		(method)0, (method)map_setInputMin, calcoffset(t_map, attr_inputMin)));
	// ATTRIBUTE: set the input maximum value
	class_addattr(c,
		attr_offset_new("inputMax", _sym_float64, 0,
		(method)0, (method)map_setInputMax, calcoffset(t_map, attr_inputMax)));	
		
	// ATTRIBUTE: set the output minimum value
	class_addattr(c,
		attr_offset_new("outputMin", _sym_float64, 0,
		(method)0, (method)map_setOutputMin, calcoffset(t_map, attr_outputMin)));
	// ATTRIBUTE: set the output maximum value
	class_addattr(c,
		attr_offset_new("outputMax", _sym_float64, 0,
		(method)0, (method)map_setOutputMax, calcoffset(t_map, attr_outputMax)));

	// Finalize our class
	class_register(CLASS_BOX, c);
	map_class = c;
	return 0;
}


/************************************************************************************/
// Object Life

void *map_new(t_symbol *name, long argc, t_atom *argv)
{
	t_map *obj;									// Declare an object (based on our struct)

	obj = (t_map *)object_alloc(map_class);		// Create object, store pointer to it (get 1 inlet free)
	if(obj){
		object_obex_store((void *)obj, _sym_dumpout, (object *)outlet_new(obj,NULL));
	    obj->outlet = outlet_new(obj, 0);
		obj->attr_function = _sym_nothing;
		obj->attr_inputMin = 0;
		obj->attr_inputMax = 1;
		obj->attr_outputMin = 0;
		obj->attr_outputMax = 1;
		obj->functionUnit = NULL;
		obj->valid = false;
		attr_args_process(obj, argc, argv);
		map_scaleInput(obj);
		map_scaleOutput(obj);
		if(!obj->functionUnit)
			object_attr_setsym(obj, gensym("function"), gensym("linear"));
	}
	return obj;										// Return pointer to our instance
}


void map_free(t_map *obj)
{
	if(obj->functionUnit)
		delete obj->functionUnit;
}


/************************************************************************************/
// Methods bound to input/inlets

// Method for Assistance Messages
void map_assist(t_map *x, void *b, long msg, long arg, char *dst)
{
	if(msg==1) 							// Inlets
		strcpy(dst, "x");
	else if(msg==2){ 					// Outlets
		switch(arg){
			case 0: strcpy(dst, "y=f(x)"); break;
			default: strcpy(dst, "dumpout"); break;
 		}
 	}
}


void map_int(t_map *obj, long x)
{
	map_float(obj, (double)x);
}


void map_float(t_map *obj, double x)
{
	double y;
	
	if(obj->valid){
		//y = obj->c * obj->functionUnit->map(obj->a * x + obj->b) + obj->d;
		obj->functionUnit->calculate(obj->a * x + obj->b, y);
		y = obj->c * y + obj->d;
		outlet_float(obj->outlet, y);
	}
}


void map_getFunctions(t_map *obj, t_symbol *msg, long argc, t_atom *argv)
{
	t_atom		a[2];
	long		numFunctions = 0;
	long		i;
	TTValue		functionNames;
	TTSymbol*	aName;
	
	atom_setsym(a+0, gensym("clear"));
	object_obex_dumpout(obj, gensym("functions"), 1, a);
	
	FunctionLib::getUnitNames(functionNames);
	numFunctions = functionNames.getSize();
	for(i=0; i<numFunctions; i++){
		functionNames.get(i, &aName);
		atom_setsym(a+0, gensym("append"));
		atom_setsym(a+1, gensym((char*)aName->getCString()));
		object_obex_dumpout(obj, gensym("functions"), 2, a);
		
		atom_setsym(a, obj->attr_function);
		object_obex_dumpout(obj, gensym("functions"), 1, a);
	}
}


void map_getParameter(t_map *obj, t_symbol *msg, long argc, t_atom *argv)
{
	t_atom*		a;
	TTSymbol*	parameterName;
	TTValue		parameterValue;
	int			numValues;
	int			i;
	TTSymbol*	tempSymbol;
	double		tempValue;
	<commit_msg>jcom.map: fixed bug with "functions.get" message<commit_after>/* 
 * jcom.map
 * External for Jamoma: map input to output: y=f(x)
 * Copyright © 2007
 * 
 * License: This code is licensed under the terms of the GNU LGPL
 * http://www.gnu.org/licenses/lgpl.html 
 */

#include "Jamoma.h"


// Data Structure for this object
typedef struct _map{
	t_object		ob;
	void			*outlet;
	t_symbol		*attr_function;
	double			attr_inputMin;
	double			attr_inputMax;
	double			attr_outputMin;
	double			attr_outputMax;
	double 			a, b;				// Coefficients used for normalizing input
	double			c, d;				// Coefficients used for scaling normalized output
	TTAudioObject	*functionUnit;
	bool			valid;				// true if the functionUnit can be used
} t_map;


// Prototypes for methods
void*		map_new(t_symbol *name, long argc, t_atom *argv);
void		map_free(t_map *obj);
void		map_assist(t_map *obj, void *b, long m, long a, char *s);
void		map_int(t_map *obj, long x);
void		map_float(t_map *obj, double x);
void		map_getFunctions(t_map *x, t_symbol *msg, long argc, t_atom *argv);
void		map_getParameter(t_map *x, t_symbol *msg, long argc, t_atom *argv);
void		map_getFunctionParameters(t_map *x, t_symbol *msg, long argc, t_atom *argv);
void		map_setParameter(t_map *x, t_symbol *msg, long argc, t_atom *argv);
t_max_err	map_setFunction(t_map *obj, void *attr, long argc, t_atom *argv);
t_max_err	map_setInputMin(t_map *obj, void *attr, long argc, t_atom *argv);
t_max_err	map_setInputMax(t_map *obj, void *attr, long argc, t_atom *argv);
void 		map_scaleInput(t_map *obj);
t_max_err	map_setOutputMin(t_map *obj, void *attr, long argc, t_atom *argv);
t_max_err	map_setOutputMax(t_map *obj, void *attr, long argc, t_atom *argv);
void 		map_scaleOutput(t_map *obj);


// Globals
t_class		*map_class;			// Required. Global pointing to this class


/************************************************************************************/
// Main() Function

int JAMOMA_EXPORT_MAXOBJ main(void)
{
	t_class *c;
	
	jamoma_init();
	common_symbols_init();

	// Define our class
	c = class_new("jcom.map",(method)map_new, (method)map_free, sizeof(t_map), (method)0L, A_GIMME, 0);

	// Make methods accessible for our class: 
	class_addmethod(c, (method)map_int,						"int",						A_LONG, 0L);
	class_addmethod(c, (method)map_float,					"float",					A_FLOAT, 0L);
	class_addmethod(c, (method)map_float,					"list",						A_GIMME, 0L);
	class_addmethod(c, (method)map_getFunctions,			"functions.get",			A_GIMME, 0);
 	class_addmethod(c, (method)map_getParameter,			"parameter.get",			A_GIMME, 0);
	class_addmethod(c, (method)map_getFunctionParameters,	"function.parameters.get",	A_GIMME, 0);
 	class_addmethod(c, (method)map_setParameter,			"parameter",				A_GIMME, 0);
	class_addmethod(c, (method)map_assist,					"assist",					A_CANT, 0L); 
    class_addmethod(c, (method)object_obex_dumpout,			"dumpout",					A_CANT,0);

	// ATTRIBUTE: set the function to use
	class_addattr(c, 
		attr_offset_new("function", _sym_symbol, 0,
		(method)0, (method)map_setFunction, calcoffset(t_map, attr_function)));
	// ATTRIBUTE: set the input minimum value
	class_addattr(c,
		attr_offset_new("inputMin", _sym_float64, 0,
		(method)0, (method)map_setInputMin, calcoffset(t_map, attr_inputMin)));
	// ATTRIBUTE: set the input maximum value
	class_addattr(c,
		attr_offset_new("inputMax", _sym_float64, 0,
		(method)0, (method)map_setInputMax, calcoffset(t_map, attr_inputMax)));	
		
	// ATTRIBUTE: set the output minimum value
	class_addattr(c,
		attr_offset_new("outputMin", _sym_float64, 0,
		(method)0, (method)map_setOutputMin, calcoffset(t_map, attr_outputMin)));
	// ATTRIBUTE: set the output maximum value
	class_addattr(c,
		attr_offset_new("outputMax", _sym_float64, 0,
		(method)0, (method)map_setOutputMax, calcoffset(t_map, attr_outputMax)));

	// Finalize our class
	class_register(CLASS_BOX, c);
	map_class = c;
	return 0;
}


/************************************************************************************/
// Object Life

void *map_new(t_symbol *name, long argc, t_atom *argv)
{
	t_map *obj;									// Declare an object (based on our struct)

	obj = (t_map *)object_alloc(map_class);		// Create object, store pointer to it (get 1 inlet free)
	if(obj){
		object_obex_store((void *)obj, _sym_dumpout, (object *)outlet_new(obj,NULL));
	    obj->outlet = outlet_new(obj, 0);
		obj->attr_function = _sym_nothing;
		obj->attr_inputMin = 0;
		obj->attr_inputMax = 1;
		obj->attr_outputMin = 0;
		obj->attr_outputMax = 1;
		obj->functionUnit = NULL;
		obj->valid = false;
		attr_args_process(obj, argc, argv);
		map_scaleInput(obj);
		map_scaleOutput(obj);
		if(!obj->functionUnit)
			object_attr_setsym(obj, gensym("function"), gensym("linear"));
	}
	return obj;										// Return pointer to our instance
}


void map_free(t_map *obj)
{
	if(obj->functionUnit)
		delete obj->functionUnit;
}


/************************************************************************************/
// Methods bound to input/inlets

// Method for Assistance Messages
void map_assist(t_map *x, void *b, long msg, long arg, char *dst)
{
	if(msg==1) 							// Inlets
		strcpy(dst, "x");
	else if(msg==2){ 					// Outlets
		switch(arg){
			case 0: strcpy(dst, "y=f(x)"); break;
			default: strcpy(dst, "dumpout"); break;
 		}
 	}
}


void map_int(t_map *obj, long x)
{
	map_float(obj, (double)x);
}


void map_float(t_map *obj, double x)
{
	double y;
	
	if(obj->valid){
		//y = obj->c * obj->functionUnit->map(obj->a * x + obj->b) + obj->d;
		obj->functionUnit->calculate(obj->a * x + obj->b, y);
		y = obj->c * y + obj->d;
		outlet_float(obj->outlet, y);
	}
}


void map_getFunctions(t_map *obj, t_symbol *msg, long argc, t_atom *argv)
{
	t_atom		a[2];
	long		numFunctions = 0;
	long		i;
	TTValue		functionNames;
	TTSymbol*	aName;
	
	atom_setsym(a+0, gensym("clear"));
	object_obex_dumpout(obj, gensym("functions"), 1, a);
	
	FunctionLib::getUnitNames(functionNames);
	numFunctions = functionNames.getSize();

	atom_setsym(a+0, gensym("append"));
	for(i=0; i<numFunctions; i++){
		functionNames.get(i, &aName);
		atom_setsym(a+1, gensym((char*)aName->getCString()));
		object_obex_dumpout(obj, gensym("functions"), 2, a);
	}
}


void map_getParameter(t_map *obj, t_symbol *msg, long argc, t_atom *argv)
{
	t_atom*		a;
	TTSymbol*	parameterName;
	TTValue		parameterValue;
	int			numValues;
	int			i;
	TTSymbol*	tempSymbol;
	double		tempValue;
	
{'fraction_non_alphanumeric': 0.17237048665620094, 'fraction_numerical': 0.008006279434850863, 'mean_word_length': 4.831121281464531, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, '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': '6260407', 'n_tokens_mistral': 5295, 'n_tokens_neox': 4884, 'n_words': 1136}