aboutsummaryrefslogtreecommitdiff
path: root/tabdump2.c
blob: 98dac2c6afb5fe5145c08b2eb9072b90580751d9 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright (C) 2004 Antoine Rousseau 

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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 GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  

*/

/*
	tabdump2 -- from tabdump (from zexy) but with min/max dumping limits.
*/

#include <m_pd.h>

static t_class *tabdump_class;

typedef struct _tabdump
{
  t_object x_obj;
  t_symbol *x_arrayname;
} t_tabdump;

static void tabdump_bang(t_tabdump *x, t_float findex)
{
  t_garray *A;
  int npoints;
  t_float *vec;

  if (!(A = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
    error("%s: no such array", x->x_arrayname->s_name);
  else if (!garray_getfloatarray(A, &npoints, &vec))
    error("%s: bad template for tabdump", x->x_arrayname->s_name);
  else
    {
      int n;
      t_atom *atombuf = (t_atom *)getbytes(sizeof(t_atom)*npoints);

      for (n = 0; n < npoints; n++) SETFLOAT(&atombuf[n], vec[n]);
      outlet_list(x->x_obj.ob_outlet, &s_list, npoints, atombuf);
    }
}


static void tabdump_dump(t_tabdump *x, t_float min, t_float max)
{
  t_garray *A;
  int npoints,nmin=(int)min,nmax=(int)max;
  t_float *vec;

  if (!(A = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
    error("%s: no such array", x->x_arrayname->s_name);
  else if (!garray_getfloatarray(A, &npoints, &vec))
    error("%s: bad template for tabdump", x->x_arrayname->s_name);
  else if ((min<0)||(max<=min)||(max>npoints)) 
  	error("tabdump: bad arguments min=%d max=%d for %s (%d elements)",
		nmin,nmax,x->x_arrayname->s_name,npoints);
  else
    {
      int n;
	  t_atom *atombuf;
	  
	  npoints=nmax-nmin;
      atombuf = (t_atom *)getbytes(sizeof(t_atom)*npoints);

      for (n = 0; n < npoints; n++) SETFLOAT(&atombuf[n], vec[n+nmin]);
      outlet_list(x->x_obj.ob_outlet, &s_list, npoints, atombuf);
    }
}


static void tabdump_set(t_tabdump *x, t_symbol *s)
{
  x->x_arrayname = s;
}

static void *tabdump_new(t_symbol *s)
{
  t_tabdump *x = (t_tabdump *)pd_new(tabdump_class);
  x->x_arrayname = s;
  outlet_new(&x->x_obj, &s_list);

  return (x);
}

static void tabdump_helper(void)
{
  post("\n tabdump2 - object : dumps a table as a package of floats");
  post("'set <table>'\t: read out another table\n"
       "'bang'\t\t: dump the table\n"
       "'dump <min> <max>'\t\t: dump the table from <min> to <max> (without <max>)\n"
       "outlet\t\t: table-data as package of floats");
  post("creation\t: \"tabdump2 <table>\"");

}

void tabdump2_setup(void)
{
  tabdump_class = class_new(gensym("tabdump2"), (t_newmethod)tabdump_new,
			     0, sizeof(t_tabdump), 0, A_DEFSYM, 0);
  class_addbang(tabdump_class, (t_method)tabdump_bang);
  class_addmethod(tabdump_class, (t_method)tabdump_dump,gensym("dump"),
  			A_FLOAT,A_FLOAT,0);
  class_addmethod(tabdump_class, (t_method)tabdump_set, gensym("set"),
		  A_SYMBOL, 0);

  class_addmethod(tabdump_class, (t_method)tabdump_helper, gensym("help"), 0);

}