Skip to content

Instantly share code, notes, and snippets.

@adamgiese
Created October 3, 2018 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamgiese/e6c7a861667b447a70f41787b34dd6f8 to your computer and use it in GitHub Desktop.
Save adamgiese/e6c7a861667b447a70f41787b34dd6f8 to your computer and use it in GitHub Desktop.
Parsing shorthand with Ramda
import { pipe, split, map, match, head, applySpec, join, addIndex, last, add } from 'ramda';
import { Time } from 'tone';
const indexedMap = addIndex(map);
const calcDuration = pipe(split('@'), last);
const parseShorthand = pipe(
split(','),
indexedMap(
applySpec({
name: pipe(split('@'), head, match(/[A-G](b|#)?/), head),
octave: pipe(split('@'), head, match(/[0-9]/), join(''), parseInt),
duration: calcDuration,
time: (note, index, array) => array.slice(0,index).map(pipe(calcDuration, Time)).reduce(add, 0)
})
),
);
parseShorthand('C4@8n,D4@16n,Eb4@16n,F4@4n,G4@8n,A4@16n,B4@16n,C5@4n').forEach(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment