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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/*
sc_noise.c:
Based on the noise ugens of SuperCollider.
Copyright (c) Tito Latini, 2012
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
08.03.2015 gausstrig was fixed to properly work at k-time.
Also I added an optional feature related to the behavior
of very first impulse.
-- Gleb Rogozinsky
Code corrected 22 Dec 2017 by JPff
*/
#include "csoundCore.h"
typedef struct {
OPDS h;
MYFLT *out, *kamp, *kdensity, density0, thresh, scale;
int32 rand;
} DUST;
typedef struct {
OPDS h;
/* 8.03.15 Added new option ifrst1 --Gleb R */
MYFLT *out, *kamp, *kfrq, *kdev, *imode, *ifrst1, frq0, first;
int32 count, rand, mmode;
} GAUSSTRIG;
#define BIPOLAR 0x7FFFFFFF /* Constant to make bipolar */
#define dv2_31 (FL(4.656612873077392578125e-10))
static int32_t dust_init(CSOUND *csound, DUST *p)
{
p->density0 = FL(0.0);
p->thresh = FL(0.0);
p->scale = FL(0.0);
p->rand = csoundRand31(&csound->randSeed1);
return OK;
}
static int32_t dust_process_krate(CSOUND *csound, DUST *p)
{
MYFLT density, thresh, scale, r;
density = *p->kdensity;
if (density != p->density0) {
thresh = p->thresh = density * csound->onedsr*csound->ksmps;
scale = p->scale = (thresh > FL(0.0) ? FL(1.0) / thresh : FL(0.0));
p->density0 = density;
}
else {
thresh = p->thresh;
scale = p->scale;
}
p->rand = csoundRand31(&p->rand);
r = (MYFLT)p->rand * dv2_31;
*p->out = *p->kamp * (r < thresh ? r*scale : FL(0.0));
return OK;
}
static int32_t dust_process_arate(CSOUND *csound, DUST *p)
{
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT *out, density, thresh, scale;
out = p->out;
density = *p->kdensity;
if (density != p->density0) {
thresh = p->thresh = density * csound->onedsr;
scale = p->scale = (thresh > FL(0.0) ? FL(1.0) / thresh : FL(0.0));
p->density0 = density;
}
else {
thresh = p->thresh;
scale = p->scale;
}
if (UNLIKELY(offset)) memset(p->out, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&p->out[nsmps], '\0', early*sizeof(MYFLT));
}
//memset(out, '\0', offset*sizeof(MYFLT));
for (n=offset; n<nsmps; n++) {
MYFLT r;
p->rand = csoundRand31(&p->rand);
r = (MYFLT)p->rand * dv2_31;
out[n] = *p->kamp * (r < thresh ? r*scale : FL(0.0));
}
return OK;
}
static int32_t dust2_process_krate(CSOUND *csound, DUST *p)
{
MYFLT density, thresh, scale, r;
density = *p->kdensity;
if (density != p->density0) {
thresh = p->thresh = density * csound->onedsr*csound->ksmps;
scale = p->scale = (thresh > FL(0.0) ? FL(2.0) / thresh : FL(0.0));
p->density0 = density;
}
else {
thresh = p->thresh;
scale = p->scale;
}
p->rand = csoundRand31(&p->rand);
r = (MYFLT)p->rand * dv2_31;
*p->out = *p->kamp * (r < thresh ? r*scale - FL(1.0) : FL(0.0));
return OK;
}
static int32_t dust2_process_arate(CSOUND *csound, DUST *p)
{
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT *out, density, thresh, scale;
out = p->out;
density = *p->kdensity;
if (density != p->density0) {
thresh = p->thresh = density * csound->onedsr;
scale = p->scale = (thresh > FL(0.0) ? FL(2.0) / thresh : FL(0.0));
p->density0 = density;
}
else {
thresh = p->thresh;
scale = p->scale;
}
if (UNLIKELY(offset)) memset(p->out, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&p->out[nsmps], '\0', early*sizeof(MYFLT));
}
//memset(out, '\0', offset*sizeof(MYFLT));
for (n=offset; n<nsmps; n++) {
MYFLT r;
p->rand = csoundRand31(&p->rand);
r = (MYFLT)p->rand * dv2_31;
out[n] = *p->kamp * (r < thresh ? r*scale - FL(1.0) : FL(0.0));
}
return OK;
}
/* gausstrig opcode based on Bhob Rainey's GaussTrig ugen */
static int32_t gausstrig_init(CSOUND* csound, GAUSSTRIG *p)
{
p->rand = csoundRand31(&csound->randSeed1);
p->first = *p->ifrst1;
#if 0
if (*p->ifrst1 > FL(0.0)) {
/* values less than FL(0.0) could be used in later versions
as an offset in samples */
int32_t nextsamps;
MYFLT nextcount, frq, dev, r1, r2;
p->frq0 = *p->kfrq;
frq = (*p->kfrq > FL(0.001) ? *p->kfrq : FL(0.001));
dev = *p->kdev;
nextsamps = (int32_t)(csound->GetSr(csound) / frq);
p->rand = csoundRand31(&p->rand);
r1 = (MYFLT)p->rand * dv2_31;
p->rand = csoundRand31(&p->rand);
r2 = (MYFLT)p->rand * dv2_31;
nextcount = SQRT(FL(-2.0) * LOG(r1)) * SIN(r2 * TWOPI_F);
if (nextcount < FL(-1.0)) {
MYFLT diff = FL(-1.0) - nextcount;
nextcount = (FL(1.0) < FL(-1.0) + diff ? FL(1.0) : FL(-1.0) + diff);
}
else if (nextcount > FL(1.0)) {
MYFLT diff = nextcount - FL(1.0);
nextcount = (FL(-1.0) > FL(1.0) - diff ? FL(-1.0) : FL(1.0) - diff);
}
p->count = (int32_t)(nextsamps + nextcount * dev * nextsamps);
}
#endif
//else {
/* GaussTrig UGen behavior */
p->count = 0;
//}
/*
* imode > 0 means better frequency modulation. If the frequency
* changes, the delay before the next impulse is calculed again.
* With the default imode value we have the classic behavior of
* the GaussTrig ugen, where the freq modulation is bypassed
* during the delay time that precedes the next impulse.
*/
p->mmode = (*p->imode <= FL(0.0) ? 0 : 1);
return OK;
}
/* a separate k-time init for proper work of gausstrig */
static int32_t gausstrig_initk(CSOUND* csound, GAUSSTRIG *p)
{
p->rand = csoundRand31(&csound->randSeed1);
p->first = *p->ifrst1;
#if 0
if (*p->ifrst1 > FL(0.0)) {
/* values less than FL(0.0) could be used in later versions
as an offset in samples */
int32_t nextsamps;
MYFLT nextcount, frq, dev, r1, r2;
p->frq0 = *p->kfrq;
frq = (*p->kfrq > FL(0.001) ? *p->kfrq : FL(0.001));
dev = *p->kdev;
/* this very line of k-time fix. Changed GetSt to GetKr */
nextsamps = (int32_t)(csound->GetKr(csound) / frq);
p->rand = csoundRand31(&p->rand);
r1 = (MYFLT)p->rand * dv2_31;
p->rand = csoundRand31(&p->rand);
r2 = (MYFLT)p->rand * dv2_31;
nextcount = SQRT(FL(-2.0) * LOG(r1)) * SIN(r2 * TWOPI_F);
if (nextcount < FL(-1.0)) {
MYFLT diff = FL(-1.0) - nextcount;
nextcount = (FL(1.0) < FL(-1.0) + diff ? FL(1.0) : FL(-1.0) + diff);
}
else if (nextcount > FL(1.0)) {
MYFLT diff = nextcount - FL(1.0);
nextcount = (FL(-1.0) > FL(1.0) - diff ? FL(-1.0) : FL(1.0) - diff);
}
p->count = (int32_t)(nextsamps + nextcount * dev * nextsamps);
}
#endif
//else {
/* GaussTrig UGen behavior */
p->count = 0;
//}
p->mmode = (*p->imode <= FL(0.0) ? 0 : 1);
return OK;
}
static int32_t gausstrig_process_krate(CSOUND* csound, GAUSSTRIG *p)
{
MYFLT frq, dev;
p->frq0 = *p->kfrq;
frq = (*p->kfrq > FL(0.001) ? *p->kfrq : FL(0.001));
dev = *p->kdev;
if (p->first > 0) {
/* values less than FL(0.0) could be used in later versions
as an offset in samples */
int32_t nextsamps;
MYFLT nextcount, r1, r2;
/* this very line of k-time fix. Changed GetSt to GetKr */
nextsamps = (int32_t)(csound->GetKr(csound) / frq);
p->rand = csoundRand31(&p->rand);
r1 = (MYFLT)p->rand * dv2_31;
p->rand = csoundRand31(&p->rand);
r2 = (MYFLT)p->rand * dv2_31;
nextcount = SQRT(FL(-2.0) * LOG(r1)) * SIN(r2 * TWOPI_F);
if (nextcount < FL(-1.0)) {
MYFLT diff = FL(-1.0) - nextcount;
nextcount = (FL(1.0) < FL(-1.0) + diff ? FL(1.0) : FL(-1.0) + diff);
}
else if (nextcount > FL(1.0)) {
MYFLT diff = nextcount - FL(1.0);
nextcount = (FL(-1.0) > FL(1.0) - diff ? FL(-1.0) : FL(1.0) - diff);
}
p->count = (int32_t)(nextsamps + nextcount * dev * nextsamps);
p->first = 0;
}
if (p->count <= 0) {
int32_t nextsamps;
MYFLT nextcount, r1, r2;
/* this very line of k-time fix. Changed GetSt to GetKr */
nextsamps = (int32_t)(csound->GetKr(csound) / frq);
p->rand = csoundRand31(&p->rand);
r1 = (MYFLT)p->rand * dv2_31;
p->rand = csoundRand31(&p->rand);
r2 = (MYFLT)p->rand * dv2_31;
nextcount = SQRT(FL(-2.0) * LOG(r1)) * SIN(r2 * TWOPI_F);
if (nextcount < FL(-1.0)) {
MYFLT diff = FL(-1.0) - nextcount;
nextcount = (FL(1.0) < FL(-1.0) + diff ? FL(1.0) : FL(-1.0) + diff);
}
else if (nextcount > FL(1.0)) {
MYFLT diff = nextcount - FL(1.0);
nextcount = (FL(-1.0) > FL(1.0) - diff ? FL(-1.0) : FL(1.0) - diff);
}
p->count = (int32_t)(nextsamps + nextcount * dev * nextsamps);
*p->out = *p->kamp;
}
else {
if (p->mmode && *p->kfrq != p->frq0)
p->count = 0;
*p->out = FL(0.0);
}
p->count--;
return OK;
}
static int32_t gausstrig_process_arate(CSOUND* csound, GAUSSTRIG *p)
{
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT *out = p->out;
MYFLT frq, dev;
p->frq0 = *p->kfrq;
frq = (p->frq0 > FL(0.001) ? p->frq0 : FL(0.001));
dev = *p->kdev;
if (UNLIKELY(offset)) memset(p->out, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&p->out[nsmps], '\0', early*sizeof(MYFLT));
}
//memset(out, '\0', offset*sizeof(MYFLT));
if (p->first > FL(0.0)) {
/* values less than FL(0.0) could be used in later versions
as an offset in samples */
int32_t nextsamps;
MYFLT nextcount, dev, r1, r2;
//p->frq0 = *p->kfrq;
//frq = (p->frq0 > FL(0.001) ? p->frq0 : FL(0.001));
dev = *p->kdev;
nextsamps = (int32_t)(csound->GetSr(csound) / frq);
p->rand = csoundRand31(&p->rand);
r1 = (MYFLT)p->rand * dv2_31;
p->rand = csoundRand31(&p->rand);
r2 = (MYFLT)p->rand * dv2_31;
nextcount = SQRT(FL(-2.0) * LOG(r1)) * SIN(r2 * TWOPI_F);
if (nextcount < FL(-1.0)) {
MYFLT diff = FL(-1.0) - nextcount;
nextcount = (FL(1.0) < FL(-1.0) + diff ? FL(1.0) : FL(-1.0) + diff);
}
else if (nextcount > FL(1.0)) {
MYFLT diff = nextcount - FL(1.0);
nextcount = (FL(-1.0) > FL(1.0) - diff ? FL(-1.0) : FL(1.0) - diff);
}
p->count = (int32_t)(nextsamps + nextcount * dev * nextsamps);
p->first = 0; /* Only once called */
}
for (n=offset; n<nsmps; n++) {
if (p->count <= 0) {
int32_t nextsamps;
MYFLT nextcount, r1, r2;
nextsamps = (int32_t)(csound->GetSr(csound) / frq);
p->rand = csoundRand31(&p->rand);
r1 = (MYFLT)p->rand * dv2_31;
p->rand = csoundRand31(&p->rand);
r2 = (MYFLT)p->rand * dv2_31;
nextcount = SQRT(FL(-2.0) * LOG(r1)) * SIN(r2 * TWOPI_F);
if (nextcount < FL(-1.0)) {
MYFLT diff = FL(-1.0) - nextcount;
nextcount = (FL(1.0) < FL(-1.0) + diff ? FL(1.0) : FL(-1.0) + diff);
}
else if (nextcount > FL(1.0)) {
MYFLT diff = nextcount - FL(1.0);
nextcount = (FL(-1.0) > FL(1.0) - diff ? FL(-1.0) : FL(1.0) - diff);
}
p->count = (int32_t)(nextsamps + nextcount * dev * nextsamps);
out[n] = *p->kamp;
}
else {
if (p->mmode && *p->kfrq != p->frq0)
p->count = 0;
out[n] = FL(0.0);
}
p->count--;
}
return OK;
}
static OENTRY scnoise_localops[] = {
{ "dust.k", sizeof(DUST), 0,3, "k", "kk",
(SUBR)dust_init, (SUBR)dust_process_krate, NULL },
{ "dust.a", sizeof(DUST), 0,3, "a", "kk",
(SUBR)dust_init, (SUBR)dust_process_arate },
{ "dust2.k", sizeof(DUST), 0,3, "k", "kk",
(SUBR)dust_init, (SUBR)dust2_process_krate, NULL },
{ "dust2.a", sizeof(DUST), 0,3, "a", "kk",
(SUBR)dust_init, (SUBR)dust2_process_arate },
{ "gausstrig.k", sizeof(GAUSSTRIG), 0,3, "k", "kkkoo",
(SUBR)gausstrig_initk, (SUBR)gausstrig_process_krate, NULL },
{ "gausstrig.a", sizeof(GAUSSTRIG), 0,3, "a", "kkkoo",
(SUBR)gausstrig_init, (SUBR)gausstrig_process_arate }
};
LINKAGE_BUILTIN(scnoise_localops)
|