aboutsummaryrefslogtreecommitdiff
path: root/utils.c
blob: 0def48040e4f93072ebe44b7cd7bf0d6173144e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Utilites to be used for algorithmic composition
*/

#include "m_pd.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

static t_class *map_class;

/*
 * linear mapping
 */
typedef struct _map {
  t_object  x_obj;
  t_float x;
  t_float min1,min2,max1,max2;
  t_float scale,translate;
  t_outlet *mapped_out;
} t_map;

void map_list(t_map *x,t_symbol *s, int argc, t_atom *argv)
{
	int i;
	float out;
	x->scale = (x->max2 - x->min2)/(x->max1 - x->min1);
	for (i=0;i<argc;i++) {
	out = (atom_getfloat(&argv[i]) - x->min1)*x->scale+x->min1+x->translate;
	SETFLOAT(&argv[i],out);
}
	outlet_list(x->mapped_out, &s_list, argc, argv);
}


void map_float(t_map *x,t_floatarg f)
{
float out;
x->scale = (x->max2 - x->min2)/(x->max1 - x->min1);
x->translate = x->min2 - x->min1;
out = (f-x->min1)*x->scale+x->min1+x->translate;
outlet_float(x->mapped_out, out);
}

void *map_new(t_floatarg min1, t_floatarg max1, t_floatarg min2,t_floatarg max2)
{
  t_map *x = (t_map *)pd_new(map_class);
  x->min1 = min1;
  x->min2 = min2;
  x->max1 = max1;
  x->max2 = max2;
  
  x->scale = (max2 - min2)/(max1 - min1);
  x->translate = min2 - min1;
  x->mapped_out = outlet_new(&x->x_obj,&s_float);
  floatinlet_new(&x->x_obj, &x->min2);
  floatinlet_new(&x->x_obj, &x->max2);
  return (void *)x;
}