2014-02-26 154 views
3

我使用這個MIDI.js庫:https://github.com/mudcube/MIDI.js如何轉置MIDI文件?

要加載的插件和播放MIDI文件,我這樣做:

window.onload = function() { 
    MIDI.loadPlugin({ 
     soundfontUrl: "./soundfont/", 
     instruments: [ "acoustic_grand_piano" ], 
     callback: function() { 
      MIDI.programChange(0, 0); 
        _player = MIDI.Player; 

     } 
    }); 

};

function playSong(){    
     _player.timeWarp = 1; // speed the song is played back 
     _player.loadFile(song[songid], _player.start); 

     _player.addListener(function(data) { 
      var now = data.now; // where we are now 
      var end = data.end; // time when song ends 
      var channel = data.channel; // channel note is playing on 
      var message = data.message; // 128 is noteOff, 144 is noteOn 
      var note = data.note; // the note 
      var velocity = data.velocity; // the velocity of the note 


     }); 
} 

var songid = 0; 
var song = ['data:audio/mid;base64,TVRoZAAAAA... 

我的問題是,無論如何轉置這個midi文件之前玩嗎?基本上我想解析一個MIDI文件(無論是.mid文件還是base64格式),將所有音符改爲+1,然後發送給播放器。任何方式在JavaScript中做到這一點?

回答

0

這不是一個完整的答案,但如果您仍然卡住,它可能會指向正確的方向。它基於花費幾分鐘時間查看MIDI.js的源代碼以及它構建的幾個軟件包。我假設你想要在讀取文件時應用轉置,並且對保存轉置文件不感興趣。

  1. addListener方法不太可能有用。它看起來像是在音符發送到合成器後立即發生的回調,即改變音高太遲。
  2. 您將需要找到將傳入文件字符轉換爲javascript數組元素的函數。這可能在jasmid庫中。
  3. 如果你真的很幸運,開發人員將提供一個鉤子,你可以使用它來設置回調,以便在函數將它們寫入數組之前修改事件屬性。否則,您需要相應地修改該功能。如果你能使它運行良好,試着說服開發人員將其修補到源代碼中 - 否則你會被困在維護自己的圖書館分支。

希望這有助於。

編輯:我想你想要的文件是https://github.com/gasman/jasmid/blob/master/midifile.js。你可以運用換位在處理音符開事件起始於線的情況下155

case 0x09: 
    event.noteNumber = param1; 
    event.velocity = stream.readInt8(); 
    if (event.velocity == 0) { 
     event.subtype = 'noteOff'; 
    } else { 
     event.subtype = 'noteOn'; 
    } 
    return event; 

,或者,當之前返回事件被推到軌道數組以線227

while (!trackStream.eof()) { 
    var event = readEvent(trackStream); 
    tracks[i].push(event);