Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

106
LINES

< > BotCompany Repo | #1014014 // Volume Meter main.js

Document

1  
/*
2  
The MIT License (MIT)
3  
4  
Copyright (c) 2014 Chris Wilson
5  
6  
Permission is hereby granted, free of charge, to any person obtaining a copy
7  
of this software and associated documentation files (the "Software"), to deal
8  
in the Software without restriction, including without limitation the rights
9  
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  
copies of the Software, and to permit persons to whom the Software is
11  
furnished to do so, subject to the following conditions:
12  
13  
The above copyright notice and this permission notice shall be included in all
14  
copies or substantial portions of the Software.
15  
16  
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  
SOFTWARE.
23  
*/
24  
var audioContext = null;
25  
var meter = null;
26  
var canvasContext = null;
27  
var WIDTH=500;
28  
var HEIGHT=50;
29  
var rafID = null;
30  
31  
window.onload = function() {
32  
33  
  // grab our canvas
34  
  var canvas = document.getElementById("meter");
35  
	canvasContext = canvas.getContext("2d");
36  
	WIDTH = canvas.width;
37  
	HEIGHT = canvas.height;
38  
	
39  
    // monkeypatch Web Audio
40  
    window.AudioContext = window.AudioContext || window.webkitAudioContext;
41  
	
42  
    // grab an audio context
43  
    audioContext = new AudioContext();
44  
45  
    // Attempt to get audio input
46  
    try {
47  
        // monkeypatch getUserMedia
48  
        navigator.getUserMedia = 
49  
        	navigator.getUserMedia ||
50  
        	navigator.webkitGetUserMedia ||
51  
        	navigator.mozGetUserMedia;
52  
53  
        // ask for an audio input
54  
        navigator.getUserMedia(
55  
        {
56  
            "audio": {
57  
                "mandatory": {
58  
                    "googEchoCancellation": "false",
59  
                    "googAutoGainControl": "false",
60  
                    "googNoiseSuppression": "false",
61  
                    "googHighpassFilter": "false"
62  
                },
63  
                "optional": []
64  
            },
65  
        }, gotStream, didntGetStream);
66  
    } catch (e) {
67  
        alert('getUserMedia threw exception :' + e);
68  
    }
69  
70  
}
71  
72  
73  
function didntGetStream() {
74  
    alert('Stream generation failed.');
75  
}
76  
77  
var mediaStreamSource = null;
78  
79  
function gotStream(stream) {
80  
    // Create an AudioNode from the stream.
81  
    mediaStreamSource = audioContext.createMediaStreamSource(stream);
82  
83  
    // Create a new volume meter and connect it.
84  
    meter = createAudioMeter(audioContext);
85  
    mediaStreamSource.connect(meter);
86  
87  
    // kick off the visual updating
88  
    drawLoop();
89  
}
90  
91  
function drawLoop( time ) {
92  
    // clear the background
93  
    canvasContext.clearRect(0,0,WIDTH,HEIGHT);
94  
95  
    // check if we're currently clipping
96  
    if (meter.checkClipping())
97  
        canvasContext.fillStyle = "red";
98  
    else
99  
        canvasContext.fillStyle = "green";
100  
101  
    // draw a bar based on the current volume
102  
    canvasContext.fillRect(0, 0, meter.volume*WIDTH*1.4, HEIGHT);
103  
104  
    // set up the next visual callback
105  
    rafID = window.requestAnimationFrame( drawLoop );
106  
}

Author comment

Began life as a copy of #1014013

download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1014014
Snippet name: Volume Meter main.js
Eternal ID of this version: #1014014/2
Text MD5: 77189ce4c654130a2521620df3ce9453
Author: stefan
Category: javax / html / speech
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-04-27 01:22:02
Source code size: 3339 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 423 / 169
Version history: 1 change(s)
Referenced in: [show references]