File size: 2,346 Bytes
f0a176a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Midi } from '@tonejs/midi';

/**
 * Generate a sample "Twinkle Twinkle Little Star" MIDI with both hands.
 */
export function generateSampleMidi() {
  const midi = new Midi();

  // Right hand melody (MIDI >= 60)
  const rhTrack = midi.addTrack();
  rhTrack.name = 'Right Hand';

  // C C G G A A G - F F E E D D C
  // Then: G G F F E E D - G G F F E E D
  const melody = [
    // Phrase 1
    60, 60, 67, 67, 69, 69, 67,
    // Phrase 2
    65, 65, 64, 64, 62, 62, 60,
    // Phrase 3
    67, 67, 65, 65, 64, 64, 62,
    // Phrase 4
    67, 67, 65, 65, 64, 64, 62,
    // Phrase 5 (repeat phrase 1)
    60, 60, 67, 67, 69, 69, 67,
    // Phrase 6 (repeat phrase 2)
    65, 65, 64, 64, 62, 62, 60,
  ];
  const durations = [
    0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1,
    0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1,
    0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1,
    0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1,
    0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1,
    0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1,
  ];

  let t = 0;
  melody.forEach((note, i) => {
    rhTrack.addNote({
      midi: note,
      time: t,
      duration: durations[i] * 0.9,
      velocity: 0.8,
    });
    t += durations[i];
  });

  // Left hand accompaniment (MIDI < 60)
  const lhTrack = midi.addTrack();
  lhTrack.name = 'Left Hand';

  const chords = [
    // Phrase 1: C major
    { notes: [48, 52, 55], time: 0, dur: 2 },
    { notes: [48, 52, 55], time: 2, dur: 1 },
    // Phrase 2: F major -> C major
    { notes: [41, 45, 48], time: 3, dur: 2 },
    { notes: [48, 52, 55], time: 5, dur: 1 },
    // Phrase 3: C -> G -> Am -> F
    { notes: [48, 52], time: 7, dur: 1 },
    { notes: [43, 47], time: 8, dur: 1 },
    { notes: [45, 48], time: 9, dur: 1 },
    // Phrase 4: same
    { notes: [41, 45], time: 10, dur: 1 },
    { notes: [48, 52], time: 11, dur: 1 },
    { notes: [43, 47], time: 12, dur: 1 },
    { notes: [45, 48], time: 13, dur: 1 },
    // Phrase 5
    { notes: [48, 52, 55], time: 14, dur: 2 },
    { notes: [48, 52, 55], time: 16, dur: 1 },
    // Phrase 6
    { notes: [41, 45, 48], time: 17, dur: 2 },
    { notes: [48, 52, 55], time: 19, dur: 2 },
  ];

  chords.forEach((chord) => {
    chord.notes.forEach((note) => {
      lhTrack.addNote({
        midi: note,
        time: chord.time,
        duration: chord.dur * 0.9,
        velocity: 0.6,
      });
    });
  });

  return midi;
}