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