1 |
|
---|
2 | Mad.Player = function (stream) {
|
---|
3 | this.stream = stream;
|
---|
4 | this.mp3 = new Mad.MP3File(stream);
|
---|
5 | this.id3 = this.mp3.getID3v2Stream();
|
---|
6 | this.mpeg = this.mp3.getMpegStream();
|
---|
7 |
|
---|
8 | this.totalLength = this.id3 && ~~this.id3.toHash()['Length'];
|
---|
9 |
|
---|
10 | // default onProgress handler
|
---|
11 | this.onProgress = function (playtime, total, preloaded) {
|
---|
12 | console.log("playtime = " + playtime + " / " + total + ", preloaded = " + preloaded);
|
---|
13 | }
|
---|
14 | };
|
---|
15 |
|
---|
16 | // Create a device.
|
---|
17 | Mad.Player.prototype.createDevice = function() {
|
---|
18 | var synth = new Mad.Synth();
|
---|
19 | this.frame = new Mad.Frame();
|
---|
20 | this.frame = Mad.Frame.decode(this.frame, this.mpeg);
|
---|
21 | if (this.frame === null) {
|
---|
22 | if (this.mpeg.error === Mad.Error.BUFLEN) {
|
---|
23 | console.log("End of file!");
|
---|
24 | }
|
---|
25 |
|
---|
26 | console.log("First error! code = " + this.mpeg.error + ", recoverable ? = " + Mad.recoverable(this.mpeg.error));
|
---|
27 | return;
|
---|
28 | }
|
---|
29 |
|
---|
30 | this.channelCount = this.frame.header.nchannels();
|
---|
31 | this.sampleRate = this.frame.header.samplerate;
|
---|
32 |
|
---|
33 | console.log("this.playing " + this.channelCount + " channels, samplerate = " + this.sampleRate + " audio, mode " + this.frame.header.mode);
|
---|
34 |
|
---|
35 | this.offset = 0;
|
---|
36 | this.absoluteFrameIndex = 0;
|
---|
37 | synth.frame(this.frame);
|
---|
38 |
|
---|
39 | this.lastRebuffer = Date.now();
|
---|
40 | this.playing = false;
|
---|
41 | this.progress();
|
---|
42 |
|
---|
43 | var self = this;
|
---|
44 | var MAX_FRAMES_IN_BUFFER = 40;
|
---|
45 |
|
---|
46 | this.refill = function (sampleBuffer) {
|
---|
47 | //console.log("delta = " + (Date.now() - self.lastRebuffer) + ", asked for " + sampleBuffer.length);
|
---|
48 | self.lastRebuffer = Date.now();
|
---|
49 |
|
---|
50 | if(!self.playing) return; // empty sampleBuffer, no prob
|
---|
51 |
|
---|
52 | var index = 0;
|
---|
53 |
|
---|
54 | while (index < sampleBuffer.length) {
|
---|
55 | for (var i = 0; i < self.channelCount; ++i) {
|
---|
56 | sampleBuffer[index++] = synth.pcm.samples[i][self.offset];
|
---|
57 | }
|
---|
58 |
|
---|
59 | self.offset++;
|
---|
60 |
|
---|
61 | if (self.offset >= synth.pcm.samples[0].length) {
|
---|
62 | self.offset = 0;
|
---|
63 |
|
---|
64 | self.frame = Mad.Frame.decode(self.frame, self.mpeg);
|
---|
65 | if (self.frame === null) {
|
---|
66 | if (self.stream.error === Mad.Error.BUFLEN) {
|
---|
67 | console.log("End of file!");
|
---|
68 | }
|
---|
69 | console.log("Error! code = " + self.mpeg.error);
|
---|
70 | self.playing = false;
|
---|
71 | self.onProgress(1.0, 1.0, 1.0);
|
---|
72 | self.dev.kill();
|
---|
73 | } else {
|
---|
74 | synth.frame(self.frame);
|
---|
75 | self.absoluteFrameIndex++;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (self.onPostProcessing) {
|
---|
81 | self.onPostProcessing.apply(this, arguments);
|
---|
82 | }
|
---|
83 |
|
---|
84 | };
|
---|
85 |
|
---|
86 | this.reinitDevice();
|
---|
87 | };
|
---|
88 |
|
---|
89 | Mad.Player.prototype.reinitDevice = function() {
|
---|
90 | if(this.dev) this.dev.kill();
|
---|
91 | // The default value is the safest
|
---|
92 | var bufferSize = null;
|
---|
93 | var self = this;
|
---|
94 |
|
---|
95 | var temp = Sink.sinks.moz.prototype.interval;
|
---|
96 | Sink.sinks.moz.prototype.interval = 100;
|
---|
97 |
|
---|
98 | this.dev = Sink(function(){
|
---|
99 | return self.refill.apply(this, arguments);
|
---|
100 | }, this.channelCount, bufferSize, this.sampleRate);
|
---|
101 |
|
---|
102 | this.dev.on && this.dev.on('error', function (e) {
|
---|
103 | console.log(e);
|
---|
104 | });
|
---|
105 |
|
---|
106 | Sink.sinks.moz.prototype.interval = temp;
|
---|
107 | }
|
---|
108 |
|
---|
109 | Mad.Player.prototype.setPlaying = function(playing) {
|
---|
110 | this.playing = playing;
|
---|
111 | if(playing) {
|
---|
112 | this.onPlay();
|
---|
113 | } else {
|
---|
114 | this.onPause();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | Mad.Player.prototype.destroy = function() {
|
---|
119 | clearTimeout(this.progressTimeout);
|
---|
120 | if(this.dev) {
|
---|
121 | this.dev.kill();
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | Mad.Player.prototype.progress = function () {
|
---|
126 | var delta = Date.now() - this.lastRebuffer;
|
---|
127 |
|
---|
128 | var playtime = ((this.absoluteFrameIndex * 1152 + this.offset) / this.sampleRate) + delta / 1000.0;
|
---|
129 | //console.log("delta = " + delta + ", contentLength = " + this.stream.contentLength + ", this.offset = " + this.mpeg.this_frame);
|
---|
130 | var total = this.totalLength ? this.totalLength : playtime * this.stream.contentLength / this.mpeg.this_frame;
|
---|
131 | var preloaded = this.stream.amountRead / this.stream.contentLength;
|
---|
132 | //console.log("amountRead = " + this.stream.amountRead + ", preloaded = " + preloaded);
|
---|
133 | this.onProgress(playtime, total, preloaded);
|
---|
134 |
|
---|
135 | var that = this;
|
---|
136 | var nextCall = function() { that.progress(); };
|
---|
137 | this.progressTimeout = setTimeout(nextCall, 250);
|
---|
138 | }
|
---|
139 |
|
---|
140 | Mad.Player.fromFile = function (file, callback) {
|
---|
141 | new Mad.FileStream(file, function (stream) {
|
---|
142 | callback(new Mad.Player(stream));
|
---|
143 | });
|
---|
144 | };
|
---|
145 |
|
---|
146 | Mad.Player.fromURL = function (url, callback) {
|
---|
147 | var stream = new Mad.AjaxStream(url);
|
---|
148 | stream.requestAbsolute(1 * 1024, function () {
|
---|
149 | callback(new Mad.Player(stream));
|
---|
150 | });
|
---|
151 | };
|
---|