aboutsummaryrefslogtreecommitdiff
path: root/src/tabread_dp.c
blob: c995adb3d2ca710a488d5a8a6ca2f09c00e1a7c5 (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
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iem_dp written by IOhannes m zmoelnig, Thomas Musil, Copyright (c) IEM KUG Graz Austria 1999 - 2007 */
/* double precision library */


#include "m_pd.h"
#include "iemlib.h"
#include "iem_dp.h"

/* -------------------------- tabread__ ------------------------------ */
/* based on miller's tabread which is part of pd */

static t_class *tabread_dp_class;

typedef struct _tabread_dp
{
  t_object  x_obj;
  t_symbol  *x_arrayname;
  t_float   x_residual;
} t_tabread_dp;

static void *tabread_dp_new(t_symbol *s)
{
  t_tabread_dp *x = (t_tabread_dp *)pd_new(tabread_dp_class);

  x->x_arrayname = s;
  floatinlet_new(&x->x_obj, &x->x_residual);
  outlet_new(&x->x_obj, &s_float);
  return (x);
}

static void tabread_dp_float(t_tabread_dp *x, t_floatarg f)
{
  t_garray *a;
  iemarray_t *vec;
  int npoints;
  
  if (!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
  {
    if (*x->x_arrayname->s_name)
      pd_error(x, "tabread__: %s: no such array", x->x_arrayname->s_name);
    vec = 0;
  }
  else if (!iemarray_getarray(a, &npoints, &vec))
  {
    pd_error(x, "%s: bad template for tabread__", x->x_arrayname->s_name);
    vec = 0;
  }
  else
  {
    double findex = iem_dp_calc_sum(f, x->x_residual);
    int n = findex;
    
    if(n < 0)
      n = 0;
    else if(n >= npoints)
      n = npoints - 1;
    outlet_float(x->x_obj.ob_outlet, (npoints ? iemarray_getfloat(vec, n) : 0));
  }
}

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

static void tabread_dp_free(t_tabread_dp *x)
{
}

void tabread_dp_setup(void)
{
  tabread_dp_class = class_new(gensym("tabread__"),
    (t_newmethod)tabread_dp_new, (t_method)tabread_dp_free, 
    sizeof(t_tabread_dp), 0, A_DEFSYM, 0);
  class_addcreator((t_newmethod)tabread_dp_new, gensym("tabread''"), A_DEFSYM, 0);
  class_addfloat(tabread_dp_class, (t_method)tabread_dp_float);
  class_addmethod(tabread_dp_class, (t_method)tabread_dp_set, gensym("set"), A_SYMBOL, 0);
}