Feroz Siddiqui
Feroz Siddiqui

Reputation: 4096

Audio Worklet - How to capture microphone streams

I am trying to capture live microphone streams from my desktop, When converting float 32 bits to Int16 Array all values are coming 0. Not sure what i have done wrong here. how can i capture the live microphone stream in audio worklet?

Below is javascript code:

    try {
        navigator.getUserMedia = navigator.getUserMedia
                || navigator.webkitGetUserMedia
                || navigator.mozGetUserMedia;
    
        microphone = navigator.getUserMedia({
            audio : true,
            video : false
        }, onMicrophoneGranted, onMicrophoneDenied);
    } catch (e) {
        alert(e)
    }
    async function onMicrophoneGranted(stream) {
        console.log(stream)
        context = new AudioContext();
        source = context.createMediaStreamSource(stream);
        await context.audioWorklet.addModule('/assets/js/buffer-detector.js');
        // Create our custom node.
        bufferDetectorNode= new AudioWorkletNode(context, 'buffer-detector');
        bufferDetectorNode.port.onmessage = (event) => {
          
          };
        
        source.connect(bufferDetectorNode);
        bufferDetectorNode.connect(context.destination);
         //source.connect(context.destination);
    }
function onMicrophoneDenied() {
    console.log('denied')
}

Below is AudioWorklet

class BufferProcessor extends AudioWorkletProcessor {
    
  
  
process (inputs) {
    console.log(inputs)
    inputs.forEach(floatArray => {
    floatArray.forEach(elem => {
     const intArray = Int16Array.from(elem)
    console.log(intArray)
})
})
       //const input = inputs[0];

    //this.append(input[0])

    return true;
  }


  static get parameterDescriptors() {
    return [{
      name: 'Buffer Detector',
    }]
  }

  constructor() {
    super();
    this.initBuffer()
  }
}



registerProcessor('buffer-detector',BufferProcessor );

Upvotes: 2

Views: 660

Answers (2)

Stefano Bider
Stefano Bider

Reputation: 312

I didn't really dig through your code, but it looks ok from a pure WebAudio API perspective. However, if your audio is all 0s after conversion to 16bits, the problem is just the conversion itself. Float32 samples are values between 0 and 1, so when you convert the array, values end up all rounded to 0 (plus perhaps the occasional 1). You need to scale the samples before converting. Int16 audio samples should be between -32768 and 32767, so simply multiply every sample by 32767 and then convert to Int16 (more precisely: multiply nagative values by -32768 and positive values by 32767).

Upvotes: 0

user14185
user14185

Reputation: 49

Your mic might be off, if its a macbook connected to a monitor make sure its open.

Here's a very comprehensive implementation of sending PCM audio with an audioworklet module: https://github.com/microsoft/cognitive-services-speech-sdk-js/blob/6848253d1d9b619dd497ce302ee1ee6212f56b20/src/common.browser/PCMRecorder.ts#L7

Upvotes: -1

Related Questions