Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate negative audio waves from input sound in javascript?

I found a javascript that captures the current microphone input just to send it out again. You can see it here:

https://codepen.io/MyXoToD/pen/bdb1b834b15aaa4b4fcc8c7b50c23a6f?editors=1010 (only works with https).

I was wondering how I can generate the "negative" waves of the sound captured. Just like "noise cancellation" works. So the script detects the current noise around me and whenever there is a wave going up I want to generate a wave that is going down to cancel each other out. Is there a way to do this in javascript?

So this outputs the current sound recored:

var input = audio_context.createMediaStreamSource(stream);
volume = audio_context.createGain();
volume.gain.value = 0.8;
input.connect(volume);
volume.connect(audio_context.destination);

The question is how to produce the negative of this sound and output this instead.

like image 612
MyXoToD Avatar asked Nov 09 '22 12:11

MyXoToD


1 Answers

You're already streaming your signal through a Web Audio chain. What you need to do is chain an AudioWorker node between the input and output nodes (after or before the gain node, doesn't matter). The script for the worker would be a very simple sign inversion.

var negationWorker = audio_context.createAudioWorker( "negate.js" )
var negationNode = negationWorker.createNode()
input.connect(negationNode)
negationNode.connect(volume)
like image 76
Touffy Avatar answered Nov 14 '22 23:11

Touffy