aboutsummaryrefslogtreecommitdiff
path: root/src/z~.c
blob: 7d786ca848a95ef9563e1e32736e705f174c6eb9 (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
121
122
123
124
125
126
127
128
129
130
131
132
/******************************************************
 *
 * zexy - implementation file
 *
 * copyleft (c) IOhannes m zmölnig
 *
 *   1999:forum::für::umläute:2004
 *
 *   institute of electronic music and acoustics (iem)
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/

/*
	here we do some sample-wise delay, so you can do your own FIR-filter-designs
	here are :: "z^(-1)", "z^(-N)"
	to do :: a "lattice~" section ...

	1302:forum::für::umläute:2000
*/

#include "zexy.h"

/* ----------------------------------------------------- */

static t_class *zNdelay_class;

typedef struct _zNdelay
{
  t_object x_obj;

  t_float *buf;
  int bufsize, phase;

} t_zNdelay;

static void zdel_float(t_zNdelay *x, t_floatarg f)
{
  int i = f+1;
  if (i<1)i=1;
  if (i==x->bufsize)return;
  freebytes(x->buf, x->bufsize*sizeof(t_float));
  x->bufsize=i;
  x->buf=(t_float *)getbytes(x->bufsize*sizeof(t_float));
  x->phase=0;
}

static t_int *zN_perform(t_int *w)
{
  t_float *in = (t_float *)(w[1]);
  t_float *out = (t_float *)(w[2]);
  t_zNdelay *x = (t_zNdelay *)(w[3]);
  int n = (int)(w[4]);

  t_float *buf = x->buf;
  int bufsize=x->bufsize, ph=x->phase;

  if (bufsize==1) {
    if (in!=out)while(n--)*out++=*in++;
  } else if (bufsize==2) {
    register t_float f, last=*buf;
    while(n--){
      f=*in++;
      *out++=last;
      last=f;
    }
    *buf=last;
  } else {
    while (n--) {
      *(buf+ph++) = *in++;
      *out++    = buf[ph%=bufsize];
    }
    x->phase=ph;
  }
  return (w+5);
}


static void zNdelay_dsp(t_zNdelay *x, t_signal **sp)
{
  dsp_add(zN_perform, 4, sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
}

static void *zNdelay_new(t_floatarg f)
{
  t_zNdelay *x = (t_zNdelay *)pd_new(zNdelay_class);
  int i = f;
  t_float *b;

  if (i<=0) i=1;
  i++;

  x->bufsize = i;
  x->buf = (t_float *)getbytes(sizeof(t_float) * x->bufsize);
  b=x->buf;
  while (i--) {
    *b++=0;
  }
  x->phase = 0;

  outlet_new(&x->x_obj, gensym("signal")); 
  return (x);
}

static void zNdelay_free(t_zNdelay *x)
{
  freebytes(x->buf, sizeof(t_float) * x->bufsize);
}


static void zdel_helper(void)
{
  post("\n%c z~\t:: samplewise delay", HEARTSYMBOL);
  post("creation :: 'z~ [<n>]' : creates a <n>-sample delay; default is 1");
}


void z_tilde_setup(void)
{
  zNdelay_class = class_new(gensym("z~"), (t_newmethod)zNdelay_new, (t_method)zNdelay_free,
			    sizeof(t_zNdelay), 0, A_DEFFLOAT, 0);
  class_addmethod(zNdelay_class, nullfn, gensym("signal"), 0);
  class_addmethod(zNdelay_class, (t_method)zNdelay_dsp, gensym("dsp"), 0);

  class_addfloat(zNdelay_class, zdel_float);
  class_addmethod(zNdelay_class, (t_method)zdel_helper, gensym("help"), 0);
  class_sethelpsymbol(zNdelay_class, gensym("zexy/z~"));
  zexy_register("z~");
}