Skip to content

Commit

Permalink
added web audio
Browse files Browse the repository at this point in the history
  • Loading branch information
samdutton committed Sep 6, 2012
1 parent 61adac1 commit f0c1802
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 15 deletions.
2 changes: 1 addition & 1 deletion css/main.css
Expand Up @@ -77,7 +77,7 @@ border: none;
p {
color: #eee;
font-size: 18px;
line-height: 1em;
line-height: 1.2em;
}

p#data {
Expand Down
6 changes: 2 additions & 4 deletions index.html
Expand Up @@ -47,18 +47,16 @@ <h1>simpl.info</h1>
<a href="softhyphen/index.html" title="Soft hyphen (&amp;shy;) example">Hyphenation (soft hyphen)</a>
<a href="iframe/index.html" title="iframe element example">&lt;iframe&gt;</a>
<a href="bigimage/index.html" title="Example of an image with large file size">Image: a big one (20MB)</a>
<a href="indexeddb/index.html" title="IndexedDB example">IndexedDB</a>
<!-- <a href="indexeddb/index.html" title="IndexedDB example">IndexedDB</a> -->
<a href="localstorage/index.html" title="localStorage">localStorage</a>
<!-- media queries -->
<a href="pagevisibility/index.html" title="Page Visibility API example">Page Visibility API</a>
<a href="peerconnection/index.html" title="PeerConnection example">PeerConnection</a>
<a href="postmessage/index.html" title="postMessage() example">postMessage()</a>
<a href="sessionstorage/index.html" title="sessionStorage example">sessionStorage</a>
<a href="track/index.html" title="Track element">&lt;track&gt;</a>
<a href="video/index.html" title="Video element">&lt;video&gt;</a>
<!-- web audio -->
<a href="webaudio/index.html" title="Web Audio">Web Audio</a>
<a href="webfonts/index.html" title="Web fonts">Web Fonts</a>
<!-- webgl -->
<a href="stnetnibew/index.html" title="Web Intents example">Web Intents</a>
<a href="webrtc/index.html" title="WebRTC">WebRTC</a>
<a href="websql/index.html" title="WebRTC">WebSQL</a>
Expand Down
36 changes: 36 additions & 0 deletions ssi.html
@@ -0,0 +1,36 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

<title>Untitled</title>

<style type="text/css">
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {

});
</script>

<script type="text/javascript">

</script>

</head>

<body>

<div id="container">
<!--#echo var="DATE_LOCAL" -->
</div><!-- container -->

</body>

</html>
Binary file added webaudio/audio/animalSounds.mp3
Binary file not shown.
Binary file added webaudio/audio/animalSounds.ogv
Binary file not shown.
27 changes: 27 additions & 0 deletions webaudio/index.html
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Web Audio</title>
<link rel="stylesheet" href="../css/main.css" />
</head>
<body>
<div id="container">

<h1><a href="../index.html" title="simpl.info home page">simpl.info</a> Web Audio</h1>

<audio autoplay controls>
<source src="audio/animalSounds.ogv" type="audio/ogg" />
<source src="audio/animalSounds.mp3" type="audio/mp3" />
This browser does not support the audio element.
</audio>

<p>For more information about Web Audio see <a href="http://www.html5rocks.com/en/tutorials/webaudio/intro/" title="HTML5 Rocks tutorial: Getting started with the Web Audio API">Getting started with the Web Audio API</a>.</p>

<script src="js/main.js"></script>

<a href="view-source:http://simpl.info/webaudio" title="View source for this page" id="viewSource">View source</a>
</div>
<script src="../js/lib/ga.js"></script>
</body>
</html>
36 changes: 36 additions & 0 deletions webaudio/js/main.js
@@ -0,0 +1,36 @@
// create an AudioContext
// create an audio source node
// connect it to a filter node
// connect that a gain node
// play sound!

// cope with browser differences
var context;
if (typeof webkitAudioContext === "function") {
context = new webkitAudioContext();
} else if (typeof AudioContext === "function") {
context = new AudioContext();
} else {
alert("Sorry! Web Audio is not supported by this browser");
}

// use the audio element to create the source node
var audioElement = document.querySelector("audio");
var sourceNode = context.createMediaElementSource(audioElement);

// connect the source node to a filter node
var filterNode = context.createBiquadFilter();
// see https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#BiquadFilterNode-section
filterNode.type = 1; // HIGHPASS
// cutoff frequency: for HIGHPASS, audio is attenuated below this frequency
filterNode.frequency.value = 1760;
sourceNode.connect(filterNode);

// connect the filter node to a gain node (to change audio volume)
var gainNode = context.createGainNode();
// default is 1 (no change); less than 1 means audio is attenuated, and vice versa
gainNode.gain.value = 0.5;
filterNode.connect(gainNode);

// connect the gain node to the destination (i.e. play the sound)
gainNode.connect(context.destination);
5 changes: 5 additions & 0 deletions websql/index.html
Expand Up @@ -4,6 +4,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WebSQL</title>
<link rel="stylesheet" href="../css/main.css">

<style>
button {
width: 60px;
Expand Down Expand Up @@ -31,7 +32,11 @@
color: #ccc;
padding: 0 15px 0 15px;
}
p#data {
height: 150px;
}
</style>

</head>
<body>
<div id="container">
Expand Down
3 changes: 2 additions & 1 deletion websql/js/main.js
Expand Up @@ -33,8 +33,9 @@ function handleError(transaction, error) {
return false;
}

var dataElement = document.getElementById("data");
function log(message){
document.getElementById("data").innerHTML += message + "<br /><br />";
dataElement.innerHTML = message + "<br /><br />" + dataElement.innerHTML;
}


Expand Down
11 changes: 2 additions & 9 deletions xhr/index.html
Expand Up @@ -11,17 +11,10 @@
<h1><a href="../index.html" title="simpl.info home page">simpl.info</a> XMLHttpRequest (aka AJAX)</h1>

<p>This page gets the following data from <a href="data.json" title="Data file in JSON format">data.json</a>:</p>

<p id="data"></p>

<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open('GET', 'data.json');
xhr.onload = function(event) {
var data = JSON.parse(this.response);
document.querySelector("#data").innerHTML = JSON.stringify(data);
}
xhr.send();
</script>
<script src="js/main.js"></script>

<a href="view-source:http://simpl.info/xhr" title="View source for this page" id="viewSource">View source</a>
</div>
Expand Down
19 changes: 19 additions & 0 deletions xhr/js/main.js
@@ -0,0 +1,19 @@
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.json");

xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
document.querySelector("#data").innerHTML = JSON.stringify(data);
}
}

/*
// can do this in Chrome, Firefox, etc.:
xhr.onload = function(event) {
var data = JSON.parse(this.response);
document.querySelector("#data").innerHTML = JSON.stringify(data);
}
*/

xhr.send();

2 comments on commit f0c1802

@asgs
Copy link

@asgs asgs commented on f0c1802 Dec 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably too late to ask, but what's the point of doing a JSON.parse only to stringify in the next statement? File - xhr/js/main.js

@samdutton
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair question :).

The code is quite contrived, but it's really just to demonstrate how XHR can be used.

Please sign in to comment.