/* Copyright 2013 Chris Wilson Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ window.AudioContext = window.AudioContext || window.webkitAudioContext; var audioInput = null, realAudioInput = null, inputPoint = null, audioRecorder = null; var rafID = null; var analyserContext = null; var canvasWidth, canvasHeight; var recIndex = 0; var recording_audio = 0; var rec_json = []; var diem = 0; function cancelAnalyserUpdates() { window.cancelAnimationFrame( rafID ); rafID = null; } var count_bin = 0; function updateAnalysers(time) { var freqByteData = new Uint8Array(analyserNode.frequencyBinCount); analyserNode.getByteFrequencyData(freqByteData); console.log(freqByteData) var total = 0; for(var i=0; i < freqByteData.length; i++){ total += freqByteData[i]; //console.log(freqByteData[i]); } total = total/512; if(total>50){ count_bin++; console.log(count_bin); } if(count_bin>=10){ window.flow() }else{ rafID = window.requestAnimationFrame( updateAnalysers ); } } function gotStream(stream) { console.log('active_flow'); window.active_flow() inputPoint = audioContext.createGain(); // Create an AudioNode from the stream. realAudioInput = audioContext.createMediaStreamSource(stream); audioInput = realAudioInput; audioInput.connect(inputPoint); //audioInput = convertToMono( input ); analyserNode = audioContext.createAnalyser(); analyserNode.fftSize = 1024; inputPoint.connect( analyserNode ); audioRecorder = new Recorder( inputPoint ); zeroGain = audioContext.createGain(); zeroGain.gain.value = 0.0; inputPoint.connect( zeroGain ); zeroGain.connect( audioContext.destination ); rafID = window.requestAnimationFrame( updateAnalysers ); } function initAudio() { audioContext = new AudioContext(); if (!navigator.getUserMedia) navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia; if (!navigator.cancelAnimationFrame) navigator.cancelAnimationFrame = navigator.webkitCancelAnimationFrame || navigator.mozCancelAnimationFrame; if (!navigator.requestAnimationFrame) navigator.requestAnimationFrame = navigator.webkitRequestAnimationFrame || navigator.mozRequestAnimationFrame; navigator.getUserMedia( { "audio": { "mandatory": { "googEchoCancellation": "false", "googAutoGainControl": "false", "googNoiseSuppression": "false", "googHighpassFilter": "false" }, "optional": [] }, }, gotStream, function(e) { alert('Error getting audio'); console.log(e); }); } // window.addEventListener('load', initAudio );