VexFlow クレシェンド・デクレシェンド

提供:yonewiki

VexFlow 使い方に戻る。

概要

 クレシェンドやデクレシェンドもTextDynamics同様、音の長さの概念をもった仕組みを利用しますので、またVoiceクラスやFormatterクラスのお世話になるややこしさがあります。なんかいろいろ頑張ったんすけど、1小節目の位置からクレシェンドが描かれてしまってギブアップです。なんで?って感じ。4拍目だけまともに描けるのも、またミソだね。嫌になってきたから、このクレシェンドはもう使わねぇって決めたね。使わねぇ。何だコレだね。


 それで、結局、また奥の手を発動させました。今度はさらに奥の手の奥の手です。ウェブフォントでフリーのBravura.woff2というのを使いました。SMuFLというプロジェクトの中で生まれた標準音楽フォントの一つです。もうひとつ奥の手は自分で作った音楽フォントを使うというのがあります。以前にわたくしは制作済みなので、それをひっぱりだしてくる感じになります。くわしくは、こちらの記事ですね。


 ご自分のサイトでBravuraフォントを使いたい場合は公式サイトからダウンロードしてくる必要があります。ウェブフォントの拡張子はwoffとwoffというのがあります。woffが初代ウェブフォントだったのですが、woff2というwoffより上位の圧縮フォントが登場してるらしいです。自分が作ったのは、まだttfとかotfの範疇の形式でFontForgeで作ったモノです。FontForgeでウェブフォント作った経験がまだないので、そこはコレから習得する感じですね。


 話がそれたので、それはおいておいて、ダウンロードしたフォントを自分のサイトにアップロードします。好きなところに配置してよいですが、あとで相対パスで指定するので、どこに置いたかは把握しておく必要はありますし、参照しやすいパスがいいでしょう。そして、以下のような記述をcssで追記します。以下の例はhtmlファイルから見て、ファイルと同じディレクトリにfontというフォルダを作って、その中にBravura.woff2などの一式のフォントを置いたとしたらの例です。


@font-face {
    font-family: 'Bravura';
    src: url('./font/Bravura.woff2') format('woff2'),
         url('./font/Bravura.woff') format('woff');
}

@font-face {
    font-family: 'BravuraText';
    src: url('./font/BravuraText.woff2') format('woff2'),
         url('./font/BravuraText.woff') format('woff');
}


 そうして、javascriptで強引にクレシェンドを呼び出すコードを書きます。かなり難しいですが、読み解いてほしいです。説明もしますけど、いかんせんJavascriptをある程度しっている必要はあります。敷居高いな。VexFlow。


 SMuFLのグリフテーブルはhttps://w3c.github.io/smufl/latest/tables/index.htmlから調べることができます。あるいはFontForgeのようなものでフォントファイルを開いて見つけるというのも手だと思います。クレシェンドはU+E53EとデクレシェンドのU+E53Fに配置されています。4.44 Dyanamicsの項目にあります。英語の項目名を読み解ける必要があります。探すのはそれぞれで頑張ってほしいですね。


 BravuraTextは、こういう説明文中に挟み込むような小さな文字としての音楽記号が詰め込まれているそうです。

 

クレシェンド・デクレシェンド


 コードは以下のとおりです。


<div id="yonet202403Mid_Output01"></div>
<script>
(function(){
class Position {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}
var VF = Vex.Flow;
const Instrument1 = new Position(0, 0);
const Instrument2 = new Position(0, 0);
(function(){
const {
  Factory,
  Stave,
  StaveNote,
  Formatter,
  Voice,
  StaveTie,
  BarLine,
  StaveConnector,
  Modifier,
  RenderContext,
  TextDynamics,
  Articulation,
  TextNote,
  Crescendo,
  Renderer
} = Vex.Flow;

let nscale = 0.64;
let nwidth = 1040;
let nheight = 720;

let nHeadMargin = 70;
let nStaveWidth = 350;

const f = new Factory({ 
  renderer: { elementId: 'yonet202403Mid_Output01', width: nwidth, height: nheight * nscale }
  });
const ctx = f.getContext();
ctx.scale(nscale, nscale);

const defaultStyle = {
  lineWidth: ctx.lineWidth,
  strokeStyle: ctx.strokeStyle,
};



for (let i = 0; i <= nwidth * (1 / nscale); i += 10) {
  ctx.beginPath();
  ctx.moveTo(0 + i, 0);
  ctx.lineTo(0 + i, nheight);
  if (i % 100 === 0){
    ctx.setLineWidth(1);
    ctx.setStrokeStyle('rgba(200, 200, 200, 0.8)');
  }
  else{
    ctx.setLineWidth(0.5);
    ctx.setStrokeStyle('rgba(230, 230, 230, 0.8)');
  }
  ctx.stroke();
}

for (let i = 0; i <= nheight; i += 10) {
  ctx.beginPath();
  ctx.moveTo(0, i);
  ctx.lineTo(nwidth * (1 / nscale), i);
  if (i % 100 === 0){
    ctx.setLineWidth(1);
    ctx.setStrokeStyle('rgba(200, 200, 200, 0.8)');
  }
  else{
    ctx.setLineWidth(0.5);
    ctx.setStrokeStyle('rgba(230, 230, 230, 0.8)');
  }
  ctx.stroke();
}

ctx.lineWidth = defaultStyle.lineWidth;
ctx.strokeStyle = defaultStyle.strokeStyle;

const stave1 = new Stave(80, 150, nHeadMargin + nStaveWidth).setContext(ctx);
stave1.setClef("treble").setContext(ctx);
stave1.setTimeSignature('4/4').setContext(ctx);
stave1.setSection("A", 0);
stave1.setMeasure(1);
stave1.setTempo({ duration: "q", dots: 0, bpm: 120 }, 0);
stave1.setBegBarType(VF.Barline.type.REPEAT_BEGIN);

Instrument1.x = stave1.x - 30;
Instrument1.y = stave1.y + 90; 

stave1.draw();

const notes1 = [
  new StaveNote({ keys: ["d/5"], duration: "8", stem_direction: -1 }),
  new StaveNote({ keys: ["e/5"], duration: "8", stem_direction: -1 }),
  new StaveNote({ keys: ["f/5"], duration: "8", stem_direction: -1 }),
  new StaveNote({ keys: ["g/5"], duration: "8", stem_direction: -1 }),
  new StaveNote({ keys: ["a/5"], duration: "8", stem_direction: -1 }),
  new StaveNote({ keys: ["b/5"], duration: "8", stem_direction: -1 }),
  new StaveNote({ keys: ["c/6"], duration: "8", stem_direction: -1 }),
  new StaveNote({ keys: ["d/6"], duration: "8", stem_direction: -1 }),
];
notes1[0].addModifier(new VF.Articulation('a.').setPosition(3));
notes1[1].addModifier(new VF.Articulation('av').setPosition(3));
notes1[2].addModifier(new VF.Articulation('a>').setPosition(3));
notes1[3].addModifier(new VF.Articulation('a^').setPosition(3));
notes1[4].addModifier(new VF.Articulation('a-').setPosition(3));
notes1[5].addModifier(new VF.Articulation('a+').setPosition(3));
notes1[6].addModifier(new VF.Articulation('ao').setPosition(3));
notes1[7].addModifier(new VF.Articulation('a|').setPosition(3));

Formatter.FormatAndDraw(ctx, stave1, notes1);

var stave2 = new Vex.Flow.Stave(stave1.width + stave1.x, stave1.y, nStaveWidth);
var stave1002 = new Vex.Flow.Stave(stave1.width + stave1.x, stave1.y + 120, 0);
stave2.setEndBarType(VF.Barline.type.REPEAT_BOTH);
stave2.setMeasure(2);
stave2.setTempo({ name: "Andante", note: "8", bpm: 120 }, 0);
stave2.setContext(ctx).draw();

var note2_1 = new Vex.Flow.StaveNote({ keys: ["F/4"], duration: "8" });
var note2_2 = new Vex.Flow.StaveNote({ keys: ["C/5"], duration: "8", stem_direction: -1 });
var note2_3 = new Vex.Flow.StaveNote({ keys: ["D/4"], duration: "8" });
var note2_4 = new Vex.Flow.StaveNote({ keys: ["C/4"], duration: "8" });
var note2_5 = new Vex.Flow.StaveNote({ keys: ["G/4"], duration: "8" });
var note2_6 = new Vex.Flow.StaveNote({ keys: ["F/4"], duration: "8" });
var note2_7 = new Vex.Flow.StaveNote({ keys: ["G/4"], duration: "8" });
var note2_8 = new Vex.Flow.StaveNote({ keys: ["F/4"], duration: "8" });

note2_1.addModifier(new VF.Articulation('am').setPosition(4));
note2_2.addModifier(new VF.Articulation('a@a').setPosition(3));
note2_3.addModifier(new VF.Articulation('a@u').setPosition(4));
note2_4.addModifier(new VF.Articulation('a-').setPosition(4));
note2_4.addModifier(new VF.Articulation('a.').setPosition(4));

var stave2Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave2Voice.addTickables([note2_1, note2_2, note2_3, note2_4, note2_5, note2_6, note2_7, note2_8]);

var formatter2 = new Vex.Flow.Formatter().joinVoices([stave2Voice]);//.format([stave2Voice], nStaveWidth);
formatter2.formatToStave([stave2Voice], stave2);

stave2Voice.draw(ctx, stave2);

var Dynamics2_1 = new Vex.Flow.TextDynamics({ text: "sfz", duration: "2" });
var Dynamics2_3 = new Vex.Flow.TextDynamics({ text: "p", duration: "4" });
var Dynamics2_4 = new Vex.Flow.TextDynamics({ text: "f", duration: "4" });
var stave2VoiceDynamics = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave2VoiceDynamics.addTickables([Dynamics2_1, Dynamics2_3, Dynamics2_4]);


var Dynamics_formatter2 = new Vex.Flow.Formatter().joinVoices([stave2VoiceDynamics]);
Dynamics_formatter2.format([stave2VoiceDynamics], nStaveWidth);

stave2VoiceDynamics.draw(ctx, stave1002);


var note2_1_X = note2_1.getAbsoluteX();
var staveFrom1To4_Y = note2_1.getStave().getYForLine(2.5);

//stave1003でクレシェンドデクレシェンドをつけようと頑張った形跡を示します。
const stave3 = new Stave(stave2.width + stave2.x, stave1.y, nStaveWidth).setContext(ctx);
const stave1003 = new Stave(stave2.width + stave2.x, stave1.y, 0).setContext(ctx);
stave3.setEndBarType(VF.Barline.type.NONE);
stave3.setSection("B", 0);
stave3.setMeasure(3);
stave3.draw();
stave1003.draw();

const notes3 = [
  new VF.TextNote({glyph: "", duration: "4"}).setContext(ctx),
//  new VF.Crescendo({duration: "4"}).setLine(10).setHeight(15),
  new VF.Crescendo({duration: "4"}).setLine(10).setHeight(15),
//  new VF.TextNote({glyph: "", duration: "4"}).setContext(ctx),
  new VF.Crescendo({duration: "4"}).setLine(10).setDecrescendo(true).setHeight(10),
  new VF.Crescendo({duration: "4"}).setLine(10).setDecrescendo(true).setHeight(5)
];
var stave3VoiceDynamics = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave3VoiceDynamics.addTickables(notes3);

var formatter3 = new Vex.Flow.Formatter().joinVoices([stave3VoiceDynamics]);
formatter3.format([stave3VoiceDynamics], nStaveWidth);

//notes3[0].setStave(stave1003);
//notes3[0].setContext(ctx).draw();
//notes3[1].setStave(stave1003);
//notes3[1].setContext(ctx).draw();
//notes3[2].setStave(stave1003);
//notes3[2].setContext(ctx).draw();
//notes3[3].setStave(stave1003);
//notes3[3].setContext(ctx).draw();


const stave4 = new Stave(stave3.width + stave3.x, stave1.y, nStaveWidth).setContext(ctx);
stave4.setBegBarType(VF.Barline.type.NONE);
stave4.setEndBarType(VF.Barline.type.REPEAT_END);
stave4.setMeasure(4);
stave4.draw();


const stave5 = new Stave(stave1.x, stave1.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
stave5.addClef("treble").setContext(ctx);
stave5.addTimeSignature('4/4').setContext(ctx);
stave5.setSection("C", 0);
stave5.setMeasure(5);
stave5.setEndBarType(VF.Barline.type.SINGLE);//規定値(省略可能)

stave5.draw();

var connector = new VF.StaveConnector(stave1, stave5);
connector.setType(VF.StaveConnector.type.SINGLE);
connector.setContext(ctx);
connector.draw();

const notes5 = [
  new StaveNote({ keys: ["a/4"], duration: "q" }),
  new StaveNote({ keys: ["b/4"], duration: "q" }),
  new StaveNote({ keys: ["c/5"], duration: "q" }),
  new StaveNote({ keys: ["d/5"], duration: "q" }),
];

Formatter.FormatAndDraw(ctx, stave5, notes5);

const tie3 = new StaveTie({
  first_note: notes5[1],
  last_note: notes5[2],
  first_indices: [0],
  last_indices: [0],
});

const tie4 = new StaveTie({
  first_note: notes5[2],
  last_note: notes5[3],
  first_indices: [0],
  last_indices: [0],
});

tie3.setContext(ctx).draw();
tie4.setContext(ctx).draw();

const stave6 = new Stave(stave5.width + stave5.x, stave5.y, nStaveWidth).setContext(ctx);
stave6.setEndBarType(VF.Barline.type.DOUBLE);
stave6.setMeasure(6);

stave6.draw();

const notes6 = [
  new StaveNote({ keys: ["e/5"], duration: "q", stem_direction: -1 }),
  new StaveNote({ keys: ["f/5"], duration: "q", stem_direction: -1 }),
  new StaveNote({ keys: ["g/5"], duration: "q", stem_direction: -1 }),
  new StaveNote({ keys: ["a/5"], duration: "q", stem_direction: -1 }),
];

Formatter.FormatAndDraw(ctx, stave6, notes6);

const tie5 = new StaveTie({
  first_note: notes5[3],
  last_note: notes6[0],
  first_indices: [0],
  last_indices: [0],
});

tie5.setContext(ctx).draw();


const stave7 = new Stave(stave6.width + stave6.x, stave5.y, nStaveWidth).setContext(ctx);
stave7.setMeasure(7);
stave7.draw();


const stave8 = new Stave(stave7.width + stave7.x, stave5.y, nStaveWidth).setContext(ctx);
stave8.setMeasure(8);
stave8.draw();


const stave9 = new Stave(stave5.x, stave5.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
stave9.setMeasure(9);
stave9.draw();

var connector9 = new VF.StaveConnector(stave5, stave9);
connector9.setType(VF.StaveConnector.type.SINGLE);
connector9.setContext(ctx);
connector9.draw();

var connector9_2 = new VF.StaveConnector(stave5, stave9);
connector9_2.setType(VF.StaveConnector.type.BRACKET);
connector9_2.setContext(ctx);
//connector9_2.setText('E.Guitar');
connector9_2.draw();

Instrument2.x = stave1.x - 30;
Instrument2.y = stave5.y + ((stave9.y - stave5.y) / 2) + 90; 


const stave10 = new Stave(stave9.width + stave9.x, stave9.y, nStaveWidth).setContext(ctx);
stave10.setMeasure(10);
stave10.draw();


const stave11 = new Stave(stave10.width + stave10.x, stave9.y, nStaveWidth).setContext(ctx);
stave11.setMeasure(11);
stave11.draw();


const stave12 = new Stave(stave11.width + stave11.x, stave9.y, nStaveWidth).setContext(ctx);
stave12.setMeasure(12);
stave12.draw();

})();

const svgElement2 = document.querySelector('#yonet202403Mid_Output01 svg');
svgElement2.style.backgroundColor = '#F5F5F5';

var svgContainer = document.getElementById('yonet202403Mid_Output01');

var svgElement = svgContainer.querySelector('svg');

var newText = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText.setAttribute('x', '100');
newText.setAttribute('y', '100');
newText.setAttribute('font-family', 'Hiragino Kaku Gothic Pro, メイリオ, MS Gothic, sans-serif');
newText.setAttribute('font-size', '50');
newText.setAttribute('fill', 'black');
newText.textContent = 'TestTitle - subTitle - lyrics: yo-net, music: yo-net';

svgElement.appendChild(newText);


var newText2 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText2.setAttribute('x', Instrument1.x);
newText2.setAttribute('y', Instrument1.y);
newText2.setAttribute('font-family', 'Bravura, VexFlow, Arial, sans-serif');
newText2.setAttribute('font-size', '20pt');
newText2.setAttribute('fill', 'black');
newText2.textContent = 'Vocal';


var textGroup2 = document.createElementNS('http://www.w3.org/2000/svg', 'g');
textGroup2.setAttribute('transform', 'rotate(-90 ' + Instrument1.x + ' ' + Instrument1.y + ')');

textGroup2.appendChild(newText2);

svgElement.appendChild(textGroup2);

var newText3 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText3.setAttribute('x', Instrument2.x);
newText3.setAttribute('y', Instrument2.y);
newText3.setAttribute('font-family', 'Bravura, VexFlow, Arial, sans-serif');
newText3.setAttribute('font-size', '20pt');
newText3.setAttribute('fill', 'black');
newText3.textContent = 'E.Guitar';

var textGroup3 = document.createElementNS('http://www.w3.org/2000/svg', 'g');
textGroup3.setAttribute('transform', 'rotate(-90 ' + Instrument2.x + ' ' + Instrument2.y + ')');

textGroup3.appendChild(newText3);

svgElement.appendChild(textGroup3);

///////////////////////////
//直線による描画の例
///////////////////////////
var crescendoX = 900; // X座標
var crescendoY = 290; // Y座標
var crescendoWidth = 200; // クレシェンドの幅
var crescendoHeight = 20; // クレシェンドの高さ

// 新しいグループ要素の作成
var newGroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
newGroup.setAttribute('transform', 'translate(0, 0)');

// クレシェンドの線のパスを作成
var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
var pathStr = 'M ' + crescendoX + ' ' + crescendoY +
    ' L ' + (crescendoX + crescendoWidth) + ' ' + (crescendoY - crescendoHeight / 2) +
    ' L ' + crescendoX + ' ' + (crescendoY - crescendoHeight);
path.setAttribute('d', pathStr);
path.setAttribute('stroke', 'black');
path.setAttribute('stroke-width', '1');
path.setAttribute('fill', 'none'); // パスを透明にする
newGroup.appendChild(path);

// グループ要素の追加
svgElement.appendChild(newGroup);

///////////////////////////
//Bravuraフォントのデクレシェンド記号
///////////////////////////
var crescendo4X = 1110; // X座標1110
var crescendo4Y = 295; // Y座標290
var crescendo4Width = 80; // クレシェンドの幅
var crescendo4Height = 100; // クレシェンドの幅

// クレシェンド記号のフォント設定
var fontSize4 = 50; // フォントサイズ
var fontFamily4 = "Bravura, Arial"; // Bravuraフォントファミリー
var textContent4 = "\uE53F"; // デクレシェンド記号

// テキスト要素の作成
var newText4 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
newText4.setAttribute('id', 'crescendoText'); // テキスト要素にIDを付ける
newText4.setAttribute('x', 0);
newText4.setAttribute('y', 0);
newText4.setAttribute('font-family', fontFamily4);
newText4.setAttribute('font-size', fontSize4);
newText4.setAttribute('fill', 'black');
newText4.textContent = textContent4;

// SVG要素にテキストを追加
svgElement.appendChild(newText4);

requestAnimationFrame(function() {
    var textWidth = document.getElementById('crescendoText').getBBox().width;
    var textHeight = document.getElementById('crescendoText').getBBox().height;

    // クレシェンドの幅に応じて縦方向のスケーリング
    var horizontalScale = crescendo4Width / textWidth;
    var verticalScale = 1.2;//crescendo4Height / textHeight; // ここは実際に表示される大きさをみてから固定で設定するべき。なぜならグリフ毎に縦横比が違うので、一筋なわではいかないから。

    newText4.setAttribute('transform', 'translate(' + crescendo4X + ',' + crescendo4Y + ') scale(' + horizontalScale + ',' + verticalScale + ')');

    // クレシェンド記号をSVG要素に追加する
    svgElement.appendChild(newText4);
});

})();
</script>


 がんばったな。まず、stave1003の定義周辺でVexFlowでクレシェンドやデクレシェンドを付けようと頑張った形跡をしめしました。なんか、変な座標に引っ張られることがあるみたいで、ものすごい幅のバグった描画になってしまう問題から抜け出すことができないことがありました。みなさんの環境では、楽譜の構造が違っているゆえに何ら問題ないけど?みたいなことはあるのかもしれません。わかりません。


 そして、svgタグに線描画によってデクレシェンドを入れ込む技について示しています。367行目からのスクリプトがそれにあたります。この後に示すBravuraフォントを使う方法とどっちが美しいかで、それぞれが歩む道はわかれると思います。ややこしさはこちらだと思います。逆向きのクレシェンドを描くときも座標の計算のやり直しが必要ですね。座標計算が好きな人はこっちかな。


 390行目あたりからが、Bravuraフォントを使ったデクレシェンド描画です。

 なんか強引なことをやったほうが達成感があるせいか、管理人はBravuraフォントを使った描画が好きですね。今後もこの手法をねじ込んでいこうと思いました。配置さえすれば複雑な図形が描けるってことですからね。汎用性が高いっす。


 スクリプトの解説はおいおいやっていきます。


 とりあえず、次に進む?


 

VexFlow 使い方に戻る。