user2887621
user2887621

Reputation: 11

Webaudio api: change the sample rate

Is it possible to change the sampling rate of the recorded wave file without using third-party software and websites , and in the js? If the recorder.js set the frequency of 44100

worker.postMessage ({
      command: 'init',
      config: {
        sampleRate: 44100
      }
} ) ;

 is written with the same frequency , and if you reduce it to 22050 , the length of the file will be 2 times more recorded and will be slow to reproduce, while increasing the speed of playback , the recording will sound fine.Actually the question whether it is possible to change the sample rate already contain files and how to do it?

Upvotes: 1

Views: 2053

Answers (2)

Seph Reed
Seph Reed

Reputation: 11028

For anyone interested... Because typed arrays are transferables, you can send them to a web worker, and down sample, then send it back or to a server or wherever.

    //get audio from user and send it to a web worker
    function recordUser(argument) {
            //
        var audioCtx = new AudioContext();
        var worker = new Worker('downsampler.js');


        // Create a ScriptProcessorNode with a bufferSize of 512 and a single input and no output channel
        var scriptNode = audioCtx.createScriptProcessor(512, 1, 0);
        console.log(scriptNode.bufferSize);

        // Give the node a function to process audio events
        scriptNode.onaudioprocess = function(audioProcessingEvent) {
            var inputBuffer = audioProcessingEvent.inputBuffer;
            console.log(inputBuffer.getChannelData(0));
            worker.postMessage(inputBuffer.getChannelData(0)); 
        }

        
        navigator.mediaDevices.getUserMedia({ audio: true })
        .then(function(mediaStream) {
            var mediaStreamSource = audioCtx.createMediaStreamSource(mediaStream);
            mediaStreamSource.connect(scriptNode);
        })
        .catch(function(err) { console.log(err.name + ": " + err.message); });
    }

The web worker is something like this. If you want to send it to a server, use a websocket. Otherwise, use post message to transfer the data back to the client. You'll need to add an event listener client side as well, so search "mdn WebWorker" to read up on that.

    //example worker that sends the data to both a web socket and back to the user
    var ws = new WebSocket('ws://localhost:4321');
    ws.binaryType = 'arraybuffer';

    self.addEventListener('message', function(e) {
        var data = e.data;


        var sendMe = new Float32Array(data.length/16);
        for(var i = 0; i * 16 < data.length; i++) {
            sendMe[i] = data[i*16];
        }


        //send to server
        ws.send(sendMe);

        //or send back to user
        self.postMessage(sendMe)

    }, false);

Upvotes: 0

Michaela.Merz
Michaela.Merz

Reputation: 153

The only way I found so far is a small resample library xaudio.js, part of the speex.js library. Works pretty nice. I use it to convert audio from native format to 8Khz Mono.

Upvotes: 1

Related Questions