@dsebastien/music-notation 0.10.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,3 @@
1
- /**
2
- * Blocks from a note's properties: an empty ```chord block in a chord note, or ```fretboard /
3
- * ```keyboard in a scale note, draws what the note's frontmatter says, so the body repeats nothing.
4
- * Used by Music Practice in Obsidian and by the Publish bundle with the page's properties.
5
- */
6
1
  /** Property names, as Music Practice and the Obsidian Starter Kit write them by default. */
7
2
  export interface PropertyNames {
8
3
  chordSymbol: string;
@@ -18,10 +13,24 @@ export interface PropertyNames {
18
13
  shape: string;
19
14
  fingeringRight: string;
20
15
  tags: string;
16
+ /** Strumming patterns: the pattern ("D DU UDU", or one bar per list item). */
17
+ strumPattern: string;
18
+ /** Strumming patterns: the time signature ("3/4", "6/8"). */
19
+ timeSignature: string;
20
+ /** Strumming patterns: the feel ("straight", "swing", "60%"). */
21
+ strumFeel: string;
22
+ /** The current BPM: a strumming pattern's tempo (20 to 240). */
23
+ bpmCurrent: string;
21
24
  }
22
25
  export declare const DEFAULT_PROPERTY_NAMES: PropertyNames;
23
26
  export type Instrument = 'guitar' | 'piano';
24
27
  type Properties = Record<string, unknown>;
28
+ /**
29
+ * The kind of a music note, from its type tag as Music Practice reads it: the last segment of
30
+ * `type/music/<subject>/<kind>` (or `type/music/<kind>`), lowercased: `arpeggio`, `chord`,
31
+ * `strum`. Null without such a tag.
32
+ */
33
+ export declare function kindOf(properties: Properties, names: PropertyNames): string | null;
25
34
  /** Piano when a tag has a `piano` segment (`type/music/piano/chord`), else guitar. */
26
35
  export declare function instrumentOf(properties: Properties, names: PropertyNames): Instrument;
27
36
  /**
@@ -12,7 +12,7 @@ export interface RenderOptions extends LayoutDefaults {
12
12
  /**
13
13
  * The note's properties (or a function of the block's element returning them, read at each
14
14
  * render, so a page can tell embeds from the note itself): an empty
15
- * `chord`, `fretboard` or `keyboard` block draws from them (see `blockFromProperties`).
15
+ * `chord`, `fretboard`, `keyboard` or `strum` block draws from them (see `blockFromProperties`).
16
16
  */
17
17
  properties?: Record<string, unknown> | ((el: Element) => Record<string, unknown> | null | undefined) | null;
18
18
  /** Property names, when they differ from the defaults. */
package/dist/index.js CHANGED
@@ -7880,7 +7880,11 @@ var DEFAULT_PROPERTY_NAMES = {
7880
7880
  scaleType: "scale_type",
7881
7881
  shape: "shape",
7882
7882
  fingeringRight: "fingering_rh",
7883
- tags: "tags"
7883
+ tags: "tags",
7884
+ strumPattern: "strum_pattern",
7885
+ timeSignature: "time_signature",
7886
+ strumFeel: "strum_feel",
7887
+ bpmCurrent: "bpm_current"
7884
7888
  };
7885
7889
  function text(properties, name) {
7886
7890
  const value = properties[name];
@@ -7888,10 +7892,22 @@ function text(properties, name) {
7888
7892
  const line = out?.replace(/[\r\n]+/g, " ").trim();
7889
7893
  return line ? line : undefined;
7890
7894
  }
7891
- function instrumentOf(properties, names) {
7895
+ function tagsOf(properties, names) {
7892
7896
  const tags = properties[names.tags];
7893
- const list = Array.isArray(tags) ? tags : typeof tags === "string" ? tags.split(/[\s,]+/) : [];
7894
- return list.some((t) => typeof t === "string" && /(^|\/)piano(\/|$)/i.test(t.replace(/^#/, ""))) ? "piano" : "guitar";
7897
+ return Array.isArray(tags) ? tags : typeof tags === "string" ? tags.split(/[\s,]+/) : [];
7898
+ }
7899
+ function kindOf(properties, names) {
7900
+ for (const tag of tagsOf(properties, names)) {
7901
+ if (typeof tag !== "string")
7902
+ continue;
7903
+ const m = /^type\/music\/(?:[^/\s]+\/)?([^/\s]+)$/i.exec(tag.trim().replace(/^#/, ""));
7904
+ if (m)
7905
+ return m[1].toLowerCase();
7906
+ }
7907
+ return null;
7908
+ }
7909
+ function instrumentOf(properties, names) {
7910
+ return tagsOf(properties, names).some((t) => typeof t === "string" && /(^|\/)piano(\/|$)/i.test(t.replace(/^#/, ""))) ? "piano" : "guitar";
7895
7911
  }
7896
7912
  function scaleShape(shape) {
7897
7913
  if (!shape)
@@ -7907,11 +7923,69 @@ function scaleShape(shape) {
7907
7923
  return ["shape: 3nps", ...nps[1] ? [`position: ${nps[1]}`] : []];
7908
7924
  return [];
7909
7925
  }
7926
+ function first(value) {
7927
+ if (typeof value === "string")
7928
+ return value.trim();
7929
+ if (typeof value === "number" && Number.isFinite(value))
7930
+ return String(value);
7931
+ if (Array.isArray(value))
7932
+ return first(value[0]);
7933
+ return "";
7934
+ }
7935
+ function cagedShape(value) {
7936
+ const m = /^(?:caged\s+)?([cagedCAGED])(?:\s*-?\s*shape\b.*)?$/i.exec(first(value));
7937
+ return m ? m[1].toUpperCase() : null;
7938
+ }
7939
+ function chordOf2(properties, names) {
7940
+ const value = first(properties[names.chordSymbol]);
7941
+ return value ? readChord(value)?.symbol ?? null : null;
7942
+ }
7943
+ function arpeggioSource(language, properties, names, instrument) {
7944
+ const kind = kindOf(properties, names);
7945
+ const guitar = instrument !== "piano";
7946
+ const fits = kind === "arpeggio" && language === (guitar ? "fretboard" : "keyboard") || kind === "chord" && guitar && language === "fretboard";
7947
+ if (!fits)
7948
+ return null;
7949
+ const chord = chordOf2(properties, names);
7950
+ if (!chord)
7951
+ return null;
7952
+ const shape = guitar ? cagedShape(properties[names.shape]) : null;
7953
+ return [`arpeggio: ${chord}`, ...shape ? [`shape: ${shape}`] : []].join(`
7954
+ `);
7955
+ }
7956
+ function swingOf(feel) {
7957
+ const value = first(feel).toLowerCase();
7958
+ if (/^(swing|swung|shuffle)/.test(value))
7959
+ return "yes";
7960
+ const percent = /^(\d{2})\s*%$/.exec(value);
7961
+ return percent ? `${percent[1]}%` : null;
7962
+ }
7963
+ var TIME = /^(\d{1,2}\/\d{1,2}|c|cut)$/i;
7964
+ function strumSource(properties, names) {
7965
+ const raw = properties[names.strumPattern];
7966
+ const bars = (Array.isArray(raw) ? raw : [raw]).map((line) => first(line)).flatMap((line) => line.split(/\r?\n/)).map((line) => line.trim()).filter(Boolean);
7967
+ if (!bars.length)
7968
+ return null;
7969
+ const time = first(properties[names.timeSignature]);
7970
+ const swing = swingOf(properties[names.strumFeel]);
7971
+ const chord = chordOf2(properties, names);
7972
+ const bpm = Number(first(properties[names.bpmCurrent]));
7973
+ return [
7974
+ ...bars,
7975
+ ...TIME.test(time) ? [`time: ${time}`] : [],
7976
+ ...swing ? [`swing: ${swing}`] : [],
7977
+ ...chord ? [`chord: ${chord}`] : [],
7978
+ ...Number.isInteger(bpm) && bpm >= 20 && bpm <= 240 ? [`tempo: ${bpm}`] : []
7979
+ ].join(`
7980
+ `);
7981
+ }
7910
7982
  function blockFromProperties(language, properties, options = {}) {
7911
7983
  const names = { ...DEFAULT_PROPERTY_NAMES, ...options.names };
7912
7984
  const instrument = options.instrument ?? instrumentOf(properties, names);
7913
7985
  const get = (name) => text(properties, name);
7914
7986
  const lines = [];
7987
+ if (language === "strum")
7988
+ return strumSource(properties, names);
7915
7989
  if (language === "chord") {
7916
7990
  const symbol = get(names.chordSymbol);
7917
7991
  if (symbol)
@@ -7949,6 +8023,9 @@ function blockFromProperties(language, properties, options = {}) {
7949
8023
  `);
7950
8024
  }
7951
8025
  if (language === "fretboard" || language === "keyboard") {
8026
+ const arpeggio = arpeggioSource(language, properties, names, instrument);
8027
+ if (arpeggio)
8028
+ return arpeggio;
7952
8029
  const root = get(names.scaleRoot)?.split("/")[0]?.trim();
7953
8030
  const type = get(names.scaleType);
7954
8031
  if (!root || !type)
@@ -8068,7 +8145,7 @@ function renderBlock(language, source, host, given = {}) {
8068
8145
  options.instrument = instrument;
8069
8146
  }
8070
8147
  let text = source;
8071
- if (!text.trim() && isDiagramLanguage(language)) {
8148
+ if (!text.trim() && (isDiagramLanguage(language) || language === "strum")) {
8072
8149
  const fromProperties = propertiesSource(language, options, host);
8073
8150
  if (fromProperties) {
8074
8151
  text = fromProperties;
@@ -10453,9 +10530,16 @@ function splitUnbarred(measures, meter) {
10453
10530
  if (eighths <= perBar || m.width - m.pad < span * 1.5)
10454
10531
  return [m];
10455
10532
  const count = Math.floor((cols.at(-1) - m.pad) / span) + 1;
10456
- return Array.from({ length: count }, (_, k) => {
10457
- const from = m.pad + k * span;
10458
- const beats = m.beats.filter((b) => (k === 0 || b.col >= from) && (k === count - 1 || b.col < from + span)).map((b) => ({ notes: b.notes, col: b.col - k * span }));
10533
+ const pieces = Array.from({ length: count }, () => []);
10534
+ for (const b of m.beats) {
10535
+ let k = Math.min(count - 1, Math.max(0, Math.floor((b.col - m.pad) / span)));
10536
+ while (k < count - 1 && b.col >= m.pad + (k + 1) * span)
10537
+ k++;
10538
+ while (k > 0 && b.col < m.pad + k * span)
10539
+ k--;
10540
+ pieces[k].push({ notes: b.notes, col: b.col - k * span });
10541
+ }
10542
+ return pieces.map((beats, k) => {
10459
10543
  return {
10460
10544
  beats,
10461
10545
  width: m.pad + span,
@@ -10776,5 +10860,5 @@ export {
10776
10860
  withTonicizations
10777
10861
  };
10778
10862
 
10779
- //# debugId=BC77BC5D932C7CDB64756E2164756E21
10863
+ //# debugId=2CDC3F46ACDDCF7D64756E2164756E21
10780
10864
  //# sourceMappingURL=index.js.map