Jump to content

sounds as data uri


Recommended Posts

I stumbled upon this site recently: http://codebase.es/riffwave/


It shows how to generate sounds and play them in pure javascript using the old HTML Audio tag with data uri as source (inline base64 encoding)

Means it should work on all modern browsers (exception below) even if they do not support the new Web Audio API !!

So should be possible to create for example a mod player that is (nearly) cross platform (compare FlodJS: http://www.photonstorm.com/flod).


Just too bad that Mobile Safari does not support audio as data uri though :( :(




Another example of this technique: http://www.p01.org/releases/JS1K_Speech_Synthesizer/

Link to comment
Share on other sites

I've heavily used this technique for my SFMaker application, mostly because the web audio API wasn't an option back when it was written.  It does work fairly well.   It is notable that IE doesn't support this technique either, since they don't support WAV audio, despite Microsoft's role in popularizing the format.   Hopefully at some point they'll change their position on that.     Mobile is always hit or miss on audio, even with this technique, but it is getting better.   On the desktop side I have fairly complicated games fully generating their audio procedurally on init using this and then playing all sounds out of <audio> tags, and it actually works pretty well. 


For anyone looking to build WAVs on their own (even to just better understand the data section,) I found this very useful:




I have a WaveFile.js that I wrote that is somewhat similar to the linked riffwave.js.  I've noticed riffwave.js always uses its own base64 encoder.   My code has a base64 encoder as a fallback, but when possible uses the browser's btoa(), which I'm guessing is notably faster, though I may be wrong.   Otherwise, the end result is probably about the same regardless of which one is used.


WaveFile.js is part of SFMaker, and the new open source version has not yet been released.   There is an old version out there, but the new one is much better.  In case anyone is interested, I just made an SFMaker repository and placed WaveFile.js from the newer version up on github so that it can be immediately used by anyone looking for this sort of thing.  It can be found here:




That is under LGPL.  I've been thinking with the new release I may switch to a much more permissive license, but that won't be until I put together the new release.   riffwave.js is listed as being Public Domain, so it may currently have a nicer license.


Example usage:

var waveFile = new WaveFile();// Set the format as (SampleRate, bitsPerSecond, numChannels)// Default is (8000, 8, 1) if this call is left outwaveFile.setFormat(11025, 8, 1); // Create your waveform data and add it to the waveFilewaveFile.data = data;//  From here you have options://  To play the sound and return a generated audio tag:var audio = waveFile.play();//  To just return an audio tagvar audio = waveFile.generateAudioTag();//  Or to return the base64 (which is extremely useful if you//  plan on generating multiple Audio tags, since it can just be//  set to the src of each, which is far faster than regenerating//  it repeatedly.)var base64 = waveFile.generateBase64();

I threw these notes and this release of WaveFile.js together hastily for the purpose of this post.  It is late, and I may have made mistakes.   If anyone has any questions, just let me know.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...