Skip to content

Instantly share code, notes, and snippets.

@Azoay
Created June 14, 2015 22:27
Show Gist options
  • Save Azoay/37ff08c5786fa259eafb to your computer and use it in GitHub Desktop.
Save Azoay/37ff08c5786fa259eafb to your computer and use it in GitHub Desktop.
web audio api サンプル1
<!DOCTYPE html>
<html>
<head lang="ja">
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<form>
<input type="button" value="start" onclick="startSound()">
<input type="button" value="stop" onclick="stopSound()">
</form>
<script type="text/javascript">
var context;// ベースとなるコンテキスト
window.addEventListener('load', init, false);
function init() {
try {
// Fix up for prefixing
window.AudioContext = window.AudioContext||window.webkitAudioContext;
context = new AudioContext();
}
catch(e) {
alert('Web Audio API is not supported in this browser');
}
}
init();
var osciillatorNode;
function startSound() {
//音の発生源
osciillatorNode = context.createOscillator();
//音の出力
var audioDestinationNode = context.destination;
//音の発生源を音の出力装置に接続!
osciillatorNode.connect(audioDestinationNode);
//音を鳴らす
osciillatorNode.start = osciillatorNode.start || osciillatorNode.noteOn; //互換対応
osciillatorNode.start();
}
function stopSound() {
//音を止める
osciillatorNode.stop();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment