aboutsummaryrefslogtreecommitdiff
path: root/ln~.c
blob: bf5939e94cb26ab3d5caaf9f984a8c6b67f9c5af (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
/*************************************************************************** 
 * File: ln~.c 
 * Auth: Iain Mott [iain.mott@bigpond.com] 
 * Maintainer: Iain Mott [iain.mott@bigpond.com] 
 * Version: Part of motex_1.1.2 
 * Date: January 2001
 * 
 * Description: Pd signal external. Finds natural log of signal. Optional argument '-1' finfs inverse. 
 * 
 * Copyright (C) 2001 by Iain Mott [iain.mott@bigpond.com] 
 * 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License, which should be included with this 
 * program, for more details. 
 * 
 ****************************************************************************/ 

#include "math.h"
#include <m_pd.h>

/* ----------------------------- ln ----------------------------- */
static t_class *ln_class;


#define INVTWOPI 0.15915494f

typedef struct _ln
{
  t_object x_obj;
  int flag;
} t_ln;

/*  static void *ln_new(t_symbol *s, int argc, t_atom *argv) */
static void *ln_new(t_floatarg f)
{
/*      if (argc > 1)  */
/*        post("+~: extra arguments ignored"); */
/*      { */
	t_ln *x = (t_ln *)pd_new(ln_class);
	outlet_new(&x->x_obj, &s_signal);
	x->flag = f;
	return (x);
/*      } */
}

t_int *ln_perform(t_int *w)
{
    t_float *in1 = (t_float *)(w[1]);
    t_float *out = (t_float *)(w[2]);

    int n = (int)(w[3]);
    int flag = (int)(w[4]);
    if(flag != -1)
      while (n--) *out++ = (t_float) log(*in1++); 
    else
      while (n--) *out++ = (t_float) exp(*in1++); 
    return (w+5);
}

t_int *ln_perf8(t_int *w)
{
  t_float *in1 = (t_float *)(w[1]);
  t_float *out = (t_float *)(w[2]);
  int n = (int)(w[3]);
  int flag = (int)(w[4]);
  if(flag != -1)
    {
      for (; n; n -= 8, in1 += 8,  out += 8)
	{
	  float f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
	  float f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];

	  out[0] = (t_float) log(f0); 
	  out[1] = (t_float) log(f1); 
	  out[2] = (t_float) log(f2); 
	  out[3] = (t_float) log(f3); 
	  out[4] = (t_float) log(f4); 
	  out[5] = (t_float) log(f5); 
	  out[6] = (t_float) log(f6); 
	  out[7] = (t_float) log(f7); 
	}
    }
  else
    {
      for (; n; n -= 8, in1 += 8,  out += 8)
	{
	  float f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
	  float f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];

	  out[0] = (t_float) exp(f0); 
	  out[1] = (t_float) exp(f1); 
	  out[2] = (t_float) exp(f2); 
	  out[3] = (t_float) exp(f3); 
	  out[4] = (t_float) exp(f4); 
	  out[5] = (t_float) exp(f5); 
	  out[6] = (t_float) exp(f6); 
	  out[7] = (t_float) exp(f7); 
	}
    }

  return (w+5);
}

void dsp_add_ln(t_sample *in1, t_sample *out, int n, int flag)
{
    if (n&7)
    	dsp_add(ln_perform, 4, in1, out, n, flag);
    else	
    	dsp_add(ln_perf8, 4, in1, out, n, flag);
}

static void ln_dsp(t_ln *x, t_signal **sp)
{
    dsp_add_ln(sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n, x->flag);
}

void ln_tilde_setup(void)
{
    ln_class = class_new(gensym("ln~"), (t_newmethod)ln_new, 0,
    	sizeof(t_ln), 0, A_DEFFLOAT, 0);
    class_addmethod(ln_class, nullfn, gensym("signal"), 0);
    class_addmethod(ln_class, (t_method)ln_dsp, gensym("dsp"), 0);
}