title: Web Audio API for live streaming?
body: <p>We need to streaming <strong>live</strong> audio (from a medical device) to web browsers with no more than 3-5s of end-to-end delay (assume 200mS or less network latency). Today we use a <strong>browser plugin</strong> (NPAPI) for <strong>decoding</strong>, <strong>filtering</strong> (high, low, band), and <strong>playback</strong> of the audio stream (delivered via Web Sockets).</p>
  
  <p><strong>We want to replace the plugin.</strong></p>
  
  <p>I was looking at various <a href="http://www.html5rocks.com/en/tutorials/webaudio/intro/" rel="noreferrer">Web Audio API demos</a> and the most of our required functionality (playback, gain control, filtering) appears to be available in <a href="http://webaudio.github.io/web-audio-api/" rel="noreferrer">Web Audio API</a>.  However, it is not clear to me if Web Audio API can be used for streamed sources as most of the Web Audio API makes use of short sounds and/or audio clips.</p>
  
  <p>Can Web Audio API be used to play live streamed audio?</p>
  
  <p><strong>Update (11-Feb-2015):</strong></p>
  
  <p>After a bit more research and local prototyping, I am not sure <strong>live audio streaming with Web Audio API</strong> is possible.  As Web Audio API's <strong>decodeAudioData</strong> isn't really designed to handle random chunks of audio data (in our case delivered via WebSockets).  It appears to need the whole 'file' in order to process it correctly.</p>
  
  <p>See stackoverflow: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8759842/how-to-stream-mp3-data-via-websockets-with-node-js-and-socket-io">How to stream MP3 data via WebSockets with node.js and socket.io?</a></li>
  <li><a href="https://stackoverflow.com/questions/10470742/define-valid-mp3-chunk-for-decodeaudiodata-webaudio-api">Define 'valid mp3 chunk' for decodeAudioData (WebAudio API)</a></li>
  </ul>
  
  <p>Now it is possible with <strong>createMediaElementSource</strong> to connect an <code>&lt;audio&gt;</code> element to Web Audio API, but it has been my experience that the <code>&lt;audio&gt;</code> element induces a huge amount of end-to-end delay (15-30s) and there doesn't appear to be any means to reduce the delay to below 3-5 seconds.</p>
  
  <p>I <em>think</em> the only solution is to use WebRTC with Web Audio API.  I was hoping to avoid WebRTC as it will require significant changes to our server-side implementation.</p>
  
  <p><strong>Update (12-Feb-2015) Part I</strong>:</p>
  
  <p>I haven't completely eliminated the <code>&lt;audio&gt;</code> tag (need to finish my prototype).  Once I have ruled it out, I suspect the createScriptProcessor (deprecated but still supported) will be a good choice for our environment as I could 'stream' (via WebSockets) our ADPCM data to the browser and then (in JavaScript) convert it to PCM.  Similar to what to Scott's library (see below) does using the createScriptProcessor.  This method doesn't require the data to be in properly sized 'chunks' and critical timing as the decodeAudioData approach.</p>
  
  <p><strong>Update (12-Feb-2015) Part II</strong>:</p>
  
  <p>After more testing, I eliminated the <code>&lt;audio&gt;</code> to Web Audio API interface because, depending on source type, compression and browser, the end-to-end delay can be 3-30s.  That leaves the createScriptProcessor method (See Scott's post below) or WebRTC.  After talking discussing with our decision makers, it has been decided we will take the WebRTC approach. I <em>assume</em> it will work. But it will require changes to our server side code.</p>
  
  <p>I'm going to mark the first answer, just so the 'question' is closed.</p>
  
  <p>Thanks for listening.  Feel free to add comments as needed.</p>