File: phasemod~.c

package info (click to toggle)
pd-lyonpotpourri 2.0%2Bgit20121009-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,180 kB
  • sloc: ansic: 18,330; makefile: 376
file content (229 lines) | stat: -rw-r--r-- 4,923 bytes parent folder | download | duplicates (3)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "MSPd.h"
#define FUNC_LEN (32768)
#define OBJECT_NAME "phasemod~"

#if __PD__
static t_class *phasemod_class;
#endif

#if __MSP__
void *phasemod_class;
#endif

typedef struct _phasemod
{
#if __MSP__
  t_pxobject x_obj;
#endif
#if __PD__
  t_object x_obj;
  float x_f;
#endif
    t_float x_val;
    float mygain;
    float *wavetab;
	float phs;
	float bendphs;
	float frequency;
	float alpha;
	short mute;
	short connections[4];
	float si_fac;
	float sr;
} t_phasemod;

void *phasemod_new(t_symbol *s, int argc, t_atom *argv);
t_int *offset_perform(t_int *w);
t_int *phasemod_perform(t_int *w);
void phasemod_float(t_phasemod *x, double f);
void phasemod_int(t_phasemod *x, long n);
void phasemod_mute(t_phasemod *x, t_floatarg toggle);
void phasemod_dsp(t_phasemod *x, t_signal **sp, short *count);
void phasemod_assist(t_phasemod *x, void *b, long m, long a, char *s);
void phasemod_dsp_free(t_phasemod *x);

#if __MSP__
void main(void)
{
    setup((t_messlist **)&phasemod_class,(method) phasemod_new, (method)phasemod_dsp_free, (short)sizeof(t_phasemod), 0L, A_GIMME, 0);
    addmess((method)phasemod_dsp, "dsp", A_CANT, 0);
    addfloat((method)phasemod_float);
    addint((method)phasemod_int);
    addmess((method)phasemod_assist,"assist",A_CANT,0);
    addmess((method)phasemod_mute,"mute",A_FLOAT,0);
    dsp_initclass();
 	post("%s %s",OBJECT_NAME, LYONPOTPOURRI_MSG);
}
#endif
#if __PD__
void phasemod_tilde_setup(void){
  phasemod_class = class_new(gensym("phasemod~"), (t_newmethod)phasemod_new, 
			    (t_method)phasemod_dsp_free,sizeof(t_phasemod), 0,A_GIMME,0);
  CLASS_MAINSIGNALIN(phasemod_class, t_phasemod, x_f);
  class_addmethod(phasemod_class,(t_method)phasemod_dsp,gensym("dsp"),0);
  class_addmethod(phasemod_class,(t_method)phasemod_mute,gensym("mute"),A_FLOAT,0);
  post("%s %s",OBJECT_NAME, LYONPOTPOURRI_MSG);
}
#endif

void phasemod_dsp_free( t_phasemod *x )
{
#if __MSP__
  dsp_free((t_pxobject *) x);
#endif
  free(x->wavetab);
}

void phasemod_mute(t_phasemod *x, t_floatarg toggle)
{
	x->mute = toggle;
}
void phasemod_assist (t_phasemod *x, void *b, long msg, long arg, char *dst)
{
	if (msg==1) {
		switch (arg) {
			case 0:
				sprintf(dst,"(signal/float) Frequency ");
				break;
			case 1:
				sprintf(dst,"(signal/float) Slope Factor ");
				break;
		}
	} else if (msg==2) {
		sprintf(dst,"(signal) Output ");
	}
}

void *phasemod_new(t_symbol *s, int argc, t_atom *argv)
{
  	int i;
#if __MSP__
    t_phasemod *x = (t_phasemod *)newobject(phasemod_class);
    dsp_setup((t_pxobject *)x,2);
    outlet_new((t_pxobject *)x, "signal");
#endif
#if __PD__
  t_phasemod *x = (t_phasemod *)pd_new(phasemod_class);
  inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
  outlet_new(&x->x_obj, gensym("signal"));
#endif
    x->phs = 0;
  	x->mute = 0;
	  x->frequency = 440.0;
	
	  x->wavetab = (float *) calloc(FUNC_LEN, sizeof(float) );
    for( i = 0 ; i < FUNC_LEN; i++ ) {
    	x->wavetab[i] = sin( TWOPI * ((float)i/(float) FUNC_LEN)) ;
    }
 	  x->bendphs = 0;
		x->sr = sys_getsr();
		if(!x->sr)
			x->sr = 44100.0;
		x->si_fac = 1.0/x->sr;
    return (x);
}

#if __MSP__
void phasemod_float(t_phasemod *x, double f)
{
	int inlet = ((t_pxobject*)x)->z_in;
	
	if (inlet == 0)
	{
		x->frequency = f;
	} 
	else if (inlet == 1 )
	{
		x->alpha = f;
	}
	
}

void phasemod_int(t_phasemod *x, long n)
{
	x->x_val = (float)n;
}
#endif

t_int *phasemod_perform(t_int *w)
{

	float phs;
	
	t_phasemod *x = (t_phasemod *) (w[1]);
	t_float *frequency_vec = (t_float *)(w[2]);
	t_float *alpha_vec = (t_float *)(w[3]);
	t_float *out = (t_float *)(w[4]);
	t_int n = w[5];
	
	short *connections = x->connections;
	float bendphs = x->bendphs;
	float *wavetab = x->wavetab;
	float si_fac = x->si_fac;
	
	float incr = x->frequency * si_fac ;
	float alpha = x->alpha;
	
	if( x->mute ){
		while(n--){
			*out++ = 0.0;
		}
		return (w + 6);
	}
	
	while (n--) { 
		if( connections[1] ){
			alpha = *alpha_vec++;
		}
		if( alpha == 0 ){
			alpha = .000001;
		}
		
		if( connections[0] ){
			incr = *frequency_vec++ * si_fac ;
		}
		// NO NEGATIVE FREQUENCIES
		if( incr < 0 )
			incr = -incr;
			
		bendphs += incr ;
		while( bendphs > 1.0 )
			bendphs -= 1.0 ;
			phs =   FUNC_LEN * ( (1 - exp(bendphs * alpha))/(1 - exp(alpha))  ); 

		while( phs < 0.0 ) {
			phs += FUNC_LEN;
		}
		while( phs >= FUNC_LEN ){
			phs -= FUNC_LEN;
		}
		*out++ =  wavetab[(int) phs] ; 
	}

	x->bendphs = bendphs;
	return (w+6);
}		

void phasemod_dsp(t_phasemod *x, t_signal **sp, short *count)
{
//	long i;
#if __MSP__
	x->connections[0] = count[0];
	x->connections[1] = count[1];
#endif
#if __PD__
	x->connections[0] = 1;
	x->connections[1] = 1;
#endif

	if(x->sr != sp[0]->s_sr){
		if(!sp[0]->s_sr){
			error("zero sampling rate");
			return;
		}
		x->sr = sp[0]->s_sr;
		x->si_fac = 1.0/x->sr;
	}
	dsp_add(phasemod_perform, 5, x, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec,  sp[0]->s_n);
}