chrisguttandin / extendable-media-recorder

An extendable drop-in replacement for the native MediaRecorder.
MIT License
258 stars 13 forks source link

Want to set sample rate from 48khz to 16khz. #674

Closed VijayDhake closed 1 year ago

VijayDhake commented 1 year ago

We are facing audio signal drop out issue in audio recording in chrome browser. But after using [extendable-media-recorder] that issue resolved. Thanks for it. But recorded audio is of 48khz but we want 16khz. Is there any way to set it. Please let me know. Thanks in advance.

chrisguttandin commented 1 year ago

Hi @VijayDhake,

there is no way to do this directly with a MediaRecorder. But it gets asked quite frequently and therefore I've added a section to the README which describes how it can be done with an AudioContext. I hope this helps.

VijayDhake commented 1 year ago

Hi @chrisguttandin,

As you suggest, I have tried the following code to resample the audio.

const audioContext = new AudioContext({ sampleRate: 16000 });
const mediaStreamAudioSourceNode = new MediaStreamAudioSourceNode(audioContext, { mediaStream: stream });
const mediaStreamAudioDestinationNode = new MediaStreamAudioDestinationNode(audioContext);

mediaStreamAudioSourceNode.connect(mediaStreamAudioDestinationNode);

const mediaRecorder = new MediaRecorder(mediaStreamAudioSourceNode.stream);

Above code is not working for me But If I change const mediaRecorder = new MediaRecorder(mediaStreamAudioSourceNode.stream); to const mediaRecorder = new MediaRecorder(mediaStreamAudioDestinationNode.stream, { mimeType: 'audio/wav' }); works fine for me. It is giving me a output 16khz, 16bit, 2 channel, But I require 16khz, 16bit, 1 channel. I have tried many ways to change it but it is not changing. There is any way to set channel. Please let me know. Thanks in advance.

chrisguttandin commented 1 year ago

Thanks for making me aware of the typo. I've fixed the readme. https://github.com/chrisguttandin/extendable-media-recorder/commit/66b9451cd14384dd66902c8f014f30928c954100

I have tried many ways to change it but it is not changing.

What exactly did you try and didn't work?

I think setting the channelCount of the MediaStreamAudioDestinationNode should work.

const mediaStreamAudioDestinationNode = new MediaStreamAudioDestinationNode(
    audioContext,
    {
        channelCount: 1
    }
);
VijayDhake commented 1 year ago
        let stream = await navigator.mediaDevices.getUserMedia({ audio: true });

            gumStream = stream;

          const audioContext = new 
![console](https://user-images.githubusercontent.com/67089587/224911604-ae2c786e-9ef2-42f5-b318-d7ef5d5520cd.png)
AudioContext({ sampleRate: 16000 });    
          const mediaStreamAudioSourceNode = new MediaStreamAudioSourceNode(audioContext, { mediaStream: stream });
          //console.log("mediaStreamAudioSourceNode", mediaStreamAudioSourceNode)

          const mediaStreamAudioDestinationNode = new MediaStreamAudioDestinationNode(audioContext);
          //console.log("mediaStreamAudioDestinationNode", mediaStreamAudioDestinationNode)

          mediaStreamAudioSourceNode.connect(mediaStreamAudioDestinationNode);

          recorder = new MediaRecorder(mediaStreamAudioDestinationNode.stream, { mimeType: 'audio/wav', channelCount: 1 });
          recorder.start();

Have already added and tried above solution. But Still output showing 2 channel. Attached screenshot of output.

console mediainfo

chrisguttandin commented 1 year ago

Could you please try again with the latest version. I hope the fix for #655 fixed this as well.