radio/libjs/wController/file/audio.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-12-17 17:15:58 +00:00
"use strict";
var File = require("./file");
var Vocabulary = require("../../wType/vocabulary");
var Vector = require("../../wType/vector");
2018-12-17 17:15:58 +00:00
var Uint64 = require("../../wType/uint64");
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();
2018-12-17 17:15:58 +00:00
this.addHandler("responseFrames");
},
destructor: function() {
this._frames.destructor();
File.fn.destructor.call(this);
},
2018-12-17 17:15:58 +00:00
hasMore: function() {
return this._totalFrames > this._loadedFrames;
},
_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;
},
_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;
}
2018-12-17 17:15:58 +00:00
this._loadedFrames += amount;
this._waitingForFrames = false;
this.trigger("newFrames", buffer);
2018-12-17 17:15:58 +00:00
}
}
},
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;
}
}
}
}
});
var framePortion = 10;
module.exports = Audio;