Browse Source

增加了浏览器音频解码功能

worker
langhuihui 6 years ago
parent
commit
e24f2191d1
  1. 8
      Jessibuca.cpp
  2. 8
      Jessibuca.js
  3. 16
      public/avc.js
  4. 26
      public/ff.js
  5. 31
      public/ff_aac.js
  6. 61
      public/renderer.js

8
Jessibuca.cpp

@ -286,10 +286,10 @@ struct Jessica
}
clock_t getTimespan(clock_t t)
{
if (videoBuffers.size() > 4)
{
videoBuffer = videoBuffer >> 1;
}
// if (videoBuffers.size() > 4)
// {
// videoBuffer = videoBuffer >> 1;
// }
return call<clock_t>("timespan", t) + videoBuffer;
}
void $close()

8
Jessibuca.js

@ -28,6 +28,7 @@ mergeInto(LibraryManager.library, {
Module.Jessibuca = Module.Jessica.extend("Jessibuca", {
__construct: function () {
this.__parent.__construct.call(this, this);
this.audioCache = []
},
__destruct: function () {
this.__parent.__destruct.call(this);
@ -124,7 +125,12 @@ mergeInto(LibraryManager.library, {
}
},
playAudio(data, len) {
postMessage({ cmd: "playAudio", buffer: HEAP16.subarray(data, data + len) })
var buffer = HEAPU8.subarray(data, data + len);
this.audioCache.push(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.length))
if (this.audioCache.length >= this.audioBuffer) {
postMessage({ cmd: "playAudio", buffer: this.audioCache }, this.audioCache)
this.audioCache.length = 0
}
},
setBuffer: function (outputArray) {
for (var i = 0; i < 3; i++) {

16
public/avc.js
File diff suppressed because it is too large
View File

26
public/ff.js
File diff suppressed because it is too large
View File

31
public/ff_aac.js
File diff suppressed because it is too large
View File

61
public/renderer.js

@ -67,36 +67,48 @@ document.addEventListener("touchend", _unlock, true);
Jessibuca.prototype.playAudio = function (data) {
var context = this.audioContext;
var isPlaying = false;
var isDecoding = false;
if (!context) return false;
var audioBuffers = [];
var playNextBuffer = function () {
isPlaying = false;
var decodeQueue = []
var _this = this
var playNextBuffer = function (e) {
// isPlaying = false;
if (audioBuffers.length) {
isPlaying = true;
var audioBufferSouceNode = context.createBufferSource();
audioBufferSouceNode.buffer = audioBuffers.shift();
audioBufferSouceNode.connect(context.destination);
audioBufferSouceNode.onended = playNextBuffer;
audioBufferSouceNode.start(0);
playBuffer(audioBuffers.shift())
}
//if (audioBuffers.length > 1) audioBuffers.shift();
};
var playBuffer = function (buffer) {
isPlaying = true;
var audioBufferSouceNode = context.createBufferSource();
audioBufferSouceNode.buffer = buffer;
audioBufferSouceNode.connect(context.destination);
// audioBufferSouceNode.onended = playNextBuffer;
audioBufferSouceNode.start();
if (!_this.audioInterval) {
_this.audioInterval = setInterval(playNextBuffer, buffer.duration * 1000 - 1);
}
// setTimeout(playNextBuffer, buffer.duration * 1000)
}
var tryPlay = function (buffer) {
if (decodeQueue.length) {
context.decodeAudioData(decodeQueue.shift(), tryPlay, console.error);
} else {
isDecoding = false
}
if (isPlaying) {
audioBuffers.push(buffer);
} else {
playBuffer(buffer)
}
}
var playAudio = function (data) {
console.log(data.buffer[0].toString(16), data.buffer[1].toString(16), data.buffer[2].toString(16))
context.decodeAudioData(data.buffer, function (buffer) {//解码成pcm流
if (isPlaying) {
audioBuffers.push(buffer);
return;
}
isPlaying = true;
var audioBufferSouceNode = context.createBufferSource();
audioBufferSouceNode.buffer = buffer;
audioBufferSouceNode.connect(context.destination);
audioBufferSouceNode.onended = playNextBuffer;
audioBufferSouceNode.start(0);
}, function (e) {
alert("Fail to decode the file.");
});
decodeQueue.push(...data)
if (!isDecoding) {
isDecoding = true
context.decodeAudioData(decodeQueue.shift(), tryPlay, console.error);
}
}
this.playAudio = playAudio
playAudio(data)
@ -407,6 +419,9 @@ Jessibuca.prototype.initRGB = function (width, height) {
//Module.print(this.imageData);
};
Jessibuca.prototype.close = function () {
if (this.audioInterval) {
clearInterval(this.audioInterval)
}
this.decoderWorker.postMessage({ cmd: "close" })
this.contextGL.clear(this.contextGL.COLOR_BUFFER_BIT);
}

Loading…
Cancel
Save