#music #ast #ast-parser #string-parser #parser #chord-progression

chord-progression-parser

a converter from chord progression strings to AST

26 releases (5 breaking)

0.6.2 May 22, 2024
0.6.1 Mar 20, 2024
0.5.0 Dec 16, 2023
0.4.3 Dec 9, 2023
0.1.16 Nov 24, 2023

#78 in WebAssembly

MIT license

255KB
6.5K SLoC

Rust 5.5K SLoC // 0.0% comments TypeScript 1K SLoC // 0.0% comments JavaScript 6 SLoC Shell 1 SLoC // 0.7% comments

chord-progression-parser

A converter from chord progression strings to AST built in Rust that outputs wasm, so it can be used from JavaScript too.

NOTE: This library releases multiple packages. GitHub release (latest SemVer)

Example

You can try it on CodeSandbox

example gif

Documents

How to use

Rust

  • Install

    cargo add chord-progression-parser
    
  • And use

    use chord_progression_parser::parse_chord_progression_string;
    
    fn main() {
      let input: &str = "
    @section=Intro
    [key=E]E - C#m(7) - Bm(7) - C#(7)
    F#m(7) - Am(7) - F#(7) - B
    
    @section=Verse
    E - C#m(7) - Bm(7) - C#(7)
    F#m(7) - Am(7) - F#(7) - B
    ";
    
        let result = parse_chord_progression_string(input);
        println!("{:#?}", result);
    }
    

JavaScript/TypeScript (using bundler, like Vite, or If you are using Next.js)

  • Install (example, use with Vite)

    npm install @lainnao/chord-progression-parser-bundler
    npm install -D vite-plugin-wasm
    
  • Edit vite.config.js

    import { defineConfig } from "vite";
    import wasm from "vite-plugin-wasm";
    
    export default defineConfig({
      plugins: [wasm()],
    });
    
  • And use

    import { parseChordProgressionString } from "@lainnao/chord-progression-parser-bundler/chord_progression_parser";
    
    const result = parseChordProgressionString("C");
    console.log(result);
    

JavaScript/TypeScript (server like Node.js, Bun)

  • Install

    npm install @lainnao/chord-progression-parser-node
    
  • And use

    import { parseChordProgressionString } from "@lainnao/chord-progression-parser-node/chord_progression_parser";
    
    const result = parseChordProgressionString("C");
    console.log(result);
    

JavaScript(CDN)

  • index.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body>
        <h1>load wasm directly example</h1>
        <h2>parse C</h2>
        <pre id="result"></pre>
        <script type="module">
          import * as mod from "https://cdn.jsdelivr.net/npm/@lainnao/chord-progression-parser-web@0.6.0/chord_progression_parser.js";
    
          (async () => {
            // initialize wasm
            await mod.default();
            // use
            const result = mod.parseChordProgressionString("C");
            console.log(result);
            document.querySelector("#result").innerHTML = JSON.stringify(
              result,
              null,
              2
            );
          })();
        </script>
      </body>
    </html>
    

Article

Dependencies

~6–14MB
~170K SLoC