aboutsummaryrefslogtreecommitdiff
path: root/simile~.c
blob: 4f7abadd763fba12fa72bb2ab249d0e7e089a936 (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
/*
 * simile~ : windowed similarity comparison for signals
 * Copyright (c) 2005, Dr Edward Kelly <morph_2016@yahoo.co.uk>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * 
 * provided with the distribution.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "m_pd.h"

static t_class *simile_tilde_class;

typedef struct _simile_tilde {
  t_object x_obj;
  t_sample f_win;
  t_float x_dummy;
  t_outlet *f_frame;
} t_simile_tilde;

t_int *simile_tilde_perform(t_int *w) {
  t_simile_tilde *x = (t_simile_tilde *)(w[1]);
  t_sample     *in1 =       (t_sample *)(w[2]);
  t_sample     *in2 =       (t_sample *)(w[3]);
  t_sample     *out =       (t_sample *)(w[4]);
  t_sample    *sign =       (t_sample *)(w[5]);
  int             n =              (int)(w[6]);

  t_sample f_win    = x->f_win > 0 ? x->f_win : 0.01;
  t_float f_accum   = 0;
  while (n--) {
    float i1=(*in1++);
    float i2=(*in2++);
    float win = x->f_win > 0 ? x->f_win : 0.001;
    float min = i1 > i2 ? i1 - i2 : i2 - i1;
    f_accum += *out++ = 1/((min/win)+1);
    *sign++ = i1 >= i2 ? 1 : -1;
  }
  outlet_float(x->f_frame, f_accum);
  return (w+7);
}

void simile_tilde_dsp(t_simile_tilde *x, t_signal **sp) {
  dsp_add(simile_tilde_perform, 6, x, 
	  sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, sp[0]->s_n);
}

void *simile_tilde_new(t_floatarg f) {
  t_simile_tilde *x = (t_simile_tilde *)pd_new(simile_tilde_class);

  x->f_win = f > 0 ? f : 0.01; /* window defaults to 0.01 if no argument given */

  inlet_new (&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
  floatinlet_new (&x->x_obj, &x->f_win);
  outlet_new(&x->x_obj, &s_signal);
  outlet_new(&x->x_obj, &s_signal);
  x->f_frame = outlet_new(&x->x_obj, gensym("float"));
  return (void *)x;
}

void simile_tilde_setup(void) {
  simile_tilde_class = class_new(gensym("simile~"), 
  (t_newmethod)simile_tilde_new, 
  0, sizeof(t_simile_tilde),
  CLASS_DEFAULT, A_DEFFLOAT, 0);

  post("|~~~~~~~~~~~~&simile~&~~~~~~~~~~~~~~|");
  post("|~&weighted similarity measurement&~|");
  post("|~~&edward&~~~~~&kelly&~~~~~&2005&~~|");


  class_addmethod(simile_tilde_class,
  (t_method)simile_tilde_dsp, gensym("dsp"), 0);
  CLASS_MAINSIGNALIN(simile_tilde_class, t_simile_tilde, x_dummy);
}