Let's say we capture audio input from microphone that is 2 channel, 44100 sample rate.
In order to avoid resampling the input data to 48000 we set the sample rate of encoder configuration to 44100.
The problem is that there is an audible pop when playback with Media Source Extensions commences, and the audio playback completes 1 second before it is suppoed to.
    let timestamp = 0
     , array = []
     , chunks = []
     , config = {
     numberOfChannels: 2,
     sampleRate: 44100,
     codec: 'opus'
    };
    for (let i = 0; i < wav.size; i += 441 * 4 * 6) {
     const int16 = new Int16Array(await wav.slice(i, i + 441 * 4 * 6).arrayBuffer());
     //Â
https://stackoverflow.com/a/35248852Â Â Â Â Â const channels = [new Float32Array(441 * 6), new Float32Array(441 * 6)];
     for (let i = 0, j = 0, n = 1; i < int16.length; i++) {
      const int = int16[i];
      // If the high bit is on, then it is a negative number, and actually counts backwards.
      const float = int >= 0x8000 ? -(0x10000 - int) / 0x8000 : int / 0x7fff;
      // deinterleave
      channels[(n = ++n % 2)][!n ? j++ : j - 1] = float;
     }
     const data = new Float32Array(441 * 2 * 6);
     const left = new Float32Array(channels.shift());
     const right = new Float32Array(channels.shift());
     data.set(left, 0);
     data.set(right, left.length);
     const frame = new AudioData({
      timestamp,
      data,
      sampleRate: 44100,
      format: 'f32-planar',
      numberOfChannels: 2,
      numberOfFrames: 441 * 6,
     });
     timestamp += frame.duration;
     array.push(frame);
    }
    console.log(array);
    const encoder = new AudioEncoder({
     error(e) {
      console.log(e);
     },
     output: async(chunk,metadata)=>{
      if (metadata.decoderConfig) {
       config.description = metadata.decoderConfig.description;
      }
      chunks.push(chunk);
     }
     ,
    });
    console.log(await AudioEncoder.isConfigSupported(config));
    encoder.configure(config);
    for (const audioData of array) {
     encoder.encode(audioData);
    }
    await encoder.flush();
    console.log(chunks);
    const audio = new Audio();
    audio.controls = true;
    audio.addEventListener('canplay', async(e)=>{
       await
audio.play();
    }, {once: true});
No resampling Web API exists that I am aware of.
How to get rid of the initial pop when playback commences and playback the last 1 second of audio when input AudioData and configuration to AudioEncoder is 44100?Â
Or is resampling to 48000 input necessary to avoid the pop at initial playback and 1 second less playback duration?