lamejs
Version:
Pure JavaScript MP3 Encoder
1,577 lines (1,393 loc) • 530 kB
JavaScript
function lamejs() {
function new_byte(count) {
return new Int8Array(count);
}
function new_short(count) {
return new Int16Array(count);
}
function new_int(count) {
return new Int32Array(count);
}
function new_float(count) {
return new Float32Array(count);
}
function new_double(count) {
return new Float64Array(count);
}
function new_float_n(args) {
if (args.length == 1) {
return new_float(args[0]);
}
var sz = args[0];
args = args.slice(1);
var A = [];
for (var i = 0; i < sz; i++) {
A.push(new_float_n(args));
}
return A;
}
function new_int_n(args) {
if (args.length == 1) {
return new_int(args[0]);
}
var sz = args[0];
args = args.slice(1);
var A = [];
for (var i = 0; i < sz; i++) {
A.push(new_int_n(args));
}
return A;
}
function new_short_n(args) {
if (args.length == 1) {
return new_short(args[0]);
}
var sz = args[0];
args = args.slice(1);
var A = [];
for (var i = 0; i < sz; i++) {
A.push(new_short_n(args));
}
return A;
}
function new_array_n(args) {
if (args.length == 1) {
return new Array(args[0]);
}
var sz = args[0];
args = args.slice(1);
var A = [];
for (var i = 0; i < sz; i++) {
A.push(new_array_n(args));
}
return A;
}
var Arrays = {};
Arrays.fill = function (a, fromIndex, toIndex, val) {
if (arguments.length == 2) {
for (var i = 0; i < a.length; i++) {
a[i] = arguments[1];
}
} else {
for (var i = fromIndex; i < toIndex; i++) {
a[i] = val;
}
}
};
var System = {};
System.arraycopy = function (src, srcPos, dest, destPos, length) {
var srcEnd = srcPos + length;
while (srcPos < srcEnd)
dest[destPos++] = src[srcPos++];
};
var Util = {};
Util.SQRT2 = 1.41421356237309504880;
Util.FAST_LOG10 = function (x) {
return Math.log10(x);
};
Util.FAST_LOG10_X = function (x, y) {
return Math.log10(x) * y;
};
function ShortBlock(ordinal) {
this.ordinal = ordinal;
}
/**
* LAME may use them, even different block types for L/R.
*/
ShortBlock.short_block_allowed = new ShortBlock(0);
/**
* LAME may use them, but always same block types in L/R.
*/
ShortBlock.short_block_coupled = new ShortBlock(1);
/**
* LAME will not use short blocks, long blocks only.
*/
ShortBlock.short_block_dispensed = new ShortBlock(2);
/**
* LAME will not use long blocks, short blocks only.
*/
ShortBlock.short_block_forced = new ShortBlock(3);
var Float = {};
Float.MAX_VALUE = 3.4028235e+38;
function VbrMode(ordinal) {
this.ordinal = ordinal;
}
VbrMode.vbr_off = new VbrMode(0);
VbrMode.vbr_mt = new VbrMode(1);
VbrMode.vbr_rh = new VbrMode(2);
VbrMode.vbr_abr = new VbrMode(3);
VbrMode.vbr_mtrh = new VbrMode(4);
VbrMode.vbr_default = VbrMode.vbr_mtrh;
var assert = function (x) {
//console.assert(x);
};
var module_exports = {
"System": System,
"VbrMode": VbrMode,
"Float": Float,
"ShortBlock": ShortBlock,
"Util": Util,
"Arrays": Arrays,
"new_array_n": new_array_n,
"new_byte": new_byte,
"new_double": new_double,
"new_float": new_float,
"new_float_n": new_float_n,
"new_int": new_int,
"new_int_n": new_int_n,
"new_short": new_short,
"new_short_n": new_short_n,
"assert": assert
};
//package mp3;
/* MPEG modes */
function MPEGMode(ordinal) {
var _ordinal = ordinal;
this.ordinal = function () {
return _ordinal;
}
}
MPEGMode.STEREO = new MPEGMode(0);
MPEGMode.JOINT_STEREO = new MPEGMode(1);
MPEGMode.DUAL_CHANNEL = new MPEGMode(2);
MPEGMode.MONO = new MPEGMode(3);
MPEGMode.NOT_SET = new MPEGMode(4);
function Version() {
/**
* URL for the LAME website.
*/
var LAME_URL = "http://www.mp3dev.org/";
/**
* Major version number.
*/
var LAME_MAJOR_VERSION = 3;
/**
* Minor version number.
*/
var LAME_MINOR_VERSION = 98;
/**
* Patch level.
*/
var LAME_PATCH_VERSION = 4;
/**
* Major version number.
*/
var PSY_MAJOR_VERSION = 0;
/**
* Minor version number.
*/
var PSY_MINOR_VERSION = 93;
/**
* A string which describes the version of LAME.
*
* @return string which describes the version of LAME
*/
this.getLameVersion = function () {
// primary to write screen reports
return (LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "." + LAME_PATCH_VERSION);
}
/**
* The short version of the LAME version string.
*
* @return short version of the LAME version string
*/
this.getLameShortVersion = function () {
// Adding date and time to version string makes it harder for output
// validation
return (LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "." + LAME_PATCH_VERSION);
}
/**
* The shortest version of the LAME version string.
*
* @return shortest version of the LAME version string
*/
this.getLameVeryShortVersion = function () {
// Adding date and time to version string makes it harder for output
return ("LAME" + LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "r");
}
/**
* String which describes the version of GPSYCHO
*
* @return string which describes the version of GPSYCHO
*/
this.getPsyVersion = function () {
return (PSY_MAJOR_VERSION + "." + PSY_MINOR_VERSION);
}
/**
* String which is a URL for the LAME website.
*
* @return string which is a URL for the LAME website
*/
this.getLameUrl = function () {
return LAME_URL;
}
/**
* Quite useless for a java version, however we are compatible ;-)
*
* @return "32bits"
*/
this.getLameOsBitness = function () {
return "32bits";
}
}
/*
* MP3 huffman table selecting and bit counting
*
* Copyright (c) 1999-2005 Takehiro TOMINAGA
* Copyright (c) 2002-2005 Gabriel Bouvigne
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* $Id: Takehiro.java,v 1.26 2011/05/24 20:48:06 kenchis Exp $ */
//package mp3;
//import java.util.Arrays;
function Takehiro() {
var qupvt = null;
this.qupvt = null;
this.setModules = function (_qupvt) {
this.qupvt = _qupvt;
qupvt = _qupvt;
}
function Bits(b) {
this.bits = 0 | b;
}
var subdv_table = [[0, 0], /* 0 bands */
[0, 0], /* 1 bands */
[0, 0], /* 2 bands */
[0, 0], /* 3 bands */
[0, 0], /* 4 bands */
[0, 1], /* 5 bands */
[1, 1], /* 6 bands */
[1, 1], /* 7 bands */
[1, 2], /* 8 bands */
[2, 2], /* 9 bands */
[2, 3], /* 10 bands */
[2, 3], /* 11 bands */
[3, 4], /* 12 bands */
[3, 4], /* 13 bands */
[3, 4], /* 14 bands */
[4, 5], /* 15 bands */
[4, 5], /* 16 bands */
[4, 6], /* 17 bands */
[5, 6], /* 18 bands */
[5, 6], /* 19 bands */
[5, 7], /* 20 bands */
[6, 7], /* 21 bands */
[6, 7], /* 22 bands */
];
/**
* nonlinear quantization of xr More accurate formula than the ISO formula.
* Takes into account the fact that we are quantizing xr . ix, but we want
* ix^4/3 to be as close as possible to x^4/3. (taking the nearest int would
* mean ix is as close as possible to xr, which is different.)
*
* From Segher Boessenkool <segher@eastsite.nl> 11/1999
*
* 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro Tominaga.
* If you need the ASM code, check CVS circa Aug 2000.
*
* 01/2004: Optimizations by Gabriel Bouvigne
*/
function quantize_lines_xrpow_01(l, istep, xr, xrPos, ix, ixPos) {
var compareval0 = (1.0 - 0.4054) / istep;
l = l >> 1;
while ((l--) != 0) {
ix[ixPos++] = (compareval0 > xr[xrPos++]) ? 0 : 1;
ix[ixPos++] = (compareval0 > xr[xrPos++]) ? 0 : 1;
}
}
/**
* XRPOW_FTOI is a macro to convert floats to ints.<BR>
* if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]<BR>
* ROUNDFAC= -0.0946<BR>
*
* if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]<BR>
* ROUNDFAC=0.4054<BR>
*
* Note: using floor() or 0| is extremely slow. On machines where the
* TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile to write
* some ASM for XRPOW_FTOI().
*/
function quantize_lines_xrpow(l, istep, xr, xrPos, ix, ixPos) {
l = l >> 1;
var remaining = l % 2;
l = l >> 1;
while (l-- != 0) {
var x0, x1, x2, x3;
var rx0, rx1, rx2, rx3;
x0 = xr[xrPos++] * istep;
x1 = xr[xrPos++] * istep;
rx0 = 0 | x0;
x2 = xr[xrPos++] * istep;
rx1 = 0 | x1;
x3 = xr[xrPos++] * istep;
rx2 = 0 | x2;
x0 += qupvt.adj43[rx0];
rx3 = 0 | x3;
x1 += qupvt.adj43[rx1];
ix[ixPos++] = 0 | x0;
x2 += qupvt.adj43[rx2];
ix[ixPos++] = 0 | x1;
x3 += qupvt.adj43[rx3];
ix[ixPos++] = 0 | x2;
ix[ixPos++] = 0 | x3;
}
if (remaining != 0) {
var x0, x1;
var rx0, rx1;
x0 = xr[xrPos++] * istep;
x1 = xr[xrPos++] * istep;
rx0 = 0 | x0;
rx1 = 0 | x1;
x0 += qupvt.adj43[rx0];
x1 += qupvt.adj43[rx1];
ix[ixPos++] = 0 | x0;
ix[ixPos++] = 0 | x1;
}
}
/**
* Quantization function This function will select which lines to quantize
* and call the proper quantization function
*/
function quantize_xrpow(xp, pi, istep, codInfo, prevNoise) {
/* quantize on xr^(3/4) instead of xr */
var sfb;
var sfbmax;
var j = 0;
var prev_data_use;
var accumulate = 0;
var accumulate01 = 0;
var xpPos = 0;
var iData = pi;
var iDataPos = 0;
var acc_iData = iData;
var acc_iDataPos = 0;
var acc_xp = xp;
var acc_xpPos = 0;
/*
* Reusing previously computed data does not seems to work if global
* gain is changed. Finding why it behaves this way would allow to use a
* cache of previously computed values (let's 10 cached values per sfb)
* that would probably provide a noticeable speedup
*/
prev_data_use = (prevNoise != null && (codInfo.global_gain == prevNoise.global_gain));
if (codInfo.block_type == Encoder.SHORT_TYPE)
sfbmax = 38;
else
sfbmax = 21;
for (sfb = 0; sfb <= sfbmax; sfb++) {
var step = -1;
if (prev_data_use || codInfo.block_type == Encoder.NORM_TYPE) {
step = codInfo.global_gain
- ((codInfo.scalefac[sfb] + (codInfo.preflag != 0 ? qupvt.pretab[sfb]
: 0)) << (codInfo.scalefac_scale + 1))
- codInfo.subblock_gain[codInfo.window[sfb]] * 8;
}
if (prev_data_use && (prevNoise.step[sfb] == step)) {
/*
* do not recompute this part, but compute accumulated lines
*/
if (accumulate != 0) {
quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,
acc_iData, acc_iDataPos);
accumulate = 0;
}
if (accumulate01 != 0) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate01 = 0;
}
} else { /* should compute this part */
var l = codInfo.width[sfb];
if ((j + codInfo.width[sfb]) > codInfo.max_nonzero_coeff) {
/* do not compute upper zero part */
var usefullsize;
usefullsize = codInfo.max_nonzero_coeff - j + 1;
Arrays.fill(pi, codInfo.max_nonzero_coeff, 576, 0);
l = usefullsize;
if (l < 0) {
l = 0;
}
/* no need to compute higher sfb values */
sfb = sfbmax + 1;
}
/* accumulate lines to quantize */
if (0 == accumulate && 0 == accumulate01) {
acc_iData = iData;
acc_iDataPos = iDataPos;
acc_xp = xp;
acc_xpPos = xpPos;
}
if (prevNoise != null && prevNoise.sfb_count1 > 0
&& sfb >= prevNoise.sfb_count1
&& prevNoise.step[sfb] > 0
&& step >= prevNoise.step[sfb]) {
if (accumulate != 0) {
quantize_lines_xrpow(accumulate, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate = 0;
acc_iData = iData;
acc_iDataPos = iDataPos;
acc_xp = xp;
acc_xpPos = xpPos;
}
accumulate01 += l;
} else {
if (accumulate01 != 0) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate01 = 0;
acc_iData = iData;
acc_iDataPos = iDataPos;
acc_xp = xp;
acc_xpPos = xpPos;
}
accumulate += l;
}
if (l <= 0) {
/*
* rh: 20040215 may happen due to "prev_data_use"
* optimization
*/
if (accumulate01 != 0) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate01 = 0;
}
if (accumulate != 0) {
quantize_lines_xrpow(accumulate, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate = 0;
}
break;
/* ends for-loop */
}
}
if (sfb <= sfbmax) {
iDataPos += codInfo.width[sfb];
xpPos += codInfo.width[sfb];
j += codInfo.width[sfb];
}
}
if (accumulate != 0) { /* last data part */
quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,
acc_iData, acc_iDataPos);
accumulate = 0;
}
if (accumulate01 != 0) { /* last data part */
quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_xpPos,
acc_iData, acc_iDataPos);
accumulate01 = 0;
}
}
/**
* ix_max
*/
function ix_max(ix, ixPos, endPos) {
var max1 = 0, max2 = 0;
do {
var x1 = ix[ixPos++];
var x2 = ix[ixPos++];
if (max1 < x1)
max1 = x1;
if (max2 < x2)
max2 = x2;
} while (ixPos < endPos);
if (max1 < max2)
max1 = max2;
return max1;
}
function count_bit_ESC(ix, ixPos, end, t1, t2, s) {
/* ESC-table is used */
var linbits = Tables.ht[t1].xlen * 65536 + Tables.ht[t2].xlen;
var sum = 0, sum2;
do {
var x = ix[ixPos++];
var y = ix[ixPos++];
if (x != 0) {
if (x > 14) {
x = 15;
sum += linbits;
}
x *= 16;
}
if (y != 0) {
if (y > 14) {
y = 15;
sum += linbits;
}
x += y;
}
sum += Tables.largetbl[x];
} while (ixPos < end);
sum2 = sum & 0xffff;
sum >>= 16;
if (sum > sum2) {
sum = sum2;
t1 = t2;
}
s.bits += sum;
return t1;
}
function count_bit_noESC(ix, ixPos, end, s) {
/* No ESC-words */
var sum1 = 0;
var hlen1 = Tables.ht[1].hlen;
do {
var x = ix[ixPos + 0] * 2 + ix[ixPos + 1];
ixPos += 2;
sum1 += hlen1[x];
} while (ixPos < end);
s.bits += sum1;
return 1;
}
function count_bit_noESC_from2(ix, ixPos, end, t1, s) {
/* No ESC-words */
var sum = 0, sum2;
var xlen = Tables.ht[t1].xlen;
var hlen;
if (t1 == 2)
hlen = Tables.table23;
else
hlen = Tables.table56;
do {
var x = ix[ixPos + 0] * xlen + ix[ixPos + 1];
ixPos += 2;
sum += hlen[x];
} while (ixPos < end);
sum2 = sum & 0xffff;
sum >>= 16;
if (sum > sum2) {
sum = sum2;
t1++;
}
s.bits += sum;
return t1;
}
function count_bit_noESC_from3(ix, ixPos, end, t1, s) {
/* No ESC-words */
var sum1 = 0;
var sum2 = 0;
var sum3 = 0;
var xlen = Tables.ht[t1].xlen;
var hlen1 = Tables.ht[t1].hlen;
var hlen2 = Tables.ht[t1 + 1].hlen;
var hlen3 = Tables.ht[t1 + 2].hlen;
do {
var x = ix[ixPos + 0] * xlen + ix[ixPos + 1];
ixPos += 2;
sum1 += hlen1[x];
sum2 += hlen2[x];
sum3 += hlen3[x];
} while (ixPos < end);
var t = t1;
if (sum1 > sum2) {
sum1 = sum2;
t++;
}
if (sum1 > sum3) {
sum1 = sum3;
t = t1 + 2;
}
s.bits += sum1;
return t;
}
/*************************************************************************/
/* choose table */
/*************************************************************************/
var huf_tbl_noESC = [1, 2, 5, 7, 7, 10, 10, 13, 13,
13, 13, 13, 13, 13, 13];
/**
* Choose the Huffman table that will encode ix[begin..end] with the fewest
* bits.
*
* Note: This code contains knowledge about the sizes and characteristics of
* the Huffman tables as defined in the IS (Table B.7), and will not work
* with any arbitrary tables.
*/
function choose_table(ix, ixPos, endPos, s) {
var max = ix_max(ix, ixPos, endPos);
switch (max) {
case 0:
return max;
case 1:
return count_bit_noESC(ix, ixPos, endPos, s);
case 2:
case 3:
return count_bit_noESC_from2(ix, ixPos, endPos,
huf_tbl_noESC[max - 1], s);
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
return count_bit_noESC_from3(ix, ixPos, endPos,
huf_tbl_noESC[max - 1], s);
default:
/* try tables with linbits */
if (max > QuantizePVT.IXMAX_VAL) {
s.bits = QuantizePVT.LARGE_BITS;
return -1;
}
max -= 15;
var choice2;
for (choice2 = 24; choice2 < 32; choice2++) {
if (Tables.ht[choice2].linmax >= max) {
break;
}
}
var choice;
for (choice = choice2 - 8; choice < 24; choice++) {
if (Tables.ht[choice].linmax >= max) {
break;
}
}
return count_bit_ESC(ix, ixPos, endPos, choice, choice2, s);
}
}
/**
* count_bit
*/
this.noquant_count_bits = function (gfc, gi, prev_noise) {
var ix = gi.l3_enc;
var i = Math.min(576, ((gi.max_nonzero_coeff + 2) >> 1) << 1);
if (prev_noise != null)
prev_noise.sfb_count1 = 0;
/* Determine count1 region */
for (; i > 1; i -= 2)
if ((ix[i - 1] | ix[i - 2]) != 0)
break;
gi.count1 = i;
/* Determines the number of bits to encode the quadruples. */
var a1 = 0;
var a2 = 0;
for (; i > 3; i -= 4) {
var p;
/* hack to check if all values <= 1 */
//throw "TODO: HACK if ((((long) ix[i - 1] | (long) ix[i - 2] | (long) ix[i - 3] | (long) ix[i - 4]) & 0xffffffffL) > 1L "
//if (true) {
if (((ix[i - 1] | ix[i - 2] | ix[i - 3] | ix[i - 4]) & 0x7fffffff) > 1) {
break;
}
p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
a1 += Tables.t32l[p];
a2 += Tables.t33l[p];
}
var bits = a1;
gi.count1table_select = 0;
if (a1 > a2) {
bits = a2;
gi.count1table_select = 1;
}
gi.count1bits = bits;
gi.big_values = i;
if (i == 0)
return bits;
if (gi.block_type == Encoder.SHORT_TYPE) {
a1 = 3 * gfc.scalefac_band.s[3];
if (a1 > gi.big_values)
a1 = gi.big_values;
a2 = gi.big_values;
} else if (gi.block_type == Encoder.NORM_TYPE) {
/* bv_scf has 576 entries (0..575) */
a1 = gi.region0_count = gfc.bv_scf[i - 2];
a2 = gi.region1_count = gfc.bv_scf[i - 1];
a2 = gfc.scalefac_band.l[a1 + a2 + 2];
a1 = gfc.scalefac_band.l[a1 + 1];
if (a2 < i) {
var bi = new Bits(bits);
gi.table_select[2] = choose_table(ix, a2, i, bi);
bits = bi.bits;
}
} else {
gi.region0_count = 7;
/* gi.region1_count = SBPSY_l - 7 - 1; */
gi.region1_count = Encoder.SBMAX_l - 1 - 7 - 1;
a1 = gfc.scalefac_band.l[7 + 1];
a2 = i;
if (a1 > a2) {
a1 = a2;
}
}
/* have to allow for the case when bigvalues < region0 < region1 */
/* (and region0, region1 are ignored) */
a1 = Math.min(a1, i);
a2 = Math.min(a2, i);
/* Count the number of bits necessary to code the bigvalues region. */
if (0 < a1) {
var bi = new Bits(bits);
gi.table_select[0] = choose_table(ix, 0, a1, bi);
bits = bi.bits;
}
if (a1 < a2) {
var bi = new Bits(bits);
gi.table_select[1] = choose_table(ix, a1, a2, bi);
bits = bi.bits;
}
if (gfc.use_best_huffman == 2) {
gi.part2_3_length = bits;
best_huffman_divide(gfc, gi);
bits = gi.part2_3_length;
}
if (prev_noise != null) {
if (gi.block_type == Encoder.NORM_TYPE) {
var sfb = 0;
while (gfc.scalefac_band.l[sfb] < gi.big_values) {
sfb++;
}
prev_noise.sfb_count1 = sfb;
}
}
return bits;
}
this.count_bits = function (gfc, xr, gi, prev_noise) {
var ix = gi.l3_enc;
/* since quantize_xrpow uses table lookup, we need to check this first: */
var w = (QuantizePVT.IXMAX_VAL) / qupvt.IPOW20(gi.global_gain);
if (gi.xrpow_max > w)
return QuantizePVT.LARGE_BITS;
quantize_xrpow(xr, ix, qupvt.IPOW20(gi.global_gain), gi, prev_noise);
if ((gfc.substep_shaping & 2) != 0) {
var j = 0;
/* 0.634521682242439 = 0.5946*2**(.5*0.1875) */
var gain = gi.global_gain + gi.scalefac_scale;
var roundfac = 0.634521682242439 / qupvt.IPOW20(gain);
for (var sfb = 0; sfb < gi.sfbmax; sfb++) {
var width = gi.width[sfb];
if (0 == gfc.pseudohalf[sfb]) {
j += width;
} else {
var k;
for (k = j, j += width; k < j; ++k) {
ix[k] = (xr[k] >= roundfac) ? ix[k] : 0;
}
}
}
}
return this.noquant_count_bits(gfc, gi, prev_noise);
}
/**
* re-calculate the best scalefac_compress using scfsi the saved bits are
* kept in the bit reservoir.
*/
function recalc_divide_init(gfc, cod_info, ix, r01_bits, r01_div, r0_tbl, r1_tbl) {
var bigv = cod_info.big_values;
for (var r0 = 0; r0 <= 7 + 15; r0++) {
r01_bits[r0] = QuantizePVT.LARGE_BITS;
}
for (var r0 = 0; r0 < 16; r0++) {
var a1 = gfc.scalefac_band.l[r0 + 1];
if (a1 >= bigv)
break;
var r0bits = 0;
var bi = new Bits(r0bits);
var r0t = choose_table(ix, 0, a1, bi);
r0bits = bi.bits;
for (var r1 = 0; r1 < 8; r1++) {
var a2 = gfc.scalefac_band.l[r0 + r1 + 2];
if (a2 >= bigv)
break;
var bits = r0bits;
bi = new Bits(bits);
var r1t = choose_table(ix, a1, a2, bi);
bits = bi.bits;
if (r01_bits[r0 + r1] > bits) {
r01_bits[r0 + r1] = bits;
r01_div[r0 + r1] = r0;
r0_tbl[r0 + r1] = r0t;
r1_tbl[r0 + r1] = r1t;
}
}
}
}
function recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl) {
var bigv = cod_info2.big_values;
for (var r2 = 2; r2 < Encoder.SBMAX_l + 1; r2++) {
var a2 = gfc.scalefac_band.l[r2];
if (a2 >= bigv)
break;
var bits = r01_bits[r2 - 2] + cod_info2.count1bits;
if (gi.part2_3_length <= bits)
break;
var bi = new Bits(bits);
var r2t = choose_table(ix, a2, bigv, bi);
bits = bi.bits;
if (gi.part2_3_length <= bits)
continue;
gi.assign(cod_info2);
gi.part2_3_length = bits;
gi.region0_count = r01_div[r2 - 2];
gi.region1_count = r2 - 2 - r01_div[r2 - 2];
gi.table_select[0] = r0_tbl[r2 - 2];
gi.table_select[1] = r1_tbl[r2 - 2];
gi.table_select[2] = r2t;
}
}
this.best_huffman_divide = function (gfc, gi) {
var cod_info2 = new GrInfo();
var ix = gi.l3_enc;
var r01_bits = new_int(7 + 15 + 1);
var r01_div = new_int(7 + 15 + 1);
var r0_tbl = new_int(7 + 15 + 1);
var r1_tbl = new_int(7 + 15 + 1);
/* SHORT BLOCK stuff fails for MPEG2 */
if (gi.block_type == Encoder.SHORT_TYPE && gfc.mode_gr == 1)
return;
cod_info2.assign(gi);
if (gi.block_type == Encoder.NORM_TYPE) {
recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div,
r0_tbl, r1_tbl);
}
var i = cod_info2.big_values;
if (i == 0 || (ix[i - 2] | ix[i - 1]) > 1)
return;
i = gi.count1 + 2;
if (i > 576)
return;
/* Determines the number of bits to encode the quadruples. */
cod_info2.assign(gi);
cod_info2.count1 = i;
var a1 = 0;
var a2 = 0;
for (; i > cod_info2.big_values; i -= 4) {
var p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2
+ ix[i - 1];
a1 += Tables.t32l[p];
a2 += Tables.t33l[p];
}
cod_info2.big_values = i;
cod_info2.count1table_select = 0;
if (a1 > a2) {
a1 = a2;
cod_info2.count1table_select = 1;
}
cod_info2.count1bits = a1;
if (cod_info2.block_type == Encoder.NORM_TYPE)
recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div,
r0_tbl, r1_tbl);
else {
/* Count the number of bits necessary to code the bigvalues region. */
cod_info2.part2_3_length = a1;
a1 = gfc.scalefac_band.l[7 + 1];
if (a1 > i) {
a1 = i;
}
if (a1 > 0) {
var bi = new Bits(cod_info2.part2_3_length);
cod_info2.table_select[0] = choose_table(ix, 0, a1, bi);
cod_info2.part2_3_length = bi.bits;
}
if (i > a1) {
var bi = new Bits(cod_info2.part2_3_length);
cod_info2.table_select[1] = choose_table(ix, a1, i, bi);
cod_info2.part2_3_length = bi.bits;
}
if (gi.part2_3_length > cod_info2.part2_3_length)
gi.assign(cod_info2);
}
}
var slen1_n = [1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16];
var slen2_n = [1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8];
var slen1_tab = [0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4];
var slen2_tab = [0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3];
Takehiro.slen1_tab = slen1_tab;
Takehiro.slen2_tab = slen2_tab;
function scfsi_calc(ch, l3_side) {
var sfb;
var gi = l3_side.tt[1][ch];
var g0 = l3_side.tt[0][ch];
for (var i = 0; i < Tables.scfsi_band.length - 1; i++) {
for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {
if (g0.scalefac[sfb] != gi.scalefac[sfb]
&& gi.scalefac[sfb] >= 0)
break;
}
if (sfb == Tables.scfsi_band[i + 1]) {
for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {
gi.scalefac[sfb] = -1;
}
l3_side.scfsi[ch][i] = 1;
}
}
var s1 = 0;
var c1 = 0;
for (sfb = 0; sfb < 11; sfb++) {
if (gi.scalefac[sfb] == -1)
continue;
c1++;
if (s1 < gi.scalefac[sfb])
s1 = gi.scalefac[sfb];
}
var s2 = 0;
var c2 = 0;
for (; sfb < Encoder.SBPSY_l; sfb++) {
if (gi.scalefac[sfb] == -1)
continue;
c2++;
if (s2 < gi.scalefac[sfb])
s2 = gi.scalefac[sfb];
}
for (var i = 0; i < 16; i++) {
if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
var c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
if (gi.part2_length > c) {
gi.part2_length = c;
gi.scalefac_compress = i;
}
}
}
}
/**
* Find the optimal way to store the scalefactors. Only call this routine
* after final scalefactors have been chosen and the channel/granule will
* not be re-encoded.
*/
this.best_scalefac_store = function (gfc, gr, ch, l3_side) {
/* use scalefac_scale if we can */
var gi = l3_side.tt[gr][ch];
var sfb, i, j, l;
var recalc = 0;
/*
* remove scalefacs from bands with ix=0. This idea comes from the AAC
* ISO docs. added mt 3/00
*/
/* check if l3_enc=0 */
j = 0;
for (sfb = 0; sfb < gi.sfbmax; sfb++) {
var width = gi.width[sfb];
j += width;
for (l = -width; l < 0; l++) {
if (gi.l3_enc[l + j] != 0)
break;
}
if (l == 0)
gi.scalefac[sfb] = recalc = -2;
/* anything goes. */
/*
* only best_scalefac_store and calc_scfsi know--and only they
* should know--about the magic number -2.
*/
}
if (0 == gi.scalefac_scale && 0 == gi.preflag) {
var s = 0;
for (sfb = 0; sfb < gi.sfbmax; sfb++)
if (gi.scalefac[sfb] > 0)
s |= gi.scalefac[sfb];
if (0 == (s & 1) && s != 0) {
for (sfb = 0; sfb < gi.sfbmax; sfb++)
if (gi.scalefac[sfb] > 0)
gi.scalefac[sfb] >>= 1;
gi.scalefac_scale = recalc = 1;
}
}
if (0 == gi.preflag && gi.block_type != Encoder.SHORT_TYPE
&& gfc.mode_gr == 2) {
for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
if (gi.scalefac[sfb] < qupvt.pretab[sfb]
&& gi.scalefac[sfb] != -2)
break;
if (sfb == Encoder.SBPSY_l) {
for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
if (gi.scalefac[sfb] > 0)
gi.scalefac[sfb] -= qupvt.pretab[sfb];
gi.preflag = recalc = 1;
}
}
for (i = 0; i < 4; i++)
l3_side.scfsi[ch][i] = 0;
if (gfc.mode_gr == 2 && gr == 1
&& l3_side.tt[0][ch].block_type != Encoder.SHORT_TYPE
&& l3_side.tt[1][ch].block_type != Encoder.SHORT_TYPE) {
scfsi_calc(ch, l3_side);
recalc = 0;
}
for (sfb = 0; sfb < gi.sfbmax; sfb++) {
if (gi.scalefac[sfb] == -2) {
gi.scalefac[sfb] = 0;
/* if anything goes, then 0 is a good choice */
}
}
if (recalc != 0) {
if (gfc.mode_gr == 2) {
this.scale_bitcount(gi);
} else {
this.scale_bitcount_lsf(gfc, gi);
}
}
}
function all_scalefactors_not_negative(scalefac, n) {
for (var i = 0; i < n; ++i) {
if (scalefac[i] < 0)
return false;
}
return true;
}
/**
* number of bits used to encode scalefacs.
*
* 18*slen1_tab[i] + 18*slen2_tab[i]
*/
var scale_short = [0, 18, 36, 54, 54, 36, 54, 72,
54, 72, 90, 72, 90, 108, 108, 126];
/**
* number of bits used to encode scalefacs.
*
* 17*slen1_tab[i] + 18*slen2_tab[i]
*/
var scale_mixed = [0, 18, 36, 54, 51, 35, 53, 71,
52, 70, 88, 69, 87, 105, 104, 122];
/**
* number of bits used to encode scalefacs.
*
* 11*slen1_tab[i] + 10*slen2_tab[i]
*/
var scale_long = [0, 10, 20, 30, 33, 21, 31, 41, 32, 42,
52, 43, 53, 63, 64, 74];
/**
* Also calculates the number of bits necessary to code the scalefactors.
*/
this.scale_bitcount = function (cod_info) {
var k, sfb, max_slen1 = 0, max_slen2 = 0;
/* maximum values */
var tab;
var scalefac = cod_info.scalefac;
if (cod_info.block_type == Encoder.SHORT_TYPE) {
tab = scale_short;
if (cod_info.mixed_block_flag != 0)
tab = scale_mixed;
} else { /* block_type == 1,2,or 3 */
tab = scale_long;
if (0 == cod_info.preflag) {
for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
if (scalefac[sfb] < qupvt.pretab[sfb])
break;
if (sfb == Encoder.SBPSY_l) {
cod_info.preflag = 1;
for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
scalefac[sfb] -= qupvt.pretab[sfb];
}
}
}
for (sfb = 0; sfb < cod_info.sfbdivide; sfb++)
if (max_slen1 < scalefac[sfb])
max_slen1 = scalefac[sfb];
for (; sfb < cod_info.sfbmax; sfb++)
if (max_slen2 < scalefac[sfb])
max_slen2 = scalefac[sfb];
/*
* from Takehiro TOMINAGA <tominaga@isoternet.org> 10/99 loop over *all*
* posible values of scalefac_compress to find the one which uses the
* smallest number of bits. ISO would stop at first valid index
*/
cod_info.part2_length = QuantizePVT.LARGE_BITS;
for (k = 0; k < 16; k++) {
if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k]
&& cod_info.part2_length > tab[k]) {
cod_info.part2_length = tab[k];
cod_info.scalefac_compress = k;
}
}
return cod_info.part2_length == QuantizePVT.LARGE_BITS;
}
/**
* table of largest scalefactor values for MPEG2
*/
var max_range_sfac_tab = [[15, 15, 7, 7],
[15, 15, 7, 0], [7, 3, 0, 0], [15, 31, 31, 0],
[7, 7, 7, 0], [3, 3, 0, 0]];
/**
* Also counts the number of bits to encode the scalefacs but for MPEG 2
* Lower sampling frequencies (24, 22.05 and 16 kHz.)
*
* This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS,
* "Audio Decoding Layer III"
*/
this.scale_bitcount_lsf = function (gfc, cod_info) {
var table_number, row_in_table, partition, nr_sfb, window;
var over;
var i, sfb;
var max_sfac = new_int(4);
//var partition_table;
var scalefac = cod_info.scalefac;
/*
* Set partition table. Note that should try to use table one, but do
* not yet...
*/
if (cod_info.preflag != 0)
table_number = 2;
else
table_number = 0;
for (i = 0; i < 4; i++)
max_sfac[i] = 0;
if (cod_info.block_type == Encoder.SHORT_TYPE) {
row_in_table = 1;
var partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
for (sfb = 0, partition = 0; partition < 4; partition++) {
nr_sfb = partition_table[partition] / 3;
for (i = 0; i < nr_sfb; i++, sfb++)
for (window = 0; window < 3; window++)
if (scalefac[sfb * 3 + window] > max_sfac[partition])
max_sfac[partition] = scalefac[sfb * 3 + window];
}
} else {
row_in_table = 0;
var partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
for (sfb = 0, partition = 0; partition < 4; partition++) {
nr_sfb = partition_table[partition];
for (i = 0; i < nr_sfb; i++, sfb++)
if (scalefac[sfb] > max_sfac[partition])
max_sfac[partition] = scalefac[sfb];
}
}
for (over = false, partition = 0; partition < 4; partition++) {
if (max_sfac[partition] > max_range_sfac_tab[table_number][partition])
over = true;
}
if (!over) {
var slen1, slen2, slen3, slen4;
cod_info.sfb_partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
for (partition = 0; partition < 4; partition++)
cod_info.slen[partition] = log2tab[max_sfac[partition]];
/* set scalefac_compress */
slen1 = cod_info.slen[0];
slen2 = cod_info.slen[1];
slen3 = cod_info.slen[2];
slen4 = cod_info.slen[3];
switch (table_number) {
case 0:
cod_info.scalefac_compress = (((slen1 * 5) + slen2) << 4)
+ (slen3 << 2) + slen4;
break;
case 1:
cod_info.scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2)
+ slen3;
break;
case 2:
cod_info.scalefac_compress = 500 + (slen1 * 3) + slen2;
break;
default:
System.err.printf("intensity stereo not implemented yet\n");
break;
}
}
if (!over) {
cod_info.part2_length = 0;
for (partition = 0; partition < 4; partition++)
cod_info.part2_length += cod_info.slen[partition]
* cod_info.sfb_partition_table[partition];
}
return over;
}
/*
* Since no bands have been over-amplified, we can set scalefac_compress and
* slen[] for the formatter
*/
var log2tab = [0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4];
this.huffman_init = function (gfc) {
for (var i = 2; i <= 576; i += 2) {
var scfb_anz = 0, bv_index;
while (gfc.scalefac_band.l[++scfb_anz] < i)
;
bv_index = subdv_table[scfb_anz][0]; // .region0_count
while (gfc.scalefac_band.l[bv_index + 1] > i)
bv_index--;
if (bv_index < 0) {
/*
* this is an indication that everything is going to be encoded
* as region0: bigvalues < region0 < region1 so lets set
* region0, region1 to some value larger than bigvalues
*/
bv_index = subdv_table[scfb_anz][0]; // .region0_count
}
gfc.bv_scf[i - 2] = bv_index;
bv_index = subdv_table[scfb_anz][1]; // .region1_count
while (gfc.scalefac_band.l[bv_index + gfc.bv_scf[i - 2] + 2] > i)
bv_index--;
if (bv_index < 0) {
bv_index = subdv_table[scfb_anz][1]; // .region1_count
}
gfc.bv_scf[i - 1] = bv_index;
}
}
}
/*
* ReplayGainAnalysis - analyzes input samples and give the recommended dB change
* Copyright (C) 2001 David Robinson and Glen Sawyer
* Improvements and optimizations added by Frank Klemm, and by Marcel Muller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* concept and filter values by David Robinson (David@Robinson.org)
* -- blame him if you think the idea is flawed
* original coding by Glen Sawyer (mp3gain@hotmail.com)
* -- blame him if you think this runs too slowly, or the coding is otherwise flawed
*
* lots of code improvements by Frank Klemm ( http://www.uni-jena.de/~pfk/mpp/ )
* -- credit him for all the _good_ programming ;)
*
*
* For an explanation of the concepts and the basic algorithms involved, go to:
* http://www.replaygain.org/
*/
/*
* Here's the deal. Call
*
* InitGainAnalysis ( long samplefreq );
*
* to initialize everything. Call
*
* AnalyzeSamples ( var Float_t* left_samples,
* var Float_t* right_samples,
* size_t num_samples,
* int num_channels );
*
* as many times as you want, with as many or as few samples as you want.
* If mono, pass the sample buffer in through left_samples, leave
* right_samples NULL, and make sure num_channels = 1.
*
* GetTitleGain()
*
* will return the recommended dB level change for all samples analyzed
* SINCE THE LAST TIME you called GetTitleGain() OR InitGainAnalysis().
*
* GetAlbumGain()
*
* will return the recommended dB level change for all samples analyzed
* since InitGainAnalysis() was called and finalized with GetTitleGain().
*
* Pseudo-code to process an album:
*
* Float_t l_samples [4096];
* Float_t r_samples [4096];
* size_t num_samples;
* unsigned int num_songs;
* unsigned int i;
*
* InitGainAnalysis ( 44100 );
* for ( i = 1; i <= num_songs; i++ ) {
* while ( ( num_samples = getSongSamples ( song[i], left_samples, right_samples ) ) > 0 )
* AnalyzeSamples ( left_samples, right_samples, num_samples, 2 );
* fprintf ("Recommended dB change for song %2d: %+6.2 dB\n", i, GetTitleGain() );
* }
* fprintf ("Recommended dB change for whole album: %+6.2 dB\n", GetAlbumGain() );
*/
/*
* So here's the main source of potential code confusion:
*
* The filters applied to the incoming samples are IIR filters,
* meaning they rely on up to <filter order> number of previous samples
* AND up to <filter order> number of previous filtered samples.
*
* I set up the AnalyzeSamples routine to minimize memory usage and interface
* complexity. The speed isn't compromised too much (I don't think), but the
* internal complexity is higher than it should be for such a relatively
* simple routine.
*
* Optimization/clarity suggestions are welcome.
*/
/**
* Table entries per dB
*/
GainAnalysis.STEPS_per_dB = 100.;
/**
* Table entries for 0...MAX_dB (normal max. values are 70...80 dB)
*/
GainAnalysis.MAX_dB = 120.;
GainAnalysis.GAIN_NOT_ENOUGH_SAMPLES = -24601;
GainAnalysis.GAIN_ANALYSIS_ERROR = 0;
GainAnalysis.GAIN_ANALYSIS_OK = 1;
GainAnalysis.INIT_GAIN_ANALYSIS_ERROR = 0;
GainAnalysis.INIT_GAIN_ANALYSIS_OK = 1;
GainAnalysis.YULE_ORDER = 10;
GainAnalysis.MAX_ORDER = GainAnalysis.YULE_ORDER;
GainAnalysis.MAX_SAMP_FREQ = 48000;
GainAnalysis.RMS_WINDOW_TIME_NUMERATOR = 1;
GainAnalysis.RMS_WINDOW_TIME_DENOMINATOR = 20;
GainAnalysis.MAX_SAMPLES_PER_WINDOW = ((GainAnalysis.MAX_SAMP_FREQ * GainAnalysis.RMS_WINDOW_TIME_NUMERATOR) / GainAnalysis.RMS_WINDOW_TIME_DENOMINATOR + 1);
function GainAnalysis() {
/**
* calibration value for 89dB
*/
var PINK_REF = 64.82;
var YULE_ORDER = GainAnalysis.YULE_ORDER;
/**
* percentile which is louder than the proposed level
*/
var RMS_PERCENTILE = 0.95;
/**
* maximum allowed sample frequency [Hz]
*/
var MAX_SAMP_FREQ = GainAnalysis.MAX_SAMP_FREQ;
var RMS_WINDOW_TIME_NUMERATOR = GainAnalysis.RMS_WINDOW_TIME_NUMERATOR;
/**
* numerator / denominator = time slice size [s]
*/
var RMS_WINDOW_TIME_DENOMINATOR = GainAnalysis.RMS_WINDOW_TIME_DENOMINATOR;
/**
* max. Samples per Time slice
*/
var MAX_SAMPLES_PER_WINDOW = GainAnalysis.MAX_SAMPLES_PER_WINDOW;
var ABYule = [
[0.03857599435200, -3.84664617118067, -0.02160367184185,
7.81501653005538, -0.00123395316851, -11.34170355132042,
-0.00009291677959, 13.05504219327545, -0.01655260341619,
-12.28759895145294, 0.02161526843274, 9.48293806319790,
-0.02074045215285, -5.87257861775999, 0.00594298065125,
2.75465861874613, 0.00306428023191, -0.86984376593551,
0.00012025322027, 0.13919314567432, 0.00288463683916],
[0.05418656406430, -3.47845948550071, -0.02911007808948,
6.36317777566148, -0.00848709379851, -8.54751527471874,
-0.00851165645469, 9.47693607801280, -0.00834990904936,
-8.81498681370155, 0.02245293253339, 6.85401540936998,
-0.02596338512915, -4.39470996079559, 0.01624864962975,
2.19611684890774, -0.00240879051584, -0.75104302451432,
0.00674613682247, 0.13149317958808, -0.00187763777362],
[0.15457299681924, -2.37898834973084, -0.09331049056315,
2.84868151156327, -0.06247880153653, -2.64577170229825,
0.02163541888798, 2.23697657451713, -0.05588393329856,
-1.67148153367602, 0.04781476674921, 1.00595954808547,
0.00222312597743, -0.45953458054983, 0.03174092540049,
0.16378164858596, -0.01390589421898, -0.05032077717131,
0.00651420667831, 0.02347897407020, -0.00881362733839],
[0.30296907319327, -1.61273165137247, -0.22613988682123,
1.07977492259970, -0.08587323730772, -0.25656257754070,
0.03282930172664, -0.16276719120440, -0.00915702933434,
-0.22638893773906, -0.02364141202522, 0.39120800788284,
-0.00584456039913, -0.22138138954925, 0.06276101321749,
0.04500235387352, -0.00000828086748, 0.02005851806501,
0.00205861885564, 0.00302439095741, -0.02950134983287],
[0.3364230485