audio and video stream with volume meter
using microphone audio to visualize a new volume meter (base script by Chris Wilson)
by Ercan Cicek
HTML
<div id="va-recorder">
<video id="stream"></video> <!-- vid stream container -->
<div id="meter" class="flex-display flex-column flex-center flex-spaceB"></div>
</div>
<script>
/*
The MIT License (MIT)
Copyright (c) 2014 Chris Wilson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
Usage:
audioNode = createAudioMeter(audioContext,clipLevel,averaging,clipLag);
audioContext: the AudioContext you're using.
clipLevel: the level (0 to 1) that you would consider "clipping".
Defaults to 0.98.
averaging: how "smoothed" you would like the meter to be over time.
Should be between 0 and less than 1. Defaults to 0.95.
clipLag: how long you would like the "clipping" indicator to show
after clipping has occured, in milliseconds. Defaults to 750ms.
Access the clipping through node.checkClipping(); use node.shutdown to get rid of it.
*/
function createAudioMeter(audioContext,clipLevel,averaging,clipLag) {
var processor = audioContext.createScriptProcessor(512);
processor.onaudioprocess =...
CSS
#va-recorder {
width: 640px;
height: 480px;
position: relative;
}
#meter {
height: 100%;
position: absolute;
top: 0;
right: 0;
transform: scale(-1);
}
#meter > div {
width: 20px;
height: 20px;
}
.yellow {
background-color: #FFEB3B;
}
.green {
background-color: #388E3C;
}
.red {
background-color: #D32F2F;
}
.transparent {
background-color: transparent !important;
}
.flex-display {
display: -webkit-box !important;
display: -ms-flexbox !important;
display: -moz-flex !important;
display: -webkit-flex !important;
display: flex !important;
}
.flex-center {
-webkit-box-align: center;
-moz-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
}
.flex-column {
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
}
.flex-spaceB {
-webkit-box-pack: justify;
-moz-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
JavaScript
var constraints = {
audio: true,
video: { width: { ideal: 640 }, height: { ideal: 480 }} // --- example
},
vid = $("#va-recorder > #stream")[0],
meter = null;
function init() {
// --- set volume meter squares (20 pcs - first 3 yellow, next 12 green, last 5 red)
for(var i = 1; i <= 20; i++) {
$("#meter").append("<div id=mtrbox_" + i + " class='mtrbox " + (i < 4 ? "yellow" : (i < 16 ? "green" : "red")) + " transparent'></div>");
}
startStream();
}
function startStream() {
// --- start audio and video stream
navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
vid.srcObject = stream;
vid.volume = 0; // --- no backcoupling
vid.play();
audioMeter(stream);
}).catch(function(error) { console.log("error message: ", error.name) });
}
function audioMeter(stream) {
// --- start audio meter as scripted by Chris Wilson (see HTML)
var audContext = new AudioContext();
var mediaStreamSource = audContext.createMediaStreamSource(stream);
meter = createAudioMeter(audContext, 0.9);
mediaStreamSource.connect(meter);
// kick off the visual updating
setInterval(drawLoop, 100);
}
function drawLoop(time) {
$(".mtrbox").addClass("transparent");
// --- round volume value to 0.05er
var relVol = (Math.ceil(meter.volume * 20) / 20).toFixed(2);
// --- get mtrbox id from rounded value
var relId = relVol * 20;
for(var i = 0; i <= relId; i++) {
$("#mtrbox_" + i).removeClass("transparent");
}
}
$(init);