是否可以对base64编码的midi数据使用出色的MidiConvert

我的数据如下所示:



我想在浏览器中执行此操作。

最佳答案

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

live example

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==");


输出:

{
  "header": {
    "PPQ": 480,
    "timeSignature": [
      4,
      4
    ],
    "bpm": 120,
    "name": ""
  },
  "startTime": 0.10520833333333333,
  "duration": 2.8406249999999984,
  ...

10-08 11:00