source: gs3-extensions/web-audio/trunk/js-mad/jsmad-master/src/stream.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: 3.9 KB
Line 
1
2Mad.Error = {
3 NONE : 0x0000, /* no error */
4
5 BUFLEN : 0x0001, /* input buffer too small (or EOF) */
6 BUFPTR : 0x0002, /* invalid (null) buffer pointer */
7
8 NOMEM : 0x0031, /* not enough memory */
9
10 LOSTSYNC : 0x0101, /* lost synchronization */
11 BADLAYER : 0x0102, /* reserved header layer value */
12 BADBITRATE : 0x0103, /* forbidden bitrate value */
13 BADSAMPLERATE : 0x0104, /* reserved sample frequency value */
14 BADEMPHASIS : 0x0105, /* reserved emphasis value */
15
16 BADCRC : 0x0201, /* CRC check failed */
17 BADBITALLOC : 0x0211, /* forbidden bit allocation value */
18 BADSCALEFACTOR : 0x0221, /* bad scalefactor index */
19 BADMODE : 0x0222, /* bad bitrate/mode combination */
20 BADFRAMELEN : 0x0231, /* bad frame length */
21 BADBIGVALUES : 0x0232, /* bad big_values count */
22 BADBLOCKTYPE : 0x0233, /* reserved block_type */
23 BADSCFSI : 0x0234, /* bad scalefactor selection info */
24 BADDATAPTR : 0x0235, /* bad main_data_begin pointer */
25 BADPART3LEN : 0x0236, /* bad audio data length */
26 BADHUFFTABLE : 0x0237, /* bad Huffman table select */
27 BADHUFFDATA : 0x0238, /* Huffman data overrun */
28 BADSTEREO : 0x0239 /* incompatible block_type for JS */
29};
30
31Mad.BUFFER_GUARD = 8;
32Mad.BUFFER_MDLEN = (511 + 2048 + Mad.BUFFER_GUARD);
33
34Mad.Stream = function (stream) {
35 this.stream = stream; /* actual buffer (js doesn't have pointers!) */
36 this.buffer = 0; /* input bitstream buffer */
37 this.bufend = stream.length; /* input bitstream buffer */
38 this.skiplen = 0; /* bytes to skip before next frame */
39
40 this.sync = 0; /* stream sync found */
41 this.freerate = 0; /* free bitrate (fixed) */
42
43 this.this_frame = 0; /* start of current frame */
44 this.next_frame = 0; /* start of next frame */
45
46 this.ptr = new Mad.Bit(this.stream, this.buffer); /* current processing bit pointer */
47
48 this.anc_ptr = /* MadBit */ null; /* ancillary bits pointer */
49 this.anc_bitlen = 0; /* number of ancillary bits */
50
51 this.main_data = /* string */ Mad.Storage.newBuffer(Mad.BUFFER_MDLEN); /* Layer III main_data() */
52 this.md_len = 0; /* bytes in main_data */
53
54 var options = 0; /* decoding options (see below) */
55 var error = Mad.Error.NONE; /* error code (see above) */
56};
57
58Mad.Stream.fromFile = function(file, callback) {
59 var reader = new FileReader();
60 reader.onloadend = function (evt) {
61 callback(new Mad.Stream(evt.target.result));
62 };
63 reader.readAsBinaryString(file);
64};
65
66Mad.Stream.prototype.readShort = function(bBigEndian) {
67 return this.stream.readU16(bBigEndian);
68};
69
70Mad.Stream.prototype.readSShort = function(bBigEndian) {
71 return this.stream.readI16(bBigEndian);
72};
73
74Mad.Stream.prototype.getU8 = function(index) {
75 return this.stream.getU8(index);
76};
77
78
79Mad.Stream.prototype.readU8 = function() {
80 return this.stream.readU8(index);
81};
82
83Mad.Stream.prototype.readChars = function(length) {
84 return this.stream.read(length);
85};
86
87Mad.Stream.prototype.peekChars = function(length) {
88 return this.stream.peek(length);
89}
90
91/*
92 * NAME: stream->sync()
93 * DESCRIPTION: locate the next stream sync word
94 */
95Mad.Stream.prototype.doSync = function() {
96 var ptr = this.ptr.nextbyte();
97 var end = this.bufend;
98
99 while (ptr < end - 1 && !(this.getU8(ptr) === 0xff && (this.getU8(ptr + 1) & 0xe0) === 0xe0)) {
100 ++ptr;
101 }
102
103 if (end - ptr < Mad.BUFFER_GUARD) {
104 return -1;
105 }
106
107 this.ptr = new Mad.Bit(this.stream, ptr);
108
109 return 0;
110}
111
112
Note: See TracBrowser for help on using the repository browser.