source: gs3-extensions/web-audio/trunk/js-mad/script/mad.js@ 28388

Last change on this file since 28388 was 28388, checked in by davidb, 11 years ago

Set of JS, CSS, PNG etc web resources to support a mixture of audio player/document display capabilities

File size: 4.9 KB
Line 
1
2
3
4/* Namespaces */
5Mad = {};
6Mad.ArrayBuffers = {};
7Mad.BinaryStrings = {};
8
9Mad.DEBUG = window.JSMAD_DEBUG || false;
10
11Mad.recoverable = function (error) {
12 return (error & 0xff00) != 0;
13};
14
15// change this value for testing
16Mad.enforceBinaryString = false;
17
18if (!Mad.enforceBinaryString && typeof(ArrayBuffer) === 'function' && typeof(Uint8Array) === 'function') {
19 Mad.DEBUG && console.log("JsMad: Using ArrayBuffer");
20 Mad.Storage = {
21 backing: 'arraybuffer',
22
23 newBuffer: function (length) {
24 return new Uint8Array(length);
25 },
26
27 memcpy: function (dst, dstOffset, pSrc, srcOffset, length) {
28 while (pSrc.parentStream) {
29 srcOffset += pSrc.start;
30 pSrc = pSrc.parentStream;
31 }
32 var src = pSrc.subarray ? pSrc : pSrc.buffer;
33 var subarr = src.subarray(srcOffset, srcOffset + length);
34
35 // oh my, memcpy actually exists in JavaScript?
36 dst.set(subarr, dstOffset);
37 return dst;
38 }
39 };
40 Mad.FileStream = function (file, callback) {
41 return new Mad.ArrayBuffers.FileStream(file, callback);
42 }
43 Mad.AjaxStream = function (file, callback) {
44 return new Mad.ArrayBuffers.AjaxStream(file, callback);
45 }
46} else {
47 console.log("JsMad: Using BinaryString");
48 Mad.Storage = {
49 backing: 'binarystring',
50
51 newBuffer: function (length) {
52 return Mad.mul("\0", length);
53 },
54
55 memcpy: function (dst, dstOffset, src, srcOffset, length) {
56 // this is a pretty weird memcpy actually - it constructs a new version of dst, because we have no other way to do it
57 return dst.slice(0, dstOffset) + src.slice(srcOffset, srcOffset + length) + dst.slice(dstOffset + length);
58 }
59 };
60 Mad.FileStream = function (file, callback) {
61 return new Mad.BinaryStrings.FileStream(file, callback);
62 }
63 Mad.AjaxStream = function (file, callback) {
64 return new Mad.BinaryStrings.AjaxStream(file, callback);
65 }
66}
67
68// credit: http://blog.stevenlevithan.com/archives/fast-string-multiply
69Mad.mul = function (str, num) {
70 var i = Math.ceil(Math.log(num) / Math.LN2), res = str;
71 do {
72 res += res;
73 } while (0 < --i);
74 return res.slice(0, str.length * num);
75};
76
77Mad.rshift = function (num, bits) {
78 return Math.floor(num / Math.pow(2, bits));
79};
80
81Mad.lshiftU32 = function (num, bits) {
82 return Mad.bitwiseAnd(Mad.lshift(num, bits), 4294967295 /* 2^32 - 1 */);
83};
84
85Mad.lshift = function (num, bits) {
86 return num * Math.pow(2, bits);
87};
88
89Mad.bitwiseOr = function (a, b) {
90 var w = 2147483648; // 2^31
91
92 var aHI = (a / w) << 0;
93 var aLO = a % w;
94 var bHI = (b / w) << 0;
95 var bLO = b % w;
96
97 return ((aHI | bHI) * w + (aLO | bLO));
98};
99
100Mad.bitwiseAnd = function (a, b) {
101 var w = 2147483648; // 2^31
102
103 var aHI = (a / w) << 0;
104 var aLO = a % w;
105 var bHI = (b / w) << 0;
106 var bLO = b % w;
107
108 return ((aHI & bHI) * w + (aLO & bLO));
109};
110
111/* Simple JavaScript Inheritance
112 * By John Resig http://ejohn.org/
113 * MIT Licensed.
114 */
115// Inspired by base2 and Prototype
116(function(){
117 var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
118 // The base Class implementation (does nothing)
119 this.Class = function(){};
120
121 // Create a new Class that inherits from this class
122 Class.extend = function(prop) {
123 var _super = this.prototype;
124
125 // Instantiate a base class (but only create the instance,
126 // don't run the init constructor)
127 initializing = true;
128 var prototype = new this();
129 initializing = false;
130
131 // Copy the properties over onto the new prototype
132 for (var name in prop) {
133 // Check if we're overwriting an existing function
134 prototype[name] = typeof prop[name] === "function" &&
135 typeof _super[name] === "function" && fnTest.test(prop[name]) ?
136 (function(name, fn){
137 return function() {
138 var tmp = this._super;
139
140 // Add a new ._super() method that is the same method
141 // but on the super-class
142 this._super = _super[name];
143
144 // The method only need to be bound temporarily, so we
145 // remove it when we're done executing
146 var ret = fn.apply(this, arguments);
147 this._super = tmp;
148
149 return ret;
150 };
151 })(name, prop[name]) :
152 prop[name];
153 }
154
155 // The dummy class constructor
156 function Class() {
157 // All construction is actually done in the init method
158 if ( !initializing && this.init )
159 this.init.apply(this, arguments);
160 }
161
162 // Populate our constructed prototype object
163 Class.prototype = prototype;
164
165 // Enforce the constructor to be what we expect
166 Class.prototype.constructor = Class;
167
168 // And make this class extendable
169 Class.extend = arguments.callee;
170
171 return Class;
172 };
173})();
174
175
Note: See TracBrowser for help on using the repository browser.