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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
|
title:: Operators
categories:: Language, Common methods
summary:: common unary and binary operators
related:: Reference/Adverbs
SuperCollider supports operator overloading. Operators can thus be applied to a variety of different objects; Numbers, Ugens, Collections, and so on. When operators are applied to ugens they result in link::Classes/BinaryOpUGen::s or link::Classes/UnaryOpUGen::s, through the methods of link::Classes/AbstractFunction::.
This is a list of some of the common unary and binary operators that are implemented by several classes.
See the specific classes for details and other operators.
You can see which classes implements a specific operator by clicking on the method name.
section:: Unary Operators
Unary operators may be written in two ways:
code::
a.operator
operator(a)
::
subsection:: Arithmetics
method:: neg
Inversion.
discussion::
code::
{
var a;
a = FSinOsc.ar(300);
[ a, a.neg ]
}.plot
::
method:: reciprocal
Reciprocal (1/x).
method:: abs
Absolute value.
method:: floor
Next lower integer.
discussion::
code::
{
var a;
a = Line.ar(-1, 1, 0.01);
[ a, a.floor ]
}.plot
::
method:: ceil
Next higher integer.
discussion::
code::
{
var a;
a = Line.ar(-1, 1, 0.01);
[ a, a.ceil ]
}.plot
::
method:: frac
Fractional part.
method:: sign
Sign function.
returns::
-1 when a < 0, +1 when a > 0, 0 when a is 0
method:: squared
Squared value.
returns::
code:: a*a ::
method:: cubed
Cubed value.
returns::
code:: a*a*a ::
method:: sqrt
Square root.
discussion::
The definition of square root is extended for signals so that sqrt(a) when a<0 returns
-sqrt(-a).
method:: exp
Exponential.
subsection:: Musical acoustics
method:: midicps
Convert MIDI note number to cycles per second.
discussion::
code::
{
Saw.ar(Line.kr(24,108,10).midicps, 0.2)
}.play
::
method:: cpsmidi
Convert cycles per second to MIDI note.
method:: midiratio
Convert an interval in MIDI notes into a frequency ratio.
method:: ratiomidi
Convert a frequency ratio to an interval in MIDI notes.
method:: dbamp
Convert decibels to linear amplitude.
method:: ampdb
Convert linear amplitude to decibels.
method:: octcps
Convert decimal octaves to cycles per second.
method:: cpsoct
Convert cycles per second to decimal octaves.
subsection:: Random operators
See also link::Guides/Randomness::
method::rand
Returns an evenly distributed random value between this and zero.
code::
10.rand;
{ SinOsc.ar(110).rand }.plot;
::
method::rand2
Returns an evenly distributed random value between [+this ... - this].
code::
10.rand2;
{ SinOsc.ar(110).rand2 }.plot;
::
method::linrand
Returns a linearly distributed random value between this and zero.
code::
10.linrand;
{ SinOsc.ar(110).linrand }.plot;
::
method::bilinrand
Returns a linearly distributed random value between [+this ... - this].
code::
10.bilinrand;
{ SinOsc.ar(110).bilinrand }.plot;
::
method::sum3rand
Returns a value from a gaussian-like random distribution between this and zero.
This was suggested by Larry Polansky as a loose approximation of gaussian. Follows the formula:
code::
{ 1.0.rand }.dup(3).sum - 1.5 * (2/3)
::
code::
10.sum3rand;
{ SinOsc.ar(110).sum3rand }.plot;
::
method::coin
Returns one or zero with the probability given by the argument.
code::
0.4.coin;
{ SinOsc.ar(110).coin }.plot;
::
subsection:: Other
method:: log
Natural logarithm.
discussion::
code::
{
var a, e;
e = exp(1);
a = Line.ar(e, 1/e, 0.01);
a.log
}.plot
::
method:: log2
Base 2 logarithm.
method:: log10
Base 10 logarithm.
method:: sin
Sine.
method:: cos
Cosine.
method:: tan
Tangent.
method:: asin
Arcsine.
method:: acos
Arccosine.
method:: atan
Arctangent.
method:: sinh
Hyperbolic sine.
method:: cosh
Hyperbolic cosine.
method:: tanh
Hyperbolic tangent.
method:: distort
Nonlinear distortion.
discussion::
The formula used is :
code::
x / (1 + abs(x))
::
Here is an example :
code::
(
{
var a;
a = Line.ar(-4, 4, 0.01);
a.distort
}.plot
)
{ FSinOsc.ar(500, 0.0, XLine.kr(0.1, 10, 10)).distort * 0.25 }.scope;
::
method:: softclip
Nonlinear distortion.
discussion::
Distortion with a perfectly linear region from -0.5 to +0.5
code::
(
{
var a;
a = Line.ar(-2, 2, 0.01);
a.softclip
}.plot
)
{ FSinOsc.ar(500,0.0, XLine.kr(0.1, 10, 10)).softclip * 0.25 }.scope(2);
::
method:: isPositive
Test if signal is >= 0.
method:: isNegative
Test if signal is < 0.
method:: isStrictlyPositive
Test if signal is > 0.
section:: Binary Operators
Three different syntaxes can be used for binary operators consisting of letters:
code::
operator(a, b)
a operator: b
a.operator(b)
::
Operators consisting of symbols are written like this:
code::
a + b
::
subsection:: Arithmetics
method:: +
Addition.
method:: -
Subtraction.
method:: *
Multiplication.
method:: /
Division.
method:: %
Floating point modulo.
method:: **
Exponentiation. Same as pow.
method:: pow
Exponentiation.
NOTE:: When used with UGens which produce a negative signal this function extends the usual definition of exponentiation and returns code::neg(neg(a) ** b)::. This allows exponentiation of negative signal values by noninteger exponents.::
method:: lcm
Least common multiple. This definition extends the usual definition and returns a negative number if strong::any of the operands:: is negative. This makes it consistent with the lattice-theoretical interpretation and its idempotency, commutative, associative, absorption laws.
Following the example of the programming language J (see: link::Guides/J-concepts-in-SC::), lcm is analogous to logical strong::and:: (see also: link::http://www.jsoftware.com/papers/eem/gcd.htm::).
code::
lcm(4, 6);
lcm(1, 1); // and
lcm(1624, 26);
lcm(1624, -26);
lcm(-1624, -26);
lcm(513, gcd(513, 44)) // absorption law -> 513.
::
code::
(
{
var mx = MouseX.kr(-20, 20);
var my = MouseY.kr(-20, 20);
SinOsc.ar(SinOsc.kr(0.3) * 20 lcm: [mx, my] * 30 + 500) * 0.1
}.play;
)
::
method:: gcd
Greatest common divisor. This definition extends the usual definition and returns a negative number if strong::both operands:: are negative. This makes it consistent with the lattice-theoretical interpretation and its idempotency, commutative, associative, absorption laws.
"greater" means "divisible by" in this interpretation, so code::gcd(-1, -1):: returns a negative number. This is necessary to make the whole system consistent (fundamental law of arithmetics, idempotency and absorption laws would fail). See examples below.
Following the example of the programming language J (see: link::Guides/J-concepts-in-SC::), gcd is analogous to logical strong::or:: (see also: link::http://www.jsoftware.com/papers/eem/gcd.htm::).
code::
gcd(4, 6);
gcd(0, 1); // or
gcd(1024, 256);
gcd(1024, -256);
gcd(-1024, -256); // "greater" means "divisible by" in this case, so this returns a negative number.
gcd(-1024, lcm(-1024, 256)) // absorption law -> -1024.
gcd(66, 54) * lcm(66, 54) == (66 * 54); // true
::
code::
(
{
var mx = MouseX.kr(-200, 200);
var my = MouseY.kr(-200, 200);
SinOsc.ar(SinOsc.kr(0.3) * 20 gcd: [mx, my] * 30 + 500) * 0.1
}.play;
)
::
Here is an overview of how negative numbers are treated:
code::
lcm(4, 6) // -> 12. "least multiple" interpreted as smallest in Z
lcm(4, -6) // -> -12 "least multiple" interpreted as smallest in Z
lcm(-4, -6) // -> -12 "least multiple" interpreted as smallest in Z
gcd(4, 6) // -> 2 "greatest divisor" interpreted as highest in Z
gcd(4, -6) // -> 2 "greatest divisor" is interpreted as highest in Z
gcd(-4, -6) // -> -2 "greatest divisor" is interpreted as *inverse* in Z. This is the only necessary asymmetry.
::
subsection:: Comparisons
method:: <
Less than.
method:: <=
Less than or equal.
method:: >
Greater than.
discussion::
With UGens, this can be useful for triggering purposes, among other things:
code::
(
{ // trigger an envelope
var trig;
trig = SinOsc.ar(1) > 0;
EnvGen.kr(Env.perc, trig, doneAction: Done.none)
* SinOsc.ar(440, 0, 0.1)
}.play
)
// trigger a synth
(
SynthDef("help-EnvGen", { arg out=0;
Out.ar(out,
EnvGen.kr(Env.perc,1.0,doneAction: Done.freeSelf)
* SinOsc.ar(440, 0, 0.1)
)
}).add;
// This synth has no output. It only checks amplitude of input and looks for a transition from < 0.2
// to > 0.2
{ SendTrig.kr(Amplitude.kr(SoundIn.ar(0)) > 0.2) }.play;
// OSCFunc to trigger synth
OSCFunc({ "triggered".postln; Synth.new("help-EnvGen") },'/tr', s.addr);
)
::
method:: >=
Greater than or equal.
method:: ==
Equal.
method:: !=
Not equal.
method:: |==|
"Lazy equality." See link::Classes/Object#-|==|::.
subsection:: Other
method:: <!
Return first argument.
code::
// this is useful when two ugens need to be called, but only one of their outputs is needed
(
{
var a, b, c;
a = Dseq([1, 2, 3, 4], inf).dpoll("a");
b = Dseq([1955, 1952, 1823, 1452], inf).dpoll("b");
c = (a <! b).dpoll("------> a <! b = "); // c only
Duty.kr(0.4, 0, c);
0.0
}.play
)
::
method:: min
Minimum.
discussion::
code::
{ // distorts and envelopes z
var z;
z = FSinOsc.ar(500);
z min: FSinOsc.ar(0.1);
}.play;
::
method:: max
Maximum.
discussion::
code::
{ // modulates and envelopes z
var z;
z = FSinOsc.ar(500);
z max: FSinOsc.ar(0.1);
}.play;
::
method:: round
Quantization by rounding. Rounds a to the nearest multiple of b.
method:: trunc
Quantization by truncation. Truncate a to a multiple of b.
method:: hypot
Hypotenuse. Returns the square root of the sum of the squares of a and b. Or equivalently, the distance from the origin
to the point (x, y).
discussion::
In this example, hypot is used to calculate a doppler shift pitch and amplitude based on distance.
code::
(
{
var x, y, distance, velocity, pitchRatio, amplitude;
// object travels 200 meters in 6 secs (=120kph) passing 10 meters
// from the listener
x = 10;
y = LFSaw.kr(1/6, 0, 100);
distance = hypot(x, y);
velocity = Slope.kr(distance);
pitchRatio = (344 - velocity) / 344; // speed of sound is 344 meters/sec
amplitude = 10 / distance.squared;
FSinOsc.ar(1000 * pitchRatio, 0, amplitude)
}.play)
::
The next example uses the distance to modulate a delay line.
code::
(
{
var x, y, distance, velocity, pitchRatio, amplitude, motorSound;
// object travels 200 meters in 6 secs (=120kph) passing 10 meters
// from the listener
x = 10;
y = LFSaw.kr(1/6, 0, 100);
distance = hypot(x, y);
amplitude = 40 / distance.squared;
motorSound = RLPF.ar(FSinOsc.ar(200, 0, LFPulse.ar(31.3, 0, 0.4)), 400, 0.3);
DelayL.ar(motorSound, 110/344, distance/344, amplitude)
}.play)
::
method:: hypotApx
Hypotenuse approximation. Returns an approximation of the square root of the sum of the squares of x and y.
discussion::
The formula used is :
code::
abs(x) + abs(y) - ((sqrt(2) - 1) * min(abs(x), abs(y)))
::
hypotApx is used to implement Complex method magnitudeApx.
This should not be used for simulating a doppler shift because it is discontinuous. Use hypot.
See also link::#hypot::, link::#atan2::.
method:: atan2
Returns the arctangent of y/x.
discussion::
OK, now we can add a pan to the link::#hypot:: doppler examples by using atan2 to find the azimuth,
or direction angle, of the sound source.
Assume speakers at +/- 45 degrees and clip the direction to between those.
code::
(
{
var x, y, distance, velocity, pitchRatio, amplitude, azimuth, panValue;
// object travels 200 meters in 6 secs (=120kph) passing 10 meters
// from the listener
x = 10;
y = LFSaw.kr(1/6, 0, 100);
distance = hypot(x, y);
velocity = Slope.kr(distance);
pitchRatio = (344 - velocity) / 344; // speed of sound is 344 meters/sec
amplitude = 10 / distance.squared;
azimuth = atan2(y, x); // azimuth in radians
panValue = (azimuth / 0.5pi).clip2(1);
Pan2.ar(FSinOsc.ar(1000 * pitchRatio), panValue, amplitude)
}.play)
(
{
var x, y, distance, velocity, pitchRatio, amplitude, motorSound,
azimuth, panValue;
// object travels 200 meters in 6 secs (=120kph) passing 10 meters
// from the listener
x = 10;
y = LFSaw.kr(1/6, 0, 100);
distance = hypot(x, y);
amplitude = 40 / distance.squared;
motorSound = RLPF.ar(FSinOsc.ar(200, 0, LFPulse.ar(31.3, 0, 0.4)), 400, 0.3);
azimuth = atan2(y, x); // azimuth in radians
panValue = (azimuth / 0.5pi).clip2(1); // make a value for Pan2 from azimuth
Pan2.ar(DelayL.ar(motorSound, 110/344, distance/344), panValue, amplitude)
}.play)
::
method:: ring1
Ring modulation plus first source.
discussion::
Return the value of ((a*b) + a). This is more efficient than using
separate unit generators for the multiply and add.
See also link::#*::, link::#ring1::, link::#ring2::, link::#ring3::, link::#ring4::.
code::
{ (FSinOsc.ar(800) ring1: FSinOsc.ar(XLine.kr(200,500,5))) * 0.125 }.play;
::
same as :
code::
(
{
var a, b;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
((a * b) + a) * 0.125
}.play)
::
normal ring modulation:
code::
(
{
var a, b;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
(a * b) * 0.125
}.play)
::
method:: ring2
Ring modulation plus both sources.
discussion::
Return the value of ((a*b) + a + b). This is more efficient than using
separate unit generators for the multiply and adds.
code::
{ (FSinOsc.ar(800) ring2: FSinOsc.ar(XLine.kr(200,500,5))) * 0.125 }.play;
::
same as :
code::
(
{
var a, b;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
((a * b) + a + b) * 0.125
}.play)
::
method:: ring3
Ring modulation variant.
discussion::
Return the value of (a*a *b). This is more efficient than using
separate unit generators for each multiply.
code::
{ (FSinOsc.ar(800) ring3: FSinOsc.ar(XLine.kr(200,500,5))) * 0.125 }.play;
::
same as :
code::
(
{
var a, b;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
(a * a * b) * 0.125;
}.play)
::
method:: ring4
Ring modulation variant.
discussion::
Return the value of ((a*a *b) - (a*b*b)). This is more efficient than using
separate unit generators for each operation.
code::
{ (FSinOsc.ar(800) ring4: FSinOsc.ar(XLine.kr(200,500,5))) * 0.125 }.play;
::
same as :
code::
(
{
var a, b;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
((a * a * b) - (a * b * b)) * 0.125
}.play)
::
method:: sumsqr
Sum of squares.
discussion::
Return the value of (a*a) + (b*b). This is more efficient than using
separate unit generators for each operation.
code::
{ (FSinOsc.ar(800) sumsqr: FSinOsc.ar(XLine.kr(200,500,5))) * 0.125 }.play;
::
same as :
code::
(
{
var a, b;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
((a * a) + (b * b)) * 0.125
}.play)
::
method:: difsqr
Difference of squares.
discussion::
Return the value of (a*a) - (b*b). This is more efficient than using
separate unit generators for each operation.
code::
{ (FSinOsc.ar(800) difsqr: FSinOsc.ar(XLine.kr(200,500,5))) * 0.125 }.play;
::
same as :
code::
(
{
var a, b;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
((a * a) - (b * b)) * 0.125
}.play)
::
method:: sqrsum
Square of the sum.
discussion::
Return the value of (a + b)**2. This is more efficient than using
separate unit generators for each operation.
code::
{ (FSinOsc.ar(800) sqrsum: FSinOsc.ar(XLine.kr(200,500,5))) * 0.125 }.play;
::
same as :
code::
(
{
var a, b, c;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
c = a + b;
(c * c) * 0.125
}.play)
::
method:: sqrdif
Square of the difference.
discussion::
Return the value of (a - b)**2. This is more efficient than using
separate unit generators for each operation.
code::
{ (FSinOsc.ar(800) sqrdif: FSinOsc.ar(XLine.kr(200,500,5))) * 0.125 }.play;
::
same as :
code::
(
{
var a, b, c;
a = FSinOsc.ar(800);
b = FSinOsc.ar(XLine.kr(200,500,5));
c = a - b;
(c * c) * 0.125
}.play)
::
method:: absdif
Absolute value of the difference. code:: abs(a - b) ::
discussion::
code::
(
{ // creates a rhythm
var mul = 0.2 absdif: FSinOsc.ar(2, 0, 0.5);
FSinOsc.ar(440, 0, mul);
}.play;
)
::
method:: moddif
On a circle, there are two distances between two points. This operator returns the smaller value of the two.
discussion::
code::
{ Line.ar(0, 4, 0.01).moddif(0) }.plot;
(
{
var mul = 0.2 moddif: FSinOsc.ar(2, 0, 0.5);
FSinOsc.ar(440, 0, mul);
}.play;
)
::
method:: thresh
Thresholding.
discussion::
0 when a < b, otherwise a.
code::
{ LFNoise0.ar(50, 0.5) thresh: 0.45 }.play // a low-rent gate
::
method:: amclip
Two quadrant multiply.
discussion::
0 when b <= 0, a*b when b > 0
code::
{ WhiteNoise.ar.amclip(FSinOsc.kr(1,0.2)) }.play; // makes a sine envelope
::
method:: scaleneg
Scale negative part of input.
discussion::
a*b when a < 0, otherwise a.
code::
{ FSinOsc.ar(500).scaleneg(Line.ar(1,-1,4)) }.play;
::
method:: clip2
Bilateral clipping.
discussion::
clips input wave a to +/- b
code::
(
{
var a;
a = Line.ar(-2, 2, 0.01);
a.clip2
}.plot2
)
{ FSinOsc.ar(400).clip2(0.2) }.scope; // clipping distortion
{ FSinOsc.ar(1000).clip2(Line.kr(0,1,8)) }.scope;
::
method:: wrap2
Bilateral wrapping.
discussion::
wraps input wave to +/- b
code::
(
{
var a;
a = Line.ar(-2, 2, 0.01);
a.wrap2
}.plot
)
{ FSinOsc.ar(1000).wrap2(Line.kr(0,1.01,8)) }.scope;
::
method:: fold2
Bilateral folding.
discussion::
folds input wave a to +/- b
code::
(
{
var a;
a = Line.ar(-2, 2, 0.01);
a.fold2
}.plot
)
{ FSinOsc.ar(1000).fold2(Line.kr(0,1,8)) }.scope;
::
method:: excess
Residual of clipping.
discussion::
Returns the difference of the original signal and its clipped form: (a - clip2(a,b)).
code::
(
{
var a;
a = Line.ar(-2, 2, 0.01);
a.excess
}.plot
)
{ FSinOsc.ar(1000).excess(Line.kr(0,1,8)) }.play;
::
|