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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// some basic physical modeling ugens - julian rohrhuber 1/04
// these are very simple implementations with cartoonification aspects.
#include "SC_PlugIn.h"
static InterfaceTable *ft;
//////////////////////////////////////////////////////////////////////////////////////////////////
struct Spring : public Unit
{
float m_pos;
float m_vel;
};
/*
struct Friction : public Unit
{
float m_pos;
float m_vel;
int m_state;
};
*/
struct Ball : public Unit
{
float m_pos;
float m_vel;
float m_prev;
};
struct TBall : public Unit
{
double m_pos;
float m_vel;
double m_prev;
};
extern "C"
{
void load(InterfaceTable *inTable);
void Spring_Ctor(Spring *unit);
void Spring_next(Spring *unit, int inNumSamples);
//void Friction_Ctor(Friction *unit);
//void Friction_next(Friction *unit, int inNumSamples);
void Ball_Ctor(Ball *unit);
void Ball_next(Ball *unit, int inNumSamples);
void TBall_Ctor(TBall *unit);
void TBall_next(TBall *unit, int inNumSamples);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Spring_Ctor(Spring *unit)
{
SETCALC(Spring_next);
unit->m_vel = 0.f;
unit->m_pos = 0.f;
Spring_next(unit, 1);
}
// in, spring, damping
void Spring_next(Spring *unit, int inNumSamples)
{
float pos = unit->m_pos;
float vel = unit->m_vel;
float *out = ZOUT(0); // out force
float *in = ZIN(0); // in force
float spring = ZIN0(1); // spring constant
float damping = 1.f - ZIN0(2);// damping
float c = SAMPLEDUR;
float rc = SAMPLERATE;
spring = spring * c;
LOOP1(inNumSamples,
float force = ZXP(in) * c - pos * spring;
vel = (force + vel) * damping;
pos += vel;
ZXP(out) = force * rc;
);
unit->m_pos = pos;
unit->m_vel = vel;
}
//////////////////////////////////////////////////////////////////////////////////////////
/*
void Friction_Ctor(Friction *unit)
{
SETCALC(Friction_next);
unit->m_vel = 0.f;
unit->m_pos = 0.f;
unit->m_state = 0;
Friction_next(unit, 1);
}
// in, spring, damping
void Friction_next(Friction *unit, int inNumSamples)
{
float pos = unit->m_pos;
float vel = unit->m_vel;
int state = unit->m_state;
...
void Index_next_a(Index *unit, int inNumSamples)
{
// get table
GET_TABLE
float *table = bufData;
int32 maxindex = tableSize - 1;
float *out = ZOUT(0);
float *in = ZIN(1);
LOOP1(inNumSamples,
int32 index = (int32)ZXP(in);
index = sc_clip(index, 0, maxindex);
ZXP(out) = table[index];
);
}
...
float *out = ZOUT(0); // out force
float *in = ZIN(0); // in displacement
float spring = ZIN0(1); // spring constant
float damping = 1.f - ZIN0(2); // friction -> replace by buffer number for friction table
float c = SAMPLEDUR;
float rc = SAMPLERATE;
spring *= c;
LOOP1(inNumSamples,
float inpos = ZXP(in) * c;
float force = inpos + pos * spring;
state = (vel > damping) ? 1 : 0; // friction table here
if(state) { // stuck
vel = 0.f;
pos += inpos;
} else { // loose
vel += force;
pos -= vel;
}
ZXP(out) = force * rc;
);
unit->m_pos = pos;
unit->m_vel = vel;
unit->m_state = state;
}
#define GET_TABLE \
float fbufnum = ZIN0(0); \
if (fbufnum != unit->m_fbufnum) { \
uint32 bufnum = (uint32)fbufnum; \
World *world = unit->mWorld; \
if (bufnum >= world->mNumSndBufs) bufnum = 0; \
unit->m_buf = world->mSndBufs + bufnum; \
} \
SndBuf *buf = unit->m_buf; \
if(!buf) { \
ClearUnitOutputs(unit, inNumSamples); \
return; \
} \
float *bufData __attribute__((__unused__)) = buf->data; \
if (!bufData) { \
ClearUnitOutputs(unit, inNumSamples); \
return; \
} \
int tableSize = buf->samples;
*/
//////////////////////////////////////////////////////////////////////////////////////////
void Ball_Ctor(Ball *unit)
{
SETCALC(Ball_next);
unit->m_vel = 0.f;
unit->m_pos = ZIN0(0);
unit->m_prev = ZIN0(0);
Ball_next(unit, 1);
}
void Ball_next(Ball *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0); // floor position
float g_in = ZIN0(1); // gravity
float damping = 1 - ZIN0(2); // damping
float k = ZIN0(3); // friction
float pos = unit->m_pos;
float vel = unit->m_vel;
float prev_floor = unit->m_prev;
float c = SAMPLEDUR;
float maxvel = c * 1000.f;
float minvel = 0.f - maxvel;
float inter = c * 1000.f;
RGen& rgen = *unit->mParent->mRGen;
float g = c * g_in;
#ifdef _MSC_VER
k *= g_in; // stickyness proportional to gravity
#else //#ifdef _MSC_VER
k = (double) k * (double) g_in; // stickyness proportional to gravity
#endif //#ifdef _MSC_VER
LOOP1(inNumSamples,
float floor = ZXP(in);
float floorvel;
float dither;
vel -= g;
pos += vel;
float dist = pos - floor;
floorvel = floor - prev_floor;
floorvel = sc_clip(floorvel, minvel, maxvel);
float vel_diff = floorvel - vel;
if(sc_abs(dist) < k) { // sticky friction: maybe vel dependant?
if(sc_abs(dist) < (k*0.005)) {
vel = 0.f;
pos = floor + g;
} else {
vel = vel_diff * inter + vel;
pos = (floor - pos) * inter + pos;
}
} else if(dist <= 0.f) {
pos = floor - dist;
vel = vel_diff;
vel *= damping;
dither = rgen.frand() * 0.00005f * g_in; // dither to reduce jitter
//if(sc_abs(dist) < 0.000001) { vel += dither; }
vel += dither;
}
prev_floor = floor;
ZXP(out) = pos;
);
unit->m_pos = pos;
unit->m_vel = vel;
unit->m_prev = prev_floor;
}
//////////////////////////////////////////////////////////////////////////////////////////
void TBall_Ctor(TBall *unit)
{
SETCALC(TBall_next);
unit->m_vel = 0.f;
unit->m_pos = ZIN0(0);
unit->m_prev = ZIN0(0);
TBall_next(unit, 1);
}
void TBall_next(TBall *unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0); // floor position
float g_in = ZIN0(1); // gravity
float damping = 1 - ZIN0(2);// damping
float k = ZIN0(3); // friction
double pos = unit->m_pos;
float vel = unit->m_vel;
double prev_floor = unit->m_prev;
float c = SAMPLEDUR;
float maxvel = c * 1000.f;
float minvel = 0.f - maxvel;
float inter = c * 10000.f;
RGen& rgen = *unit->mParent->mRGen;
float g = c * g_in;
#ifdef _MSC_VER
k *= g_in; // stickyness proportional to gravity
#else //#ifdef _MSC_VER
k = (double) k * (double) g_in; // stickyness proportional to gravity
#endif //#ifdef _MSC_VER
LOOP1(inNumSamples,
double floor = ZXP(in);
float floorvel;
float outval = 0.f;
float dither;
vel -= g;
pos += vel;
double dist = pos - floor;
floorvel = floor - prev_floor;
floorvel = sc_clip(floorvel, minvel, maxvel);
float vel_diff = floorvel - vel;
if(sc_abs(dist) < k) { // sticky friction: vel dependant?
if(sc_abs(dist) < (k*0.005)) {
vel = 0.f;
pos = floor + g;
} else {
vel = vel_diff * inter + vel;
pos = (floor - pos) * inter + pos;
}
} else if(dist <= 0.f) {
pos = floor - dist;
vel = floorvel - vel;
vel *= damping;
outval = vel;
dither = rgen.frand() * 0.001f * g_in; // dither to reduce sampling jitter
//if(sc_abs(dist) < 0.003) { vel += dither; }
vel += dither;
}
prev_floor = floor;
ZXP(out) = outval;
);
unit->m_pos = pos;
unit->m_vel = vel;
unit->m_prev = prev_floor;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
PluginLoad(PhysicalModeling)
{
ft = inTable;
DefineSimpleUnit(Spring);
// DefineSimpleUnit(Friction);
DefineSimpleUnit(Ball);
DefineSimpleUnit(TBall);
}
|