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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*
* permut.c - applies a (random) permutation on a signal block
* intended for spectral processing, dynwav
*
* Copyright (c) 2000-2003 by Tom Schouten
*
* 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 of the License, 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <m_pd.h>
#include <math.h>
#include "extlib_util.h"
#include <stdlib.h>
typedef struct permutctl
{
char c_type;
t_int *c_permutationtable;
int c_blocksize;
} t_permutctl;
typedef struct permut
{
t_object x_obj;
t_float x_f;
t_permutctl x_ctl;
} t_permut;
static inline void permut_perform_permutation(t_float *S, int n, t_int *f)
{
t_int k,l;
t_float swap;
for(k=0; k<n; k++)
{
l = f[k];
while (l<k) l = f[l];
swap = S[k];
S[k] = S[l];
S[l] = swap;
}
}
static void permut_random(t_permut *x, t_floatarg seed)
{
int i,j;
int N = x->x_ctl.c_blocksize;
int mask = N-1;
t_int *p = x->x_ctl.c_permutationtable;
int r, last = 0;
//srand(* ((unsigned int *)(&seed)));
srand (((t_flint)seed).i);
if(p)
{
p[0] = rand() & mask;
for (i=1;i<N;i++)
{
r = rand() & mask;
j = 0;
while(j<i)
{
if (p[j] == r)
{
r = (r + 1) & mask;
j = 0;
}
else j++;
}
p[i] = r;
}
}
}
static void permut_bang(t_permut *x)
{
unsigned int r = rand();
//permut_random(x, *((float *)(&r)));
permut_random(x, ((t_flint)r).f);
}
static void permut_resize_table(t_permut *x, int size)
{
if (x->x_ctl.c_blocksize != size)
{
if (x->x_ctl.c_permutationtable)
free(x->x_ctl.c_permutationtable);
x->x_ctl.c_permutationtable = (t_int *)malloc(sizeof(int)*size);
x->x_ctl.c_blocksize = size;
/* make sure it's initialized */
permut_bang(x);
}
}
static t_int *permut_perform(t_int *w)
{
t_float *in = (float *)(w[3]);
t_float *out = (float *)(w[4]);
t_permutctl *ctl = (t_permutctl *)(w[1]);
t_int i;
t_int n = (t_int)(w[2]);
t_float x,y;
t_int *p = ctl->c_permutationtable;
if (in != out)
for (i=0; i<n; i++) out[i] = in[i];
permut_perform_permutation(out, n, p);
return (w+5);
}
static void permut_dsp(t_permut *x, t_signal **sp)
{
permut_resize_table(x, sp[0]->s_n);
dsp_add(permut_perform, 4, &x->x_ctl, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec);
}
static void permut_free(t_permut *x)
{
if (x->x_ctl.c_permutationtable) free(x->x_ctl.c_permutationtable);
}
t_class *permut_class;
static void *permut_new(void)
{
t_permut *x = (t_permut *)pd_new(permut_class);
outlet_new(&x->x_obj, gensym("signal"));
x->x_ctl.c_permutationtable = 0;
x->x_ctl.c_blocksize = 0;
permut_resize_table(x, 64);
permut_random(x, 0);
return (void *)x;
}
void permut_tilde_setup(void)
{
//post("permut~ v0.1");
permut_class = class_new(gensym("permut~"), (t_newmethod)permut_new,
(t_method)permut_free, sizeof(t_permut), 0, A_DEFFLOAT, 0);
CLASS_MAINSIGNALIN(permut_class, t_permut, x_f);
class_addmethod(permut_class, (t_method)permut_random, gensym("random"), A_FLOAT, 0);
class_addmethod(permut_class, (t_method)permut_bang, gensym("bang"), 0);
class_addmethod(permut_class, (t_method)permut_dsp, gensym("dsp"), 0);
}
|