The navigator.getUserMedia(....)
call can be used in some modern browser to record audio in Javascript.
Is there a way to adjust/set the input volume level of the microphone?
The setting for this is not always optimal. Sometimes the microphone is set to record only at a very very low input volume. Sure the user could adjust manually the input volume level in his system, but most of my users might lack the knowledge to do this. Therefore it would be best I could adjust the microphone volume dynamically inside of the very JavaScript application which records the data via the "navigator.getUserMedia(....)
" way?
Are there solutions out there to influence this input volume level of the microphone?
You can also adjust microphone input volume using the classic Control Panel. You can launch this tool from the speaker icon in your taskbar’s notification area, which is opposite the Start button. First, right-click the speaker icon and select “Sounds” from the menu that appears. In the “Sound” window that opens, click the “Recording” tab.
First of all, MediaRecorder.start () is used to start recording the stream once the record button is pressed: When the MediaRecorder is recording, the MediaRecorder.state property will return a value of "recording". As recording progresses, we need to collect the audio data.
Select the one you’d like to adjust, then click the “Properties” button. In the “Properties” window that appears, click the “Levels” tab. In the “Levels” tab, use the Microphone slider to adjust the input level of the microphone.
Once getUserMedia has created a media stream successfully, you create a new Media Recorder instance with the MediaRecorder () constructor and pass it the stream directly. This is your entry point into using the MediaRecorder API — the stream is now ready to be captured into a Blob, in the default encoding format of your browser.
The Web Audio API enables you to change the input of the microphone for the stream.
Here is an example, which shows how to convert a MediaStream
to another MediaStream
, but with the ability to change the volume on the fly. If you just want to play back the audio, then you can change gainNode.connect(dest)
to gainNode.connect(ctx.destination)
and remove the two other lines that use the dest
variable.
if (!navigator.getUserMedia) {
navigator.getUserMedia = navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
}
navigator.getUserMedia({
audio: true
}, function(stream) {
var ctx = new AudioContext();
var source = ctx.createMediaStreamSource(stream);
var dest = ctx.createMediaStreamDestination();
var gainNode = ctx.createGain();
source.connect(gainNode);
gainNode.connect(dest);
document.getElementById('volume').onchange = function() {
gainNode.gain.value = this.value; // Any number between 0 and 1.
};
gainNode.gain.value = document.getElementById('volume').value;
// Example: play the audio
// Or if you use WebRTC, use peerConnection.addStream(dest.stream);
new Audio(URL.createObjectURL(dest.stream)).play();
// Store the source and destination in a global variable
// to avoid losing the audio to garbage collection.
window.leakMyAudioNodes = [source, dest];
}, function(e) {
alert(e); // TODO: Handle error.
});
// For the demo only:
document.getElementById('volume').onchange = function() {
alert('Please provide access to the microhone before using this.');
};
Volume: <input type=range id=volume min=0 max=1 value=1 step=0.01>
Note: The live demo via Stack snippet does not work in Chrome because getUserMedia seems non-functional for sandboxed frames. If you use Chrome, try snippet at: https://robwu.nl/s/mediasource-change-volume.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With