Spaces:
Sleeping
Sleeping
| 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; | |
| } | |