微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从getChannelData编码回音频

如何解决从getChannelData编码回音频

调用getChannelData并执行一些操作,并从Float32Array中删除值。

如何将这些数据编码回可以保存的形式?

const blob = new Blob(this.chunks,{ type: audioType });
// generate audio url from blob
const audioContext = new (window.AudioContext ||
  window.webkitaudiocontext)();
// reading the file with file reader using a method that uses read file in a promise 
ReadFile(blob).then((arrayBuffer) => {
  audioContext.decodeAudioData(arrayBuffer).then((audioBuffer) => {
    const audioBufferSourceNode = audioContext.createBufferSource();
    const numChannels = audioBuffer.numberOfChannels;
    const leftChannelArray = audioBuffer.getChannelData(0);
    // audioBufferSourceNode.buffer = leftChannelArray;
    let rightChannelArray;
    if (numChannels>1) {
      rightChannelArray = audioBuffer.getChannelData(1);
    }
    const monoChannelTrimmed = trimsilence(leftChannelArray,rightChannelArray) //we look on both sides for silence,we delete the array values and merge the channels
    //Now i want to turn monoChannelTrimmed into a usable audio file

我一直在努力地将这个渠道改回可用的渠道。我已经尝试过该领域其他问题的一些建议,例如Converting Float32Array to Uint8Array while Preserving IEEE 754 Representation,但是如果有人有建议,我将非常渴望尝试。

解决方法

您可能可以使用MediaStream Recording API

以下是如何使用它的一小段代码,大部分取自示例,但已修改为使用WebAudio OscillatorNode作为源。您可以将AudioBufferSourceNode替换为monoTrimmedChannel

let c;
let s;
let d;
let mediaRecorder;

let recordedChunks = [];

function handleDataAvailable(event) {
  console.log("data-available");
  if (event.data.size > 0) {
    recordedChunks.push(event.data);
    download();
  } else {
    // ...
  }
}

function download() {
  let blob = new Blob(recordedChunks,{
    type: "video/webm"
  });
  let url = URL.createObjectURL(blob);
  let a = document.createElement("a");
  document.body.appendChild(a);
  a.style = "display: none";
  a.href = url;
  a.download = "test.mp3";
  a.click();
  window.URL.revokeObjectURL(url);
}

setTimeout(event => {
  console.log("stopping");
  mediaRecorder.stop();
},9000);

function start() {
  console.log("start");
  c = new AudioContext();
  s = new OscillatorNode(c);
  d = new MediaStreamAudioDestinationNode(c);
  s.connect(d);

  mediaRecorder =  new MediaRecorder(d.stream,{
    mimeType:  "audio/webm"
  });
  mediaRecorder.ondataavailable = handleDataAvailable;

  s.start();
  mediaRecorder.start();
}

我在本地进行了测试,它创建了一个test.webm文件,该文件可以按预期播放漂亮的振荡器音。您可能需要调整一些内容。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。