handled audio playback with aurora library

This commit is contained in:
Blue 2018-12-22 00:21:12 +03:00
parent 9b5198a6ce
commit 22323b504f
13 changed files with 11711 additions and 41 deletions

View file

@ -2,6 +2,7 @@
var File = require("./file");
var Vocabulary = require("../../wType/vocabulary");
var Vector = require("../../wType/vector");
var Uint64 = require("../../wType/uint64");
var Audio = File.inherit({
@ -12,10 +13,15 @@ var Audio = File.inherit({
this._loadedFrames = 0;
this._totalFrames = 0;
this._waitingForFrames = false;
this._portions = [];
this._frames = new Vector();
this.addHandler("responseFrames");
},
destructor: function() {
this._frames.destructor();
File.fn.destructor.call(this);
},
hasMore: function() {
return this._totalFrames > this._loadedFrames;
},
@ -37,12 +43,25 @@ var Audio = File.inherit({
var success = data.at("result").valueOf();
if (success === 0) {
var amount = data.at("amount").valueOf();
var blob = data.at("data").clone();
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._portions.push(blob);
this.trigger("newFrames", blob);
this.trigger("newFrames", buffer);
}
}
},

View file

@ -12,6 +12,12 @@ var Audio = require("./file/audio");
var Enum = require("../utils/enum");
var StateMachine = require("../utils/stateMachine");
var Source = AV.EventEmitter.extend(function() {
this.prototype.start = function(){}
this.prototype.pause = function(){}
this.prototype.reset = function(){}
});
var Player = Controller.inherit({
className: "Player",
constructor: function(addr) {
@ -21,11 +27,11 @@ var Player = Controller.inherit({
this.views = Object.create(null);
this.mode = PlayerMode.straight.playback;
this._audio = null;
this._sound = new window.Audio();
this._ctx = new AudioContext();
this._currentTime = 0;
this._source = new Source();
this._asset = new AV.Asset(this._source);
this._player = new AV.Player(this._asset);
this._player.play();
this._createStateMachine();
this._proxySchedule = this._schedule.bind(this);
this.addHandler("get");
this.addHandler("viewsChange");
@ -34,8 +40,7 @@ var Player = Controller.inherit({
},
destructor: function() {
this._fsm.destructor();
this._sound.pause();
this._ctx.close();
this._player.stop();
Controller.fn.destructor.call(this);
},
@ -140,7 +145,7 @@ var Player = Controller.inherit({
this.trigger("data");
},
_h_pause: function(ev) {
this._fsm.manipulation("plause");
this._fsm.manipulation("pause");
},
_h_play: function(ev) {
this._fsm.manipulation("play");
@ -164,7 +169,9 @@ var Player = Controller.inherit({
}
},
_onAudioNewFrames: function(frames) {
this._ctx.decodeAudioData(frames.valueOf(), this._proxySchedule);
var data = new Uint8Array(frames);
this._source.emit("data", new AV.Buffer(data));
this._fsm.manipulation("newFrames");
if (this._audio.hasMore()) {
this._audio.requestMore();
@ -218,24 +225,24 @@ var Player = Controller.inherit({
case "paused":
switch (e.oldState) {
case "playing":
this._sound.pause();
this._player.pause();
break;
}
break;
case "pausedAllLoaded":
switch (e.oldState) {
case "playingAllLoaded":
this._sound.pause();
this._player.pause();
break;
}
break;
case "playing":
this._sound.play();
this._player.play();
break;
case "playingAllLoaded":
switch (e.oldState) {
case "pausedAllLoaded":
this._sound.play();
this._player.play();
break;
}
break;
@ -246,14 +253,6 @@ var Player = Controller.inherit({
},
_removeView: function(type) {
//TODO
},
_schedule: function(buffer) {
var source = this._ctx.createBufferSource();
source.buffer = buffer;
source.connect(this._ctx.destination);
source.start(this._currentTime);
this._currentTime += buffer.duration;
}
});
@ -294,7 +293,7 @@ graphs[PlayerMode.straight.playback] = {
play: "playingAllLoaded"
},
"playing": {
pause: "pause",
pause: "paused",
noMoreFrames: "playingAllLoaded"
},
"playingAllLoaded": {

View file

@ -45,7 +45,7 @@ var Vocabulary = Controller.inherit({
var keys = insert.getKeys();
for (var j = 0; j < keys.length; ++j) {
key = keys[i];
key = keys[j];
this.addElement(key, insert.at(key).clone());
}
this.trigger("change", data.clone());