Share
Embed
web audio api (htmlday osaka)
huydx
June 09, 2013
Transcript
45 slides
-
ࣗݾհ ໊લɿυʔεΞϯϑΠ @huydx ग़ɿϕτφϜ ৬ۀɿֶੜ (ShareWis IncͰΠϯλʔϯʣ Ruby + javascript
http://huydx.com Saturday, June 8, 2013 -
ΞδΣϯμ ‣ ΠϯτϩμΫγϣϯ ‣ ྺ࢙ ‣ Web audio api͏ͨΊඞཁͳࣝ ‣
Web audio apiͷհʗߏ ‣ σϞλΠϜ ‣ Web audio apiͷϥΠϒϥϦΛհ ‣ ࣭λΠϜ Saturday, June 8, 2013 -
Web audio apiͷྺ࢙ •Google Chrome͔Β࢝Ί •OpenALΛجͮ͘ •2011 / 15 /
12 : First Draft •2012 / 13 / 12 : ݱࡏͷ൛ Saturday, June 8, 2013 -
ΣϒͰAudioͷྺ࢙ʁ ‣ <bgsound> λάɹ(IE ͷΈʂʣ ‣ എܠͰԻΛ໐Β͢ ‣ <bgsound src=”audio/welcome.wav”
/> ‣ ϑϥογϡ ‣ <audio>λά ‣ ࣮؆୯ ‣ ؆୯ͳԻ੍ޚͰ͖Δ ‣ Web audio API ొʂʂʂʂ Saturday, June 8, 2013 -
·ͣaudioσʔλΛϩʔυ function init() { if (typeof AudioContext !== "undefined") {
context = new AudioContext(); } else if (typeof webkitAudioContext !== "undefined") { context = new webkitAudioContext(); } else { throw new Error('AudioContext not supported. :('); } } Saturday, June 8, 2013 -
audioσʔλΛϩʔυ function init() { if (typeof AudioContext !== "undefined") {
context = new AudioContext(); } else if (typeof webkitAudioContext !== "undefined") { context = new webkitAudioContext(); } else { throw new Error('AudioContext not supported. :('); } } ·ͣAudioContextΛॳظԽ Saturday, June 8, 2013 -
function startSound() { // Note: this loads asynchronously var request
= new XMLHttpRequest(); request.open("GET", url, true); request.responseType = "arraybuffer"; request.onload = function() { var audioData = request.response; context.decodeAudioData(audioData, function(buffer) { playmusic(buffer) }, onError); }; request.send(); } audioσʔλΛϩʔυ ࣍ʹXHRͰʢFileReaderͰΑ͍)σʔλऔಘ Saturday, June 8, 2013 -
ϩʔυͨ͠σʔλΛSource + DestinationΛ࡞ͬͯɺܨ͕Δ function playmusic(buffer) { sourceNode = context.createBufferSource( );
sourceNode.connect( context.destination ); sourceNode.buffer = buffer; sourceNode.noteOn( 0 ); } initSound( ); startSound( ); օେମͷྲྀΕΛѲ͢ΔͰ͠ΐ͏ audioσʔλΛϩʔυ Saturday, June 8, 2013 -
Source Destination Input (audio data) Output (speaker) Audio Context Ұ൪؆୯ͳάϥϑ
Saturday, June 8, 2013 -
ॲཧϊʔυͷछྨ ‣ GainNode (རಘʣ ‣ BiquadFilterNode (ϑΟϧλʣ ‣ PannerNodeʢ̏࣍ݩAudioૢ࡞ʣ ‣
ConvolverNodeʢΈࠐΈʣ ‣ AnalyzerNodeʢੳʣ ‣ ....... Saturday, June 8, 2013 -
࣌ؒௐ Gun.prototype.shoot = function (type, rounds, interval, random, random2) {
if (typeof random == 'undefined') { random = 0; } var time = context.currentTime; for (var i = 0; i < rounds; i++) { var source = this.makeSource(this.buffers[type]); source.playbackRate.value = 1 + Math.random() * random2; source.noteOn(time + i * interval + Math.random() * random); } } σʔϞ Saturday, June 8, 2013 -
AnalyzerNode ͬͱ͍͜͠ͷྫɿ http://html5-demos.appspot.com/static/webaudio/createMediaSourceElement.html http://chimera.labs.oreilly.com/books/1234000001552/ch05.html#s05_3 function getAverageVolume(array) { var vals =
0; var length = array.length; for (var i=0; i<length; i++) { vals += array[i]; } return (vals / length) } window.javascriptnode.onaudioprocess = function() { var array = new Uint8Array(analyzer.frequencyBinCount); analyzer.getByteFrequencyData(array); var average = getAverageVolume(array); ctx.clearRect(0, 0, 60, 130); ctx.fillStyle="#00FFFF"; ctx.fillRect(0, 130-average,25,130); } पΛarrayʹอଘ Saturday, June 8, 2013 -
ConvolverNode function audioGraph(audioData) { var convolver; soundSource = context.createBufferSource(); soundBuffer
= context.createBuffer(audioData, true); soundSource.buffer = soundBuffer; convolver = context.createConvolver(); soundSource.connect(convolver); convolver.connect(context.destination); Convolve('echo.mp3', convolver, function() {playSound()}); } function Convolve(url, convolver, callback) { var request = new XMLHttpRequest(); request.open("GET", url, true); request.responseType = "arraybuffer"; request.onload = function () { convolver.buffer = context.createBuffer(request.response, false); callback(); } request.send(); } Saturday, June 8, 2013 -
OscillatorͰAudioੜ(γϯηγεʣ var osc = audioctx.createOscillator(); var gain = audioctx.createGain(); osc.connect(gain);
gain.connect(audioctx.destination); function Setup() { if(play == 0) { osc.start(0); play = 1; } var type = document.getElementById("type").selectedIndex; var freq = parseFloat(document.getElementById("freq").value); var level = parseFloat(document.getElementById("level").value); osc.type = type; osc.frequency.value = freq; gain.gain.value = level; } Saturday, June 8, 2013 -
࣮ࡍͷԠ༻ • ήʔϜʢAngryBird, CasinoRPG) • ΞϓϦͷUXϑΟουόοΫʢՄࢹԽʣ • MusicalΞϓϦέʔγϣϯʢDJ, γϯηγ εʣ
• RealTimeͰAudioΤϑΣΫτՃ Saturday, June 8, 2013 -
ϥΠϒϥϦ ‣ MusicJS (ָثͷγϯηγε༻ʣ ‣ Howler.js (Web audio apiͷϥού) ‣
Dancer.js (Web audio apiͷϥούʣ ‣ ...... Saturday, June 8, 2013