Permalink
Browse files

arraybuffer vs binarystring experiment. Probably breaks everything, b…

…ut hey, who's your daddy?
  • Loading branch information...
1 parent eee2622 commit 53daceac2e978aff17e2a5f01b745230fccf02c4 Amos Wenger committed Sep 29, 2011
@@ -14,10 +14,10 @@ var domReady = function ()
console.log("File loaded");
var mpeg = mp3.getMpegStream();
- console.log("Decoding first 2000 frames");
+ console.log("Decoding first 1 frames");
var frame = new Mad.Frame();
- for(var i = 0; i < 2000; i += 1) {
+ for(var i = 0; i < 1; i += 1) {
frame = Mad.Frame.decode(frame, mpeg);
}
console.log("Done!");
View
@@ -12,16 +12,21 @@
<script type="text/javascript" src="/src/layer3.js"></script>
<script type="text/javascript" src="/src/frame.js"></script>
<script type="text/javascript" src="/src/synth.js"></script>
-
- <script type="text/javascript" src="/src/bytestream.js"></script>
- <script type="text/javascript" src="/src/filestream.js"></script>
- <script type="text/javascript" src="/src/substream.js"></script>
+
+ <script type="text/javascript" src="/src/arraybuffer/bytestream.js"></script>
+ <script type="text/javascript" src="/src/arraybuffer/filestream.js"></script>
+ <script type="text/javascript" src="/src/arraybuffer/substream.js"></script>
+
+ <!--
+ <script type="text/javascript" src="/src/binarystring/bytestream.js"></script>
+ <script type="text/javascript" src="/src/binarystring/filestream.js"></script>
+ <script type="text/javascript" src="/src/binarystring/substream.js"></script>
+ -->
+
<script type="text/javascript" src="/src/id3v22stream.js"></script>
<script type="text/javascript" src="/src/id3v23stream.js"></script>
<script type="text/javascript" src="/src/mp3file.js"></script>
- <script type="text/javascript" src="/src/ajaxstream.js"></script>
- <script type="text/javascript" src="/src/stringstream.js"></script>
<script type="text/javascript" src="/src/player.js"></script> </head>
<body>
<div id="content">
@@ -146,7 +146,7 @@ Mad.AjaxStream.prototype.get = function(offset, length) {
}
}
-Mad.ByteStream.prototype.getU8 = function(offset, bigEndian) {
+Mad.AjaxStream.prototype.getU8 = function(offset, bigEndian) {
if(this.state.byteBuffer) {
return this.state.byteBuffer[offset];
}
@@ -0,0 +1,166 @@
+Mad.ByteStream = function(url) { }
+
+Mad.ByteStream.prototype.available = function(n) {
+ return this.absoluteAvailable(this.state.offset + n);
+}
+
+Mad.ByteStream.prototype.strEquals = function (offset, string) {
+ for (var i = 0; i < string.length; i++) {
+ var left = this.getU8(offset + i, false);
+ var right = string.charCodeAt(i);
+
+ console.log(left + " vs " + right);
+ if(left != right) return false;
+ }
+ return true;
+}
+
+Mad.ByteStream.prototype.getU8 = function(offset, bigEndian) {
+ var bytes = this.get(offset, 1);
+ console.log("get(" + offset + ") = " + bytes[0]);
+ return bytes[0];
+}
+
+Mad.ByteStream.prototype.getU16 = function(offset, bigEndian) {
+ var bytes = this.get(offset, 2);
+
+ if (!bigEndian) {
+ bytes = bytes.reverse();
+ }
+
+ return (bytes[0] << 8) | bytes[1];
+}
+
+Mad.ByteStream.prototype.getU24 = function(offset, bigEndian) {
+ var bytes = this.get(offset, 3);
+
+ if (!bigEndian) {
+ bytes = bytes.reverse();
+ }
+
+ return (bytes[0] << 16) | (bytes[1] << 8) | bytes[2];
+}
+
+Mad.ByteStream.prototype.getU32 = function(offset, bigEndian) {
+ var bytes = this.get(offset, 4);
+
+ if (!bigEndian) {
+ bytes = bytes.reverse();
+ }
+
+ return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
+}
+
+Mad.ByteStream.prototype.getI8 = function(offset, bigEndian) {
+ return this.getU8(offset, bigEndian) - 128; // 2 ** 7
+}
+
+Mad.ByteStream.prototype.getI16 = function(offset, bigEndian) {
+ return this.getU16(offset, bigEndian) - 65536; // 2 ** 15
+}
+
+Mad.ByteStream.prototype.getI32 = function(offset, bigEndian) {
+ return this.getU32(offset, bigEndian) - 2147483648; // 2 ** 31
+}
+
+Mad.ByteStream.prototype.getSyncInteger = function(offset) {
+ var bytes = this.get(offset, 4);
+
+ return (bytes[0] << 21) | (bytes[1] << 14) | (bytes[2] << 7) | bytes[3];
+}
+
+Mad.ByteStream.prototype.peekU8 = function(bigEndian) {
+ return this.getU8(this.state.offset, bigEndian);
+}
+
+Mad.ByteStream.prototype.peekU16 = function(bigEndian) {
+ return this.getU16(this.state.offset, bigEndian);
+}
+
+Mad.ByteStream.prototype.peekU24 = function(bigEndian) {
+ return this.getU24(this.state.offset, bigEndian);
+}
+
+Mad.ByteStream.prototype.peekU32 = function(bigEndian) {
+ return this.getU32(this.state.offset, bigEndian);
+}
+
+Mad.ByteStream.prototype.peekI8 = function(bigEndian) {
+ return this.getI8(this.state.offset, bigEndian);
+}
+
+Mad.ByteStream.prototype.peekI16 = function(bigEndian) {
+ return this.getI16(this.state.offset, bigEndian);
+}
+
+Mad.ByteStream.prototype.peekI32 = function(bigEndian) {
+ return this.getI32(this.state.offset, bigEndian);
+}
+
+Mad.ByteStream.prototype.peekSyncInteger = function() {
+ return this.getSyncInteger(this.state.offset);
+}
+
+Mad.ByteStream.prototype.readU8 = function(bigEndian) {
+ var result = this.peekU8(bigEndian);
+
+ this.seek(1);
+
+ return result;
+}
+
+Mad.ByteStream.prototype.readU16 = function(bigEndian) {
+ var result = this.peekU16(bigEndian);
+
+ this.seek(2);
+
+ return result;
+}
+
+Mad.ByteStream.prototype.readU24 = function(bigEndian) {
+ var result = this.peekU24(bigEndian);
+
+ this.seek(3);
+
+ return result;
+}
+
+Mad.ByteStream.prototype.readU32 = function(bigEndian) {
+ var result = this.peekU32(bigEndian);
+
+ this.seek(4);
+
+ return result;
+}
+
+Mad.ByteStream.prototype.readI8 = function(bigEndian) {
+ var result = this.peekI8(bigEndian);
+
+ this.seek(1);
+
+ return result;
+}
+
+Mad.ByteStream.prototype.readI16 = function(bigEndian) {
+ var result = this.peekI16(bigEndian);
+
+ this.seek(2);
+
+ return result;
+}
+
+Mad.ByteStream.prototype.readI32 = function(bigEndian) {
+ var result = this.peekI32(bigEndian);
+
+ this.seek(4);
+
+ return result;
+}
+
+Mad.ByteStream.prototype.readSyncInteger = function() {
+ var result = this.getSyncInteger(this.state.offset);
+
+ this.seek(4);
+
+ return result;
+}
@@ -4,10 +4,11 @@ Mad.FileStream = function(file, callback) {
var self = this, reader = new FileReader();
reader.onload = function () {
+ console.log("Read whole file!");
self.state.buffer = reader.result;
self.state.mainView = new Uint8Array(self.state.buffer);
- self.state.amountRead = self.state.buffer.length;
- self.state.contentLength = self.state.buffer.length;
+ self.state.amountRead = self.state.buffer.byteLength;
+ self.state.contentLength = self.state.buffer.byteLength;
self.length = self.state.amountRead;
@@ -27,9 +28,8 @@ Mad.FileStream.prototype.absoluteAvailable = function(n, updated) {
return n < this.state.amountRead;
}
-Mad.ByteStream.prototype.getU8 = function(offset, bigEndian) {
- var bytes = this.get(offset, 1);
- return bytes.charCodeAt(0);
+Mad.FileStream.prototype.getU8 = function(offset, bigEndian) {
+ return this.state.mainView[offset];
}
@@ -59,7 +59,7 @@ Mad.FileStream.prototype.peek = function(n) {
Mad.FileStream.prototype.get = function(offset, length) {
if (offset + length < this.state.contentLength) {
- return this.state.buffer.slice(offset, offset + length);
+ return new Uint8Array(this.state.buffer, offset, length);
} else {
throw 'TODO: THROW GET ERROR!';
}
File renamed without changes.
@@ -0,0 +1,47 @@
+Mad.SubStream = function(stream, offset, length) {
+ this.state = { 'offset': 0 };
+
+ this.state.start = offset;
+
+ this.parentStream = stream;
+
+ this.length = length;
+}
+
+Mad.SubStream.prototype = new Mad.ByteStream;
+
+Mad.SubStream.prototype.absoluteAvailable = function(n) {
+ return this.parentStream.absoluteAvailable(this.state.start + n);
+}
+
+Mad.SubStream.prototype.seek = function(n) {
+ this.state.offset += n;
+}
+
+Mad.SubStream.prototype.read = function(n) {
+ var result = this.peek(n);
+
+ this.seek(n);
+
+ return result;
+}
+
+Mad.SubStream.prototype.peek = function(n) {
+ return this.get(this.state.offset, n);
+}
+
+Mad.SubStream.prototype.get = function(offset, length) {
+ return this.parentStream.get(this.state.start + offset, length);
+}
+
+Mad.SubStream.prototype.slice = function(start, end) {
+ return this.parentStream.get(this.state.start + start, end - start);
+}
+
+Mad.SubStream.prototype.requestAbsolute = function(n, callback) {
+ this.parentStream.requestAbsolute(this.state.start + n)
+}
+
+Mad.SubStream.prototype.request = function(n, callback) {
+ this.parentStream.requestAbsolute(this.state.start + this.state.offset + n)
+}
Oops, something went wrong.

0 comments on commit 53dacea

Please sign in to comment.