Web Audio API ã§é³ãåçããã
ãªã¬ã® Advent Calendar 2015 - Adventar ã® 13 æ¥ç®ã§ã.
ããããæ¸ãããWebä¸ã§ãã¢ãä½ãããçãªã¨ã³ããªã¼ãè»ä¸¦ã¿ãã¡ã«ãªã£ã¦ããã®ã§ ããããæ¸ããªãããã¨æãã¾ã.
Chrome ã ã¨ãã¾ã«ããé³ããªããªã, Safari ã ã¨ãã¯ãåããªã, Firefox ã¯å½æé³åç系㮠API ãéã£ãã®ã§èæ ®ãã¦ããªãã¨ããæ²æ¨ãªç¶æ³ã§ãã. IE ã¯è«å¤ã§ãã.
ããã§, ãããã«ãã®ã¾ã¾ãã®ã¨ã³ããªã¼ãéæ¾ãã«ããããã«ããããªãã®ã§æ¸ããªããã¦ãããã¨æãã¾ã.
ãã®é ã¯, Web Audio API ãã¾ã ã¾ã å®ç¨ã¬ãã«ã§ã¯ãªãã£ãã®ã§ Audio è¦ç´ ã使ã£ã¦é³ãé³´ããã¦ããã®ã§ãã, ä»ã§ã¯ã ã㶠Web Auido API ãå®å®ãã¦ããã®ã§ãã¡ãã使ã£ãåçæ¹æ³ã«æ¸ãæãã¦ããã¾ã.
ã£ã¦ãã¨ã§ä»åã¯ãã®å¸ç³ã¨ã㦠Web Audio API ã® Getting Started, æå°ãµã³ãã«ãä½ã£ã¦ããããã¨æãã¾ã.
Web ä¸ã§ã®é³ãåçããæ¹æ³ã¯ï¼éãï¼
Web ä¸ã§é³ãåçããæ¹æ³ã¯ç¾ç¶ï¼éãããã¾ã.
ä¸ã¤ãã¤ç´¹ä»ãã¦ããããã¨æãã¾ã.
ã¿ããªå¤§å¥½ã(ã ã£ã) Flash ã§é³ãåç
Flash ãã©ã°ã¤ã³ããã©ã¦ã¶ã«ã¤ã³ã¹ãã¼ã«ãã¦, Flash å
ã§å¦çãæ¸ããã¨ã§ãµã¦ã³ããåçãã¾ã.
æã¯, Web ä¸ã§é³ãåçããå¯ä¸ã®æ段ã§ãã. ãããªãé³ãé³´ãåºããµã¤ãã¯å¤§æµãã使ã£ã¦ã¾ã.
Audio è¦ç´ ã使ã£ã¦é³ãåç
æãæ軽ãªæ¹æ³ã§ãã.
JavaScript ã§ä¸è¨ã®ããã«æ¸ããã¨ã§ã«ã³ã¿ã³ã«é³ãé³´ããã¾ã.
new Audio('sound.mp3').play();
ããã¯è¦ç´ ã§ãããã®ã§ HTML ä¸ã«ä¸è¨ã®ããã«åãè¾¼ããã¨ãã§ãã¾ã.
<audio src='sound.mp3' autoplay />
ãã , ããã¯è²ã ã¨å¶ç´ãå¤ãåä½ãä¸å®å®ã§ã.
ç¹ã« iPhone, Android ã§ä½¿ç¨ããé, ã¦ã¼ã¶ã®ã¤ã³ã¿ã©ã¯ã·ã§ã³(ã¿ããã¤ãã³ããã¹ã¯ãã¼ã«ã¤ãã³ããªã©)å ã§ãã åçã§ããªã ã¨ããåé¡ãããã¾ã.
new Audio('sound.mp3').play(); // åçãããªã
elm.onclick = function() {
new Audio('sound.mp3').play(); // åçããã
}
ã¾ã, åããµã¦ã³ããã¼ã¿ãåæã«åçã§ããªãã¨ãã£ãåé¡ããã, ã²ã¼ã ãªã©ã§ä½¿ãã«ã¯é£ããç¶æ³ã§ãã.
Web Audio API ã§é³ãåç
ä»åã®æ¬é¡ã¨ãªã Web Audio API ã§ãã.
Web Audio API ã¯, HTML5 ããå°å ¥ããã, é³ãã¡ã¤ã«ã Web ä¸ã§æ±ãããã® API ã§ã.
ã¡ãã£ã¨ä½¿ãæ¹ã¯è¤éã§ãã, Audio è¦ç´ ã«ãã£ããããªå¶ç´ããªã æ·±ãã¨ããã¾ã§ãµã¦ã³ããæ±ããã¨ãã§ãã¾ã.
詳ããã¯ä¸ã§è§£èª¬ãã¦ããã¾ã.
Web Audio Demo
ä»åä½ã£ã Web Audio API ã®æå°ãµã³ãã«ã§ã. Play ãã¿ã³ãã¯ãªãã¯ããã¨é³ãåçãããã®ãããããã¨æãã¾ã.
ã¾ã, ä¸è¨ã®ãªã³ã¯ããåä½èµ·å, ãããã¯ãã¦ã³ãã¼ãã§ãã¾ã.
Web Audio API - Getting Started or Download
Web Audio API ã使ã£ãã³ã¼ã
ä¸ã®ãã¢ã®å ¨ä½ã³ã¼ãã«ãªãã¾ã.
HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>Web Audio API Getting Started</title>
<meta name="description" content="html5,webaudioapi,javascript" />
<style>
#btn {
font-size: 4rem;
padding: 1rem 2rem;
}
</style>
<script src='main.js'></script>
</head>
<body>
<button id='btn'>Play</button>
</body>
</html>
JavaScript
main.js ã®å®è£ ã§ã.
/*
* main.js
*/
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
// Audio ç¨ã® buffer ãèªã¿è¾¼ã
var getAudioBuffer = function(url, fn) {
var req = new XMLHttpRequest();
// array buffer ãæå®
req.responseType = 'arraybuffer';
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 0 || req.status === 200) {
// array buffer ã audio buffer ã«å¤æ
context.decodeAudioData(req.response, function(buffer) {
// ã³ã¼ã«ããã¯ãå®è¡
fn(buffer);
});
}
}
};
req.open('GET', url, true);
req.send('');
};
// ãµã¦ã³ããåç
var playSound = function(buffer) {
// source ãä½æ
var source = context.createBufferSource();
// buffer ãã»ãã
source.buffer = buffer;
// context ã« connect
source.connect(context.destination);
// åç
source.start(0);
};
// main
window.onload = function() {
// ãµã¦ã³ããèªã¿è¾¼ã
getAudioBuffer('se.mp3', function(buffer) {
// èªã¿è¾¼ã¿å®äºå¾ã«ãã¿ã³ã«ã¯ãªãã¯ã¤ãã³ããç»é²
var btn = document.getElementById('btn');
btn.onclick = function() {
// ãµã¦ã³ããåç
playSound(buffer);
};
});
};
Web Audio API ã使ã£ã¦ã¿ãã
主㫠JavaScript é¨åã®è§£èª¬ãè¡ãã¾ã.
Step1. Web Audio ãã¯ãã¹ãã©ã¦ã¶å¯¾å¿ãããã
ä¸é¨ã®ãã©ã¦ã¶ã§ã¯ã¾ã Web Audio ç¨ã«ä½¿ã AudioContext ã«ãã³ãã¼ãã¬ãã£ãã¯ã¹ãä»ãã¦ãã¾ã. ä»å¾ã¯å ¨ã¦å¤ããã¨ã¯æãã¾ãã念ã®ãã対å¿ããã¦ããã¾ããã.
åå¨ãã§ãã¯ã㦠webkit ããã£ã¤ãã¦ãã ãã§ã.
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
Step2. AudioBuffer ãèªã¿è¾¼ãã
getAudioBuffer()
ã®é¨åã§ãã.
ããã¯, ä¸è¦è¤éã«è¦ãã¾ãã xhr(ajax) ã§é³å£°ãã¡ã¤ã«ãèªã¿è¾¼ã¿,
Audio ç¨ã«ãã³ã¼ããã¦ããã ãã§ã.
ããã¹ãã§ã¯ãªã arraybuffer
ã¨ãã¦ãã¼ããããã®ã§
responseType = 'arraybuffer';
ãæå®ãã¾ã.
ãã¼ãå®äºå¾ã« step1 ã§çæãã context
ã® decodeAudioData
ã使ã£ã¦
ArrayBuffer
ã AudioBuffer
ã«å¤æãã¦ãã¾ã.
// array buffer ã audio buffer ã«å¤æ
context.decodeAudioData(req.response, function(buffer) {
// ã³ã¼ã«ããã¯ãå®è¡
fn(buffer);
});
Step3. ãµã¦ã³ããåçããå¦çãä½ãã
playSound()
é¨åã®è§£èª¬ã«ãªãã¾ã.
ããã§ã¯ Step2 ã§ä½æããã buffer ã渡ãããåæã®é¢æ°ã¨ãã¦æ±ç¨çã«ä½ãã¾ã.
// ãµã¦ã³ããåç
var playSound = function(buffer) {
...
};
ã¾ã, node ãä½ãã¾ã.
// source ãä½æ
var source = context.createBufferSource();
次ã«å¼æ°ã¨ãã¦æ¸¡ããã buffer ãã»ãããã¾ã.
// buffer ãã»ãã
source.buffer = buffer;
ããã context ã«ã²ãä»ãã¾ã
// context ã« connect
source.connect(context.destination);
ãã¨ã¯åçããã ãã§ã.
// åç
source.start(0);
以å㯠noteOn()
ã¨ãã start()
ã¨åçã®ã¡ã½ããããã£ãã®ã§ãã
å»æ¢ãããã®ã§ä½¿ããªããã注æãã¦ãã ãã.
ç§ã®ã©ã¤ãã©ãªã¯ããã§è»ä¸¦ã¿ä¸åº¦æ»ã«ã¾ãã.
ãããã¨ã¯å¤ãã§ãã, ä¸æ¦é¢æ°åãã¦ããã° buffer ã渡ãã ããªã®ã§ æ°è»½ã«è²ã ãªã¨ããã§é³ãåçã§ãã¾ãã.
Step4. å®éã«é³ãåçãã¦ã¿ãã
ãã¨ã¯, Step1, 2, 3 ã§ä½ã£ã¦ãããã®ã使ãã ãã§ã. ä»å㯠html å´ã§ãã¿ã³ãè¨ç½®ãã¦, click ããã¨é³ãåçããããã«ãã¦ãã¾ã.
// ãµã¦ã³ããèªã¿è¾¼ã
getAudioBuffer('se.mp3', function(buffer) {
// èªã¿è¾¼ã¿å®äºå¾ã«ãã¿ã³ã«ã¯ãªãã¯ã¤ãã³ããç»é²
var btn = document.getElementById('btn');
btn.onclick = function() {
// ãµã¦ã³ããåç
playSound(buffer);
};
});
ããã§ãã¿ã³ãã¯ãªãã¯ãã㨠se.mp3
ãåçãããããã«ãªãã¾ã.
è¿ã WebAudio API ã§ãã¢ããä½ãã¨ã³ããªã¼æ¸ãã¾ã! ãã¾ã«è¦ãã¦ããããã¨å¹¸ãã§ã.
Reference
- é³æ¥½ãã¡ã¤ã«ã¯ éçé ããã®ãã®ã使ç¨ããã¦é ãã¦ãã¾ã.