@tonaljs/range
Version:
Create (musical) note ranges
1 lines • 2.73 kB
Source Map (JSON)
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { compact, range } from \"@tonaljs/collection\";\nimport { midiToNoteName, toMidi, ToNoteNameOptions } from \"@tonaljs/midi\";\n\n/**\n * Create a numeric range. You supply a list of notes or numbers and it will\n * be connected to create complex ranges.\n *\n * @param {Array} notes - the list of notes or midi numbers used\n * @return {Array} an array of numbers or empty array if not valid parameters\n *\n * @example\n * numeric([\"C5\", \"C4\"]) // => [ 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60 ]\n * // it works midi notes\n * numeric([10, 5]) // => [ 10, 9, 8, 7, 6, 5 ]\n * // complex range\n * numeric([\"C4\", \"E4\", \"Bb3\"]) // => [60, 61, 62, 63, 64, 63, 62, 61, 60, 59, 58]\n */\nexport function numeric(notes: (string | number)[]): number[] {\n const midi: number[] = compact(\n notes.map((note) => (typeof note === \"number\" ? note : toMidi(note))),\n );\n if (!notes.length || midi.length !== notes.length) {\n // there is no valid notes\n return [];\n }\n\n return midi.reduce(\n (result, note) => {\n const last: number = result[result.length - 1];\n return result.concat(range(last, note).slice(1));\n },\n [midi[0]],\n );\n}\n\n/**\n * Create a range of chromatic notes. The altered notes will use flats.\n *\n * @function\n * @param {Array} notes - the list of notes or midi note numbers to create a range from\n * @param {Object} options - The same as `midiToNoteName` (`{ sharps: boolean, pitchClass: boolean }`)\n * @return {Array} an array of note names\n *\n * @example\n * Range.chromatic([\"C2, \"E2\", \"D2\"]) // => [\"C2\", \"Db2\", \"D2\", \"Eb2\", \"E2\", \"Eb2\", \"D2\"]\n * // with sharps\n * Range.chromatic([\"C2\", \"C3\"], { sharps: true }) // => [ \"C2\", \"C#2\", \"D2\", \"D#2\", \"E2\", \"F2\", \"F#2\", \"G2\", \"G#2\", \"A2\", \"A#2\", \"B2\", \"C3\" ]\n */\nexport function chromatic(\n notes: (string | number)[],\n options?: ToNoteNameOptions,\n): string[] {\n return numeric(notes).map((midi) => midiToNoteName(midi, options));\n}\n\n/** @deprecated */\nexport default { numeric, chromatic };\n"],"mappings":";AAAA,SAAS,SAAS,aAAa;AAC/B,SAAS,gBAAgB,cAAiC;AAgBnD,SAAS,QAAQ,OAAsC;AAC5D,QAAM,OAAiB;AAAA,IACrB,MAAM,IAAI,CAAC,SAAU,OAAO,SAAS,WAAW,OAAO,OAAO,IAAI,CAAE;AAAA,EACtE;AACA,MAAI,CAAC,MAAM,UAAU,KAAK,WAAW,MAAM,QAAQ;AAEjD,WAAO,CAAC;AAAA,EACV;AAEA,SAAO,KAAK;AAAA,IACV,CAAC,QAAQ,SAAS;AAChB,YAAM,OAAe,OAAO,OAAO,SAAS,CAAC;AAC7C,aAAO,OAAO,OAAO,MAAM,MAAM,IAAI,EAAE,MAAM,CAAC,CAAC;AAAA,IACjD;AAAA,IACA,CAAC,KAAK,CAAC,CAAC;AAAA,EACV;AACF;AAeO,SAAS,UACd,OACA,SACU;AACV,SAAO,QAAQ,KAAK,EAAE,IAAI,CAAC,SAAS,eAAe,MAAM,OAAO,CAAC;AACnE;AAGA,IAAO,gBAAQ,EAAE,SAAS,UAAU;","names":[]}