music21j

repository·master·Indexed 41 days ago

https://github.com/cuthbertlab/music21j

A JavaScript reinterpretation of the Python music21 package, providing a toolkit for computer-aided musicology. Version 0.23.7 offers an interactive framework for musical analysis, MIDI interaction, and notation visualization via Vexflow in the browser.

Tokens
2.6K
Snippets
11
Records
12
Agent score
1%

What's inside music21j

  1. Set up a local development environment

    master

    To develop on the music21j repository using Vite:

    1. Install dependencies:
      npm install
      npx playwright install chromium
    2. Start the development server:
      npm run dev
    3. Navigate to http://localhost:5173/testHTML/ to view demos.
    $ npm install
    $ npx playwright install chromium
    $ npm run dev
  2. Run music21j tests

    master

    music21j tests run in a real browser using QUnit + Playwright, orchestrated via Vite. This allows tests to render SVG output and exercise audio-related APIs.

    Headless Execution

    To run the full test suite headlessly, use npm test. This starts a Vite development server and runs QUnit tests in headless Chromium.

    Note: The first time you run tests, you must install the headless Chromium browser:

    npx playwright install chromium

    Running with an active Vite server

    If you already have the Vite development server running, you can run tests using:

    npm run test:qunit

    You can also view tests and their output by navigating to http://localhost:5173/tests/ in your browser.

    # Install headless browser (required once)
    npx playwright install chromium
    
    # Run full test suite headlessly
    $ npm test
    
    # Run tests if Vite server is already running
    $ npm run test:qunit
  3. Update music21j dependencies

    master

    To keep dependencies up to date, it is recommended to run npm audit and npm audit fix frequently. To check for major version updates across all dependencies, use npm-check-updates (or ncu).

    # Check for updates
    $ npx npm-check-updates
    
    # Apply updates and install
    $ npx npm-check-updates -u
    $ npm install
  4. Filter music21j tests by module or name

    master

    You can narrow down the test execution by using the MODULE and FILTER environment variables with npm test or npm run test:qunit.

    • MODULE: Matches a single suite (a file in tests/moduleTests/).
    • FILTER: A substring or regex that matches against specific test names.

    Alternatively, if viewing tests via the browser at http://localhost:5173/tests/, you can use these as URL query parameters.

    # Run only the 'key' suite headlessly
    $ MODULE=key npm test
    
    # Run every test whose name contains 'update'
    $ FILTER=update npm test
    
    # Combine: only 'update' tests inside the 'key' suite
    $ MODULE=key FILTER=update npm test
  5. Build music21j for browser use

    master

    To create a production build, run npm run build. This uses Vite to produce:

    • build/music21.debug.js: A UMD bundle that exposes a global music21 object.
    • Various sourcemaps.

    The output is suitable for direct browser inclusion or npm publishing.

    $ npm run build
  6. Load soundfonts from an external CDN

    master

    By default, music21j attempts to fetch its own soundfonts. To use soundfonts from an external source (like a CDN) and avoid the default fetch, follow these steps:

    1. Set window.m21conf = { loadSoundfont: false } before loading the music21j script.
    2. Load the music21j script.
    3. Set music21.common.urls.soundfontUrl to your desired URL.
    4. Call music21.miditools.loadSoundfont(instrumentName, callback) to load the specific instrument.
    <body
    <script>
        window.m21conf = { loadSoundfont: false };
    </script>
    <script src="https://cdn.jsdelivr.net/npm/music21j/releases/music21.debug.js"></script>
    <script>
        music21.common.urls.soundfontUrl =
            'https://cdn.jsdelivr.net/gh/gleitz/midi-js-soundfonts/FluidR3_GM/';
        music21.miditools.loadSoundfont('clarinet', i => {
            const tn = music21.tinyNotation.TinyNotation('4/4 c4 d e f g1');
            tn.instrument = i;
            tn.playStream();
        });
    </script>
    </body>
  7. Import and use music21j

    master

    music21j is a JavaScript reimplementation of core music21 features. It provides a wide array of musical abstractions including notes, chords, streams, pitch, and harmony. The library is designed to run in modern browsers with SVG and Canvas support.

    Note: All interfaces are currently in beta and may change between releases. It is recommended to pin to a specific version if using it in a stable environment.

    import * as m21 from 'music21j';
    
    // Access various musical modules
    const note = m21.note;
    const chord = m21.chord;
    const stream = m21.stream;
    // ... etc
  8. Use music21j in a JavaScript/TypeScript project

    master

    Import the music21j module to access its musical analysis and notation capabilities. Once imported, you can instantiate classes like music21.note.Note and music21.stream.Stream.

    import * as music21 from 'music21j';
    
    const n = new music21.note.Note('F#');
    // etc.
  9. Use music21j in an HTML page via CDN

    master

    To use music21j directly in an HTML file without a build step, include the CSS and the debug JavaScript file from a CDN. Note that this example assumes a local Vite development server environment for the script execution context.

    <!DOCTYPE html>
    <html lang="en">                                                                                            
    <head>                                                    
      <meta charset="utf-8">
      <title>music21 test</title>
      <link rel="stylesheet"                                                                                  
            href="https://cdn.jsdelivr.net/npm/music21j/releases/music21j.css">
      <script src="https://cdn.jsdelivr.net/npm/music21j/releases/music21.debug.js"></script>                 
    </head>                                                                                                     
    <body
      <script>                                                                                                
          const n = new music21.note.Note('F#');            
          const s = new music21.stream.Stream();                               
          s.append(n);
          s.appendNewDOM();                                                                                   
      </script>                                             
    </body>
    </html>
  10. Reference the music21j exported modules

    master

    The following modules are exported by the music21j entrypoint. Use these to access specific musical logic, data structures, and utilities:

    • articulations: Articulation markings
    • audioRecording: Audio recording utilities
    • audioSearch: Audio search capabilities
    • bar: Bar/measure logic
    • beam: Beam logic
    • chord: Chord logic and structures
    • chordTables: Chord lookup tables
    • clef: Clef definitions
    • converter: Data conversion utilities
    • defaults: Library default settings
    • derivation: Musical derivation logic
    • derivation: Musical derivation logic
    • duration: Duration logic
    • dynamics: Dynamics markings
    • editorial: Editorial/notation adjustments
    • expressions: Musical expressions
    • figuredBass: Figured bass logic
    • fromPython: Logic for interacting with Python music21
    • harmony: Harmonic analysis and structures
    • instrument: Instrument definitions
    • interval: Interval logic
    • key: Key/tonality logic
    • keyboard: Keyboard/input logic
    • layout: Layout and positioning
    • metadata: Metadata handling
    • meter: Meter/time signature logic
    • miditools: MIDI manipulation tools
    • musicxml: MusicXML handling
    • note: Note logic and structures
    • parseLoader: Parsing and loading utilities
    • pitch: Pitch logic
    • renderOptions: Options for rendering
    • roman: Roman numeral analysis
    • scale: Scale logic
    • sites: Site/location logic
    • stream: Musical stream containers
    • style: Styling and notation appearance
    • svgs: SVG generation/handling
    • tempo: Tempo logic
    • tie: Tie logic
    • tinyNotation: TinyNotation support
    • vfShims: VexFlow shims
    • vfShow: VexFlow showing utilities
    • voiceLeading: Voice leading logic
    • webmidi: WebMIDI integration
    • exceptions21: Custom music21 error types
    • base / prebase / common: Core foundational classes and utilities
    • debug: Debugging utilities
    • MIDI: MIDI interface (via midicube)
    • Vex: VexFlow interface