Skip to content

Commit c63cec5

Browse files
committed
デコード時に同時にインタリーブ解除するオプションを追加 (2ch, floatの時のみ)
1 parent 7339037 commit c63cec5

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

libopus.worker.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
importScripts("libopus.js");
22
var handle_ = null;
33
var encoding_ = false, decoding_ = false;
4-
var in_ = null, out_ = null, max_packet_size_ = null;
4+
var in_ = null, out_ = null, out2_ = null, max_packet_size_ = null;
55
var sampling_ = null, channels_ = null, framesize_ = null;
6-
var float_ = false;
6+
var float_ = false, deinterleave = false;
77
onmessage = function(ev) {
88
const OPUS_OK = 0;
99
const OPUS_APPLICATION_AUDIO = 2049;
@@ -15,6 +15,7 @@ onmessage = function(ev) {
1515
channels_ = data.channels;
1616
framesize_ = data.framesize;
1717
if (data.float) float_ = true;
18+
if (data.deinterleave) deinterleave = true;
1819
if (!sampling_ || !channels_ || (data.type !== 'encoder' && data.type !== 'decoder')) {
1920
postMessage("argument error");
2021
return;
@@ -31,6 +32,8 @@ onmessage = function(ev) {
3132
in_ = _malloc(max_packet_size_);
3233
framesize_ = 120 /*[ms]*/ * sampling_ / 1000;
3334
out_ = _malloc(framesize_ * channels_ * (float_ ? 4 : 2));
35+
if (deinterleave)
36+
out2_ = _malloc(framesize_ * channels_ * (float_ ? 4 : 2));
3437
}
3538
if (getValue(i32ptr, 'i32') != OPUS_OK) {
3639
postMessage("opus_" + (encoding_ ? "encoder" : "decoder") + "_create: failed. err=" + getValue(i32ptr, 'i32'));
@@ -80,7 +83,18 @@ onmessage = function(ev) {
8083
postMessage("opus_decode failed. err=" + ret);
8184
return;
8285
}
83-
ret *= channels_ * (float_ ? 4 : 2);
84-
postMessage(HEAPU8.buffer.slice(out_, out_ + ret));
86+
var bytes_per_sample = (float_ ? 4 : 2);
87+
if (deinterleave && channels_ === 2 && float_) {
88+
var l = out2_ >>> 2;
89+
var r = l + ret;
90+
var m = out_ >>> 2;
91+
for (var i = 0; i < ret; i ++) {
92+
HEAPF32[l + i] = HEAPF32[m + i * 2 + 0];
93+
HEAPF32[r + i] = HEAPF32[m + i * 2 + 1];
94+
}
95+
postMessage(HEAPU8.buffer.slice(out2_, out2_ + (ret * channels_ * bytes_per_sample)));
96+
} else {
97+
postMessage(HEAPU8.buffer.slice(out_, out_ + (ret * channels_ * bytes_per_sample)));
98+
}
8599
}
86100
};

0 commit comments

Comments
 (0)