npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@onjmin/chord-parser

v1.0.3

Published

コードネーム(chord symbol)⇄ 構成音(chord tones)を相互変換する、依存ゼロの軽量パーサ

Readme

@onjmin/chord-parser

コードネーム(chord symbol / 例: Cmaj7)⇄ 構成音(chord tones / 例: [0, 4, 7, 11])を相互変換する、依存ゼロ・軽量の TypeScript パーサです。

  • (A) コードネーム → 構成音parseChord / parseChords
  • (B) 構成音 → コードネームdetectChord

用語について Cmaj7 Dm7 のような表記は、一般に コードネーム(英: chord symbol / lead-sheet symbol)と呼びます。日本語の「コード記法」は通称で、音楽理論上の標準語ではありません。本ライブラリでは標準語に合わせて chord symbol(コードネーム)chord tones / 構成音 という名前を採用しています。

デモ: https://onjmin.github.io/chord-parser/demo/ ・ API ドキュメント: https://onjmin.github.io/chord-parser/

インストール

pnpm add @onjmin/chord-parser
# npm i @onjmin/chord-parser / yarn add @onjmin/chord-parser

ESM / CJS / 型定義 すべて同梱。ブラウザからは CDN 直読みも可能です。

import { parseChord, detectChord, parseChords } from "@onjmin/chord-parser";

(A) コードネーム → 構成音

parseChord(symbol)

単一のコードネームを解析して構成音を返します。

parseChord("Cmaj7");
// {
//   symbol: "Cmaj7",
//   root: 0,                  // ルートの pitch class(0=C, 1=C#, ... 11=B)
//   notes: [0, 4, 7, 11],     // ルートを 0 とした絶対半音オフセット(昇順)
//   pitchClasses: [0, 4, 7, 11], // オクターブを無視した pitch class 集合
//   intervals: [0, 4, 7, 11], // ルートからの相対音程
// }

parseChord("Dm7").pitchClasses;  // [0, 2, 5, 9]
parseChord("C/E").notes;         // [4, 7, 12]  ベース音 E を最低音に置いた転回形

| 例 | 意味 | |----|------| | C Cm Cdim Caug / C+ | 三和音(メジャー/マイナー/ディミニッシュ/オーグメント) | | Cmaj7 CM7 C7 Cm7 CmM7 | 各種7th | | Cdim7 Cm7b5 / | 減七 / 半減七 | | C6 Cm6 C69 | 6th 系 | | Csus4 Csus2 C7sus4 | サスペンド | | Cadd9 C9 CM9 Cm9 | テンション/拡張 | | C(#5) C(9,11) C7b9 C7#11 | 括弧つき構成音指定 | | Comit3 Cno5 | 構成音の省略 | | C/E ConE Dm7/G | 分数コード / オンコード(転回形・ハイブリッド) |

ルート音は C D E F G A B、変化記号は # ♯ b ♭m/min/minormaj/major/Mdim/aug など別綴りや全角記号( Δ ø)にも対応します。解析できない文字列では ChordSyntaxError を投げます。

parseChords(str, bpm = 120)

コード進行の文字列を、時刻つきイベント列に変換します。

parseChords("C | G | Am | F", 120);
// [
//   { key: "C", chord: "",  when: 0, duration: 2 },
//   { key: "G", chord: "",  when: 2, duration: 2 },
//   { key: "A", chord: "m", when: 4, duration: 2 },
//   { key: "F", chord: "",  when: 6, duration: 2 },
// ]

記法:

  • 行ごとに | l で小節を区切る
  • = 直前コードの継続 / % 直前コードの繰り返し
  • _ 休符 / NN.C. ノーコード
  • # で始まる行はコメント

keychord を連結して parseChord に渡せば、各コードの構成音が得られます。

for (const ev of parseChords("C | Am | F | G")) {
  const { notes } = parseChord(`${ev.key}${ev.chord}`);
  // ev.when / ev.duration(秒)に合わせてノートを鳴らす
}

(B) 構成音 → コードネーム

detectChord(notes, options?)

構成音からコードネームを推定します。一般性と基本形/転回形で順位づけした候補配列を返します。

detectChord([0, 4, 7]);          // [{ symbol: "C", ... }, ...]
detectChord([60, 64, 67, 71]);   // [{ symbol: "CM7", ... }]  MIDIノート番号でも可
detectChord([4, 7, 12])[0];      // { symbol: "C/E", inversion: true, root: 0, bass: 4, ... }
detectChord([0, 3, 7], { flat: true })[0].symbol; // "Cm"(フラット表記)

候補の各要素:

interface ChordCandidate {
  symbol: string;      // 推定コードネーム(転回形は "C/E" のようにベース併記)
  rootSymbol: string;  // 転回形を考慮しない基本形("C")
  root: number;        // ルートの pitch class
  quality: string;     // クオリティ部分("m7" など)
  bass: number;        // 最低音の pitch class
  inversion: boolean;  // ベース ≠ ルート(転回形 / 分数コード)か
}

| オプション | 既定 | 説明 | |------------|------|------| | flat | false | 音名をフラット表記(Db 等)にする | | bass | 最小値 | ベース音として扱う値を明示(pitch class のみで音域情報がないとき用) |

入力は pitch class(0〜11) でも MIDI ノート番号 でも構いません(オクターブは無視、最低音をベースとして転回形を判定)。完全一致のみを返し、見つからなければ空配列です。

しくみ

検出テーブルは parseChord 自身で生成しています(C を各クオリティに付けて構成音を求め、その pitch class 集合を逆引きキーにする)。そのため (A) と (B) の解釈は必ず一致し、detectChordrootSymbolparseChord に通すと元の構成音へ戻ります(往復一致)。検出対象のクオリティ一覧は QUALITIES で参照できます。

その他のエクスポート

  • noteName(pc, flat?) … pitch class → 音名(0 → "C"
  • SHARP_NAMES / FLAT_NAMES … 音名テーブル
  • toPitchClass(n) … 任意の整数を 0〜11 に正規化
  • QUALITIES … 検出対象コードクオリティの定義一覧(優先度順)

由来

(A) のパーサは rpgen3/pianoparseChord.mjs / parseChords.mjs を TypeScript へ移植し、外部依存(SortedSet / toHan)を内製の軽量実装へ置き換えたものです。maj7 がメジャー7thとして解釈されるよう、元実装の取りこぼしを修正しています。(B) は本ライブラリ独自の逆引き方式です。

開発

pnpm install
pnpm build      # tsup で dist を生成
pnpm test       # node:test
pnpm typecheck
pnpm dev        # ビルド + ローカルデモサーバ(http://localhost:40299)

License

MIT © onjmin