aboutsummaryrefslogtreecommitdiff
path: root/framescore~.c
blob: 51fef994173f5efccc4fc34068dd24a3d35b3449 (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
/*
 * framescore~ : Weighted block comparison.
 * 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 *framescore_tilde_class;

typedef struct _framescore_tilde 
{
  t_object x_obj;
  t_float f;
  t_float f_max, f_win, f_accum;
  t_outlet *f_score;
} t_framescore_tilde;

t_int *framescore_tilde_perform(t_int *w)
{
  t_framescore_tilde *x = (t_framescore_tilde *)(w[1]);
  t_sample         *in1 =           (t_sample *)(w[2]);
  t_sample         *in2 =           (t_sample *)(w[3]);
  int                 n =                  (int)(w[4]);
  float vector1;
  float vector2;
  x->f_accum = 0;
  float block_accum = 0;
  x->f_max = 0;
  float score = 0;
  float avg = 0;
  int block = n;
  x->f_win = x->f_win > 0 ? x->f_win : 0.01;

  while (n--)
    {
      vector1 = (*in1++);
      vector2 = (*in2++);
      vector1 = vector1 > 0 ? vector1 : 0 - vector1;
      vector2 = vector2 > 0 ? vector2 : 0 - vector2;
      block_accum += vector2;
      x->f_max = vector2 > x->f_max ? vector2 : x->f_max;
      float diff = vector1 > vector2 ? vector1 - vector2 : vector2 - vector1;
      x->f_accum += (1/((diff/x->f_win)+1)) * vector2;
    }
  score = x->f_accum / x->f_max;
  block_accum /= x->f_max;
  avg = score / block_accum;
  outlet_float(x->f_score, avg);
  
  return(w+5);
}

void framescore_tilde_dsp(t_framescore_tilde *x, t_signal **sp)
{
  dsp_add(framescore_tilde_perform, 4, x,
	  sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

void *framescore_tilde_new(t_floatarg f)
{
  t_framescore_tilde *x = (t_framescore_tilde *)pd_new(framescore_tilde_class);

  x->f_win = f;

  inlet_new (&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
  floatinlet_new (&x->x_obj, &x->f_win);
  x->f_score = outlet_new(&x->x_obj, gensym("float"));

  return (void *)x;
}


void framescore_tilde_setup(void)
{
  framescore_tilde_class = class_new(gensym("framescore~"),
				     (t_newmethod)framescore_tilde_new,
				     0, sizeof(t_framescore_tilde),
				     CLASS_DEFAULT, A_DEFFLOAT, 0);

  post("|+++++++++++framescore~+++++++++++++|");
  post("|+++++weighted block comparison+++++|");
  post("|+++edward+++++++kelly+++++++2005+++|");

  class_addmethod(framescore_tilde_class, (t_method)framescore_tilde_dsp,
		  gensym("dsp"), 0);

  CLASS_MAINSIGNALIN(framescore_tilde_class, t_framescore_tilde, f);
}