Wednesday, November 30th, -0001

HTML5 Audio Tutorial: Rotating Channels

Category: HTML, Sound

Thomas Sturm continues to add great content to his HTML5 stories in flight. His latest talks on HTML 5 Audio and goes into a tactic to implement rotating channels:

Here is one solution to overcome the single-channel limitation of the audio tag: Use multiple rotating audio channels and assign new sounds to currently unused channels. Click the links above rapidly to test this.

In the example above we use 10 channels (generated audio objects) and whenever the user clicks another sound to play, the script finds an inactive (and therefore unblocked) channel and then loads and plays the selected sound.

Each of the sounds is being preloaded with an audio html tag that is actually never used to play the sound – the autobuffer=”autobuffer” property forces the browser to load all of the sounds when the page loads, instead of when the sound is played for the first time through one of the generated audio channels.

The script checks each channel if it is done playing the previous sound. There is an “ended” property for the audio object, but since that property is “false” when new objects are created (which is correct, but inconvenient), I’ve decided to keep track of the expected end times for each sound channel instead.

  1. <audio id="multiaudio1" src="audio/flute_c_long_01.wav" autobuffer="autobuffer"></audio>
  2. <audio id="multiaudio2" src="audio/piano_chord.wav" autobuffer="autobuffer"></audio>
  3. <audio id="multiaudio3" src="audio/synth_vox.wav" autobuffer="autobuffer"></audio>
  4. <audio id="multiaudio4" src="audio/shimmer.wav" autobuffer="autobuffer"></audio>
  5. <audio id="multiaudio5" src="audio/sweep.wav" autobuffer="autobuffer"></audio>
  6.  
  7. <a href="javascript:play_multi_sound('multiaudio1');">Flute</a><br />
  8. <a href="javascript:play_multi_sound('multiaudio2');">Piano Chord</a><br />
  9. <a href="javascript:play_multi_sound('multiaudio3');">Synth Vox</a><br />
  10. <a href="javascript:play_multi_sound('multiaudio4');">Shimmer</a><br />
  11. <a href="javascript:play_multi_sound('multiaudio5');">Sweep</a><br />
  12.  
  13. <script type="text/javascript">
  14.     var channel_max = 10;                                       // number of channels
  15.     audiochannels = new Array();
  16.     for (a=0;a<channel_max ;a++) {                                  // prepare the channels
  17.         audiochannels&#91;a&#93; = new Array();
  18.         audiochannels&#91;a&#93;&#91;'channel'&#93; = new Audio();                      // create a new audio object
  19.         audiochannels&#91;a&#93;&#91;'finished'&#93; = -1;                          // expected end time for this channel
  20.     }
  21.     function play_multi_sound(s) {
  22.         for (a=0;a<audiochannels.length;a++) {
  23.             thistime = new Date();
  24.             if (audiochannels&#91;a&#93;&#91;'finished'&#93; < thistime.getTime()) {            // is this channel finished?
  25.                 audiochannels&#91;a&#93;&#91;'finished'&#93; = thistime.getTime() + document.getElementById(s).duration*1000;
  26.                 audiochannels&#91;a&#93;&#91;'channel'&#93;.src = document.getElementById(s).src;
  27.                 audiochannels&#91;a&#93;&#91;'channel'&#93;.load();
  28.                 audiochannels&#91;a&#93;&#91;'channel'&#93;.play();
  29.                 break;
  30.             }
  31.         }
  32.     }
  33. </script>

Posted by Dion Almaer at 12:00 am
Comment here

+++--
3.5 rating from 4 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.