Simple generic utilities (type check, common math functions, etc.)
npm install --save @ircam/sc-utils
- atodb
- dbtoa
- decibelToLinear
- decibelToPower
- delay
- ftom
- getTime
- idGenerator
- isBrowser
- isFunction
- isNumber
- isPlainObject
- isString
- isTypedArray
- linearScale
- linearToDecibel
- mtof
- powerToDecibel
- sleep
Convert a linear gain into dB
Alias: linearToDecibel
val
number Value to convert
import { atodb } from '@ircam/sc-utils';
atodb(0);
// > 1
Returns number
Convert a dB into linear gain
Alias: decibelToLinear
val
number Value to convert
import { dbtoa } from '@ircam/sc-utils';
dbtoa(0);
// > 1
Returns number
Convert a dB into linear gain (i.e. gain)
Alias: dbtoa
val
number Value to convert
import { decibelToLinear } from '@ircam/sc-utils';
decibelToLinear(0);
// > 1
Returns number
Convert a dB into power gain
val
number Value to convert
import { decibelToPower } from '@ircam/sc-utils';
decibelToPower(0);
// > 1
Returns number
Wait for a given number of milliseconds.
See also sleep
ms
Number Number of milliseconds to wait
import { delay } from '@ircam/sc-utils';
// wait for 1 second
await delay(1000);
Returns Promise
Convert a frequency in Hz to a MIDI note
freq
number Frequency to convert
import { ftom } from '@ircam/sc-utils';
const freq = ftom(440);
// > 69
Returns 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)
Create a iterator of incrementing ids
import { idGenerator } from '@ircam/sc-utils';
const generator = idGenerator();
const id = generator.next().value
Returns Iterator
Check if the platform is a browser or a node process
import { isBrowser } from '@ircam/sc-utils';
isBrowser();
// > true|false
Returns boolean
Check if the value is a function
val
any Value to check
import { isFunction } from '@ircam/sc-utils';
isFunction(() => {});
// > true
Returns boolean
Check if the value is a number, including Infinity. If you want to excluse Infinity, check the native Number.isFinite function
val
any Value to check
import { isNumber } from '@ircam/sc-utils';
isNumber(42);
// > true
Returns boolean
Check if the value is a Plain Old Javascript Object (POJO)
val
any Value to check
import { isObject } from '@ircam/sc-utils';
isObject({ a: 1 });
// > true
Returns boolean
Check if the value is a string
val
any Value to check
import { isString } from '@ircam/sc-utils';
isString('test');
// > true
Returns boolean
Check if the value is a TypedArray
val
any Value to check
import { isTypedArray } from '@ircam/sc-utils';
isTypedArray(new Float32Array([1, 2, 3]));
// > true
Returns boolean
Create a scale function
minIn
number Minimum inputmaxIn
number Maximum inputminOut
number Minimum outputmaxOut
number Maximum outputclamp
boolean Clamp output (optional, defaultfalse
)
import { scale } from '@ircam/sc-utils';
const myScale = scale(0, 1, 50, 100);
myScale(0.5);
// > 75
Returns Function
Convert a linear gain into dB
Alias: atodb
val
number Value to convert
import { decibelToPower } from '@ircam/sc-utils';
decibelToPower(0);
// > 1
Returns number
Convert a MIDI note to frequency
midiNote
number MIDI Note to convert
import { mtof } from '@ircam/sc-utils';
const freq = mtof(69);
// > 440
Returns number
Convert a linear gain into dB
val
number Value to convert
import { decibelToPower } from '@ircam/sc-utils';
decibelToPower(0);
// > 1
Returns number
Wait for a given number of seconds.
See also delay
sec
Number Number of seconds to wait
import { sleep } from '@ircam/sc-utils';
// wait for 1 second
await sleep(1);
Returns Promise