Changing the sample rate of audio recording in Javascript

Hi I have a working voice recorder on a browser. It implements navigator.getUserMedia and AudioContext It records the sounds in .wav format - then converts to .mp3 and uploads the file.

The file sample rate is set to 44100Hz, and the mp3 output is resampled to 8000Hz. The problem with is the amount of local storage needed to record a large audio file at this sample rate. I am reducing the file size by converting to MP3, sample rate of 8000. I read that the sampleRate is readonly. So i guess the sample rate can be set on instantiation. My question is how can I change the sample rate on an audio recorder with javascript?

nb. the code below has been trimmed to show the parts of interest.

    try{
    window.AudioContext = window.AudioContext || window.webkitAudioContext;
    navigator.getMedia = ( navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
    window.URL = window.URL || window.webkitURL;
    audioCtx = new AudioContext;
    var buffer = audioCtx.createBuffer(1, 8000, 8000);// this does not work!
    }
    catch (e)
    {
        console.log('unable to create a audiocontext');
        exit;
    }
    navigator.getMedia({audio: true}, startUserMedia, function(e) {
                    console.log('No live audio input: ' + e);}
                );
    function startUserMedia(stream) {
        var input = audioCtx.createMediaStreamSource(stream);
        console.log('Media stream created.' );
        console.log("input sample rate " +input.context.sampleRate);
        input.connect(audioCtx.destination);
        console.log('Input connected to audio context destination.');
        recorder = new Recorder(input);
        console.log('Recorder initialised.');
        var mediaStreamSource = audioCtx.createMediaStreamSource(stream);
        var analyser = audioCtx.createAnalyser();
        analyser.smoothingTimeConstant = 0.3;
        analyser.fftSize = 2048;
        var javascriptNode = audioCtx.createScriptProcessor(2048, 1, 1);
        mediaStreamSource.connect(analyser);
        analyser.connect(javascriptNode);
        javascriptNode.connect(audioCtx.destination);
        javascriptNode.onaudioprocess = visualiser(analyser);
    }

Answers:

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.