Simple generic utilities (type check, common math functions, etc.)
npm install --save @ircam/sc-utils
- almostEqual
- almostEqualArray
- atodb
- counter
- dbtoa
- decibelToLinear
- decibelToPower
- delay
- exponentialScale
- frequencyToMidi
- ftom
- getTime
- hertzToNormalised
- idGenerator
- isBrowser
- isDefined
- isFunction
- isNumber
- isPlainObject
- isSequence
- isString
- isTouchDevice
- isTypedArray
- isURL
- linearScale
- linearToDecibel
- logarithmicScale
- midiToFrequency
- modulo
- mtof
- normalisedToHertz
- normalizedToTableScale
- powerToDecibel
- sleep
- tableToNormalizedScale
Checks if two numeric values are approximately equal within a given tolerance.
valuenumber The first value to compare.referencenumber The second value to compare.tolerancenumber The tolerance within which the values are considered equal. Note: tolerance must take into account the sum of all relative and absolute errors. (optional, defaultNumber.EPSILON)
import { almostEqual } from '@ircam/sc-utils';
almostEqual(0.1 + 0.2, 0.3); // true
almostEqual(0.1 + 0.2, 1e-18); // false
almostEqual(0.1, 0.11, 0.02); // trueReturns boolean Returns true if the values are approximately
equal, otherwise false.
- See: almostEqual
Checks if two arrays of numeric values are approximately equal element-wise within a given tolerance.
valueArray<number> The first array to compare.referenceArray<number> The second array to compare.tolerancenumber The tolerance within which the values are considered equal. Note: tolerance must take into account the sum of all relative and absolute errors. (optional, defaultNumber.EPSILON)
import { almostEqualArray } from '@ircam/sc-utils';
almostEqualArray([0.1, 0.1 + 0.2], [0.1, 0.3]); // trueReturns boolean Returns true if the arrays have got the same size, and
if every value of the same index are approximately equal. Otherwise false.
Convert a linear gain into dB
Alias: linearToDecibel
valnumber Value to convert
import { atodb } from '@ircam/sc-utils';
atodb(0);
// > 1Returns number
Create a counter function.
fromnumber Start of the counter, included (optional, default0)tonumber End of the counter, included (optional, defaultNumber.MAX_SAFE_INTEGER)stepnumber Increment / decrement step, if 0 returnsfromforever (optional, default1)
import { counter } from '@ircam/sc-utils';
const myCounter = counter(0.1, 0.3, 0.1);
counter(); // 0.1
counter(); // 0.2
counter(); // 0.3
counter(); // 0.1Returns Function
Convert a dB into linear gain
Alias: decibelToLinear
valnumber Value to convert
import { dbtoa } from '@ircam/sc-utils';
dbtoa(0);
// > 1Returns number
Convert a dB into linear gain (i.e. gain)
Alias: dbtoa
valnumber Value to convert
import { decibelToLinear } from '@ircam/sc-utils';
decibelToLinear(0);
// > 1Returns number
Convert a dB into power gain
valnumber Value to convert
import { decibelToPower } from '@ircam/sc-utils';
decibelToPower(0);
// > 1Returns number
Wait for a given number of milliseconds.
See also sleep
msNumber Number of milliseconds to wait
import { delay } from '@ircam/sc-utils';
// wait for 1 second
await delay(1000);Returns Promise
Create an exponential scale function.
inputStartnumber Start value of input rangeinputEndnumber End value of input rangeoutputStartnumber Start value of output rangeoutputEndnumber End value of output rangebasenumber Base value for exponential scaling, default to2(optional, default2)clipboolean Clip output to output range, default tofalse(optional, defaultfalse)
const { exponentialScale } = utils;
const midiToFreq = exponentialScale(69, 81, 440, 880);
midiToFreq(57);
// > 220Convert a frequency in Hz to a MIDI note
freqnumber Frequency to convert
import { frequencyToMidi } from '@ircam/sc-utils';
const freq = frequencyToMidi(440);
// > 69Returns number
Convert a frequency in Hz to a MIDI note
Alias: frequencyToMidi
freqnumber Frequency to convert
import { ftom } from '@ircam/sc-utils';
const freq = ftom(440);
// > 69Returns number
Provide a unified clock in seconds accross platforms, with an origin defined by the start of the process.
import { getTime } from '@ircam/sc-utils';
setInterval(() => {
const now = getTime();
// ...
}, 1000);Returns number
Convert a frequency in Hertz to a normalised one in [0, 1].
Normalised frequency of 1 is half the sample-rate (Nyquist frequency).
-
frequencyHertznumber Frequency in Hertz to convert -
sampleRatenumber Twice the Nyquist frequency (optional, default{})sampleRate.sampleRate(optional, default2)
import { hertzToNormalised } from '@ircam/sc-utils';
hertzToNormalised(12000, {sampleRate: 48000});
// > 0.5Returns number
Create a iterator of incrementing ids
DEPRECATED Use the more generic and user friendly counter instead.
import { idGenerator } from '@ircam/sc-utils';
const generator = idGenerator();
const id = generator.next().valueReturns Iterator
Check if the platform is a browser or a node process
import { isBrowser } from '@ircam/sc-utils';
isBrowser();
// > true|falseReturns boolean
Check if the value is defined.
valany Value to check
import { isDefined } from '@ircam/sc-utils';
isDefined(42); // true
isDefined(undefined); // false
isDefined(); // false
isDefined(null); // true
isDefined(NaN); // true
isDefined(0); // trueReturns boolean
Check if the value is a function
valany Value to check
import { isFunction } from '@ircam/sc-utils';
isFunction(() => {});
// > trueReturns boolean
Check if the value is a number, including Infinity. If you want to excluse Infinity, check the native Number.isFinite function
valany Value to check
import { isNumber } from '@ircam/sc-utils';
isNumber(42);
// > trueReturns boolean
Check if the value is a Plain Old Javascript Object (POJO)
valany Value to check
import { isPlainObject } from '@ircam/sc-utils';
isPlainObject({ a: 1 });
// > trueReturns boolean
Check if the value is a sequence (Array or TypedArray) of finite numbers
valany Value to check
import { isSequence } from '@ircam/sc-utils';
isSequence([1, 2, 3]);
// > trueReturns boolean
Check if the value is a string
valany Value to check
import { isString } from '@ircam/sc-utils';
isString('test');
// > trueReturns boolean
Check if the device supports touch events
import { isTouchDevice } from '@ircam/sc-utils';
isTouchDevice();
// > true|falseReturns boolean
Check if the value is a TypedArray
valany Value to check
import { isTypedArray } from '@ircam/sc-utils';
isTypedArray(new Float32Array([1, 2, 3]));
// > trueReturns boolean
Check if the value is a valid URL
urlvalany Value to check
import { isURL } from '@ircam/sc-utils';
isURL('http://sub.my-site.org/abcd?test=123');
// > trueReturns boolean
Create a linear scale function.
inputStartnumber Start value of input rangeinputEndnumber End value of input rangeoutputStartnumber Start value of output rangeoutputEndnumber End value of output rangeclipboolean Clip output to output range, default tofalse(optional, defaultfalse)
import { linearScale } from '@ircam/sc-utils';
const myScale = linearScale(0, 1, 50, 100);
myScale(0.5);
// > 75Returns Function
Convert a linear gain into dB
Alias: atodb
valnumber Value to convert
import { decibelToPower } from '@ircam/sc-utils';
decibelToPower(0);
// > 1Returns number
Create a logarithmic scale function.
inputStartnumber Start value of input rangeinputEndnumber End value of input rangeoutputStartnumber Start value of output rangeoutputEndnumber End value of output rangebasenumber Base value for logarithmic scaling, default to2(optional, default2)clipboolean Clip output to output range, default tofalse(optional, defaultfalse)
const { logarithmicScale } = utils;
const freqToMidi = logarithmicScale(440, 880, 69, 81);
freqToMidi(220);
// > 57Convert a MIDI note to frequency
midiNotenumber MIDI Note to convert
import { midiToFrequency } from '@ircam/sc-utils';
const freq = midiToFrequency(69);
// > 440Returns number
Calculates the modulo operation with an optional offset.
valuenumber The value to apply the modulo operation to.modulusnumber The modulus divisor.offsetnumber Optional offset to apply before and after the modulo operation. (optional, default0)
modulo(-1, 360); // returns 359
modulo(1, 360); // returns 1
modulo(-1, -360); // returns -1
modulo(1, -360); // returns -359modulo(-1, 360, -180); // returns -1
modulo(1, 360, -180); // returns 1
modulo(-1, -360, 180); // returns -1
modulo(1, -360, 180); // returns 1Returns number The result of the modulo operation adjusted by the offset. without offset: result in [0, modulus] for modulus > 0 result in [modulus, 0] for modulus < 0with offset: result in [offset, offset + modulus] for modulus > 0 result in [modulus + offset, offset] for modulus < 0
Convert a MIDI note to frequency
Alias: midiToFrequency
midiNotenumber MIDI Note to convert
import { mtof } from '@ircam/sc-utils';
const freq = mtof(69);
// > 440Returns number
Convert a normalised frequency, in [0, 1], to a frequency in Hertz.
Normalised frequency of 1 is half the sample-rate (Nyquist frequency).
-
frequencyNormalisednumber Normalised frequency to convert -
sampleRatenumber Twice the Nyquist frequency (optional, default{})sampleRate.sampleRate(optional, default2)
import { normalisedToHertz } from '@ircam/sc-utils';
normalisedToHertz(0.5, {sampleRate: 48000});
// > 12000Returns number
Create a scale function that returns a linearly interpolated value from the given transfert table according to the given normalized position.
import { normalizedToTableScale } from '@ircam/sc-utils'
const scale = normalizedToTableScale([1, 2, 4])
scale(0); // 1
scale(0.25); // 1.5
scale(0.5); // 2
scale(0.75); // 3
scale(1); // 4Returns function
Convert a linear gain into dB
valnumber Value to convert
import { decibelToPower } from '@ircam/sc-utils';
decibelToPower(0);
// > 1Returns number
Wait for a given number of seconds.
See also delay
secNumber Number of seconds to wait
import { sleep } from '@ircam/sc-utils';
// wait for 1 second
await sleep(1);Returns Promise
Create a scale function that returns a normalized position in the transfert table according to the given value.
import { tableToNormalized } from '@ircam/sc-utils'
const scale = tableToNormalized([1, 2, 4])
scale(1); // 0
scale(1.5); // 0.25
scale(2); // 0.5
scale(3); // 0.75
scale(4); // 1Returns function