「VexFlow 装飾音符」の版間の差分

提供:yonewiki
編集の要約なし
 
(同じ利用者による、間の51版が非表示)
1行目: 1行目:
<yjavascript></script>
<yjavascript></script>
<!--<script src="https://wiki.yo-net.jp/vexflow/build/cjs/vexflow@1.0.02.js"></script>-->
<script src="https://wiki.yo-net.jp/vexflow/build/cjs/vexflow@4.2.2.0001.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vexflow@4.0.2/build/cjs/vexflow.js></script>
<!--<script src="https://cdn.jsdelivr.net/npm/vexflow@4.2.3/build/cjs/vexflow.js"></script>-->
<!--<script src="https://cdn.jsdelivr.net/npm/vexflow@4.0.2/build/cjs/vexflow.js></script>-->
</yjavascript>
</yjavascript>
[[VexFlow 使い方]]に戻る。
[[VexFlow 使い方]]に戻る。
== '''概要''' ==
== '''概要''' ==
 いったん。例示の楽譜をリセットしましょう。ごちゃごちゃし過ぎてきましたね。装飾音符簡単じゃなかったな。サンプルと同じようには動かんかった。謎だわ。VexFlow。まだまだ管理人を悩ませおるわ。公式サンプルでは引数をオブジェクトを { notes: gracenotes0, slur: true } のように指定する感じですが、普通にVex.Flow.GraceNoteGroupオブジェクトからそれをするとエラーになります。f = new Factory(なんたら);のような f オブジェクトから動かすとオブジェクトがf.GraceNoteGroup({ notes: gracenotes0, slur: true }) が設定できるみたいですね。それがいやで、VexFlowのソースを変更してみたりしました。格闘の様子は[[Node.js VexFlowでGruntを使う|こちらの記事]]から見ることができます。普通にVex.Flow.GraceNoteGroupからやる場合は第一引数がnotes:キーの対にあたるオブジェクトを、第二引数にはslur:の対にあたるブール値を設定するといいみたいです。よくよく調べてみるとVex.Flow.Accidental({type:'#'})も同じような仕組みで、このようにオブジェクトを指定すると動きませんでした。第一引数に直接typeの対になる値を記述するとVex.Flow.Accidental('#')のようにして使えるみたいですね。Accidentalはtypeだけしか設定しないので、あまりややこしくないですね。GraceNoteGroupはnotesとslurを設定するので、引数が二個いる感じです。二つ目の引数は省略しても動きます。デフォルトはfalseです。装飾音符のスラーダサいっすね。シャープの上を通過するカーブを描くなんてね。もっと違う方法でカーブが描けないか挑戦する必要がありそう。道のりは長いなコレ。ちょっと離れ過ぎて配置されるのも納得がいかないけどつめる設定方法も無いみたいだし。
 いったん。例示の楽譜をリセットしましょう。ごちゃごちゃし過ぎてきましたね。装飾音符簡単じゃなかったな。サンプルと同じようには動かんかった。謎だわ。VexFlow。まだまだ管理人を悩ませおるわ。公式サンプルでは引数をオブジェクトを { notes: gracenotes0, slur: true } のように指定する感じですが、普通にVex.Flow.GraceNoteGroupオブジェクトからそれをするとエラーになります。f = new Factory(なんたら);のような f オブジェクトから動かすとオブジェクトがf.GraceNoteGroup({ notes: gracenotes0, slur: true }) が設定できるみたいですね。f.なんちゃらでずっと操作する方式もあります。2小節目はf.なんちゃらを使う方式を使っています。オススメはしませんが勉強にもなるので、やってみて下さい。こういう書き方もできる!みたいなね。自分はf.オブジェクトをずっと記述していく方法のそれがいやでもあり、Vex.Flow.GraceNoteGroup({ notes: gracenotes0, slur: true })という指定もできてはいいではないかと思い、VexFlowのソースを変更してみたりしました。格闘の様子は[[Node.js VexFlowでGruntを使う|こちらの記事]]から見ることができます。皆さんは、そんなことせずに、順番に第一引数、第二引数と設定して使うのがよいと思います。ただサンプルはあてにならなくなりますけど、理解していれば問題ない。なぜそうなってしまっているのか、はさきほどの記事を理解できたら、わかると思います。普通にVex.Flow.GraceNoteGroupからやる場合は第一引数がnotes:キーの対にあたるオブジェクトを、第二引数にはslur:の対にあたるブール値を設定するといいみたいです。よくよく調べてみるとVex.Flow.Accidental({type:'#'})も同じような仕組みで、このようにオブジェクトを指定すると動きませんでした。第一引数に直接typeの対になる値を記述するとVex.Flow.Accidental('#')のようにして使えるみたいですね。Accidentalはtypeだけしか設定しないので、あまりややこしくないですね。GraceNoteGroupはnotesとslurを設定するので、引数が二個いる感じです。二つ目の引数は省略しても動きます。デフォルトはfalseです。装飾音符のスラーダサいっすね。シャープの上を通過するカーブを描くなんてね。もっと違う方法でカーブが描けないか挑戦する必要がありそう。道のりは長いなコレ。ちょっと離れ過ぎて配置されるのも納得がいかないけどつめる設定方法も無いみたいだし。




39行目: 40行目:
   StaveTie,
   StaveTie,
   BarLine,
   BarLine,
  Beam,
  Dot,
   StaveConnector,
   StaveConnector,
   Modifier,
   Modifier,
123行目: 126行目:
//notes1[0].addModifier(new VF.Accidental('#'), 0);
//notes1[0].addModifier(new VF.Accidental('#'), 0);
notes1[0].addModifier(new VF.Accidental({type:'#'}), 0);
notes1[0].addModifier(new VF.Accidental({type:'#'}), 0);
notes1[0].addModifier(new VF.GraceNoteGroup(gracenotes1, true), 0);//Vex.Flow.Modifier.Position.LEFT
notes1[0].addModifier(new VF.GraceNoteGroup({notes: gracenotes1, slur: true}), 0);//Vex.Flow.Modifier.Position.LEFT
//notes1[0].addModifier(0, new VF.Articulation({ type: 'a>' }).setPosition(VF.Modifier.Position.ABOVE));
//notes1[0].addModifier(0, new VF.Articulation({ type: 'a>' }).setPosition(VF.Modifier.Position.ABOVE));


135行目: 138行目:
stave1Voice.draw(ctx, stave1);
stave1Voice.draw(ctx, stave1);


var stave2 = new Vex.Flow.Stave(stave1.width + stave1.x, stave1.y, nStaveWidth);
const stave2 = f.Stave({ x: stave1.width + stave1.x, y: stave1.y, width: nStaveWidth });
//var stave2 = new Vex.Flow.Stave(stave1.width + stave1.x, stave1.y, nStaveWidth);
stave2.setMeasure(2);
stave2.setMeasure(2);
stave2.setContext(ctx).draw();
stave2.setContext(ctx).draw();
const gracenotes2 = [{ keys: ['d/4'], duration: '8', slash: false }].map(f.GraceNote.bind(f));
//const notes2 = [
//  new StaveNote({ keys: ["c/4"], duration: "4" }),
//  new StaveNote({ keys: ["d/4"], duration: "4" }),
//  new StaveNote({ keys: ["e/4"], duration: "4" }),
//  new StaveNote({ keys: ["f/4"], duration: "4" })
//];
const notes2 = [
        f.StaveNote({ keys: ['c/4'], duration: '4', auto_stem: true }),
        f.StaveNote({ keys: ["d/4"], duration: "4" }),
        f.StaveNote({ keys: ["e/4"], duration: "4" }),
        f.StaveNote({ keys: ["f/4"], duration: "4" })
];
notes2[0].addModifier(f.GraceNoteGroup({ notes: gracenotes2, slur: true }), 0);
//notes1[0].addModifier(new VF.GraceNoteGroup({notes: gracenotes1, slur: true}), 0);
const stave2Voice = f.Voice({ num_beats: 4, beat_value: 4 }).setStrict(false).addTickables(notes2);
var formatter2 = new VF.Formatter().joinVoices([stave2Voice]);
formatter2.formatToStave([stave2Voice], stave2);
f.draw();
//var stave2Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
//stave2Voice.setStrict(false);
//stave2Voice.addTickables(notes2);
//var formatter2 = new Vex.Flow.Formatter().joinVoices([stave2Voice]);
////formatter1.format([stave1Voice], nStaveWidth);
//formatter2.formatToStave([stave2Voice], stave2);
//stave2Voice.draw(ctx, stave2);


const stave3 = new Stave(stave2.width + stave2.x, stave1.y, nStaveWidth).setContext(ctx);
const stave3 = new Stave(stave2.width + stave2.x, stave1.y, nStaveWidth).setContext(ctx);
166行目: 201行目:
stave5.addClef("treble").setContext(ctx);
stave5.addClef("treble").setContext(ctx);
stave5.addTimeSignature('4/4').setContext(ctx);
stave5.addTimeSignature('4/4').setContext(ctx);
stave5.addKeySignature('E').setContext(ctx);
stave5.setMeasure(5);
stave5.setMeasure(5);
stave5.draw();
stave5.draw();


174行目: 209行目:
connector.setContext(ctx);
connector.setContext(ctx);
connector.draw();
connector.draw();
const gracenotes5 = [
  new VF.GraceNote({ keys: ['f/5'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['g/5'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['b/5'], duration: '16', slash: false }),
];
const notes5 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["c/6"], duration: "2", stem_direction: -1 })
];
notes5[2].addModifier(new VF.GraceNoteGroup(gracenotes5, true).beamNotes(), 0);
var stave5Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave5Voice.setStrict(false);
stave5Voice.addTickables(notes5);
var formatter5 = new Vex.Flow.Formatter().joinVoices([stave5Voice]);
formatter5.format([stave5Voice], nStaveWidth);
stave5Voice.draw(ctx, stave5);
function dotted(staveNote, noteIndex = -1) {
  if (noteIndex < 0) {
    Dot.buildAndAttach([staveNote], {
      all: true
    });
  }
  else {
    for(noteIndexValue of noteIndex){
      Dot.buildAndAttach([staveNote], {
        index: noteIndexValue
      });
    }
  }
  return staveNote;
}


const stave6 = new Stave(stave5.width + stave5.x, stave5.y, nStaveWidth).setContext(ctx);
const stave6 = new Stave(stave5.width + stave5.x, stave5.y, nStaveWidth).setContext(ctx);
stave6.setMeasure(6);
stave6.setMeasure(6);
stave6.draw();
stave6.draw();
const notes6 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1  }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1  }),
  dotted(new StaveNote({ keys: ["f/5"], duration: "4d", stem_direction: -1  })),
  new StaveNote({ keys: ["e/5"], duration: "16", stem_direction: -1  }),
  new StaveNote({ keys: ["f/5"], duration: "16", stem_direction: -1  })
];
//const notes6beam = notes6[3].concat(notes6[4]);
const beam6 = new VF.Beam([notes6[3],notes6[4]]);
var stave6Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave6Voice.addTickables(notes6);
var formatter6 = new Vex.Flow.Formatter().joinVoices([stave6Voice]);
formatter6.format([stave6Voice], nStaveWidth);
stave6Voice.draw(ctx, stave6);
beam6.setContext(ctx).draw();


const stave7 = new Stave(stave6.width + stave6.x, stave5.y, nStaveWidth).setContext(ctx);
const stave7 = new Stave(stave6.width + stave6.x, stave5.y, nStaveWidth).setContext(ctx);
stave7.setMeasure(7);
stave7.setMeasure(7);
stave7.draw();
stave7.draw();
const gracenotes7 = [
  new VF.GraceNote({ keys: ['e/6'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['f/6'], duration: '16', slash: false })
];
const notes7 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["g/6"], duration: "2", stem_direction: -1 })
];
notes7[2].addModifier(new VF.GraceNoteGroup(gracenotes7, true).beamNotes(), 0);
var stave7Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave7Voice.setStrict(false);
stave7Voice.addTickables(notes7);
var formatter7 = new Vex.Flow.Formatter().joinVoices([stave7Voice]);
formatter7.format([stave7Voice], nStaveWidth);
stave7Voice.draw(ctx, stave7);


const stave8 = new Stave(stave7.width + stave7.x, stave5.y, nStaveWidth).setContext(ctx);
const stave8 = new Stave(stave7.width + stave7.x, stave5.y, nStaveWidth).setContext(ctx);
stave8.setMeasure(8);
stave8.setMeasure(8);
stave8.draw();
stave8.draw();
const notes8 = [
  new StaveNote({ keys: ["g/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["g/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["f/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["e/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["e/6"], duration: "2", stem_direction: -1  })
];
const beam8_1 = new VF.Beam([notes8[0],notes8[1]]);
const beam8_2 = new VF.Beam([notes8[2],notes8[3]]);
var stave8Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave8Voice.addTickables(notes8);
var formatter8 = new Vex.Flow.Formatter().joinVoices([stave8Voice]);
formatter8.format([stave8Voice], nStaveWidth);
stave8Voice.draw(ctx, stave8);
beam8_1.setContext(ctx).draw();
beam8_2.setContext(ctx).draw();


const stave9 = new Stave(stave5.x, stave5.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
const stave9 = new Stave(stave5.x, stave5.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
266行目: 402行目:




<syntaxhighlight lang="javascript" line start="1" highlight="345-348">
<syntaxhighlight lang="javascript" line start="1" highlight="98-100,113,126-128,130,132-141">
<div id="yonet202403Mid_Output01"></div>
<div id="yonet202403Mid_Output01"></div>
<script>
<script>
///////////////////////
///////////////////////
(function(){
(function(){
289行目: 426行目:
   StaveTie,
   StaveTie,
   BarLine,
   BarLine,
  Beam,
  Dot,
   StaveConnector,
   StaveConnector,
   Modifier,
   Modifier,
355行目: 494行目:
stave1.setClef("treble").setContext(ctx);
stave1.setClef("treble").setContext(ctx);
stave1.setTimeSignature('4/4').setContext(ctx);
stave1.setTimeSignature('4/4').setContext(ctx);
stave1.setSection("A", 0);
stave1.setMeasure(1);
stave1.setMeasure(1);
stave1.setTempo({ duration: "q", dots: 0, bpm: 120 }, 0);
stave1.draw();
stave1.setBegBarType(VF.Barline.type.REPEAT_BEGIN);//BarLine.type.REPEAT_BEGIN


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


stave1.draw();
const gracenotes1 = [
 
  new VF.GraceNote({ keys: ['b/4'], duration: '8', slash: true })
];


const notes1 = [
const notes1 = [
   new StaveNote({ keys: ["d/5"], duration: "8", stem_direction: -1 }),
   new StaveNote({ keys: ["c/4", "e/4", "g/4"], duration: "4" }),
  new StaveNote({ keys: ["e/5"], duration: "8", stem_direction: -1 }),
   new StaveNote({ keys: ["d/4", "f/4", "a/4"], duration: "4" }),
   new StaveNote({ keys: ["f/5"], duration: "8", stem_direction: -1 }),
   new StaveNote({ keys: ["e/4", "g/4", "b/4"], duration: "4" }),
  new StaveNote({ keys: ["g/5"], duration: "8", stem_direction: -1 }),
   new StaveNote({ keys: ["f/4", "a/4", "c/5"], duration: "4" })
   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[0].addModifier(new VF.Accidental({type:'#'}), 0);
notes1[2].addModifier(new VF.Articulation('a>').setPosition(3));
//一般的には以下の記述方法を使います。
notes1[3].addModifier(new VF.Articulation('a^').setPosition(3));
//.addModifier(new VF.Accidental('#'), 0)
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);
notes1[0].addModifier(new VF.GraceNoteGroup({notes: gracenotes1, slur: true}), 0);
//同じく
//.addModifier(new VF.GraceNoteGroup(gracenotes1, true}), 0)


var stave2 = new Vex.Flow.Stave(stave1.width + stave1.x, stave1.y, nStaveWidth);
var stave1Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
var stave1002 = new Vex.Flow.Stave(stave1.width + stave1.x, stave1.y + 120, 0);
stave1Voice.setStrict(false);
stave2.setEndBarType(VF.Barline.type.REPEAT_BOTH);
stave1Voice.addTickables(notes1);
 
var formatter1 = new Vex.Flow.Formatter().joinVoices([stave1Voice]);
formatter1.formatToStave([stave1Voice], stave1);
stave1Voice.draw(ctx, stave1);
 
//Factoryクラスのオブジェクトfの関数を使う方法
const stave2 = f.Stave({ x: stave1.width + stave1.x, y: stave1.y, width: nStaveWidth });
stave2.setMeasure(2);
stave2.setMeasure(2);
stave2.setTempo({ name: "Andante", note: "8", bpm: 120 }, 0);
stave2.setContext(ctx).draw();
stave2.setContext(ctx).draw();


var note2_1 = new Vex.Flow.StaveNote({ keys: ["F/4"], duration: "8" });
const gracenotes2 = [{ keys: ['d/4'], duration: '8', slash: false }].map(f.GraceNote.bind(f));
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));
const notes2 = [
note2_2.addModifier(new VF.Articulation('a@a').setPosition(3));
        f.StaveNote({ keys: ['c/4'], duration: '4', auto_stem: true }),
note2_3.addModifier(new VF.Articulation('a@u').setPosition(4));
        f.StaveNote({ keys: ["d/4"], duration: "4" }),
note2_4.addModifier(new VF.Articulation('a-').setPosition(4));
        f.StaveNote({ keys: ["e/4"], duration: "4" }),
note2_4.addModifier(new VF.Articulation('a.').setPosition(4));
        f.StaveNote({ keys: ["f/4"], duration: "4" })
 
];
var stave2Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
notes2[0].addModifier(f.GraceNoteGroup({ notes: gracenotes2, slur: true }), 0);
stave2Voice.addTickables([note2_1, note2_2, note2_3, note2_4, note2_5, note2_6, note2_7, note2_8]);
const stave2Voice = f.Voice({ num_beats: 4, beat_value: 4 }).setStrict(false).addTickables(notes2);
 
var formatter2 = new VF.Formatter().joinVoices([stave2Voice]);
var formatter2 = new Vex.Flow.Formatter().joinVoices([stave2Voice]);//.format([stave2Voice], nStaveWidth);
formatter2.formatToStave([stave2Voice], stave2);
formatter2.formatToStave([stave2Voice], stave2);
 
f.draw();//ここはいろいろな記述の最後に一回だけ使うと思う。なので、この先もっとこの方法で小節を描画する場合はそのスクリプトを先に書いて最後に.draw()を実行かな。
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);


const stave3 = new Stave(stave2.width + stave2.x, stave1.y, nStaveWidth).setContext(ctx);
const stave3 = new Stave(stave2.width + stave2.x, stave1.y, nStaveWidth).setContext(ctx);
stave3.setEndBarType(VF.Barline.type.NONE);
stave3.setSection("B", 0);
stave3.setMeasure(3);
stave3.setMeasure(3);
stave3.draw();
stave3.draw();
440行目: 553行目:


const stave4 = new Stave(stave3.width + stave3.x, stave1.y, nStaveWidth).setContext(ctx);
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.setMeasure(4);
stave4.draw();
stave4.draw();
451行目: 562行目:
   new StaveNote({ keys: ["f/4", "a/4", "c/5", "e/5"], duration: "4" })
   new StaveNote({ keys: ["f/4", "a/4", "c/5", "e/5"], duration: "4" })
];
];
notes4[0].addStroke(0, new VF.Stroke(3));
notes4[1].addStroke(0, new VF.Stroke(4));
notes4[2].addStroke(0, new VF.Stroke(3));
notes4[3].addStroke(0, new VF.Stroke(4));


var stave4Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
var stave4Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
461行目: 568行目:
var formatter4 = new Vex.Flow.Formatter().joinVoices([stave4Voice]);
var formatter4 = new Vex.Flow.Formatter().joinVoices([stave4Voice]);
formatter4.format([stave4Voice], nStaveWidth);
formatter4.format([stave4Voice], nStaveWidth);
var vibrato4 = new VF.VibratoBracket({
  start: notes4[1],
  stop: notes4[3],
});
vibrato4.setLine(2);


stave4Voice.draw(ctx, stave4);
stave4Voice.draw(ctx, stave4);


vibrato4.setContext(ctx).draw();
//ここから「からたち野道」のイントロピアノ旋律。装飾音符を使う暫定1位。
 
const stave5 = new Stave(stave1.x, stave1.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
const stave5 = new Stave(stave1.x, stave1.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
stave5.addClef("treble").setContext(ctx);
stave5.addClef("treble").setContext(ctx);
stave5.addTimeSignature('4/4').setContext(ctx);
stave5.addTimeSignature('4/4').setContext(ctx);
stave5.setSection("C", 0);
stave5.addKeySignature('E').setContext(ctx);
stave5.setMeasure(5);
stave5.setMeasure(5);
stave5.setEndBarType(VF.Barline.type.SINGLE);
stave5.draw();
stave5.draw();


485行目: 583行目:
connector.setContext(ctx);
connector.setContext(ctx);
connector.draw();
connector.draw();
const gracenotes5 = [
  new VF.GraceNote({ keys: ['f/5'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['g/5'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['b/5'], duration: '16', slash: false }),
];


const notes5 = [
const notes5 = [
   new StaveNote({ keys: ["a/4"], duration: "q" }),
   new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1 }),
   new StaveNote({ keys: ["b/4"], duration: "q" }),
   new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
   new StaveNote({ keys: ["c/5"], duration: "q" }),
   new StaveNote({ keys: ["c/6"], duration: "2", stem_direction: -1 })
  new StaveNote({ keys: ["d/5"], duration: "q" }),
];
];
notes5[2].addModifier(new VF.GraceNoteGroup(gracenotes5, true).beamNotes(), 0);


var stave5Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
var stave5Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave5Voice.setStrict(false);
stave5Voice.addTickables(notes5);
stave5Voice.addTickables(notes5);


var formatter5 = new Vex.Flow.Formatter().joinVoices([stave5Voice]);
var formatter5 = new Vex.Flow.Formatter().joinVoices([stave5Voice]);
formatter5.format([stave5Voice], nStaveWidth);
formatter5.format([stave5Voice], nStaveWidth);
var vibrato5 = new VF.VibratoBracket({
  start: notes5[3],
  stop: null,
});
vibrato5.setLine(2);
vibrato5.setHarsh(true);


stave5Voice.draw(ctx, stave5);
stave5Voice.draw(ctx, stave5);


vibrato5.setContext(ctx).draw();
function dotted(staveNote, noteIndex = -1) {
 
  if (noteIndex < 0) {
const tie3 = new StaveTie({
    Dot.buildAndAttach([staveNote], {
  first_note: notes5[1],
      all: true
  last_note: notes5[2],
    });
  first_indices: [0],
  }
  last_indices: [0],
  else {
});
    for(noteIndexValue of noteIndex){
 
      Dot.buildAndAttach([staveNote], {
const tie4 = new StaveTie({
        index: noteIndexValue
  first_note: notes5[2],
      });
  last_note: notes5[3],
    }
  first_indices: [0],
  }
  last_indices: [0],
  return staveNote;
});
}
 
tie3.setContext(ctx).draw();
tie4.setContext(ctx).draw();


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


const notes6 = [
const notes6 = [
   new StaveNote({ keys: ["e/5"], duration: "q", stem_direction: -1 }),
   new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1  }),
   new StaveNote({ keys: ["f/5"], duration: "q", stem_direction: -1 }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
   new StaveNote({ keys: ["g/5"], duration: "q", stem_direction: -1 }),
   dotted(new StaveNote({ keys: ["f/5"], duration: "4d", stem_direction: -1 })),
   new StaveNote({ keys: ["a/5"], duration: "q", stem_direction: -1 }),
   new StaveNote({ keys: ["e/5"], duration: "16", stem_direction: -1 }),
   new StaveNote({ keys: ["f/5"], duration: "16", stem_direction: -1 })
];
];
const beam6 = new VF.Beam([notes6[3],notes6[4]]);


Formatter.FormatAndDraw(ctx, stave6, notes6);
var stave6Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
 
stave6Voice.addTickables(notes6);
const tie5 = new StaveTie({
  first_note: notes5[3],
  last_note: notes6[0],
  first_indices: [0],
  last_indices: [0],
});


tie5.setContext(ctx).draw();
var formatter6 = new Vex.Flow.Formatter().joinVoices([stave6Voice]);
formatter6.format([stave6Voice], nStaveWidth);


stave6Voice.draw(ctx, stave6);
beam6.setContext(ctx).draw();


const stave7 = new Stave(stave6.width + stave6.x, stave5.y, nStaveWidth).setContext(ctx);
const stave7 = new Stave(stave6.width + stave6.x, stave5.y, nStaveWidth).setContext(ctx);
stave7.setMeasure(7);
stave7.setMeasure(7);
stave7.draw();
stave7.draw();
const gracenotes7 = [
  new VF.GraceNote({ keys: ['e/6'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['f/6'], duration: '16', slash: false })
];


const notes7 = [
const notes7 = [
   new StaveNote({ keys: ["a/4"], duration: "q" }),
   new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["b/4"], duration: "q" }),
   new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
   new StaveNote({ keys: ["c/5"], duration: "q" }),
   new StaveNote({ keys: ["g/6"], duration: "2", stem_direction: -1 })
   new StaveNote({ keys: ["d/5"], duration: "q" }),
];
];
notes7[2].addModifier(new VF.GraceNoteGroup(gracenotes7, true).beamNotes(), 0);


var stave7Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
var stave7Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave7Voice.setStrict(false);
stave7Voice.addTickables(notes7);
stave7Voice.addTickables(notes7);


var formatter7 = new Vex.Flow.Formatter().joinVoices([stave7Voice]);
var formatter7 = new Vex.Flow.Formatter().joinVoices([stave7Voice]);
formatter7.format([stave7Voice], nStaveWidth);
formatter7.format([stave7Voice], nStaveWidth);
var bracket7top = new VF.TextBracket({
  start: notes7[1],
  stop: notes7[3],
  text: "8",
  superscript: "va",
  position: 1
});
bracket7top.setLine(2);
var bracket7bottom = new VF.TextBracket({
  start: notes7[1],
  stop: notes7[3],
  text: "8",
  superscript: "vb",
  position: -1
});
bracket7bottom.setLine(4);
bracket7bottom.render_options.bracket_height = 40;
bracket7bottom.render_options.line_width = 2;
bracket7bottom.render_options.show_bracket = true;
bracket7bottom.setDashed(true, [2, 2]);
bracket7bottom.font.size = 15;
bracket7bottom.setDashed(false);
bracket7bottom.render_options.underline_superscript = false;
bracket7bottom.setFont({
  weight: "bold",
  family: "Arial",
  size: 15
});


stave7Voice.draw(ctx, stave7);
stave7Voice.draw(ctx, stave7);
bracket7top.setContext(ctx).draw();
bracket7bottom.setContext(ctx).draw();


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


const notes8 = [
const notes8 = [
   new StaveNote({ keys: ["g/4"], duration: "4s" }),
   new StaveNote({ keys: ["g/6"], duration: "8", stem_direction: -1  }),
   new StaveNote({ keys: ["g/4"], duration: "4s" }),
   new StaveNote({ keys: ["g/6"], duration: "8", stem_direction: -1  }),
   new StaveNote({ keys: ["g/4"], duration: "4s" }),
   new StaveNote({ keys: ["f/6"], duration: "8", stem_direction: -1  }),
   new StaveNote({ keys: ["g/4"], duration: "4s" }),
   new StaveNote({ keys: ["e/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["e/6"], duration: "2", stem_direction: -1  })
];
];
const beam8_1 = new VF.Beam([notes8[0],notes8[1]]);
const beam8_2 = new VF.Beam([notes8[2],notes8[3]]);


var stave8Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
var stave8Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
624行目: 693行目:


stave8Voice.draw(ctx, stave8);
stave8Voice.draw(ctx, stave8);
beam8_1.setContext(ctx).draw();
beam8_2.setContext(ctx).draw();


const stave9 = new Stave(stave5.x, stave5.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
const stave9 = new Stave(stave5.x, stave5.y + 200, nHeadMargin + nStaveWidth).setContext(ctx);
637行目: 708行目:
connector9_2.setType(VF.StaveConnector.type.BRACKET);
connector9_2.setType(VF.StaveConnector.type.BRACKET);
connector9_2.setContext(ctx);
connector9_2.setContext(ctx);
//connector9_2.setText('E.Guitar');
connector9_2.draw();
connector9_2.draw();


662行目: 732行目:
const svgElement2 = document.querySelector('#yonet202403Mid_Output01 svg');
const svgElement2 = document.querySelector('#yonet202403Mid_Output01 svg');
svgElement2.style.backgroundColor = '#F5F5F5';
svgElement2.style.backgroundColor = '#F5F5F5';
var svgContainer = document.getElementById('yonet202403Mid_Output01');
var svgContainer = document.getElementById('yonet202403Mid_Output01');
var svgElement = svgContainer.querySelector('svg');
var svgElement = svgContainer.querySelector('svg');


674行目: 742行目:
newText.setAttribute('fill', 'black');
newText.setAttribute('fill', 'black');
newText.textContent = 'TestTitle - subTitle - lyrics: yo-net, music: yo-net';
newText.textContent = 'TestTitle - subTitle - lyrics: yo-net, music: yo-net';
svgElement.appendChild(newText);
svgElement.appendChild(newText);


var newText2 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
var newText2 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
685行目: 751行目:
newText2.setAttribute('fill', 'black');
newText2.setAttribute('fill', 'black');
newText2.textContent = 'Vocal';
newText2.textContent = 'Vocal';
var textGroup2 = document.createElementNS('http://www.w3.org/2000/svg', 'g');
var textGroup2 = document.createElementNS('http://www.w3.org/2000/svg', 'g');
textGroup2.setAttribute('transform', 'rotate(-90 ' + Instrument1.x + ' ' + Instrument1.y + ')');
textGroup2.setAttribute('transform', 'rotate(-90 ' + Instrument1.x + ' ' + Instrument1.y + ')');
textGroup2.appendChild(newText2);
textGroup2.appendChild(newText2);
svgElement.appendChild(textGroup2);
svgElement.appendChild(textGroup2);


700行目: 763行目:
newText3.setAttribute('fill', 'black');
newText3.setAttribute('fill', 'black');
newText3.textContent = 'E.Guitar';
newText3.textContent = 'E.Guitar';
var textGroup3 = document.createElementNS('http://www.w3.org/2000/svg', 'g');
var textGroup3 = document.createElementNS('http://www.w3.org/2000/svg', 'g');
textGroup3.setAttribute('transform', 'rotate(-90 ' + Instrument2.x + ' ' + Instrument2.y + ')');
textGroup3.setAttribute('transform', 'rotate(-90 ' + Instrument2.x + ' ' + Instrument2.y + ')');
textGroup3.appendChild(newText3);
svgElement.appendChild(textGroup3);


textGroup3.appendChild(newText3);
})();
</script>
</syntaxhighlight>
 
 
 なんやかんやありましたが、装飾音符の記事も書ききれそうです。第一弾は2小節目までの描画としました。もうちょっといろいろな装飾音符についても追加で記事にするつもりですが、また後日だな。
 
 
 まずは、GraceNoteというクラスのコンストラクタを使って、StaveNoteと同じ要領で装飾音符の音符部分を定義しておく必要がある。このあとAddModiferの引数として、GraceNoteGroupを指定するのだが、そのGraceNoteGroupの引数の中で予め、定義しておいた以下のようなGraceNoteのオブジェクトgracenotes1のようなものを指定する必要がある。


svgElement.appendChild(textGroup3);


///////////////////////////
<syntaxhighlight lang="javascript">
//直線による描画の例
const gracenotes1 = [
///////////////////////////
  new VF.GraceNote({ keys: ['b/4'], duration: '8', slash: true })
var crescendoX = 900; // X座標
];
var crescendoY = 290; // Y座標
</syntaxhighlight>
var crescendoWidth = 200; // クレシェンドの幅
var crescendoHeight = 20; // クレシェンドの高さ


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


// クレシェンドの線のパスを作成
 あとはStaveNoteで作成した通常の音符のオブジェクトのaddModifer関数を使って、装飾音符の要素であるGraceNoteGroupクラスの値を次のように指定する。notes1[0].addModifier(new VF.GraceNoteGroup(gracenotes1, true), 0);StaveNoteのオブジェクトが持つaddModifier関数は、Accidentalのような臨時記号#やbや♮のようなものを付けるときにも使ったのと同じです。どんなクラスの引数なのかによって動作が変わるようになっています。GraceNoteGroupは以下のような引数をとります。
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);


///////////////////////////
'''第一引数:'''GraceNoteクラスを使った装飾音符のオブジェクトを指定します。内部ではnotes:キーに対応するような値になっていますが、このようなオブジェクト{notes: gracenotes1, slur: true}といった指定は出来ません。
//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"; // デクレシェンド記号


// テキスト要素の作成
'''第二引数:'''スラーマークを付けるかつけないかのboolean値です。true or false です。サンプルのとおり臨時記号を横切るようにスラーが単純にひかれたりするので、複雑な表記のときにあまり役に立たないです。自分でカーブをひいた方が綺麗な楽譜になる場合もありそうです。
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() {
 addModifierの第二引数の数値はStaveNoteのオブジェクトの配列n番目の要素について書かれた和音の何番目に対して追記するものかを指定しますが、和音になっていて数値を指定しても特に変化はなく一番低い音に対してスラーがひかれる感じです。
    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 + ')');
 音符の小節内の配置を調整するFormatterクラスのオブジェクトの関数.formatToStaveを実行するときに予め、Voiceクラスのオブジェクトの関数.setStrictで.setStrict(false)のように設定をしておかないとエラーになってしまいます。つまり、装飾音符を付加すると配置計算を厳密に指定することはできないということです。GraceNoteGroup関数内部からもsetStrictをfalseにしたり、とりあえずVoiceコンストラクタの指定を{ num_beats: 4, beat_value: 4 }にしたりすることを試みるようなプログラムになっている印象でした。


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


})();
 またまたかゆいところに手が届かないVexFlowが露見してますね。小難しいことは対応してるくせに大事なところがいい加減なんだよな。装飾音符と音符の幅も大きいままで変えられないし。ラデツキー行進曲とか装飾音符が最高な曲を美しく描けないなんて淋しいじゃない。
</script>
</syntaxhighlight>




 スラッシュノートはものすごく簡単で、durationの記号の後ろにsをつけるだけです。難しいことはないです。
 最後に、管理人がもっとも美しいと感じたピアノ旋律のTHE BOOMの「からたち野道」のイントロの装飾音符の描き方を示しておきました。これくらいまで、VexFlowの描画技術があれば十分だと思います。装飾音符史上最高の使い方だと思いました。2番目は、JUDY AND MARYの「散歩道」のギターソロの終わりの部分の装飾音符ですね。暫定1位と暫定2位を紹介しておきました。からたちってのはミカン科の植物でトゲのある枝に果実がなります。戦争のある時代の少女が愛する人を想いながら神社へとむかうカラタチのある野道を空を見上げたり、赤い実を食べて、唇を赤くしたりしながら、日々、往復している様子を描いていると思います。


 
 


[[VexFlow 使い方]]に戻る。
[[VexFlow 使い方]]に戻る。

2024年3月26日 (火) 18:24時点における最新版

VexFlow 使い方に戻る。

概要

 いったん。例示の楽譜をリセットしましょう。ごちゃごちゃし過ぎてきましたね。装飾音符簡単じゃなかったな。サンプルと同じようには動かんかった。謎だわ。VexFlow。まだまだ管理人を悩ませおるわ。公式サンプルでは引数をオブジェクトを { notes: gracenotes0, slur: true } のように指定する感じですが、普通にVex.Flow.GraceNoteGroupオブジェクトからそれをするとエラーになります。f = new Factory(なんたら);のような f オブジェクトから動かすとオブジェクトがf.GraceNoteGroup({ notes: gracenotes0, slur: true }) が設定できるみたいですね。f.なんちゃらでずっと操作する方式もあります。2小節目はf.なんちゃらを使う方式を使っています。オススメはしませんが勉強にもなるので、やってみて下さい。こういう書き方もできる!みたいなね。自分はf.オブジェクトをずっと記述していく方法のそれがいやでもあり、Vex.Flow.GraceNoteGroup({ notes: gracenotes0, slur: true })という指定もできてはいいではないかと思い、VexFlowのソースを変更してみたりしました。格闘の様子はこちらの記事から見ることができます。皆さんは、そんなことせずに、順番に第一引数、第二引数と設定して使うのがよいと思います。ただサンプルはあてにならなくなりますけど、理解していれば問題ない。なぜそうなってしまっているのか、はさきほどの記事を理解できたら、わかると思います。普通にVex.Flow.GraceNoteGroupからやる場合は第一引数がnotes:キーの対にあたるオブジェクトを、第二引数にはslur:の対にあたるブール値を設定するといいみたいです。よくよく調べてみるとVex.Flow.Accidental({type:'#'})も同じような仕組みで、このようにオブジェクトを指定すると動きませんでした。第一引数に直接typeの対になる値を記述するとVex.Flow.Accidental('#')のようにして使えるみたいですね。Accidentalはtypeだけしか設定しないので、あまりややこしくないですね。GraceNoteGroupはnotesとslurを設定するので、引数が二個いる感じです。二つ目の引数は省略しても動きます。デフォルトはfalseです。装飾音符のスラーダサいっすね。シャープの上を通過するカーブを描くなんてね。もっと違う方法でカーブが描けないか挑戦する必要がありそう。道のりは長いなコレ。ちょっと離れ過ぎて配置されるのも納得がいかないけどつめる設定方法も無いみたいだし。


 スーパーリロードか履歴を消さないと新しいVexFlowに差し替えた効果は見れないので、ちゃんと楽譜が表示されない人はやってみて、それでもちゃんと表示されない時は、管理人が開発段階なので、不具合を巻き起こしている可能性があります。スーパーリロードのせいが4割、管理人が開発段階で放置して見れないが6割くらいかな。そういう意味ではJavaScriptファイルは差し替える都度ファイル名を変えてキャッシュを使わないようにするのが最も親切なんだろうな。でも全部のファイル名を変えるなんて出来ないし。難しいことになってるなWeb閲覧者側に勘付かせるのも手なのかも知れない。スーパーリロードや履歴って何?という知識も与えなきゃいけない。

 

装飾音符


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


<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,
  Beam,
  Dot,
  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.setMeasure(1);
stave1.draw();

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

const gracenotes1 = [
  new VF.GraceNote({ keys: ['b/4'], duration: '8', slash: true })
];

const notes1 = [
  new StaveNote({ keys: ["c/4", "e/4", "g/4"], duration: "4" }),
  new StaveNote({ keys: ["d/4", "f/4", "a/4"], duration: "4" }),
  new StaveNote({ keys: ["e/4", "g/4", "b/4"], duration: "4" }),
  new StaveNote({ keys: ["f/4", "a/4", "c/5"], duration: "4" })
];
//以下のようなオブジェクト指定はこのサイトだけの特別な記法です。
notes1[0].addModifier(new VF.Accidental({type:'#'}), 0);
//一般的には以下の記述方法を使います。
//.addModifier(new VF.Accidental('#'), 0)

notes1[0].addModifier(new VF.GraceNoteGroup({notes: gracenotes1, slur: true}), 0);
//同じく
//.addModifier(new VF.GraceNoteGroup(gracenotes1, true}), 0)

var stave1Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave1Voice.setStrict(false);
stave1Voice.addTickables(notes1);

var formatter1 = new Vex.Flow.Formatter().joinVoices([stave1Voice]);
formatter1.formatToStave([stave1Voice], stave1);
stave1Voice.draw(ctx, stave1);

//Factoryクラスのオブジェクトfの関数を使う方法
const stave2 = f.Stave({ x: stave1.width + stave1.x, y: stave1.y, width: nStaveWidth });
stave2.setMeasure(2);
stave2.setContext(ctx).draw();

const gracenotes2 = [{ keys: ['d/4'], duration: '8', slash: false }].map(f.GraceNote.bind(f));

const notes2 = [
        f.StaveNote({ keys: ['c/4'], duration: '4', auto_stem: true }),
        f.StaveNote({ keys: ["d/4"], duration: "4" }),
        f.StaveNote({ keys: ["e/4"], duration: "4" }),
        f.StaveNote({ keys: ["f/4"], duration: "4" })
];
notes2[0].addModifier(f.GraceNoteGroup({ notes: gracenotes2, slur: true }), 0);
const stave2Voice = f.Voice({ num_beats: 4, beat_value: 4 }).setStrict(false).addTickables(notes2);
var formatter2 = new VF.Formatter().joinVoices([stave2Voice]);
formatter2.formatToStave([stave2Voice], stave2);
f.draw();//ここはいろいろな記述の最後に一回だけ使うと思う。なので、この先もっとこの方法で小節を描画する場合はそのスクリプトを先に書いて最後に.draw()を実行かな。
//ここまで

const stave3 = new Stave(stave2.width + stave2.x, stave1.y, nStaveWidth).setContext(ctx);
stave3.setMeasure(3);
stave3.draw();


const stave4 = new Stave(stave3.width + stave3.x, stave1.y, nStaveWidth).setContext(ctx);
stave4.setMeasure(4);
stave4.draw();

const notes4 = [
  new StaveNote({ keys: ["c/4", "e/4", "g/4", "b/4"], duration: "4" }),
  new StaveNote({ keys: ["d/4", "f/4", "a/4", "c/5"], duration: "4" }),
  new StaveNote({ keys: ["e/4", "g/4", "b/4", "d/5"], duration: "4" }),
  new StaveNote({ keys: ["f/4", "a/4", "c/5", "e/5"], duration: "4" })
];

var stave4Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave4Voice.addTickables(notes4);

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

stave4Voice.draw(ctx, stave4);

//ここから「からたち野道」のイントロピアノ旋律。装飾音符を使う暫定1位。
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.addKeySignature('E').setContext(ctx);
stave5.setMeasure(5);
stave5.draw();

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

const gracenotes5 = [
  new VF.GraceNote({ keys: ['f/5'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['g/5'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['b/5'], duration: '16', slash: false }),
];

const notes5 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["c/6"], duration: "2", stem_direction: -1 })
];

notes5[2].addModifier(new VF.GraceNoteGroup(gracenotes5, true).beamNotes(), 0);

var stave5Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave5Voice.setStrict(false);
stave5Voice.addTickables(notes5);

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

stave5Voice.draw(ctx, stave5);

function dotted(staveNote, noteIndex = -1) {
  if (noteIndex < 0) {
    Dot.buildAndAttach([staveNote], {
      all: true
    });
  } 
  else {
    for(noteIndexValue of noteIndex){
      Dot.buildAndAttach([staveNote], {
        index: noteIndexValue
      });
    }
  }
  return staveNote;
}

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

const notes6 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1  }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1  }),
  dotted(new StaveNote({ keys: ["f/5"], duration: "4d", stem_direction: -1  })),
  new StaveNote({ keys: ["e/5"], duration: "16", stem_direction: -1  }),
  new StaveNote({ keys: ["f/5"], duration: "16", stem_direction: -1  })
];
const beam6 = new VF.Beam([notes6[3],notes6[4]]);

var stave6Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave6Voice.addTickables(notes6);

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

stave6Voice.draw(ctx, stave6);
beam6.setContext(ctx).draw();

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

const gracenotes7 = [
  new VF.GraceNote({ keys: ['e/6'], duration: '16', slash: false }),
  new VF.GraceNote({ keys: ['f/6'], duration: '16', slash: false })
];

const notes7 = [
  new StaveNote({ keys: ["g/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["b/5"], duration: "4", stem_direction: -1 }),
  new StaveNote({ keys: ["g/6"], duration: "2", stem_direction: -1 })
];

notes7[2].addModifier(new VF.GraceNoteGroup(gracenotes7, true).beamNotes(), 0);

var stave7Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave7Voice.setStrict(false);
stave7Voice.addTickables(notes7);

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

stave7Voice.draw(ctx, stave7);

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

const notes8 = [
  new StaveNote({ keys: ["g/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["g/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["f/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["e/6"], duration: "8", stem_direction: -1  }),
  new StaveNote({ keys: ["e/6"], duration: "2", stem_direction: -1  })
];

const beam8_1 = new VF.Beam([notes8[0],notes8[1]]);
const beam8_2 = new VF.Beam([notes8[2],notes8[3]]);

var stave8Voice = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4 });
stave8Voice.addTickables(notes8);

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

stave8Voice.draw(ctx, stave8);
beam8_1.setContext(ctx).draw();
beam8_2.setContext(ctx).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.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);

})();
</script>


 なんやかんやありましたが、装飾音符の記事も書ききれそうです。第一弾は2小節目までの描画としました。もうちょっといろいろな装飾音符についても追加で記事にするつもりですが、また後日だな。


 まずは、GraceNoteというクラスのコンストラクタを使って、StaveNoteと同じ要領で装飾音符の音符部分を定義しておく必要がある。このあとAddModiferの引数として、GraceNoteGroupを指定するのだが、そのGraceNoteGroupの引数の中で予め、定義しておいた以下のようなGraceNoteのオブジェクトgracenotes1のようなものを指定する必要がある。


const gracenotes1 = [
  new VF.GraceNote({ keys: ['b/4'], duration: '8', slash: true })
];


 あとはStaveNoteで作成した通常の音符のオブジェクトのaddModifer関数を使って、装飾音符の要素であるGraceNoteGroupクラスの値を次のように指定する。notes1[0].addModifier(new VF.GraceNoteGroup(gracenotes1, true), 0);StaveNoteのオブジェクトが持つaddModifier関数は、Accidentalのような臨時記号#やbや♮のようなものを付けるときにも使ったのと同じです。どんなクラスの引数なのかによって動作が変わるようになっています。GraceNoteGroupは以下のような引数をとります。


第一引数:GraceNoteクラスを使った装飾音符のオブジェクトを指定します。内部ではnotes:キーに対応するような値になっていますが、このようなオブジェクト{notes: gracenotes1, slur: true}といった指定は出来ません。


第二引数:スラーマークを付けるかつけないかのboolean値です。true or false です。サンプルのとおり臨時記号を横切るようにスラーが単純にひかれたりするので、複雑な表記のときにあまり役に立たないです。自分でカーブをひいた方が綺麗な楽譜になる場合もありそうです。


 addModifierの第二引数の数値はStaveNoteのオブジェクトの配列n番目の要素について書かれた和音の何番目に対して追記するものかを指定しますが、和音になっていて数値を指定しても特に変化はなく一番低い音に対してスラーがひかれる感じです。


 音符の小節内の配置を調整するFormatterクラスのオブジェクトの関数.formatToStaveを実行するときに予め、Voiceクラスのオブジェクトの関数.setStrictで.setStrict(false)のように設定をしておかないとエラーになってしまいます。つまり、装飾音符を付加すると配置計算を厳密に指定することはできないということです。GraceNoteGroup関数内部からもsetStrictをfalseにしたり、とりあえずVoiceコンストラクタの指定を{ num_beats: 4, beat_value: 4 }にしたりすることを試みるようなプログラムになっている印象でした。


 またまたかゆいところに手が届かないVexFlowが露見してますね。小難しいことは対応してるくせに大事なところがいい加減なんだよな。装飾音符と音符の幅も大きいままで変えられないし。ラデツキー行進曲とか装飾音符が最高な曲を美しく描けないなんて淋しいじゃない。


 最後に、管理人がもっとも美しいと感じたピアノ旋律のTHE BOOMの「からたち野道」のイントロの装飾音符の描き方を示しておきました。これくらいまで、VexFlowの描画技術があれば十分だと思います。装飾音符史上最高の使い方だと思いました。2番目は、JUDY AND MARYの「散歩道」のギターソロの終わりの部分の装飾音符ですね。暫定1位と暫定2位を紹介しておきました。からたちってのはミカン科の植物でトゲのある枝に果実がなります。戦争のある時代の少女が愛する人を想いながら神社へとむかうカラタチのある野道を空を見上げたり、赤い実を食べて、唇を赤くしたりしながら、日々、往復している様子を描いていると思います。

 

VexFlow 使い方に戻る。