Skip to content

Commit

Permalink
poc: fraction shorthands
Browse files Browse the repository at this point in the history
  • Loading branch information
felixroos committed Nov 7, 2023
1 parent 682ac25 commit f46cf02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/pattern.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Fraction from './fraction.mjs';
import Hap from './hap.mjs';
import State from './state.mjs';
import { unionWithObj } from './value.mjs';

import { char2fraction } from './util.mjs';
import { compose, removeUndefineds, flatten, id, listRange, curry, _mod, numeralArgs, parseNumeral } from './util.mjs';
import drawLine from './drawLine.mjs';
import { logger } from './logger.mjs';
Expand Down Expand Up @@ -1903,6 +1903,7 @@ export const when = register('when', function (on, func, pat) {
* "c3 eb3 g3".off(1/8, x=>x.add(7)).note()
*/
export const off = register('off', function (time_pat, func, pat) {
time_pat = char2fraction(time_pat);
return stack(pat, func(pat.late(time_pat)));
});

Expand Down
20 changes: 20 additions & 0 deletions packages/core/util.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Fraction from './fraction.mjs';

/*
util.mjs - <short description TODO>
Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/strudel/blob/main/packages/core/util.mjs>
Expand Down Expand Up @@ -274,3 +276,21 @@ export const sol2note = (n, notation = 'letters') => {
const oct = Math.floor(n / 12) - 1;
return note + oct;
};

const fractionCharacters = {
w: Fraction(1),
h: Fraction(0.5),
q: Fraction(0.25),
e: Fraction(0.125),
s: Fraction(0.0625),
t: Fraction(1 / 3),
f: Fraction(0.2),
x: Fraction(1 / 6),
};
const validFractionCharacters = Object.keys(fractionCharacters);
export const char2fraction = (maybeChar) => {
if (typeof maybeChar === 'string' && validFractionCharacters.includes(maybeChar)) {
return fractionCharacters[maybeChar];
}
return maybeChar;
};

0 comments on commit f46cf02

Please sign in to comment.