Skip to content

Instantly share code, notes, and snippets.

@teamikl
Last active October 24, 2015 16:45
Show Gist options
  • Save teamikl/36ad887fa28e6cfc259c to your computer and use it in GitHub Desktop.
Save teamikl/36ad887fa28e6cfc259c to your computer and use it in GitHub Desktop.
MuseScore2 plugin - Add note interval as lyrics
import QtQuick 2.0
import MuseScore 1.0
MuseScore {
menuPath: "Plugins.NoteInterval"
/**
* TPC (Tonal pitch class)
* ピッチは5度圏上での距離で比較する
*/
property variant intervalNames: {
"-11": "bb6",
"-10": "bb3",
"-9": "bb7",
"-8": "b4",
"-7": "b1",
"-6": "b5",
"-5": "b2",
"-4": "b6", // ???
"-3": "b3",
"-2": "b7",
"-1": "4", // 11
0: "1",
1: "5",
2: "2",
3: "6",
4: "3",
5: "7",
6: "#11", // #11
7: "#8",
8: "#5", // 13
9: "#9",
10: "#6",
11: "#10",
}
function map(callback, xs) {
var dest = [];
for (var idx in xs)
dest.push(callback(xs[idx]));
return dest;
}
function forEach(obj, callback) {
for (var key in obj) {
callback(obj[key], key);
}
}
function getIntervalText(root, note) {
return intervalNames[note - root];
}
// lyrics 要素を生成
function newLyric(element) {
var lyric = newElement(Element.LYRICS);
element.add(lyric);
lyric.text = "*";
return lyric;
}
onRun: {
// スコアが選択されていない場合、何もせず終了
if (typeof curScore === 'undefined')
Qt.quit();
// コードのルート音を覚えておく変数
var rootTpc = null;
// 全てのセグメントを走査
for (var segment = curScore.firstSegment();
segment != null;
segment = segment.next)
{
// 音符・休符以外は読み飛ばす
if (segment.segmentType != Segment.ChordRest)
continue;
// コード・シンボルからルート音の TPC を得る
forEach(segment.annotations, function(anon) {
if (anon.type == Element.HARMONY) {
rootTpc = anon.rootTpc;
}
});
// コードが設定されてない場合、何もしない
if (rootTpc === null)
continue;
// 最初のトラック(track=0)のみ対象
var element = segment.elementAt(0);
// 音符のピッチを調べ、ルートからの距離を歌詞へ追加
if (element && element.type == Element.CHORD) {
// 歌詞要素を所得。既にある場合は上書き、そうでなければ新規生成。
var lyric = element.lyrics[0] || newLyric(element);
// 各音のインターバルを調べる
var xs = map(function(note){
return getIntervalText(rootTpc, note.tpc);
}, element.notes);
// 歌詞のテキストへ追加
if (lyric)
lyric.text = xs.join(",");
}
}
Qt.quit();
}
}
@teamikl
Copy link
Author

teamikl commented Oct 24, 2015

インストール方法

  1. プラグイン・クリエーターを開き、新規作成・コードを貼り付け・保存
    • もしくは、ファイルをダウンロードして、プラグインのフォルダへ置く
  2. プラグイン・マネージャーを開いてプラグインを有効にする
  3. プラグインのメニューに menuPath に設定した値が追加されます

使用上の注意

既存の譜表の、一番上の楽器のみを対象とします。(リードシートを想定)

歌詞のデータを上書きしてしまうため、元データがある場合はバックアップを取るなどしてください。
スコアを閉じる前なら、元に戻すでプラグインの実行結果を戻すことはできます。

※ 和音は想定していません。単音のみ。(現在作業中)

注釈

ノートのピッチは、TPC(Tonal Pitch Class)で管理されていて、
5度圏の順序に沿って番号が割り振られています。

これは、Gb (b5) と F# (#4) は異なるデータ値ですが、
12 の剰余を求めると同じ値になる仕組みです。

5線上のフラット・シャープを判断してインターバルを決定しているため、
Abm 上の B 音が #9 とされます。 Cb が b3

@teamikl
Copy link
Author

teamikl commented Oct 24, 2015

分数コード/UST等が課題
root以外に base 基準の度数も取れるようにする
表示方法は後で検討。(オプションで選択・両方表示)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment