简体   繁体   English

如何从音轨中获取 ArrayBuffer?

[英]How to get ArrayBuffer from audio track?

I need to join multiples audio tracks in js and I need to get a ArrayBuffer to do audioContext.decodeAudioData().我需要在 js 中加入多个音轨,我需要一个 ArrayBuffer 来执行 audioContext.decodeAudioData()。 How can I do this?我怎样才能做到这一点?

that's the code:那是代码:

 let tracks = []
  streamsAudio.forEach((track) => {
    let elem = document.createElement('audio')
    elem.srcObject = track.src
    elem.play()

    tracks.push(track.src.getAudioTracks()[0])
  })


 const ac = new AudioContext();
 
ac.decodeAudioData(ArrayBuffer, function(data) {
 })
 
   

    

It's not clear why you need an ArrayBuffer so you can decode the data.不清楚为什么需要ArrayBuffer以便解码数据。 The audio tag can already decode the audio for you.音频标签已经可以为您解码音频。

One way is to attach your audio element to a MediaElementAudioSourceNode .一种方法是将audio元素附加到MediaElementAudioSourceNode Connect this to ScriptProcessorNode or AudioWorkletNode to get the data from the individual tracks.将其连接到ScriptProcessorNodeAudioWorkletNode以从各个轨道获取数据。 A quick sketch (untested):快速草图(未经测试):

// Make elem global instead of local for this to work.  Then
let s = new MediaElementAudiouSourceNode(ac, {mediaElement: elem});
// 8192 is just an example.  You could choose other values
let spn = ac.createScriptProcessorNode(8192, <number of channels>, 0);
spn.onaudioprocess = (e) => {
  let input = e.inputBuffer;
  for (let k = 0; k < input.length; ++k) {
    let channel = input.getChannelData(k);
    // channel has one channel from the tracks.  Process it as you see
    // fit.
  }
}

s.connect(spn).connect(ac.destination);

You will have to determine the number of channels for the script processor.您必须确定脚本处理器的通道数。 It may not be the same as the the number of tracks if a track contains, say, stereo audio.如果轨道包含立体声音频,则它可能与轨道数不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM