File: windowvec~.c

package info (click to toggle)
pd-lyonpotpourri 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,724 kB
  • sloc: ansic: 24,685; makefile: 106; perl: 12
file content (97 lines) | stat: -rw-r--r-- 2,293 bytes parent folder | download
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
/* Required Header Files */

#include "MSPd.h"

/* The class pointer */

static t_class *windowvec_class;

/* The object structure */

typedef struct _windowvec {
  t_object obj;
  t_float x_f;
  t_float *envelope;
  long vecsize;
  long oldbytes;
} t_windowvec;

#define OBJECT_NAME "windowvec~"

/* Function prototypes */

static void *windowvec_new(void);
static void windowvec_dsp(t_windowvec *x, t_signal **sp, short *count);
static t_int *windowvec_perform(t_int *w);

/* The object setup function */

void windowvec_tilde_setup(void)
{
  windowvec_class = class_new(gensym("windowvec~"), (t_newmethod)windowvec_new, 0, sizeof(t_windowvec), 0,0);
  CLASS_MAINSIGNALIN(windowvec_class, t_windowvec, x_f);
  class_addmethod(windowvec_class, (t_method)windowvec_dsp, gensym("dsp"), A_CANT, 0);
  potpourri_announce(OBJECT_NAME);
}

/* The new instance routine */

void *windowvec_new(void)
{
  t_windowvec *x = (t_windowvec *)pd_new(windowvec_class);
  outlet_new(&x->obj, gensym("signal"));
  x->vecsize = 0;
  x->envelope = NULL;
  return x;
}

/* The free memory function*/

void windowvec_free(t_windowvec *x, t_signal **sp, short *count)
{
  freebytes(x->envelope, x->oldbytes);
}

/* The perform routine */

t_int *windowvec_perform(t_int *w)
{
  t_windowvec *x = (t_windowvec *) (w[1]);
  t_float *input = (t_float *) (w[2]);
  t_float *output = (t_float *) (w[3]);
  int n = (int) w[4];
  int i;
  t_float *envelope = x->envelope;

  /* Apply a Hann window to the input vector */

  for(i=0; i < n; i++) {
    output[i] = input[i] * envelope[i];
  }
  return w + 5;
}

void windowvec_dsp(t_windowvec *x, t_signal **sp, short *count)
{
  int i;
  t_float twopi = 8. * atan(1);
  if(x->vecsize != sp[0]->s_n) {
    x->vecsize = sp[0]->s_n;

    /* Allocate memory */

    if(x->envelope == NULL) {
      x->envelope = (t_float *) getbytes(x->vecsize * sizeof(t_float));
    } else {
      x->envelope = (t_float *) resizebytes(x->envelope, x->oldbytes, x->vecsize * sizeof(t_float));
    }
    x->oldbytes = x->vecsize * sizeof(t_float);

    /* Generate a Hann window */

    for(i = 0 ; i < x->vecsize; i++) {
      x->envelope[i] = - 0.5 * cos(twopi * (i / (t_float)x->vecsize)) + 0.5;
    }
  }
  dsp_add(windowvec_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, (t_int)sp[0]->s_n);
}