import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import midiPackage from "../frontend/node_modules/@tonejs/midi/build/Midi.js"; const { Midi } = midiPackage; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "backend", "sample_midi"); const expected = [ ["starlight-steps-beginner.mid", 4], ["starlight-steps-easy.mid", 4], ["evening-waltz-beginner.mid", 3], ["sunrise-arpeggio-easy.mid", 4], ["black-key-pentatonic-beginner.mid", 4], ]; for (const [name, meter] of expected) { const midi = new Midi(fs.readFileSync(path.join(root, name))); const [right, left] = midi.tracks; if (midi.tracks.length !== 2 || !right?.notes.length || !left?.notes.length) { throw new Error(`${name}: 오른손과 왼손 트랙이 각각 필요합니다`); } if (midi.duration < 60 || midi.duration > 90) { throw new Error(`${name}: 곡 길이가 1분에서 1분 30초 사이여야 합니다 (${midi.duration.toFixed(1)}초)`); } if (midi.header.timeSignatures[0]?.timeSignature[0] !== meter) { throw new Error(`${name}: 박자가 일치하지 않습니다`); } if ([...right.notes, ...left.notes].some((note) => note.midi < 21 || note.midi > 108)) { throw new Error(`${name}: 피아노 음역을 벗어났습니다`); } console.log(`${name}: ${midi.duration.toFixed(1)}초, 오른손 ${right.notes.length}음, 왼손 ${left.notes.length}음`); }