Recording Google Chrome audio stutters on CPU usage when using chrome.tabCapture.capture

Viewed 338

So I'm trying to record some audio on google chrome through an extension. I'm doing it this way since I can't get the audio stream by other means and windows doesn't let me record an individual process without a virtual audio cable.

I noticed that all the recordings have micro stutters that align with my computer's CPU usage. The stutters only exist when capturing the audio is enabled. It doesn't happen when its playing back normally on the web page.

In order to exacerbate the micro stutters into big noticeable stutters. I maxed out my CPU with some while loops threads doing float calculations. The moment I do that, you can hear the stutters.

You can hear the issue clearly here:

https://cdn.discordapp.com/attachments/462271295774654484/692816020011745320/Ruggero_Leoncavallo_-_Pagliacci_Act_1_Vesti_la_giubba_-_Live.mp3

Any attempt to set the audio constraint with an exact value besides 0.01 fails in the promise. I tried using an AudioContext hoping it could maybe back log the buffer or let me decrease the latency more. Neither worked. Increasing the latency just makes it stutter without maxing out my CPU. How can I prevent this stuttering issue while recording? Are there any alternatives?

chrome.tabCapture.capture(
{
    audio: true,
    audioConstraints:
    {
        mandatory:
        {
            chromeMediaSource: 'tab'
        }
    }
},
(stream) =>
{  
    console.log("Started recording, stream: ", stream);

    // Latency doesn't go lower than 0.01
    // higher latency makes stronger stutters

    var ctx = new AudioContext({latencyHint:0.0000});
    var source = ctx.createMediaStreamSource(stream);
    var dest = ctx.createMediaStreamDestination();

    console.log("Con", ctx, source, dest);

    mediaRecorder = new MediaRecorder(dest.stream,
    {
        mimeType:"audio/webm"
    });

    source.connect(dest);

    console.log("Recorder:" , mediaRecorder);


    var chunks = [];

    mediaRecorder.ondataavailable = function (blob)
    {
        console.log("data available", blob);

        chunks.push(blob.data);
    };

    mediaRecorder.onstop = function(e) 
    {
        console.log("Stopped recording");

        var blob = new Blob(chunks,  { 'type' : 'audio/webm' });

        console.log("My blob is: ", blob);

        var audioURL = window.URL.createObjectURL(blob);

        chrome.downloads.download({url: audioURL, filename: "test.webm", saveAs: false}, function(downloadId)
        {
            console.log("finished downloading it");
        });
    }

    // Playing it back so I can hear the stutters while its recording 
    let audio = new Audio();
    audio.srcObject = dest.stream;
    audio.play();

    // changing the batch duration doesn't matter
    mediaRecorder.start(1000);
});
0 Answers
Related