Skip to content

Instantly share code, notes, and snippets.

@wararyo
Created February 9, 2019 19:26
Show Gist options
  • Save wararyo/bd5ff242c6ee1ee9831d3e98f1206018 to your computer and use it in GitHub Desktop.
Save wararyo/bd5ff242c6ee1ee9831d3e98f1206018 to your computer and use it in GitHub Desktop.
MIDIファイルをCHUNITHM.sunaba.txtで使える音楽データに変換します。
var resolution = 192 // MIDIファイルの分解能 四分音符の長さ
var sunaba_spanY = 1152 // Sunabaでの一小節の長さ
var midiFileParser = require('midi-file-parser');
var file = require('fs').readFileSync('Music2.mid', 'binary')
var midi = midiFileParser(file);
var Generate = function(noteList){
//velからattenを計算
noteList.forEach((note, i) => {
atten = Math.round(1000 / note.vel)
time = Math.round(note.time / resolution / 4 * sunaba_spanY)
console.log('addSoundNote('+note.ch+","+time+","+(note.noteNo-12)+","+atten+')')
});
}
var noteList = [];
function Note(ch,time,noteNo,vel,duration){
this.ch = ch
this.time = time
this.noteNo = noteNo
this.vel = vel
this.duration = duration
}
midi.tracks.forEach((trackData, i) => {
currentNoteNo = 0
currentVelocity = 0
currentDeltaTime = 0
absoluteTime = 0
trackData.forEach((event) => {
if(event.subtype == 'noteOn') {
if(currentNoteNo == 0) {//ノートオフの次にノート音が来てたら
currentNoteNo = event.noteNumber
currentVelocity = event.velocity
currentDeltaTime = event.deltaTime
absoluteTime += event.deltaTime
}
}
if(event.subtype == 'noteOff') {
if(currentNoteNo == event.noteNumber) {//ちゃんとノート音の直後に来てたら
//convertToSunaba(event.channel,absoluteTime,currentNoteNo,currentVelocity,event.deltaTime)
noteList.push(new Note(event.channel,absoluteTime,currentNoteNo,currentVelocity,event.deltaTime));
currentNoteNo = 0
currentVelocity = 0
currentDeltaTime = 0
absoluteTime += event.deltaTime
}
}
});
});
noteList.sort(function(a,b){
return a.time - b.time;
});
Generate(noteList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment