javascript - Displaying audio data in HTML page -
javascript - Displaying audio data in HTML page -
so have next javascript code makes sound visualiser works on chrome microphone input. although, actual visualiser works well, wondering if sound info uint8array called array , display in paragraph id = "arrayindex". however, when seek display array[i], comes out 0 , doesn't alter visualizer. have thought how can capture values in array? thanks!
addeventlistener('load', init); function init() { window.requestanimationframe = window.requestanimationframe || window.mozrequestanimationframe || window.webkitrequestanimationframe || window.msrequestanimationframe; window.audiocontext = window.ctx = document.getelementbyid('c').getcontext('2d'); gradient = ctx.createlineargradient(0, 0, 0, 200); gradient.addcolorstop(1, '#add8e6'); gradient.addcolorstop(0.65, '#576d74'); gradient.addcolorstop(0.45, '#ffaa00'); gradient.addcolorstop(0, '#ff0000'); ctx.fillstyle = gradient; window.audiocontext = window.webkitaudiocontext || window.audiocontext; context = new audiocontext(); analyser = context.createanalyser(); analyser.fftsize = 512; analyser.smoothingtimeconstant = 0.9; navigator.getmedia = (navigator.getusermedia || navigator.webkitgetusermedia || navigator.mozgetusermedia || navigator.msgetusermedia); navigator.getmedia({ audio: true, video: false }, function (localmediastream) { source = context.createmediastreamsource(localmediastream); console.log(source); source.connect(analyser); draw(); }, function (err) { console.log(err); }); } function draw() { var array = new uint8array(analyser.frequencybincount); analyser.getbytefrequencydata(array); ctx.clearrect(0, 0, 512, 256); var barheight; (var = 0; < array.length; i++) { barheight = 256-array[i]; ctx.fillrect(i * 2, barheight, 1, 256); document.getelementbyid("arrayindex").innerhtml = barheight; } requestanimationframe(draw);
}
you right!
there quirk in both chrome , firefox. if prepare frequency index e.g. 50 works. bad thought updating html @ such high rate (i.e. on 15khz in case). quirk might due ridiculous update rate. also, work if take update out of loop.
here working fiddle: notice text below spectrogram updates correctly.
here code fiddle:
var canvasctx = document.getelementbyid('canvas').getcontext('2d'); var buffersize = 4096; var audiocontext; seek { window.audiocontext = window.audiocontext || window.webkitaudiocontext; audiocontext = new audiocontext(); } grab (e) { alert('web sound api not supported in browser'); } // check if there microphone input. seek { navigator.getusermedia = navigator.getusermedia || navigator.webkitgetusermedia || navigator.mozgetusermedia || navigator.msgetusermedia; var hasmicrophoneinput = (navigator.getusermedia || navigator.webkitgetusermedia || navigator.mozgetusermedia || navigator.msgetusermedia); } grab (e) { alert("getusermedia() not supported in browser"); } // create analyser node. var analyser = audiocontext.createanalyser(); analyser.fftsize = 512; analyser.smoothingtimeconstant = 0.9; var bufferlength = analyser.frequencybincount; var dataarray = new uint8array(bufferlength); var errorcallback = function (e) { alert("error in getusermedia: " + e); }; // access microphone , start pumping info through graph. navigator.getusermedia({ audio: true }, function (stream) { // microphone -> mypcmprocessingnode -> destination. var microphone = audiocontext.createmediastreamsource(stream); microphone.connect(analyser); analyser.connect(audiocontext.destination); //microphone.start(0); }, errorcallback); // draw oscilloscope of current sound source function draw() { drawvisual = requestanimationframe(draw); analyser.getbytefrequencydata(dataarray); canvasctx.fillstyle = 'rgb(200, 200, 200)'; var width = 500; var height = 256; canvasctx.clearrect(0, 0, width, height); (var = 0; < dataarray.length; i++) { barheight = height - dataarray[i]; canvasctx.fillrect(i * 2, barheight, 1, dataarray[i]); // bad thought update element in loop: // however, if do, next line gives 0, seems bug. document.getelementbyid("arrayindex").innerhtml = dataarray[i]; // line works though. document.getelementbyid("arrayindex").innerhtml = dataarray[50]; } }; draw();
here more background on webapi real time sound processing:.
javascript html audio visualization
Comments
Post a Comment