Created
October 6, 2011 11:47
-
-
Save jussi-kalliokoski/1267221 to your computer and use it in GitHub Desktop.
Methods for creating an audio tag filled with a specified time of silence.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(g){ | |
var urlPath = | |
window.URL ? 'URL' : | |
window.webkitURL ? 'webkitURL' : | |
window.mozURL ? 'mozURL' : | |
window.msURL ? 'msURL' : | |
window.oURL ? 'oURL' : ''; | |
var blobPath = | |
window.BlobBuilder ? 'BlobBuilder' : | |
window.WebKitBlobBuilder ? 'WebKitBlobBuilder' : | |
window.MozBlobBuilder ? 'MozBlobBuilder' : | |
window.MSBlobBuilder ? 'MSBlobBuilder' : | |
window.OBlobBuilder ? 'OBlobBuilder' : ''; | |
var char = String.fromCharCode; | |
function dWord(w){ | |
return char(0x000000FF & w) + | |
char((0x0000FF00 & w) >> 8) + | |
char((0x00FF0000 & w) >> 16) + | |
char((0xFF000000 & w) >> 24) ; | |
} | |
function sWord(w){ | |
return char(0x00FF & w) + | |
char((0xFF00 & w) >> 8); | |
} | |
/** | |
* Creates a dynamic url of the supplied data, using Blob URLs where available, falling back to Data URLs. | |
* | |
* @param {String} data The data to create the dynamic URL from. | |
* @param {String} mime The mime type of the data (default: application/octet). | |
*/ | |
function DynUrl(data, mime){ | |
var bb; | |
this.mime = mime || this.mime; | |
/* Try to use Blob URLs */ | |
if (/* OK, it's not working for some reason, <FIXME> */0&&/* /* </FIXME> */urlPath && blobPath){ | |
bb = new window[blobPath](); | |
bb.append(data); | |
this.url = window[urlPath].createObjectURL(bb.getBlob()); | |
this.type = 'blob'; | |
/* Fall back to Data URLs */ | |
} else { | |
this.url = 'data:' + mime + ';base64,' + btoa(data); | |
} | |
} | |
DynUrl.prototype = { | |
url: null, | |
mime: 'application/octet', | |
type: 'data', | |
revoke: function(){ | |
switch (type){ | |
case 'blob': | |
window[urlPath].revokeObjectURL(this.blob); break; | |
default: | |
/* Nothing to do here. */ | |
} | |
}, | |
toString: function(){ | |
return this.url; | |
} | |
}; | |
/** | |
* Generates an audio element of specified duration of silence. | |
* | |
* @param {Number} duration Duration of silence in seconds (default: 5). | |
* @return {Audio} The generated audio element. | |
*/ | |
function Silence(duration){ | |
this.duration = isNaN(duration) || duration === null ? this.duration : duration; | |
this.elem = document.createElement('audio'); | |
var sr = 11025, | |
l = ~~(this.duration * sr), | |
data = | |
'WAVE' + // RIFF ID | |
'fmt ' + // Chunk ID | |
dWord(16) + // Chunk size | |
sWord(1) + // Format | |
sWord(1) + // Channels | |
dWord(sr) + // Sample rate | |
dWord(sr) + // Byte rate (8 bit audio) | |
sWord(1) + // Block align (8 bit audio) | |
sWord(8) + // Bits per sample | |
'data' + // Chunk ID | |
dWord(l) + // Chunk size | |
Array(l+1).join('\x7F') ; // Data | |
data = | |
'RIFF' + // Group ID | |
dWord(data.length) + // File Length | |
data; | |
this.url = new DynUrl(data, 'audio/wav'); | |
this.elem.src = this.url; | |
} | |
Silence.prototype = { | |
elem: null, | |
url: null, | |
duration: 5, | |
/* Call this when discarding the audio to avoid memory leaks */ | |
destroy: function(){ | |
this.url.revoke(); | |
} | |
}; | |
g.DynUrl = DynUrl; | |
g.Silence = Silence; | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment