tmhglnd / total-serialism

Toolbox full of Algorithmic Composition methods
MIT License
151 stars 10 forks source link

Convert duration values to ms #3

Open tmhglnd opened 4 years ago

tmhglnd commented 4 years ago

An enhancement to the Translate library of a method that converts various duration values from different platforms to a corresponding millisecond value based on a set tempo.

example

const TL = require('total-serialism').Translate;

TL.setTempo(120);
//=> set tempo to 120 beats per minute

var rhythm = [8n, 4n, 16n];
TL.toTime(rhythm)
//=> [ 250, 500, 125 ]

But could also work with division notation

const TL = require('total-serialism').Translate;

TL.setTempo(120);
//=> set tempo to 120 beats per minute

var rhythm = ['1/8', '1/4', '1/16'];
TL.toTime(rhythm)
//=> [ 250, 500, 125 ]

Or with floating point ratio values

const TL = require('total-serialism').Translate;

TL.setTempo(120);
//=> set tempo to 120 beats per minute

var rhythm = [0.125, 0.25, 0.0625];
TL.toTime(rhythm)
//=> [ 250, 500, 125 ]
tmhglnd commented 2 years ago

Not yet possible with one function that works for all, but there are now several translate methods for different types of input. Using the Time Value Syntax of Cycling'74's MaxMSP

// format: TL.func(<Array>, <bpm>)

TL.divisionToMs(['1/4', ['1/8', ['3/16', '1/4']], '1/6', '2'], 100);
//=> [ 600, [ 300, [ 450, 600 ] ], 400, 4800 ] 
// Alias: TL.dtoms()

TL.ratioToMs([0.25, [0.125, [0.1875, 0.25]], 0.1667, 2], 100);
//=> [ 600, [ 300, [ 450, 600 ] ], 400.08, 4800 ] 
// Alias: TL.rtoms()

TL.timevalueToMs(['4n', ['8nt', ['16nd', '2nd']], '32n', '3m'], 100);
//=> [ 600, [ 200, [ 225, 1800 ] ], 75, 7200 ] 
// Alias. TL.ttoms()