2. Index
๏ง Web Audio API
๏ง Modular routing
๏ง Basic usage
๏ง Background Music
๏ง Sound effects
๏ง 3D Positional sound
๏ง Room effects and filters
๏ง Detect clipping
3. ์ ํ์ ์ธ <audio>
๏ง ๋จ์ํ ๊ฒ์์ <audio>๋ก ๊ฐ๋ฅํ์ง๋ง, ์ ํ์ ์ด๋ค.
<audio controls loop>
<source src=โmusic.wav">
</audio>
<audio> canโt :
๏ง No ability to apply filters to the sound signal
๏ง No way to access the raw PCM data
๏ง No concept of position and direction of sources and listeners
๏ง No fine-grained timing
4. Web Audio API
๏ง High-level JavaScript API for processing and synthesizing audio in web
application.
๏ http://www.w3.org/TR/webaudio/
๏ง AudioContext๋ ๋ชจ๋ ์ฌ์ด๋ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌ ๋ฐ ์ฌ์ํ๋ค.
๏ง ์ฌ์์ ์ํด์ ์ฌ์ด๋ ์์ค๋ฅผ ์์ฑํ ํ, AudioContext ์ธ์คํด์ค์ ์ฐ๊ฒฐํด์ผ
ํ๋ค.
11. <audio> to Web Audio API
๏ง <audio>๋ ์์ค๋ฅผ ๋ชจ๋ ๋ค์ด๋ก๋ ๋ฐ์ง ์์๋ ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ์ผ๋ก ์ฌ์๋๋ค.
๏ง ์คํธ๋ฆฌ๋ฐ ์์ค๋ฅผ Web Audio API์ ์ฐ๊ฒฐํ๋ฉด ์คํธ๋ฆฌ๋ฐ์ ๋ถ์ํ๊ฑฐ๋ ์กฐ์ํ
์ ์๊ฒ ๋๋ค.
๏ง <audio>๋ฅผ Web Audio context๋ก ๊ฐ์ ธ์ค๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค.
var audioElement = document.querySelector('audio');
var mediaSourceNode = context.createMediaElementSource(audioElement);
mediaSourceNode.connect(context.destination);
audioElement.play();
๏ง createAnalyser() creates a RealtimeAnalyserNode which provide real-time frequency and time-
domain analysis information.
๏ context.createAnalyser()๋ฅผ ํตํ equalizer graph ๋ฐ๋ชจ :
http://html5-demos.appspot.com/static/webaudio/createMediaSourceElement.html
12. Sound effects
๏ง BGM๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋น์ทํ ์ข ๋ฅ์ ๋ฐ๋ณต์ ์ง๋ฃจํ๊ณ ์ง์ฆ์ค๋ฝ๋ค.
๏ง ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ์ฌ๋ฌ ์ข ๋ฅ์ ์ฌ์ด๋ ํ์ ์ค๋นํด ์ฌ์ฉํ ์ ์๋ค.
๏ง ์ฌ๋ฌ ์บ๋ฆญํฐ๊ฐ ๋ค์ ๋ฑ์ฅํ๋ ๋ฐฐํ ์ฅ๋ฉด์ธ ๊ฒฝ์ฐ์๋ ๋์์ ์๋ง์ ์ดํํธ๊ฐ
์ฌ์๋ ์ ์๋ค.
// currentTime, starts at zero when the context is created and increases in real-time.
var time = context.currentTime;
for (var i = 0; i < rounds; i++) {
var source = this.makeSource(this.buffers[M4A1]);
source.noteOn(time + i * interval);
}
13. 3D Positional sound
๏ง AudioPannerNode๋ฅผ ํ์ฉํด ์์น์ ๊ณต๊ฐ์ ํ๋ก์ธ์ฑ ํ๋ค.
var panner = context.createPanner();
// Directional model
panner.coneOuterGain = 0.5;
panner.coneOuterAngle = 180;
panner.coneInnerAngle = 0;
// Position, orientaion and velocity
panner.setPosition(x, y, z); // 3D cartesian coordinate relative to the listener attribute
panner.setOrientation(x, y, z); // pointing in the 3D cartesian coordinate space
panner.setVelocity(x, y, z); // direction of travel and the speed in 3D space
source.connect(panner);
panner.connect(context.destination);
17. Detect clipping
// Assume entire sound output is being piped through the mix node.
var mix = context.createGainNode();
var meter = context.createJavaScriptNode(2048, 1, 1);
// Porcess audio event binding
meter.onaudioprocess = function (e) {
var leftBuffer = e.inputBuffer.getChannelData(0);
var rightBuffer = e.inputBuffer.getChannelData(1);
checkClipping(leftBuffer);
checkClipping(rightBuffer);
};
mix.connect(meter);
meter.connect(context.destination);
mix.connect(context.destination);
// Check if buffer exceeds
function checkClipping(buffer) {
var isClipping = false;
// Iterate through buffer to check if any of the |values| exceeds 1.
for (var i = 0; i < buffer.length; i++) {
var absValue = Math.abs(buffer[i]);
if (absValue >= 1) {
isClipping = true;
break;
}
}
}