简体   繁体   中英

Javascript Web Audio API AnalyserNode Not Working when using AudioBufferSource

I'm trying to build an "offline" benchmark version of a VAD algorithm I'm working on. in the online version I'm using createMediaStreamSource for input to an analyser node and it works perfectly fine. at the offline version i want to load and split a recorded audio file, so I'm using an xhr to load the file as ArrayBuffer and then splitting it (so it will simulate an audio stream) and using it as a source createBufferSource.

this is the code for splitting the audioBuffer:

let audio_dur = audioBuffer.duration;

  let segments_num = Math.ceil(audio_dur / segment_dur);
  let segment_length = Math.ceil(audioBuffer.length / segments_num);
  segmentsArr = new Array(segments_num);

  let AudioData = new Float32Array(audioBuffer.length);
  AudioData = audioBuffer.getChannelData(0);


  for (let i = 0; i <= segments_num-1; i++){
    segmentsArr[i] = AudioData.slice(i*segment_length,(i+1)*segment_length-1);
  }

then, the part for connecting it to the analyser:

const analyser = audioCtx.createAnalyser();
analyser.minDecibels = min_decibels;
analyser.fftSize = fft_size;

const T_data = new Float32Array(analyser.fftSize);
const F_data = new Uint8Array(analyser.frequencyBinCount);

let segments_num = segmentsArr.length;
let segment_length = segmentsArr[1].length;

var cur_Buffer = audioCtx.createBuffer(1, segment_length, audioCtx.sampleRate);

for (let segment_ind = 0; segment_ind <= segments_num-1; segment_ind++) {
  let cur_segment = segmentsArr[segment_ind];
  cur_Buffer.copyToChannel(cur_segment,0,0);

  let cur_source = audioCtx.createBufferSource();
  cur_source.loop = false;
  cur_source.buffer = newBuffer;

  cur_source.connect(analyser);

  analyser.getByteFrequencyData(F_data); // get current data
  analyser.getFloatTimeDomainData(T_data); // get current data
  ...

and the code goes on.

PROBLEM IS: the time data and frequency data returned from the analyser are always empty.

before asked: 1. minDecibels is at -100Db (lowest possible). 2. the segmentsArr is not empty and I'm able to play it segment-by-segment, using the exact same way for creating AudioBufferSource and then connecting it to audio destination.

ANSWERED: Thanks to @cwilso, the problem was I haven't used cur_source.start at each for every new source, thanks a lot.

It's hard to see from this code exactly how this is connected and the code is starting.

1) You're calling start() on the buffer source nodes, right? 2) You're calling getByteFrequencyData(), etc after that start happens? 3) You do hear the buffer chunks being played through the destination?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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