abcjs

repository·main·Indexed 25 days ago

https://github.com/paulrosen/abcjs

A JavaScript library for rendering standard ABC music notation in web browsers. It supports sheet music visualization as SVG, MIDI generation, and audio playback. Key features include the renderAbc function, a Synth API for audio synthesis, interactive editing via Editor and EditArea classes, and support for string tablature and jazz-style chord grids.

Tokens
1.6K
Snippets
5
Records
18
Agent score
81%

What's inside abcjs

  1. Overview of abcjs

    main
    abcjs is a JavaScript library designed for rendering standard music notation (ABC notation) directly in a web browser. It allows developers to incorporate sheet music into websites, generate MIDI files, or play music directly in the browser. It can be used as a standard library integration or via a Greasemonkey script to transform ABC text on existing websites.
  2. Understanding Absolute and Relative Elements

    main

    In the abcjs engraving model, musical notation is composed of two types of objects that work together:

    • Absolute Element: A visual piece of music that moves as a single unit (e.g., a key signature containing multiple sharp symbols). It has an X and Y position in the SVG and contains an array of Relative Elements.
    • Relative Element: Glyphs that are attached to an Absolute Element. They use relative positioning to stay attached to their parent (e.g., a trill, a flat sign, a stem, or a dot attached to a note head).
  3. How the abcjs engraver processes music notation

    main

    The abcjs engraver takes the output from the parser and processes it through three main stages to transform musical data into a visual, interactive SVG:

    1. Element Creation: The parser's output structure is traversed to create musical elements.
    2. Layout: The engine runs separate algorithms for horizontal and vertical positioning once element sizes are known.
    3. Drawing: The engine traverses the structure one final time to render each element into the SVG based on the calculated layout.
  4. Configure midi.js as a peerDependency

    main

    Starting from version 6.0.0-beta.26, midi.js is no longer a direct dependency but a peerDependency. If you are using the old style of sound generation that requires midi.js, you must include it in your package.json:

    "midi": "https://github.com/paulrosen/MIDI.js.git#abcjs"

    Note: This requirement does not apply if you are using the minified version of the library via a <script> tag.

  5. Use chordGrid for jazz-style chord views

    main

    In version 6.6.0 and later, you can use the chordGrid option within the renderAbc parameters to display chords in a layout similar to traditional jazz lead sheets.

    Available options:

    • chordGrid: "noMusic": Displays only the chords.
    • chordGrid: "withMusic": Prints the chords above the standard music notation.
  6. Configure the default soundfont

    main

    abcjs uses a default soundfont for audio playback. If you need to use a specific soundfont (for example, to revert to the older FluidR3_GM style), use the soundFontUrl option when calling the synth functions.

    To use the older soundfont, set: soundFontUrl: "https://paulrosen.github.io/midi-js-soundfonts/FluidR3_GM/"

  7. Run abcjs using Docker Compose

    main

    You can run the abcjs application using Docker Compose. The setup builds the image from the local directory and maps the local source code and node_modules into the container to allow for development.

    Note that the host port for the application is determined by the environment variable $abcjs_docs.

    services:
      app:
        image: abcjs
        build: .
        container_name: abcjs
        volumes:
          - .:/srv/app
          - ./node_modules:/srv/app/node_modules
        ports:
          - $abcjs_docs:8080
  8. Style the audio control warning

    main

    If you are using abcjs-audio.css and want to hide the CSS warning element when styling the audio control with your own custom CSS, add the following rule:

    .abcjs-css-warning {
    	display: none;
    }
  9. Create MIDI files from ABC notation

    main

    Use the create function to convert an ABC tune object into MIDI data. This function processes the ABC notation, extracts metadata (title, key, meter), and translates musical events (notes, text, instrument programs) into a MIDI format.

    Parameters

    • abcTune: An object representing the ABC tune, which must implement methods like setUpAudio(options), getKeySignature(), getMeterFraction(), and millisecondsPerMeasure().
    • options (optional): An object to configure the MIDI creation process.

    Options

    • options.pan: An array of panning values (integers) corresponding to each track in the ABC tune. If provided, options.pan[i] sets the pan for track i.

    Return Value

    Returns the raw MIDI data (typically a Uint8Array or similar buffer) generated from the tune.