从base64编码的Midi转换Midi

时间:2018-12-15 21:09:16

标签: javascript parsing base64 midi

是否可以将出色的MidiConvert与base64编码的midi数据一起使用?

我的数据如下:

data:audio/midi;base64,TVRoZAAAAAYAAQAOAeBNVHJrAAAALwD/Awl...

我想在浏览器中做到这一点。

1 个答案:

答案 0 :(得分:2)

我能够将其与以下方法一起使用,将base64编码的数据转换为Blob,以便可以用FileReader进行读取,从而使其可以与{{ 1}}:

live example

MidiConvert.parse

输出:

dataURItoBlob = dataURI => {
  const byteString = atob(dataURI.split(',')[1]);
  const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

  let ab = new ArrayBuffer(byteString.length);
  let ia = new Uint8Array(ab);
  for (let i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
  }

  return new Blob([ab], {type: mimeString});
};

parseMidiFromDataURI = dataURI => {
  const reader = new FileReader();
  reader.onload = e => {
    const partsData = MidiConvert.parse(e.target.result);
    console.log(JSON.stringify(partsData, undefined, 2));
  };
  reader.readAsArrayBuffer(dataURItoBlob(dataURI));
};

parseMidiFromDataURI("data:audio/midi;base64,TVRoZAAAAAYAAAABAeBNVHJrAAAA5wD/WAQEAhgIAP9RAwehIADAAADAAGWQXH8AwABlgFx/AMAAZZBZfwDAAGWAWX8AwABlkFZ/AMAAZYBWfwDAAGWQQ38AkEF/AJBCfwCQQH8AkD9/AMlPAJlPfwDAAACQU38AwABlgEN/AMAAZYBCfwDAAGWAP38AwABlgFN/AMAAZZBDfwDJTwCZT38AwAAAkFB/AMAAZYBBfwDAAGWJT38AwABliQB/AMAAZcAAZYBAfwDAAGWAUH8AwABlkE1/AMAAZYBDfwDAAGXAAGWATX8AwABlwABlwABlwABlwABlkDh/AP8vAA==");