Home

Awesome

dilla-expressions

npm npm GitHub stars GitHub forks

Travis branch Code Climate Code Climate David dependencies David devDependencies

expand expressions for repeating notes into flat positions

Built for and used in dilla and bap, by Adam Renklint

Install

$ npm install --save dilla-expressions

Usage

var expr = require('dilla-expressions');

var notes = [
  ['*.even.01']
];

var expanded = expr(notes, {
  'barsPerLoop': 2,
  'beatsPerBar': 4
});

expect(expanded.length).to.equal(4);
expect(expanded[0][0]).to.equal('1.2.01');
expect(expanded[1][0]).to.equal('1.4.01');
expect(expanded[2][0]).to.equal('2.2.01');
expect(expanded[3][0]).to.equal('2.4.01');

Operators

Wildcard

> "*.*.32"
= "1.1.32", "1.2.32", "1.3.32", "1.4.32", "2.1.32", "2.2.32", "2.3.32", "2.4.32"

Odd/even

> "1.even.01"
= "1.2.01", "1.4.01"

> "1.odd.01"
= "1.1.01", "1.1.01"

Modulus

> "1.%3.%30"
= "1.1.01", "1.1.31", "1.1.61", "1.1.91", "1.4.01", "1.4.31", "1.4.61", "1.4.91"

> "1.1.5%20"
= "1.1.05", "1.1.25", "1.1.45", "1.1.65", "1.1.85"

Greater than

> "1.>2.01"
= "1.3.01", "1.4.01"

Less than

> "1.<4.01"
= "1.1.01", "1.2.01", "1.3.01"

Custom expander

It is possible to extend dilla-expressions with your own expression expander.

The position is split into three fragments. An expander function will be called for each fragment of the position expression that is not a simple number or already expanded by a previous expander, i.e. wildcard, modulus, etc...

Start and end represent the start and endpoints for the current fragment, and would most times be the same as barsPerLoop, but could be another value if the fragment is combined with the greater than or less than operators.

var expanded = expr([
  ['foo.bar.baz']
], {
  'barsPerLoop': 2,
  'beatsPerBar': 4,
  'expander': function (fragment, start, end) {
    if (fragment === 'foo') { return [1, 2]; }
    else if (fragment === 'bar') { return [end/2]; }
    else if (fragment === 'baz') { return [15]; }
  }
});

expect(expanded.length).to.equal(2);
expect(expanded[0][0]).to.equal('1.2.15');
expect(expanded[1][0]).to.equal('2.2.15');

Develop

Changelog

License

MIT © Adam Renklint