Base class for numbers which can be represented by a single one dimensional value.
Most of the Unary and Binary operations are also implemented by UnaryOpUGen and BinaryOpUGen, so you can get more examples by looking at the help for those.
allocates a new SimpleNumber.
Addition
Subtraction
Multiplication
Division
Modulo
Modulo
Integer Division
Exponentiation
Is not
greater than
greater than
greater or equal than
smaller or equal than
Least common multiple
Greatest common divisor
Round to multiple of aNumber
Round up to a multiple of aNumber. For roundDown use trunc.
Truncate to multiple of aNumber (e.g. it rounds numbers down to a multiple of aNumber).
Rounds the value to a multiple of resolution. By using margin and strength you can control which values will be rounded, and by how much.
Conceptually this is the equivalent of MIDI quantization in a DAW/MIDI sequencer. In particular it allows a certain sloppiness close to the resolution value.
Note: this method expects values >= 0.
resolution |
Round this value to a multiple of resolution. E.g. if you chose 1, then all values would be rounded to the nearest integer. |
margin |
Values that are within ±margin from a multiple of resolution will be left as they are. E.g. if you chose a resolution value of 0.5 and a margin of 0.01, then the values 0.501 and 0.499 would be left as they are, but the value 0.502 would become 0.5. This should be a value between 0 and resolution. |
strength |
Determines the degree to which this number will be changed. If strength is 1, then this function will return the nearest resolution. If it is 0, then value of this number will be left unchanged. E.g. If the resolution was 1 and strength was 0.5, then the value 0.6 would become 0.8. |
((0..10) / 5).collect { |num| [num, num.softRound(1, 0, 1)] }; ((0..10) / 5).collect { |num| [num, num.softRound(1, 0.3, 1)] }; ((0..10) / 5).collect { |num| [num, num.softRound(1, 0, 0.5)] };
Rounds the values margin distance from resolution to a multiple of resolution. By using margin and strength you can control when values will be rounded, and by how much.
Conceptually this is the equivalent of 'snap' in a graphics program. Values within a certain distance (margin) from a grid line are snapped to it. All other values are unchanged.
Note: this method expects values >= 0.
resolution |
Snap this value to a multiple of resolution. E.g. if you chose 1, then all values would be rounded to the nearest integer. |
margin |
Only values that are greater ±margin from a multiple of resolution value will be changed. Values that are less than margin will be unchanged. E.g. if you chose a resolution value of 0.5 and a margin of 0.01, then the values 0.501 and 0.499 would be snapped to 0.5, but the value 0.502 would be unchanged. This should be a value between 0 and resolution. |
strength |
Determines the degree to which this number will be changed. If strength is 1, then this function will return the nearest resolution. If it is 0, then value of this number will be left unchanged. E.g. If the resolution was 1 and strength was 0.5, then the value 0.6 would become 0.8. |
((0..10) / 5).collect { |num| [num, num.snap(1, 0, 1)] }; ((0..10) / 5).collect { |num| [num, num.snap(1, 0.3, 1)] }; ((0..10) / 5).collect { |num| [num, num.snap(1, 0, 0.5)] };
Minimum
Maximum
Arctangent of (this/aNumber)
Square root of the sum of the squares.
Base e logarithm.
Base 2 logarithm.
Base 10 logarithm.
negation
absolute value.
Answer -1 if negative, +1 if positive or 0 if zero.
next larger integer.
next smaller integer
Sine
Cosine
Tangent
Arcsine
Arccosine
Arctangent
Hyperbolic sine
Hyperbolic cosine
Hyperbolic tangent
fractional part
the square of the number
the cube of the number
the square root of the number.
e to the power of the receiver.
1 / this
this to the power of aNumber
the folded value, a bitwise or with aNumber
the number relative to this that is the previous power of aNumber
the next power of aNumber
the number relative to this that is the next power of 2
the next power of three
a hash value
the receiver. aNumber is ignored.
Bitwise And
Bitwise Or
Bitwise Exclusive Or
Binary Hamming distance: the count of bits that are not the same in the two numbers
true if bit at index aNumber is set.
ones complement
Binary shift left.
Binary shift right.
Unsigned binary shift right.
performs a binary right shift
performs an unsigned right shift
performs a binary left shift
performs a bitwise or with aNumber
performs a bitwise and with aNumber
(a * b) + a
((a*b) + a + b)
(a * a *b)
((a*a *b) - (a*b*b))
(a*a) - (b*b)
(a*a) + (b*b)
(a - b) ** 2
(a + b) ** 2
(a - b).abs
On a circle, there are two distances between two points. This operator returns the smaller value of the two.
moddif(0.75, 0, 1)
0 when b <= 0, a*b when b > 0
a * b when a < 0, otherwise a.
clips receiver to +/- aNumber
Returns the difference of the receiver and its clipped form.
(a - clip2(a,b))
this * a + b
Answer if the number is >= 0.
Answer if the number is < 0.
Answer if the number is > 0.
true, if strictly positive ( > 0), otherwise false (see Boolean)
denominator | |
fasterBetter |
if true, asFraction may find a much closer approximation and do it faster. |
an array of denominator and divisor of the nearest and smallest fraction
Converts this into an audiorate input.
Produces a time string in the format ddd:hh:mm:ss.sss
, interpreting the receiver as time in seconds. See String: -asSecs for the inverse function.
precision |
accuracy of the millisecond format; the string will always be formatted with 3 decimal places for milliseconds. Minimum value: 0.001. |
maxDays |
maximum number of days |
dropDaysIfPossible |
a Boolean. If set to |
( var start; start = Main.elapsedTime; { loop { (Main.elapsedTime - start).asTimeString.postln; 0.05.wait } }.fork; )
this as Point. x = y = this.
this as Point. x = y = this.
this as Float
a Rect with x = y = w = h = this.
this as a Boolean. this > 0
the values as Quant
this as Integer
within a routine, yield the number so that the clock can wait for this many beats. Outside a Routine, this trows an error (see also Routine for details).
Create a routine by a function fork
( fork { 1.wait; "I did wait".postln; 1.0.rand.wait; "No you didn't".postln; 2.wait; (1..).do { |i| "yes I did".postln; i.asFloat.rand.wait; "no you didn't".postln; i.wait } } )
like wait, only specify a time (measured in beats of the current thread's clock). Outside a Routine, this trows an error (see also Routine for details).
make the current thread sleep, until woken up by re-scheduling. Outside a Routine, this trows an error (see also Routine for details).
clock |
the next possible multiple of the clock's beats.
the value in the list closest to this
( l = [0, 0.5, 0.9, 1]; (0, 0.05..1).collect { |i| i.nearestInList(l) } )
scale |
an array of SimpleNumbers each treated as a step in the octave. |
stepsPerOctave |
12 by default |
the value in the collection closest to this, assuming an octave repeating table of note values.
( l = [0, 1, 5, 9, 11]; // pentatonic scale (60, 61..76).collect { |i| i.nearestInScale(l, 12) } )
return an arithmetic series from this over second to last.
This is used in the shortcuts:
(0..100); (1, 3 .. 17)
If second is nil, it is one magnitude step towards last (1 or -1). Examples:
series(5, 7, 10); series(5, nil, 10); (5, 7 .. 10)
a Routine that iterates over the numbers from this to last.
Since this is a lazy operation, last may be inf, generating an endless series (see also List Comprehensions)
r = seriesIter(0, 5); r.nextN(8); r.nextN(8);
a value for a rectangular window function between 0 and 1.
a value for a hanning window function between 0 and 1.
a value for a welsh window function between 0 and 1.
a value for a triangle window function between 0 and 1.
a nonlinear distortion function.
Distortion with a perfectly linear region from -0.5 to +0.5
Map receiver in the onto an S-curve.
((0..100) / 100 ).collect(_.scurve).plot
Map receiver onto a ramp starting at 0.
((-100..100) / 100 ).collect(_.ramp).plot
scale |
an array of SimpleNumbers each treated as a step in the octave. |
stepsPerOctave |
12 is the standard chromatic scale. |
the value is truncated to an integer and used as an index into an octave repeating table of note values. Indices wrap around the table and shift octaves as they do.
( l = [0, 1, 5, 9, 11]; // pentatonic scale (1, 2..15).collect{|i| i.degreeToKey(l, 12) }; )
inverse of degreeToKey.
scale |
an array of SimpleNumbers each treated as a step in the octave. |
stepsPerOctave |
12 is the standard chromatic scale. |
( l = [0, 1, 5, 9, 11]; // pentatonic scale (60, 61..75).collect { |i| i.keyToDegree(l, 12) } )
( l = [0, 1, 5, 9, 11]; // pentatonic scale (60, 61..75).postln.collect { |i| i.keyToDegree(l, 12).degreeToKey(l) } )
map the receiver onto a gauss function.
Uses the formula:
a * (exp(squared(this - b) / (-2.0 * squared(c)))) Default values: a = 1; b = 0; c = 1
Example code
(0..1000).normalize(-10, 10).collect { |num| num.gaussCurve }.plot;
that |
the number to compare with within precision |
precision |
The absolute precision, independent of the value compared |
relativePrecision |
The precision relative to the larger absolute of the values compared. |
true if receiver is closer to that than precision.
3.1.equalWithPrecision(3.0, 0.05); // false 3.1.equalWithPrecision(3.0, 0.1); // false 3.1.equalWithPrecision(3.0, 0.11); // true 3000.1.equalWithPrecision(3000.0, 0, 0.01); // true 3.1.equalWithPrecision(3.0, 0, 0.01); // false
Deprecated. Round the receiver to the quantum. If you're looking for MIDI quantization type features use SimpleNumber#-softRound
quantum |
amount. |
tolerance |
allowed tolerance. |
strength |
Determines how much the value is allowed to differ in the tolerance range. |
((0..10) / 10).collect { |num| num.quantize(1, 0.3, 0.5) }.postcs.plot; ((0..10) / 10).collect { |num| num.quantize(1, 0.6, 0.5) }.postcs.plot; ((0..10) / 10).collect { |num| num.quantize(1, 1.0, 0.5) }.postcs.plot;
map the receiver from an assumed linear input range to a linear output range. If the input exceeds the assumed input range, the behaviour is specified by the clip argument.
inMin |
assumed input minimum |
inMax |
assumed input maximum |
outMin |
output minimum |
outMax |
output maximum |
clip |
nil (don't clip) \max (clip ceiling) \min (clip floor) \minmax (clip both - this is default). |
(0..10).collect { |num| num.linlin(0, 10, -4.3, 100) }; (0..10).linlin(0, 10, -4.3, 100); // equivalent.
map the receiver from an assumed linear input range (inMin..inMax) to an exponential output range (outMin..outMax). The output range must not include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
inMin |
assumed input minimum |
inMax |
assumed input maximum |
outMin |
output minimum |
outMax |
output maximum |
clip |
nil (don't clip) \max (clip ceiling) \min (clip floor) \minmax (clip both - this is default). |
(0..10).collect { |num| num.linexp(0, 10, 4.3, 100) }; (0..10).linexp(0, 10, 4.3, 100); // equivalent.
map the receiver from an assumed exponential input range (inMin..inMax) to a linear output range (outMin..outMax). If the input exceeds the assumed input range. The input range must not include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
inMin |
assumed input minimum |
inMax |
assumed input maximum |
outMin |
output minimum |
outMax |
output maximum |
clip |
nil (don't clip) \max (clip ceiling) \min (clip floor) \minmax (clip both - this is default). |
(1..10).collect { |num| num.explin(0.1, 10, -4.3, 100) }; (1..10).explin(0.1, 10, -4.3, 100); // equivalent.
map the receiver from an assumed exponential input range (inMin..inMax) to an exponential output range (outMin..outMax). If the input exceeds the assumed input range. Both input range and output range must not include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
inMin |
assumed input minimum |
inMax |
assumed input maximum |
outMin |
output minimum |
outMax |
output maximum |
clip |
nil (don't clip) \max (clip ceiling) \min (clip floor) \minmax (clip both - this is default). |
(1..10).collect { |num| num.expexp(0.1, 10, 4.3, 100) }; (1..10).expexp(0.1, 10, 4.3, 100); // equivalent.
map the receiver from an assumed linear input range (inMin..inMax) to an exponential curve output range (outMin..outMax). A curve is like the curve parameter in Env. Unlike with linexp, the output range may include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
inMin |
assumed input minimum |
inMax |
assumed input maximum |
outMin |
output minimum |
outMax |
output maximum |
curve |
0 (linear) <0 (concave, negatively curved) >0 (convex, positively curved) |
clip |
nil (don't clip) \max (clip ceiling) \min (clip floor) \minmax (clip both - this is default). |
(0..10).collect { |num| num.lincurve(0, 10, -4.3, 100, -3) }; (0..10).lincurve(0, 10, -4.3, 100, -3); // equivalent.
// different curves: (-4..4).do { |val| (0..100).collect(_.lincurve(0, 100, 0, 1, val)).plot }
map the receiver from an assumed curve-exponential input range (inMin..inMax) to a linear output range (outMin..outMax). If the input exceeds the assumed input range. A curve is like the curve parameter in Env. Unlike with explin, the input range may include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
inMin |
assumed input minimum |
inMax |
assumed input maximum |
outMin |
output minimum |
outMax |
output maximum |
curve |
0 (linear) <0 (concave, negatively curved) >0 (convex, positively curved) |
clip |
nil (don't clip) \max (clip ceiling) \min (clip floor) \minmax (clip both - this is default). |
(1..10).collect { |num| num.curvelin(0, 10, -4.3, 100, -3) }; (1..10).curvelin(0, 10, -4.3, 100, -3); // equivalent.
// different curves: (-4..4).do { |val| (0..100).collect(_.curvelin(0, 100, 0, 1, val)).plot }
map the receiver from two assumed linear input ranges (inMin..inCenter) and (inCenter..inMax) to two linear output ranges (outMin..outCenter) and (outCenter..outMax). If the input exceeds the input range, the following behaviours are specified by the clip argument.
inCenter | |
inMin |
assumed input minimum |
inMax |
assumed input maximum |
outCenter | |
outMin |
output minimum |
outMax |
output maximum |
clip |
nil (don't clip) \max (clip ceiling) \min (clip floor) \minmax (clip both - this is default). |
var center = 0.5, ctlCenter; w = Window("bilin", Rect(100, 100, 200, 100)).front; a = Slider(w, Rect(20, 20, 150, 20)).value_(0.5); b = Slider(w, Rect(20, 45, 150, 20)).value_(0.5); b.action = { center = b.value }; a.mouseDownAction = { ctlCenter = a.value }; a.action = { b.value = a.value.bilin(ctlCenter, 0, 1, center, 0, 1); };
map the receiver from two assumed exponential input ranges (inMin..inCenter) and (inCenter..inMax) to two linear output ranges (outMin..outCenter) and (outCenter..outMax). The input range must not include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
inCenter | |
inMin |
assumed input minimum |
inMax |
assumed input maximum |
outCenter | |
outMin |
output minimum |
outMax |
output maximum |
clip |
nil (don't clip) \max (clip ceiling) \min (clip floor) \minmax (clip both - this is default). |
// doesn't properly work yet. ( var center = 0.5, ctlCenter; w = Window("biexp", Rect(100, 100, 200, 100)).front; a = Slider(w, Rect(20, 20, 150, 20)).value_(0.5); b = Slider(w, Rect(20, 45, 150, 20)).value_(0.5); b.action = { center = b.value }; a.mouseDownAction = { ctlCenter = a.value + 0.05 }; a.action = { b.value = (a.value + 0.1).biexp(ctlCenter, 0.1, 1.1, center, 0, 1); }; )
map the receiver onto an L-curve.
Uses the formula
a * (m * exp(x) * rTau + 1) / (n * exp(x) * rTau + 1)
This is used for smoothing values and limiting them to a range.
(0..1000).normalize(-10, 10).collect { |num| num.lcurve }.plot;
converts degree to radian
converts radian to degree
Convert MIDI note to cycles per second
cycles per second
Convert cycles per second to MIDI note.
midi note
Convert an interval in semitones to a ratio.
a ratio
Convert a ratio to an interval in semitones.
an interval in semitones
Convert a linear amplitude to decibels.
Convert a decibels to a linear amplitude.
Convert decimal octaves to cycles per second.
Convert cycles per second to decimal octaves.
stores this on the given stream
prints this on the given stream
Let x be the receiver clipped to the range [0, 1]. With probability x, return true. With probability 1 - x, return false.
Random number from zero up to the receiver, exclusive.
a random number from -this to +this.
aNumber |
the upper limit |
adverb |
a random number in the interval ]a, b[.
If both a and b are Integer then the result will be an Integer.
a linearly distributed random number from zero to this.
Bilateral linearly distributed random number from -this to +this.
This was suggested by Larry Polansky as a poor man's gaussian.
A random number from -this to +this that is the result of summing three uniform random generators to yield a bell-like distribution.
an exponentially distributed random number in the interval ]a, b[. This is always a Float. (Note that the distribution of numbers is not exactly an exponential distribution, since that would be unbounded: we might call it a logarithmic uniform distribution.)
aNumber |
the upper limit |
adverb |
a gaussian distributed random number.
standardDeviation |
the upper limit |
Always returns a Float.
(0..1000).collect { |num| gauss(0.0, num) }.plot;
randomly partition a number into parts of at least min size.
parts |
number of parts |
min |
the minimum size |
75.partition(8, 3); 75.partition(75, 1);
Some methods to ease the development of generic ugen code.
this
false if receiver cannot be used in UGen.