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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*
VASP modular - vector assembling signal processor / objects for Max/MSP and PD
Copyright (c) 2002 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.
*/
#include "main.h"
#include "ops_gen.h"
#include "util.h"
#include <math.h>
// --- osc ---------------------------------------
/*! \brief Generator for real (cos) oscillations
*/
BL VecOp::d_osc(OpParam &p)
{
register R ph = p.gen.ph,phinc = p.gen.phinc;
if(p.revdir) ph -= (p.frames-1)*(phinc = -phinc);
for(I i = 0; i < p.frames; ++i,ph += phinc,p.rddt += p.rds) *p.rddt = cos(ph);
return true;
}
/*! \brief multiplicative generator for real (cos) oscillations
*/
BL VecOp::d_mosc(OpParam &p)
{
register R ph = p.gen.ph,phinc = p.gen.phinc;
if(p.revdir) ph -= (p.frames-1)*(phinc = -phinc);
for(I i = 0; i < p.frames; ++i,ph += phinc,p.rsdt += p.rss,p.rddt += p.rds)
*p.rddt = *p.rsdt * cos(ph);
return true;
}
/*! \brief Generator for real (cos) oscillations.
\param arg argument list
\param arg.perlen Period length (in samples)
\param arg.stph Starting phase
\param mul true for multiplication to exisiting date
\return normalized destination vasp
\todo Replace period length by frequency specification
*/
Vasp *VaspOp::m_osc(OpParam &p,CVasp &src,const Argument &arg,CVasp *dst,BL mul)
{
Vasp *ret = NULL;
if(arg.IsList() && arg.GetList().Count() >= 1) {
RVecBlock *vecs = GetRVecs(p.opname,src,dst);
if(vecs) {
// period length
p.gen.phinc = 2*PI/flext::GetAFloat(arg.GetList()[0]);
// starting phase
p.gen.ph = arg.GetList().Count() >= 2?flext::GetAFloat(arg.GetList()[1]):0;
ret = DoOp(vecs,mul?VecOp::d_mosc:VecOp::d_osc,p);
delete vecs;
}
}
else
post("%s - no arguments: no operation",p.opName());
return ret;
}
/*! \brief Generator for complex oscillations.
*/
BL VecOp::d_cosc(OpParam &p)
{
register R ph = p.gen.ph,phinc = p.gen.phinc;
if(p.revdir) ph -= (p.frames-1)*(phinc = -phinc);
for(; p.frames--; ph += phinc,p.rddt += p.rds,p.iddt += p.ids)
*p.rddt = cos(ph),*p.iddt = sin(ph);
return true;
}
/*! \brief Multiplicative generator for complex oscillations.
*/
BL VecOp::d_mcosc(OpParam &p)
{
register R ph = p.gen.ph,phinc = p.gen.phinc;
if(p.revdir) ph -= (p.frames-1)*(phinc = -phinc);
for(; p.frames--; ph += phinc,p.rsdt += p.rss,p.isdt += p.iss,p.rddt += p.rds,p.iddt += p.ids) {
R zre = cos(ph),zim = sin(ph);
register const R r = *p.rsdt * zre - *p.isdt * zim;
*p.iddt = *p.isdt * zre + *p.rsdt * zim;
*p.rddt = r;
}
return true;
}
/*! \brief Generator for complex (cos+i sin) oscillations.
\param arg argument list
\param arg.perlen Period length (in samples)
\param arg.stph Starting phase
\param mul true for multiplication to exisiting date
\return normalized destination vasp
\todo Replace period length by frequency specification
*/
Vasp *VaspOp::m_cosc(OpParam &p,CVasp &src,const Argument &arg,CVasp *dst,BL mul)
{
Vasp *ret = NULL;
if(arg.IsList() && arg.GetList().Count() >= 1) {
CVecBlock *vecs = GetCVecs(p.opname,src,dst);
if(vecs) {
// period length
p.gen.phinc = 2*PI/flext::GetAFloat(arg.GetList()[0]);
// starting phase
p.gen.ph = arg.GetList().Count() >= 2?flext::GetAFloat(arg.GetList()[1]):0;
ret = DoOp(vecs,mul?VecOp::d_mcosc:VecOp::d_cosc,p);
delete vecs;
}
}
else
post("%s - no arguments: no operation",p.opName());
return ret;
}
VASP_ANYOP("vasp.osc",osc,1,false,VASP_ARG(),"Calculates a cosine wave")
VASP_ANYOP("vasp.*osc",mosc,1,true,VASP_ARG(),"Multiplies with a cosine wave")
VASP_ANYOP("vasp.cosc",cosc,1,false,VASP_ARG(),"Calculates a complex oscillation (cos + i sin)")
VASP_ANYOP("vasp.*cosc",mcosc,1,true,VASP_ARG(),"Multiplies with a complex oscillation (cos + i sin)")
// --- phasor ---------------------------------------
// ! look up Höldrich's pd phasor code
/*! \brief Generator for saw wave oscillations.
*/
BL VecOp::d_phasor(OpParam &p)
{
register R ph = p.gen.ph,phinc = p.gen.phinc;
if(p.revdir) ph -= (p.frames-1)*(phinc = -phinc);
for(; p.frames--; ph += phinc,p.rddt += p.rds) *p.rddt = fmod((F)ph,1.F);
return true;
}
/*! \brief Multiplicative generator for saw wave oscillations.
*/
BL VecOp::d_mphasor(OpParam &p)
{
register R ph = p.gen.ph,phinc = p.gen.phinc;
if(p.revdir) ph -= (p.frames-1)*(phinc = -phinc);
for(; p.frames--; ph += phinc,p.rddt += p.rds,p.rsdt += p.rss) *p.rddt = *p.rsdt * fmod((F)ph,1.F);
return true;
}
/*! \brief Generator for sawtooth oscillations.
\param arg argument list
\param arg.perlen Period length (in samples)
\param arg.stph Starting phase
\param mul true for multiplication to exisiting date
\return normalized destination vasp
\todo Replace period length by frequency specification
*/
Vasp *VaspOp::m_phasor(OpParam &p,CVasp &src,const Argument &arg,CVasp *dst,BL mul)
{
Vasp *ret = NULL;
if(arg.IsList() && arg.GetList().Count() >= 1) {
RVecBlock *vecs = GetRVecs(p.opname,src,dst);
if(vecs) {
// period length
p.gen.phinc = 1./flext::GetAFloat(arg.GetList()[0]);
// starting phase
p.gen.ph = arg.GetList().Count() >= 2?flext::GetAFloat(arg.GetList()[1]):0;
ret = DoOp(vecs,mul?VecOp::d_mphasor:VecOp::d_phasor,p);
delete vecs;
}
}
else
post("%s - no arguments: no operation",p.opName());
return ret;
}
VASP_ANYOP("vasp.phasor",phasor,1,false,VASP_ARG(),"Calculates a sawtooth wave")
VASP_ANYOP("vasp.*phasor",mphasor,1,true,VASP_ARG(),"Multiplies with a sawtooth wave")
// --- noise --------------------------------
static F rnd()
{
static I val = 307*1319;
F ret = ((F)((val&0x7fffffff)-0x40000000))*(F)(1.0/0x40000000);
val = val * 435898247 + 382842987;
return ret;
}
/*! \brief Vector function for pseudorandom noise.
*/
BL VecOp::d_noise(OpParam &p)
{
for(; p.frames--; p.rddt += p.rds) *p.rddt = rnd();
return true;
}
/*! \brief Generator for real valued noise.
\return normalized destination vasp
*/
Vasp *VaspOp::m_noise(OpParam &p,CVasp &src,CVasp *dst)
{
Vasp *ret = NULL;
RVecBlock *vecs = GetRVecs(p.opname,src,dst);
if(vecs) {
ret = DoOp(vecs,VecOp::d_noise,p);
delete vecs;
}
return ret;
}
/*! \brief Vector function for pseudorandom complex noise.
*/
BL VecOp::d_cnoise(OpParam &p)
{
for(; p.frames--; p.rddt += p.rds,p.iddt += p.ids) {
R amp = rnd();
R arg = rnd()*(2.*PI);
*p.rddt = amp*cos(arg);
*p.iddt = amp*sin(arg);
}
return true;
}
/*! \brief Generator for complex noise (complex abs, complex arg).
\return normalized destination vasp
\todo Replace period length by frequency specification
*/
Vasp *VaspOp::m_cnoise(OpParam &p,CVasp &src,CVasp *dst)
{
Vasp *ret = NULL;
CVecBlock *vecs = GetCVecs(p.opname,src,dst);
if(vecs) {
ret = DoOp(vecs,VecOp::d_cnoise,p);
delete vecs;
}
return ret;
}
VASP_UNARY("vasp.noise",noise,false,"Fills the vectors with white noise")
VASP_UNARY("vasp.cnoise",cnoise,false,"Fills the vectors with complex white noise (radius and angle are random)")
|