Upgrade to Pro — share decks privately, control downloads, hide ads and more …

web audio api (htmlday osaka)

Avatar for huydx huydx
June 09, 2013

web audio api (htmlday osaka)

Avatar for huydx

huydx

June 09, 2013
Tweet

More Decks by huydx

Other Decks in Programming

Transcript

  1. ΞδΣϯμ ‣ ΠϯτϩμΫγϣϯ ‣ ྺ࢙ ‣ Web audio api࢖͏ͨΊඞཁͳ஌ࣝ ‣

    Web audio apiͷ঺հʗߏ੒ ‣ σϞλΠϜ ‣ Web audio apiͷϥΠϒϥϦ౳Λ঺հ ‣ ࣭໰λΠϜ Saturday, June 8, 2013
  2. Web audio apiͷྺ࢙ •Google Chrome͔Β࢝Ί •OpenALΛجͮ͘ •2011 / 15 /

    12 : First Draft •2012 / 13 / 12 : ݱࡏͷ൛ Saturday, June 8, 2013
  3. ΢ΣϒͰAudioͷྺ࢙ʁ ‣ <bgsound> λάɹ(IE ͷΈʂʣ ‣ എܠͰԻΛ໐Β͢ ‣ <bgsound src=”audio/welcome.wav”

    /> ‣ ϑϥογϡ ‣ <audio>λά ‣ ࣮૷؆୯ ‣ ؆୯ͳԻ੠੍ޚͰ͖Δ ‣ Web audio API ొ৔ʂʂʂʂ Saturday, June 8, 2013
  4. ·ͣ͸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
  5. 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
  6. 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
  7. ϩʔυͨ͠σʔλΛ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
  8. ॲཧϊʔυͷछྨ ‣ GainNode (རಘʣ ‣ BiquadFilterNode (ϑΟϧλʣ ‣ PannerNodeʢ̏࣍ݩAudioૢ࡞ʣ ‣

    ConvolverNodeʢ৞ΈࠐΈʣ ‣ AnalyzerNodeʢ෼ੳʣ ‣ ....... Saturday, June 8, 2013
  9. ࣌ؒௐ੔ 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
  10. 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
  11. 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
  12. 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
  13. ϥΠϒϥϦ౳ ‣ MusicJS (ָثͷγϯηγε༻ʣ ‣ Howler.js (Web audio apiͷϥού) ‣

    Dancer.js (Web audio apiͷϥούʣ ‣ ...... Saturday, June 8, 2013