Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
HTML5 Zone is brought to you in partnership with:

I'm a Worldwide Developer Evangelist for Adobe. My job basically entails me traveling the world and talking about the developer tools and technologies that Adobe has to offer or that we support. I'm also the author of Driving Technical Change. It's about convinving reluctant co-workers to adopt new tools and techniques. Terrence is a DZone MVB and is not an employee of DZone and has posted 30 posts at DZone. You can read more from them at their website. View Full User Profile

Inception Score Easter Egg with Web Audio API

02.16.2012
Email
Views: 1195
  • submit to reddit
The HTML5 Microzone is presented by DZone and Microsoft to bring you the most interesting and relevant content on emerging web standards.  Experience all that the HTML5 Microzone has to offer on our homepage and check out the cutting edge web development tutorials on Script Junkie, Build My Pinned Site, and the HTML5 DevCenter.

There's a great video on YouTube detailing an Easter Egg in the score for the movie Inception.  Basically Inception is about dreams and the slowing down of time. Likewise the score is based on the slowing down of music that is played inside the plot of the movie.  Pretty cool. Feel free to check out the video before continuing.

I wanted to use this concept to show off the Audio capabilities in HTML5. Basically I want to:

  • Play the Inception Score
  • Play the Edith Piaf song
  • Play the Edit Piaf song slowed down
  • Play the Inception score over the slowed down Edith Piaf song.

But the vanilla tag didn't work for me. The tag does have the ability to be slowed down, but it seems that in Chrome and Safari, I could not get the rate below 50% of the original. On Chrome the sound stopped playing if the rate was below 50%, and on Safari the sound just never got slower even though the rate was below 50%.

I figured I would give the Web Audio API a try and see what it could do. So I wrote a function that could play back a sound at a given rate:

function playAudio(file, speed){	
	if (typeof AudioContext == "function") {
		var audioContext = new AudioContext();
	} else if (typeof webkitAudioContext == "function") {
		var audioContext = new webkitAudioContext();
	}

	var source = audioContext.createBufferSource();
	source.connect(audioContext.destination);


	var xhr = new XMLHttpRequest();
	xhr.open("GET", file, true);
	xhr.responseType = "arraybuffer";
	xhr.onload = function() {
		var buffer = audioContext.createBuffer(xhr.response, false);
		source.buffer = buffer;
		source.playbackRate.value = speed;
		source.noteOn(0);
	};
	xhr.send();
}

That worked. All I had to do was set up some HTML to display it, some CSS to make it look less plain, and some buttons to make it work.

There's a working demo here:

Audio API Demo

Oh before you click on that link - It only works on Chrome. And sometimes it has the tendency to stop working all together. The fix is to empty your browser cache, and restart Chrome. But hey, still a cool proof of concept.

 

Source: http://www.terrenceryan.com/blog/post.cfm/inception-score-easter-egg-with-web-audio-api

 

Published at DZone with permission of Terrence Ryan, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

HTML5 is the most dramatic step in the evolution of web standards. It incorporates features such as geolocation, video playback and drag-and-drop. HTML5 allows developers to create rich internet applications without the need for third party APIs and browser plug-ins.  Under the banner of HTML5, modern web standards such as CSS3, SVG, XHR2, WebSockets, IndexedDB, and AppCache are pushing the boundaries for what a browser can achieve using web standards.  This Microzone is supported by Microsoft, and it will delve into the intricacies of using these new web technologies and teach you how to make your websites compatible with all of the modern browsers.