Decoding with my own translation of libmad, loading and playback progress, next and prev songs

This commit is contained in:
Blue 2019-01-24 22:54:16 +03:00 committed by Gitea
parent baa6f4ef23
commit d6e22f6111
21 changed files with 290 additions and 11796 deletions

View file

@ -9,79 +9,16 @@ var Audio = File.inherit({
className: "Audio",
constructor: function Audio(addr) {
File.fn.constructor.call(this, addr);
this._loadedFrames = 0;
this._totalFrames = 0;
this._waitingForFrames = false;
this._frames = new Vector();
this.addHandler("responseFrames");
},
destructor: function() {
this._frames.destructor();
File.fn.destructor.call(this);
},
hasMore: function() {
return this._totalFrames > this._loadedFrames;
return this.getSize() > this.data.length();
},
_getAdditional: function(add) {
var ac = File.fn._getAdditional.call(this, add);
if (ac) {
this._loadedFrames = 0;
this._totalFrames = this._additional.at("framesAmount").valueOf();
this._waitingForFrames = false;
}
return ac;
getBitrate: function() {
return this._additional.at("bitrate").valueOf();
},
_h_responseFrames: function(ev) {
if (this._waitingForFrames === true) {
var data = ev.getData();
var success = data.at("result").valueOf();
if (success === 0) {
var frames = data.at("frames");
var amount = frames.length();
var buffer = new ArrayBuffer(0);
for (var i = 0; i < amount; ++i) {
var blob = frames.at(i).clone();
this._frames.push(blob);
var frame = blob.valueOf();
var newArr = new Uint8Array(buffer.byteLength + frame.byteLength);
newArr.set(new Uint8Array(buffer), 0);
newArr.set(new Uint8Array(frame), buffer.byteLength);
buffer = newArr.buffer;
}
this._loadedFrames += amount;
this._waitingForFrames = false;
this.trigger("newFrames", buffer);
}
}
},
requestMore: function() {
if (!this._waitingForFrames) {
if (this._registered && this._subscribed) {
var allowed = this._totalFrames - this._loadedFrames;
if (allowed > 0) {
var vc = new Vocabulary();
vc.insert("index", new Uint64(this._loadedFrames));
vc.insert("amount", new Uint64(Math.min(framePortion, allowed)));
this.send(vc, "requestFrames");
this._waitingForFrames = true;
}
}
}
getDuration: function() {
return this._additional.at("duration").valueOf();
}
});
var framePortion = 10;
module.exports = Audio;

View file

@ -2,6 +2,8 @@
var Controller = require("../controller");
var WVocabulary = require("../../wType/vocabulary");
var Uint64 = require("../../wType/uint64");
var Blob = require("../../wType/blob");
var File = Controller.inherit({
"className": "File",
@ -10,17 +12,17 @@ var File = Controller.inherit({
this._hasData = false;
this._hasAdditional = false;
this.data = null;
this.data = new Blob();
this._additional = null;
this._waitingForSlice = false;
this._need = 0;
this.addHandler("get");
this.addHandler("getAdditional");
this.addHandler("getSlice");
},
"destructor": function() {
if (this._hasData) {
this.data.destructor();
}
this.data.destructor();
if (this._hasAdditional) {
this._additional.destructor();
}
@ -46,6 +48,9 @@ var File = Controller.inherit({
"getMimeType": function() {
return this._additional.at("mimeType").toString();
},
"getSize": function() {
return this._additional.at("size").valueOf();
},
"_h_get": function(ev) {
var dt = ev.getData();
@ -55,7 +60,9 @@ var File = Controller.inherit({
}
this._hasData = true;
var oldData = this.data;
this.data = dt.at("data").clone();
oldData.destructor();
this.trigger("data");
},
"_h_getAdditional": function(ev) {
@ -69,6 +76,24 @@ var File = Controller.inherit({
this.trigger("ready");
}
},
"_h_getSlice": function(ev) {
if (this._waitingForSlice) {
this._waitingForSlice = false;
var vc = ev.getData();
if (vc.at("result").valueOf() == 0) {
var slice = vc.at("slice");
this.data["+="](slice);
this.trigger("slice", slice);
if (this.getSize() === this.data.length()) {
this._hasData = true;
this.trigger("data");
}
} else {
this.trigger("serviceMessage", "Error receiving slice from " + this._pairAddress.toString(), 1);
}
}
},
"needData": function() {
if (this._need === 0) {
var vc = new WVocabulary();
@ -77,6 +102,30 @@ var File = Controller.inherit({
}
++this._need;
},
"requestSlice": function(size) {
if (this._hasAdditional) {
if (this._waitingForSlice) {
throw new Error("An attempt to request a slice of data from " + this._pairAddress.toString() + " while another request is in progress");
}
var begin = this.data.length();
var newSize = Math.min(size, this.getSize() - begin);
if (newSize !== size) {
//TODO may be inform the developer about that?
}
if (newSize === 0) {
return; //TODO may be inform the developer about that?
}
var vc = new WVocabulary();
vc.insert("begin", new Uint64(begin));
vc.insert("size", new Uint64(newSize));
this.send(vc, "getSlice");
this._waitingForSlice = true;
} else {
throw new Error("An attempt to request a slice of data from " + this._pairAddress.toString() + " before controller got initialized");
}
},
"subscribe": function() {
Controller.fn.subscribe.call(this);