API Documentation

Use the Notations library programmatically

The Notations library provides a programmatic API for parsing, modeling, and rendering Carnatic music notation. This guide shows you how to use the library in your JavaScript or TypeScript applications.

Basic Usage

Import the library and use the load function to parse notation:

import * as N from "notations";

const source = `
\cycle("|4|2|2|")
\beatDuration(4)
Sw: S R G M P D N S.
Sh: sa ri ga ma pa dha ni sa
`;

const [notation, beatLayout, errors] = N.load(source);

if (errors.length === 0) {
  console.log("Notation loaded successfully");
  console.log("Notation:", notation);
  console.log("Beat Layout:", beatLayout);
} else {
  console.error("Errors:", errors);
}

Return Values

The N.load() function returns a tuple with three elements:

  1. notation - The parsed notation object containing all commands, roles, and atoms
  2. beatLayout - Layout information for rendering the notation
  3. errors - Array of parsing errors (empty if successful)

Working with Notation Objects

The notation object contains structured data about your composition:

const [notation, beatLayout, errors] = N.load(source);

// Access metadata
console.log("Current cycle:", notation.currentCycle);
console.log("Current APB:", notation.currentAPB);

// Access roles
notation.roles.forEach(role => {
  console.log("Role:", role.name);
});

// Access blocks and lines
notation.blocks.forEach(block => {
  block.lines.forEach(line => {
    console.log("Line:", line);
  });
});

Error Handling

Always check for parsing errors before using the notation:

const [notation, beatLayout, errors] = N.load(source);

if (errors.length > 0) {
  errors.forEach(error => {
    console.error("Error:", error.message);
    console.error("Line:", error.line);
    console.error("Column:", error.column);
  });
  return;
}

// Safe to use notation here
processNotation(notation, beatLayout);

Using the Parser Directly

For more control, use the Parser class. Give it the gamakas to read: the parser knows the ~ syntax but not what any gamaka means. load() and parse() pass the Carnatic ones for you.

import { Parser, carnaticGamakas } from "notations";

const parser = new Parser({ embellishments: carnaticGamakas });
const result = parser.parse(source);

console.log("Commands:", parser.commands);
console.log("Metadata:", parser.metadata);
console.log("Errors:", parser.errors);

Parsing Without the Renderer

The notations entry includes the SVG renderer. Two smaller entries leave it out:

  • notations/core - The runtime model: atoms, groups, cycles, notations, AtomIterator and the beat layout. No parser, no renderer, no DOM. About 8 KB gzipped.
  • notations/parser - The DSL parser, load() and parse() with the Carnatic gamakas wired in, and carnaticGamakas itself. It builds the model from notations/core.
import { load } from "notations/parser";
import { AtomIterator } from "notations/core";

const [notation, , errors] = load(source);

From the notation back to the source

Every parsed atom knows its place in the source: atom.sourceRange is [start, end), counted from the start of the whole source, front matter included. atomAt(notation, offset) finds the atom under a caret, so an editor can show which note the caret is on.

On a rendered view, view.elementsFor(atom) gives what is drawn for an atom, and view.atomFor(element) gives the atom under a click. view.mark(atom, "playing") adds the class mark-playing, and view.unmark("playing") clears it. A player or an editor calls these from its own events. notations runs no clock.

import { atomAt } from "notations";

const atom = atomAt(notation, editor.selectionStart);
view.unmark("cursor");
if (atom) view.mark(atom, "cursor");

Printing

notations/export prints a notation on its own, sized to the page. printNotation(source) renders the source again into a print-only copy and opens the print dialog, where "Save as PDF" gives a PDF. Every line scales by the same factor, so the widest line fills the page. It needs a browser, and it doesn't pull in the editor or the playgrounds from notations/web.

import { printNotation } from "notations/export";

printNotation(source);

Exporting SVG and PNG

exportSVG(source) returns the notation as one standalone SVG string. It is always in the light theme, on white unless you pass background: null, and it needs none of the page's CSS. Margin labels, headings and front matter become SVG text, keeping bold and italic. Other markdown formatting, such as links and tables, comes out as plain text. exportPNG(source) draws that SVG onto a canvas at twice its size and returns a PNG Blob. downloadSVG and downloadPNG save either as a file.

import { downloadSVG, exportPNG } from "notations/export";

downloadSVG(source, "kriti.svg");
const png = await exportPNG(source, { scale: 3 });

Rendering

The library provides rendering capabilities through the NotationView:

import * as N from "notations";

const [notation, beatLayout, errors] = N.load(source);

if (errors.length === 0) {
  const container = document.getElementById("notation-container");
  const view = new N.Carnatic.NotationView(container);
  view.renderNotation(notation, beatLayout);
}

TypeScript Support

The library includes full TypeScript type definitions:

import * as N from "notations";
import { Notation, GlobalBeatLayout, ParseError } from "notations";

function loadNotation(source: string): [Notation, GlobalBeatLayout, ParseError[]] {
  return N.load(source);
}

Web Components

For easy integration in web applications, use the separate web-components package:

npm install notations-web notations
import { NotationBlock } from "notations-web";
import * as NV from "./NotationViewer";

const container = document.getElementById("notation-container");
const config = {
  createViewer: NV.createViewer,
  cssClasses: {
    sourceContainer: "bg-white",
    outputContainer: "p-4"
  }
};

const block = new NotationBlock(container, config);

See the Integration Guide for detailed setup instructions.

Core Concepts

  • Notation - Top-level object containing the entire composition
  • Block - A section of notation with multiple lines
  • Line - A horizontal line of notation containing multiple roles
  • Role - A type of content (Sw for swaras, Sh for sahitya)
  • Atom - Basic unit (Note, Space, Rest, Group)
  • GlobalBeatLayout - Layout information for rendering

See Also