diff --git a/.github/workflows/aws.yml b/.github/workflows/aws.yml index 1250228..f311410 100644 --- a/.github/workflows/aws.yml +++ b/.github/workflows/aws.yml @@ -3,7 +3,7 @@ name: Appliance Dev Deployment on: push: branches: - - all-0.5 + - all-0.6 jobs: deploy: @@ -37,7 +37,3 @@ jobs: - name: Deploy to S3 Bucket run: | aws s3 sync ./dist/y/ s3://sballappliance - - - name: Invalidate CloudFront Cache - run: | - aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*" diff --git a/.gitignore b/.gitignore index 681b621..42c2786 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ # Only exists if Bazel was run /bazel-out .angular/cache/ +/src/assets/sb-all/ +/src/assets/games/ # dependencies **/node_modules diff --git a/src/assets/games/asset-manifest.json b/src/assets/games/asset-manifest.json deleted file mode 100644 index f1d38b0..0000000 --- a/src/assets/games/asset-manifest.json +++ /dev/null @@ -1,84 +0,0 @@ -{ - "files": { - "main.css": "./static/css/main.58d558c4.css", - "main.js": "./static/js/main.60432f80.js", - "static/js/787.f8217a87.chunk.js": "./static/js/787.f8217a87.chunk.js", - "static/media/turn2.png": "./static/media/turn2.4b40ec04128a619ac2d9.png", - "static/media/turn1.png": "./static/media/turn1.00ecc8a971f1eda97011.png", - "static/media/main.png": "./static/media/main.5169fee2caf0ba2682ec.png", - "static/media/elephant.png": "./static/media/elephant.b55f3755c0cf639d1921.png", - "static/media/sweet_sour.png": "./static/media/sweet_sour.5d52aad4b8f06d2c0497.png", - "static/media/avatar.png": "./static/media/avatar.89c66c75f1dcc6e758cf.png", - "static/media/p2win.png": "./static/media/p2win.1612b3018a6b18519b09.png", - "static/media/story14.png": "./static/media/story14.edcdf79670e08e703ba4.png", - "static/media/story13.png": "./static/media/story13.e08adf8bee7be54223f0.png", - "static/media/story11.png": "./static/media/story11.572b948ad72174a837c4.png", - "static/media/story16.png": "./static/media/story16.4da4196586df3cbc8b55.png", - "static/media/story10.png": "./static/media/story10.ccd34e1616a0eda4bdbb.png", - "static/media/story12.png": "./static/media/story12.592cf68ce89503a1f6a6.png", - "static/media/story15.png": "./static/media/story15.808f108379b4d5257f42.png", - "static/media/story21.png": "./static/media/story21.afe507b6c5784cd38459.png", - "static/media/story20.png": "./static/media/story20.32afa6704c99797a46ae.png", - "static/media/story17.png": "./static/media/story17.39c1ab5089f1d17e65a5.png", - "static/media/story24.png": "./static/media/story24.912ce4221725cb7a7284.png", - "static/media/story25.png": "./static/media/story25.1d44624099b041329c4f.png", - "static/media/story22.png": "./static/media/story22.7c64bb5d3e3243879328.png", - "static/media/story23.png": "./static/media/story23.23e0ad279ccd3d0a0414.png", - "static/media/mobilemain.svg": "./static/media/mobilemain.a18037aea105a03f0806.svg", - "static/media/draw.png": "./static/media/draw.6c1f808d7accc970aaa9.png", - "static/media/S6.m4a": "./static/media/S6.9534ebaa01b6834977b2.m4a", - "static/media/over.png": "./static/media/over.07e5b626f0fcb7214e78.png", - "static/media/story8.png": "./static/media/story8.07e5b626f0fcb7214e78.png", - "static/media/S1.m4a": "./static/media/S1.6768149750c8f6073776.m4a", - "static/media/S5.m4a": "./static/media/S5.c2d22ac7291b1b69c3e9.m4a", - "static/media/S2.m4a": "./static/media/S2.4f421f90490d14c6d78a.m4a", - "static/media/S4.m4a": "./static/media/S4.a5f40a09791db69d2b9f.m4a", - "static/media/S3.m4a": "./static/media/S3.2c8124e7d26dee6c02e8.m4a", - "static/media/V5.m4a": "./static/media/V5.e8e615a6d617c274b714.m4a", - "static/media/V4.m4a": "./static/media/V4.f54d977bf95bd7977586.m4a", - "static/media/V2.m4a": "./static/media/V2.ae476c82fd8a61da90dc.m4a", - "static/media/V7.m4a": "./static/media/V7.437da67e03abadbc1bd1.m4a", - "static/media/satya.png": "./static/media/satya.4fd7c902b2340259a901.png", - "static/media/V1.m4a": "./static/media/V1.ca83ff731e34b2176cf0.m4a", - "static/media/helping.png": "./static/media/helping.e37a70e233f0a0fe03e4.png", - "static/media/singing.png": "./static/media/singing.c41ab5c69edd2cda6e26.png", - "static/media/found.png": "./static/media/found.2c8af5ef059a5ba2ff3c.png", - "static/media/V6.m4a": "./static/media/V6.2c48d69c7c37a71fd5fd.m4a", - "static/media/win.mp3": "./static/media/win.37b45d4b4b0828435ef4.mp3", - "static/media/V3.m4a": "./static/media/V3.2f9859320f25cb7a36b4.m4a", - "static/media/selectplayer.mp3": "./static/media/selectplayer.b6ba29659cf55d571cc0.mp3", - "static/media/p1win.png": "./static/media/p1win.2557e91180bee3eec4c9.png", - "static/media/p2.png": "./static/media/p2.3072f2d32702f6cd5e7c.png", - "static/media/V8.m4a": "./static/media/V8.db9d7d7b922bda5a3e90.m4a", - "static/media/p4.png": "./static/media/p4.3aeade315a6fa5581201.png", - "static/media/mode.png": "./static/media/mode.5a34c2ba627db1d52d60.png", - "static/media/story.png": "./static/media/story.b37f6d4798672cc50932.png", - "static/media/ps2.png": "./static/media/ps2.667aa15fc9699023bb88.png", - "static/media/ps4.png": "./static/media/ps4.6f76494722b88eafaed9.png", - "static/media/p1.png": "./static/media/p1.4e0394882f313a71ed60.png", - "static/media/p3.png": "./static/media/p3.77a7c0db4407165f293a.png", - "static/media/ps1.png": "./static/media/ps1.44fbb00d3dba4bbc01c9.png", - "static/media/ps3.png": "./static/media/ps3.af6a77e84626f76d33e7.png", - "static/media/trophy2.png": "./static/media/trophy2.16816c37af764d3b2144.png", - "static/media/logo.png": "./static/media/logo.2fec8c17bc95b1ab1227.png", - "static/media/p2s.png": "./static/media/p2s.da3d9f34642a44fd9197.png", - "static/media/p1s.png": "./static/media/p1s.9cdd5ced8800cbdcaecf.png", - "static/media/trophy.png": "./static/media/trophy.3b283ec8d382b5c6bf3d.png", - "static/media/total.png": "./static/media/total.fd2810be2f3a2db89f70.png", - "static/media/gt1.png": "./static/media/gt1.0bbfd9af7f97ea5c6a0d.png", - "static/media/checkresult.png": "./static/media/checkresult.290e47b47dd6124e5384.png", - "static/media/coin.mp3": "./static/media/coin.f9f1bc3d8573b0b56e70.mp3", - "static/media/home.png": "./static/media/home.cbc316c7e8d4384eb0d5.png", - "static/media/mic.png": "./static/media/mic.d4d4580e37a3db8dc917.png", - "static/media/next.png": "./static/media/next.901e22c11b3e54798515.png", - "static/media/mobileGameBar.svg": "./static/media/mobileGameBar.f8996823297078247341e8029e055df8.svg", - "index.html": "./index.html", - "main.58d558c4.css.map": "./static/css/main.58d558c4.css.map", - "main.60432f80.js.map": "./static/js/main.60432f80.js.map", - "787.f8217a87.chunk.js.map": "./static/js/787.f8217a87.chunk.js.map" - }, - "entrypoints": [ - "static/css/main.58d558c4.css", - "static/js/main.60432f80.js" - ] -} \ No newline at end of file diff --git a/src/assets/games/favicon.ico b/src/assets/games/favicon.ico deleted file mode 100644 index a11777c..0000000 Binary files a/src/assets/games/favicon.ico and /dev/null differ diff --git a/src/assets/games/index.html b/src/assets/games/index.html deleted file mode 100644 index b3b47f3..0000000 --- a/src/assets/games/index.html +++ /dev/null @@ -1 +0,0 @@ -
e.pos-1&&(i=e.pos-1);var o=0|256*e.bag[i]/e.sum;o>255&&(o=255),t[n]=255&o}}(o.VBR_seek_table,s);var u=o.sideinfo_len;e.error_protection&&(u-=2),e.VBR==m.vbr_off?(i[u++]=255&g.charAt(0),i[u++]=255&g.charAt(1),i[u++]=255&g.charAt(2),i[u++]=255&g.charAt(3)):(i[u++]=255&h.charAt(0),i[u++]=255&h.charAt(1),i[u++]=255&h.charAt(2),i[u++]=255&h.charAt(3)),_(i,u,15),_(i,u+=4,o.VBR_seek_table.nVbrNumFrames),u+=4;var d=o.VBR_seek_table.nBytesWritten+o.VBR_seek_table.TotalFrameSize;_(i,u,0|d),u+=4,f.arraycopy(s,0,i,u,s.length),u+=s.length,e.error_protection&&n.CRC_writeheader(o,i);var v=0;for(l=0;l255?255:e.lowpassfreq/100+.5),w=0,S=0,E=e.internal_flags.noise_shaping,k=0,B=0,R=0,C=0!=(1&e.exp_nspsytune),M=0!=(2&e.exp_nspsytune),T=!1,I=!1,P=e.internal_flags.nogap_total,O=e.internal_flags.nogap_current,N=e.ATHtype;switch(e.VBR){case vbr_abr:c=e.VBR_mean_bitrate_kbps;break;case vbr_off:c=e.brate;break;default:c=e.VBR_min_bitrate_kbps}switch(o=0+(e.VBR.ordinal() E[A+3-2]?O/=E[A+3-2]:O=E[A+3-2]>10*O?E[A+3-2]/(10*O):0,x[A+3]=O}for(A=0;A<3;++A){var N=E[3*A+3]+E[3*A+4]+E[3*A+5],L=1;6*E[3*A+5] \n * Joint work of Euclid and M. Hendry\n */\n function gcd(i, j) {\n return j != 0 ? gcd(j, i % j) : i;\n }\n\n /**\n * Resampling via FIR filter, blackman window.\n */\n function blackman(x, fcn, l) {\n /*\n * This algorithm from: SIGNAL PROCESSING ALGORITHMS IN FORTRAN AND C\n * S.D. Stearns and R.A. David, Prentice-Hall, 1992\n */\n var wcn = (Math.PI * fcn);\n\n x /= l;\n if (x < 0)\n x = 0;\n if (x > 1)\n x = 1;\n var x2 = x - .5;\n\n var bkwn = 0.42 - 0.5 * Math.cos(2 * x * Math.PI) + 0.08 * Math.cos(4 * x * Math.PI);\n if (Math.abs(x2) < 1e-9)\n return (wcn / Math.PI);\n else\n return (bkwn * Math.sin(l * wcn * x2) / (Math.PI * l * x2));\n }\n\n function fill_buffer_resample(gfp, outbuf, outbufPos, desired_len, inbuf, in_bufferPos, len, num_used, ch) {\n var gfc = gfp.internal_flags;\n var i, j = 0, k;\n /* number of convolution functions to pre-compute */\n var bpc = gfp.out_samplerate\n / gcd(gfp.out_samplerate, gfp.in_samplerate);\n if (bpc > LameInternalFlags.BPC)\n bpc = LameInternalFlags.BPC;\n\n var intratio = (Math.abs(gfc.resample_ratio\n - Math.floor(.5 + gfc.resample_ratio)) < .0001) ? 1 : 0;\n var fcn = 1.00 / gfc.resample_ratio;\n if (fcn > 1.00)\n fcn = 1.00;\n var filter_l = 31;\n if (0 == filter_l % 2)\n --filter_l;\n /* must be odd */\n filter_l += intratio;\n /* unless resample_ratio=int, it must be even */\n\n var BLACKSIZE = filter_l + 1;\n /* size of data needed for FIR */\n\n if (gfc.fill_buffer_resample_init == 0) {\n gfc.inbuf_old[0] = new_float(BLACKSIZE);\n gfc.inbuf_old[1] = new_float(BLACKSIZE);\n for (i = 0; i <= 2 * bpc; ++i)\n gfc.blackfilt[i] = new_float(BLACKSIZE);\n\n gfc.itime[0] = 0;\n gfc.itime[1] = 0;\n\n /* precompute blackman filter coefficients */\n for (j = 0; j <= 2 * bpc; j++) {\n var sum = 0.;\n var offset = (j - bpc) / (2. * bpc);\n for (i = 0; i <= filter_l; i++)\n sum += gfc.blackfilt[j][i] = blackman(i - offset, fcn,\n filter_l);\n for (i = 0; i <= filter_l; i++)\n gfc.blackfilt[j][i] /= sum;\n }\n gfc.fill_buffer_resample_init = 1;\n }\n\n var inbuf_old = gfc.inbuf_old[ch];\n\n /* time of j'th element in inbuf = itime + j/ifreq; */\n /* time of k'th element in outbuf = j/ofreq */\n for (k = 0; k < desired_len; k++) {\n var time0;\n var joff;\n\n time0 = k * gfc.resample_ratio;\n /* time of k'th output sample */\n j = 0 | Math.floor(time0 - gfc.itime[ch]);\n\n /* check if we need more input data */\n if ((filter_l + j - filter_l / 2) >= len)\n break;\n\n /* blackman filter. by default, window centered at j+.5(filter_l%2) */\n /* but we want a window centered at time0. */\n var offset = (time0 - gfc.itime[ch] - (j + .5 * (filter_l % 2)));\n\n /* find the closest precomputed window for this offset: */\n joff = 0 | Math.floor((offset * 2 * bpc) + bpc + .5);\n var xvalue = 0.;\n for (i = 0; i <= filter_l; ++i) {\n var j2 = i + j - filter_l / 2;\n var y;\n y = (j2 < 0) ? inbuf_old[BLACKSIZE + j2] : inbuf[in_bufferPos\n + j2];\n xvalue += y * gfc.blackfilt[joff][i];\n }\n outbuf[outbufPos + k] = xvalue;\n }\n\n /* k = number of samples added to outbuf */\n /* last k sample used data from [j-filter_l/2,j+filter_l-filter_l/2] */\n\n /* how many samples of input data were used: */\n num_used.num_used = Math.min(len, filter_l + j - filter_l / 2);\n\n /*\n * adjust our input time counter. Incriment by the number of samples\n * used, then normalize so that next output sample is at time 0, next\n * input buffer is at time itime[ch]\n */\n gfc.itime[ch] += num_used.num_used - k * gfc.resample_ratio;\n\n /* save the last BLACKSIZE samples into the inbuf_old buffer */\n if (num_used.num_used >= BLACKSIZE) {\n for (i = 0; i < BLACKSIZE; i++)\n inbuf_old[i] = inbuf[in_bufferPos + num_used.num_used + i\n - BLACKSIZE];\n } else {\n /* shift in num_used.num_used samples into inbuf_old */\n var n_shift = BLACKSIZE - num_used.num_used;\n /*\n * number of samples to\n * shift\n */\n\n /*\n * shift n_shift samples by num_used.num_used, to make room for the\n * num_used new samples\n */\n for (i = 0; i < n_shift; ++i)\n inbuf_old[i] = inbuf_old[i + num_used.num_used];\n\n /* shift in the num_used.num_used samples */\n for (j = 0; i < BLACKSIZE; ++i, ++j)\n inbuf_old[i] = inbuf[in_bufferPos + j];\n\n }\n return k;\n /* return the number samples created at the new samplerate */\n }\n\n function fill_buffer(gfp, mfbuf, in_buffer, in_bufferPos, nsamples, io) {\n var gfc = gfp.internal_flags;\n\n /* copy in new samples into mfbuf, with resampling if necessary */\n if ((gfc.resample_ratio < .9999) || (gfc.resample_ratio > 1.0001)) {\n for (var ch = 0; ch < gfc.channels_out; ch++) {\n var numUsed = new NumUsed();\n io.n_out = fill_buffer_resample(gfp, mfbuf[ch], gfc.mf_size,\n gfp.framesize, in_buffer[ch], in_bufferPos, nsamples,\n numUsed, ch);\n io.n_in = numUsed.num_used;\n }\n } else {\n io.n_out = Math.min(gfp.framesize, nsamples);\n io.n_in = io.n_out;\n for (var i = 0; i < io.n_out; ++i) {\n mfbuf[0][gfc.mf_size + i] = in_buffer[0][in_bufferPos + i];\n if (gfc.channels_out == 2)\n mfbuf[1][gfc.mf_size + i] = in_buffer[1][in_bufferPos + i];\n }\n }\n }\n\n }\n\n\n\n function GetAudio() {\n var parse;\n var mpg;\n\n this.setModules = function (parse2, mpg2) {\n parse = parse2;\n mpg = mpg2;\n }\n }\n\n\n function Parse() {\n var ver;\n var id3;\n var pre;\n\n this.setModules = function (ver2, id32, pre2) {\n ver = ver2;\n id3 = id32;\n pre = pre2;\n }\n }\n\n function MPGLib() {\n }\n\n function ID3Tag() {\n var bits;\n var ver;\n\n this.setModules = function (_bits, _ver) {\n bits = _bits;\n ver = _ver;\n }\n }\n\n function Mp3Encoder(channels, samplerate, kbps) {\n if (arguments.length != 3) {\n console.error('WARN: Mp3Encoder(channels, samplerate, kbps) not specified');\n channels = 1;\n samplerate = 44100;\n kbps = 128;\n }\n var lame = new Lame();\n var gaud = new GetAudio();\n var ga = new GainAnalysis();\n var bs = new BitStream();\n var p = new Presets();\n var qupvt = new QuantizePVT();\n var qu = new Quantize();\n var vbr = new VBRTag();\n var ver = new Version();\n var id3 = new ID3Tag();\n var rv = new Reservoir();\n var tak = new Takehiro();\n var parse = new Parse();\n var mpg = new MPGLib();\n\n lame.setModules(ga, bs, p, qupvt, qu, vbr, ver, id3, mpg);\n bs.setModules(ga, mpg, ver, vbr);\n id3.setModules(bs, ver);\n p.setModules(lame);\n qu.setModules(bs, rv, qupvt, tak);\n qupvt.setModules(tak, rv, lame.enc.psy);\n rv.setModules(bs);\n tak.setModules(qupvt);\n vbr.setModules(lame, bs, ver);\n gaud.setModules(parse, mpg);\n parse.setModules(ver, id3, p);\n\n var gfp = lame.lame_init();\n\n gfp.num_channels = channels;\n gfp.in_samplerate = samplerate;\n gfp.brate = kbps;\n gfp.mode = MPEGMode.STEREO;\n gfp.quality = 3;\n gfp.bWriteVbrTag = false;\n gfp.disable_reservoir = true;\n gfp.write_id3tag_automatic = false;\n\n var retcode = lame.lame_init_params(gfp);\n var maxSamples = 1152;\n var mp3buf_size = 0 | (1.25 * maxSamples + 7200);\n var mp3buf = new_byte(mp3buf_size);\n\n this.encodeBuffer = function (left, right) {\n if (channels == 1) {\n right = left;\n }\n if (left.length > maxSamples) {\n maxSamples = left.length;\n mp3buf_size = 0 | (1.25 * maxSamples + 7200);\n mp3buf = new_byte(mp3buf_size);\n }\n\n var _sz = lame.lame_encode_buffer(gfp, left, right, left.length, mp3buf, 0, mp3buf_size);\n return new Int8Array(mp3buf.subarray(0, _sz));\n };\n\n this.flush = function () {\n var _sz = lame.lame_encode_flush(gfp, mp3buf, 0, mp3buf_size);\n return new Int8Array(mp3buf.subarray(0, _sz));\n };\n }\n\n function WavHeader() {\n this.dataOffset = 0;\n this.dataLen = 0;\n this.channels = 0;\n this.sampleRate = 0;\n }\n\n function fourccToInt(fourcc) {\n return fourcc.charCodeAt(0) << 24 | fourcc.charCodeAt(1) << 16 | fourcc.charCodeAt(2) << 8 | fourcc.charCodeAt(3);\n }\n\n WavHeader.RIFF = fourccToInt(\"RIFF\");\n WavHeader.WAVE = fourccToInt(\"WAVE\");\n WavHeader.fmt_ = fourccToInt(\"fmt \");\n WavHeader.data = fourccToInt(\"data\");\n\n WavHeader.readHeader = function (dataView) {\n var w = new WavHeader();\n\n var header = dataView.getUint32(0, false);\n if (WavHeader.RIFF != header) {\n return;\n }\n var fileLen = dataView.getUint32(4, true);\n if (WavHeader.WAVE != dataView.getUint32(8, false)) {\n return;\n }\n if (WavHeader.fmt_ != dataView.getUint32(12, false)) {\n return;\n }\n var fmtLen = dataView.getUint32(16, true);\n var pos = 16 + 4;\n switch (fmtLen) {\n case 16:\n case 18:\n w.channels = dataView.getUint16(pos + 2, true);\n w.sampleRate = dataView.getUint32(pos + 4, true);\n break;\n default:\n throw 'extended fmt chunk not implemented';\n }\n pos += fmtLen;\n var data = WavHeader.data;\n var len = 0;\n while (data != header) {\n header = dataView.getUint32(pos, false);\n len = dataView.getUint32(pos + 4, true);\n if (data == header) {\n break;\n }\n pos += (len + 8);\n }\n w.dataLen = len;\n w.dataOffset = pos + 8;\n return w;\n };\n\n L3Side.SFBMAX = (Encoder.SBMAX_s * 3);\n//testFullLength();\n lamejs.Mp3Encoder = Mp3Encoder;\n lamejs.WavHeader = WavHeader;\n }\n//fs=require('fs');\n lamejs();\n\n var mp3Encoder, maxSamples = 1152,\n wav, samplesLeft, config, dataBuffer, samplesRight;\n var clearBuffer = function () {\n dataBuffer = [];\n };\n\n var appendToBuffer = function (mp3Buf) {\n dataBuffer.push(new Int8Array(mp3Buf));\n };\n\n\n var init = function (prefConfig) {\n config = prefConfig || {};\n clearBuffer();\n };\n\n var encode = function (arrayBuffer) {\n wav = lamejs.WavHeader.readHeader(new DataView(arrayBuffer));\n if (!wav) {\n self.postMessage({cmd: \"error\", msg: \"Specified file is not a Wave file\"});\n return;\n }\n\n var dataView = new Int16Array(arrayBuffer, wav.dataOffset, wav.dataLen / 2);\n samplesLeft = wav.channels === 1 ? dataView : new Int16Array(wav.dataLen / (2 * wav.channels));\n samplesRight = wav.channels === 2 ? new Int16Array(wav.dataLen / (2 * wav.channels)) : undefined;\n if (wav.channels > 1) {\n for (var i = 0; i < samplesLeft.length; i++) {\n samplesLeft[i] = dataView[i * 2];\n samplesRight[i] = dataView[i * 2 + 1];\n }\n }\n\n mp3Encoder = new lamejs.Mp3Encoder(wav.channels, wav.sampleRate, config.bitRate || 96);\n\n var remaining = samplesLeft.length;\n for (var i = 0; remaining >= maxSamples; i += maxSamples) {\n var left = samplesLeft.subarray(i, i + maxSamples);\n var right;\n if (samplesRight) {\n right = samplesRight.subarray(i, i + maxSamples);\n }\n var mp3buf = mp3Encoder.encodeBuffer(left, right);\n appendToBuffer(mp3buf);\n remaining -= maxSamples;\n self.postMessage({\n cmd: \"progress\",\n progress: (1 - remaining / samplesLeft.length)\n });\n }\n };\n\n var finish = function () {\n if (!wav) {\n return;\n }\n var mp3buf = mp3Encoder.flush();\n appendToBuffer(mp3buf);\n self.postMessage({\n cmd: \"end\",\n buf: dataBuffer\n });\n clearBuffer(); //free up memory\n };\n self.onmessage = function (e) {\n switch (e.data.cmd) {\n case \"init\":\n init(e.data.config);\n break;\n\n case \"encode\":\n encode(e.data.rawInput);\n break;\n\n case \"finish\":\n finish();\n break;\n }\n };\n};\n\nlet code = workercode.toString();\ncode = code.substring(code.indexOf(\"{\") + 1, code.lastIndexOf(\"}\"));\n\nconst blob = new Blob([code], {type: \"application/javascript\"});\nconst worker_script = URL.createObjectURL(blob);\n\nmodule.exports = worker_script;\n\n\n\n\n\n","/**\n * @license React\n * react-dom.production.min.js\n *\n * Copyright (c) Facebook, Inc. and its affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n/*\n Modernizr 3.0.0pre (Custom Build) | MIT\n*/\n'use strict';var aa=require(\"react\"),ca=require(\"scheduler\");function p(a){for(var b=\"https://reactjs.org/docs/error-decoder.html?invariant=\"+a,c=1;c a||125d?(a.sortIndex=c,f(t,a),null===h(r)&&a===h(t)&&(B?(E(L),L=-1):B=!0,K(H,c-d))):(a.sortIndex=e,f(r,a),A||z||(A=!0,I(J)));return a};\nexports.unstable_shouldYield=M;exports.unstable_wrapCallback=function(a){var b=y;return function(){var c=y;y=b;try{return a.apply(this,arguments)}finally{y=c}}};\n","'use strict';\n\nif (process.env.NODE_ENV === 'production') {\n module.exports = require('./cjs/scheduler.production.min.js');\n} else {\n module.exports = require('./cjs/scheduler.development.js');\n}\n","var map = {\n\t\"./avatar.png\": 8308,\n\t\"./checkresult.png\": 5437,\n\t\"./coin.png\": 5691,\n\t\"./draw.png\": 1131,\n\t\"./elephant.png\": 1294,\n\t\"./found.png\": 3587,\n\t\"./gt1.png\": 1640,\n\t\"./helping.png\": 8285,\n\t\"./home.png\": 6553,\n\t\"./homeicon.png\": 8599,\n\t\"./listen.png\": 3667,\n\t\"./logo.png\": 6949,\n\t\"./love.png\": 3825,\n\t\"./main.png\": 763,\n\t\"./mic.png\": 9252,\n\t\"./mode.png\": 9945,\n\t\"./netskill.png\": 3710,\n\t\"./next.png\": 913,\n\t\"./over.png\": 9750,\n\t\"./p1.png\": 6231,\n\t\"./p1s.png\": 9741,\n\t\"./p1u.png\": 4771,\n\t\"./p1win.png\": 3177,\n\t\"./p2.png\": 5783,\n\t\"./p2s.png\": 4203,\n\t\"./p2u.png\": 5465,\n\t\"./p2win.png\": 5701,\n\t\"./p3.png\": 6574,\n\t\"./p4.png\": 4619,\n\t\"./pause.png\": 1068,\n\t\"./play.png\": 5093,\n\t\"./ps1.png\": 7945,\n\t\"./ps2.png\": 2623,\n\t\"./ps3.png\": 5990,\n\t\"./ps4.png\": 9875,\n\t\"./pt1.png\": 9845,\n\t\"./pt2.png\": 3928,\n\t\"./refresh.png\": 9046,\n\t\"./satya.png\": 1711,\n\t\"./singing.png\": 3608,\n\t\"./start.png\": 2333,\n\t\"./story.png\": 846,\n\t\"./story10.png\": 6202,\n\t\"./story11.png\": 8296,\n\t\"./story12.png\": 3892,\n\t\"./story13.png\": 3722,\n\t\"./story14.png\": 4226,\n\t\"./story15.png\": 5641,\n\t\"./story16.png\": 3370,\n\t\"./story17.png\": 1852,\n\t\"./story20.png\": 9554,\n\t\"./story21.png\": 6335,\n\t\"./story22.png\": 9890,\n\t\"./story23.png\": 1702,\n\t\"./story24.png\": 1473,\n\t\"./story25.png\": 6905,\n\t\"./story8.png\": 4430,\n\t\"./sunbird.png\": 2135,\n\t\"./sunbirdlogo.png\": 8247,\n\t\"./sweet_sour.png\": 9041,\n\t\"./total.png\": 4749,\n\t\"./trophy.png\": 7826,\n\t\"./trophy2.png\": 2216,\n\t\"./turn1.png\": 5106,\n\t\"./turn2.png\": 6038\n};\n\n\nfunction webpackContext(req) {\n\tvar id = webpackContextResolve(req);\n\treturn __webpack_require__(id);\n}\nfunction webpackContextResolve(req) {\n\tif(!__webpack_require__.o(map, req)) {\n\t\tvar e = new Error(\"Cannot find module '\" + req + \"'\");\n\t\te.code = 'MODULE_NOT_FOUND';\n\t\tthrow e;\n\t}\n\treturn map[req];\n}\nwebpackContext.keys = function webpackContextKeys() {\n\treturn Object.keys(map);\n};\nwebpackContext.resolve = webpackContextResolve;\nmodule.exports = webpackContext;\nwebpackContext.id = 2715;","var map = {\n\t\"./story.png\": 846,\n\t\"./story10.png\": 6202,\n\t\"./story11.png\": 8296,\n\t\"./story12.png\": 3892,\n\t\"./story13.png\": 3722,\n\t\"./story14.png\": 4226,\n\t\"./story15.png\": 5641,\n\t\"./story16.png\": 3370,\n\t\"./story17.png\": 1852,\n\t\"./story20.png\": 9554,\n\t\"./story21.png\": 6335,\n\t\"./story22.png\": 9890,\n\t\"./story23.png\": 1702,\n\t\"./story24.png\": 1473,\n\t\"./story25.png\": 6905,\n\t\"./story8.png\": 4430\n};\n\n\nfunction webpackContext(req) {\n\tvar id = webpackContextResolve(req);\n\treturn __webpack_require__(id);\n}\nfunction webpackContextResolve(req) {\n\tif(!__webpack_require__.o(map, req)) {\n\t\tvar e = new Error(\"Cannot find module '\" + req + \"'\");\n\t\te.code = 'MODULE_NOT_FOUND';\n\t\tthrow e;\n\t}\n\treturn map[req];\n}\nwebpackContext.keys = function webpackContextKeys() {\n\treturn Object.keys(map);\n};\nwebpackContext.resolve = webpackContextResolve;\nmodule.exports = webpackContext;\nwebpackContext.id = 4991;","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n// expose the modules object (__webpack_modules__)\n__webpack_require__.m = __webpack_modules__;\n\n","// getDefaultExport function for compatibility with non-harmony modules\n__webpack_require__.n = function(module) {\n\tvar getter = module && module.__esModule ?\n\t\tfunction() { return module['default']; } :\n\t\tfunction() { return module; };\n\t__webpack_require__.d(getter, { a: getter });\n\treturn getter;\n};","var getProto = Object.getPrototypeOf ? function(obj) { return Object.getPrototypeOf(obj); } : function(obj) { return obj.__proto__; };\nvar leafPrototypes;\n// create a fake namespace object\n// mode & 1: value is a module id, require it\n// mode & 2: merge all properties of value into the ns\n// mode & 4: return value when already ns object\n// mode & 16: return value when it's Promise-like\n// mode & 8|1: behave like require\n__webpack_require__.t = function(value, mode) {\n\tif(mode & 1) value = this(value);\n\tif(mode & 8) return value;\n\tif(typeof value === 'object' && value) {\n\t\tif((mode & 4) && value.__esModule) return value;\n\t\tif((mode & 16) && typeof value.then === 'function') return value;\n\t}\n\tvar ns = Object.create(null);\n\t__webpack_require__.r(ns);\n\tvar def = {};\n\tleafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)];\n\tfor(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) {\n\t\tObject.getOwnPropertyNames(current).forEach(function(key) { def[key] = function() { return value[key]; }; });\n\t}\n\tdef['default'] = function() { return value; };\n\t__webpack_require__.d(ns, def);\n\treturn ns;\n};","// define getter functions for harmony exports\n__webpack_require__.d = function(exports, definition) {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.f = {};\n// This file contains only the entry chunk.\n// The chunk loading function for additional chunks\n__webpack_require__.e = function(chunkId) {\n\treturn Promise.all(Object.keys(__webpack_require__.f).reduce(function(promises, key) {\n\t\t__webpack_require__.f[key](chunkId, promises);\n\t\treturn promises;\n\t}, []));\n};","// This function allow to reference async chunks\n__webpack_require__.u = function(chunkId) {\n\t// return url for filenames based on template\n\treturn \"static/js/\" + chunkId + \".\" + \"f8217a87\" + \".chunk.js\";\n};","// This function allow to reference async chunks\n__webpack_require__.miniCssF = function(chunkId) {\n\t// return url for filenames based on template\n\treturn undefined;\n};","__webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }","var inProgress = {};\nvar dataWebpackPrefix = \"netsill-gaming-app:\";\n// loadScript function to load a script via script tag\n__webpack_require__.l = function(url, done, key, chunkId) {\n\tif(inProgress[url]) { inProgress[url].push(done); return; }\n\tvar script, needAttach;\n\tif(key !== undefined) {\n\t\tvar scripts = document.getElementsByTagName(\"script\");\n\t\tfor(var i = 0; i < scripts.length; i++) {\n\t\t\tvar s = scripts[i];\n\t\t\tif(s.getAttribute(\"src\") == url || s.getAttribute(\"data-webpack\") == dataWebpackPrefix + key) { script = s; break; }\n\t\t}\n\t}\n\tif(!script) {\n\t\tneedAttach = true;\n\t\tscript = document.createElement('script');\n\n\t\tscript.charset = 'utf-8';\n\t\tscript.timeout = 120;\n\t\tif (__webpack_require__.nc) {\n\t\t\tscript.setAttribute(\"nonce\", __webpack_require__.nc);\n\t\t}\n\t\tscript.setAttribute(\"data-webpack\", dataWebpackPrefix + key);\n\t\tscript.src = url;\n\t}\n\tinProgress[url] = [done];\n\tvar onScriptComplete = function(prev, event) {\n\t\t// avoid mem leaks in IE.\n\t\tscript.onerror = script.onload = null;\n\t\tclearTimeout(timeout);\n\t\tvar doneFns = inProgress[url];\n\t\tdelete inProgress[url];\n\t\tscript.parentNode && script.parentNode.removeChild(script);\n\t\tdoneFns && doneFns.forEach(function(fn) { return fn(event); });\n\t\tif(prev) return prev(event);\n\t};\n\tvar timeout = setTimeout(onScriptComplete.bind(null, undefined, { type: 'timeout', target: script }), 120000);\n\tscript.onerror = onScriptComplete.bind(null, script.onerror);\n\tscript.onload = onScriptComplete.bind(null, script.onload);\n\tneedAttach && document.head.appendChild(script);\n};","// define __esModule on exports\n__webpack_require__.r = function(exports) {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","__webpack_require__.p = \"./\";","// no baseURI\n\n// object to store loaded and loading chunks\n// undefined = chunk not loaded, null = chunk preloaded/prefetched\n// [resolve, reject, Promise] = chunk loading, 0 = chunk loaded\nvar installedChunks = {\n\t179: 0\n};\n\n__webpack_require__.f.j = function(chunkId, promises) {\n\t\t// JSONP chunk loading for javascript\n\t\tvar installedChunkData = __webpack_require__.o(installedChunks, chunkId) ? installedChunks[chunkId] : undefined;\n\t\tif(installedChunkData !== 0) { // 0 means \"already installed\".\n\n\t\t\t// a Promise means \"currently loading\".\n\t\t\tif(installedChunkData) {\n\t\t\t\tpromises.push(installedChunkData[2]);\n\t\t\t} else {\n\t\t\t\tif(true) { // all chunks have JS\n\t\t\t\t\t// setup Promise in chunk cache\n\t\t\t\t\tvar promise = new Promise(function(resolve, reject) { installedChunkData = installedChunks[chunkId] = [resolve, reject]; });\n\t\t\t\t\tpromises.push(installedChunkData[2] = promise);\n\n\t\t\t\t\t// start chunk loading\n\t\t\t\t\tvar url = __webpack_require__.p + __webpack_require__.u(chunkId);\n\t\t\t\t\t// create error before stack unwound to get useful stacktrace later\n\t\t\t\t\tvar error = new Error();\n\t\t\t\t\tvar loadingEnded = function(event) {\n\t\t\t\t\t\tif(__webpack_require__.o(installedChunks, chunkId)) {\n\t\t\t\t\t\t\tinstalledChunkData = installedChunks[chunkId];\n\t\t\t\t\t\t\tif(installedChunkData !== 0) installedChunks[chunkId] = undefined;\n\t\t\t\t\t\t\tif(installedChunkData) {\n\t\t\t\t\t\t\t\tvar errorType = event && (event.type === 'load' ? 'missing' : event.type);\n\t\t\t\t\t\t\t\tvar realSrc = event && event.target && event.target.src;\n\t\t\t\t\t\t\t\terror.message = 'Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')';\n\t\t\t\t\t\t\t\terror.name = 'ChunkLoadError';\n\t\t\t\t\t\t\t\terror.type = errorType;\n\t\t\t\t\t\t\t\terror.request = realSrc;\n\t\t\t\t\t\t\t\tinstalledChunkData[1](error);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t\t__webpack_require__.l(url, loadingEnded, \"chunk-\" + chunkId, chunkId);\n\t\t\t\t} else installedChunks[chunkId] = 0;\n\t\t\t}\n\t\t}\n};\n\n// no prefetching\n\n// no preloaded\n\n// no HMR\n\n// no HMR manifest\n\n// no on chunks loaded\n\n// install a JSONP callback for chunk loading\nvar webpackJsonpCallback = function(parentChunkLoadingFunction, data) {\n\tvar chunkIds = data[0];\n\tvar moreModules = data[1];\n\tvar runtime = data[2];\n\t// add \"moreModules\" to the modules object,\n\t// then flag all \"chunkIds\" as loaded and fire callback\n\tvar moduleId, chunkId, i = 0;\n\tif(chunkIds.some(function(id) { return installedChunks[id] !== 0; })) {\n\t\tfor(moduleId in moreModules) {\n\t\t\tif(__webpack_require__.o(moreModules, moduleId)) {\n\t\t\t\t__webpack_require__.m[moduleId] = moreModules[moduleId];\n\t\t\t}\n\t\t}\n\t\tif(runtime) var result = runtime(__webpack_require__);\n\t}\n\tif(parentChunkLoadingFunction) parentChunkLoadingFunction(data);\n\tfor(;i < chunkIds.length; i++) {\n\t\tchunkId = chunkIds[i];\n\t\tif(__webpack_require__.o(installedChunks, chunkId) && installedChunks[chunkId]) {\n\t\t\tinstalledChunks[chunkId][0]();\n\t\t}\n\t\tinstalledChunks[chunkId] = 0;\n\t}\n\n}\n\nvar chunkLoadingGlobal = self[\"webpackChunknetsill_gaming_app\"] = self[\"webpackChunknetsill_gaming_app\"] || [];\nchunkLoadingGlobal.forEach(webpackJsonpCallback.bind(null, 0));\nchunkLoadingGlobal.push = webpackJsonpCallback.bind(null, chunkLoadingGlobal.push.bind(chunkLoadingGlobal));","////////////////////////////////////////////////////////////////////////////////\n//#region Types and Constants\n////////////////////////////////////////////////////////////////////////////////\n\n/**\n * Actions represent the type of change to a location value.\n */\nexport enum Action {\n /**\n * A POP indicates a change to an arbitrary index in the history stack, such\n * as a back or forward navigation. It does not describe the direction of the\n * navigation, only that the current index changed.\n *\n * Note: This is the default action for newly created history objects.\n */\n Pop = \"POP\",\n\n /**\n * A PUSH indicates a new entry being added to the history stack, such as when\n * a link is clicked and a new page loads. When this happens, all subsequent\n * entries in the stack are lost.\n */\n Push = \"PUSH\",\n\n /**\n * A REPLACE indicates the entry at the current index in the history stack\n * being replaced by a new one.\n */\n Replace = \"REPLACE\",\n}\n\n/**\n * The pathname, search, and hash values of a URL.\n */\nexport interface Path {\n /**\n * A URL pathname, beginning with a /.\n */\n pathname: string;\n\n /**\n * A URL search string, beginning with a ?.\n */\n search: string;\n\n /**\n * A URL fragment identifier, beginning with a #.\n */\n hash: string;\n}\n\n/**\n * An entry in a history stack. A location contains information about the\n * URL path, as well as possibly some arbitrary state and a key.\n */\nexport interface Location extends Path {\n /**\n * A value of arbitrary data associated with this location.\n */\n state: any;\n\n /**\n * A unique string associated with this location. May be used to safely store\n * and retrieve data in some other storage API, like `localStorage`.\n *\n * Note: This value is always \"default\" on the initial location.\n */\n key: string;\n}\n\n/**\n * A change to the current location.\n */\nexport interface Update {\n /**\n * The action that triggered the change.\n */\n action: Action;\n\n /**\n * The new location.\n */\n location: Location;\n\n /**\n * The delta between this location and the former location in the history stack\n */\n delta: number | null;\n}\n\n/**\n * A function that receives notifications about location changes.\n */\nexport interface Listener {\n (update: Update): void;\n}\n\n/**\n * Describes a location that is the destination of some navigation, either via\n * `history.push` or `history.replace`. May be either a URL or the pieces of a\n * URL path.\n */\nexport type To = string | Partial>8),t[5]=byte(255&n)},this.flush_bitstream=function(e){var t,n,r=e.internal_flags,i=r.h_ptr-1;if(-1==i&&(i=G.MAX_HEADER_BUF-1),t=r.l3_side,!((n=C(e,new R))<0)){if(A(e,n),r.ResvSize=0,t.main_data_begin=0,r.findReplayGain){var o=a.GetTitleGain(r.rgdata);r.RadioGain=0|Math.floor(10*o+.5)}r.findPeakSample&&(r.noclipGainChange=0|Math.ceil(20*Math.log10(r.PeakSample/32767)*10),r.noclipGainChange>0&&(EQ(e.scale,1)||EQ(e.scale,0))?r.noclipScale=Math.floor(32767/r.PeakSample*100)/100:r.noclipScale=-1)}},this.add_dummy_byte=function(e,t,n){for(var r,a=e.internal_flags;n-- >0;)for(v(0,t,8),r=0;r0&&!e.ATHonly?u/=n.en.l[o]:u=0,d.pinfo.thr[r][i][o]=f*Math.max(u*n.thm.l[o],d.ATH.l[o]),d.pinfo.LAMEsfb[r][i][o]=0,0!=t.preflag&&o>=11&&(d.pinfo.LAMEsfb[r][i][o]=-p*c[o]),o=c){++i;break}var d=e.PSY.bo_s_weight[i],p=1-d;s=d*t[o],l=d*n[o],e.en[r].s[i][a]+=s,e.thm[r].s[i][a]+=l,s=p*t[o],l=p*n[o]}for(;i
\n * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
\n * ROUNDFAC= -0.0946
\n *\n * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
\n * ROUNDFAC=0.4054
\n *\n * Note: using floor() or 0| is extremely slow. On machines where the\n * TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile to write\n * some ASM for XRPOW_FTOI().\n */\n function quantize_lines_xrpow(l, istep, xr, xrPos, ix, ixPos) {\n\n l = l >> 1;\n var remaining = l % 2;\n l = l >> 1;\n while (l-- != 0) {\n var x0, x1, x2, x3;\n var rx0, rx1, rx2, rx3;\n\n x0 = xr[xrPos++] * istep;\n x1 = xr[xrPos++] * istep;\n rx0 = 0 | x0;\n x2 = xr[xrPos++] * istep;\n rx1 = 0 | x1;\n x3 = xr[xrPos++] * istep;\n rx2 = 0 | x2;\n x0 += qupvt.adj43[rx0];\n rx3 = 0 | x3;\n x1 += qupvt.adj43[rx1];\n ix[ixPos++] = 0 | x0;\n x2 += qupvt.adj43[rx2];\n ix[ixPos++] = 0 | x1;\n x3 += qupvt.adj43[rx3];\n ix[ixPos++] = 0 | x2;\n ix[ixPos++] = 0 | x3;\n }\n if (remaining != 0) {\n var x0, x1;\n var rx0, rx1;\n\n x0 = xr[xrPos++] * istep;\n x1 = xr[xrPos++] * istep;\n rx0 = 0 | x0;\n rx1 = 0 | x1;\n x0 += qupvt.adj43[rx0];\n x1 += qupvt.adj43[rx1];\n ix[ixPos++] = 0 | x0;\n ix[ixPos++] = 0 | x1;\n }\n }\n\n /**\n * Quantization function This function will select which lines to quantize\n * and call the proper quantization function\n */\n function quantize_xrpow(xp, pi, istep, codInfo, prevNoise) {\n /* quantize on xr^(3/4) instead of xr */\n var sfb;\n var sfbmax;\n var j = 0;\n var prev_data_use;\n var accumulate = 0;\n var accumulate01 = 0;\n var xpPos = 0;\n var iData = pi;\n var iDataPos = 0;\n var acc_iData = iData;\n var acc_iDataPos = 0;\n var acc_xp = xp;\n var acc_xpPos = 0;\n\n /*\n * Reusing previously computed data does not seems to work if global\n * gain is changed. Finding why it behaves this way would allow to use a\n * cache of previously computed values (let's 10 cached values per sfb)\n * that would probably provide a noticeable speedup\n */\n prev_data_use = (prevNoise != null && (codInfo.global_gain == prevNoise.global_gain));\n\n if (codInfo.block_type == Encoder.SHORT_TYPE)\n sfbmax = 38;\n else\n sfbmax = 21;\n\n for (sfb = 0; sfb <= sfbmax; sfb++) {\n var step = -1;\n\n if (prev_data_use || codInfo.block_type == Encoder.NORM_TYPE) {\n step = codInfo.global_gain\n - ((codInfo.scalefac[sfb] + (codInfo.preflag != 0 ? qupvt.pretab[sfb]\n : 0)) << (codInfo.scalefac_scale + 1))\n - codInfo.subblock_gain[codInfo.window[sfb]] * 8;\n }\n if (prev_data_use && (prevNoise.step[sfb] == step)) {\n /*\n * do not recompute this part, but compute accumulated lines\n */\n if (accumulate != 0) {\n quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,\n acc_iData, acc_iDataPos);\n accumulate = 0;\n }\n if (accumulate01 != 0) {\n quantize_lines_xrpow_01(accumulate01, istep, acc_xp,\n acc_xpPos, acc_iData, acc_iDataPos);\n accumulate01 = 0;\n }\n } else { /* should compute this part */\n var l = codInfo.width[sfb];\n\n if ((j + codInfo.width[sfb]) > codInfo.max_nonzero_coeff) {\n /* do not compute upper zero part */\n var usefullsize;\n usefullsize = codInfo.max_nonzero_coeff - j + 1;\n Arrays.fill(pi, codInfo.max_nonzero_coeff, 576, 0);\n l = usefullsize;\n\n if (l < 0) {\n l = 0;\n }\n\n /* no need to compute higher sfb values */\n sfb = sfbmax + 1;\n }\n\n /* accumulate lines to quantize */\n if (0 == accumulate && 0 == accumulate01) {\n acc_iData = iData;\n acc_iDataPos = iDataPos;\n acc_xp = xp;\n acc_xpPos = xpPos;\n }\n if (prevNoise != null && prevNoise.sfb_count1 > 0\n && sfb >= prevNoise.sfb_count1\n && prevNoise.step[sfb] > 0\n && step >= prevNoise.step[sfb]) {\n\n if (accumulate != 0) {\n quantize_lines_xrpow(accumulate, istep, acc_xp,\n acc_xpPos, acc_iData, acc_iDataPos);\n accumulate = 0;\n acc_iData = iData;\n acc_iDataPos = iDataPos;\n acc_xp = xp;\n acc_xpPos = xpPos;\n }\n accumulate01 += l;\n } else {\n if (accumulate01 != 0) {\n quantize_lines_xrpow_01(accumulate01, istep, acc_xp,\n acc_xpPos, acc_iData, acc_iDataPos);\n accumulate01 = 0;\n acc_iData = iData;\n acc_iDataPos = iDataPos;\n acc_xp = xp;\n acc_xpPos = xpPos;\n }\n accumulate += l;\n }\n\n if (l <= 0) {\n /*\n * rh: 20040215 may happen due to \"prev_data_use\"\n * optimization\n */\n if (accumulate01 != 0) {\n quantize_lines_xrpow_01(accumulate01, istep, acc_xp,\n acc_xpPos, acc_iData, acc_iDataPos);\n accumulate01 = 0;\n }\n if (accumulate != 0) {\n quantize_lines_xrpow(accumulate, istep, acc_xp,\n acc_xpPos, acc_iData, acc_iDataPos);\n accumulate = 0;\n }\n\n break;\n /* ends for-loop */\n }\n }\n if (sfb <= sfbmax) {\n iDataPos += codInfo.width[sfb];\n xpPos += codInfo.width[sfb];\n j += codInfo.width[sfb];\n }\n }\n if (accumulate != 0) { /* last data part */\n quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,\n acc_iData, acc_iDataPos);\n accumulate = 0;\n }\n if (accumulate01 != 0) { /* last data part */\n quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_xpPos,\n acc_iData, acc_iDataPos);\n accumulate01 = 0;\n }\n\n }\n\n /**\n * ix_max\n */\n function ix_max(ix, ixPos, endPos) {\n var max1 = 0, max2 = 0;\n\n do {\n var x1 = ix[ixPos++];\n var x2 = ix[ixPos++];\n if (max1 < x1)\n max1 = x1;\n\n if (max2 < x2)\n max2 = x2;\n } while (ixPos < endPos);\n if (max1 < max2)\n max1 = max2;\n return max1;\n }\n\n function count_bit_ESC(ix, ixPos, end, t1, t2, s) {\n /* ESC-table is used */\n var linbits = Tables.ht[t1].xlen * 65536 + Tables.ht[t2].xlen;\n var sum = 0, sum2;\n\n do {\n var x = ix[ixPos++];\n var y = ix[ixPos++];\n\n if (x != 0) {\n if (x > 14) {\n x = 15;\n sum += linbits;\n }\n x *= 16;\n }\n\n if (y != 0) {\n if (y > 14) {\n y = 15;\n sum += linbits;\n }\n x += y;\n }\n\n sum += Tables.largetbl[x];\n } while (ixPos < end);\n\n sum2 = sum & 0xffff;\n sum >>= 16;\n\n if (sum > sum2) {\n sum = sum2;\n t1 = t2;\n }\n\n s.bits += sum;\n return t1;\n }\n\n function count_bit_noESC(ix, ixPos, end, s) {\n /* No ESC-words */\n var sum1 = 0;\n var hlen1 = Tables.ht[1].hlen;\n\n do {\n var x = ix[ixPos + 0] * 2 + ix[ixPos + 1];\n ixPos += 2;\n sum1 += hlen1[x];\n } while (ixPos < end);\n\n s.bits += sum1;\n return 1;\n }\n\n function count_bit_noESC_from2(ix, ixPos, end, t1, s) {\n /* No ESC-words */\n var sum = 0, sum2;\n var xlen = Tables.ht[t1].xlen;\n var hlen;\n if (t1 == 2)\n hlen = Tables.table23;\n else\n hlen = Tables.table56;\n\n do {\n var x = ix[ixPos + 0] * xlen + ix[ixPos + 1];\n ixPos += 2;\n sum += hlen[x];\n } while (ixPos < end);\n\n sum2 = sum & 0xffff;\n sum >>= 16;\n\n if (sum > sum2) {\n sum = sum2;\n t1++;\n }\n\n s.bits += sum;\n return t1;\n }\n\n function count_bit_noESC_from3(ix, ixPos, end, t1, s) {\n /* No ESC-words */\n var sum1 = 0;\n var sum2 = 0;\n var sum3 = 0;\n var xlen = Tables.ht[t1].xlen;\n var hlen1 = Tables.ht[t1].hlen;\n var hlen2 = Tables.ht[t1 + 1].hlen;\n var hlen3 = Tables.ht[t1 + 2].hlen;\n\n do {\n var x = ix[ixPos + 0] * xlen + ix[ixPos + 1];\n ixPos += 2;\n sum1 += hlen1[x];\n sum2 += hlen2[x];\n sum3 += hlen3[x];\n } while (ixPos < end);\n var t = t1;\n if (sum1 > sum2) {\n sum1 = sum2;\n t++;\n }\n if (sum1 > sum3) {\n sum1 = sum3;\n t = t1 + 2;\n }\n s.bits += sum1;\n\n return t;\n }\n\n /*************************************************************************/\n /* choose table */\n /*************************************************************************/\n\n var huf_tbl_noESC = [1, 2, 5, 7, 7, 10, 10, 13, 13,\n 13, 13, 13, 13, 13, 13];\n\n /**\n * Choose the Huffman table that will encode ix[begin..end] with the fewest\n * bits.\n *\n * Note: This code contains knowledge about the sizes and characteristics of\n * the Huffman tables as defined in the IS (Table B.7), and will not work\n * with any arbitrary tables.\n */\n function choose_table(ix, ixPos, endPos, s) {\n var max = ix_max(ix, ixPos, endPos);\n\n switch (max) {\n case 0:\n return max;\n\n case 1:\n return count_bit_noESC(ix, ixPos, endPos, s);\n\n case 2:\n case 3:\n return count_bit_noESC_from2(ix, ixPos, endPos,\n huf_tbl_noESC[max - 1], s);\n\n case 4:\n case 5:\n case 6:\n case 7:\n case 8:\n case 9:\n case 10:\n case 11:\n case 12:\n case 13:\n case 14:\n case 15:\n return count_bit_noESC_from3(ix, ixPos, endPos,\n huf_tbl_noESC[max - 1], s);\n\n default:\n /* try tables with linbits */\n if (max > QuantizePVT.IXMAX_VAL) {\n s.bits = QuantizePVT.LARGE_BITS;\n return -1;\n }\n max -= 15;\n var choice2;\n for (choice2 = 24; choice2 < 32; choice2++) {\n if (Tables.ht[choice2].linmax >= max) {\n break;\n }\n }\n var choice;\n for (choice = choice2 - 8; choice < 24; choice++) {\n if (Tables.ht[choice].linmax >= max) {\n break;\n }\n }\n return count_bit_ESC(ix, ixPos, endPos, choice, choice2, s);\n }\n }\n\n /**\n * count_bit\n */\n this.noquant_count_bits = function (gfc, gi, prev_noise) {\n var ix = gi.l3_enc;\n var i = Math.min(576, ((gi.max_nonzero_coeff + 2) >> 1) << 1);\n\n if (prev_noise != null)\n prev_noise.sfb_count1 = 0;\n\n /* Determine count1 region */\n for (; i > 1; i -= 2)\n if ((ix[i - 1] | ix[i - 2]) != 0)\n break;\n gi.count1 = i;\n\n /* Determines the number of bits to encode the quadruples. */\n var a1 = 0;\n var a2 = 0;\n for (; i > 3; i -= 4) {\n var p;\n /* hack to check if all values <= 1 */\n //throw \"TODO: HACK if ((((long) ix[i - 1] | (long) ix[i - 2] | (long) ix[i - 3] | (long) ix[i - 4]) & 0xffffffffL) > 1L \"\n //if (true) {\n if (((ix[i - 1] | ix[i - 2] | ix[i - 3] | ix[i - 4]) & 0x7fffffff) > 1) {\n break;\n }\n p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];\n a1 += Tables.t32l[p];\n a2 += Tables.t33l[p];\n }\n var bits = a1;\n gi.count1table_select = 0;\n if (a1 > a2) {\n bits = a2;\n gi.count1table_select = 1;\n }\n\n gi.count1bits = bits;\n gi.big_values = i;\n if (i == 0)\n return bits;\n\n if (gi.block_type == Encoder.SHORT_TYPE) {\n a1 = 3 * gfc.scalefac_band.s[3];\n if (a1 > gi.big_values)\n a1 = gi.big_values;\n a2 = gi.big_values;\n\n } else if (gi.block_type == Encoder.NORM_TYPE) {\n /* bv_scf has 576 entries (0..575) */\n a1 = gi.region0_count = gfc.bv_scf[i - 2];\n a2 = gi.region1_count = gfc.bv_scf[i - 1];\n\n a2 = gfc.scalefac_band.l[a1 + a2 + 2];\n a1 = gfc.scalefac_band.l[a1 + 1];\n if (a2 < i) {\n var bi = new Bits(bits);\n gi.table_select[2] = choose_table(ix, a2, i, bi);\n bits = bi.bits;\n }\n } else {\n gi.region0_count = 7;\n /* gi.region1_count = SBPSY_l - 7 - 1; */\n gi.region1_count = Encoder.SBMAX_l - 1 - 7 - 1;\n a1 = gfc.scalefac_band.l[7 + 1];\n a2 = i;\n if (a1 > a2) {\n a1 = a2;\n }\n }\n\n /* have to allow for the case when bigvalues < region0 < region1 */\n /* (and region0, region1 are ignored) */\n a1 = Math.min(a1, i);\n a2 = Math.min(a2, i);\n\n\n /* Count the number of bits necessary to code the bigvalues region. */\n if (0 < a1) {\n var bi = new Bits(bits);\n gi.table_select[0] = choose_table(ix, 0, a1, bi);\n bits = bi.bits;\n }\n if (a1 < a2) {\n var bi = new Bits(bits);\n gi.table_select[1] = choose_table(ix, a1, a2, bi);\n bits = bi.bits;\n }\n if (gfc.use_best_huffman == 2) {\n gi.part2_3_length = bits;\n best_huffman_divide(gfc, gi);\n bits = gi.part2_3_length;\n }\n\n if (prev_noise != null) {\n if (gi.block_type == Encoder.NORM_TYPE) {\n var sfb = 0;\n while (gfc.scalefac_band.l[sfb] < gi.big_values) {\n sfb++;\n }\n prev_noise.sfb_count1 = sfb;\n }\n }\n\n return bits;\n }\n\n this.count_bits = function (gfc, xr, gi, prev_noise) {\n var ix = gi.l3_enc;\n\n /* since quantize_xrpow uses table lookup, we need to check this first: */\n var w = (QuantizePVT.IXMAX_VAL) / qupvt.IPOW20(gi.global_gain);\n\n if (gi.xrpow_max > w)\n return QuantizePVT.LARGE_BITS;\n\n quantize_xrpow(xr, ix, qupvt.IPOW20(gi.global_gain), gi, prev_noise);\n\n if ((gfc.substep_shaping & 2) != 0) {\n var j = 0;\n /* 0.634521682242439 = 0.5946*2**(.5*0.1875) */\n var gain = gi.global_gain + gi.scalefac_scale;\n var roundfac = 0.634521682242439 / qupvt.IPOW20(gain);\n for (var sfb = 0; sfb < gi.sfbmax; sfb++) {\n var width = gi.width[sfb];\n if (0 == gfc.pseudohalf[sfb]) {\n j += width;\n } else {\n var k;\n for (k = j, j += width; k < j; ++k) {\n ix[k] = (xr[k] >= roundfac) ? ix[k] : 0;\n }\n }\n }\n }\n return this.noquant_count_bits(gfc, gi, prev_noise);\n }\n\n /**\n * re-calculate the best scalefac_compress using scfsi the saved bits are\n * kept in the bit reservoir.\n */\n function recalc_divide_init(gfc, cod_info, ix, r01_bits, r01_div, r0_tbl, r1_tbl) {\n var bigv = cod_info.big_values;\n\n for (var r0 = 0; r0 <= 7 + 15; r0++) {\n r01_bits[r0] = QuantizePVT.LARGE_BITS;\n }\n\n for (var r0 = 0; r0 < 16; r0++) {\n var a1 = gfc.scalefac_band.l[r0 + 1];\n if (a1 >= bigv)\n break;\n var r0bits = 0;\n var bi = new Bits(r0bits);\n var r0t = choose_table(ix, 0, a1, bi);\n r0bits = bi.bits;\n\n for (var r1 = 0; r1 < 8; r1++) {\n var a2 = gfc.scalefac_band.l[r0 + r1 + 2];\n if (a2 >= bigv)\n break;\n var bits = r0bits;\n bi = new Bits(bits);\n var r1t = choose_table(ix, a1, a2, bi);\n bits = bi.bits;\n if (r01_bits[r0 + r1] > bits) {\n r01_bits[r0 + r1] = bits;\n r01_div[r0 + r1] = r0;\n r0_tbl[r0 + r1] = r0t;\n r1_tbl[r0 + r1] = r1t;\n }\n }\n }\n }\n\n function recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl) {\n var bigv = cod_info2.big_values;\n\n for (var r2 = 2; r2 < Encoder.SBMAX_l + 1; r2++) {\n var a2 = gfc.scalefac_band.l[r2];\n if (a2 >= bigv)\n break;\n var bits = r01_bits[r2 - 2] + cod_info2.count1bits;\n if (gi.part2_3_length <= bits)\n break;\n\n var bi = new Bits(bits);\n var r2t = choose_table(ix, a2, bigv, bi);\n bits = bi.bits;\n if (gi.part2_3_length <= bits)\n continue;\n\n gi.assign(cod_info2);\n gi.part2_3_length = bits;\n gi.region0_count = r01_div[r2 - 2];\n gi.region1_count = r2 - 2 - r01_div[r2 - 2];\n gi.table_select[0] = r0_tbl[r2 - 2];\n gi.table_select[1] = r1_tbl[r2 - 2];\n gi.table_select[2] = r2t;\n }\n }\n\n this.best_huffman_divide = function (gfc, gi) {\n var cod_info2 = new GrInfo();\n var ix = gi.l3_enc;\n var r01_bits = new_int(7 + 15 + 1);\n var r01_div = new_int(7 + 15 + 1);\n var r0_tbl = new_int(7 + 15 + 1);\n var r1_tbl = new_int(7 + 15 + 1);\n\n /* SHORT BLOCK stuff fails for MPEG2 */\n if (gi.block_type == Encoder.SHORT_TYPE && gfc.mode_gr == 1)\n return;\n\n cod_info2.assign(gi);\n if (gi.block_type == Encoder.NORM_TYPE) {\n recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);\n recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div,\n r0_tbl, r1_tbl);\n }\n var i = cod_info2.big_values;\n if (i == 0 || (ix[i - 2] | ix[i - 1]) > 1)\n return;\n\n i = gi.count1 + 2;\n if (i > 576)\n return;\n\n /* Determines the number of bits to encode the quadruples. */\n cod_info2.assign(gi);\n cod_info2.count1 = i;\n var a1 = 0;\n var a2 = 0;\n\n\n for (; i > cod_info2.big_values; i -= 4) {\n var p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2\n + ix[i - 1];\n a1 += Tables.t32l[p];\n a2 += Tables.t33l[p];\n }\n cod_info2.big_values = i;\n\n cod_info2.count1table_select = 0;\n if (a1 > a2) {\n a1 = a2;\n cod_info2.count1table_select = 1;\n }\n\n cod_info2.count1bits = a1;\n\n if (cod_info2.block_type == Encoder.NORM_TYPE)\n recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div,\n r0_tbl, r1_tbl);\n else {\n /* Count the number of bits necessary to code the bigvalues region. */\n cod_info2.part2_3_length = a1;\n a1 = gfc.scalefac_band.l[7 + 1];\n if (a1 > i) {\n a1 = i;\n }\n if (a1 > 0) {\n var bi = new Bits(cod_info2.part2_3_length);\n cod_info2.table_select[0] = choose_table(ix, 0, a1, bi);\n cod_info2.part2_3_length = bi.bits;\n }\n if (i > a1) {\n var bi = new Bits(cod_info2.part2_3_length);\n cod_info2.table_select[1] = choose_table(ix, a1, i, bi);\n cod_info2.part2_3_length = bi.bits;\n }\n if (gi.part2_3_length > cod_info2.part2_3_length)\n gi.assign(cod_info2);\n }\n }\n\n var slen1_n = [1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16];\n var slen2_n = [1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8];\n var slen1_tab = [0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4];\n var slen2_tab = [0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3];\n Takehiro.slen1_tab = slen1_tab;\n Takehiro.slen2_tab = slen2_tab;\n\n function scfsi_calc(ch, l3_side) {\n var sfb;\n var gi = l3_side.tt[1][ch];\n var g0 = l3_side.tt[0][ch];\n\n for (var i = 0; i < Tables.scfsi_band.length - 1; i++) {\n for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {\n if (g0.scalefac[sfb] != gi.scalefac[sfb]\n && gi.scalefac[sfb] >= 0)\n break;\n }\n if (sfb == Tables.scfsi_band[i + 1]) {\n for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {\n gi.scalefac[sfb] = -1;\n }\n l3_side.scfsi[ch][i] = 1;\n }\n }\n var s1 = 0;\n var c1 = 0;\n for (sfb = 0; sfb < 11; sfb++) {\n if (gi.scalefac[sfb] == -1)\n continue;\n c1++;\n if (s1 < gi.scalefac[sfb])\n s1 = gi.scalefac[sfb];\n }\n var s2 = 0;\n var c2 = 0;\n for (; sfb < Encoder.SBPSY_l; sfb++) {\n if (gi.scalefac[sfb] == -1)\n continue;\n c2++;\n if (s2 < gi.scalefac[sfb])\n s2 = gi.scalefac[sfb];\n }\n\n for (var i = 0; i < 16; i++) {\n if (s1 < slen1_n[i] && s2 < slen2_n[i]) {\n var c = slen1_tab[i] * c1 + slen2_tab[i] * c2;\n if (gi.part2_length > c) {\n gi.part2_length = c;\n gi.scalefac_compress = i;\n }\n }\n }\n }\n\n /**\n * Find the optimal way to store the scalefactors. Only call this routine\n * after final scalefactors have been chosen and the channel/granule will\n * not be re-encoded.\n */\n this.best_scalefac_store = function (gfc, gr, ch, l3_side) {\n /* use scalefac_scale if we can */\n var gi = l3_side.tt[gr][ch];\n var sfb, i, j, l;\n var recalc = 0;\n\n /*\n * remove scalefacs from bands with ix=0. This idea comes from the AAC\n * ISO docs. added mt 3/00\n */\n /* check if l3_enc=0 */\n j = 0;\n for (sfb = 0; sfb < gi.sfbmax; sfb++) {\n var width = gi.width[sfb];\n j += width;\n for (l = -width; l < 0; l++) {\n if (gi.l3_enc[l + j] != 0)\n break;\n }\n if (l == 0)\n gi.scalefac[sfb] = recalc = -2;\n /* anything goes. */\n /*\n * only best_scalefac_store and calc_scfsi know--and only they\n * should know--about the magic number -2.\n */\n }\n\n if (0 == gi.scalefac_scale && 0 == gi.preflag) {\n var s = 0;\n for (sfb = 0; sfb < gi.sfbmax; sfb++)\n if (gi.scalefac[sfb] > 0)\n s |= gi.scalefac[sfb];\n\n if (0 == (s & 1) && s != 0) {\n for (sfb = 0; sfb < gi.sfbmax; sfb++)\n if (gi.scalefac[sfb] > 0)\n gi.scalefac[sfb] >>= 1;\n\n gi.scalefac_scale = recalc = 1;\n }\n }\n\n if (0 == gi.preflag && gi.block_type != Encoder.SHORT_TYPE\n && gfc.mode_gr == 2) {\n for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)\n if (gi.scalefac[sfb] < qupvt.pretab[sfb]\n && gi.scalefac[sfb] != -2)\n break;\n if (sfb == Encoder.SBPSY_l) {\n for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)\n if (gi.scalefac[sfb] > 0)\n gi.scalefac[sfb] -= qupvt.pretab[sfb];\n\n gi.preflag = recalc = 1;\n }\n }\n\n for (i = 0; i < 4; i++)\n l3_side.scfsi[ch][i] = 0;\n\n if (gfc.mode_gr == 2 && gr == 1\n && l3_side.tt[0][ch].block_type != Encoder.SHORT_TYPE\n && l3_side.tt[1][ch].block_type != Encoder.SHORT_TYPE) {\n scfsi_calc(ch, l3_side);\n recalc = 0;\n }\n for (sfb = 0; sfb < gi.sfbmax; sfb++) {\n if (gi.scalefac[sfb] == -2) {\n gi.scalefac[sfb] = 0;\n /* if anything goes, then 0 is a good choice */\n }\n }\n if (recalc != 0) {\n if (gfc.mode_gr == 2) {\n this.scale_bitcount(gi);\n } else {\n this.scale_bitcount_lsf(gfc, gi);\n }\n }\n }\n\n function all_scalefactors_not_negative(scalefac, n) {\n for (var i = 0; i < n; ++i) {\n if (scalefac[i] < 0)\n return false;\n }\n return true;\n }\n\n /**\n * number of bits used to encode scalefacs.\n *\n * 18*slen1_tab[i] + 18*slen2_tab[i]\n */\n var scale_short = [0, 18, 36, 54, 54, 36, 54, 72,\n 54, 72, 90, 72, 90, 108, 108, 126];\n\n /**\n * number of bits used to encode scalefacs.\n *\n * 17*slen1_tab[i] + 18*slen2_tab[i]\n */\n var scale_mixed = [0, 18, 36, 54, 51, 35, 53, 71,\n 52, 70, 88, 69, 87, 105, 104, 122];\n\n /**\n * number of bits used to encode scalefacs.\n *\n * 11*slen1_tab[i] + 10*slen2_tab[i]\n */\n var scale_long = [0, 10, 20, 30, 33, 21, 31, 41, 32, 42,\n 52, 43, 53, 63, 64, 74];\n\n /**\n * Also calculates the number of bits necessary to code the scalefactors.\n */\n this.scale_bitcount = function (cod_info) {\n var k, sfb, max_slen1 = 0, max_slen2 = 0;\n\n /* maximum values */\n var tab;\n var scalefac = cod_info.scalefac;\n\n\n if (cod_info.block_type == Encoder.SHORT_TYPE) {\n tab = scale_short;\n if (cod_info.mixed_block_flag != 0)\n tab = scale_mixed;\n } else { /* block_type == 1,2,or 3 */\n tab = scale_long;\n if (0 == cod_info.preflag) {\n for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)\n if (scalefac[sfb] < qupvt.pretab[sfb])\n break;\n\n if (sfb == Encoder.SBPSY_l) {\n cod_info.preflag = 1;\n for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)\n scalefac[sfb] -= qupvt.pretab[sfb];\n }\n }\n }\n\n for (sfb = 0; sfb < cod_info.sfbdivide; sfb++)\n if (max_slen1 < scalefac[sfb])\n max_slen1 = scalefac[sfb];\n\n for (; sfb < cod_info.sfbmax; sfb++)\n if (max_slen2 < scalefac[sfb])\n max_slen2 = scalefac[sfb];\n\n /*\n * from Takehiro TOMINAGA \n * Switch mappings for VBR mode VBR_RH\n * vbr_q qcomp_l qcomp_s expY st_lrm st_s mask adj_l adj_s ath_lower ath_curve ath_sens interChR safejoint sfb21mod msfix\n *
\n */\n var vbr_old_switch_map = [\n new VBRPresets(0, 9, 9, 0, 5.20, 125.0, -4.2, -6.3, 4.8, 1, 0, 0, 2, 21, 0.97),\n new VBRPresets(1, 9, 9, 0, 5.30, 125.0, -3.6, -5.6, 4.5, 1.5, 0, 0, 2, 21, 1.35),\n new VBRPresets(2, 9, 9, 0, 5.60, 125.0, -2.2, -3.5, 2.8, 2, 0, 0, 2, 21, 1.49),\n new VBRPresets(3, 9, 9, 1, 5.80, 130.0, -1.8, -2.8, 2.6, 3, -4, 0, 2, 20, 1.64),\n new VBRPresets(4, 9, 9, 1, 6.00, 135.0, -0.7, -1.1, 1.1, 3.5, -8, 0, 2, 0, 1.79),\n new VBRPresets(5, 9, 9, 1, 6.40, 140.0, 0.5, 0.4, -7.5, 4, -12, 0.0002, 0, 0, 1.95),\n new VBRPresets(6, 9, 9, 1, 6.60, 145.0, 0.67, 0.65, -14.7, 6.5, -19, 0.0004, 0, 0, 2.30),\n new VBRPresets(7, 9, 9, 1, 6.60, 145.0, 0.8, 0.75, -19.7, 8, -22, 0.0006, 0, 0, 2.70),\n new VBRPresets(8, 9, 9, 1, 6.60, 145.0, 1.2, 1.15, -27.5, 10, -23, 0.0007, 0, 0, 0),\n new VBRPresets(9, 9, 9, 1, 6.60, 145.0, 1.6, 1.6, -36, 11, -25, 0.0008, 0, 0, 0),\n new VBRPresets(10, 9, 9, 1, 6.60, 145.0, 2.0, 2.0, -36, 12, -25, 0.0008, 0, 0, 0)\n ];\n\n /**\n * \n * vbr_q qcomp_l qcomp_s expY st_lrm st_s mask adj_l adj_s ath_lower ath_curve ath_sens interChR safejoint sfb21mod msfix\n *
\n */\n var vbr_psy_switch_map = [\n new VBRPresets(0, 9, 9, 0, 4.20, 25.0, -7.0, -4.0, 7.5, 1, 0, 0, 2, 26, 0.97),\n new VBRPresets(1, 9, 9, 0, 4.20, 25.0, -5.6, -3.6, 4.5, 1.5, 0, 0, 2, 21, 1.35),\n new VBRPresets(2, 9, 9, 0, 4.20, 25.0, -4.4, -1.8, 2, 2, 0, 0, 2, 18, 1.49),\n new VBRPresets(3, 9, 9, 1, 4.20, 25.0, -3.4, -1.25, 1.1, 3, -4, 0, 2, 15, 1.64),\n new VBRPresets(4, 9, 9, 1, 4.20, 25.0, -2.2, 0.1, 0, 3.5, -8, 0, 2, 0, 1.79),\n new VBRPresets(5, 9, 9, 1, 4.20, 25.0, -1.0, 1.65, -7.7, 4, -12, 0.0002, 0, 0, 1.95),\n new VBRPresets(6, 9, 9, 1, 4.20, 25.0, -0.0, 2.47, -7.7, 6.5, -19, 0.0004, 0, 0, 2),\n new VBRPresets(7, 9, 9, 1, 4.20, 25.0, 0.5, 2.0, -14.5, 8, -22, 0.0006, 0, 0, 2),\n new VBRPresets(8, 9, 9, 1, 4.20, 25.0, 1.0, 2.4, -22.0, 10, -23, 0.0007, 0, 0, 2),\n new VBRPresets(9, 9, 9, 1, 4.20, 25.0, 1.5, 2.95, -30.0, 11, -25, 0.0008, 0, 0, 2),\n new VBRPresets(10, 9, 9, 1, 4.20, 25.0, 2.0, 2.95, -36.0, 12, -30, 0.0008, 0, 0, 2)\n ];\n\n function apply_vbr_preset(gfp, a, enforce) {\n var vbr_preset = gfp.VBR == VbrMode.vbr_rh ? vbr_old_switch_map\n : vbr_psy_switch_map;\n\n var x = gfp.VBR_q_frac;\n var p = vbr_preset[a];\n var q = vbr_preset[a + 1];\n var set = p;\n\n // NOOP(vbr_q);\n // NOOP(quant_comp);\n // NOOP(quant_comp_s);\n // NOOP(expY);\n p.st_lrm = p.st_lrm + x * (q.st_lrm - p.st_lrm);\n // LERP(st_lrm);\n p.st_s = p.st_s + x * (q.st_s - p.st_s);\n // LERP(st_s);\n p.masking_adj = p.masking_adj + x * (q.masking_adj - p.masking_adj);\n // LERP(masking_adj);\n p.masking_adj_short = p.masking_adj_short + x\n * (q.masking_adj_short - p.masking_adj_short);\n // LERP(masking_adj_short);\n p.ath_lower = p.ath_lower + x * (q.ath_lower - p.ath_lower);\n // LERP(ath_lower);\n p.ath_curve = p.ath_curve + x * (q.ath_curve - p.ath_curve);\n // LERP(ath_curve);\n p.ath_sensitivity = p.ath_sensitivity + x\n * (q.ath_sensitivity - p.ath_sensitivity);\n // LERP(ath_sensitivity);\n p.interch = p.interch + x * (q.interch - p.interch);\n // LERP(interch);\n // NOOP(safejoint);\n // NOOP(sfb21mod);\n p.msfix = p.msfix + x * (q.msfix - p.msfix);\n // LERP(msfix);\n\n lame_set_VBR_q(gfp, set.vbr_q);\n\n if (enforce != 0)\n gfp.quant_comp = set.quant_comp;\n else if (!(Math.abs(gfp.quant_comp - -1) > 0))\n gfp.quant_comp = set.quant_comp;\n // SET_OPTION(quant_comp, set.quant_comp, -1);\n if (enforce != 0)\n gfp.quant_comp_short = set.quant_comp_s;\n else if (!(Math.abs(gfp.quant_comp_short - -1) > 0))\n gfp.quant_comp_short = set.quant_comp_s;\n // SET_OPTION(quant_comp_short, set.quant_comp_s, -1);\n if (set.expY != 0) {\n gfp.experimentalY = set.expY != 0;\n }\n if (enforce != 0)\n gfp.internal_flags.nsPsy.attackthre = set.st_lrm;\n else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre - -1) > 0))\n gfp.internal_flags.nsPsy.attackthre = set.st_lrm;\n // SET_OPTION(short_threshold_lrm, set.st_lrm, -1);\n if (enforce != 0)\n gfp.internal_flags.nsPsy.attackthre_s = set.st_s;\n else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre_s - -1) > 0))\n gfp.internal_flags.nsPsy.attackthre_s = set.st_s;\n // SET_OPTION(short_threshold_s, set.st_s, -1);\n if (enforce != 0)\n gfp.maskingadjust = set.masking_adj;\n else if (!(Math.abs(gfp.maskingadjust - 0) > 0))\n gfp.maskingadjust = set.masking_adj;\n // SET_OPTION(maskingadjust, set.masking_adj, 0);\n if (enforce != 0)\n gfp.maskingadjust_short = set.masking_adj_short;\n else if (!(Math.abs(gfp.maskingadjust_short - 0) > 0))\n gfp.maskingadjust_short = set.masking_adj_short;\n // SET_OPTION(maskingadjust_short, set.masking_adj_short, 0);\n if (enforce != 0)\n gfp.ATHlower = -set.ath_lower / 10.0;\n else if (!(Math.abs((-gfp.ATHlower * 10.0) - 0) > 0))\n gfp.ATHlower = -set.ath_lower / 10.0;\n // SET_OPTION(ATHlower, set.ath_lower, 0);\n if (enforce != 0)\n gfp.ATHcurve = set.ath_curve;\n else if (!(Math.abs(gfp.ATHcurve - -1) > 0))\n gfp.ATHcurve = set.ath_curve;\n // SET_OPTION(ATHcurve, set.ath_curve, -1);\n if (enforce != 0)\n gfp.athaa_sensitivity = set.ath_sensitivity;\n else if (!(Math.abs(gfp.athaa_sensitivity - -1) > 0))\n gfp.athaa_sensitivity = set.ath_sensitivity;\n // SET_OPTION(athaa_sensitivity, set.ath_sensitivity, 0);\n if (set.interch > 0) {\n if (enforce != 0)\n gfp.interChRatio = set.interch;\n else if (!(Math.abs(gfp.interChRatio - -1) > 0))\n gfp.interChRatio = set.interch;\n // SET_OPTION(interChRatio, set.interch, -1);\n }\n\n /* parameters for which there is no proper set/get interface */\n if (set.safejoint > 0) {\n gfp.exp_nspsytune = gfp.exp_nspsytune | set.safejoint;\n }\n if (set.sfb21mod > 0) {\n gfp.exp_nspsytune = gfp.exp_nspsytune | (set.sfb21mod << 20);\n }\n if (enforce != 0)\n gfp.msfix = set.msfix;\n else if (!(Math.abs(gfp.msfix - -1) > 0))\n gfp.msfix = set.msfix;\n // SET_OPTION(msfix, set.msfix, -1);\n\n if (enforce == 0) {\n gfp.VBR_q = a;\n gfp.VBR_q_frac = x;\n }\n }\n\n /**\n * \n * Switch mappings for ABR mode\n *\n * kbps quant q_s safejoint nsmsfix st_lrm st_s ns-bass scale msk ath_lwr ath_curve interch , sfscale\n *
\n */\n var abr_switch_map = [\n new ABRPresets(8, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -30.0, 11, 0.0012, 1), /* 8, impossible to use in stereo */\n new ABRPresets(16, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -25.0, 11, 0.0010, 1), /* 16 */\n new ABRPresets(24, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -20.0, 11, 0.0010, 1), /* 24 */\n new ABRPresets(32, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -15.0, 11, 0.0010, 1), /* 32 */\n new ABRPresets(40, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -10.0, 11, 0.0009, 1), /* 40 */\n new ABRPresets(48, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -10.0, 11, 0.0009, 1), /* 48 */\n new ABRPresets(56, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -6.0, 11, 0.0008, 1), /* 56 */\n new ABRPresets(64, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -2.0, 11, 0.0008, 1), /* 64 */\n new ABRPresets(80, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, .0, 8, 0.0007, 1), /* 80 */\n new ABRPresets(96, 9, 9, 0, 2.50, 6.60, 145, 0, 0.95, 0, 1.0, 5.5, 0.0006, 1), /* 96 */\n new ABRPresets(112, 9, 9, 0, 2.25, 6.60, 145, 0, 0.95, 0, 2.0, 4.5, 0.0005, 1), /* 112 */\n new ABRPresets(128, 9, 9, 0, 1.95, 6.40, 140, 0, 0.95, 0, 3.0, 4, 0.0002, 1), /* 128 */\n new ABRPresets(160, 9, 9, 1, 1.79, 6.00, 135, 0, 0.95, -2, 5.0, 3.5, 0, 1), /* 160 */\n new ABRPresets(192, 9, 9, 1, 1.49, 5.60, 125, 0, 0.97, -4, 7.0, 3, 0, 0), /* 192 */\n new ABRPresets(224, 9, 9, 1, 1.25, 5.20, 125, 0, 0.98, -6, 9.0, 2, 0, 0), /* 224 */\n new ABRPresets(256, 9, 9, 1, 0.97, 5.20, 125, 0, 1.00, -8, 10.0, 1, 0, 0), /* 256 */\n new ABRPresets(320, 9, 9, 1, 0.90, 5.20, 125, 0, 1.00, -10, 12.0, 0, 0, 0) /* 320 */\n ];\n\n function apply_abr_preset(gfp, preset, enforce) {\n /* Variables for the ABR stuff */\n var actual_bitrate = preset;\n\n var r = lame.nearestBitrateFullIndex(preset);\n\n gfp.VBR = VbrMode.vbr_abr;\n gfp.VBR_mean_bitrate_kbps = actual_bitrate;\n gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps, 320);\n gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps, 8);\n gfp.brate = gfp.VBR_mean_bitrate_kbps;\n if (gfp.VBR_mean_bitrate_kbps > 320) {\n gfp.disable_reservoir = true;\n }\n\n /* parameters for which there is no proper set/get interface */\n if (abr_switch_map[r].safejoint > 0)\n gfp.exp_nspsytune = gfp.exp_nspsytune | 2;\n /* safejoint */\n\n if (abr_switch_map[r].sfscale > 0) {\n gfp.internal_flags.noise_shaping = 2;\n }\n /* ns-bass tweaks */\n if (Math.abs(abr_switch_map[r].nsbass) > 0) {\n var k = (int)(abr_switch_map[r].nsbass * 4);\n if (k < 0)\n k += 64;\n gfp.exp_nspsytune = gfp.exp_nspsytune | (k << 2);\n }\n\n if (enforce != 0)\n gfp.quant_comp = abr_switch_map[r].quant_comp;\n else if (!(Math.abs(gfp.quant_comp - -1) > 0))\n gfp.quant_comp = abr_switch_map[r].quant_comp;\n // SET_OPTION(quant_comp, abr_switch_map[r].quant_comp, -1);\n if (enforce != 0)\n gfp.quant_comp_short = abr_switch_map[r].quant_comp_s;\n else if (!(Math.abs(gfp.quant_comp_short - -1) > 0))\n gfp.quant_comp_short = abr_switch_map[r].quant_comp_s;\n // SET_OPTION(quant_comp_short, abr_switch_map[r].quant_comp_s, -1);\n\n if (enforce != 0)\n gfp.msfix = abr_switch_map[r].nsmsfix;\n else if (!(Math.abs(gfp.msfix - -1) > 0))\n gfp.msfix = abr_switch_map[r].nsmsfix;\n // SET_OPTION(msfix, abr_switch_map[r].nsmsfix, -1);\n\n if (enforce != 0)\n gfp.internal_flags.nsPsy.attackthre = abr_switch_map[r].st_lrm;\n else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre - -1) > 0))\n gfp.internal_flags.nsPsy.attackthre = abr_switch_map[r].st_lrm;\n // SET_OPTION(short_threshold_lrm, abr_switch_map[r].st_lrm, -1);\n if (enforce != 0)\n gfp.internal_flags.nsPsy.attackthre_s = abr_switch_map[r].st_s;\n else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre_s - -1) > 0))\n gfp.internal_flags.nsPsy.attackthre_s = abr_switch_map[r].st_s;\n // SET_OPTION(short_threshold_s, abr_switch_map[r].st_s, -1);\n\n /*\n * ABR seems to have big problems with clipping, especially at low\n * bitrates\n */\n /*\n * so we compensate for that here by using a scale value depending on\n * bitrate\n */\n if (enforce != 0)\n gfp.scale = abr_switch_map[r].scale;\n else if (!(Math.abs(gfp.scale - -1) > 0))\n gfp.scale = abr_switch_map[r].scale;\n // SET_OPTION(scale, abr_switch_map[r].scale, -1);\n\n if (enforce != 0)\n gfp.maskingadjust = abr_switch_map[r].masking_adj;\n else if (!(Math.abs(gfp.maskingadjust - 0) > 0))\n gfp.maskingadjust = abr_switch_map[r].masking_adj;\n // SET_OPTION(maskingadjust, abr_switch_map[r].masking_adj, 0);\n if (abr_switch_map[r].masking_adj > 0) {\n if (enforce != 0)\n gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * .9);\n else if (!(Math.abs(gfp.maskingadjust_short - 0) > 0))\n gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * .9);\n // SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj *\n // .9, 0);\n } else {\n if (enforce != 0)\n gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * 1.1);\n else if (!(Math.abs(gfp.maskingadjust_short - 0) > 0))\n gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * 1.1);\n // SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj *\n // 1.1, 0);\n }\n\n if (enforce != 0)\n gfp.ATHlower = -abr_switch_map[r].ath_lower / 10.;\n else if (!(Math.abs((-gfp.ATHlower * 10.) - 0) > 0))\n gfp.ATHlower = -abr_switch_map[r].ath_lower / 10.;\n // SET_OPTION(ATHlower, abr_switch_map[r].ath_lower, 0);\n if (enforce != 0)\n gfp.ATHcurve = abr_switch_map[r].ath_curve;\n else if (!(Math.abs(gfp.ATHcurve - -1) > 0))\n gfp.ATHcurve = abr_switch_map[r].ath_curve;\n // SET_OPTION(ATHcurve, abr_switch_map[r].ath_curve, -1);\n\n if (enforce != 0)\n gfp.interChRatio = abr_switch_map[r].interch;\n else if (!(Math.abs(gfp.interChRatio - -1) > 0))\n gfp.interChRatio = abr_switch_map[r].interch;\n // SET_OPTION(interChRatio, abr_switch_map[r].interch, -1);\n\n return preset;\n }\n\n this.apply_preset = function(gfp, preset, enforce) {\n /* translate legacy presets */\n switch (preset) {\n case Lame.R3MIX:\n {\n preset = Lame.V3;\n gfp.VBR = VbrMode.vbr_mtrh;\n break;\n }\n case Lame.MEDIUM:\n {\n preset = Lame.V4;\n gfp.VBR = VbrMode.vbr_rh;\n break;\n }\n case Lame.MEDIUM_FAST:\n {\n preset = Lame.V4;\n gfp.VBR = VbrMode.vbr_mtrh;\n break;\n }\n case Lame.STANDARD:\n {\n preset = Lame.V2;\n gfp.VBR = VbrMode.vbr_rh;\n break;\n }\n case Lame.STANDARD_FAST:\n {\n preset = Lame.V2;\n gfp.VBR = VbrMode.vbr_mtrh;\n break;\n }\n case Lame.EXTREME:\n {\n preset = Lame.V0;\n gfp.VBR = VbrMode.vbr_rh;\n break;\n }\n case Lame.EXTREME_FAST:\n {\n preset = Lame.V0;\n gfp.VBR = VbrMode.vbr_mtrh;\n break;\n }\n case Lame.INSANE:\n {\n preset = 320;\n gfp.preset = preset;\n apply_abr_preset(gfp, preset, enforce);\n gfp.VBR = VbrMode.vbr_off;\n return preset;\n }\n }\n\n gfp.preset = preset;\n {\n switch (preset) {\n case Lame.V9:\n apply_vbr_preset(gfp, 9, enforce);\n return preset;\n case Lame.V8:\n apply_vbr_preset(gfp, 8, enforce);\n return preset;\n case Lame.V7:\n apply_vbr_preset(gfp, 7, enforce);\n return preset;\n case Lame.V6:\n apply_vbr_preset(gfp, 6, enforce);\n return preset;\n case Lame.V5:\n apply_vbr_preset(gfp, 5, enforce);\n return preset;\n case Lame.V4:\n apply_vbr_preset(gfp, 4, enforce);\n return preset;\n case Lame.V3:\n apply_vbr_preset(gfp, 3, enforce);\n return preset;\n case Lame.V2:\n apply_vbr_preset(gfp, 2, enforce);\n return preset;\n case Lame.V1:\n apply_vbr_preset(gfp, 1, enforce);\n return preset;\n case Lame.V0:\n apply_vbr_preset(gfp, 0, enforce);\n return preset;\n default:\n break;\n }\n }\n if (8 <= preset && preset <= 320) {\n return apply_abr_preset(gfp, preset, enforce);\n }\n\n /* no corresponding preset found */\n gfp.preset = 0;\n return preset;\n }\n\n // Rest from getset.c:\n\n /**\n * VBR quality level.
\n * 0 = highest
\n * 9 = lowest\n */\n function lame_set_VBR_q(gfp, VBR_q) {\n var ret = 0;\n\n if (0 > VBR_q) {\n /* Unknown VBR quality level! */\n ret = -1;\n VBR_q = 0;\n }\n if (9 < VBR_q) {\n ret = -1;\n VBR_q = 9;\n }\n\n gfp.VBR_q = VBR_q;\n gfp.VBR_q_frac = 0;\n return ret;\n }\n\n }\n\n /*\n * bit reservoir source file\n *\n * Copyright (c) 1999-2000 Mark Taylor\n *\n * This library is free software; you can redistribute it and/or\n * modify it under the terms of the GNU Lesser General Public\n * License as published by the Free Software Foundation; either\n * version 2 of the License, or (at your option) any later version.\n *\n * This library is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Library General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this library; if not, write to the\n * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n * Boston, MA 02111-1307, USA.\n */\n\n /* $Id: Reservoir.java,v 1.9 2011/05/24 20:48:06 kenchis Exp $ */\n\n//package mp3;\n\n /**\n * ResvFrameBegin:
\n * Called (repeatedly) at the beginning of a frame. Updates the maximum size of\n * the reservoir, and checks to make sure main_data_begin was set properly by\n * the formatter
\n * Background information:\n *\n * This is the original text from the ISO standard. Because of sooo many bugs\n * and irritations correcting comments are added in brackets []. A '^W' means\n * you should remove the last word.\n *\n * \n * 1. The following rule can be used to calculate the maximum\n * number of bits used for one granule [^W frame]:
\n */\n\n\n function Reservoir() {\n var bs;\n\n this.setModules = function(_bs) {\n bs = _bs;\n }\n\n this.ResvFrameBegin = function(gfp, mean_bits) {\n var gfc = gfp.internal_flags;\n var maxmp3buf;\n var l3_side = gfc.l3_side;\n\n var frameLength = bs.getframebits(gfp);\n mean_bits.bits = (frameLength - gfc.sideinfo_len * 8) / gfc.mode_gr;\n\n /**\n *
\n * At the highest possible bitrate of Layer III (320 kbps\n * per stereo signal [^W^W^W], 48 kHz) the frames must be of\n * [^W^W^W are designed to have] constant length, i.e.\n * one buffer [^W^W the frame] length is:
\n *\n * 320 kbps * 1152/48 kHz = 7680 bit = 960 byte\n *\n * This value is used as the maximum buffer per channel [^W^W] at\n * lower bitrates [than 320 kbps]. At 64 kbps mono or 128 kbps\n * stereo the main granule length is 64 kbps * 576/48 kHz = 768 bit\n * [per granule and channel] at 48 kHz sampling frequency.\n * This means that there is a maximum deviation (short time buffer\n * [= reservoir]) of 7680 - 2*2*768 = 4608 bits is allowed at 64 kbps.\n * The actual deviation is equal to the number of bytes [with the\n * meaning of octets] denoted by the main_data_end offset pointer.\n * The actual maximum deviation is (2^9-1)*8 bit = 4088 bits\n * [for MPEG-1 and (2^8-1)*8 bit for MPEG-2, both are hard limits].\n * ... The xchange of buffer bits between the left and right channel\n * is allowed without restrictions [exception: dual channel].\n * Because of the [constructed] constraint on the buffer size\n * main_data_end is always set to 0 in the case of bit_rate_index==14,\n * i.e. data rate 320 kbps per stereo signal [^W^W^W]. In this case\n * all data are allocated between adjacent header [^W sync] words\n * [, i.e. there is no buffering at all].\n * \n * Meaning of the variables:\n * resvLimit: (0, 8, ..., 8*255 (MPEG-2), 8*511 (MPEG-1))\n * Number of bits can be stored in previous frame(s) due to\n * counter size constaints\n * maxmp3buf: ( ??? ... 8*1951 (MPEG-1 and 2), 8*2047 (MPEG-2.5))\n * Number of bits allowed to encode one frame (you can take 8*511 bit\n * from the bit reservoir and at most 8*1440 bit from the current\n * frame (320 kbps, 32 kHz), so 8*1951 bit is the largest possible\n * value for MPEG-1 and -2)\n *\n * maximum allowed granule/channel size times 4 = 8*2047 bits.,\n * so this is the absolute maximum supported by the format.\n *\n *\n * fullFrameBits: maximum number of bits available for encoding\n * the current frame.\n *\n * mean_bits: target number of bits per granule.\n *\n * frameLength:\n *\n * gfc.ResvMax: maximum allowed reservoir\n *\n * gfc.ResvSize: current reservoir size\n *\n * l3_side.resvDrain_pre:\n * ancillary data to be added to previous frame:\n * (only usefull in VBR modes if it is possible to have\n * maxmp3buf < fullFrameBits)). Currently disabled,\n * see #define NEW_DRAIN\n * 2010-02-13: RH now enabled, it seems to be needed for CBR too,\n * as there exists one example, where the FhG decoder\n * can't decode a -b320 CBR file anymore.\n *\n * l3_side.resvDrain_post:\n * ancillary data to be added to this frame:\n *\n *
\n */\n\n /* main_data_begin has 9 bits in MPEG-1, 8 bits MPEG-2 */\n var resvLimit = (8 * 256) * gfc.mode_gr - 8;\n\n /*\n * maximum allowed frame size. dont use more than this number of bits,\n * even if the frame has the space for them:\n */\n if (gfp.brate > 320) {\n /* in freeformat the buffer is constant */\n maxmp3buf = 8 * ((int) ((gfp.brate * 1000)\n / (gfp.out_samplerate / 1152) / 8 + .5));\n } else {\n /*\n * all mp3 decoders should have enough buffer to handle this value:\n * size of a 320kbps 32kHz frame\n */\n maxmp3buf = 8 * 1440;\n\n /*\n * Bouvigne suggests this more lax interpretation of the ISO doc\n * instead of using 8*960.\n */\n\n if (gfp.strict_ISO) {\n maxmp3buf = 8 * ((int) (320000 / (gfp.out_samplerate / 1152) / 8 + .5));\n }\n }\n\n gfc.ResvMax = maxmp3buf - frameLength;\n if (gfc.ResvMax > resvLimit)\n gfc.ResvMax = resvLimit;\n if (gfc.ResvMax < 0 || gfp.disable_reservoir)\n gfc.ResvMax = 0;\n\n var fullFrameBits = mean_bits.bits * gfc.mode_gr\n + Math.min(gfc.ResvSize, gfc.ResvMax);\n\n if (fullFrameBits > maxmp3buf)\n fullFrameBits = maxmp3buf;\n\n\n l3_side.resvDrain_pre = 0;\n\n // frame analyzer code\n if (gfc.pinfo != null) {\n /*\n * expected bits per channel per granule [is this also right for\n * mono/stereo, MPEG-1/2 ?]\n */\n gfc.pinfo.mean_bits = mean_bits.bits / 2;\n gfc.pinfo.resvsize = gfc.ResvSize;\n }\n\n return fullFrameBits;\n }\n\n /**\n * returns targ_bits: target number of bits to use for 1 granule
\n * extra_bits: amount extra available from reservoir
\n * Mark Taylor 4/99\n */\n this.ResvMaxBits = function(gfp, mean_bits, targ_bits, cbr) {\n var gfc = gfp.internal_flags;\n var add_bits;\n var ResvSize = gfc.ResvSize, ResvMax = gfc.ResvMax;\n\n /* compensate the saved bits used in the 1st granule */\n if (cbr != 0)\n ResvSize += mean_bits;\n\n if ((gfc.substep_shaping & 1) != 0)\n ResvMax *= 0.9;\n\n targ_bits.bits = mean_bits;\n\n /* extra bits if the reservoir is almost full */\n if (ResvSize * 10 > ResvMax * 9) {\n add_bits = ResvSize - (ResvMax * 9) / 10;\n targ_bits.bits += add_bits;\n gfc.substep_shaping |= 0x80;\n } else {\n add_bits = 0;\n gfc.substep_shaping &= 0x7f;\n /*\n * build up reservoir. this builds the reservoir a little slower\n * than FhG. It could simple be mean_bits/15, but this was rigged to\n * always produce 100 (the old value) at 128kbs\n */\n if (!gfp.disable_reservoir && 0 == (gfc.substep_shaping & 1))\n targ_bits.bits -= .1 * mean_bits;\n }\n\n /* amount from the reservoir we are allowed to use. ISO says 6/10 */\n var extra_bits = (ResvSize < (gfc.ResvMax * 6) / 10 ? ResvSize\n : (gfc.ResvMax * 6) / 10);\n extra_bits -= add_bits;\n\n if (extra_bits < 0)\n extra_bits = 0;\n return extra_bits;\n }\n\n /**\n * Called after a granule's bit allocation. Readjusts the size of the\n * reservoir to reflect the granule's usage.\n */\n this.ResvAdjust = function(gfc, gi) {\n gfc.ResvSize -= gi.part2_3_length + gi.part2_length;\n }\n\n /**\n * Called after all granules in a frame have been allocated. Makes sure that\n * the reservoir size is within limits, possibly by adding stuffing bits.\n */\n this.ResvFrameEnd = function(gfc, mean_bits) {\n var over_bits;\n var l3_side = gfc.l3_side;\n\n gfc.ResvSize += mean_bits * gfc.mode_gr;\n var stuffingBits = 0;\n l3_side.resvDrain_post = 0;\n l3_side.resvDrain_pre = 0;\n\n /* we must be byte aligned */\n if ((over_bits = gfc.ResvSize % 8) != 0)\n stuffingBits += over_bits;\n\n over_bits = (gfc.ResvSize - stuffingBits) - gfc.ResvMax;\n if (over_bits > 0) {\n stuffingBits += over_bits;\n }\n\n /*\n * NOTE: enabling the NEW_DRAIN code fixes some problems with FhG\n * decoder shipped with MS Windows operating systems. Using this, it is\n * even possible to use Gabriel's lax buffer consideration again, which\n * assumes, any decoder should have a buffer large enough for a 320 kbps\n * frame at 32 kHz sample rate.\n *\n * old drain code: lame -b320 BlackBird.wav --. does not play with\n * GraphEdit.exe using FhG decoder V1.5 Build 50\n *\n * new drain code: lame -b320 BlackBird.wav --. plays fine with\n * GraphEdit.exe using FhG decoder V1.5 Build 50\n *\n * Robert Hegemann, 2010-02-13.\n */\n /*\n * drain as many bits as possible into previous frame ancillary data In\n * particular, in VBR mode ResvMax may have changed, and we have to make\n * sure main_data_begin does not create a reservoir bigger than ResvMax\n * mt 4/00\n */\n {\n var mdb_bytes = Math.min(l3_side.main_data_begin * 8, stuffingBits) / 8;\n l3_side.resvDrain_pre += 8 * mdb_bytes;\n stuffingBits -= 8 * mdb_bytes;\n gfc.ResvSize -= 8 * mdb_bytes;\n l3_side.main_data_begin -= mdb_bytes;\n }\n /* drain the rest into this frames ancillary data */\n l3_side.resvDrain_post += stuffingBits;\n gfc.ResvSize -= stuffingBits;\n }\n }\n\n\n /**\n * A Vbr header may be present in the ancillary data field of the first frame of\n * an mp3 bitstream
\n * The Vbr header (optionally) contains\n * \n *
\n *\n * toc (table of contents) gives seek points for random access.
\n * The ith entry determines the seek point for i-percent duration.
\n * seek point in bytes = (toc[i]/256.0) * total_bitstream_bytes
\n * e.g. half duration seek point = (toc[50]/256.0) * total_bitstream_bytes\n */\n VBRTag.NUMTOCENTRIES = 100;\n VBRTag.MAXFRAMESIZE = 2880;\n\n function VBRTag() {\n\n var lame;\n var bs;\n var v;\n\n this.setModules = function (_lame, _bs, _v) {\n lame = _lame;\n bs = _bs;\n v = _v;\n };\n\n var FRAMES_FLAG = 0x0001;\n var BYTES_FLAG = 0x0002;\n var TOC_FLAG = 0x0004;\n var VBR_SCALE_FLAG = 0x0008;\n\n var NUMTOCENTRIES = VBRTag.NUMTOCENTRIES;\n\n /**\n * (0xB40) the max freeformat 640 32kHz framesize.\n */\n var MAXFRAMESIZE = VBRTag.MAXFRAMESIZE;\n\n /**\n * \n * 4 bytes for Header Tag\n * 4 bytes for Header Flags\n * 100 bytes for entry (toc)\n * 4 bytes for frame size\n * 4 bytes for stream size\n * 4 bytes for VBR scale. a VBR quality indicator: 0=best 100=worst\n * 20 bytes for LAME tag. for example, \"LAME3.12 (beta 6)\"\n * ___________\n * 140 bytes\n *
\n */\n var VBRHEADERSIZE = (NUMTOCENTRIES + 4 + 4 + 4 + 4 + 4);\n\n var LAMEHEADERSIZE = (VBRHEADERSIZE + 9 + 1 + 1 + 8\n + 1 + 1 + 3 + 1 + 1 + 2 + 4 + 2 + 2);\n\n /**\n * The size of the Xing header MPEG-1, bit rate in kbps.\n */\n var XING_BITRATE1 = 128;\n /**\n * The size of the Xing header MPEG-2, bit rate in kbps.\n */\n var XING_BITRATE2 = 64;\n /**\n * The size of the Xing header MPEG-2.5, bit rate in kbps.\n */\n var XING_BITRATE25 = 32;\n\n /**\n * ISO-8859-1 charset for byte to string operations.\n */\n var ISO_8859_1 = null; //Charset.forName(\"ISO-8859-1\");\n\n /**\n * VBR header magic string.\n */\n var VBRTag0 = \"Xing\";\n /**\n * VBR header magic string (VBR == VBRMode.vbr_off).\n */\n var VBRTag1 = \"Info\";\n\n /**\n * Lookup table for fast CRC-16 computation. Uses the polynomial\n * x^16+x^15+x^2+1\n */\n var crc16Lookup = [0x0000, 0xC0C1, 0xC181, 0x0140,\n 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741,\n 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0, 0x0D80, 0xCD41,\n 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40,\n 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941,\n 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40,\n 0xDD01, 0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1, 0xD581, 0x1540,\n 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341,\n 0x1100, 0xD1C1, 0xD081, 0x1040, 0xF001, 0x30C0, 0x3180, 0xF141,\n 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740,\n 0xF501, 0x35C0, 0x3480, 0xF441, 0x3C00, 0xFCC1, 0xFD81, 0x3D40,\n 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41,\n 0x3900, 0xF9C1, 0xF881, 0x3840, 0x2800, 0xE8C1, 0xE981, 0x2940,\n 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41,\n 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0, 0x2580, 0xE541,\n 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340,\n 0xE101, 0x21C0, 0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141,\n 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740,\n 0xA501, 0x65C0, 0x6480, 0xA441, 0x6C00, 0xACC1, 0xAD81, 0x6D40,\n 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41,\n 0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1, 0xB981, 0x7940,\n 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41,\n 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541,\n 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340,\n 0xB101, 0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1, 0x9181, 0x5140,\n 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741,\n 0x5500, 0x95C1, 0x9481, 0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41,\n 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40,\n 0x9901, 0x59C0, 0x5880, 0x9841, 0x8801, 0x48C0, 0x4980, 0x8941,\n 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40,\n 0x8D01, 0x4DC0, 0x4C80, 0x8C41, 0x4400, 0x84C1, 0x8581, 0x4540,\n 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341,\n 0x4100, 0x81C1, 0x8081, 0x4040];\n\n /***********************************************************************\n * Robert Hegemann 2001-01-17\n ***********************************************************************/\n\n function addVbr(v, bitrate) {\n v.nVbrNumFrames++;\n v.sum += bitrate;\n v.seen++;\n\n if (v.seen < v.want) {\n return;\n }\n\n if (v.pos < v.size) {\n v.bag[v.pos] = v.sum;\n v.pos++;\n v.seen = 0;\n }\n if (v.pos == v.size) {\n for (var i = 1; i < v.size; i += 2) {\n v.bag[i / 2] = v.bag[i];\n }\n v.want *= 2;\n v.pos /= 2;\n }\n }\n\n function xingSeekTable(v, t) {\n if (v.pos <= 0)\n return;\n\n for (var i = 1; i < NUMTOCENTRIES; ++i) {\n var j = i / NUMTOCENTRIES, act, sum;\n var indx = 0 | (Math.floor(j * v.pos));\n if (indx > v.pos - 1)\n indx = v.pos - 1;\n act = v.bag[indx];\n sum = v.sum;\n var seek_point = 0 | (256. * act / sum);\n if (seek_point > 255)\n seek_point = 255;\n t[i] = 0xff & seek_point;\n }\n }\n\n /**\n * Add VBR entry, used to fill the VBR TOC entries.\n *\n * @param gfp\n * global flags\n */\n this.addVbrFrame = function (gfp) {\n var gfc = gfp.internal_flags;\n var kbps = Tables.bitrate_table[gfp.version][gfc.bitrate_index];\n addVbr(gfc.VBR_seek_table, kbps);\n }\n\n /**\n * Read big endian integer (4-bytes) from header.\n *\n * @param buf\n * header containing the integer\n * @param bufPos\n * offset into the header\n * @return extracted integer\n */\n function extractInteger(buf, bufPos) {\n var x = buf[bufPos + 0] & 0xff;\n x <<= 8;\n x |= buf[bufPos + 1] & 0xff;\n x <<= 8;\n x |= buf[bufPos + 2] & 0xff;\n x <<= 8;\n x |= buf[bufPos + 3] & 0xff;\n return x;\n }\n\n /**\n * Write big endian integer (4-bytes) in the header.\n *\n * @param buf\n * header to write the integer into\n * @param bufPos\n * offset into the header\n * @param value\n * integer value to write\n */\n function createInteger(buf, bufPos, value) {\n buf[bufPos + 0] = 0xff & ((value >> 24) & 0xff);\n buf[bufPos + 1] = 0xff & ((value >> 16) & 0xff);\n buf[bufPos + 2] = 0xff & ((value >> 8) & 0xff);\n buf[bufPos + 3] = 0xff & (value & 0xff);\n }\n\n /**\n * Write big endian short (2-bytes) in the header.\n *\n * @param buf\n * header to write the integer into\n * @param bufPos\n * offset into the header\n * @param value\n * integer value to write\n */\n function createShort(buf, bufPos, value) {\n buf[bufPos + 0] = 0xff & ((value >> 8) & 0xff);\n buf[bufPos + 1] = 0xff & (value & 0xff);\n }\n\n /**\n * Check for magic strings (Xing/Info).\n *\n * @param buf\n * header to check\n * @param bufPos\n * header offset to check\n * @return magic string found\n */\n function isVbrTag(buf, bufPos) {\n return new String(buf, bufPos, VBRTag0.length(), ISO_8859_1)\n .equals(VBRTag0)\n || new String(buf, bufPos, VBRTag1.length(), ISO_8859_1)\n .equals(VBRTag1);\n }\n\n function shiftInBitsValue(x, n, v) {\n return 0xff & ((x << n) | (v & ~(-1 << n)));\n }\n\n /**\n * Construct the MP3 header using the settings of the global flags.\n *\n * \n *\n * @param gfp\n * global flags\n * @param buffer\n * header\n */\n function setLameTagFrameHeader(gfp, buffer) {\n var gfc = gfp.internal_flags;\n\n // MP3 Sync Word\n buffer[0] = shiftInBitsValue(buffer[0], 8, 0xff);\n\n buffer[1] = shiftInBitsValue(buffer[1], 3, 7);\n buffer[1] = shiftInBitsValue(buffer[1], 1,\n (gfp.out_samplerate < 16000) ? 0 : 1);\n // Version\n buffer[1] = shiftInBitsValue(buffer[1], 1, gfp.version);\n // 01 == Layer 3\n buffer[1] = shiftInBitsValue(buffer[1], 2, 4 - 3);\n // Error protection\n buffer[1] = shiftInBitsValue(buffer[1], 1, (!gfp.error_protection) ? 1\n : 0);\n\n // Bit rate\n buffer[2] = shiftInBitsValue(buffer[2], 4, gfc.bitrate_index);\n // Frequency\n buffer[2] = shiftInBitsValue(buffer[2], 2, gfc.samplerate_index);\n // Pad. Bit\n buffer[2] = shiftInBitsValue(buffer[2], 1, 0);\n // Priv. Bit\n buffer[2] = shiftInBitsValue(buffer[2], 1, gfp.extension);\n\n // Mode\n buffer[3] = shiftInBitsValue(buffer[3], 2, gfp.mode.ordinal());\n // Mode extension (Used with Joint Stereo)\n buffer[3] = shiftInBitsValue(buffer[3], 2, gfc.mode_ext);\n // Copy\n buffer[3] = shiftInBitsValue(buffer[3], 1, gfp.copyright);\n // Original\n buffer[3] = shiftInBitsValue(buffer[3], 1, gfp.original);\n // Emphasis\n buffer[3] = shiftInBitsValue(buffer[3], 2, gfp.emphasis);\n\n /* the default VBR header. 48 kbps layer III, no padding, no crc */\n /* but sampling freq, mode and copyright/copy protection taken */\n /* from first valid frame */\n buffer[0] = 0xff;\n var abyte = 0xff & (buffer[1] & 0xf1);\n var bitrate;\n if (1 == gfp.version) {\n bitrate = XING_BITRATE1;\n } else {\n if (gfp.out_samplerate < 16000)\n bitrate = XING_BITRATE25;\n else\n bitrate = XING_BITRATE2;\n }\n\n if (gfp.VBR == VbrMode.vbr_off)\n bitrate = gfp.brate;\n\n var bbyte;\n if (gfp.free_format)\n bbyte = 0x00;\n else\n bbyte = 0xff & (16 * lame.BitrateIndex(bitrate, gfp.version,\n gfp.out_samplerate));\n\n /*\n * Use as much of the info from the real frames in the Xing header:\n * samplerate, channels, crc, etc...\n */\n if (gfp.version == 1) {\n /* MPEG1 */\n buffer[1] = 0xff & (abyte | 0x0a);\n /* was 0x0b; */\n abyte = 0xff & (buffer[2] & 0x0d);\n /* AF keep also private bit */\n buffer[2] = 0xff & (bbyte | abyte);\n /* 64kbs MPEG1 frame */\n } else {\n /* MPEG2 */\n buffer[1] = 0xff & (abyte | 0x02);\n /* was 0x03; */\n abyte = 0xff & (buffer[2] & 0x0d);\n /* AF keep also private bit */\n buffer[2] = 0xff & (bbyte | abyte);\n /* 64kbs MPEG2 frame */\n }\n }\n\n /**\n * Get VBR tag information\n *\n * @param buf\n * header to analyze\n * @param bufPos\n * offset into the header\n * @return VBR tag data\n */\n this.getVbrTag = function (buf) {\n var pTagData = new VBRTagData();\n var bufPos = 0;\n\n /* get Vbr header data */\n pTagData.flags = 0;\n\n /* get selected MPEG header data */\n var hId = (buf[bufPos + 1] >> 3) & 1;\n var hSrIndex = (buf[bufPos + 2] >> 2) & 3;\n var hMode = (buf[bufPos + 3] >> 6) & 3;\n var hBitrate = ((buf[bufPos + 2] >> 4) & 0xf);\n hBitrate = Tables.bitrate_table[hId][hBitrate];\n\n /* check for FFE syncword */\n if ((buf[bufPos + 1] >> 4) == 0xE)\n pTagData.samprate = Tables.samplerate_table[2][hSrIndex];\n else\n pTagData.samprate = Tables.samplerate_table[hId][hSrIndex];\n\n /* determine offset of header */\n if (hId != 0) {\n /* mpeg1 */\n if (hMode != 3)\n bufPos += (32 + 4);\n else\n bufPos += (17 + 4);\n } else {\n /* mpeg2 */\n if (hMode != 3)\n bufPos += (17 + 4);\n else\n bufPos += (9 + 4);\n }\n\n if (!isVbrTag(buf, bufPos))\n return null;\n\n bufPos += 4;\n\n pTagData.hId = hId;\n\n /* get flags */\n var head_flags = pTagData.flags = extractInteger(buf, bufPos);\n bufPos += 4;\n\n if ((head_flags & FRAMES_FLAG) != 0) {\n pTagData.frames = extractInteger(buf, bufPos);\n bufPos += 4;\n }\n\n if ((head_flags & BYTES_FLAG) != 0) {\n pTagData.bytes = extractInteger(buf, bufPos);\n bufPos += 4;\n }\n\n if ((head_flags & TOC_FLAG) != 0) {\n if (pTagData.toc != null) {\n for (var i = 0; i < NUMTOCENTRIES; i++)\n pTagData.toc[i] = buf[bufPos + i];\n }\n bufPos += NUMTOCENTRIES;\n }\n\n pTagData.vbrScale = -1;\n\n if ((head_flags & VBR_SCALE_FLAG) != 0) {\n pTagData.vbrScale = extractInteger(buf, bufPos);\n bufPos += 4;\n }\n\n pTagData.headersize = ((hId + 1) * 72000 * hBitrate)\n / pTagData.samprate;\n\n bufPos += 21;\n var encDelay = buf[bufPos + 0] << 4;\n encDelay += buf[bufPos + 1] >> 4;\n var encPadding = (buf[bufPos + 1] & 0x0F) << 8;\n encPadding += buf[bufPos + 2] & 0xff;\n /* check for reasonable values (this may be an old Xing header, */\n /* not a INFO tag) */\n if (encDelay < 0 || encDelay > 3000)\n encDelay = -1;\n if (encPadding < 0 || encPadding > 3000)\n encPadding = -1;\n\n pTagData.encDelay = encDelay;\n pTagData.encPadding = encPadding;\n\n /* success */\n return pTagData;\n }\n\n /**\n * Initializes the header\n *\n * @param gfp\n * global flags\n */\n this.InitVbrTag = function (gfp) {\n var gfc = gfp.internal_flags;\n\n /**\n * \n * Xing VBR pretends to be a 48kbs layer III frame. (at 44.1kHz).\n * (at 48kHz they use 56kbs since 48kbs frame not big enough for\n * table of contents)\n * let's always embed Xing header inside a 64kbs layer III frame.\n * this gives us enough room for a LAME version string too.\n * size determined by sampling frequency (MPEG1)\n * 32kHz: 216 bytes@48kbs 288bytes@ 64kbs\n * 44.1kHz: 156 bytes 208bytes@64kbs (+1 if padding = 1)\n * 48kHz: 144 bytes 192\n *\n * MPEG 2 values are the same since the framesize and samplerate\n * are each reduced by a factor of 2.\n *
\n */\n var kbps_header;\n if (1 == gfp.version) {\n kbps_header = XING_BITRATE1;\n } else {\n if (gfp.out_samplerate < 16000)\n kbps_header = XING_BITRATE25;\n else\n kbps_header = XING_BITRATE2;\n }\n\n if (gfp.VBR == VbrMode.vbr_off)\n kbps_header = gfp.brate;\n\n // make sure LAME Header fits into Frame\n var totalFrameSize = ((gfp.version + 1) * 72000 * kbps_header)\n / gfp.out_samplerate;\n var headerSize = (gfc.sideinfo_len + LAMEHEADERSIZE);\n gfc.VBR_seek_table.TotalFrameSize = totalFrameSize;\n if (totalFrameSize < headerSize || totalFrameSize > MAXFRAMESIZE) {\n /* disable tag, it wont fit */\n gfp.bWriteVbrTag = false;\n return;\n }\n\n gfc.VBR_seek_table.nVbrNumFrames = 0;\n gfc.VBR_seek_table.nBytesWritten = 0;\n gfc.VBR_seek_table.sum = 0;\n\n gfc.VBR_seek_table.seen = 0;\n gfc.VBR_seek_table.want = 1;\n gfc.VBR_seek_table.pos = 0;\n\n if (gfc.VBR_seek_table.bag == null) {\n gfc.VBR_seek_table.bag = new int[400];\n gfc.VBR_seek_table.size = 400;\n }\n\n // write dummy VBR tag of all 0's into bitstream\n var buffer = new_byte(MAXFRAMESIZE);\n\n setLameTagFrameHeader(gfp, buffer);\n var n = gfc.VBR_seek_table.TotalFrameSize;\n for (var i = 0; i < n; ++i) {\n bs.add_dummy_byte(gfp, buffer[i] & 0xff, 1);\n }\n }\n\n /**\n * Fast CRC-16 computation (uses table crc16Lookup).\n *\n * @param value\n * @param crc\n * @return\n */\n function crcUpdateLookup(value, crc) {\n var tmp = crc ^ value;\n crc = (crc >> 8) ^ crc16Lookup[tmp & 0xff];\n return crc;\n }\n\n this.updateMusicCRC = function (crc, buffer, bufferPos, size) {\n for (var i = 0; i < size; ++i)\n crc[0] = crcUpdateLookup(buffer[bufferPos + i], crc[0]);\n }\n\n /**\n * Write LAME info: mini version + info on various switches used (Jonathan\n * Dee 2001/08/31).\n *\n * @param gfp\n * global flags\n * @param musicLength\n * music length\n * @param streamBuffer\n * pointer to output buffer\n * @param streamBufferPos\n * offset into the output buffer\n * @param crc\n * computation of CRC-16 of Lame Tag so far (starting at frame\n * sync)\n * @return number of bytes written to the stream\n */\n function putLameVBR(gfp, musicLength, streamBuffer, streamBufferPos, crc) {\n var gfc = gfp.internal_flags;\n var bytesWritten = 0;\n\n /* encoder delay */\n var encDelay = gfp.encoder_delay;\n /* encoder padding */\n var encPadding = gfp.encoder_padding;\n\n /* recall: gfp.VBR_q is for example set by the switch -V */\n /* gfp.quality by -q, -h, -f, etc */\n var quality = (100 - 10 * gfp.VBR_q - gfp.quality);\n\n var version = v.getLameVeryShortVersion();\n var vbr;\n var revision = 0x00;\n var revMethod;\n // numbering different in vbr_mode vs. Lame tag\n var vbrTypeTranslator = [1, 5, 3, 2, 4, 0, 3];\n var lowpass = 0 | (((gfp.lowpassfreq / 100.0) + .5) > 255 ? 255\n : (gfp.lowpassfreq / 100.0) + .5);\n var peakSignalAmplitude = 0;\n var radioReplayGain = 0;\n var audiophileReplayGain = 0;\n var noiseShaping = gfp.internal_flags.noise_shaping;\n var stereoMode = 0;\n var nonOptimal = 0;\n var sourceFreq = 0;\n var misc = 0;\n var musicCRC = 0;\n\n // psy model type: Gpsycho or NsPsytune\n var expNPsyTune = (gfp.exp_nspsytune & 1) != 0;\n var safeJoint = (gfp.exp_nspsytune & 2) != 0;\n var noGapMore = false;\n var noGapPrevious = false;\n var noGapCount = gfp.internal_flags.nogap_total;\n var noGapCurr = gfp.internal_flags.nogap_current;\n\n // 4 bits\n var athType = gfp.ATHtype;\n var flags = 0;\n\n // vbr modes\n var abrBitrate;\n switch (gfp.VBR) {\n case vbr_abr:\n abrBitrate = gfp.VBR_mean_bitrate_kbps;\n break;\n case vbr_off:\n abrBitrate = gfp.brate;\n break;\n default:\n abrBitrate = gfp.VBR_min_bitrate_kbps;\n }\n\n // revision and vbr method\n if (gfp.VBR.ordinal() < vbrTypeTranslator.length)\n vbr = vbrTypeTranslator[gfp.VBR.ordinal()];\n else\n vbr = 0x00; // unknown\n\n revMethod = 0x10 * revision + vbr;\n\n // ReplayGain\n if (gfc.findReplayGain) {\n if (gfc.RadioGain > 0x1FE)\n gfc.RadioGain = 0x1FE;\n if (gfc.RadioGain < -0x1FE)\n gfc.RadioGain = -0x1FE;\n\n // set name code\n radioReplayGain = 0x2000;\n // set originator code to `determined automatically'\n radioReplayGain |= 0xC00;\n\n if (gfc.RadioGain >= 0) {\n // set gain adjustment\n radioReplayGain |= gfc.RadioGain;\n } else {\n // set the sign bit\n radioReplayGain |= 0x200;\n // set gain adjustment\n radioReplayGain |= -gfc.RadioGain;\n }\n }\n\n // peak sample\n if (gfc.findPeakSample)\n peakSignalAmplitude = Math\n .abs(0 | ((( gfc.PeakSample) / 32767.0) * Math.pow(2, 23) + .5));\n\n // nogap\n if (noGapCount != -1) {\n if (noGapCurr > 0)\n noGapPrevious = true;\n\n if (noGapCurr < noGapCount - 1)\n noGapMore = true;\n }\n\n // flags\n flags = athType + ((expNPsyTune ? 1 : 0) << 4)\n + ((safeJoint ? 1 : 0) << 5) + ((noGapMore ? 1 : 0) << 6)\n + ((noGapPrevious ? 1 : 0) << 7);\n\n if (quality < 0)\n quality = 0;\n\n // stereo mode field (Intensity stereo is not implemented)\n switch (gfp.mode) {\n case MONO:\n stereoMode = 0;\n break;\n case STEREO:\n stereoMode = 1;\n break;\n case DUAL_CHANNEL:\n stereoMode = 2;\n break;\n case JOINT_STEREO:\n if (gfp.force_ms)\n stereoMode = 4;\n else\n stereoMode = 3;\n break;\n case NOT_SET:\n //$FALL-THROUGH$\n default:\n stereoMode = 7;\n break;\n }\n\n if (gfp.in_samplerate <= 32000)\n sourceFreq = 0x00;\n else if (gfp.in_samplerate == 48000)\n sourceFreq = 0x02;\n else if (gfp.in_samplerate > 48000)\n sourceFreq = 0x03;\n else {\n // default is 44100Hz\n sourceFreq = 0x01;\n }\n\n // Check if the user overrided the default LAME behavior with some\n // nasty options\n if (gfp.short_blocks == ShortBlock.short_block_forced\n || gfp.short_blocks == ShortBlock.short_block_dispensed\n || ((gfp.lowpassfreq == -1) && (gfp.highpassfreq == -1)) || /* \"-k\" */\n (gfp.scale_left < gfp.scale_right)\n || (gfp.scale_left > gfp.scale_right)\n || (gfp.disable_reservoir && gfp.brate < 320) || gfp.noATH\n || gfp.ATHonly || (athType == 0) || gfp.in_samplerate <= 32000)\n nonOptimal = 1;\n\n misc = noiseShaping + (stereoMode << 2) + (nonOptimal << 5)\n + (sourceFreq << 6);\n\n musicCRC = gfc.nMusicCRC;\n\n // Write all this information into the stream\n\n createInteger(streamBuffer, streamBufferPos + bytesWritten, quality);\n bytesWritten += 4;\n\n for (var j = 0; j < 9; j++) {\n streamBuffer[streamBufferPos + bytesWritten + j] = 0xff & version .charAt(j);\n }\n bytesWritten += 9;\n\n streamBuffer[streamBufferPos + bytesWritten] = 0xff & revMethod;\n bytesWritten++;\n\n streamBuffer[streamBufferPos + bytesWritten] = 0xff & lowpass;\n bytesWritten++;\n\n createInteger(streamBuffer, streamBufferPos + bytesWritten,\n peakSignalAmplitude);\n bytesWritten += 4;\n\n createShort(streamBuffer, streamBufferPos + bytesWritten,\n radioReplayGain);\n bytesWritten += 2;\n\n createShort(streamBuffer, streamBufferPos + bytesWritten,\n audiophileReplayGain);\n bytesWritten += 2;\n\n streamBuffer[streamBufferPos + bytesWritten] = 0xff & flags;\n bytesWritten++;\n\n if (abrBitrate >= 255)\n streamBuffer[streamBufferPos + bytesWritten] = 0xFF;\n else\n streamBuffer[streamBufferPos + bytesWritten] = 0xff & abrBitrate;\n bytesWritten++;\n\n streamBuffer[streamBufferPos + bytesWritten] = 0xff & (encDelay >> 4);\n streamBuffer[streamBufferPos + bytesWritten + 1] = 0xff & ((encDelay << 4) + (encPadding >> 8));\n streamBuffer[streamBufferPos + bytesWritten + 2] = 0xff & encPadding;\n\n bytesWritten += 3;\n\n streamBuffer[streamBufferPos + bytesWritten] = 0xff & misc;\n bytesWritten++;\n\n // unused in rev0\n streamBuffer[streamBufferPos + bytesWritten++] = 0;\n\n createShort(streamBuffer, streamBufferPos + bytesWritten, gfp.preset);\n bytesWritten += 2;\n\n createInteger(streamBuffer, streamBufferPos + bytesWritten, musicLength);\n bytesWritten += 4;\n\n createShort(streamBuffer, streamBufferPos + bytesWritten, musicCRC);\n bytesWritten += 2;\n\n // Calculate tag CRC.... must be done here, since it includes previous\n // information\n\n for (var i = 0; i < bytesWritten; i++)\n crc = crcUpdateLookup(streamBuffer[streamBufferPos + i], crc);\n\n createShort(streamBuffer, streamBufferPos + bytesWritten, crc);\n bytesWritten += 2;\n\n return bytesWritten;\n }\n\n function skipId3v2(fpStream) {\n // seek to the beginning of the stream\n fpStream.seek(0);\n // read 10 bytes in case there's an ID3 version 2 header here\n var id3v2Header = new_byte(10);\n fpStream.readFully(id3v2Header);\n /* does the stream begin with the ID3 version 2 file identifier? */\n var id3v2TagSize;\n if (!new String(id3v2Header, \"ISO-8859-1\").startsWith(\"ID3\")) {\n /*\n * the tag size (minus the 10-byte header) is encoded into four\n * bytes where the most significant bit is clear in each byte\n */\n id3v2TagSize = (((id3v2Header[6] & 0x7f) << 21)\n | ((id3v2Header[7] & 0x7f) << 14)\n | ((id3v2Header[8] & 0x7f) << 7) | (id3v2Header[9] & 0x7f))\n + id3v2Header.length;\n } else {\n /* no ID3 version 2 tag in this stream */\n id3v2TagSize = 0;\n }\n return id3v2TagSize;\n }\n\n this.getLameTagFrame = function (gfp, buffer) {\n var gfc = gfp.internal_flags;\n\n if (!gfp.bWriteVbrTag) {\n return 0;\n }\n if (gfc.Class_ID != Lame.LAME_ID) {\n return 0;\n }\n if (gfc.VBR_seek_table.pos <= 0) {\n return 0;\n }\n if (buffer.length < gfc.VBR_seek_table.TotalFrameSize) {\n return gfc.VBR_seek_table.TotalFrameSize;\n }\n\n Arrays.fill(buffer, 0, gfc.VBR_seek_table.TotalFrameSize, 0);\n\n // 4 bytes frame header\n setLameTagFrameHeader(gfp, buffer);\n\n // Create TOC entries\n var toc = new_byte(NUMTOCENTRIES);\n\n if (gfp.free_format) {\n for (var i = 1; i < NUMTOCENTRIES; ++i)\n toc[i] = 0xff & (255 * i / 100);\n } else {\n xingSeekTable(gfc.VBR_seek_table, toc);\n }\n\n // Start writing the tag after the zero frame\n var streamIndex = gfc.sideinfo_len;\n /**\n * Note: Xing header specifies that Xing data goes in the ancillary data\n * with NO ERROR PROTECTION. If error protecton in enabled, the Xing\n * data still starts at the same offset, and now it is in sideinfo data\n * block, and thus will not decode correctly by non-Xing tag aware\n * players\n */\n if (gfp.error_protection)\n streamIndex -= 2;\n\n // Put Vbr tag\n if (gfp.VBR == VbrMode.vbr_off) {\n buffer[streamIndex++] = 0xff & VBRTag1.charAt(0);\n buffer[streamIndex++] = 0xff & VBRTag1.charAt(1);\n buffer[streamIndex++] = 0xff & VBRTag1.charAt(2);\n buffer[streamIndex++] = 0xff & VBRTag1.charAt(3);\n\n } else {\n buffer[streamIndex++] = 0xff & VBRTag0.charAt(0);\n buffer[streamIndex++] = 0xff & VBRTag0.charAt(1);\n buffer[streamIndex++] = 0xff & VBRTag0.charAt(2);\n buffer[streamIndex++] = 0xff & VBRTag0.charAt(3);\n }\n\n // Put header flags\n createInteger(buffer, streamIndex, FRAMES_FLAG + BYTES_FLAG + TOC_FLAG\n + VBR_SCALE_FLAG);\n streamIndex += 4;\n\n // Put Total Number of frames\n createInteger(buffer, streamIndex, gfc.VBR_seek_table.nVbrNumFrames);\n streamIndex += 4;\n\n // Put total audio stream size, including Xing/LAME Header\n var streamSize = (gfc.VBR_seek_table.nBytesWritten + gfc.VBR_seek_table.TotalFrameSize);\n createInteger(buffer, streamIndex, 0 | streamSize);\n streamIndex += 4;\n\n /* Put TOC */\n System.arraycopy(toc, 0, buffer, streamIndex, toc.length);\n streamIndex += toc.length;\n\n if (gfp.error_protection) {\n // (jo) error_protection: add crc16 information to header\n bs.CRC_writeheader(gfc, buffer);\n }\n\n // work out CRC so far: initially crc = 0\n var crc = 0x00;\n for (var i = 0; i < streamIndex; i++)\n crc = crcUpdateLookup(buffer[i], crc);\n // Put LAME VBR info\n streamIndex += putLameVBR(gfp, streamSize, buffer, streamIndex, crc);\n\n return gfc.VBR_seek_table.TotalFrameSize;\n }\n\n /**\n * Write final VBR tag to the file.\n *\n * @param gfp\n * global flags\n * @param stream\n * stream to add the VBR tag to\n * @return 0 (OK), -1 else\n * @throws IOException\n * I/O error\n */\n this.putVbrTag = function (gfp, stream) {\n var gfc = gfp.internal_flags;\n\n if (gfc.VBR_seek_table.pos <= 0)\n return -1;\n\n // Seek to end of file\n stream.seek(stream.length());\n\n // Get file size, abort if file has zero length.\n if (stream.length() == 0)\n return -1;\n\n // The VBR tag may NOT be located at the beginning of the stream. If an\n // ID3 version 2 tag was added, then it must be skipped to write the VBR\n // tag data.\n var id3v2TagSize = skipId3v2(stream);\n\n // Seek to the beginning of the stream\n stream.seek(id3v2TagSize);\n\n var buffer = new_byte(MAXFRAMESIZE);\n var bytes = getLameTagFrame(gfp, buffer);\n if (bytes > buffer.length) {\n return -1;\n }\n\n if (bytes < 1) {\n return 0;\n }\n\n // Put it all to disk again\n stream.write(buffer, 0, bytes);\n // success\n return 0;\n }\n\n }\n\n\n\n BitStream.EQ = function (a, b) {\n return (Math.abs(a) > Math.abs(b)) ? (Math.abs((a) - (b)) <= (Math\n .abs(a) * 1e-6))\n : (Math.abs((a) - (b)) <= (Math.abs(b) * 1e-6));\n };\n\n BitStream.NEQ = function (a, b) {\n return !BitStream.EQ(a, b);\n };\n\n function BitStream() {\n var self = this;\n var CRC16_POLYNOMIAL = 0x8005;\n\n /*\n * we work with ints, so when doing bit manipulation, we limit ourselves to\n * MAX_LENGTH-2 just to be on the safe side\n */\n var MAX_LENGTH = 32;\n\n //GainAnalysis ga;\n //MPGLib mpg;\n //Version ver;\n //VBRTag vbr;\n var ga = null;\n var mpg = null;\n var ver = null;\n var vbr = null;\n\n //public final void setModules(GainAnalysis ga, MPGLib mpg, Version ver,\n //\tVBRTag vbr) {\n\n this.setModules = function (_ga, _mpg, _ver, _vbr) {\n ga = _ga;\n mpg = _mpg;\n ver = _ver;\n vbr = _vbr;\n };\n\n /**\n * Bit stream buffer.\n */\n //private byte[] buf;\n var buf = null;\n /**\n * Bit counter of bit stream.\n */\n var totbit = 0;\n /**\n * Pointer to top byte in buffer.\n */\n var bufByteIdx = 0;\n /**\n * Pointer to top bit of top byte in buffer.\n */\n var bufBitIdx = 0;\n\n /**\n * compute bitsperframe and mean_bits for a layer III frame\n */\n this.getframebits = function (gfp) {\n var gfc = gfp.internal_flags;\n var bit_rate;\n\n /* get bitrate in kbps [?] */\n if (gfc.bitrate_index != 0)\n bit_rate = Tables.bitrate_table[gfp.version][gfc.bitrate_index];\n else\n bit_rate = gfp.brate;\n\n /* main encoding routine toggles padding on and off */\n /* one Layer3 Slot consists of 8 bits */\n var bytes = 0 | (gfp.version + 1) * 72000 * bit_rate / gfp.out_samplerate + gfc.padding;\n return 8 * bytes;\n };\n\n function putheader_bits(gfc) {\n System.arraycopy(gfc.header[gfc.w_ptr].buf, 0, buf, bufByteIdx, gfc.sideinfo_len);\n bufByteIdx += gfc.sideinfo_len;\n totbit += gfc.sideinfo_len * 8;\n gfc.w_ptr = (gfc.w_ptr + 1) & (LameInternalFlags.MAX_HEADER_BUF - 1);\n }\n\n /**\n * write j bits into the bit stream\n */\n function putbits2(gfc, val, j) {\n\n while (j > 0) {\n var k;\n if (bufBitIdx == 0) {\n bufBitIdx = 8;\n bufByteIdx++;\n if (gfc.header[gfc.w_ptr].write_timing == totbit) {\n putheader_bits(gfc);\n }\n buf[bufByteIdx] = 0;\n }\n\n k = Math.min(j, bufBitIdx);\n j -= k;\n\n bufBitIdx -= k;\n\n /* 32 too large on 32 bit machines */\n\n buf[bufByteIdx] |= ((val >> j) << bufBitIdx);\n totbit += k;\n }\n }\n\n /**\n * write j bits into the bit stream, ignoring frame headers\n */\n function putbits_noheaders(gfc, val, j) {\n\n while (j > 0) {\n var k;\n if (bufBitIdx == 0) {\n bufBitIdx = 8;\n bufByteIdx++;\n buf[bufByteIdx] = 0;\n }\n\n k = Math.min(j, bufBitIdx);\n j -= k;\n\n bufBitIdx -= k;\n\n /* 32 too large on 32 bit machines */\n\n buf[bufByteIdx] |= ((val >> j) << bufBitIdx);\n totbit += k;\n }\n }\n\n /**\n * Some combinations of bitrate, Fs, and stereo make it impossible to stuff\n * out a frame using just main_data, due to the limited number of bits to\n * indicate main_data_length. In these situations, we put stuffing bits into\n * the ancillary data...\n */\n function drain_into_ancillary(gfp, remainingBits) {\n var gfc = gfp.internal_flags;\n var i;\n\n if (remainingBits >= 8) {\n putbits2(gfc, 0x4c, 8);\n remainingBits -= 8;\n }\n if (remainingBits >= 8) {\n putbits2(gfc, 0x41, 8);\n remainingBits -= 8;\n }\n if (remainingBits >= 8) {\n putbits2(gfc, 0x4d, 8);\n remainingBits -= 8;\n }\n if (remainingBits >= 8) {\n putbits2(gfc, 0x45, 8);\n remainingBits -= 8;\n }\n\n if (remainingBits >= 32) {\n var version = ver.getLameShortVersion();\n if (remainingBits >= 32)\n for (i = 0; i < version.length && remainingBits >= 8; ++i) {\n remainingBits -= 8;\n putbits2(gfc, version.charAt(i), 8);\n }\n }\n\n for (; remainingBits >= 1; remainingBits -= 1) {\n putbits2(gfc, gfc.ancillary_flag, 1);\n gfc.ancillary_flag ^= (!gfp.disable_reservoir ? 1 : 0);\n }\n\n\n }\n\n /**\n * write N bits into the header\n */\n function writeheader(gfc, val, j) {\n var ptr = gfc.header[gfc.h_ptr].ptr;\n\n while (j > 0) {\n var k = Math.min(j, 8 - (ptr & 7));\n j -= k;\n /* >> 32 too large for 32 bit machines */\n\n gfc.header[gfc.h_ptr].buf[ptr >> 3] |= ((val >> j)) << (8 - (ptr & 7) - k);\n ptr += k;\n }\n gfc.header[gfc.h_ptr].ptr = ptr;\n }\n\n function CRC_update(value, crc) {\n value <<= 8;\n for (var i = 0; i < 8; i++) {\n value <<= 1;\n crc <<= 1;\n\n if ((((crc ^ value) & 0x10000) != 0))\n crc ^= CRC16_POLYNOMIAL;\n }\n return crc;\n }\n\n this.CRC_writeheader = function (gfc, header) {\n var crc = 0xffff;\n /* (jo) init crc16 for error_protection */\n\n crc = CRC_update(header[2] & 0xff, crc);\n crc = CRC_update(header[3] & 0xff, crc);\n for (var i = 6; i < gfc.sideinfo_len; i++) {\n crc = CRC_update(header[i] & 0xff, crc);\n }\n\n header[4] = (byte)(crc >> 8);\n header[5] = (byte)(crc & 255);\n };\n\n function encodeSideInfo2(gfp, bitsPerFrame) {\n var gfc = gfp.internal_flags;\n var l3_side;\n var gr, ch;\n\n l3_side = gfc.l3_side;\n gfc.header[gfc.h_ptr].ptr = 0;\n Arrays.fill(gfc.header[gfc.h_ptr].buf, 0, gfc.sideinfo_len, 0);\n if (gfp.out_samplerate < 16000)\n writeheader(gfc, 0xffe, 12);\n else\n writeheader(gfc, 0xfff, 12);\n writeheader(gfc, (gfp.version), 1);\n writeheader(gfc, 4 - 3, 2);\n writeheader(gfc, (!gfp.error_protection ? 1 : 0), 1);\n writeheader(gfc, (gfc.bitrate_index), 4);\n writeheader(gfc, (gfc.samplerate_index), 2);\n writeheader(gfc, (gfc.padding), 1);\n writeheader(gfc, (gfp.extension), 1);\n writeheader(gfc, (gfp.mode.ordinal()), 2);\n writeheader(gfc, (gfc.mode_ext), 2);\n writeheader(gfc, (gfp.copyright), 1);\n writeheader(gfc, (gfp.original), 1);\n writeheader(gfc, (gfp.emphasis), 2);\n if (gfp.error_protection) {\n writeheader(gfc, 0, 16);\n /* dummy */\n }\n\n if (gfp.version == 1) {\n /* MPEG1 */\n writeheader(gfc, (l3_side.main_data_begin), 9);\n\n if (gfc.channels_out == 2)\n writeheader(gfc, l3_side.private_bits, 3);\n else\n writeheader(gfc, l3_side.private_bits, 5);\n\n for (ch = 0; ch < gfc.channels_out; ch++) {\n var band;\n for (band = 0; band < 4; band++) {\n writeheader(gfc, l3_side.scfsi[ch][band], 1);\n }\n }\n\n for (gr = 0; gr < 2; gr++) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n var gi = l3_side.tt[gr][ch];\n writeheader(gfc, gi.part2_3_length + gi.part2_length, 12);\n writeheader(gfc, gi.big_values / 2, 9);\n writeheader(gfc, gi.global_gain, 8);\n writeheader(gfc, gi.scalefac_compress, 4);\n\n if (gi.block_type != Encoder.NORM_TYPE) {\n writeheader(gfc, 1, 1);\n /* window_switching_flag */\n writeheader(gfc, gi.block_type, 2);\n writeheader(gfc, gi.mixed_block_flag, 1);\n\n if (gi.table_select[0] == 14)\n gi.table_select[0] = 16;\n writeheader(gfc, gi.table_select[0], 5);\n if (gi.table_select[1] == 14)\n gi.table_select[1] = 16;\n writeheader(gfc, gi.table_select[1], 5);\n\n writeheader(gfc, gi.subblock_gain[0], 3);\n writeheader(gfc, gi.subblock_gain[1], 3);\n writeheader(gfc, gi.subblock_gain[2], 3);\n } else {\n writeheader(gfc, 0, 1);\n /* window_switching_flag */\n if (gi.table_select[0] == 14)\n gi.table_select[0] = 16;\n writeheader(gfc, gi.table_select[0], 5);\n if (gi.table_select[1] == 14)\n gi.table_select[1] = 16;\n writeheader(gfc, gi.table_select[1], 5);\n if (gi.table_select[2] == 14)\n gi.table_select[2] = 16;\n writeheader(gfc, gi.table_select[2], 5);\n\n writeheader(gfc, gi.region0_count, 4);\n writeheader(gfc, gi.region1_count, 3);\n }\n writeheader(gfc, gi.preflag, 1);\n writeheader(gfc, gi.scalefac_scale, 1);\n writeheader(gfc, gi.count1table_select, 1);\n }\n }\n } else {\n /* MPEG2 */\n writeheader(gfc, (l3_side.main_data_begin), 8);\n writeheader(gfc, l3_side.private_bits, gfc.channels_out);\n\n gr = 0;\n for (ch = 0; ch < gfc.channels_out; ch++) {\n var gi = l3_side.tt[gr][ch];\n writeheader(gfc, gi.part2_3_length + gi.part2_length, 12);\n writeheader(gfc, gi.big_values / 2, 9);\n writeheader(gfc, gi.global_gain, 8);\n writeheader(gfc, gi.scalefac_compress, 9);\n\n if (gi.block_type != Encoder.NORM_TYPE) {\n writeheader(gfc, 1, 1);\n /* window_switching_flag */\n writeheader(gfc, gi.block_type, 2);\n writeheader(gfc, gi.mixed_block_flag, 1);\n\n if (gi.table_select[0] == 14)\n gi.table_select[0] = 16;\n writeheader(gfc, gi.table_select[0], 5);\n if (gi.table_select[1] == 14)\n gi.table_select[1] = 16;\n writeheader(gfc, gi.table_select[1], 5);\n\n writeheader(gfc, gi.subblock_gain[0], 3);\n writeheader(gfc, gi.subblock_gain[1], 3);\n writeheader(gfc, gi.subblock_gain[2], 3);\n } else {\n writeheader(gfc, 0, 1);\n /* window_switching_flag */\n if (gi.table_select[0] == 14)\n gi.table_select[0] = 16;\n writeheader(gfc, gi.table_select[0], 5);\n if (gi.table_select[1] == 14)\n gi.table_select[1] = 16;\n writeheader(gfc, gi.table_select[1], 5);\n if (gi.table_select[2] == 14)\n gi.table_select[2] = 16;\n writeheader(gfc, gi.table_select[2], 5);\n\n writeheader(gfc, gi.region0_count, 4);\n writeheader(gfc, gi.region1_count, 3);\n }\n\n writeheader(gfc, gi.scalefac_scale, 1);\n writeheader(gfc, gi.count1table_select, 1);\n }\n }\n\n if (gfp.error_protection) {\n /* (jo) error_protection: add crc16 information to header */\n CRC_writeheader(gfc, gfc.header[gfc.h_ptr].buf);\n }\n\n {\n var old = gfc.h_ptr;\n\n gfc.h_ptr = (old + 1) & (LameInternalFlags.MAX_HEADER_BUF - 1);\n gfc.header[gfc.h_ptr].write_timing = gfc.header[old].write_timing\n + bitsPerFrame;\n\n if (gfc.h_ptr == gfc.w_ptr) {\n /* yikes! we are out of header buffer space */\n System.err\n .println(\"Error: MAX_HEADER_BUF too small in bitstream.c \\n\");\n }\n\n }\n }\n\n function huffman_coder_count1(gfc, gi) {\n /* Write count1 area */\n var h = Tables.ht[gi.count1table_select + 32];\n var i, bits = 0;\n\n var ix = gi.big_values;\n var xr = gi.big_values;\n\n for (i = (gi.count1 - gi.big_values) / 4; i > 0; --i) {\n var huffbits = 0;\n var p = 0, v;\n\n v = gi.l3_enc[ix + 0];\n if (v != 0) {\n p += 8;\n if (gi.xr[xr + 0] < 0)\n huffbits++;\n }\n\n v = gi.l3_enc[ix + 1];\n if (v != 0) {\n p += 4;\n huffbits *= 2;\n if (gi.xr[xr + 1] < 0)\n huffbits++;\n }\n\n v = gi.l3_enc[ix + 2];\n if (v != 0) {\n p += 2;\n huffbits *= 2;\n if (gi.xr[xr + 2] < 0)\n huffbits++;\n }\n\n v = gi.l3_enc[ix + 3];\n if (v != 0) {\n p++;\n huffbits *= 2;\n if (gi.xr[xr + 3] < 0)\n huffbits++;\n }\n\n ix += 4;\n xr += 4;\n putbits2(gfc, huffbits + h.table[p], h.hlen[p]);\n bits += h.hlen[p];\n }\n return bits;\n }\n\n /**\n * Implements the pseudocode of page 98 of the IS\n */\n function Huffmancode(gfc, tableindex, start, end, gi) {\n var h = Tables.ht[tableindex];\n var bits = 0;\n\n if (0 == tableindex)\n return bits;\n\n for (var i = start; i < end; i += 2) {\n var cbits = 0;\n var xbits = 0;\n var linbits = h.xlen;\n var xlen = h.xlen;\n var ext = 0;\n var x1 = gi.l3_enc[i];\n var x2 = gi.l3_enc[i + 1];\n\n if (x1 != 0) {\n if (gi.xr[i] < 0)\n ext++;\n cbits--;\n }\n\n if (tableindex > 15) {\n /* use ESC-words */\n if (x1 > 14) {\n var linbits_x1 = x1 - 15;\n ext |= linbits_x1 << 1;\n xbits = linbits;\n x1 = 15;\n }\n\n if (x2 > 14) {\n var linbits_x2 = x2 - 15;\n ext <<= linbits;\n ext |= linbits_x2;\n xbits += linbits;\n x2 = 15;\n }\n xlen = 16;\n }\n\n if (x2 != 0) {\n ext <<= 1;\n if (gi.xr[i + 1] < 0)\n ext++;\n cbits--;\n }\n\n\n x1 = x1 * xlen + x2;\n xbits -= cbits;\n cbits += h.hlen[x1];\n\n\n putbits2(gfc, h.table[x1], cbits);\n putbits2(gfc, ext, xbits);\n bits += cbits + xbits;\n }\n return bits;\n }\n\n /**\n * Note the discussion of huffmancodebits() on pages 28 and 29 of the IS, as\n * well as the definitions of the side information on pages 26 and 27.\n */\n function ShortHuffmancodebits(gfc, gi) {\n var region1Start = 3 * gfc.scalefac_band.s[3];\n if (region1Start > gi.big_values)\n region1Start = gi.big_values;\n\n /* short blocks do not have a region2 */\n var bits = Huffmancode(gfc, gi.table_select[0], 0, region1Start, gi);\n bits += Huffmancode(gfc, gi.table_select[1], region1Start,\n gi.big_values, gi);\n return bits;\n }\n\n function LongHuffmancodebits(gfc, gi) {\n var bigvalues, bits;\n var region1Start, region2Start;\n\n bigvalues = gi.big_values;\n\n var i = gi.region0_count + 1;\n region1Start = gfc.scalefac_band.l[i];\n i += gi.region1_count + 1;\n region2Start = gfc.scalefac_band.l[i];\n\n if (region1Start > bigvalues)\n region1Start = bigvalues;\n\n if (region2Start > bigvalues)\n region2Start = bigvalues;\n\n bits = Huffmancode(gfc, gi.table_select[0], 0, region1Start, gi);\n bits += Huffmancode(gfc, gi.table_select[1], region1Start,\n region2Start, gi);\n bits += Huffmancode(gfc, gi.table_select[2], region2Start, bigvalues,\n gi);\n return bits;\n }\n\n function writeMainData(gfp) {\n var gr, ch, sfb, data_bits, tot_bits = 0;\n var gfc = gfp.internal_flags;\n var l3_side = gfc.l3_side;\n\n if (gfp.version == 1) {\n /* MPEG 1 */\n for (gr = 0; gr < 2; gr++) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n var gi = l3_side.tt[gr][ch];\n var slen1 = Takehiro.slen1_tab[gi.scalefac_compress];\n var slen2 = Takehiro.slen2_tab[gi.scalefac_compress];\n data_bits = 0;\n for (sfb = 0; sfb < gi.sfbdivide; sfb++) {\n if (gi.scalefac[sfb] == -1)\n continue;\n /* scfsi is used */\n putbits2(gfc, gi.scalefac[sfb], slen1);\n data_bits += slen1;\n }\n for (; sfb < gi.sfbmax; sfb++) {\n if (gi.scalefac[sfb] == -1)\n continue;\n /* scfsi is used */\n putbits2(gfc, gi.scalefac[sfb], slen2);\n data_bits += slen2;\n }\n\n if (gi.block_type == Encoder.SHORT_TYPE) {\n data_bits += ShortHuffmancodebits(gfc, gi);\n } else {\n data_bits += LongHuffmancodebits(gfc, gi);\n }\n data_bits += huffman_coder_count1(gfc, gi);\n /* does bitcount in quantize.c agree with actual bit count? */\n tot_bits += data_bits;\n }\n /* for ch */\n }\n /* for gr */\n } else {\n /* MPEG 2 */\n gr = 0;\n for (ch = 0; ch < gfc.channels_out; ch++) {\n var gi = l3_side.tt[gr][ch];\n var i, sfb_partition, scale_bits = 0;\n data_bits = 0;\n sfb = 0;\n sfb_partition = 0;\n\n if (gi.block_type == Encoder.SHORT_TYPE) {\n for (; sfb_partition < 4; sfb_partition++) {\n var sfbs = gi.sfb_partition_table[sfb_partition] / 3;\n var slen = gi.slen[sfb_partition];\n for (i = 0; i < sfbs; i++, sfb++) {\n putbits2(gfc,\n Math.max(gi.scalefac[sfb * 3 + 0], 0), slen);\n putbits2(gfc,\n Math.max(gi.scalefac[sfb * 3 + 1], 0), slen);\n putbits2(gfc,\n Math.max(gi.scalefac[sfb * 3 + 2], 0), slen);\n scale_bits += 3 * slen;\n }\n }\n data_bits += ShortHuffmancodebits(gfc, gi);\n } else {\n for (; sfb_partition < 4; sfb_partition++) {\n var sfbs = gi.sfb_partition_table[sfb_partition];\n var slen = gi.slen[sfb_partition];\n for (i = 0; i < sfbs; i++, sfb++) {\n putbits2(gfc, Math.max(gi.scalefac[sfb], 0), slen);\n scale_bits += slen;\n }\n }\n data_bits += LongHuffmancodebits(gfc, gi);\n }\n data_bits += huffman_coder_count1(gfc, gi);\n /* does bitcount in quantize.c agree with actual bit count? */\n tot_bits += scale_bits + data_bits;\n }\n /* for ch */\n }\n /* for gf */\n return tot_bits;\n }\n\n /* main_data */\n\n function TotalBytes() {\n this.total = 0;\n }\n\n /*\n * compute the number of bits required to flush all mp3 frames currently in\n * the buffer. This should be the same as the reservoir size. Only call this\n * routine between frames - i.e. only after all headers and data have been\n * added to the buffer by format_bitstream().\n *\n * Also compute total_bits_output = size of mp3 buffer (including frame\n * headers which may not have yet been send to the mp3 buffer) + number of\n * bits needed to flush all mp3 frames.\n *\n * total_bytes_output is the size of the mp3 output buffer if\n * lame_encode_flush_nogap() was called right now.\n */\n function compute_flushbits(gfp, total_bytes_output) {\n var gfc = gfp.internal_flags;\n var flushbits, remaining_headers;\n var bitsPerFrame;\n var last_ptr, first_ptr;\n first_ptr = gfc.w_ptr;\n /* first header to add to bitstream */\n last_ptr = gfc.h_ptr - 1;\n /* last header to add to bitstream */\n if (last_ptr == -1)\n last_ptr = LameInternalFlags.MAX_HEADER_BUF - 1;\n\n /* add this many bits to bitstream so we can flush all headers */\n flushbits = gfc.header[last_ptr].write_timing - totbit;\n total_bytes_output.total = flushbits;\n\n if (flushbits >= 0) {\n /* if flushbits >= 0, some headers have not yet been written */\n /* reduce flushbits by the size of the headers */\n remaining_headers = 1 + last_ptr - first_ptr;\n if (last_ptr < first_ptr)\n remaining_headers = 1 + last_ptr - first_ptr\n + LameInternalFlags.MAX_HEADER_BUF;\n flushbits -= remaining_headers * 8 * gfc.sideinfo_len;\n }\n\n /*\n * finally, add some bits so that the last frame is complete these bits\n * are not necessary to decode the last frame, but some decoders will\n * ignore last frame if these bits are missing\n */\n bitsPerFrame = self.getframebits(gfp);\n flushbits += bitsPerFrame;\n total_bytes_output.total += bitsPerFrame;\n /* round up: */\n if ((total_bytes_output.total % 8) != 0)\n total_bytes_output.total = 1 + (total_bytes_output.total / 8);\n else\n total_bytes_output.total = (total_bytes_output.total / 8);\n total_bytes_output.total += bufByteIdx + 1;\n\n if (flushbits < 0) {\n System.err.println(\"strange error flushing buffer ... \\n\");\n }\n return flushbits;\n }\n\n this.flush_bitstream = function (gfp) {\n var gfc = gfp.internal_flags;\n var l3_side;\n var flushbits;\n var last_ptr = gfc.h_ptr - 1;\n /* last header to add to bitstream */\n if (last_ptr == -1)\n last_ptr = LameInternalFlags.MAX_HEADER_BUF - 1;\n l3_side = gfc.l3_side;\n\n if ((flushbits = compute_flushbits(gfp, new TotalBytes())) < 0)\n return;\n drain_into_ancillary(gfp, flushbits);\n\n /* check that the 100% of the last frame has been written to bitstream */\n\n /*\n * we have padded out all frames with ancillary data, which is the same\n * as filling the bitreservoir with ancillary data, so :\n */\n gfc.ResvSize = 0;\n l3_side.main_data_begin = 0;\n\n /* save the ReplayGain value */\n if (gfc.findReplayGain) {\n var RadioGain = ga.GetTitleGain(gfc.rgdata);\n gfc.RadioGain = Math.floor(RadioGain * 10.0 + 0.5) | 0;\n /* round to nearest */\n }\n\n /* find the gain and scale change required for no clipping */\n if (gfc.findPeakSample) {\n gfc.noclipGainChange = Math.ceil(Math\n .log10(gfc.PeakSample / 32767.0) * 20.0 * 10.0) | 0;\n /* round up */\n\n if (gfc.noclipGainChange > 0) {\n /* clipping occurs */\n if (EQ(gfp.scale, 1.0) || EQ(gfp.scale, 0.0))\n gfc.noclipScale = (Math\n .floor((32767.0 / gfc.PeakSample) * 100.0) / 100.0);\n /* round down */\n else {\n /*\n * the user specified his own scaling factor. We could\n * suggest the scaling factor of\n * (32767.0/gfp.PeakSample)*(gfp.scale) but it's usually\n * very inaccurate. So we'd rather not advice him on the\n * scaling factor.\n */\n gfc.noclipScale = -1;\n }\n } else\n /* no clipping */\n gfc.noclipScale = -1;\n }\n };\n\n this.add_dummy_byte = function (gfp, val, n) {\n var gfc = gfp.internal_flags;\n var i;\n\n while (n-- > 0) {\n putbits_noheaders(gfc, val, 8);\n\n for (i = 0; i < LameInternalFlags.MAX_HEADER_BUF; ++i)\n gfc.header[i].write_timing += 8;\n }\n };\n\n /**\n * This is called after a frame of audio has been quantized and coded. It\n * will write the encoded audio to the bitstream. Note that from a layer3\n * encoder's perspective the bit stream is primarily a series of main_data()\n * blocks, with header and side information inserted at the proper locations\n * to maintain framing. (See Figure A.7 in the IS).\n */\n this.format_bitstream = function (gfp) {\n var gfc = gfp.internal_flags;\n var l3_side;\n l3_side = gfc.l3_side;\n\n var bitsPerFrame = this.getframebits(gfp);\n drain_into_ancillary(gfp, l3_side.resvDrain_pre);\n\n encodeSideInfo2(gfp, bitsPerFrame);\n var bits = 8 * gfc.sideinfo_len;\n bits += writeMainData(gfp);\n drain_into_ancillary(gfp, l3_side.resvDrain_post);\n bits += l3_side.resvDrain_post;\n\n l3_side.main_data_begin += (bitsPerFrame - bits) / 8;\n\n /*\n * compare number of bits needed to clear all buffered mp3 frames with\n * what we think the resvsize is:\n */\n if (compute_flushbits(gfp, new TotalBytes()) != gfc.ResvSize) {\n System.err.println(\"Internal buffer inconsistency. flushbits <> ResvSize\");\n }\n\n /*\n * compare main_data_begin for the next frame with what we think the\n * resvsize is:\n */\n if ((l3_side.main_data_begin * 8) != gfc.ResvSize) {\n System.err.printf(\"bit reservoir error: \\n\"\n + \"l3_side.main_data_begin: %d \\n\"\n + \"Resvoir size: %d \\n\"\n + \"resv drain (post) %d \\n\"\n + \"resv drain (pre) %d \\n\"\n + \"header and sideinfo: %d \\n\"\n + \"data bits: %d \\n\"\n + \"total bits: %d (remainder: %d) \\n\"\n + \"bitsperframe: %d \\n\",\n 8 * l3_side.main_data_begin, gfc.ResvSize,\n l3_side.resvDrain_post, l3_side.resvDrain_pre,\n 8 * gfc.sideinfo_len, bits - l3_side.resvDrain_post - 8\n * gfc.sideinfo_len, bits, bits % 8, bitsPerFrame);\n\n System.err.println(\"This is a fatal error. It has several possible causes:\");\n System.err.println(\"90%% LAME compiled with buggy version of gcc using advanced optimizations\");\n System.err.println(\" 9%% Your system is overclocked\");\n System.err.println(\" 1%% bug in LAME encoding library\");\n\n gfc.ResvSize = l3_side.main_data_begin * 8;\n }\n //;\n\n if (totbit > 1000000000) {\n /*\n * to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset\n * bit counter\n */\n var i;\n for (i = 0; i < LameInternalFlags.MAX_HEADER_BUF; ++i)\n gfc.header[i].write_timing -= totbit;\n totbit = 0;\n }\n\n return 0;\n };\n\n /**\n * \n * copy data out of the internal MP3 bit buffer into a user supplied\n * unsigned char buffer.\n *\n * mp3data=0 indicates data in buffer is an id3tags and VBR tags\n * mp3data=1 data is real mp3 frame data.\n *
\n */\n this.copy_buffer = function (gfc, buffer, bufferPos, size, mp3data) {\n var minimum = bufByteIdx + 1;\n if (minimum <= 0)\n return 0;\n if (size != 0 && minimum > size) {\n /* buffer is too small */\n return -1;\n }\n System.arraycopy(buf, 0, buffer, bufferPos, minimum);\n bufByteIdx = -1;\n bufBitIdx = 0;\n\n if (mp3data != 0) {\n var crc = new_int(1);\n crc[0] = gfc.nMusicCRC;\n vbr.updateMusicCRC(crc, buffer, bufferPos, minimum);\n gfc.nMusicCRC = crc[0];\n\n /**\n * sum number of bytes belonging to the mp3 stream this info will be\n * written into the Xing/LAME header for seeking\n */\n if (minimum > 0) {\n gfc.VBR_seek_table.nBytesWritten += minimum;\n }\n\n if (gfc.decode_on_the_fly) { /* decode the frame */\n var pcm_buf = new_float_n([2, 1152]);\n var mp3_in = minimum;\n var samples_out = -1;\n var i;\n\n /* re-synthesis to pcm. Repeat until we get a samples_out=0 */\n while (samples_out != 0) {\n\n samples_out = mpg.hip_decode1_unclipped(gfc.hip, buffer,\n bufferPos, mp3_in, pcm_buf[0], pcm_buf[1]);\n /*\n * samples_out = 0: need more data to decode samples_out =\n * -1: error. Lets assume 0 pcm output samples_out = number\n * of samples output\n */\n\n /*\n * set the lenght of the mp3 input buffer to zero, so that\n * in the next iteration of the loop we will be querying\n * mpglib about buffered data\n */\n mp3_in = 0;\n\n if (samples_out == -1) {\n /*\n * error decoding. Not fatal, but might screw up the\n * ReplayGain tag. What should we do? Ignore for now\n */\n samples_out = 0;\n }\n if (samples_out > 0) {\n /* process the PCM data */\n\n /*\n * this should not be possible, and indicates we have\n * overflown the pcm_buf buffer\n */\n\n if (gfc.findPeakSample) {\n for (i = 0; i < samples_out; i++) {\n if (pcm_buf[0][i] > gfc.PeakSample)\n gfc.PeakSample = pcm_buf[0][i];\n else if (-pcm_buf[0][i] > gfc.PeakSample)\n gfc.PeakSample = -pcm_buf[0][i];\n }\n if (gfc.channels_out > 1)\n for (i = 0; i < samples_out; i++) {\n if (pcm_buf[1][i] > gfc.PeakSample)\n gfc.PeakSample = pcm_buf[1][i];\n else if (-pcm_buf[1][i] > gfc.PeakSample)\n gfc.PeakSample = -pcm_buf[1][i];\n }\n }\n\n if (gfc.findReplayGain)\n if (ga.AnalyzeSamples(gfc.rgdata, pcm_buf[0], 0,\n pcm_buf[1], 0, samples_out,\n gfc.channels_out) == GainAnalysis.GAIN_ANALYSIS_ERROR)\n return -6;\n\n }\n /* if (samples_out>0) */\n }\n /* while (samples_out!=0) */\n }\n /* if (gfc.decode_on_the_fly) */\n\n }\n /* if (mp3data) */\n return minimum;\n };\n\n this.init_bit_stream_w = function (gfc) {\n buf = new_byte(Lame.LAME_MAXMP3BUFFER);\n\n gfc.h_ptr = gfc.w_ptr = 0;\n gfc.header[gfc.h_ptr].write_timing = 0;\n bufByteIdx = -1;\n bufBitIdx = 0;\n totbit = 0;\n };\n\n // From machine.h\n\n\n }\n\n function HuffCodeTab(len, max, tab, hl) {\n this.xlen = len;\n this.linmax = max;\n this.table = tab;\n this.hlen = hl;\n }\n\n var Tables = {};\n\n\n Tables.t1HB = [\n 1, 1,\n 1, 0\n ];\n\n Tables.t2HB = [\n 1, 2, 1,\n 3, 1, 1,\n 3, 2, 0\n ];\n\n Tables.t3HB = [\n 3, 2, 1,\n 1, 1, 1,\n 3, 2, 0\n ];\n\n Tables.t5HB = [\n 1, 2, 6, 5,\n 3, 1, 4, 4,\n 7, 5, 7, 1,\n 6, 1, 1, 0\n ];\n\n Tables.t6HB = [\n 7, 3, 5, 1,\n 6, 2, 3, 2,\n 5, 4, 4, 1,\n 3, 3, 2, 0\n ];\n\n Tables.t7HB = [\n 1, 2, 10, 19, 16, 10,\n 3, 3, 7, 10, 5, 3,\n 11, 4, 13, 17, 8, 4,\n 12, 11, 18, 15, 11, 2,\n 7, 6, 9, 14, 3, 1,\n 6, 4, 5, 3, 2, 0\n ];\n\n Tables.t8HB = [\n 3, 4, 6, 18, 12, 5,\n 5, 1, 2, 16, 9, 3,\n 7, 3, 5, 14, 7, 3,\n 19, 17, 15, 13, 10, 4,\n 13, 5, 8, 11, 5, 1,\n 12, 4, 4, 1, 1, 0\n ];\n\n Tables.t9HB = [\n 7, 5, 9, 14, 15, 7,\n 6, 4, 5, 5, 6, 7,\n 7, 6, 8, 8, 8, 5,\n 15, 6, 9, 10, 5, 1,\n 11, 7, 9, 6, 4, 1,\n 14, 4, 6, 2, 6, 0\n ];\n\n Tables.t10HB = [\n 1, 2, 10, 23, 35, 30, 12, 17,\n 3, 3, 8, 12, 18, 21, 12, 7,\n 11, 9, 15, 21, 32, 40, 19, 6,\n 14, 13, 22, 34, 46, 23, 18, 7,\n 20, 19, 33, 47, 27, 22, 9, 3,\n 31, 22, 41, 26, 21, 20, 5, 3,\n 14, 13, 10, 11, 16, 6, 5, 1,\n 9, 8, 7, 8, 4, 4, 2, 0\n ];\n\n Tables.t11HB = [\n 3, 4, 10, 24, 34, 33, 21, 15,\n 5, 3, 4, 10, 32, 17, 11, 10,\n 11, 7, 13, 18, 30, 31, 20, 5,\n 25, 11, 19, 59, 27, 18, 12, 5,\n 35, 33, 31, 58, 30, 16, 7, 5,\n 28, 26, 32, 19, 17, 15, 8, 14,\n 14, 12, 9, 13, 14, 9, 4, 1,\n 11, 4, 6, 6, 6, 3, 2, 0\n ];\n\n Tables.t12HB = [\n 9, 6, 16, 33, 41, 39, 38, 26,\n 7, 5, 6, 9, 23, 16, 26, 11,\n 17, 7, 11, 14, 21, 30, 10, 7,\n 17, 10, 15, 12, 18, 28, 14, 5,\n 32, 13, 22, 19, 18, 16, 9, 5,\n 40, 17, 31, 29, 17, 13, 4, 2,\n 27, 12, 11, 15, 10, 7, 4, 1,\n 27, 12, 8, 12, 6, 3, 1, 0\n ];\n\n Tables.t13HB = [\n 1, 5, 14, 21, 34, 51, 46, 71, 42, 52, 68, 52, 67, 44, 43, 19,\n 3, 4, 12, 19, 31, 26, 44, 33, 31, 24, 32, 24, 31, 35, 22, 14,\n 15, 13, 23, 36, 59, 49, 77, 65, 29, 40, 30, 40, 27, 33, 42, 16,\n 22, 20, 37, 61, 56, 79, 73, 64, 43, 76, 56, 37, 26, 31, 25, 14,\n 35, 16, 60, 57, 97, 75, 114, 91, 54, 73, 55, 41, 48, 53, 23, 24,\n 58, 27, 50, 96, 76, 70, 93, 84, 77, 58, 79, 29, 74, 49, 41, 17,\n 47, 45, 78, 74, 115, 94, 90, 79, 69, 83, 71, 50, 59, 38, 36, 15,\n 72, 34, 56, 95, 92, 85, 91, 90, 86, 73, 77, 65, 51, 44, 43, 42,\n 43, 20, 30, 44, 55, 78, 72, 87, 78, 61, 46, 54, 37, 30, 20, 16,\n 53, 25, 41, 37, 44, 59, 54, 81, 66, 76, 57, 54, 37, 18, 39, 11,\n 35, 33, 31, 57, 42, 82, 72, 80, 47, 58, 55, 21, 22, 26, 38, 22,\n 53, 25, 23, 38, 70, 60, 51, 36, 55, 26, 34, 23, 27, 14, 9, 7,\n 34, 32, 28, 39, 49, 75, 30, 52, 48, 40, 52, 28, 18, 17, 9, 5,\n 45, 21, 34, 64, 56, 50, 49, 45, 31, 19, 12, 15, 10, 7, 6, 3,\n 48, 23, 20, 39, 36, 35, 53, 21, 16, 23, 13, 10, 6, 1, 4, 2,\n 16, 15, 17, 27, 25, 20, 29, 11, 17, 12, 16, 8, 1, 1, 0, 1\n ];\n\n Tables.t15HB = [\n 7, 12, 18, 53, 47, 76, 124, 108, 89, 123, 108, 119, 107, 81, 122, 63,\n 13, 5, 16, 27, 46, 36, 61, 51, 42, 70, 52, 83, 65, 41, 59, 36,\n 19, 17, 15, 24, 41, 34, 59, 48, 40, 64, 50, 78, 62, 80, 56, 33,\n 29, 28, 25, 43, 39, 63, 55, 93, 76, 59, 93, 72, 54, 75, 50, 29,\n 52, 22, 42, 40, 67, 57, 95, 79, 72, 57, 89, 69, 49, 66, 46, 27,\n 77, 37, 35, 66, 58, 52, 91, 74, 62, 48, 79, 63, 90, 62, 40, 38,\n 125, 32, 60, 56, 50, 92, 78, 65, 55, 87, 71, 51, 73, 51, 70, 30,\n 109, 53, 49, 94, 88, 75, 66, 122, 91, 73, 56, 42, 64, 44, 21, 25,\n 90, 43, 41, 77, 73, 63, 56, 92, 77, 66, 47, 67, 48, 53, 36, 20,\n 71, 34, 67, 60, 58, 49, 88, 76, 67, 106, 71, 54, 38, 39, 23, 15,\n 109, 53, 51, 47, 90, 82, 58, 57, 48, 72, 57, 41, 23, 27, 62, 9,\n 86, 42, 40, 37, 70, 64, 52, 43, 70, 55, 42, 25, 29, 18, 11, 11,\n 118, 68, 30, 55, 50, 46, 74, 65, 49, 39, 24, 16, 22, 13, 14, 7,\n 91, 44, 39, 38, 34, 63, 52, 45, 31, 52, 28, 19, 14, 8, 9, 3,\n 123, 60, 58, 53, 47, 43, 32, 22, 37, 24, 17, 12, 15, 10, 2, 1,\n 71, 37, 34, 30, 28, 20, 17, 26, 21, 16, 10, 6, 8, 6, 2, 0\n ];\n\n Tables.t16HB = [\n 1, 5, 14, 44, 74, 63, 110, 93, 172, 149, 138, 242, 225, 195, 376, 17,\n 3, 4, 12, 20, 35, 62, 53, 47, 83, 75, 68, 119, 201, 107, 207, 9,\n 15, 13, 23, 38, 67, 58, 103, 90, 161, 72, 127, 117, 110, 209, 206, 16,\n 45, 21, 39, 69, 64, 114, 99, 87, 158, 140, 252, 212, 199, 387, 365, 26,\n 75, 36, 68, 65, 115, 101, 179, 164, 155, 264, 246, 226, 395, 382, 362, 9,\n 66, 30, 59, 56, 102, 185, 173, 265, 142, 253, 232, 400, 388, 378, 445, 16,\n 111, 54, 52, 100, 184, 178, 160, 133, 257, 244, 228, 217, 385, 366, 715, 10,\n 98, 48, 91, 88, 165, 157, 148, 261, 248, 407, 397, 372, 380, 889, 884, 8,\n 85, 84, 81, 159, 156, 143, 260, 249, 427, 401, 392, 383, 727, 713, 708, 7,\n 154, 76, 73, 141, 131, 256, 245, 426, 406, 394, 384, 735, 359, 710, 352, 11,\n 139, 129, 67, 125, 247, 233, 229, 219, 393, 743, 737, 720, 885, 882, 439, 4,\n 243, 120, 118, 115, 227, 223, 396, 746, 742, 736, 721, 712, 706, 223, 436, 6,\n 202, 224, 222, 218, 216, 389, 386, 381, 364, 888, 443, 707, 440, 437, 1728, 4,\n 747, 211, 210, 208, 370, 379, 734, 723, 714, 1735, 883, 877, 876, 3459, 865, 2,\n 377, 369, 102, 187, 726, 722, 358, 711, 709, 866, 1734, 871, 3458, 870, 434, 0,\n 12, 10, 7, 11, 10, 17, 11, 9, 13, 12, 10, 7, 5, 3, 1, 3\n ];\n\n Tables.t24HB = [\n 15, 13, 46, 80, 146, 262, 248, 434, 426, 669, 653, 649, 621, 517, 1032, 88,\n 14, 12, 21, 38, 71, 130, 122, 216, 209, 198, 327, 345, 319, 297, 279, 42,\n 47, 22, 41, 74, 68, 128, 120, 221, 207, 194, 182, 340, 315, 295, 541, 18,\n 81, 39, 75, 70, 134, 125, 116, 220, 204, 190, 178, 325, 311, 293, 271, 16,\n 147, 72, 69, 135, 127, 118, 112, 210, 200, 188, 352, 323, 306, 285, 540, 14,\n 263, 66, 129, 126, 119, 114, 214, 202, 192, 180, 341, 317, 301, 281, 262, 12,\n 249, 123, 121, 117, 113, 215, 206, 195, 185, 347, 330, 308, 291, 272, 520, 10,\n 435, 115, 111, 109, 211, 203, 196, 187, 353, 332, 313, 298, 283, 531, 381, 17,\n 427, 212, 208, 205, 201, 193, 186, 177, 169, 320, 303, 286, 268, 514, 377, 16,\n 335, 199, 197, 191, 189, 181, 174, 333, 321, 305, 289, 275, 521, 379, 371, 11,\n 668, 184, 183, 179, 175, 344, 331, 314, 304, 290, 277, 530, 383, 373, 366, 10,\n 652, 346, 171, 168, 164, 318, 309, 299, 287, 276, 263, 513, 375, 368, 362, 6,\n 648, 322, 316, 312, 307, 302, 292, 284, 269, 261, 512, 376, 370, 364, 359, 4,\n 620, 300, 296, 294, 288, 282, 273, 266, 515, 380, 374, 369, 365, 361, 357, 2,\n 1033, 280, 278, 274, 267, 264, 259, 382, 378, 372, 367, 363, 360, 358, 356, 0,\n 43, 20, 19, 17, 15, 13, 11, 9, 7, 6, 4, 7, 5, 3, 1, 3\n ];\n\n Tables.t32HB = [\n 1 << 0, 5 << 1, 4 << 1, 5 << 2, 6 << 1, 5 << 2, 4 << 2, 4 << 3,\n 7 << 1, 3 << 2, 6 << 2, 0 << 3, 7 << 2, 2 << 3, 3 << 3, 1 << 4\n ];\n\n Tables.t33HB = [\n 15 << 0, 14 << 1, 13 << 1, 12 << 2, 11 << 1, 10 << 2, 9 << 2, 8 << 3,\n 7 << 1, 6 << 2, 5 << 2, 4 << 3, 3 << 2, 2 << 3, 1 << 3, 0 << 4\n ];\n\n Tables.t1l = [\n 1, 4,\n 3, 5\n ];\n\n Tables.t2l = [\n 1, 4, 7,\n 4, 5, 7,\n 6, 7, 8\n ];\n\n Tables.t3l = [\n 2, 3, 7,\n 4, 4, 7,\n 6, 7, 8\n ];\n\n Tables.t5l = [\n 1, 4, 7, 8,\n 4, 5, 8, 9,\n 7, 8, 9, 10,\n 8, 8, 9, 10\n ];\n\n Tables.t6l = [\n 3, 4, 6, 8,\n 4, 4, 6, 7,\n 5, 6, 7, 8,\n 7, 7, 8, 9\n ];\n\n Tables.t7l = [\n 1, 4, 7, 9, 9, 10,\n 4, 6, 8, 9, 9, 10,\n 7, 7, 9, 10, 10, 11,\n 8, 9, 10, 11, 11, 11,\n 8, 9, 10, 11, 11, 12,\n 9, 10, 11, 12, 12, 12\n ];\n\n Tables.t8l = [\n 2, 4, 7, 9, 9, 10,\n 4, 4, 6, 10, 10, 10,\n 7, 6, 8, 10, 10, 11,\n 9, 10, 10, 11, 11, 12,\n 9, 9, 10, 11, 12, 12,\n 10, 10, 11, 11, 13, 13\n ];\n\n Tables.t9l = [\n 3, 4, 6, 7, 9, 10,\n 4, 5, 6, 7, 8, 10,\n 5, 6, 7, 8, 9, 10,\n 7, 7, 8, 9, 9, 10,\n 8, 8, 9, 9, 10, 11,\n 9, 9, 10, 10, 11, 11\n ];\n\n Tables.t10l = [\n 1, 4, 7, 9, 10, 10, 10, 11,\n 4, 6, 8, 9, 10, 11, 10, 10,\n 7, 8, 9, 10, 11, 12, 11, 11,\n 8, 9, 10, 11, 12, 12, 11, 12,\n 9, 10, 11, 12, 12, 12, 12, 12,\n 10, 11, 12, 12, 13, 13, 12, 13,\n 9, 10, 11, 12, 12, 12, 13, 13,\n 10, 10, 11, 12, 12, 13, 13, 13\n ];\n\n Tables.t11l = [\n 2, 4, 6, 8, 9, 10, 9, 10,\n 4, 5, 6, 8, 10, 10, 9, 10,\n 6, 7, 8, 9, 10, 11, 10, 10,\n 8, 8, 9, 11, 10, 12, 10, 11,\n 9, 10, 10, 11, 11, 12, 11, 12,\n 9, 10, 11, 12, 12, 13, 12, 13,\n 9, 9, 9, 10, 11, 12, 12, 12,\n 9, 9, 10, 11, 12, 12, 12, 12\n ];\n\n Tables.t12l = [\n 4, 4, 6, 8, 9, 10, 10, 10,\n 4, 5, 6, 7, 9, 9, 10, 10,\n 6, 6, 7, 8, 9, 10, 9, 10,\n 7, 7, 8, 8, 9, 10, 10, 10,\n 8, 8, 9, 9, 10, 10, 10, 11,\n 9, 9, 10, 10, 10, 11, 10, 11,\n 9, 9, 9, 10, 10, 11, 11, 12,\n 10, 10, 10, 11, 11, 11, 11, 12\n ];\n\n Tables.t13l = [\n 1, 5, 7, 8, 9, 10, 10, 11, 10, 11, 12, 12, 13, 13, 14, 14,\n 4, 6, 8, 9, 10, 10, 11, 11, 11, 11, 12, 12, 13, 14, 14, 14,\n 7, 8, 9, 10, 11, 11, 12, 12, 11, 12, 12, 13, 13, 14, 15, 15,\n 8, 9, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 15, 15,\n 9, 9, 11, 11, 12, 12, 13, 13, 12, 13, 13, 14, 14, 15, 15, 16,\n 10, 10, 11, 12, 12, 12, 13, 13, 13, 13, 14, 13, 15, 15, 16, 16,\n 10, 11, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16,\n 11, 11, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 18, 18,\n 10, 10, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 16, 17, 17,\n 11, 11, 12, 12, 13, 13, 13, 15, 14, 15, 15, 16, 16, 16, 18, 17,\n 11, 12, 12, 13, 13, 14, 14, 15, 14, 15, 16, 15, 16, 17, 18, 19,\n 12, 12, 12, 13, 14, 14, 14, 14, 15, 15, 15, 16, 17, 17, 17, 18,\n 12, 13, 13, 14, 14, 15, 14, 15, 16, 16, 17, 17, 17, 18, 18, 18,\n 13, 13, 14, 15, 15, 15, 16, 16, 16, 16, 16, 17, 18, 17, 18, 18,\n 14, 14, 14, 15, 15, 15, 17, 16, 16, 19, 17, 17, 17, 19, 18, 18,\n 13, 14, 15, 16, 16, 16, 17, 16, 17, 17, 18, 18, 21, 20, 21, 18\n ];\n\n Tables.t15l = [\n 3, 5, 6, 8, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 14,\n 5, 5, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13,\n 6, 7, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 13,\n 7, 8, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13,\n 8, 8, 9, 9, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13,\n 9, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 13, 14,\n 10, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 14, 14,\n 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 14,\n 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 14, 14, 14,\n 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14,\n 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 15, 14,\n 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15,\n 12, 12, 11, 12, 12, 12, 13, 13, 13, 13, 13, 13, 14, 14, 15, 15,\n 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15,\n 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 14, 15,\n 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15\n ];\n\n Tables.t16_5l = [\n 1, 5, 7, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 11,\n 4, 6, 8, 9, 10, 11, 11, 11, 12, 12, 12, 13, 14, 13, 14, 11,\n 7, 8, 9, 10, 11, 11, 12, 12, 13, 12, 13, 13, 13, 14, 14, 12,\n 9, 9, 10, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 15, 15, 13,\n 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 12,\n 10, 10, 11, 11, 12, 13, 13, 14, 13, 14, 14, 15, 15, 15, 16, 13,\n 11, 11, 11, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 13,\n 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 17, 17, 13,\n 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 13,\n 12, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 15, 16, 15, 14,\n 12, 13, 12, 13, 14, 14, 14, 14, 15, 16, 16, 16, 17, 17, 16, 13,\n 13, 13, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, 16, 15, 16, 14,\n 13, 14, 14, 14, 14, 15, 15, 15, 15, 17, 16, 16, 16, 16, 18, 14,\n 15, 14, 14, 14, 15, 15, 16, 16, 16, 18, 17, 17, 17, 19, 17, 14,\n 14, 15, 13, 14, 16, 16, 15, 16, 16, 17, 18, 17, 19, 17, 16, 14,\n 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 12\n ];\n\n Tables.t16l = [\n 1, 5, 7, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 10,\n 4, 6, 8, 9, 10, 11, 11, 11, 12, 12, 12, 13, 14, 13, 14, 10,\n 7, 8, 9, 10, 11, 11, 12, 12, 13, 12, 13, 13, 13, 14, 14, 11,\n 9, 9, 10, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 15, 15, 12,\n 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 11,\n 10, 10, 11, 11, 12, 13, 13, 14, 13, 14, 14, 15, 15, 15, 16, 12,\n 11, 11, 11, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 12,\n 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 17, 17, 12,\n 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 12,\n 12, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 15, 16, 15, 13,\n 12, 13, 12, 13, 14, 14, 14, 14, 15, 16, 16, 16, 17, 17, 16, 12,\n 13, 13, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, 16, 15, 16, 13,\n 13, 14, 14, 14, 14, 15, 15, 15, 15, 17, 16, 16, 16, 16, 18, 13,\n 15, 14, 14, 14, 15, 15, 16, 16, 16, 18, 17, 17, 17, 19, 17, 13,\n 14, 15, 13, 14, 16, 16, 15, 16, 16, 17, 18, 17, 19, 17, 16, 13,\n 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 10\n ];\n\n Tables.t24l = [\n 4, 5, 7, 8, 9, 10, 10, 11, 11, 12, 12, 12, 12, 12, 13, 10,\n 5, 6, 7, 8, 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 12, 10,\n 7, 7, 8, 9, 9, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 9,\n 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 9,\n 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 9,\n 10, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 9,\n 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 9,\n 11, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 10,\n 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 10,\n 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 10,\n 12, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 10,\n 12, 12, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 10,\n 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 10,\n 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 10,\n 13, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 10,\n 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 6\n ];\n\n Tables.t32l = [\n 1 + 0, 4 + 1, 4 + 1, 5 + 2, 4 + 1, 6 + 2, 5 + 2, 6 + 3,\n 4 + 1, 5 + 2, 5 + 2, 6 + 3, 5 + 2, 6 + 3, 6 + 3, 6 + 4\n ];\n\n Tables.t33l = [\n 4 + 0, 4 + 1, 4 + 1, 4 + 2, 4 + 1, 4 + 2, 4 + 2, 4 + 3,\n 4 + 1, 4 + 2, 4 + 2, 4 + 3, 4 + 2, 4 + 3, 4 + 3, 4 + 4\n ];\n\n Tables.ht = [\n /* xlen, linmax, table, hlen */\n new HuffCodeTab(0, 0, null, null),\n new HuffCodeTab(2, 0, Tables.t1HB, Tables.t1l),\n new HuffCodeTab(3, 0, Tables.t2HB, Tables.t2l),\n new HuffCodeTab(3, 0, Tables.t3HB, Tables.t3l),\n new HuffCodeTab(0, 0, null, null), /* Apparently not used */\n new HuffCodeTab(4, 0, Tables.t5HB, Tables.t5l),\n new HuffCodeTab(4, 0, Tables.t6HB, Tables.t6l),\n new HuffCodeTab(6, 0, Tables.t7HB, Tables.t7l),\n new HuffCodeTab(6, 0, Tables.t8HB, Tables.t8l),\n new HuffCodeTab(6, 0, Tables.t9HB, Tables.t9l),\n new HuffCodeTab(8, 0, Tables.t10HB, Tables.t10l),\n new HuffCodeTab(8, 0, Tables.t11HB, Tables.t11l),\n new HuffCodeTab(8, 0, Tables.t12HB, Tables.t12l),\n new HuffCodeTab(16, 0, Tables.t13HB, Tables.t13l),\n new HuffCodeTab(0, 0, null, Tables.t16_5l), /* Apparently not used */\n new HuffCodeTab(16, 0, Tables.t15HB, Tables.t15l),\n\n new HuffCodeTab(1, 1, Tables.t16HB, Tables.t16l),\n new HuffCodeTab(2, 3, Tables.t16HB, Tables.t16l),\n new HuffCodeTab(3, 7, Tables.t16HB, Tables.t16l),\n new HuffCodeTab(4, 15, Tables.t16HB, Tables.t16l),\n new HuffCodeTab(6, 63, Tables.t16HB, Tables.t16l),\n new HuffCodeTab(8, 255, Tables.t16HB, Tables.t16l),\n new HuffCodeTab(10, 1023, Tables.t16HB, Tables.t16l),\n new HuffCodeTab(13, 8191, Tables.t16HB, Tables.t16l),\n\n new HuffCodeTab(4, 15, Tables.t24HB, Tables.t24l),\n new HuffCodeTab(5, 31, Tables.t24HB, Tables.t24l),\n new HuffCodeTab(6, 63, Tables.t24HB, Tables.t24l),\n new HuffCodeTab(7, 127, Tables.t24HB, Tables.t24l),\n new HuffCodeTab(8, 255, Tables.t24HB, Tables.t24l),\n new HuffCodeTab(9, 511, Tables.t24HB, Tables.t24l),\n new HuffCodeTab(11, 2047, Tables.t24HB, Tables.t24l),\n new HuffCodeTab(13, 8191, Tables.t24HB, Tables.t24l),\n\n new HuffCodeTab(0, 0, Tables.t32HB, Tables.t32l),\n new HuffCodeTab(0, 0, Tables.t33HB, Tables.t33l),\n ];\n\n /**\n * \n * for (i = 0; i < 16*16; i++) [\n * largetbl[i] = ((ht[16].hlen[i]) << 16) + ht[24].hlen[i];\n * ]\n *
\n *\n */\n Tables.largetbl = [\n 0x010004, 0x050005, 0x070007, 0x090008, 0x0a0009, 0x0a000a, 0x0b000a, 0x0b000b,\n 0x0c000b, 0x0c000c, 0x0c000c, 0x0d000c, 0x0d000c, 0x0d000c, 0x0e000d, 0x0a000a,\n 0x040005, 0x060006, 0x080007, 0x090008, 0x0a0009, 0x0b000a, 0x0b000a, 0x0b000b,\n 0x0c000b, 0x0c000b, 0x0c000c, 0x0d000c, 0x0e000c, 0x0d000c, 0x0e000c, 0x0a000a,\n 0x070007, 0x080007, 0x090008, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000b,\n 0x0d000b, 0x0c000b, 0x0d000b, 0x0d000c, 0x0d000c, 0x0e000c, 0x0e000d, 0x0b0009,\n 0x090008, 0x090008, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000a, 0x0c000b,\n 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0c0009,\n 0x0a0009, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000a, 0x0d000a, 0x0d000b,\n 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000d, 0x0b0009,\n 0x0a000a, 0x0a0009, 0x0b000a, 0x0b000a, 0x0c000a, 0x0d000a, 0x0d000b, 0x0e000b,\n 0x0d000b, 0x0e000b, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x0c0009,\n 0x0b000a, 0x0b000a, 0x0b000a, 0x0c000a, 0x0d000a, 0x0d000b, 0x0d000b, 0x0d000b,\n 0x0e000b, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x10000d, 0x0c0009,\n 0x0b000b, 0x0b000a, 0x0c000a, 0x0c000a, 0x0d000b, 0x0d000b, 0x0d000b, 0x0e000b,\n 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x11000d, 0x11000d, 0x0c000a,\n 0x0b000b, 0x0c000b, 0x0c000b, 0x0d000b, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000b,\n 0x0f000b, 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x10000d, 0x10000d, 0x0c000a,\n 0x0c000b, 0x0c000b, 0x0c000b, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000b, 0x0f000c,\n 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x0f000d, 0x10000d, 0x0f000d, 0x0d000a,\n 0x0c000c, 0x0d000b, 0x0c000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0e000c,\n 0x0f000c, 0x10000c, 0x10000c, 0x10000d, 0x11000d, 0x11000d, 0x10000d, 0x0c000a,\n 0x0d000c, 0x0d000c, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0f000c, 0x10000c,\n 0x10000c, 0x10000c, 0x10000c, 0x10000d, 0x10000d, 0x0f000d, 0x10000d, 0x0d000a,\n 0x0d000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c,\n 0x0f000c, 0x11000c, 0x10000d, 0x10000d, 0x10000d, 0x10000d, 0x12000d, 0x0d000a,\n 0x0f000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x10000c, 0x10000c,\n 0x10000d, 0x12000d, 0x11000d, 0x11000d, 0x11000d, 0x13000d, 0x11000d, 0x0d000a,\n 0x0e000d, 0x0f000c, 0x0d000c, 0x0e000c, 0x10000c, 0x10000c, 0x0f000c, 0x10000d,\n 0x10000d, 0x11000d, 0x12000d, 0x11000d, 0x13000d, 0x11000d, 0x10000d, 0x0d000a,\n 0x0a0009, 0x0a0009, 0x0a0009, 0x0b0009, 0x0b0009, 0x0c0009, 0x0c0009, 0x0c0009,\n 0x0d0009, 0x0d0009, 0x0d0009, 0x0d000a, 0x0d000a, 0x0d000a, 0x0d000a, 0x0a0006\n ];\n /**\n * \n * for (i = 0; i < 3*3; i++) [\n * table23[i] = ((ht[2].hlen[i]) << 16) + ht[3].hlen[i];\n * ]\n *
\n *\n */\n Tables.table23 = [\n 0x010002, 0x040003, 0x070007,\n 0x040004, 0x050004, 0x070007,\n 0x060006, 0x070007, 0x080008\n ];\n\n /**\n * \n * for (i = 0; i < 4*4; i++) [\n * table56[i] = ((ht[5].hlen[i]) << 16) + ht[6].hlen[i];\n * ]\n *
\n *\n */\n Tables.table56 = [\n 0x010003, 0x040004, 0x070006, 0x080008, 0x040004, 0x050004, 0x080006, 0x090007,\n 0x070005, 0x080006, 0x090007, 0x0a0008, 0x080007, 0x080007, 0x090008, 0x0a0009\n ];\n\n Tables.bitrate_table = [\n [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1], /* MPEG 2 */\n [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1], /* MPEG 1 */\n [0, 8, 16, 24, 32, 40, 48, 56, 64, -1, -1, -1, -1, -1, -1, -1], /* MPEG 2.5 */\n ];\n\n /**\n * MPEG 2, MPEG 1, MPEG 2.5.\n */\n Tables.samplerate_table = [\n [22050, 24000, 16000, -1],\n [44100, 48000, 32000, -1],\n [11025, 12000, 8000, -1],\n ];\n\n /**\n * This is the scfsi_band table from 2.4.2.7 of the IS.\n */\n Tables.scfsi_band = [0, 6, 11, 16, 21];\n\n function MeanBits(meanBits) {\n this.bits = meanBits;\n }\n\n//package mp3;\n\n function CalcNoiseResult() {\n /**\n * sum of quantization noise > masking\n */\n this.over_noise = 0.;\n /**\n * sum of all quantization noise\n */\n this.tot_noise = 0.;\n /**\n * max quantization noise\n */\n this.max_noise = 0.;\n /**\n * number of quantization noise > masking\n */\n this.over_count = 0;\n /**\n * SSD-like cost of distorted bands\n */\n this.over_SSD = 0;\n this.bits = 0;\n }\n\n function VBRQuantize() {\n var qupvt;\n var tak;\n\n this.setModules = function (_qupvt, _tk) {\n qupvt = _qupvt;\n tak = _tk;\n }\n //TODO\n\n }\n\n\n\n /**\n * ATH related stuff, if something new ATH related has to be added, please plug\n * it here into the ATH.\n */\n function ATH() {\n /**\n * Method for the auto adjustment.\n */\n this.useAdjust = 0;\n /**\n * factor for tuning the (sample power) point below which adaptive threshold\n * of hearing adjustment occurs\n */\n this.aaSensitivityP = 0.;\n /**\n * Lowering based on peak volume, 1 = no lowering.\n */\n this.adjust = 0.;\n /**\n * Limit for dynamic ATH adjust.\n */\n this.adjustLimit = 0.;\n /**\n * Determined to lower x dB each second.\n */\n this.decay = 0.;\n /**\n * Lowest ATH value.\n */\n this.floor = 0.;\n /**\n * ATH for sfbs in long blocks.\n */\n this.l = new_float(Encoder.SBMAX_l);\n /**\n * ATH for sfbs in short blocks.\n */\n this.s = new_float(Encoder.SBMAX_s);\n /**\n * ATH for partitioned sfb21 in long blocks.\n */\n this.psfb21 = new_float(Encoder.PSFB21);\n /**\n * ATH for partitioned sfb12 in short blocks.\n */\n this.psfb12 = new_float(Encoder.PSFB12);\n /**\n * ATH for long block convolution bands.\n */\n this.cb_l = new_float(Encoder.CBANDS);\n /**\n * ATH for short block convolution bands.\n */\n this.cb_s = new_float(Encoder.CBANDS);\n /**\n * Equal loudness weights (based on ATH).\n */\n this.eql_w = new_float(Encoder.BLKSIZE / 2);\n }\n\n\n function LameGlobalFlags() {\n\n this.class_id = 0;\n\n /* input description */\n\n /**\n * number of samples. default=-1\n */\n this.num_samples = 0;\n /**\n * input number of channels. default=2\n */\n this.num_channels = 0;\n /**\n * input_samp_rate in Hz. default=44.1 kHz\n */\n this.in_samplerate = 0;\n /**\n * output_samp_rate. default: LAME picks best value at least not used for\n * MP3 decoding: Remember 44.1 kHz MP3s and AC97\n */\n this.out_samplerate = 0;\n /**\n * scale input by this amount before encoding at least not used for MP3\n * decoding\n */\n this.scale = 0.;\n /**\n * scale input of channel 0 (left) by this amount before encoding\n */\n this.scale_left = 0.;\n /**\n * scale input of channel 1 (right) by this amount before encoding\n */\n this.scale_right = 0.;\n\n /* general control params */\n /**\n * collect data for a MP3 frame analyzer?\n */\n this.analysis = false;\n /**\n * add Xing VBR tag?\n */\n this.bWriteVbrTag = false;\n\n /**\n * use lame/mpglib to convert mp3 to wav\n */\n this.decode_only = false;\n /**\n * quality setting 0=best, 9=worst default=5\n */\n this.quality = 0;\n /**\n * see enum default = LAME picks best value\n */\n this.mode = MPEGMode.STEREO;\n /**\n * force M/S mode. requires mode=1\n */\n this.force_ms = false;\n /**\n * use free format? default=0\n */\n this.free_format = false;\n /**\n * find the RG value? default=0\n */\n this.findReplayGain = false;\n /**\n * decode on the fly? default=0\n */\n this.decode_on_the_fly = false;\n /**\n * 1 (default) writes ID3 tags, 0 not\n */\n this.write_id3tag_automatic = false;\n\n /*\n * set either brate>0 or compression_ratio>0, LAME will compute the value of\n * the variable not set. Default is compression_ratio = 11.025\n */\n /**\n * bitrate\n */\n this.brate = 0;\n /**\n * sizeof(wav file)/sizeof(mp3 file)\n */\n this.compression_ratio = 0.;\n\n /* frame params */\n /**\n * mark as copyright. default=0\n */\n this.copyright = 0;\n /**\n * mark as original. default=1\n */\n this.original = 0;\n /**\n * the MP3 'private extension' bit. Meaningless\n */\n this.extension = 0;\n /**\n * Input PCM is emphased PCM (for instance from one of the rarely emphased\n * CDs), it is STRONGLY not recommended to use this, because psycho does not\n * take it into account, and last but not least many decoders don't care\n * about these bits\n */\n this.emphasis = 0;\n /**\n * use 2 bytes per frame for a CRC checksum. default=0\n */\n this.error_protection = 0;\n /**\n * enforce ISO spec as much as possible\n */\n this.strict_ISO = false;\n\n /**\n * use bit reservoir?\n */\n this.disable_reservoir = false;\n\n /* quantization/noise shaping */\n this.quant_comp = 0;\n this.quant_comp_short = 0;\n this.experimentalY = false;\n this.experimentalZ = 0;\n this.exp_nspsytune = 0;\n\n this.preset = 0;\n\n /* VBR control */\n this.VBR = null;\n /**\n * Range [0,...,1[\n */\n this.VBR_q_frac = 0.;\n /**\n * Range [0,...,9]\n */\n this.VBR_q = 0;\n this.VBR_mean_bitrate_kbps = 0;\n this.VBR_min_bitrate_kbps = 0;\n this.VBR_max_bitrate_kbps = 0;\n /**\n * strictly enforce VBR_min_bitrate normaly, it will be violated for analog\n * silence\n */\n this.VBR_hard_min = 0;\n\n /* resampling and filtering */\n\n /**\n * freq in Hz. 0=lame choses. -1=no filter\n */\n this.lowpassfreq = 0;\n /**\n * freq in Hz. 0=lame choses. -1=no filter\n */\n this.highpassfreq = 0;\n /**\n * freq width of filter, in Hz (default=15%)\n */\n this.lowpasswidth = 0;\n /**\n * freq width of filter, in Hz (default=15%)\n */\n this.highpasswidth = 0;\n\n /*\n * psycho acoustics and other arguments which you should not change unless\n * you know what you are doing\n */\n\n this.maskingadjust = 0.;\n this.maskingadjust_short = 0.;\n /**\n * only use ATH\n */\n this.ATHonly = false;\n /**\n * only use ATH for short blocks\n */\n this.ATHshort = false;\n /**\n * disable ATH\n */\n this.noATH = false;\n /**\n * select ATH formula\n */\n this.ATHtype = 0;\n /**\n * change ATH formula 4 shape\n */\n this.ATHcurve = 0.;\n /**\n * lower ATH by this many db\n */\n this.ATHlower = 0.;\n /**\n * select ATH auto-adjust scheme\n */\n this.athaa_type = 0;\n /**\n * select ATH auto-adjust loudness calc\n */\n this.athaa_loudapprox = 0;\n /**\n * dB, tune active region of auto-level\n */\n this.athaa_sensitivity = 0.;\n this.short_blocks = null;\n /**\n * use temporal masking effect\n */\n this.useTemporal = false;\n this.interChRatio = 0.;\n /**\n * Naoki's adjustment of Mid/Side maskings\n */\n this.msfix = 0.;\n\n /**\n * 0 off, 1 on\n */\n this.tune = false;\n /**\n * used to pass values for debugging and stuff\n */\n this.tune_value_a = 0.;\n\n /************************************************************************/\n /* internal variables, do not set... */\n /* provided because they may be of use to calling application */\n /************************************************************************/\n\n /**\n * 0=MPEG-2/2.5 1=MPEG-1\n */\n this.version = 0;\n this.encoder_delay = 0;\n /**\n * number of samples of padding appended to input\n */\n this.encoder_padding = 0;\n this.framesize = 0;\n /**\n * number of frames encoded\n */\n this.frameNum = 0;\n /**\n * is this struct owned by calling program or lame?\n */\n this.lame_allocated_gfp = 0;\n /**************************************************************************/\n /* more internal variables are stored in this structure: */\n /**************************************************************************/\n this.internal_flags = null;\n }\n\n\n\n function CBRNewIterationLoop(_quantize) {\n var quantize = _quantize;\n this.quantize = quantize;\n this.iteration_loop = function(gfp, pe, ms_ener_ratio, ratio) {\n var gfc = gfp.internal_flags;\n var l3_xmin = new_float(L3Side.SFBMAX);\n var xrpow = new_float(576);\n var targ_bits = new_int(2);\n var mean_bits = 0, max_bits;\n var l3_side = gfc.l3_side;\n\n var mb = new MeanBits(mean_bits);\n this.quantize.rv.ResvFrameBegin(gfp, mb);\n mean_bits = mb.bits;\n\n /* quantize! */\n for (var gr = 0; gr < gfc.mode_gr; gr++) {\n\n /*\n * calculate needed bits\n */\n max_bits = this.quantize.qupvt.on_pe(gfp, pe, targ_bits, mean_bits,\n gr, gr);\n\n if (gfc.mode_ext == Encoder.MPG_MD_MS_LR) {\n this.quantize.ms_convert(gfc.l3_side, gr);\n this.quantize.qupvt.reduce_side(targ_bits, ms_ener_ratio[gr],\n mean_bits, max_bits);\n }\n\n for (var ch = 0; ch < gfc.channels_out; ch++) {\n var adjust, masking_lower_db;\n var cod_info = l3_side.tt[gr][ch];\n\n if (cod_info.block_type != Encoder.SHORT_TYPE) {\n // NORM, START or STOP type\n adjust = 0;\n masking_lower_db = gfc.PSY.mask_adjust - adjust;\n } else {\n adjust = 0;\n masking_lower_db = gfc.PSY.mask_adjust_short - adjust;\n }\n gfc.masking_lower = Math.pow(10.0,\n masking_lower_db * 0.1);\n\n /*\n * init_outer_loop sets up cod_info, scalefac and xrpow\n */\n this.quantize.init_outer_loop(gfc, cod_info);\n if (this.quantize.init_xrpow(gfc, cod_info, xrpow)) {\n /*\n * xr contains energy we will have to encode calculate the\n * masking abilities find some good quantization in\n * outer_loop\n */\n this.quantize.qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info,\n l3_xmin);\n this.quantize.outer_loop(gfp, cod_info, l3_xmin, xrpow, ch,\n targ_bits[ch]);\n }\n\n this.quantize.iteration_finish_one(gfc, gr, ch);\n } /* for ch */\n } /* for gr */\n\n this.quantize.rv.ResvFrameEnd(gfc, mean_bits);\n }\n }\n\n\n function ReplayGain() {\n this.linprebuf = new_float(GainAnalysis.MAX_ORDER * 2);\n /**\n * left input samples, with pre-buffer\n */\n this.linpre = 0;\n this.lstepbuf = new_float(GainAnalysis.MAX_SAMPLES_PER_WINDOW + GainAnalysis.MAX_ORDER);\n /**\n * left \"first step\" (i.e. post first filter) samples\n */\n this.lstep = 0;\n this.loutbuf = new_float(GainAnalysis.MAX_SAMPLES_PER_WINDOW + GainAnalysis.MAX_ORDER);\n /**\n * left \"out\" (i.e. post second filter) samples\n */\n this.lout = 0;\n this.rinprebuf = new_float(GainAnalysis.MAX_ORDER * 2);\n /**\n * right input samples ...\n */\n this.rinpre = 0;\n this.rstepbuf = new_float(GainAnalysis.MAX_SAMPLES_PER_WINDOW + GainAnalysis.MAX_ORDER);\n this.rstep = 0;\n this.routbuf = new_float(GainAnalysis.MAX_SAMPLES_PER_WINDOW + GainAnalysis.MAX_ORDER);\n this.rout = 0;\n /**\n * number of samples required to reach number of milliseconds required\n * for RMS window\n */\n this.sampleWindow = 0;\n this.totsamp = 0;\n this.lsum = 0.;\n this.rsum = 0.;\n this.freqindex = 0;\n this.first = 0;\n this.A = new_int(0 | (GainAnalysis.STEPS_per_dB * GainAnalysis.MAX_dB));\n this.B = new_int(0 | (GainAnalysis.STEPS_per_dB * GainAnalysis.MAX_dB));\n\n }\n\n//package mp3;\n\n /**\n * Layer III side information.\n *\n * @author Ken\n *\n */\n\n\n\n function ScaleFac(arrL, arrS, arr21, arr12) {\n\n this.l = new_int(1 + Encoder.SBMAX_l);\n this.s = new_int(1 + Encoder.SBMAX_s);\n this.psfb21 = new_int(1 + Encoder.PSFB21);\n this.psfb12 = new_int(1 + Encoder.PSFB12);\n var l = this.l;\n var s = this.s;\n\n if (arguments.length == 4) {\n //public ScaleFac(final int[] arrL, final int[] arrS, final int[] arr21,\n // final int[] arr12) {\n this.arrL = arguments[0];\n this.arrS = arguments[1];\n this.arr21 = arguments[2];\n this.arr12 = arguments[3];\n\n System.arraycopy(this.arrL, 0, l, 0, Math.min(this.arrL.length, this.l.length));\n System.arraycopy(this.arrS, 0, s, 0, Math.min(this.arrS.length, this.s.length));\n System.arraycopy(this.arr21, 0, this.psfb21, 0, Math.min(this.arr21.length, this.psfb21.length));\n System.arraycopy(this.arr12, 0, this.psfb12, 0, Math.min(this.arr12.length, this.psfb12.length));\n }\n }\n\n /*\n * quantize_pvt source file\n *\n * Copyright (c) 1999-2002 Takehiro Tominaga\n * Copyright (c) 2000-2002 Robert Hegemann\n * Copyright (c) 2001 Naoki Shibata\n * Copyright (c) 2002-2005 Gabriel Bouvigne\n *\n * This library is free software; you can redistribute it and/or\n * modify it under the terms of the GNU Lesser General Public\n * License as published by the Free Software Foundation; either\n * version 2 of the License, or (at your option) any later version.\n *\n * This library is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Library General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this library; if not, write to the\n * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n * Boston, MA 02111-1307, USA.\n */\n\n /* $Id: QuantizePVT.java,v 1.24 2011/05/24 20:48:06 kenchis Exp $ */\n\n\n QuantizePVT.Q_MAX = (256 + 1);\n QuantizePVT.Q_MAX2 = 116;\n QuantizePVT.LARGE_BITS = 100000;\n QuantizePVT.IXMAX_VAL = 8206;\n\n function QuantizePVT() {\n\n var tak = null;\n var rv = null;\n var psy = null;\n\n this.setModules = function (_tk, _rv, _psy) {\n tak = _tk;\n rv = _rv;\n psy = _psy;\n };\n\n function POW20(x) {\n return pow20[x + QuantizePVT.Q_MAX2];\n }\n\n this.IPOW20 = function (x) {\n return ipow20[x];\n }\n\n /**\n * smallest such that 1.0+DBL_EPSILON != 1.0\n */\n var DBL_EPSILON = 2.2204460492503131e-016;\n\n /**\n * ix always <= 8191+15. see count_bits()\n */\n var IXMAX_VAL = QuantizePVT.IXMAX_VAL;\n\n var PRECALC_SIZE = (IXMAX_VAL + 2);\n\n var Q_MAX = QuantizePVT.Q_MAX;\n\n\n /**\n * \n * minimum possible number of\n * -cod_info.global_gain + ((scalefac[] + (cod_info.preflag ? pretab[sfb] : 0))\n * << (cod_info.scalefac_scale + 1)) + cod_info.subblock_gain[cod_info.window[sfb]] * 8;\n *\n * for long block, 0+((15+3)<<2) = 18*4 = 72\n * for short block, 0+(15<<2)+7*8 = 15*4+56 = 116\n *
\n */\n var Q_MAX2 = QuantizePVT.Q_MAX2;\n\n var LARGE_BITS = QuantizePVT.LARGE_BITS;\n\n\n /**\n * Assuming dynamic range=96dB, this value should be 92\n */\n var NSATHSCALE = 100;\n\n /**\n * The following table is used to implement the scalefactor partitioning for\n * MPEG2 as described in section 2.4.3.2 of the IS. The indexing corresponds\n * to the way the tables are presented in the IS:\n *\n * [table_number][row_in_table][column of nr_of_sfb]\n */\n this.nr_of_sfb_block = [\n [[6, 5, 5, 5], [9, 9, 9, 9], [6, 9, 9, 9]],\n [[6, 5, 7, 3], [9, 9, 12, 6], [6, 9, 12, 6]],\n [[11, 10, 0, 0], [18, 18, 0, 0], [15, 18, 0, 0]],\n [[7, 7, 7, 0], [12, 12, 12, 0], [6, 15, 12, 0]],\n [[6, 6, 6, 3], [12, 9, 9, 6], [6, 12, 9, 6]],\n [[8, 8, 5, 0], [15, 12, 9, 0], [6, 18, 9, 0]]];\n\n /**\n * Table B.6: layer3 preemphasis\n */\n var pretab = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,\n 2, 2, 3, 3, 3, 2, 0];\n this.pretab = pretab;\n\n /**\n * Here are MPEG1 Table B.8 and MPEG2 Table B.1 -- Layer III scalefactor\n * bands.
\n * Index into this using a method such as:
\n * idx = fr_ps.header.sampling_frequency + (fr_ps.header.version * 3)\n */\n this.sfBandIndex = [\n // Table B.2.b: 22.05 kHz\n new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,\n 522, 576],\n [0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192]\n , [0, 0, 0, 0, 0, 0, 0] // sfb21 pseudo sub bands\n , [0, 0, 0, 0, 0, 0, 0] // sfb12 pseudo sub bands\n ),\n /* Table B.2.c: 24 kHz */ /* docs: 332. mpg123(broken): 330 */\n new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464,\n 540, 576],\n [0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192]\n , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */\n , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */\n ),\n /* Table B.2.a: 16 kHz */\n new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,\n 522, 576],\n [0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192]\n , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */\n , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */\n ),\n /* Table B.8.b: 44.1 kHz */\n new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418,\n 576],\n [0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192]\n , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */\n , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */\n ),\n /* Table B.8.c: 48 kHz */\n new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384,\n 576],\n [0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192]\n , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */\n , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */\n ),\n /* Table B.8.a: 32 kHz */\n new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550,\n 576],\n [0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192]\n , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */\n , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */\n ),\n /* MPEG-2.5 11.025 kHz */\n new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,\n 522, 576],\n [0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,\n 402 / 3, 522 / 3, 576 / 3]\n , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */\n , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */\n ),\n /* MPEG-2.5 12 kHz */\n new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,\n 522, 576],\n [0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,\n 402 / 3, 522 / 3, 576 / 3]\n , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */\n , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */\n ),\n /* MPEG-2.5 8 kHz */\n new ScaleFac([0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570,\n 572, 574, 576],\n [0 / 3, 24 / 3, 48 / 3, 72 / 3, 108 / 3, 156 / 3, 216 / 3, 288 / 3, 372 / 3, 480 / 3, 486 / 3,\n 492 / 3, 498 / 3, 576 / 3]\n , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */\n , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */\n )\n ];\n\n var pow20 = new_float(Q_MAX + Q_MAX2 + 1);\n var ipow20 = new_float(Q_MAX);\n var pow43 = new_float(PRECALC_SIZE);\n\n var adj43 = new_float(PRECALC_SIZE);\n this.adj43 = adj43;\n\n /**\n * \n * compute the ATH for each scalefactor band cd range: 0..96db\n *\n * Input: 3.3kHz signal 32767 amplitude (3.3kHz is where ATH is smallest =\n * -5db) longblocks: sfb=12 en0/bw=-11db max_en0 = 1.3db shortblocks: sfb=5\n * -9db 0db\n *\n * Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated) longblocks: amp=1\n * sfb=12 en0/bw=-103 db max_en0 = -92db amp=32767 sfb=12 -12 db -1.4db\n *\n * Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated) shortblocks: amp=1\n * sfb=5 en0/bw= -99 -86 amp=32767 sfb=5 -9 db 4db\n *\n *\n * MAX energy of largest wave at 3.3kHz = 1db AVE energy of largest wave at\n * 3.3kHz = -11db Let's take AVE: -11db = maximum signal in sfb=12. Dynamic\n * range of CD: 96db. Therefor energy of smallest audible wave in sfb=12 =\n * -11 - 96 = -107db = ATH at 3.3kHz.\n *\n * ATH formula for this wave: -5db. To adjust to LAME scaling, we need ATH =\n * ATH_formula - 103 (db) ATH = ATH * 2.5e-10 (ener)\n *
\n */\n function ATHmdct(gfp, f) {\n var ath = psy.ATHformula(f, gfp);\n\n ath -= NSATHSCALE;\n\n /* modify the MDCT scaling for the ATH and convert to energy */\n ath = Math.pow(10.0, ath / 10.0 + gfp.ATHlower);\n return ath;\n }\n\n function compute_ath(gfp) {\n var ATH_l = gfp.internal_flags.ATH.l;\n var ATH_psfb21 = gfp.internal_flags.ATH.psfb21;\n var ATH_s = gfp.internal_flags.ATH.s;\n var ATH_psfb12 = gfp.internal_flags.ATH.psfb12;\n var gfc = gfp.internal_flags;\n var samp_freq = gfp.out_samplerate;\n\n for (var sfb = 0; sfb < Encoder.SBMAX_l; sfb++) {\n var start = gfc.scalefac_band.l[sfb];\n var end = gfc.scalefac_band.l[sfb + 1];\n ATH_l[sfb] = Float.MAX_VALUE;\n for (var i = start; i < end; i++) {\n var freq = i * samp_freq / (2 * 576);\n var ATH_f = ATHmdct(gfp, freq);\n /* freq in kHz */\n ATH_l[sfb] = Math.min(ATH_l[sfb], ATH_f);\n }\n }\n\n for (var sfb = 0; sfb < Encoder.PSFB21; sfb++) {\n var start = gfc.scalefac_band.psfb21[sfb];\n var end = gfc.scalefac_band.psfb21[sfb + 1];\n ATH_psfb21[sfb] = Float.MAX_VALUE;\n for (var i = start; i < end; i++) {\n var freq = i * samp_freq / (2 * 576);\n var ATH_f = ATHmdct(gfp, freq);\n /* freq in kHz */\n ATH_psfb21[sfb] = Math.min(ATH_psfb21[sfb], ATH_f);\n }\n }\n\n for (var sfb = 0; sfb < Encoder.SBMAX_s; sfb++) {\n var start = gfc.scalefac_band.s[sfb];\n var end = gfc.scalefac_band.s[sfb + 1];\n ATH_s[sfb] = Float.MAX_VALUE;\n for (var i = start; i < end; i++) {\n var freq = i * samp_freq / (2 * 192);\n var ATH_f = ATHmdct(gfp, freq);\n /* freq in kHz */\n ATH_s[sfb] = Math.min(ATH_s[sfb], ATH_f);\n }\n ATH_s[sfb] *= (gfc.scalefac_band.s[sfb + 1] - gfc.scalefac_band.s[sfb]);\n }\n\n for (var sfb = 0; sfb < Encoder.PSFB12; sfb++) {\n var start = gfc.scalefac_band.psfb12[sfb];\n var end = gfc.scalefac_band.psfb12[sfb + 1];\n ATH_psfb12[sfb] = Float.MAX_VALUE;\n for (var i = start; i < end; i++) {\n var freq = i * samp_freq / (2 * 192);\n var ATH_f = ATHmdct(gfp, freq);\n /* freq in kHz */\n ATH_psfb12[sfb] = Math.min(ATH_psfb12[sfb], ATH_f);\n }\n /* not sure about the following */\n ATH_psfb12[sfb] *= (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12]);\n }\n\n /*\n * no-ATH mode: reduce ATH to -200 dB\n */\n if (gfp.noATH) {\n for (var sfb = 0; sfb < Encoder.SBMAX_l; sfb++) {\n ATH_l[sfb] = 1E-20;\n }\n for (var sfb = 0; sfb < Encoder.PSFB21; sfb++) {\n ATH_psfb21[sfb] = 1E-20;\n }\n for (var sfb = 0; sfb < Encoder.SBMAX_s; sfb++) {\n ATH_s[sfb] = 1E-20;\n }\n for (var sfb = 0; sfb < Encoder.PSFB12; sfb++) {\n ATH_psfb12[sfb] = 1E-20;\n }\n }\n\n /*\n * work in progress, don't rely on it too much\n */\n gfc.ATH.floor = 10. * Math.log10(ATHmdct(gfp, -1.));\n }\n\n /**\n * initialization for iteration_loop\n */\n this.iteration_init = function (gfp) {\n var gfc = gfp.internal_flags;\n var l3_side = gfc.l3_side;\n var i;\n\n if (gfc.iteration_init_init == 0) {\n gfc.iteration_init_init = 1;\n\n l3_side.main_data_begin = 0;\n compute_ath(gfp);\n\n pow43[0] = 0.0;\n for (i = 1; i < PRECALC_SIZE; i++)\n pow43[i] = Math.pow(i, 4.0 / 3.0);\n\n for (i = 0; i < PRECALC_SIZE - 1; i++)\n adj43[i] = ((i + 1) - Math.pow(\n 0.5 * (pow43[i] + pow43[i + 1]), 0.75));\n adj43[i] = 0.5;\n\n for (i = 0; i < Q_MAX; i++)\n ipow20[i] = Math.pow(2.0, (i - 210) * -0.1875);\n for (i = 0; i <= Q_MAX + Q_MAX2; i++)\n pow20[i] = Math.pow(2.0, (i - 210 - Q_MAX2) * 0.25);\n\n tak.huffman_init(gfc);\n\n {\n var bass, alto, treble, sfb21;\n\n i = (gfp.exp_nspsytune >> 2) & 63;\n if (i >= 32)\n i -= 64;\n bass = Math.pow(10, i / 4.0 / 10.0);\n\n i = (gfp.exp_nspsytune >> 8) & 63;\n if (i >= 32)\n i -= 64;\n alto = Math.pow(10, i / 4.0 / 10.0);\n\n i = (gfp.exp_nspsytune >> 14) & 63;\n if (i >= 32)\n i -= 64;\n treble = Math.pow(10, i / 4.0 / 10.0);\n\n /*\n * to be compatible with Naoki's original code, the next 6 bits\n * define only the amount of changing treble for sfb21\n */\n i = (gfp.exp_nspsytune >> 20) & 63;\n if (i >= 32)\n i -= 64;\n sfb21 = treble * Math.pow(10, i / 4.0 / 10.0);\n for (i = 0; i < Encoder.SBMAX_l; i++) {\n var f;\n if (i <= 6)\n f = bass;\n else if (i <= 13)\n f = alto;\n else if (i <= 20)\n f = treble;\n else\n f = sfb21;\n\n gfc.nsPsy.longfact[i] = f;\n }\n for (i = 0; i < Encoder.SBMAX_s; i++) {\n var f;\n if (i <= 5)\n f = bass;\n else if (i <= 10)\n f = alto;\n else if (i <= 11)\n f = treble;\n else\n f = sfb21;\n\n gfc.nsPsy.shortfact[i] = f;\n }\n }\n }\n }\n\n /**\n * allocate bits among 2 channels based on PE
\n * mt 6/99
\n * bugfixes rh 8/01: often allocated more than the allowed 4095 bits\n */\n this.on_pe = function (gfp, pe,\n targ_bits, mean_bits, gr, cbr) {\n var gfc = gfp.internal_flags;\n var tbits = 0, bits;\n var add_bits = new_int(2);\n var ch;\n\n /* allocate targ_bits for granule */\n var mb = new MeanBits(tbits);\n var extra_bits = rv.ResvMaxBits(gfp, mean_bits, mb, cbr);\n tbits = mb.bits;\n /* maximum allowed bits for this granule */\n var max_bits = tbits + extra_bits;\n if (max_bits > LameInternalFlags.MAX_BITS_PER_GRANULE) {\n // hard limit per granule\n max_bits = LameInternalFlags.MAX_BITS_PER_GRANULE;\n }\n for (bits = 0, ch = 0; ch < gfc.channels_out; ++ch) {\n /******************************************************************\n * allocate bits for each channel\n ******************************************************************/\n targ_bits[ch] = Math.min(LameInternalFlags.MAX_BITS_PER_CHANNEL,\n tbits / gfc.channels_out);\n\n add_bits[ch] = 0 | (targ_bits[ch] * pe[gr][ch] / 700.0 - targ_bits[ch]);\n\n /* at most increase bits by 1.5*average */\n if (add_bits[ch] > mean_bits * 3 / 4)\n add_bits[ch] = mean_bits * 3 / 4;\n if (add_bits[ch] < 0)\n add_bits[ch] = 0;\n\n if (add_bits[ch] + targ_bits[ch] > LameInternalFlags.MAX_BITS_PER_CHANNEL)\n add_bits[ch] = Math.max(0,\n LameInternalFlags.MAX_BITS_PER_CHANNEL - targ_bits[ch]);\n\n bits += add_bits[ch];\n }\n if (bits > extra_bits) {\n for (ch = 0; ch < gfc.channels_out; ++ch) {\n add_bits[ch] = extra_bits * add_bits[ch] / bits;\n }\n }\n\n for (ch = 0; ch < gfc.channels_out; ++ch) {\n targ_bits[ch] += add_bits[ch];\n extra_bits -= add_bits[ch];\n }\n\n for (bits = 0, ch = 0; ch < gfc.channels_out; ++ch) {\n bits += targ_bits[ch];\n }\n if (bits > LameInternalFlags.MAX_BITS_PER_GRANULE) {\n var sum = 0;\n for (ch = 0; ch < gfc.channels_out; ++ch) {\n targ_bits[ch] *= LameInternalFlags.MAX_BITS_PER_GRANULE;\n targ_bits[ch] /= bits;\n sum += targ_bits[ch];\n }\n }\n\n return max_bits;\n }\n\n this.reduce_side = function (targ_bits, ms_ener_ratio, mean_bits, max_bits) {\n\n /*\n * ms_ener_ratio = 0: allocate 66/33 mid/side fac=.33 ms_ener_ratio =.5:\n * allocate 50/50 mid/side fac= 0\n */\n /* 75/25 split is fac=.5 */\n var fac = .33 * (.5 - ms_ener_ratio) / .5;\n if (fac < 0)\n fac = 0;\n if (fac > .5)\n fac = .5;\n\n /* number of bits to move from side channel to mid channel */\n /* move_bits = fac*targ_bits[1]; */\n var move_bits = 0 | (fac * .5 * (targ_bits[0] + targ_bits[1]));\n\n if (move_bits > LameInternalFlags.MAX_BITS_PER_CHANNEL - targ_bits[0]) {\n move_bits = LameInternalFlags.MAX_BITS_PER_CHANNEL - targ_bits[0];\n }\n if (move_bits < 0)\n move_bits = 0;\n\n if (targ_bits[1] >= 125) {\n /* dont reduce side channel below 125 bits */\n if (targ_bits[1] - move_bits > 125) {\n\n /* if mid channel already has 2x more than average, dont bother */\n /* mean_bits = bits per granule (for both channels) */\n if (targ_bits[0] < mean_bits)\n targ_bits[0] += move_bits;\n targ_bits[1] -= move_bits;\n } else {\n targ_bits[0] += targ_bits[1] - 125;\n targ_bits[1] = 125;\n }\n }\n\n move_bits = targ_bits[0] + targ_bits[1];\n if (move_bits > max_bits) {\n targ_bits[0] = (max_bits * targ_bits[0]) / move_bits;\n targ_bits[1] = (max_bits * targ_bits[1]) / move_bits;\n }\n };\n\n /**\n * Robert Hegemann 2001-04-27:\n * this adjusts the ATH, keeping the original noise floor\n * affects the higher frequencies more than the lower ones\n */\n this.athAdjust = function (a, x, athFloor) {\n /*\n * work in progress\n */\n var o = 90.30873362;\n var p = 94.82444863;\n var u = Util.FAST_LOG10_X(x, 10.0);\n var v = a * a;\n var w = 0.0;\n u -= athFloor;\n /* undo scaling */\n if (v > 1E-20)\n w = 1. + Util.FAST_LOG10_X(v, 10.0 / o);\n if (w < 0)\n w = 0.;\n u *= w;\n u += athFloor + o - p;\n /* redo scaling */\n\n return Math.pow(10., 0.1 * u);\n };\n\n /**\n * Calculate the allowed distortion for each scalefactor band, as determined\n * by the psychoacoustic model. xmin(sb) = ratio(sb) * en(sb) / bw(sb)\n *\n * returns number of sfb's with energy > ATH\n */\n this.calc_xmin = function (gfp, ratio, cod_info, pxmin) {\n var pxminPos = 0;\n var gfc = gfp.internal_flags;\n var gsfb, j = 0, ath_over = 0;\n var ATH = gfc.ATH;\n var xr = cod_info.xr;\n var enable_athaa_fix = (gfp.VBR == VbrMode.vbr_mtrh) ? 1 : 0;\n var masking_lower = gfc.masking_lower;\n\n if (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) {\n /* was already done in PSY-Model */\n masking_lower = 1.0;\n }\n\n for (gsfb = 0; gsfb < cod_info.psy_lmax; gsfb++) {\n var en0, xmin;\n var rh1, rh2;\n var width, l;\n\n if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)\n xmin = athAdjust(ATH.adjust, ATH.l[gsfb], ATH.floor);\n else\n xmin = ATH.adjust * ATH.l[gsfb];\n\n width = cod_info.width[gsfb];\n rh1 = xmin / width;\n rh2 = DBL_EPSILON;\n l = width >> 1;\n en0 = 0.0;\n do {\n var xa, xb;\n xa = xr[j] * xr[j];\n en0 += xa;\n rh2 += (xa < rh1) ? xa : rh1;\n j++;\n xb = xr[j] * xr[j];\n en0 += xb;\n rh2 += (xb < rh1) ? xb : rh1;\n j++;\n } while (--l > 0);\n if (en0 > xmin)\n ath_over++;\n\n if (gsfb == Encoder.SBPSY_l) {\n var x = xmin * gfc.nsPsy.longfact[gsfb];\n if (rh2 < x) {\n rh2 = x;\n }\n }\n if (enable_athaa_fix != 0) {\n xmin = rh2;\n }\n if (!gfp.ATHonly) {\n var e = ratio.en.l[gsfb];\n if (e > 0.0) {\n var x;\n x = en0 * ratio.thm.l[gsfb] * masking_lower / e;\n if (enable_athaa_fix != 0)\n x *= gfc.nsPsy.longfact[gsfb];\n if (xmin < x)\n xmin = x;\n }\n }\n if (enable_athaa_fix != 0)\n pxmin[pxminPos++] = xmin;\n else\n pxmin[pxminPos++] = xmin * gfc.nsPsy.longfact[gsfb];\n }\n /* end of long block loop */\n\n /* use this function to determine the highest non-zero coeff */\n var max_nonzero = 575;\n if (cod_info.block_type != Encoder.SHORT_TYPE) {\n // NORM, START or STOP type, but not SHORT\n var k = 576;\n while (k-- != 0 && BitStream.EQ(xr[k], 0)) {\n max_nonzero = k;\n }\n }\n cod_info.max_nonzero_coeff = max_nonzero;\n\n for (var sfb = cod_info.sfb_smin; gsfb < cod_info.psymax; sfb++, gsfb += 3) {\n var width, b;\n var tmpATH;\n if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)\n tmpATH = athAdjust(ATH.adjust, ATH.s[sfb], ATH.floor);\n else\n tmpATH = ATH.adjust * ATH.s[sfb];\n\n width = cod_info.width[gsfb];\n for (b = 0; b < 3; b++) {\n var en0 = 0.0, xmin;\n var rh1, rh2;\n var l = width >> 1;\n\n rh1 = tmpATH / width;\n rh2 = DBL_EPSILON;\n do {\n var xa, xb;\n xa = xr[j] * xr[j];\n en0 += xa;\n rh2 += (xa < rh1) ? xa : rh1;\n j++;\n xb = xr[j] * xr[j];\n en0 += xb;\n rh2 += (xb < rh1) ? xb : rh1;\n j++;\n } while (--l > 0);\n if (en0 > tmpATH)\n ath_over++;\n if (sfb == Encoder.SBPSY_s) {\n var x = tmpATH * gfc.nsPsy.shortfact[sfb];\n if (rh2 < x) {\n rh2 = x;\n }\n }\n if (enable_athaa_fix != 0)\n xmin = rh2;\n else\n xmin = tmpATH;\n\n if (!gfp.ATHonly && !gfp.ATHshort) {\n var e = ratio.en.s[sfb][b];\n if (e > 0.0) {\n var x;\n x = en0 * ratio.thm.s[sfb][b] * masking_lower / e;\n if (enable_athaa_fix != 0)\n x *= gfc.nsPsy.shortfact[sfb];\n if (xmin < x)\n xmin = x;\n }\n }\n if (enable_athaa_fix != 0)\n pxmin[pxminPos++] = xmin;\n else\n pxmin[pxminPos++] = xmin * gfc.nsPsy.shortfact[sfb];\n }\n /* b */\n if (gfp.useTemporal) {\n if (pxmin[pxminPos - 3] > pxmin[pxminPos - 3 + 1])\n pxmin[pxminPos - 3 + 1] += (pxmin[pxminPos - 3] - pxmin[pxminPos - 3 + 1])\n * gfc.decay;\n if (pxmin[pxminPos - 3 + 1] > pxmin[pxminPos - 3 + 2])\n pxmin[pxminPos - 3 + 2] += (pxmin[pxminPos - 3 + 1] - pxmin[pxminPos - 3 + 2])\n * gfc.decay;\n }\n }\n /* end of short block sfb loop */\n\n return ath_over;\n };\n\n function StartLine(j) {\n this.s = j;\n }\n\n this.calc_noise_core = function (cod_info, startline, l, step) {\n var noise = 0;\n var j = startline.s;\n var ix = cod_info.l3_enc;\n\n if (j > cod_info.count1) {\n while ((l--) != 0) {\n var temp;\n temp = cod_info.xr[j];\n j++;\n noise += temp * temp;\n temp = cod_info.xr[j];\n j++;\n noise += temp * temp;\n }\n } else if (j > cod_info.big_values) {\n var ix01 = new_float(2);\n ix01[0] = 0;\n ix01[1] = step;\n while ((l--) != 0) {\n var temp;\n temp = Math.abs(cod_info.xr[j]) - ix01[ix[j]];\n j++;\n noise += temp * temp;\n temp = Math.abs(cod_info.xr[j]) - ix01[ix[j]];\n j++;\n noise += temp * temp;\n }\n } else {\n while ((l--) != 0) {\n var temp;\n temp = Math.abs(cod_info.xr[j]) - pow43[ix[j]] * step;\n j++;\n noise += temp * temp;\n temp = Math.abs(cod_info.xr[j]) - pow43[ix[j]] * step;\n j++;\n noise += temp * temp;\n }\n }\n\n startline.s = j;\n return noise;\n }\n\n /**\n * \n * -oo dB => -1.00\n * - 6 dB => -0.97\n * - 3 dB => -0.80\n * - 2 dB => -0.64\n * - 1 dB => -0.38\n * 0 dB => 0.00\n * + 1 dB => +0.49\n * + 2 dB => +1.06\n * + 3 dB => +1.68\n * + 6 dB => +3.69\n * +10 dB => +6.45\n *
\n */\n this.calc_noise = function (cod_info, l3_xmin, distort, res, prev_noise) {\n var distortPos = 0;\n var l3_xminPos = 0;\n var sfb, l, over = 0;\n var over_noise_db = 0;\n /* 0 dB relative to masking */\n var tot_noise_db = 0;\n /* -200 dB relative to masking */\n var max_noise = -20.0;\n var j = 0;\n var scalefac = cod_info.scalefac;\n var scalefacPos = 0;\n\n res.over_SSD = 0;\n\n for (sfb = 0; sfb < cod_info.psymax; sfb++) {\n var s = cod_info.global_gain\n - (((scalefac[scalefacPos++]) + (cod_info.preflag != 0 ? pretab[sfb]\n : 0)) << (cod_info.scalefac_scale + 1))\n - cod_info.subblock_gain[cod_info.window[sfb]] * 8;\n var noise = 0.0;\n\n if (prev_noise != null && (prev_noise.step[sfb] == s)) {\n\n /* use previously computed values */\n noise = prev_noise.noise[sfb];\n j += cod_info.width[sfb];\n distort[distortPos++] = noise / l3_xmin[l3_xminPos++];\n\n noise = prev_noise.noise_log[sfb];\n\n } else {\n var step = POW20(s);\n l = cod_info.width[sfb] >> 1;\n\n if ((j + cod_info.width[sfb]) > cod_info.max_nonzero_coeff) {\n var usefullsize;\n usefullsize = cod_info.max_nonzero_coeff - j + 1;\n\n if (usefullsize > 0)\n l = usefullsize >> 1;\n else\n l = 0;\n }\n\n var sl = new StartLine(j);\n noise = this.calc_noise_core(cod_info, sl, l, step);\n j = sl.s;\n\n if (prev_noise != null) {\n /* save noise values */\n prev_noise.step[sfb] = s;\n prev_noise.noise[sfb] = noise;\n }\n\n noise = distort[distortPos++] = noise / l3_xmin[l3_xminPos++];\n\n /* multiplying here is adding in dB, but can overflow */\n noise = Util.FAST_LOG10(Math.max(noise, 1E-20));\n\n if (prev_noise != null) {\n /* save noise values */\n prev_noise.noise_log[sfb] = noise;\n }\n }\n\n if (prev_noise != null) {\n /* save noise values */\n prev_noise.global_gain = cod_info.global_gain;\n }\n\n tot_noise_db += noise;\n\n if (noise > 0.0) {\n var tmp;\n\n tmp = Math.max(0 | (noise * 10 + .5), 1);\n res.over_SSD += tmp * tmp;\n\n over++;\n /* multiplying here is adding in dB -but can overflow */\n /* over_noise *= noise; */\n over_noise_db += noise;\n }\n max_noise = Math.max(max_noise, noise);\n\n }\n\n res.over_count = over;\n res.tot_noise = tot_noise_db;\n res.over_noise = over_noise_db;\n res.max_noise = max_noise;\n\n return over;\n }\n\n /**\n * updates plotting data\n *\n * Mark Taylor 2000-??-??\n *\n * Robert Hegemann: moved noise/distortion calc into it\n */\n this.set_pinfo = function (gfp, cod_info, ratio, gr, ch) {\n var gfc = gfp.internal_flags;\n var sfb, sfb2;\n var l;\n var en0, en1;\n var ifqstep = (cod_info.scalefac_scale == 0) ? .5 : 1.0;\n var scalefac = cod_info.scalefac;\n\n var l3_xmin = new_float(L3Side.SFBMAX);\n var xfsf = new_float(L3Side.SFBMAX);\n var noise = new CalcNoiseResult();\n\n calc_xmin(gfp, ratio, cod_info, l3_xmin);\n calc_noise(cod_info, l3_xmin, xfsf, noise, null);\n\n var j = 0;\n sfb2 = cod_info.sfb_lmax;\n if (cod_info.block_type != Encoder.SHORT_TYPE\n && 0 == cod_info.mixed_block_flag)\n sfb2 = 22;\n for (sfb = 0; sfb < sfb2; sfb++) {\n var start = gfc.scalefac_band.l[sfb];\n var end = gfc.scalefac_band.l[sfb + 1];\n var bw = end - start;\n for (en0 = 0.0; j < end; j++)\n en0 += cod_info.xr[j] * cod_info.xr[j];\n en0 /= bw;\n /* convert to MDCT units */\n /* scaling so it shows up on FFT plot */\n en1 = 1e15;\n gfc.pinfo.en[gr][ch][sfb] = en1 * en0;\n gfc.pinfo.xfsf[gr][ch][sfb] = en1 * l3_xmin[sfb] * xfsf[sfb] / bw;\n\n if (ratio.en.l[sfb] > 0 && !gfp.ATHonly)\n en0 = en0 / ratio.en.l[sfb];\n else\n en0 = 0.0;\n\n gfc.pinfo.thr[gr][ch][sfb] = en1\n * Math.max(en0 * ratio.thm.l[sfb], gfc.ATH.l[sfb]);\n\n /* there is no scalefactor bands >= SBPSY_l */\n gfc.pinfo.LAMEsfb[gr][ch][sfb] = 0;\n if (cod_info.preflag != 0 && sfb >= 11)\n gfc.pinfo.LAMEsfb[gr][ch][sfb] = -ifqstep * pretab[sfb];\n\n if (sfb < Encoder.SBPSY_l) {\n /* scfsi should be decoded by caller side */\n gfc.pinfo.LAMEsfb[gr][ch][sfb] -= ifqstep * scalefac[sfb];\n }\n }\n /* for sfb */\n\n if (cod_info.block_type == Encoder.SHORT_TYPE) {\n sfb2 = sfb;\n for (sfb = cod_info.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {\n var start = gfc.scalefac_band.s[sfb];\n var end = gfc.scalefac_band.s[sfb + 1];\n var bw = end - start;\n for (var i = 0; i < 3; i++) {\n for (en0 = 0.0, l = start; l < end; l++) {\n en0 += cod_info.xr[j] * cod_info.xr[j];\n j++;\n }\n en0 = Math.max(en0 / bw, 1e-20);\n /* convert to MDCT units */\n /* scaling so it shows up on FFT plot */\n en1 = 1e15;\n\n gfc.pinfo.en_s[gr][ch][3 * sfb + i] = en1 * en0;\n gfc.pinfo.xfsf_s[gr][ch][3 * sfb + i] = en1 * l3_xmin[sfb2]\n * xfsf[sfb2] / bw;\n if (ratio.en.s[sfb][i] > 0)\n en0 = en0 / ratio.en.s[sfb][i];\n else\n en0 = 0.0;\n if (gfp.ATHonly || gfp.ATHshort)\n en0 = 0;\n\n gfc.pinfo.thr_s[gr][ch][3 * sfb + i] = en1\n * Math.max(en0 * ratio.thm.s[sfb][i],\n gfc.ATH.s[sfb]);\n\n /* there is no scalefactor bands >= SBPSY_s */\n gfc.pinfo.LAMEsfb_s[gr][ch][3 * sfb + i] = -2.0\n * cod_info.subblock_gain[i];\n if (sfb < Encoder.SBPSY_s) {\n gfc.pinfo.LAMEsfb_s[gr][ch][3 * sfb + i] -= ifqstep\n * scalefac[sfb2];\n }\n sfb2++;\n }\n }\n }\n /* block type short */\n gfc.pinfo.LAMEqss[gr][ch] = cod_info.global_gain;\n gfc.pinfo.LAMEmainbits[gr][ch] = cod_info.part2_3_length\n + cod_info.part2_length;\n gfc.pinfo.LAMEsfbits[gr][ch] = cod_info.part2_length;\n\n gfc.pinfo.over[gr][ch] = noise.over_count;\n gfc.pinfo.max_noise[gr][ch] = noise.max_noise * 10.0;\n gfc.pinfo.over_noise[gr][ch] = noise.over_noise * 10.0;\n gfc.pinfo.tot_noise[gr][ch] = noise.tot_noise * 10.0;\n gfc.pinfo.over_SSD[gr][ch] = noise.over_SSD;\n }\n\n /**\n * updates plotting data for a whole frame\n *\n * Robert Hegemann 2000-10-21\n */\n function set_frame_pinfo(gfp, ratio) {\n var gfc = gfp.internal_flags;\n\n gfc.masking_lower = 1.0;\n\n /*\n * for every granule and channel patch l3_enc and set info\n */\n for (var gr = 0; gr < gfc.mode_gr; gr++) {\n for (var ch = 0; ch < gfc.channels_out; ch++) {\n var cod_info = gfc.l3_side.tt[gr][ch];\n var scalefac_sav = new_int(L3Side.SFBMAX);\n System.arraycopy(cod_info.scalefac, 0, scalefac_sav, 0,\n scalefac_sav.length);\n\n /*\n * reconstruct the scalefactors in case SCFSI was used\n */\n if (gr == 1) {\n var sfb;\n for (sfb = 0; sfb < cod_info.sfb_lmax; sfb++) {\n if (cod_info.scalefac[sfb] < 0) /* scfsi */\n cod_info.scalefac[sfb] = gfc.l3_side.tt[0][ch].scalefac[sfb];\n }\n }\n\n set_pinfo(gfp, cod_info, ratio[gr][ch], gr, ch);\n System.arraycopy(scalefac_sav, 0, cod_info.scalefac, 0,\n scalefac_sav.length);\n }\n /* for ch */\n }\n /* for gr */\n }\n\n }\n\n\n function CalcNoiseData() {\n this.global_gain = 0;\n this.sfb_count1 = 0;\n this.step = new_int(39);\n this.noise = new_float(39);\n this.noise_log = new_float(39);\n }\n\n//package mp3;\n\n\n function GrInfo() {\n //float xr[] = new float[576];\n this.xr = new_float(576);\n //int l3_enc[] = new int[576];\n this.l3_enc = new_int(576);\n //int scalefac[] = new int[L3Side.SFBMAX];\n this.scalefac = new_int(L3Side.SFBMAX);\n this.xrpow_max = 0.;\n\n this.part2_3_length = 0;\n this.big_values = 0;\n this.count1 = 0;\n this.global_gain = 0;\n this.scalefac_compress = 0;\n this.block_type = 0;\n this.mixed_block_flag = 0;\n this.table_select = new_int(3);\n this.subblock_gain = new_int(3 + 1);\n this.region0_count = 0;\n this.region1_count = 0;\n this.preflag = 0;\n this.scalefac_scale = 0;\n this.count1table_select = 0;\n\n this.part2_length = 0;\n this.sfb_lmax = 0;\n this.sfb_smin = 0;\n this.psy_lmax = 0;\n this.sfbmax = 0;\n this.psymax = 0;\n this.sfbdivide = 0;\n this.width = new_int(L3Side.SFBMAX);\n this.window = new_int(L3Side.SFBMAX);\n this.count1bits = 0;\n /**\n * added for LSF\n */\n this.sfb_partition_table = null;\n this.slen = new_int(4);\n\n this.max_nonzero_coeff = 0;\n\n var self = this;\n function clone_int(array) {\n return new Int32Array(array);\n }\n function clone_float(array) {\n return new Float32Array(array);\n }\n this.assign = function (other) {\n self.xr = clone_float(other.xr); //.slice(0); //clone();\n self.l3_enc = clone_int(other.l3_enc); //.slice(0); //clone();\n self.scalefac = clone_int(other.scalefac);//.slice(0); //clone();\n self.xrpow_max = other.xrpow_max;\n\n self.part2_3_length = other.part2_3_length;\n self.big_values = other.big_values;\n self.count1 = other.count1;\n self.global_gain = other.global_gain;\n self.scalefac_compress = other.scalefac_compress;\n self.block_type = other.block_type;\n self.mixed_block_flag = other.mixed_block_flag;\n self.table_select = clone_int(other.table_select);//.slice(0); //clone();\n self.subblock_gain = clone_int(other.subblock_gain); //.slice(0); //.clone();\n self.region0_count = other.region0_count;\n self.region1_count = other.region1_count;\n self.preflag = other.preflag;\n self.scalefac_scale = other.scalefac_scale;\n self.count1table_select = other.count1table_select;\n\n self.part2_length = other.part2_length;\n self.sfb_lmax = other.sfb_lmax;\n self.sfb_smin = other.sfb_smin;\n self.psy_lmax = other.psy_lmax;\n self.sfbmax = other.sfbmax;\n self.psymax = other.psymax;\n self.sfbdivide = other.sfbdivide;\n self.width = clone_int(other.width); //.slice(0); //.clone();\n self.window = clone_int(other.window); //.slice(0); //.clone();\n self.count1bits = other.count1bits;\n\n self.sfb_partition_table = other.sfb_partition_table.slice(0); //.clone();\n self.slen = clone_int(other.slen); //.slice(0); //.clone();\n self.max_nonzero_coeff = other.max_nonzero_coeff;\n }\n }\n\n\n var L3Side = {};\n\n\n /**\n * max scalefactor band, max(SBMAX_l, SBMAX_s*3, (SBMAX_s-3)*3+8)\n */\n L3Side.SFBMAX = (Encoder.SBMAX_s * 3);\n\n /*\n * MP3 quantization\n *\n * Copyright (c) 1999-2000 Mark Taylor\n * Copyright (c) 1999-2003 Takehiro Tominaga\n * Copyright (c) 2000-2007 Robert Hegemann\n * Copyright (c) 2001-2005 Gabriel Bouvigne\n *\n * This library is free software; you can redistribute it and/or\n * modify it under the terms of the GNU Lesser General Public\n * License as published by the Free Software Foundation; either\n * version 2 of the License, or (at your option) any later version.\n *\n * This library is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Library General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this library; if not, write to the\n * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n * Boston, MA 02111-1307, USA.\n */\n\n /* $Id: Quantize.java,v 1.24 2011/05/24 20:48:06 kenchis Exp $ */\n\n//package mp3;\n\n//import java.util.Arrays;\n\n\n function Quantize() {\n var bs;\n this.rv = null;\n var rv;\n this.qupvt = null;\n var qupvt;\n\n var vbr = new VBRQuantize();\n var tk;\n\n this.setModules = function (_bs, _rv, _qupvt, _tk) {\n bs = _bs;\n rv = _rv;\n this.rv = _rv;\n qupvt = _qupvt;\n this.qupvt = _qupvt;\n tk = _tk;\n vbr.setModules(qupvt, tk);\n }\n\n /**\n * convert from L/R <. Mid/Side\n */\n this.ms_convert = function (l3_side, gr) {\n for (var i = 0; i < 576; ++i) {\n var l = l3_side.tt[gr][0].xr[i];\n var r = l3_side.tt[gr][1].xr[i];\n l3_side.tt[gr][0].xr[i] = (l + r) * (Util.SQRT2 * 0.5);\n l3_side.tt[gr][1].xr[i] = (l - r) * (Util.SQRT2 * 0.5);\n }\n };\n\n /**\n * mt 6/99\n *\n * initializes cod_info, scalefac and xrpow\n *\n * returns 0 if all energies in xr are zero, else 1\n */\n function init_xrpow_core(cod_info, xrpow, upper, sum) {\n sum = 0;\n for (var i = 0; i <= upper; ++i) {\n var tmp = Math.abs(cod_info.xr[i]);\n sum += tmp;\n xrpow[i] = Math.sqrt(tmp * Math.sqrt(tmp));\n\n if (xrpow[i] > cod_info.xrpow_max)\n cod_info.xrpow_max = xrpow[i];\n }\n return sum;\n }\n\n this.init_xrpow = function (gfc, cod_info, xrpow) {\n var sum = 0;\n var upper = 0 | cod_info.max_nonzero_coeff;\n\n cod_info.xrpow_max = 0;\n\n /*\n * check if there is some energy we have to quantize and calculate xrpow\n * matching our fresh scalefactors\n */\n\n Arrays.fill(xrpow, upper, 576, 0);\n\n sum = init_xrpow_core(cod_info, xrpow, upper, sum);\n\n /*\n * return 1 if we have something to quantize, else 0\n */\n if (sum > 1E-20) {\n var j = 0;\n if ((gfc.substep_shaping & 2) != 0)\n j = 1;\n\n for (var i = 0; i < cod_info.psymax; i++)\n gfc.pseudohalf[i] = j;\n\n return true;\n }\n\n Arrays.fill(cod_info.l3_enc, 0, 576, 0);\n return false;\n }\n\n /**\n * Gabriel Bouvigne feb/apr 2003
\n * Analog silence detection in partitionned sfb21 or sfb12 for short blocks\n *\n * From top to bottom of sfb, changes to 0 coeffs which are below ath. It\n * stops on the first coeff higher than ath.\n */\n function psfb21_analogsilence(gfc, cod_info) {\n var ath = gfc.ATH;\n var xr = cod_info.xr;\n\n if (cod_info.block_type != Encoder.SHORT_TYPE) {\n /* NORM, START or STOP type, but not SHORT blocks */\n var stop = false;\n for (var gsfb = Encoder.PSFB21 - 1; gsfb >= 0 && !stop; gsfb--) {\n var start = gfc.scalefac_band.psfb21[gsfb];\n var end = gfc.scalefac_band.psfb21[gsfb + 1];\n var ath21 = qupvt.athAdjust(ath.adjust, ath.psfb21[gsfb],\n ath.floor);\n\n if (gfc.nsPsy.longfact[21] > 1e-12)\n ath21 *= gfc.nsPsy.longfact[21];\n\n for (var j = end - 1; j >= start; j--) {\n if (Math.abs(xr[j]) < ath21)\n xr[j] = 0;\n else {\n stop = true;\n break;\n }\n }\n }\n } else {\n /* note: short blocks coeffs are reordered */\n for (var block = 0; block < 3; block++) {\n var stop = false;\n for (var gsfb = Encoder.PSFB12 - 1; gsfb >= 0 && !stop; gsfb--) {\n var start = gfc.scalefac_band.s[12]\n * 3\n + (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12])\n * block\n + (gfc.scalefac_band.psfb12[gsfb] - gfc.scalefac_band.psfb12[0]);\n var end = start\n + (gfc.scalefac_band.psfb12[gsfb + 1] - gfc.scalefac_band.psfb12[gsfb]);\n var ath12 = qupvt.athAdjust(ath.adjust, ath.psfb12[gsfb],\n ath.floor);\n\n if (gfc.nsPsy.shortfact[12] > 1e-12)\n ath12 *= gfc.nsPsy.shortfact[12];\n\n for (var j = end - 1; j >= start; j--) {\n if (Math.abs(xr[j]) < ath12)\n xr[j] = 0;\n else {\n stop = true;\n break;\n }\n }\n }\n }\n }\n\n }\n\n this.init_outer_loop = function (gfc, cod_info) {\n /*\n * initialize fresh cod_info\n */\n cod_info.part2_3_length = 0;\n cod_info.big_values = 0;\n cod_info.count1 = 0;\n cod_info.global_gain = 210;\n cod_info.scalefac_compress = 0;\n /* mixed_block_flag, block_type was set in psymodel.c */\n cod_info.table_select[0] = 0;\n cod_info.table_select[1] = 0;\n cod_info.table_select[2] = 0;\n cod_info.subblock_gain[0] = 0;\n cod_info.subblock_gain[1] = 0;\n cod_info.subblock_gain[2] = 0;\n cod_info.subblock_gain[3] = 0;\n /* this one is always 0 */\n cod_info.region0_count = 0;\n cod_info.region1_count = 0;\n cod_info.preflag = 0;\n cod_info.scalefac_scale = 0;\n cod_info.count1table_select = 0;\n cod_info.part2_length = 0;\n cod_info.sfb_lmax = Encoder.SBPSY_l;\n cod_info.sfb_smin = Encoder.SBPSY_s;\n cod_info.psy_lmax = gfc.sfb21_extra ? Encoder.SBMAX_l : Encoder.SBPSY_l;\n cod_info.psymax = cod_info.psy_lmax;\n cod_info.sfbmax = cod_info.sfb_lmax;\n cod_info.sfbdivide = 11;\n for (var sfb = 0; sfb < Encoder.SBMAX_l; sfb++) {\n cod_info.width[sfb] = gfc.scalefac_band.l[sfb + 1]\n - gfc.scalefac_band.l[sfb];\n /* which is always 0. */\n cod_info.window[sfb] = 3;\n }\n if (cod_info.block_type == Encoder.SHORT_TYPE) {\n var ixwork = new_float(576);\n\n cod_info.sfb_smin = 0;\n cod_info.sfb_lmax = 0;\n if (cod_info.mixed_block_flag != 0) {\n /*\n * MPEG-1: sfbs 0-7 long block, 3-12 short blocks MPEG-2(.5):\n * sfbs 0-5 long block, 3-12 short blocks\n */\n cod_info.sfb_smin = 3;\n cod_info.sfb_lmax = gfc.mode_gr * 2 + 4;\n }\n cod_info.psymax = cod_info.sfb_lmax\n + 3\n * ((gfc.sfb21_extra ? Encoder.SBMAX_s : Encoder.SBPSY_s) - cod_info.sfb_smin);\n cod_info.sfbmax = cod_info.sfb_lmax + 3\n * (Encoder.SBPSY_s - cod_info.sfb_smin);\n cod_info.sfbdivide = cod_info.sfbmax - 18;\n cod_info.psy_lmax = cod_info.sfb_lmax;\n /* re-order the short blocks, for more efficient encoding below */\n /* By Takehiro TOMINAGA */\n /*\n * Within each scalefactor band, data is given for successive time\n * windows, beginning with window 0 and ending with window 2. Within\n * each window, the quantized values are then arranged in order of\n * increasing frequency...\n */\n var ix = gfc.scalefac_band.l[cod_info.sfb_lmax];\n System.arraycopy(cod_info.xr, 0, ixwork, 0, 576);\n for (var sfb = cod_info.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {\n var start = gfc.scalefac_band.s[sfb];\n var end = gfc.scalefac_band.s[sfb + 1];\n for (var window = 0; window < 3; window++) {\n for (var l = start; l < end; l++) {\n cod_info.xr[ix++] = ixwork[3 * l + window];\n }\n }\n }\n\n var j = cod_info.sfb_lmax;\n for (var sfb = cod_info.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {\n cod_info.width[j] = cod_info.width[j + 1] = cod_info.width[j + 2] = gfc.scalefac_band.s[sfb + 1]\n - gfc.scalefac_band.s[sfb];\n cod_info.window[j] = 0;\n cod_info.window[j + 1] = 1;\n cod_info.window[j + 2] = 2;\n j += 3;\n }\n }\n\n cod_info.count1bits = 0;\n cod_info.sfb_partition_table = qupvt.nr_of_sfb_block[0][0];\n cod_info.slen[0] = 0;\n cod_info.slen[1] = 0;\n cod_info.slen[2] = 0;\n cod_info.slen[3] = 0;\n\n cod_info.max_nonzero_coeff = 575;\n\n /*\n * fresh scalefactors are all zero\n */\n Arrays.fill(cod_info.scalefac, 0);\n\n psfb21_analogsilence(gfc, cod_info);\n };\n\n function BinSearchDirection(ordinal) {\n this.ordinal = ordinal;\n }\n\n BinSearchDirection.BINSEARCH_NONE = new BinSearchDirection(0);\n BinSearchDirection.BINSEARCH_UP = new BinSearchDirection(1);\n BinSearchDirection.BINSEARCH_DOWN = new BinSearchDirection(2);\n\n /**\n * author/date??\n *\n * binary step size search used by outer_loop to get a quantizer step size\n * to start with\n */\n function bin_search_StepSize(gfc, cod_info, desired_rate, ch, xrpow) {\n var nBits;\n var CurrentStep = gfc.CurrentStep[ch];\n var flagGoneOver = false;\n var start = gfc.OldValue[ch];\n var Direction = BinSearchDirection.BINSEARCH_NONE;\n cod_info.global_gain = start;\n desired_rate -= cod_info.part2_length;\n\n for (; ;) {\n var step;\n nBits = tk.count_bits(gfc, xrpow, cod_info, null);\n\n if (CurrentStep == 1 || nBits == desired_rate)\n break;\n /* nothing to adjust anymore */\n\n if (nBits > desired_rate) {\n /* increase Quantize_StepSize */\n if (Direction == BinSearchDirection.BINSEARCH_DOWN)\n flagGoneOver = true;\n\n if (flagGoneOver)\n CurrentStep /= 2;\n Direction = BinSearchDirection.BINSEARCH_UP;\n step = CurrentStep;\n } else {\n /* decrease Quantize_StepSize */\n if (Direction == BinSearchDirection.BINSEARCH_UP)\n flagGoneOver = true;\n\n if (flagGoneOver)\n CurrentStep /= 2;\n Direction = BinSearchDirection.BINSEARCH_DOWN;\n step = -CurrentStep;\n }\n cod_info.global_gain += step;\n if (cod_info.global_gain < 0) {\n cod_info.global_gain = 0;\n flagGoneOver = true;\n }\n if (cod_info.global_gain > 255) {\n cod_info.global_gain = 255;\n flagGoneOver = true;\n }\n }\n\n\n while (nBits > desired_rate && cod_info.global_gain < 255) {\n cod_info.global_gain++;\n nBits = tk.count_bits(gfc, xrpow, cod_info, null);\n }\n gfc.CurrentStep[ch] = (start - cod_info.global_gain >= 4) ? 4 : 2;\n gfc.OldValue[ch] = cod_info.global_gain;\n cod_info.part2_3_length = nBits;\n return nBits;\n }\n\n this.trancate_smallspectrums = function (gfc, gi, l3_xmin, work) {\n var distort = new_float(L3Side.SFBMAX);\n\n if ((0 == (gfc.substep_shaping & 4) && gi.block_type == Encoder.SHORT_TYPE)\n || (gfc.substep_shaping & 0x80) != 0)\n return;\n qupvt.calc_noise(gi, l3_xmin, distort, new CalcNoiseResult(), null);\n for (var j = 0; j < 576; j++) {\n var xr = 0.0;\n if (gi.l3_enc[j] != 0)\n xr = Math.abs(gi.xr[j]);\n work[j] = xr;\n }\n\n var j = 0;\n var sfb = 8;\n if (gi.block_type == Encoder.SHORT_TYPE)\n sfb = 6;\n do {\n var allowedNoise, trancateThreshold;\n var nsame, start;\n\n var width = gi.width[sfb];\n j += width;\n if (distort[sfb] >= 1.0)\n continue;\n\n Arrays.sort(work, j - width, width);\n if (BitStream.EQ(work[j - 1], 0.0))\n continue;\n /* all zero sfb */\n\n allowedNoise = (1.0 - distort[sfb]) * l3_xmin[sfb];\n trancateThreshold = 0.0;\n start = 0;\n do {\n var noise;\n for (nsame = 1; start + nsame < width; nsame++)\n if (BitStream.NEQ(work[start + j - width], work[start + j\n + nsame - width]))\n break;\n\n noise = work[start + j - width] * work[start + j - width]\n * nsame;\n if (allowedNoise < noise) {\n if (start != 0)\n trancateThreshold = work[start + j - width - 1];\n break;\n }\n allowedNoise -= noise;\n start += nsame;\n } while (start < width);\n if (BitStream.EQ(trancateThreshold, 0.0))\n continue;\n\n do {\n if (Math.abs(gi.xr[j - width]) <= trancateThreshold)\n gi.l3_enc[j - width] = 0;\n } while (--width > 0);\n } while (++sfb < gi.psymax);\n\n gi.part2_3_length = tk.noquant_count_bits(gfc, gi, null);\n };\n\n /**\n * author/date??\n *\n * Function: Returns zero if there is a scalefac which has not been\n * amplified. Otherwise it returns one.\n */\n function loop_break(cod_info) {\n for (var sfb = 0; sfb < cod_info.sfbmax; sfb++)\n if (cod_info.scalefac[sfb]\n + cod_info.subblock_gain[cod_info.window[sfb]] == 0)\n return false;\n\n return true;\n }\n\n /* mt 5/99: Function: Improved calc_noise for a single channel */\n\n function penalties(noise) {\n return Util.FAST_LOG10((0.368 + 0.632 * noise * noise * noise));\n }\n\n /**\n * author/date??\n *\n * several different codes to decide which quantization is better\n */\n function get_klemm_noise(distort, gi) {\n var klemm_noise = 1E-37;\n for (var sfb = 0; sfb < gi.psymax; sfb++)\n klemm_noise += penalties(distort[sfb]);\n\n return Math.max(1e-20, klemm_noise);\n }\n\n function quant_compare(quant_comp, best, calc, gi, distort) {\n /**\n * noise is given in decibels (dB) relative to masking thesholds.
\n *\n * over_noise: ??? (the previous comment is fully wrong)
\n * tot_noise: ??? (the previous comment is fully wrong)
\n * max_noise: max quantization noise\n */\n var better;\n\n switch (quant_comp) {\n default:\n case 9:\n {\n if (best.over_count > 0) {\n /* there are distorted sfb */\n better = calc.over_SSD <= best.over_SSD;\n if (calc.over_SSD == best.over_SSD)\n better = calc.bits < best.bits;\n } else {\n /* no distorted sfb */\n better = ((calc.max_noise < 0) && ((calc.max_noise * 10 + calc.bits) <= (best.max_noise * 10 + best.bits)));\n }\n break;\n }\n\n case 0:\n better = calc.over_count < best.over_count\n || (calc.over_count == best.over_count && calc.over_noise < best.over_noise)\n || (calc.over_count == best.over_count\n && BitStream.EQ(calc.over_noise, best.over_noise) && calc.tot_noise < best.tot_noise);\n break;\n\n case 8:\n calc.max_noise = get_klemm_noise(distort, gi);\n //$FALL-THROUGH$\n case 1:\n better = calc.max_noise < best.max_noise;\n break;\n case 2:\n better = calc.tot_noise < best.tot_noise;\n break;\n case 3:\n better = (calc.tot_noise < best.tot_noise)\n && (calc.max_noise < best.max_noise);\n break;\n case 4:\n better = (calc.max_noise <= 0.0 && best.max_noise > 0.2)\n || (calc.max_noise <= 0.0 && best.max_noise < 0.0\n && best.max_noise > calc.max_noise - 0.2 && calc.tot_noise < best.tot_noise)\n || (calc.max_noise <= 0.0 && best.max_noise > 0.0\n && best.max_noise > calc.max_noise - 0.2 && calc.tot_noise < best.tot_noise\n + best.over_noise)\n || (calc.max_noise > 0.0 && best.max_noise > -0.05\n && best.max_noise > calc.max_noise - 0.1 && calc.tot_noise\n + calc.over_noise < best.tot_noise\n + best.over_noise)\n || (calc.max_noise > 0.0 && best.max_noise > -0.1\n && best.max_noise > calc.max_noise - 0.15 && calc.tot_noise\n + calc.over_noise + calc.over_noise < best.tot_noise\n + best.over_noise + best.over_noise);\n break;\n case 5:\n better = calc.over_noise < best.over_noise\n || (BitStream.EQ(calc.over_noise, best.over_noise) && calc.tot_noise < best.tot_noise);\n break;\n case 6:\n better = calc.over_noise < best.over_noise\n || (BitStream.EQ(calc.over_noise, best.over_noise) && (calc.max_noise < best.max_noise || (BitStream\n .EQ(calc.max_noise, best.max_noise) && calc.tot_noise <= best.tot_noise)));\n break;\n case 7:\n better = calc.over_count < best.over_count\n || calc.over_noise < best.over_noise;\n break;\n }\n\n if (best.over_count == 0) {\n /*\n * If no distorted bands, only use this quantization if it is\n * better, and if it uses less bits. Unfortunately, part2_3_length\n * is sometimes a poor estimator of the final size at low bitrates.\n */\n better = better && calc.bits < best.bits;\n }\n\n return better;\n }\n\n /**\n * author/date??\n *\n * \n * Amplify the scalefactor bands that violate the masking threshold.\n * See ISO 11172-3 Section C.1.5.4.3.5\n *\n * distort[] = noise/masking\n * distort[] > 1 ==> noise is not masked\n * distort[] < 1 ==> noise is masked\n * max_dist = maximum value of distort[]\n *\n * Three algorithms:\n * noise_shaping_amp\n * 0 Amplify all bands with distort[]>1.\n *\n * 1 Amplify all bands with distort[] >= max_dist^(.5);\n * ( 50% in the db scale)\n *\n * 2 Amplify first band with distort[] >= max_dist;\n *\n *\n * For algorithms 0 and 1, if max_dist < 1, then amplify all bands\n * with distort[] >= .95*max_dist. This is to make sure we always\n * amplify at least one band.\n *
\n */\n function amp_scalefac_bands(gfp, cod_info, distort, xrpow, bRefine) {\n var gfc = gfp.internal_flags;\n var ifqstep34;\n\n if (cod_info.scalefac_scale == 0) {\n ifqstep34 = 1.29683955465100964055;\n /* 2**(.75*.5) */\n } else {\n ifqstep34 = 1.68179283050742922612;\n /* 2**(.75*1) */\n }\n\n /* compute maximum value of distort[] */\n var trigger = 0;\n for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {\n if (trigger < distort[sfb])\n trigger = distort[sfb];\n }\n\n var noise_shaping_amp = gfc.noise_shaping_amp;\n if (noise_shaping_amp == 3) {\n if (bRefine)\n noise_shaping_amp = 2;\n else\n noise_shaping_amp = 1;\n }\n switch (noise_shaping_amp) {\n case 2:\n /* amplify exactly 1 band */\n break;\n\n case 1:\n /* amplify bands within 50% of max (on db scale) */\n if (trigger > 1.0)\n trigger = Math.pow(trigger, .5);\n else\n trigger *= .95;\n break;\n\n case 0:\n default:\n /* ISO algorithm. amplify all bands with distort>1 */\n if (trigger > 1.0)\n trigger = 1.0;\n else\n trigger *= .95;\n break;\n }\n\n var j = 0;\n for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {\n var width = cod_info.width[sfb];\n var l;\n j += width;\n if (distort[sfb] < trigger)\n continue;\n\n if ((gfc.substep_shaping & 2) != 0) {\n gfc.pseudohalf[sfb] = (0 == gfc.pseudohalf[sfb]) ? 1 : 0;\n if (0 == gfc.pseudohalf[sfb] && gfc.noise_shaping_amp == 2)\n return;\n }\n cod_info.scalefac[sfb]++;\n for (l = -width; l < 0; l++) {\n xrpow[j + l] *= ifqstep34;\n if (xrpow[j + l] > cod_info.xrpow_max)\n cod_info.xrpow_max = xrpow[j + l];\n }\n\n if (gfc.noise_shaping_amp == 2)\n return;\n }\n }\n\n /**\n * Takehiro Tominaga 2000-xx-xx\n *\n * turns on scalefac scale and adjusts scalefactors\n */\n function inc_scalefac_scale(cod_info, xrpow) {\n var ifqstep34 = 1.29683955465100964055;\n\n var j = 0;\n for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {\n var width = cod_info.width[sfb];\n var s = cod_info.scalefac[sfb];\n if (cod_info.preflag != 0)\n s += qupvt.pretab[sfb];\n j += width;\n if ((s & 1) != 0) {\n s++;\n for (var l = -width; l < 0; l++) {\n xrpow[j + l] *= ifqstep34;\n if (xrpow[j + l] > cod_info.xrpow_max)\n cod_info.xrpow_max = xrpow[j + l];\n }\n }\n cod_info.scalefac[sfb] = s >> 1;\n }\n cod_info.preflag = 0;\n cod_info.scalefac_scale = 1;\n }\n\n /**\n * Takehiro Tominaga 2000-xx-xx\n *\n * increases the subblock gain and adjusts scalefactors\n */\n function inc_subblock_gain(gfc, cod_info, xrpow) {\n var sfb;\n var scalefac = cod_info.scalefac;\n\n /* subbloc_gain can't do anything in the long block region */\n for (sfb = 0; sfb < cod_info.sfb_lmax; sfb++) {\n if (scalefac[sfb] >= 16)\n return true;\n }\n\n for (var window = 0; window < 3; window++) {\n var s1 = 0;\n var s2 = 0;\n\n for (sfb = cod_info.sfb_lmax + window; sfb < cod_info.sfbdivide; sfb += 3) {\n if (s1 < scalefac[sfb])\n s1 = scalefac[sfb];\n }\n for (; sfb < cod_info.sfbmax; sfb += 3) {\n if (s2 < scalefac[sfb])\n s2 = scalefac[sfb];\n }\n\n if (s1 < 16 && s2 < 8)\n continue;\n\n if (cod_info.subblock_gain[window] >= 7)\n return true;\n\n /*\n * even though there is no scalefactor for sfb12 subblock gain\n * affects upper frequencies too, that's why we have to go up to\n * SBMAX_s\n */\n cod_info.subblock_gain[window]++;\n var j = gfc.scalefac_band.l[cod_info.sfb_lmax];\n for (sfb = cod_info.sfb_lmax + window; sfb < cod_info.sfbmax; sfb += 3) {\n var amp;\n var width = cod_info.width[sfb];\n var s = scalefac[sfb];\n s = s - (4 >> cod_info.scalefac_scale);\n if (s >= 0) {\n scalefac[sfb] = s;\n j += width * 3;\n continue;\n }\n\n scalefac[sfb] = 0;\n {\n var gain = 210 + (s << (cod_info.scalefac_scale + 1));\n amp = qupvt.IPOW20(gain);\n }\n j += width * (window + 1);\n for (var l = -width; l < 0; l++) {\n xrpow[j + l] *= amp;\n if (xrpow[j + l] > cod_info.xrpow_max)\n cod_info.xrpow_max = xrpow[j + l];\n }\n j += width * (3 - window - 1);\n }\n\n {\n var amp = qupvt.IPOW20(202);\n j += cod_info.width[sfb] * (window + 1);\n for (var l = -cod_info.width[sfb]; l < 0; l++) {\n xrpow[j + l] *= amp;\n if (xrpow[j + l] > cod_info.xrpow_max)\n cod_info.xrpow_max = xrpow[j + l];\n }\n }\n }\n return false;\n }\n\n /**\n * \n * Takehiro Tominaga /date??\n * Robert Hegemann 2000-09-06: made a function of it\n *\n * amplifies scalefactor bands,\n * - if all are already amplified returns 0\n * - if some bands are amplified too much:\n * * try to increase scalefac_scale\n * * if already scalefac_scale was set\n * try on short blocks to increase subblock gain\n *
\n */\n function balance_noise(gfp, cod_info, distort, xrpow, bRefine) {\n var gfc = gfp.internal_flags;\n\n amp_scalefac_bands(gfp, cod_info, distort, xrpow, bRefine);\n\n /*\n * check to make sure we have not amplified too much loop_break returns\n * 0 if there is an unamplified scalefac scale_bitcount returns 0 if no\n * scalefactors are too large\n */\n\n var status = loop_break(cod_info);\n\n if (status)\n return false;\n /* all bands amplified */\n\n /*\n * not all scalefactors have been amplified. so these scalefacs are\n * possibly valid. encode them:\n */\n if (gfc.mode_gr == 2)\n status = tk.scale_bitcount(cod_info);\n else\n status = tk.scale_bitcount_lsf(gfc, cod_info);\n\n if (!status)\n return true;\n /* amplified some bands not exceeding limits */\n\n /*\n * some scalefactors are too large. lets try setting scalefac_scale=1\n */\n if (gfc.noise_shaping > 1) {\n Arrays.fill(gfc.pseudohalf, 0);\n if (0 == cod_info.scalefac_scale) {\n inc_scalefac_scale(cod_info, xrpow);\n status = false;\n } else {\n if (cod_info.block_type == Encoder.SHORT_TYPE\n && gfc.subblock_gain > 0) {\n status = (inc_subblock_gain(gfc, cod_info, xrpow) || loop_break(cod_info));\n }\n }\n }\n\n if (!status) {\n if (gfc.mode_gr == 2)\n status = tk.scale_bitcount(cod_info);\n else\n status = tk.scale_bitcount_lsf(gfc, cod_info);\n }\n return !status;\n }\n\n /**\n * \n * Function: The outer iteration loop controls the masking conditions\n * of all scalefactorbands. It computes the best scalefac and\n * global gain. This module calls the inner iteration loop\n *\n * mt 5/99 completely rewritten to allow for bit reservoir control,\n * mid/side channels with L/R or mid/side masking thresholds,\n * and chooses best quantization instead of last quantization when\n * no distortion free quantization can be found.\n *\n * added VBR support mt 5/99\n *\n * some code shuffle rh 9/00\n *
\n *\n * @param l3_xmin\n * allowed distortion\n * @param xrpow\n * coloured magnitudes of spectral\n * @param targ_bits\n * maximum allowed bits\n */\n this.outer_loop = function (gfp, cod_info, l3_xmin, xrpow, ch, targ_bits) {\n var gfc = gfp.internal_flags;\n var cod_info_w = new GrInfo();\n var save_xrpow = new_float(576);\n var distort = new_float(L3Side.SFBMAX);\n var best_noise_info = new CalcNoiseResult();\n var better;\n var prev_noise = new CalcNoiseData();\n var best_part2_3_length = 9999999;\n var bEndOfSearch = false;\n var bRefine = false;\n var best_ggain_pass1 = 0;\n\n bin_search_StepSize(gfc, cod_info, targ_bits, ch, xrpow);\n\n if (0 == gfc.noise_shaping)\n /* fast mode, no noise shaping, we are ready */\n return 100;\n /* default noise_info.over_count */\n\n /* compute the distortion in this quantization */\n /* coefficients and thresholds both l/r (or both mid/side) */\n qupvt.calc_noise(cod_info, l3_xmin, distort, best_noise_info,\n prev_noise);\n best_noise_info.bits = cod_info.part2_3_length;\n\n cod_info_w.assign(cod_info);\n var age = 0;\n System.arraycopy(xrpow, 0, save_xrpow, 0, 576);\n\n while (!bEndOfSearch) {\n /* BEGIN MAIN LOOP */\n do {\n var noise_info = new CalcNoiseResult();\n var search_limit;\n var maxggain = 255;\n\n /*\n * When quantization with no distorted bands is found, allow up\n * to X new unsuccesful tries in serial. This gives us more\n * possibilities for different quant_compare modes. Much more\n * than 3 makes not a big difference, it is only slower.\n */\n\n if ((gfc.substep_shaping & 2) != 0) {\n search_limit = 20;\n } else {\n search_limit = 3;\n }\n\n /*\n * Check if the last scalefactor band is distorted. in VBR mode\n * we can't get rid of the distortion, so quit now and VBR mode\n * will try again with more bits. (makes a 10% speed increase,\n * the files I tested were binary identical, 2000/05/20 Robert\n * Hegemann) distort[] > 1 means noise > allowed noise\n */\n if (gfc.sfb21_extra) {\n if (distort[cod_info_w.sfbmax] > 1.0)\n break;\n if (cod_info_w.block_type == Encoder.SHORT_TYPE\n && (distort[cod_info_w.sfbmax + 1] > 1.0 || distort[cod_info_w.sfbmax + 2] > 1.0))\n break;\n }\n\n /* try a new scalefactor conbination on cod_info_w */\n if (!balance_noise(gfp, cod_info_w, distort, xrpow, bRefine))\n break;\n if (cod_info_w.scalefac_scale != 0)\n maxggain = 254;\n\n /*\n * inner_loop starts with the initial quantization step computed\n * above and slowly increases until the bits < huff_bits. Thus\n * it is important not to start with too large of an inital\n * quantization step. Too small is ok, but inner_loop will take\n * longer\n */\n var huff_bits = targ_bits - cod_info_w.part2_length;\n if (huff_bits <= 0)\n break;\n\n /*\n * increase quantizer stepsize until needed bits are below\n * maximum\n */\n while ((cod_info_w.part2_3_length = tk.count_bits(gfc, xrpow,\n cod_info_w, prev_noise)) > huff_bits\n && cod_info_w.global_gain <= maxggain)\n cod_info_w.global_gain++;\n\n if (cod_info_w.global_gain > maxggain)\n break;\n\n if (best_noise_info.over_count == 0) {\n\n while ((cod_info_w.part2_3_length = tk.count_bits(gfc,\n xrpow, cod_info_w, prev_noise)) > best_part2_3_length\n && cod_info_w.global_gain <= maxggain)\n cod_info_w.global_gain++;\n\n if (cod_info_w.global_gain > maxggain)\n break;\n }\n\n /* compute the distortion in this quantization */\n qupvt.calc_noise(cod_info_w, l3_xmin, distort, noise_info,\n prev_noise);\n noise_info.bits = cod_info_w.part2_3_length;\n\n /*\n * check if this quantization is better than our saved\n * quantization\n */\n if (cod_info.block_type != Encoder.SHORT_TYPE) {\n // NORM, START or STOP type\n better = gfp.quant_comp;\n } else\n better = gfp.quant_comp_short;\n\n better = quant_compare(better, best_noise_info, noise_info,\n cod_info_w, distort) ? 1 : 0;\n\n /* save data so we can restore this quantization later */\n if (better != 0) {\n best_part2_3_length = cod_info.part2_3_length;\n best_noise_info = noise_info;\n cod_info.assign(cod_info_w);\n age = 0;\n /* save data so we can restore this quantization later */\n /* store for later reuse */\n System.arraycopy(xrpow, 0, save_xrpow, 0, 576);\n } else {\n /* early stop? */\n if (gfc.full_outer_loop == 0) {\n if (++age > search_limit\n && best_noise_info.over_count == 0)\n break;\n if ((gfc.noise_shaping_amp == 3) && bRefine && age > 30)\n break;\n if ((gfc.noise_shaping_amp == 3)\n && bRefine\n && (cod_info_w.global_gain - best_ggain_pass1) > 15)\n break;\n }\n }\n } while ((cod_info_w.global_gain + cod_info_w.scalefac_scale) < 255);\n\n if (gfc.noise_shaping_amp == 3) {\n if (!bRefine) {\n /* refine search */\n cod_info_w.assign(cod_info);\n System.arraycopy(save_xrpow, 0, xrpow, 0, 576);\n age = 0;\n best_ggain_pass1 = cod_info_w.global_gain;\n\n bRefine = true;\n } else {\n /* search already refined, stop */\n bEndOfSearch = true;\n }\n\n } else {\n bEndOfSearch = true;\n }\n }\n\n /*\n * finish up\n */\n if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)\n /* restore for reuse on next try */\n System.arraycopy(save_xrpow, 0, xrpow, 0, 576);\n /*\n * do the 'substep shaping'\n */\n else if ((gfc.substep_shaping & 1) != 0)\n trancate_smallspectrums(gfc, cod_info, l3_xmin, xrpow);\n\n return best_noise_info.over_count;\n }\n\n /**\n * Robert Hegemann 2000-09-06\n *\n * update reservoir status after FINAL quantization/bitrate\n */\n this.iteration_finish_one = function (gfc, gr, ch) {\n var l3_side = gfc.l3_side;\n var cod_info = l3_side.tt[gr][ch];\n\n /*\n * try some better scalefac storage\n */\n tk.best_scalefac_store(gfc, gr, ch, l3_side);\n\n /*\n * best huffman_divide may save some bits too\n */\n if (gfc.use_best_huffman == 1)\n tk.best_huffman_divide(gfc, cod_info);\n\n /*\n * update reservoir status after FINAL quantization/bitrate\n */\n rv.ResvAdjust(gfc, cod_info);\n };\n\n /**\n *\n * 2000-09-04 Robert Hegemann\n *\n * @param l3_xmin\n * allowed distortion of the scalefactor\n * @param xrpow\n * coloured magnitudes of spectral values\n */\n this.VBR_encode_granule = function (gfp, cod_info, l3_xmin, xrpow, ch, min_bits, max_bits) {\n var gfc = gfp.internal_flags;\n var bst_cod_info = new GrInfo();\n var bst_xrpow = new_float(576);\n var Max_bits = max_bits;\n var real_bits = max_bits + 1;\n var this_bits = (max_bits + min_bits) / 2;\n var dbits, over, found = 0;\n var sfb21_extra = gfc.sfb21_extra;\n\n Arrays.fill(bst_cod_info.l3_enc, 0);\n\n /*\n * search within round about 40 bits of optimal\n */\n do {\n\n if (this_bits > Max_bits - 42)\n gfc.sfb21_extra = false;\n else\n gfc.sfb21_extra = sfb21_extra;\n\n over = outer_loop(gfp, cod_info, l3_xmin, xrpow, ch, this_bits);\n\n /*\n * is quantization as good as we are looking for ? in this case: is\n * no scalefactor band distorted?\n */\n if (over <= 0) {\n found = 1;\n /*\n * now we know it can be done with \"real_bits\" and maybe we can\n * skip some iterations\n */\n real_bits = cod_info.part2_3_length;\n\n /*\n * store best quantization so far\n */\n bst_cod_info.assign(cod_info);\n System.arraycopy(xrpow, 0, bst_xrpow, 0, 576);\n\n /*\n * try with fewer bits\n */\n max_bits = real_bits - 32;\n dbits = max_bits - min_bits;\n this_bits = (max_bits + min_bits) / 2;\n } else {\n /*\n * try with more bits\n */\n min_bits = this_bits + 32;\n dbits = max_bits - min_bits;\n this_bits = (max_bits + min_bits) / 2;\n\n if (found != 0) {\n found = 2;\n /*\n * start again with best quantization so far\n */\n cod_info.assign(bst_cod_info);\n System.arraycopy(bst_xrpow, 0, xrpow, 0, 576);\n }\n }\n } while (dbits > 12);\n\n gfc.sfb21_extra = sfb21_extra;\n\n /*\n * found=0 => nothing found, use last one found=1 => we just found the\n * best and left the loop found=2 => we restored a good one and have now\n * l3_enc to restore too\n */\n if (found == 2) {\n System.arraycopy(bst_cod_info.l3_enc, 0, cod_info.l3_enc, 0, 576);\n }\n }\n\n /**\n * Robert Hegemann 2000-09-05\n *\n * calculates * how many bits are available for analog silent granules * how\n * many bits to use for the lowest allowed bitrate * how many bits each\n * bitrate would provide\n */\n this.get_framebits = function (gfp, frameBits) {\n var gfc = gfp.internal_flags;\n\n /*\n * always use at least this many bits per granule per channel unless we\n * detect analog silence, see below\n */\n gfc.bitrate_index = gfc.VBR_min_bitrate;\n var bitsPerFrame = bs.getframebits(gfp);\n\n /*\n * bits for analog silence\n */\n gfc.bitrate_index = 1;\n bitsPerFrame = bs.getframebits(gfp);\n\n for (var i = 1; i <= gfc.VBR_max_bitrate; i++) {\n gfc.bitrate_index = i;\n var mb = new MeanBits(bitsPerFrame);\n frameBits[i] = rv.ResvFrameBegin(gfp, mb);\n bitsPerFrame = mb.bits;\n }\n };\n\n /* RH: this one needs to be overhauled sometime */\n\n /**\n * \n * 2000-09-04 Robert Hegemann\n *\n * * converts LR to MS coding when necessary\n * * calculates allowed/adjusted quantization noise amounts\n * * detects analog silent frames\n *\n * some remarks:\n * - lower masking depending on Quality setting\n * - quality control together with adjusted ATH MDCT scaling\n * on lower quality setting allocate more noise from\n * ATH masking, and on higher quality setting allocate\n * less noise from ATH masking.\n * - experiments show that going more than 2dB over GPSYCHO's\n * limits ends up in very annoying artefacts\n *
\n */\n this.VBR_old_prepare = function (gfp, pe, ms_ener_ratio, ratio, l3_xmin, frameBits, min_bits,\n max_bits, bands) {\n var gfc = gfp.internal_flags;\n\n var masking_lower_db, adjust = 0.0;\n var analog_silence = 1;\n var bits = 0;\n\n gfc.bitrate_index = gfc.VBR_max_bitrate;\n var avg = rv.ResvFrameBegin(gfp, new MeanBits(0)) / gfc.mode_gr;\n\n get_framebits(gfp, frameBits);\n\n for (var gr = 0; gr < gfc.mode_gr; gr++) {\n var mxb = qupvt.on_pe(gfp, pe, max_bits[gr], avg, gr, 0);\n if (gfc.mode_ext == Encoder.MPG_MD_MS_LR) {\n ms_convert(gfc.l3_side, gr);\n qupvt.reduce_side(max_bits[gr], ms_ener_ratio[gr], avg, mxb);\n }\n for (var ch = 0; ch < gfc.channels_out; ++ch) {\n var cod_info = gfc.l3_side.tt[gr][ch];\n\n if (cod_info.block_type != Encoder.SHORT_TYPE) {\n // NORM, START or STOP type\n adjust = 1.28 / (1 + Math\n .exp(3.5 - pe[gr][ch] / 300.)) - 0.05;\n masking_lower_db = gfc.PSY.mask_adjust - adjust;\n } else {\n adjust = 2.56 / (1 + Math\n .exp(3.5 - pe[gr][ch] / 300.)) - 0.14;\n masking_lower_db = gfc.PSY.mask_adjust_short - adjust;\n }\n gfc.masking_lower = Math.pow(10.0,\n masking_lower_db * 0.1);\n\n init_outer_loop(gfc, cod_info);\n bands[gr][ch] = qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info,\n l3_xmin[gr][ch]);\n if (bands[gr][ch] != 0)\n analog_silence = 0;\n\n min_bits[gr][ch] = 126;\n\n bits += max_bits[gr][ch];\n }\n }\n for (var gr = 0; gr < gfc.mode_gr; gr++) {\n for (var ch = 0; ch < gfc.channels_out; ch++) {\n if (bits > frameBits[gfc.VBR_max_bitrate]) {\n max_bits[gr][ch] *= frameBits[gfc.VBR_max_bitrate];\n max_bits[gr][ch] /= bits;\n }\n if (min_bits[gr][ch] > max_bits[gr][ch])\n min_bits[gr][ch] = max_bits[gr][ch];\n\n }\n /* for ch */\n }\n /* for gr */\n\n return analog_silence;\n };\n\n this.bitpressure_strategy = function (gfc, l3_xmin, min_bits, max_bits) {\n for (var gr = 0; gr < gfc.mode_gr; gr++) {\n for (var ch = 0; ch < gfc.channels_out; ch++) {\n var gi = gfc.l3_side.tt[gr][ch];\n var pxmin = l3_xmin[gr][ch];\n var pxminPos = 0;\n for (var sfb = 0; sfb < gi.psy_lmax; sfb++)\n pxmin[pxminPos++] *= 1. + .029 * sfb * sfb\n / Encoder.SBMAX_l / Encoder.SBMAX_l;\n\n if (gi.block_type == Encoder.SHORT_TYPE) {\n for (var sfb = gi.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {\n pxmin[pxminPos++] *= 1. + .029 * sfb * sfb\n / Encoder.SBMAX_s / Encoder.SBMAX_s;\n pxmin[pxminPos++] *= 1. + .029 * sfb * sfb\n / Encoder.SBMAX_s / Encoder.SBMAX_s;\n pxmin[pxminPos++] *= 1. + .029 * sfb * sfb\n / Encoder.SBMAX_s / Encoder.SBMAX_s;\n }\n }\n max_bits[gr][ch] = 0 | Math.max(min_bits[gr][ch],\n 0.9 * max_bits[gr][ch]);\n }\n }\n };\n\n this.VBR_new_prepare = function (gfp, pe, ratio, l3_xmin, frameBits, max_bits) {\n var gfc = gfp.internal_flags;\n\n var analog_silence = 1;\n var avg = 0, bits = 0;\n var maximum_framebits;\n\n if (!gfp.free_format) {\n gfc.bitrate_index = gfc.VBR_max_bitrate;\n\n var mb = new MeanBits(avg);\n rv.ResvFrameBegin(gfp, mb);\n avg = mb.bits;\n\n get_framebits(gfp, frameBits);\n maximum_framebits = frameBits[gfc.VBR_max_bitrate];\n } else {\n gfc.bitrate_index = 0;\n var mb = new MeanBits(avg);\n maximum_framebits = rv.ResvFrameBegin(gfp, mb);\n avg = mb.bits;\n frameBits[0] = maximum_framebits;\n }\n\n for (var gr = 0; gr < gfc.mode_gr; gr++) {\n qupvt.on_pe(gfp, pe, max_bits[gr], avg, gr, 0);\n if (gfc.mode_ext == Encoder.MPG_MD_MS_LR) {\n ms_convert(gfc.l3_side, gr);\n }\n for (var ch = 0; ch < gfc.channels_out; ++ch) {\n var cod_info = gfc.l3_side.tt[gr][ch];\n\n gfc.masking_lower = Math.pow(10.0,\n gfc.PSY.mask_adjust * 0.1);\n\n init_outer_loop(gfc, cod_info);\n if (0 != qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info,\n l3_xmin[gr][ch]))\n analog_silence = 0;\n\n bits += max_bits[gr][ch];\n }\n }\n for (var gr = 0; gr < gfc.mode_gr; gr++) {\n for (var ch = 0; ch < gfc.channels_out; ch++) {\n if (bits > maximum_framebits) {\n max_bits[gr][ch] *= maximum_framebits;\n max_bits[gr][ch] /= bits;\n }\n\n }\n /* for ch */\n }\n /* for gr */\n\n return analog_silence;\n };\n\n /**\n * calculates target bits for ABR encoding\n *\n * mt 2000/05/31\n */\n this.calc_target_bits = function (gfp, pe, ms_ener_ratio, targ_bits, analog_silence_bits, max_frame_bits) {\n var gfc = gfp.internal_flags;\n var l3_side = gfc.l3_side;\n var res_factor;\n var gr, ch, totbits, mean_bits = 0;\n\n gfc.bitrate_index = gfc.VBR_max_bitrate;\n var mb = new MeanBits(mean_bits);\n max_frame_bits[0] = rv.ResvFrameBegin(gfp, mb);\n mean_bits = mb.bits;\n\n gfc.bitrate_index = 1;\n mean_bits = bs.getframebits(gfp) - gfc.sideinfo_len * 8;\n analog_silence_bits[0] = mean_bits / (gfc.mode_gr * gfc.channels_out);\n\n mean_bits = gfp.VBR_mean_bitrate_kbps * gfp.framesize * 1000;\n if ((gfc.substep_shaping & 1) != 0)\n mean_bits *= 1.09;\n mean_bits /= gfp.out_samplerate;\n mean_bits -= gfc.sideinfo_len * 8;\n mean_bits /= (gfc.mode_gr * gfc.channels_out);\n\n /**\n * \n * res_factor is the percentage of the target bitrate that should\n * be used on average. the remaining bits are added to the\n * bitreservoir and used for difficult to encode frames.\n *\n * Since we are tracking the average bitrate, we should adjust\n * res_factor \"on the fly\", increasing it if the average bitrate\n * is greater than the requested bitrate, and decreasing it\n * otherwise. Reasonable ranges are from .9 to 1.0\n *\n * Until we get the above suggestion working, we use the following\n * tuning:\n * compression ratio res_factor\n * 5.5 (256kbps) 1.0 no need for bitreservoir\n * 11 (128kbps) .93 7% held for reservoir\n *\n * with linear interpolation for other values.\n *
\n */\n res_factor = .93 + .07 * (11.0 - gfp.compression_ratio)\n / (11.0 - 5.5);\n if (res_factor < .90)\n res_factor = .90;\n if (res_factor > 1.00)\n res_factor = 1.00;\n\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n var sum = 0;\n for (ch = 0; ch < gfc.channels_out; ch++) {\n targ_bits[gr][ch] = (int)(res_factor * mean_bits);\n\n if (pe[gr][ch] > 700) {\n var add_bits = (int)((pe[gr][ch] - 700) / 1.4);\n\n var cod_info = l3_side.tt[gr][ch];\n targ_bits[gr][ch] = (int)(res_factor * mean_bits);\n\n /* short blocks use a little extra, no matter what the pe */\n if (cod_info.block_type == Encoder.SHORT_TYPE) {\n if (add_bits < mean_bits / 2)\n add_bits = mean_bits / 2;\n }\n /* at most increase bits by 1.5*average */\n if (add_bits > mean_bits * 3 / 2)\n add_bits = mean_bits * 3 / 2;\n else if (add_bits < 0)\n add_bits = 0;\n\n targ_bits[gr][ch] += add_bits;\n }\n if (targ_bits[gr][ch] > LameInternalFlags.MAX_BITS_PER_CHANNEL) {\n targ_bits[gr][ch] = LameInternalFlags.MAX_BITS_PER_CHANNEL;\n }\n sum += targ_bits[gr][ch];\n }\n /* for ch */\n if (sum > LameInternalFlags.MAX_BITS_PER_GRANULE) {\n for (ch = 0; ch < gfc.channels_out; ++ch) {\n targ_bits[gr][ch] *= LameInternalFlags.MAX_BITS_PER_GRANULE;\n targ_bits[gr][ch] /= sum;\n }\n }\n }\n /* for gr */\n\n if (gfc.mode_ext == Encoder.MPG_MD_MS_LR)\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n qupvt.reduce_side(targ_bits[gr], ms_ener_ratio[gr], mean_bits\n * gfc.channels_out,\n LameInternalFlags.MAX_BITS_PER_GRANULE);\n }\n\n /*\n * sum target bits\n */\n totbits = 0;\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n if (targ_bits[gr][ch] > LameInternalFlags.MAX_BITS_PER_CHANNEL)\n targ_bits[gr][ch] = LameInternalFlags.MAX_BITS_PER_CHANNEL;\n totbits += targ_bits[gr][ch];\n }\n }\n\n /*\n * repartion target bits if needed\n */\n if (totbits > max_frame_bits[0]) {\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n targ_bits[gr][ch] *= max_frame_bits[0];\n targ_bits[gr][ch] /= totbits;\n }\n }\n }\n }\n\n }\n\n /*\n * MP3 window subband -> subband filtering -> mdct routine\n *\n * Copyright (c) 1999-2000 Takehiro Tominaga\n *\n *\n * This library is free software; you can redistribute it and/or\n * modify it under the terms of the GNU Lesser General Public\n * License as published by the Free Software Foundation; either\n * version 2 of the License, or (at your option) any later version.\n *\n * This library is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Library General Public License for more details.\n *\n * You should have received a copy of the GNU Library General Public\n * License along with this library; if not, write to the\n * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n * Boston, MA 02111-1307, USA.\n */\n /*\n * Special Thanks to Patrick De Smet for your advices.\n */\n\n /* $Id: NewMDCT.java,v 1.11 2011/05/24 20:48:06 kenchis Exp $ */\n\n//package mp3;\n\n//import java.util.Arrays;\n\n\n\n function NewMDCT() {\n\n var enwindow = [\n -4.77e-07 * 0.740951125354959 / 2.384e-06,\n 1.03951e-04 * 0.740951125354959 / 2.384e-06,\n 9.53674e-04 * 0.740951125354959 / 2.384e-06,\n 2.841473e-03 * 0.740951125354959 / 2.384e-06,\n 3.5758972e-02 * 0.740951125354959 / 2.384e-06,\n 3.401756e-03 * 0.740951125354959 / 2.384e-06,\n 9.83715e-04 * 0.740951125354959 / 2.384e-06,\n 9.9182e-05 * 0.740951125354959 / 2.384e-06, /* 15 */\n 1.2398e-05 * 0.740951125354959 / 2.384e-06,\n 1.91212e-04 * 0.740951125354959 / 2.384e-06,\n 2.283096e-03 * 0.740951125354959 / 2.384e-06,\n 1.6994476e-02 * 0.740951125354959 / 2.384e-06,\n -1.8756866e-02 * 0.740951125354959 / 2.384e-06,\n -2.630711e-03 * 0.740951125354959 / 2.384e-06,\n -2.47478e-04 * 0.740951125354959 / 2.384e-06,\n -1.4782e-05 * 0.740951125354959 / 2.384e-06,\n 9.063471690191471e-01, 1.960342806591213e-01,\n\n -4.77e-07 * 0.773010453362737 / 2.384e-06,\n 1.05858e-04 * 0.773010453362737 / 2.384e-06,\n 9.30786e-04 * 0.773010453362737 / 2.384e-06,\n 2.521515e-03 * 0.773010453362737 / 2.384e-06,\n 3.5694122e-02 * 0.773010453362737 / 2.384e-06,\n 3.643036e-03 * 0.773010453362737 / 2.384e-06,\n 9.91821e-04 * 0.773010453362737 / 2.384e-06,\n 9.6321e-05 * 0.773010453362737 / 2.384e-06, /* 14 */\n 1.1444e-05 * 0.773010453362737 / 2.384e-06,\n 1.65462e-04 * 0.773010453362737 / 2.384e-06,\n 2.110004e-03 * 0.773010453362737 / 2.384e-06,\n 1.6112804e-02 * 0.773010453362737 / 2.384e-06,\n -1.9634247e-02 * 0.773010453362737 / 2.384e-06,\n -2.803326e-03 * 0.773010453362737 / 2.384e-06,\n -2.77042e-04 * 0.773010453362737 / 2.384e-06,\n -1.6689e-05 * 0.773010453362737 / 2.384e-06,\n 8.206787908286602e-01, 3.901806440322567e-01,\n\n -4.77e-07 * 0.803207531480645 / 2.384e-06,\n 1.07288e-04 * 0.803207531480645 / 2.384e-06,\n 9.02653e-04 * 0.803207531480645 / 2.384e-06,\n 2.174854e-03 * 0.803207531480645 / 2.384e-06,\n 3.5586357e-02 * 0.803207531480645 / 2.384e-06,\n 3.858566e-03 * 0.803207531480645 / 2.384e-06,\n 9.95159e-04 * 0.803207531480645 / 2.384e-06,\n 9.3460e-05 * 0.803207531480645 / 2.384e-06, /* 13 */\n 1.0014e-05 * 0.803207531480645 / 2.384e-06,\n 1.40190e-04 * 0.803207531480645 / 2.384e-06,\n 1.937389e-03 * 0.803207531480645 / 2.384e-06,\n 1.5233517e-02 * 0.803207531480645 / 2.384e-06,\n -2.0506859e-02 * 0.803207531480645 / 2.384e-06,\n -2.974033e-03 * 0.803207531480645 / 2.384e-06,\n -3.07560e-04 * 0.803207531480645 / 2.384e-06,\n -1.8120e-05 * 0.803207531480645 / 2.384e-06,\n 7.416505462720353e-01, 5.805693545089249e-01,\n\n -4.77e-07 * 0.831469612302545 / 2.384e-06,\n 1.08242e-04 * 0.831469612302545 / 2.384e-06,\n 8.68797e-04 * 0.831469612302545 / 2.384e-06,\n 1.800537e-03 * 0.831469612302545 / 2.384e-06,\n 3.5435200e-02 * 0.831469612302545 / 2.384e-06,\n 4.049301e-03 * 0.831469612302545 / 2.384e-06,\n 9.94205e-04 * 0.831469612302545 / 2.384e-06,\n 9.0599e-05 * 0.831469612302545 / 2.384e-06, /* 12 */\n 9.060e-06 * 0.831469612302545 / 2.384e-06,\n 1.16348e-04 * 0.831469612302545 / 2.384e-06,\n 1.766682e-03 * 0.831469612302545 / 2.384e-06,\n 1.4358521e-02 * 0.831469612302545 / 2.384e-06,\n -2.1372318e-02 * 0.831469612302545 / 2.384e-06,\n -3.14188e-03 * 0.831469612302545 / 2.384e-06,\n -3.39031e-04 * 0.831469612302545 / 2.384e-06,\n -1.9550e-05 * 0.831469612302545 / 2.384e-06,\n 6.681786379192989e-01, 7.653668647301797e-01,\n\n -4.77e-07 * 0.857728610000272 / 2.384e-06,\n 1.08719e-04 * 0.857728610000272 / 2.384e-06,\n 8.29220e-04 * 0.857728610000272 / 2.384e-06,\n 1.399517e-03 * 0.857728610000272 / 2.384e-06,\n 3.5242081e-02 * 0.857728610000272 / 2.384e-06,\n 4.215240e-03 * 0.857728610000272 / 2.384e-06,\n 9.89437e-04 * 0.857728610000272 / 2.384e-06,\n 8.7261e-05 * 0.857728610000272 / 2.384e-06, /* 11 */\n 8.106e-06 * 0.857728610000272 / 2.384e-06,\n 9.3937e-05 * 0.857728610000272 / 2.384e-06,\n 1.597881e-03 * 0.857728610000272 / 2.384e-06,\n 1.3489246e-02 * 0.857728610000272 / 2.384e-06,\n -2.2228718e-02 * 0.857728610000272 / 2.384e-06,\n -3.306866e-03 * 0.857728610000272 / 2.384e-06,\n -3.71456e-04 * 0.857728610000272 / 2.384e-06,\n -2.1458e-05 * 0.857728610000272 / 2.384e-06,\n 5.993769336819237e-01, 9.427934736519954e-01,\n\n -4.77e-07 * 0.881921264348355 / 2.384e-06,\n 1.08719e-04 * 0.881921264348355 / 2.384e-06,\n 7.8392e-04 * 0.881921264348355 / 2.384e-06,\n 9.71317e-04 * 0.881921264348355 / 2.384e-06,\n 3.5007000e-02 * 0.881921264348355 / 2.384e-06,\n 4.357815e-03 * 0.881921264348355 / 2.384e-06,\n 9.80854e-04 * 0.881921264348355 / 2.384e-06,\n 8.3923e-05 * 0.881921264348355 / 2.384e-06, /* 10 */\n 7.629e-06 * 0.881921264348355 / 2.384e-06,\n 7.2956e-05 * 0.881921264348355 / 2.384e-06,\n 1.432419e-03 * 0.881921264348355 / 2.384e-06,\n 1.2627602e-02 * 0.881921264348355 / 2.384e-06,\n -2.3074150e-02 * 0.881921264348355 / 2.384e-06,\n -3.467083e-03 * 0.881921264348355 / 2.384e-06,\n -4.04358e-04 * 0.881921264348355 / 2.384e-06,\n -2.3365e-05 * 0.881921264348355 / 2.384e-06,\n 5.345111359507916e-01, 1.111140466039205e+00,\n\n -9.54e-07 * 0.903989293123443 / 2.384e-06,\n 1.08242e-04 * 0.903989293123443 / 2.384e-06,\n 7.31945e-04 * 0.903989293123443 / 2.384e-06,\n 5.15938e-04 * 0.903989293123443 / 2.384e-06,\n 3.4730434e-02 * 0.903989293123443 / 2.384e-06,\n 4.477024e-03 * 0.903989293123443 / 2.384e-06,\n 9.68933e-04 * 0.903989293123443 / 2.384e-06,\n 8.0585e-05 * 0.903989293123443 / 2.384e-06, /* 9 */\n 6.676e-06 * 0.903989293123443 / 2.384e-06,\n 5.2929e-05 * 0.903989293123443 / 2.384e-06,\n 1.269817e-03 * 0.903989293123443 / 2.384e-06,\n 1.1775017e-02 * 0.903989293123443 / 2.384e-06,\n -2.3907185e-02 * 0.903989293123443 / 2.384e-06,\n -3.622532e-03 * 0.903989293123443 / 2.384e-06,\n -4.38213e-04 * 0.903989293123443 / 2.384e-06,\n -2.5272e-05 * 0.903989293123443 / 2.384e-06,\n 4.729647758913199e-01, 1.268786568327291e+00,\n\n -9.54e-07 * 0.92387953251128675613 / 2.384e-06,\n 1.06812e-04 * 0.92387953251128675613 / 2.384e-06,\n 6.74248e-04 * 0.92387953251128675613 / 2.384e-06,\n 3.3379e-05 * 0.92387953251128675613 / 2.384e-06,\n 3.4412861e-02 * 0.92387953251128675613 / 2.384e-06,\n 4.573822e-03 * 0.92387953251128675613 / 2.384e-06,\n 9.54151e-04 * 0.92387953251128675613 / 2.384e-06,\n 7.6771e-05 * 0.92387953251128675613 / 2.384e-06,\n 6.199e-06 * 0.92387953251128675613 / 2.384e-06,\n 3.4332e-05 * 0.92387953251128675613 / 2.384e-06,\n 1.111031e-03 * 0.92387953251128675613 / 2.384e-06,\n 1.0933399e-02 * 0.92387953251128675613 / 2.384e-06,\n -2.4725437e-02 * 0.92387953251128675613 / 2.384e-06,\n -3.771782e-03 * 0.92387953251128675613 / 2.384e-06,\n -4.72546e-04 * 0.92387953251128675613 / 2.384e-06,\n -2.7657e-05 * 0.92387953251128675613 / 2.384e-06,\n 4.1421356237309504879e-01, /* tan(PI/8) */\n 1.414213562373095e+00,\n\n -9.54e-07 * 0.941544065183021 / 2.384e-06,\n 1.05381e-04 * 0.941544065183021 / 2.384e-06,\n 6.10352e-04 * 0.941544065183021 / 2.384e-06,\n -4.75883e-04 * 0.941544065183021 / 2.384e-06,\n 3.4055710e-02 * 0.941544065183021 / 2.384e-06,\n 4.649162e-03 * 0.941544065183021 / 2.384e-06,\n 9.35555e-04 * 0.941544065183021 / 2.384e-06,\n 7.3433e-05 * 0.941544065183021 / 2.384e-06, /* 7 */\n 5.245e-06 * 0.941544065183021 / 2.384e-06,\n 1.7166e-05 * 0.941544065183021 / 2.384e-06,\n 9.56535e-04 * 0.941544065183021 / 2.384e-06,\n 1.0103703e-02 * 0.941544065183021 / 2.384e-06,\n -2.5527000e-02 * 0.941544065183021 / 2.384e-06,\n -3.914356e-03 * 0.941544065183021 / 2.384e-06,\n -5.07355e-04 * 0.941544065183021 / 2.384e-06,\n -3.0041e-05 * 0.941544065183021 / 2.384e-06,\n 3.578057213145241e-01, 1.546020906725474e+00,\n\n -9.54e-07 * 0.956940335732209 / 2.384e-06,\n 1.02520e-04 * 0.956940335732209 / 2.384e-06,\n 5.39303e-04 * 0.956940335732209 / 2.384e-06,\n -1.011848e-03 * 0.956940335732209 / 2.384e-06,\n 3.3659935e-02 * 0.956940335732209 / 2.384e-06,\n 4.703045e-03 * 0.956940335732209 / 2.384e-06,\n 9.15051e-04 * 0.956940335732209 / 2.384e-06,\n 7.0095e-05 * 0.956940335732209 / 2.384e-06, /* 6 */\n 4.768e-06 * 0.956940335732209 / 2.384e-06,\n 9.54e-07 * 0.956940335732209 / 2.384e-06,\n 8.06808e-04 * 0.956940335732209 / 2.384e-06,\n 9.287834e-03 * 0.956940335732209 / 2.384e-06,\n -2.6310921e-02 * 0.956940335732209 / 2.384e-06,\n -4.048824e-03 * 0.956940335732209 / 2.384e-06,\n -5.42164e-04 * 0.956940335732209 / 2.384e-06,\n -3.2425e-05 * 0.956940335732209 / 2.384e-06,\n 3.033466836073424e-01, 1.662939224605090e+00,\n\n -1.431e-06 * 0.970031253194544 / 2.384e-06,\n 9.9182e-05 * 0.970031253194544 / 2.384e-06,\n 4.62532e-04 * 0.970031253194544 / 2.384e-06,\n -1.573563e-03 * 0.970031253194544 / 2.384e-06,\n 3.3225536e-02 * 0.970031253194544 / 2.384e-06,\n 4.737377e-03 * 0.970031253194544 / 2.384e-06,\n 8.91685e-04 * 0.970031253194544 / 2.384e-06,\n 6.6280e-05 * 0.970031253194544 / 2.384e-06, /* 5 */\n 4.292e-06 * 0.970031253194544 / 2.384e-06,\n -1.3828e-05 * 0.970031253194544 / 2.384e-06,\n 6.61850e-04 * 0.970031253194544 / 2.384e-06,\n 8.487225e-03 * 0.970031253194544 / 2.384e-06,\n -2.7073860e-02 * 0.970031253194544 / 2.384e-06,\n -4.174709e-03 * 0.970031253194544 / 2.384e-06,\n -5.76973e-04 * 0.970031253194544 / 2.384e-06,\n -3.4809e-05 * 0.970031253194544 / 2.384e-06,\n 2.504869601913055e-01, 1.763842528696710e+00,\n\n -1.431e-06 * 0.98078528040323 / 2.384e-06,\n 9.5367e-05 * 0.98078528040323 / 2.384e-06,\n 3.78609e-04 * 0.98078528040323 / 2.384e-06,\n -2.161503e-03 * 0.98078528040323 / 2.384e-06,\n 3.2754898e-02 * 0.98078528040323 / 2.384e-06,\n 4.752159e-03 * 0.98078528040323 / 2.384e-06,\n 8.66413e-04 * 0.98078528040323 / 2.384e-06,\n 6.2943e-05 * 0.98078528040323 / 2.384e-06, /* 4 */\n 3.815e-06 * 0.98078528040323 / 2.384e-06,\n -2.718e-05 * 0.98078528040323 / 2.384e-06,\n 5.22137e-04 * 0.98078528040323 / 2.384e-06,\n 7.703304e-03 * 0.98078528040323 / 2.384e-06,\n -2.7815342e-02 * 0.98078528040323 / 2.384e-06,\n -4.290581e-03 * 0.98078528040323 / 2.384e-06,\n -6.11782e-04 * 0.98078528040323 / 2.384e-06,\n -3.7670e-05 * 0.98078528040323 / 2.384e-06,\n 1.989123673796580e-01, 1.847759065022573e+00,\n\n -1.907e-06 * 0.989176509964781 / 2.384e-06,\n 9.0122e-05 * 0.989176509964781 / 2.384e-06,\n 2.88486e-04 * 0.989176509964781 / 2.384e-06,\n -2.774239e-03 * 0.989176509964781 / 2.384e-06,\n 3.2248020e-02 * 0.989176509964781 / 2.384e-06,\n 4.748821e-03 * 0.989176509964781 / 2.384e-06,\n 8.38757e-04 * 0.989176509964781 / 2.384e-06,\n 5.9605e-05 * 0.989176509964781 / 2.384e-06, /* 3 */\n 3.338e-06 * 0.989176509964781 / 2.384e-06,\n -3.9577e-05 * 0.989176509964781 / 2.384e-06,\n 3.88145e-04 * 0.989176509964781 / 2.384e-06,\n 6.937027e-03 * 0.989176509964781 / 2.384e-06,\n -2.8532982e-02 * 0.989176509964781 / 2.384e-06,\n -4.395962e-03 * 0.989176509964781 / 2.384e-06,\n -6.46591e-04 * 0.989176509964781 / 2.384e-06,\n -4.0531e-05 * 0.989176509964781 / 2.384e-06,\n 1.483359875383474e-01, 1.913880671464418e+00,\n\n -1.907e-06 * 0.995184726672197 / 2.384e-06,\n 8.4400e-05 * 0.995184726672197 / 2.384e-06,\n 1.91689e-04 * 0.995184726672197 / 2.384e-06,\n -3.411293e-03 * 0.995184726672197 / 2.384e-06,\n 3.1706810e-02 * 0.995184726672197 / 2.384e-06,\n 4.728317e-03 * 0.995184726672197 / 2.384e-06,\n 8.09669e-04 * 0.995184726672197 / 2.384e-06,\n 5.579e-05 * 0.995184726672197 / 2.384e-06,\n 3.338e-06 * 0.995184726672197 / 2.384e-06,\n -5.0545e-05 * 0.995184726672197 / 2.384e-06,\n 2.59876e-04 * 0.995184726672197 / 2.384e-06,\n 6.189346e-03 * 0.995184726672197 / 2.384e-06,\n -2.9224873e-02 * 0.995184726672197 / 2.384e-06,\n -4.489899e-03 * 0.995184726672197 / 2.384e-06,\n -6.80923e-04 * 0.995184726672197 / 2.384e-06,\n -4.3392e-05 * 0.995184726672197 / 2.384e-06,\n 9.849140335716425e-02, 1.961570560806461e+00,\n\n -2.384e-06 * 0.998795456205172 / 2.384e-06,\n 7.7724e-05 * 0.998795456205172 / 2.384e-06,\n 8.8215e-05 * 0.998795456205172 / 2.384e-06,\n -4.072189e-03 * 0.998795456205172 / 2.384e-06,\n 3.1132698e-02 * 0.998795456205172 / 2.384e-06,\n 4.691124e-03 * 0.998795456205172 / 2.384e-06,\n 7.79152e-04 * 0.998795456205172 / 2.384e-06,\n 5.2929e-05 * 0.998795456205172 / 2.384e-06,\n 2.861e-06 * 0.998795456205172 / 2.384e-06,\n -6.0558e-05 * 0.998795456205172 / 2.384e-06,\n 1.37329e-04 * 0.998795456205172 / 2.384e-06,\n 5.462170e-03 * 0.998795456205172 / 2.384e-06,\n -2.9890060e-02 * 0.998795456205172 / 2.384e-06,\n -4.570484e-03 * 0.998795456205172 / 2.384e-06,\n -7.14302e-04 * 0.998795456205172 / 2.384e-06,\n -4.6253e-05 * 0.998795456205172 / 2.384e-06,\n 4.912684976946725e-02, 1.990369453344394e+00,\n\n 3.5780907e-02 * Util.SQRT2 * 0.5 / 2.384e-06,\n 1.7876148e-02 * Util.SQRT2 * 0.5 / 2.384e-06,\n 3.134727e-03 * Util.SQRT2 * 0.5 / 2.384e-06,\n 2.457142e-03 * Util.SQRT2 * 0.5 / 2.384e-06,\n 9.71317e-04 * Util.SQRT2 * 0.5 / 2.384e-06,\n 2.18868e-04 * Util.SQRT2 * 0.5 / 2.384e-06,\n 1.01566e-04 * Util.SQRT2 * 0.5 / 2.384e-06,\n 1.3828e-05 * Util.SQRT2 * 0.5 / 2.384e-06,\n\n 3.0526638e-02 / 2.384e-06, 4.638195e-03 / 2.384e-06,\n 7.47204e-04 / 2.384e-06, 4.9591e-05 / 2.384e-06,\n 4.756451e-03 / 2.384e-06, 2.1458e-05 / 2.384e-06,\n -6.9618e-05 / 2.384e-06, /* 2.384e-06/2.384e-06 */\n ];\n\n var NS = 12;\n var NL = 36;\n\n var win = [\n [\n 2.382191739347913e-13,\n 6.423305872147834e-13,\n 9.400849094049688e-13,\n 1.122435026096556e-12,\n 1.183840321267481e-12,\n 1.122435026096556e-12,\n 9.400849094049690e-13,\n 6.423305872147839e-13,\n 2.382191739347918e-13,\n\n 5.456116108943412e-12,\n 4.878985199565852e-12,\n 4.240448995017367e-12,\n 3.559909094758252e-12,\n 2.858043359288075e-12,\n 2.156177623817898e-12,\n 1.475637723558783e-12,\n 8.371015190102974e-13,\n 2.599706096327376e-13,\n\n -5.456116108943412e-12,\n -4.878985199565852e-12,\n -4.240448995017367e-12,\n -3.559909094758252e-12,\n -2.858043359288076e-12,\n -2.156177623817898e-12,\n -1.475637723558783e-12,\n -8.371015190102975e-13,\n -2.599706096327376e-13,\n\n -2.382191739347923e-13,\n -6.423305872147843e-13,\n -9.400849094049696e-13,\n -1.122435026096556e-12,\n -1.183840321267481e-12,\n -1.122435026096556e-12,\n -9.400849094049694e-13,\n -6.423305872147840e-13,\n -2.382191739347918e-13,\n ],\n [\n 2.382191739347913e-13,\n 6.423305872147834e-13,\n 9.400849094049688e-13,\n 1.122435026096556e-12,\n 1.183840321267481e-12,\n 1.122435026096556e-12,\n 9.400849094049688e-13,\n 6.423305872147841e-13,\n 2.382191739347918e-13,\n\n 5.456116108943413e-12,\n 4.878985199565852e-12,\n 4.240448995017367e-12,\n 3.559909094758253e-12,\n 2.858043359288075e-12,\n 2.156177623817898e-12,\n 1.475637723558782e-12,\n 8.371015190102975e-13,\n 2.599706096327376e-13,\n\n -5.461314069809755e-12,\n -4.921085770524055e-12,\n -4.343405037091838e-12,\n -3.732668368707687e-12,\n -3.093523840190885e-12,\n -2.430835727329465e-12,\n -1.734679010007751e-12,\n -9.748253656609281e-13,\n -2.797435120168326e-13,\n\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n -2.283748241799531e-13,\n -4.037858874020686e-13,\n -2.146547464825323e-13,\n ],\n [\n 1.316524975873958e-01, /* win[SHORT_TYPE] */\n 4.142135623730950e-01,\n 7.673269879789602e-01,\n\n 1.091308501069271e+00, /* tantab_l */\n 1.303225372841206e+00,\n 1.569685577117490e+00,\n 1.920982126971166e+00,\n 2.414213562373094e+00,\n 3.171594802363212e+00,\n 4.510708503662055e+00,\n 7.595754112725146e+00,\n 2.290376554843115e+01,\n\n 0.98480775301220802032, /* cx */\n 0.64278760968653936292,\n 0.34202014332566882393,\n 0.93969262078590842791,\n -0.17364817766693030343,\n -0.76604444311897790243,\n 0.86602540378443870761,\n 0.500000000000000e+00,\n\n -5.144957554275265e-01, /* ca */\n -4.717319685649723e-01,\n -3.133774542039019e-01,\n -1.819131996109812e-01,\n -9.457419252642064e-02,\n -4.096558288530405e-02,\n -1.419856857247115e-02,\n -3.699974673760037e-03,\n\n 8.574929257125442e-01, /* cs */\n 8.817419973177052e-01,\n 9.496286491027329e-01,\n 9.833145924917901e-01,\n 9.955178160675857e-01,\n 9.991605581781475e-01,\n 9.998991952444470e-01,\n 9.999931550702802e-01,\n ],\n [\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 0.000000000000000e+00,\n 2.283748241799531e-13,\n 4.037858874020686e-13,\n 2.146547464825323e-13,\n\n 5.461314069809755e-12,\n 4.921085770524055e-12,\n 4.343405037091838e-12,\n 3.732668368707687e-12,\n 3.093523840190885e-12,\n 2.430835727329466e-12,\n 1.734679010007751e-12,\n 9.748253656609281e-13,\n 2.797435120168326e-13,\n\n -5.456116108943413e-12,\n -4.878985199565852e-12,\n -4.240448995017367e-12,\n -3.559909094758253e-12,\n -2.858043359288075e-12,\n -2.156177623817898e-12,\n -1.475637723558782e-12,\n -8.371015190102975e-13,\n -2.599706096327376e-13,\n\n -2.382191739347913e-13,\n -6.423305872147834e-13,\n -9.400849094049688e-13,\n -1.122435026096556e-12,\n -1.183840321267481e-12,\n -1.122435026096556e-12,\n -9.400849094049688e-13,\n -6.423305872147841e-13,\n -2.382191739347918e-13,\n ]\n ];\n\n var tantab_l = win[Encoder.SHORT_TYPE];\n var cx = win[Encoder.SHORT_TYPE];\n var ca = win[Encoder.SHORT_TYPE];\n var cs = win[Encoder.SHORT_TYPE];\n\n /**\n * new IDCT routine written by Takehiro TOMINAGA\n *\n * PURPOSE: Overlapping window on PCM samples
\n *\n * SEMANTICS:
\n * 32 16-bit pcm samples are scaled to fractional 2's complement and\n * concatenated to the end of the window buffer #x#. The updated window\n * buffer #x# is then windowed by the analysis window #c# to produce the\n * windowed sample #z#\n */\n var order = [\n 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,\n 2, 3, 18, 19, 10, 11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31\n ];\n\n /**\n * returns sum_j=0^31 a[j]*cos(PI*j*(k+1/2)/32), 0<=k<32\n */\n function window_subband(x1, x1Pos, a) {\n var wp = 10;\n\n var x2 = x1Pos + 238 - 14 - 286;\n\n for (var i = -15; i < 0; i++) {\n var w, s, t;\n\n w = enwindow[wp + -10];\n s = x1[x2 + -224] * w;\n t = x1[x1Pos + 224] * w;\n w = enwindow[wp + -9];\n s += x1[x2 + -160] * w;\n t += x1[x1Pos + 160] * w;\n w = enwindow[wp + -8];\n s += x1[x2 + -96] * w;\n t += x1[x1Pos + 96] * w;\n w = enwindow[wp + -7];\n s += x1[x2 + -32] * w;\n t += x1[x1Pos + 32] * w;\n w = enwindow[wp + -6];\n s += x1[x2 + 32] * w;\n t += x1[x1Pos + -32] * w;\n w = enwindow[wp + -5];\n s += x1[x2 + 96] * w;\n t += x1[x1Pos + -96] * w;\n w = enwindow[wp + -4];\n s += x1[x2 + 160] * w;\n t += x1[x1Pos + -160] * w;\n w = enwindow[wp + -3];\n s += x1[x2 + 224] * w;\n t += x1[x1Pos + -224] * w;\n\n w = enwindow[wp + -2];\n s += x1[x1Pos + -256] * w;\n t -= x1[x2 + 256] * w;\n w = enwindow[wp + -1];\n s += x1[x1Pos + -192] * w;\n t -= x1[x2 + 192] * w;\n w = enwindow[wp + 0];\n s += x1[x1Pos + -128] * w;\n t -= x1[x2 + 128] * w;\n w = enwindow[wp + 1];\n s += x1[x1Pos + -64] * w;\n t -= x1[x2 + 64] * w;\n w = enwindow[wp + 2];\n s += x1[x1Pos + 0] * w;\n t -= x1[x2 + 0] * w;\n w = enwindow[wp + 3];\n s += x1[x1Pos + 64] * w;\n t -= x1[x2 + -64] * w;\n w = enwindow[wp + 4];\n s += x1[x1Pos + 128] * w;\n t -= x1[x2 + -128] * w;\n w = enwindow[wp + 5];\n s += x1[x1Pos + 192] * w;\n t -= x1[x2 + -192] * w;\n\n /*\n * this multiplyer could be removed, but it needs more 256 FLOAT\n * data. thinking about the data cache performance, I think we\n * should not use such a huge table. tt 2000/Oct/25\n */\n s *= enwindow[wp + 6];\n w = t - s;\n a[30 + i * 2] = t + s;\n a[31 + i * 2] = enwindow[wp + 7] * w;\n wp += 18;\n x1Pos--;\n x2++;\n }\n {\n var s, t, u, v;\n t = x1[x1Pos + -16] * enwindow[wp + -10];\n s = x1[x1Pos + -32] * enwindow[wp + -2];\n t += (x1[x1Pos + -48] - x1[x1Pos + 16]) * enwindow[wp + -9];\n s += x1[x1Pos + -96] * enwindow[wp + -1];\n t += (x1[x1Pos + -80] + x1[x1Pos + 48]) * enwindow[wp + -8];\n s += x1[x1Pos + -160] * enwindow[wp + 0];\n t += (x1[x1Pos + -112] - x1[x1Pos + 80]) * enwindow[wp + -7];\n s += x1[x1Pos + -224] * enwindow[wp + 1];\n t += (x1[x1Pos + -144] + x1[x1Pos + 112]) * enwindow[wp + -6];\n s -= x1[x1Pos + 32] * enwindow[wp + 2];\n t += (x1[x1Pos + -176] - x1[x1Pos + 144]) * enwindow[wp + -5];\n s -= x1[x1Pos + 96] * enwindow[wp + 3];\n t += (x1[x1Pos + -208] + x1[x1Pos + 176]) * enwindow[wp + -4];\n s -= x1[x1Pos + 160] * enwindow[wp + 4];\n t += (x1[x1Pos + -240] - x1[x1Pos + 208]) * enwindow[wp + -3];\n s -= x1[x1Pos + 224];\n\n u = s - t;\n v = s + t;\n\n t = a[14];\n s = a[15] - t;\n\n a[31] = v + t; /* A0 */\n a[30] = u + s; /* A1 */\n a[15] = u - s; /* A2 */\n a[14] = v - t; /* A3 */\n }\n {\n var xr;\n xr = a[28] - a[0];\n a[0] += a[28];\n a[28] = xr * enwindow[wp + -2 * 18 + 7];\n xr = a[29] - a[1];\n a[1] += a[29];\n a[29] = xr * enwindow[wp + -2 * 18 + 7];\n\n xr = a[26] - a[2];\n a[2] += a[26];\n a[26] = xr * enwindow[wp + -4 * 18 + 7];\n xr = a[27] - a[3];\n a[3] += a[27];\n a[27] = xr * enwindow[wp + -4 * 18 + 7];\n\n xr = a[24] - a[4];\n a[4] += a[24];\n a[24] = xr * enwindow[wp + -6 * 18 + 7];\n xr = a[25] - a[5];\n a[5] += a[25];\n a[25] = xr * enwindow[wp + -6 * 18 + 7];\n\n xr = a[22] - a[6];\n a[6] += a[22];\n a[22] = xr * Util.SQRT2;\n xr = a[23] - a[7];\n a[7] += a[23];\n a[23] = xr * Util.SQRT2 - a[7];\n a[7] -= a[6];\n a[22] -= a[7];\n a[23] -= a[22];\n\n xr = a[6];\n a[6] = a[31] - xr;\n a[31] = a[31] + xr;\n xr = a[7];\n a[7] = a[30] - xr;\n a[30] = a[30] + xr;\n xr = a[22];\n a[22] = a[15] - xr;\n a[15] = a[15] + xr;\n xr = a[23];\n a[23] = a[14] - xr;\n a[14] = a[14] + xr;\n\n xr = a[20] - a[8];\n a[8] += a[20];\n a[20] = xr * enwindow[wp + -10 * 18 + 7];\n xr = a[21] - a[9];\n a[9] += a[21];\n a[21] = xr * enwindow[wp + -10 * 18 + 7];\n\n xr = a[18] - a[10];\n a[10] += a[18];\n a[18] = xr * enwindow[wp + -12 * 18 + 7];\n xr = a[19] - a[11];\n a[11] += a[19];\n a[19] = xr * enwindow[wp + -12 * 18 + 7];\n\n xr = a[16] - a[12];\n a[12] += a[16];\n a[16] = xr * enwindow[wp + -14 * 18 + 7];\n xr = a[17] - a[13];\n a[13] += a[17];\n a[17] = xr * enwindow[wp + -14 * 18 + 7];\n\n xr = -a[20] + a[24];\n a[20] += a[24];\n a[24] = xr * enwindow[wp + -12 * 18 + 7];\n xr = -a[21] + a[25];\n a[21] += a[25];\n a[25] = xr * enwindow[wp + -12 * 18 + 7];\n\n xr = a[4] - a[8];\n a[4] += a[8];\n a[8] = xr * enwindow[wp + -12 * 18 + 7];\n xr = a[5] - a[9];\n a[5] += a[9];\n a[9] = xr * enwindow[wp + -12 * 18 + 7];\n\n xr = a[0] - a[12];\n a[0] += a[12];\n a[12] = xr * enwindow[wp + -4 * 18 + 7];\n xr = a[1] - a[13];\n a[1] += a[13];\n a[13] = xr * enwindow[wp + -4 * 18 + 7];\n xr = a[16] - a[28];\n a[16] += a[28];\n a[28] = xr * enwindow[wp + -4 * 18 + 7];\n xr = -a[17] + a[29];\n a[17] += a[29];\n a[29] = xr * enwindow[wp + -4 * 18 + 7];\n\n xr = Util.SQRT2 * (a[2] - a[10]);\n a[2] += a[10];\n a[10] = xr;\n xr = Util.SQRT2 * (a[3] - a[11]);\n a[3] += a[11];\n a[11] = xr;\n xr = Util.SQRT2 * (-a[18] + a[26]);\n a[18] += a[26];\n a[26] = xr - a[18];\n xr = Util.SQRT2 * (-a[19] + a[27]);\n a[19] += a[27];\n a[27] = xr - a[19];\n\n xr = a[2];\n a[19] -= a[3];\n a[3] -= xr;\n a[2] = a[31] - xr;\n a[31] += xr;\n xr = a[3];\n a[11] -= a[19];\n a[18] -= xr;\n a[3] = a[30] - xr;\n a[30] += xr;\n xr = a[18];\n a[27] -= a[11];\n a[19] -= xr;\n a[18] = a[15] - xr;\n a[15] += xr;\n\n xr = a[19];\n a[10] -= xr;\n a[19] = a[14] - xr;\n a[14] += xr;\n xr = a[10];\n a[11] -= xr;\n a[10] = a[23] - xr;\n a[23] += xr;\n xr = a[11];\n a[26] -= xr;\n a[11] = a[22] - xr;\n a[22] += xr;\n xr = a[26];\n a[27] -= xr;\n a[26] = a[7] - xr;\n a[7] += xr;\n\n xr = a[27];\n a[27] = a[6] - xr;\n a[6] += xr;\n\n xr = Util.SQRT2 * (a[0] - a[4]);\n a[0] += a[4];\n a[4] = xr;\n xr = Util.SQRT2 * (a[1] - a[5]);\n a[1] += a[5];\n a[5] = xr;\n xr = Util.SQRT2 * (a[16] - a[20]);\n a[16] += a[20];\n a[20] = xr;\n xr = Util.SQRT2 * (a[17] - a[21]);\n a[17] += a[21];\n a[21] = xr;\n\n xr = -Util.SQRT2 * (a[8] - a[12]);\n a[8] += a[12];\n a[12] = xr - a[8];\n xr = -Util.SQRT2 * (a[9] - a[13]);\n a[9] += a[13];\n a[13] = xr - a[9];\n xr = -Util.SQRT2 * (a[25] - a[29]);\n a[25] += a[29];\n a[29] = xr - a[25];\n xr = -Util.SQRT2 * (a[24] + a[28]);\n a[24] -= a[28];\n a[28] = xr - a[24];\n\n xr = a[24] - a[16];\n a[24] = xr;\n xr = a[20] - xr;\n a[20] = xr;\n xr = a[28] - xr;\n a[28] = xr;\n\n xr = a[25] - a[17];\n a[25] = xr;\n xr = a[21] - xr;\n a[21] = xr;\n xr = a[29] - xr;\n a[29] = xr;\n\n xr = a[17] - a[1];\n a[17] = xr;\n xr = a[9] - xr;\n a[9] = xr;\n xr = a[25] - xr;\n a[25] = xr;\n xr = a[5] - xr;\n a[5] = xr;\n xr = a[21] - xr;\n a[21] = xr;\n xr = a[13] - xr;\n a[13] = xr;\n xr = a[29] - xr;\n a[29] = xr;\n\n xr = a[1] - a[0];\n a[1] = xr;\n xr = a[16] - xr;\n a[16] = xr;\n xr = a[17] - xr;\n a[17] = xr;\n xr = a[8] - xr;\n a[8] = xr;\n xr = a[9] - xr;\n a[9] = xr;\n xr = a[24] - xr;\n a[24] = xr;\n xr = a[25] - xr;\n a[25] = xr;\n xr = a[4] - xr;\n a[4] = xr;\n xr = a[5] - xr;\n a[5] = xr;\n xr = a[20] - xr;\n a[20] = xr;\n xr = a[21] - xr;\n a[21] = xr;\n xr = a[12] - xr;\n a[12] = xr;\n xr = a[13] - xr;\n a[13] = xr;\n xr = a[28] - xr;\n a[28] = xr;\n xr = a[29] - xr;\n a[29] = xr;\n\n xr = a[0];\n a[0] += a[31];\n a[31] -= xr;\n xr = a[1];\n a[1] += a[30];\n a[30] -= xr;\n xr = a[16];\n a[16] += a[15];\n a[15] -= xr;\n xr = a[17];\n a[17] += a[14];\n a[14] -= xr;\n xr = a[8];\n a[8] += a[23];\n a[23] -= xr;\n xr = a[9];\n a[9] += a[22];\n a[22] -= xr;\n xr = a[24];\n a[24] += a[7];\n a[7] -= xr;\n xr = a[25];\n a[25] += a[6];\n a[6] -= xr;\n xr = a[4];\n a[4] += a[27];\n a[27] -= xr;\n xr = a[5];\n a[5] += a[26];\n a[26] -= xr;\n xr = a[20];\n a[20] += a[11];\n a[11] -= xr;\n xr = a[21];\n a[21] += a[10];\n a[10] -= xr;\n xr = a[12];\n a[12] += a[19];\n a[19] -= xr;\n xr = a[13];\n a[13] += a[18];\n a[18] -= xr;\n xr = a[28];\n a[28] += a[3];\n a[3] -= xr;\n xr = a[29];\n a[29] += a[2];\n a[2] -= xr;\n }\n }\n\n /**\n * Function: Calculation of the MDCT In the case of long blocks (type 0,1,3)\n * there are 36 coefficents in the time domain and 18 in the frequency\n * domain.
\n * In the case of short blocks (type 2) there are 3 transformations with\n * short length. This leads to 12 coefficents in the time and 6 in the\n * frequency domain. In this case the results are stored side by side in the\n * vector out[].\n *\n * New layer3\n */\n function mdct_short(inout, inoutPos) {\n for (var l = 0; l < 3; l++) {\n var tc0, tc1, tc2, ts0, ts1, ts2;\n\n ts0 = inout[inoutPos + 2 * 3] * win[Encoder.SHORT_TYPE][0]\n - inout[inoutPos + 5 * 3];\n tc0 = inout[inoutPos + 0 * 3] * win[Encoder.SHORT_TYPE][2]\n - inout[inoutPos + 3 * 3];\n tc1 = ts0 + tc0;\n tc2 = ts0 - tc0;\n\n ts0 = inout[inoutPos + 5 * 3] * win[Encoder.SHORT_TYPE][0]\n + inout[inoutPos + 2 * 3];\n tc0 = inout[inoutPos + 3 * 3] * win[Encoder.SHORT_TYPE][2]\n + inout[inoutPos + 0 * 3];\n ts1 = ts0 + tc0;\n ts2 = -ts0 + tc0;\n\n tc0 = (inout[inoutPos + 1 * 3] * win[Encoder.SHORT_TYPE][1] - inout[inoutPos + 4 * 3]) * 2.069978111953089e-11;\n /*\n * tritab_s [ 1 ]\n */\n ts0 = (inout[inoutPos + 4 * 3] * win[Encoder.SHORT_TYPE][1] + inout[inoutPos + 1 * 3]) * 2.069978111953089e-11;\n /*\n * tritab_s [ 1 ]\n */\n inout[inoutPos + 3 * 0] = tc1 * 1.907525191737280e-11 + tc0;\n /*\n * tritab_s[ 2 ]\n */\n inout[inoutPos + 3 * 5] = -ts1 * 1.907525191737280e-11 + ts0;\n /*\n * tritab_s[0 ]\n */\n tc2 = tc2 * 0.86602540378443870761 * 1.907525191737281e-11;\n /*\n * tritab_s[ 2]\n */\n ts1 = ts1 * 0.5 * 1.907525191737281e-11 + ts0;\n inout[inoutPos + 3 * 1] = tc2 - ts1;\n inout[inoutPos + 3 * 2] = tc2 + ts1;\n\n tc1 = tc1 * 0.5 * 1.907525191737281e-11 - tc0;\n ts2 = ts2 * 0.86602540378443870761 * 1.907525191737281e-11;\n /*\n * tritab_s[ 0]\n */\n inout[inoutPos + 3 * 3] = tc1 + ts2;\n inout[inoutPos + 3 * 4] = tc1 - ts2;\n\n inoutPos++;\n }\n }\n\n function mdct_long(out, outPos, _in) {\n var ct, st;\n {\n var tc1, tc2, tc3, tc4, ts5, ts6, ts7, ts8;\n /* 1,2, 5,6, 9,10, 13,14, 17 */\n tc1 = _in[17] - _in[9];\n tc3 = _in[15] - _in[11];\n tc4 = _in[14] - _in[12];\n ts5 = _in[0] + _in[8];\n ts6 = _in[1] + _in[7];\n ts7 = _in[2] + _in[6];\n ts8 = _in[3] + _in[5];\n\n out[outPos + 17] = (ts5 + ts7 - ts8) - (ts6 - _in[4]);\n st = (ts5 + ts7 - ts8) * cx[12 + 7] + (ts6 - _in[4]);\n ct = (tc1 - tc3 - tc4) * cx[12 + 6];\n out[outPos + 5] = ct + st;\n out[outPos + 6] = ct - st;\n\n tc2 = (_in[16] - _in[10]) * cx[12 + 6];\n ts6 = ts6 * cx[12 + 7] + _in[4];\n ct = tc1 * cx[12 + 0] + tc2 + tc3 * cx[12 + 1] + tc4 * cx[12 + 2];\n st = -ts5 * cx[12 + 4] + ts6 - ts7 * cx[12 + 5] + ts8 * cx[12 + 3];\n out[outPos + 1] = ct + st;\n out[outPos + 2] = ct - st;\n\n ct = tc1 * cx[12 + 1] - tc2 - tc3 * cx[12 + 2] + tc4 * cx[12 + 0];\n st = -ts5 * cx[12 + 5] + ts6 - ts7 * cx[12 + 3] + ts8 * cx[12 + 4];\n out[outPos + 9] = ct + st;\n out[outPos + 10] = ct - st;\n\n ct = tc1 * cx[12 + 2] - tc2 + tc3 * cx[12 + 0] - tc4 * cx[12 + 1];\n st = ts5 * cx[12 + 3] - ts6 + ts7 * cx[12 + 4] - ts8 * cx[12 + 5];\n out[outPos + 13] = ct + st;\n out[outPos + 14] = ct - st;\n }\n {\n var ts1, ts2, ts3, ts4, tc5, tc6, tc7, tc8;\n\n ts1 = _in[8] - _in[0];\n ts3 = _in[6] - _in[2];\n ts4 = _in[5] - _in[3];\n tc5 = _in[17] + _in[9];\n tc6 = _in[16] + _in[10];\n tc7 = _in[15] + _in[11];\n tc8 = _in[14] + _in[12];\n\n out[outPos + 0] = (tc5 + tc7 + tc8) + (tc6 + _in[13]);\n ct = (tc5 + tc7 + tc8) * cx[12 + 7] - (tc6 + _in[13]);\n st = (ts1 - ts3 + ts4) * cx[12 + 6];\n out[outPos + 11] = ct + st;\n out[outPos + 12] = ct - st;\n\n ts2 = (_in[7] - _in[1]) * cx[12 + 6];\n tc6 = _in[13] - tc6 * cx[12 + 7];\n ct = tc5 * cx[12 + 3] - tc6 + tc7 * cx[12 + 4] + tc8 * cx[12 + 5];\n st = ts1 * cx[12 + 2] + ts2 + ts3 * cx[12 + 0] + ts4 * cx[12 + 1];\n out[outPos + 3] = ct + st;\n out[outPos + 4] = ct - st;\n\n ct = -tc5 * cx[12 + 5] + tc6 - tc7 * cx[12 + 3] - tc8 * cx[12 + 4];\n st = ts1 * cx[12 + 1] + ts2 - ts3 * cx[12 + 2] - ts4 * cx[12 + 0];\n out[outPos + 7] = ct + st;\n out[outPos + 8] = ct - st;\n\n ct = -tc5 * cx[12 + 4] + tc6 - tc7 * cx[12 + 5] - tc8 * cx[12 + 3];\n st = ts1 * cx[12 + 0] - ts2 + ts3 * cx[12 + 1] - ts4 * cx[12 + 2];\n out[outPos + 15] = ct + st;\n out[outPos + 16] = ct - st;\n }\n }\n\n this.mdct_sub48 = function(gfc, w0, w1) {\n var wk = w0;\n var wkPos = 286;\n /* thinking cache performance, ch->gr loop is better than gr->ch loop */\n for (var ch = 0; ch < gfc.channels_out; ch++) {\n for (var gr = 0; gr < gfc.mode_gr; gr++) {\n var band;\n var gi = (gfc.l3_side.tt[gr][ch]);\n var mdct_enc = gi.xr;\n var mdct_encPos = 0;\n var samp = gfc.sb_sample[ch][1 - gr];\n var sampPos = 0;\n\n for (var k = 0; k < 18 / 2; k++) {\n window_subband(wk, wkPos, samp[sampPos]);\n window_subband(wk, wkPos + 32, samp[sampPos + 1]);\n sampPos += 2;\n wkPos += 64;\n /*\n * Compensate for inversion in the analysis filter\n */\n for (band = 1; band < 32; band += 2) {\n samp[sampPos - 1][band] *= -1;\n }\n }\n\n /*\n * Perform imdct of 18 previous subband samples + 18 current\n * subband samples\n */\n for (band = 0; band < 32; band++, mdct_encPos += 18) {\n var type = gi.block_type;\n var band0 = gfc.sb_sample[ch][gr];\n var band1 = gfc.sb_sample[ch][1 - gr];\n if (gi.mixed_block_flag != 0 && band < 2)\n type = 0;\n if (gfc.amp_filter[band] < 1e-12) {\n Arrays.fill(mdct_enc, mdct_encPos + 0,\n mdct_encPos + 18, 0);\n } else {\n if (gfc.amp_filter[band] < 1.0) {\n for (var k = 0; k < 18; k++)\n band1[k][order[band]] *= gfc.amp_filter[band];\n }\n if (type == Encoder.SHORT_TYPE) {\n for (var k = -NS / 4; k < 0; k++) {\n var w = win[Encoder.SHORT_TYPE][k + 3];\n mdct_enc[mdct_encPos + k * 3 + 9] = band0[9 + k][order[band]]\n * w - band0[8 - k][order[band]];\n mdct_enc[mdct_encPos + k * 3 + 18] = band0[14 - k][order[band]]\n * w + band0[15 + k][order[band]];\n mdct_enc[mdct_encPos + k * 3 + 10] = band0[15 + k][order[band]]\n * w - band0[14 - k][order[band]];\n mdct_enc[mdct_encPos + k * 3 + 19] = band1[2 - k][order[band]]\n * w + band1[3 + k][order[band]];\n mdct_enc[mdct_encPos + k * 3 + 11] = band1[3 + k][order[band]]\n * w - band1[2 - k][order[band]];\n mdct_enc[mdct_encPos + k * 3 + 20] = band1[8 - k][order[band]]\n * w + band1[9 + k][order[band]];\n }\n mdct_short(mdct_enc, mdct_encPos);\n } else {\n var work = new_float(18);\n for (var k = -NL / 4; k < 0; k++) {\n var a, b;\n a = win[type][k + 27]\n * band1[k + 9][order[band]]\n + win[type][k + 36]\n * band1[8 - k][order[band]];\n b = win[type][k + 9]\n * band0[k + 9][order[band]]\n - win[type][k + 18]\n * band0[8 - k][order[band]];\n work[k + 9] = a - b * tantab_l[3 + k + 9];\n work[k + 18] = a * tantab_l[3 + k + 9] + b;\n }\n\n mdct_long(mdct_enc, mdct_encPos, work);\n }\n }\n /*\n * Perform aliasing reduction butterfly\n */\n if (type != Encoder.SHORT_TYPE && band != 0) {\n for (var k = 7; k >= 0; --k) {\n var bu, bd;\n bu = mdct_enc[mdct_encPos + k] * ca[20 + k]\n + mdct_enc[mdct_encPos + -1 - k]\n * cs[28 + k];\n bd = mdct_enc[mdct_encPos + k] * cs[28 + k]\n - mdct_enc[mdct_encPos + -1 - k]\n * ca[20 + k];\n\n mdct_enc[mdct_encPos + -1 - k] = bu;\n mdct_enc[mdct_encPos + k] = bd;\n }\n }\n }\n }\n wk = w1;\n wkPos = 286;\n if (gfc.mode_gr == 1) {\n for (var i = 0; i < 18; i++) {\n System.arraycopy(gfc.sb_sample[ch][1][i], 0,\n gfc.sb_sample[ch][0][i], 0, 32);\n }\n }\n }\n }\n }\n\n//package mp3;\n\n\n function III_psy_ratio() {\n this.thm = new III_psy_xmin();\n this.en = new III_psy_xmin();\n }\n\n\n /**\n * ENCDELAY The encoder delay.\n *\n * Minimum allowed is MDCTDELAY (see below)\n *\n * The first 96 samples will be attenuated, so using a value less than 96\n * will result in corrupt data for the first 96-ENCDELAY samples.\n *\n * suggested: 576 set to 1160 to sync with FhG.\n */\n Encoder.ENCDELAY = 576;\n /**\n * make sure there is at least one complete frame after the last frame\n * containing real data\n *\n * Using a value of 288 would be sufficient for a a very sophisticated\n * decoder that can decode granule-by-granule instead of frame by frame. But\n * lets not assume this, and assume the decoder will not decode frame N\n * unless it also has data for frame N+1\n */\n Encoder.POSTDELAY = 1152;\n\n /**\n * delay of the MDCT used in mdct.c original ISO routines had a delay of\n * 528! Takehiro's routines:\n */\n Encoder.MDCTDELAY = 48;\n Encoder.FFTOFFSET = (224 + Encoder.MDCTDELAY);\n\n /**\n * Most decoders, including the one we use, have a delay of 528 samples.\n */\n Encoder.DECDELAY = 528;\n\n /**\n * number of subbands\n */\n Encoder.SBLIMIT = 32;\n\n /**\n * parition bands bands\n */\n Encoder.CBANDS = 64;\n\n /**\n * number of critical bands/scale factor bands where masking is computed\n */\n Encoder.SBPSY_l = 21;\n Encoder.SBPSY_s = 12;\n\n /**\n * total number of scalefactor bands encoded\n */\n Encoder.SBMAX_l = 22;\n Encoder.SBMAX_s = 13;\n Encoder.PSFB21 = 6;\n Encoder.PSFB12 = 6;\n\n /**\n * FFT sizes\n */\n Encoder.BLKSIZE = 1024;\n Encoder.HBLKSIZE = (Encoder.BLKSIZE / 2 + 1);\n Encoder.BLKSIZE_s = 256;\n Encoder.HBLKSIZE_s = (Encoder.BLKSIZE_s / 2 + 1);\n\n Encoder.NORM_TYPE = 0;\n Encoder.START_TYPE = 1;\n Encoder.SHORT_TYPE = 2;\n Encoder.STOP_TYPE = 3;\n\n /**\n * \n * Mode Extention:\n * When we are in stereo mode, there are 4 possible methods to store these\n * two channels. The stereo modes -m? are using a subset of them.\n *\n * -ms: MPG_MD_LR_LR\n * -mj: MPG_MD_LR_LR and MPG_MD_MS_LR\n * -mf: MPG_MD_MS_LR\n * -mi: all\n *
\n */\n Encoder.MPG_MD_LR_LR = 0;\n Encoder.MPG_MD_LR_I = 1;\n Encoder.MPG_MD_MS_LR = 2;\n Encoder.MPG_MD_MS_I = 3;\n\n Encoder.fircoef = [-0.0207887 * 5, -0.0378413 * 5,\n -0.0432472 * 5, -0.031183 * 5, 7.79609e-18 * 5, 0.0467745 * 5,\n 0.10091 * 5, 0.151365 * 5, 0.187098 * 5];\n\n function Encoder() {\n\n var FFTOFFSET = Encoder.FFTOFFSET;\n var MPG_MD_MS_LR = Encoder.MPG_MD_MS_LR;\n //BitStream bs;\n //PsyModel psy;\n //VBRTag vbr;\n //QuantizePVT qupvt;\n var bs = null;\n this.psy = null;\n var psy = null;\n var vbr = null;\n var qupvt = null;\n\n //public final void setModules(BitStream bs, PsyModel psy, QuantizePVT qupvt,\n // VBRTag vbr) {\n this.setModules = function (_bs, _psy, _qupvt, _vbr) {\n bs = _bs;\n this.psy = _psy;\n psy = _psy;\n vbr = _vbr;\n qupvt = _qupvt;\n };\n\n var newMDCT = new NewMDCT();\n\n /***********************************************************************\n *\n * encoder and decoder delays\n *\n ***********************************************************************/\n\n /**\n * \n * layer III enc->dec delay: 1056 (1057?) (observed)\n * layer II enc->dec delay: 480 (481?) (observed)\n *\n * polyphase 256-16 (dec or enc) = 240\n * mdct 256+32 (9*32) (dec or enc) = 288\n * total: 512+16\n *\n * My guess is that delay of polyphase filterbank is actualy 240.5\n * (there are technical reasons for this, see postings in mp3encoder).\n * So total Encode+Decode delay = ENCDELAY + 528 + 1\n *
\n */\n\n\n /**\n * auto-adjust of ATH, useful for low volume Gabriel Bouvigne 3 feb 2001\n *\n * modifies some values in gfp.internal_flags.ATH (gfc.ATH)\n */\n//private void adjust_ATH(final LameInternalFlags gfc) {\n function adjust_ATH(gfc) {\n var gr2_max, max_pow;\n\n if (gfc.ATH.useAdjust == 0) {\n gfc.ATH.adjust = 1.0;\n /* no adjustment */\n return;\n }\n\n /* jd - 2001 mar 12, 27, jun 30 */\n /* loudness based on equal loudness curve; */\n /* use granule with maximum combined loudness */\n max_pow = gfc.loudness_sq[0][0];\n gr2_max = gfc.loudness_sq[1][0];\n if (gfc.channels_out == 2) {\n max_pow += gfc.loudness_sq[0][1];\n gr2_max += gfc.loudness_sq[1][1];\n } else {\n max_pow += max_pow;\n gr2_max += gr2_max;\n }\n if (gfc.mode_gr == 2) {\n max_pow = Math.max(max_pow, gr2_max);\n }\n max_pow *= 0.5;\n /* max_pow approaches 1.0 for full band noise */\n\n /* jd - 2001 mar 31, jun 30 */\n /* user tuning of ATH adjustment region */\n max_pow *= gfc.ATH.aaSensitivityP;\n\n /*\n * adjust ATH depending on range of maximum value\n */\n\n /* jd - 2001 feb27, mar12,20, jun30, jul22 */\n /* continuous curves based on approximation */\n /* to GB's original values. */\n /* For an increase in approximate loudness, */\n /* set ATH adjust to adjust_limit immediately */\n /* after a delay of one frame. */\n /* For a loudness decrease, reduce ATH adjust */\n /* towards adjust_limit gradually. */\n /* max_pow is a loudness squared or a power. */\n if (max_pow > 0.03125) { /* ((1 - 0.000625)/ 31.98) from curve below */\n if (gfc.ATH.adjust >= 1.0) {\n gfc.ATH.adjust = 1.0;\n } else {\n /* preceding frame has lower ATH adjust; */\n /* ascend only to the preceding adjust_limit */\n /* in case there is leading low volume */\n if (gfc.ATH.adjust < gfc.ATH.adjustLimit) {\n gfc.ATH.adjust = gfc.ATH.adjustLimit;\n }\n }\n gfc.ATH.adjustLimit = 1.0;\n } else { /* adjustment curve */\n /* about 32 dB maximum adjust (0.000625) */\n var adj_lim_new = 31.98 * max_pow + 0.000625;\n if (gfc.ATH.adjust >= adj_lim_new) { /* descend gradually */\n gfc.ATH.adjust *= adj_lim_new * 0.075 + 0.925;\n if (gfc.ATH.adjust < adj_lim_new) { /* stop descent */\n gfc.ATH.adjust = adj_lim_new;\n }\n } else { /* ascend */\n if (gfc.ATH.adjustLimit >= adj_lim_new) {\n gfc.ATH.adjust = adj_lim_new;\n } else {\n /* preceding frame has lower ATH adjust; */\n /* ascend only to the preceding adjust_limit */\n if (gfc.ATH.adjust < gfc.ATH.adjustLimit) {\n gfc.ATH.adjust = gfc.ATH.adjustLimit;\n }\n }\n }\n gfc.ATH.adjustLimit = adj_lim_new;\n }\n }\n\n /**\n * \n * some simple statistics\n *\n * bitrate index 0: free bitrate . not allowed in VBR mode\n * : bitrates, kbps depending on MPEG version\n * bitrate index 15: forbidden\n *\n * mode_ext:\n * 0: LR\n * 1: LR-i\n * 2: MS\n * 3: MS-i\n *
\n */\n function updateStats(gfc) {\n var gr, ch;\n\n /* count bitrate indices */\n gfc.bitrate_stereoMode_Hist[gfc.bitrate_index][4]++;\n gfc.bitrate_stereoMode_Hist[15][4]++;\n\n /* count 'em for every mode extension in case of 2 channel encoding */\n if (gfc.channels_out == 2) {\n gfc.bitrate_stereoMode_Hist[gfc.bitrate_index][gfc.mode_ext]++;\n gfc.bitrate_stereoMode_Hist[15][gfc.mode_ext]++;\n }\n for (gr = 0; gr < gfc.mode_gr; ++gr) {\n for (ch = 0; ch < gfc.channels_out; ++ch) {\n var bt = gfc.l3_side.tt[gr][ch].block_type | 0;\n if (gfc.l3_side.tt[gr][ch].mixed_block_flag != 0)\n bt = 4;\n gfc.bitrate_blockType_Hist[gfc.bitrate_index][bt]++;\n gfc.bitrate_blockType_Hist[gfc.bitrate_index][5]++;\n gfc.bitrate_blockType_Hist[15][bt]++;\n gfc.bitrate_blockType_Hist[15][5]++;\n }\n }\n }\n\n function lame_encode_frame_init(gfp, inbuf) {\n var gfc = gfp.internal_flags;\n\n var ch, gr;\n\n if (gfc.lame_encode_frame_init == 0) {\n /* prime the MDCT/polyphase filterbank with a short block */\n var i, j;\n var primebuff0 = new_float(286 + 1152 + 576);\n var primebuff1 = new_float(286 + 1152 + 576);\n gfc.lame_encode_frame_init = 1;\n for (i = 0, j = 0; i < 286 + 576 * (1 + gfc.mode_gr); ++i) {\n if (i < 576 * gfc.mode_gr) {\n primebuff0[i] = 0;\n if (gfc.channels_out == 2)\n primebuff1[i] = 0;\n } else {\n primebuff0[i] = inbuf[0][j];\n if (gfc.channels_out == 2)\n primebuff1[i] = inbuf[1][j];\n ++j;\n }\n }\n /* polyphase filtering / mdct */\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n gfc.l3_side.tt[gr][ch].block_type = Encoder.SHORT_TYPE;\n }\n }\n newMDCT.mdct_sub48(gfc, primebuff0, primebuff1);\n\n /* check FFT will not use a negative starting offset */\n /* check if we have enough data for FFT */\n /* check if we have enough data for polyphase filterbank */\n }\n\n }\n\n /**\n * \n * encodeframe() Layer 3\n *\n * encode a single frame\n *\n *\n * lame_encode_frame()\n *\n *\n * gr 0 gr 1\n * inbuf: |--------------|--------------|--------------|\n *\n *\n * Polyphase (18 windows, each shifted 32)\n * gr 0:\n * window1 <----512---.\n * window18 <----512---.\n *\n * gr 1:\n * window1 <----512---.\n * window18 <----512---.\n *\n *\n *\n * MDCT output: |--------------|--------------|--------------|\n *\n * FFT's <---------1024---------.\n * <---------1024-------.\n *\n *\n *\n * inbuf = buffer of PCM data size=MP3 framesize\n * encoder acts on inbuf[ch][0], but output is delayed by MDCTDELAY\n * so the MDCT coefficints are from inbuf[ch][-MDCTDELAY]\n *\n * psy-model FFT has a 1 granule delay, so we feed it data for the\n * next granule.\n * FFT is centered over granule: 224+576+224\n * So FFT starts at: 576-224-MDCTDELAY\n *\n * MPEG2: FFT ends at: BLKSIZE+576-224-MDCTDELAY (1328)\n * MPEG1: FFT ends at: BLKSIZE+2*576-224-MDCTDELAY (1904)\n *\n * MPEG2: polyphase first window: [0..511]\n * 18th window: [544..1055] (1056)\n * MPEG1: 36th window: [1120..1631] (1632)\n * data needed: 512+framesize-32\n *\n * A close look newmdct.c shows that the polyphase filterbank\n * only uses data from [0..510] for each window. Perhaps because the window\n * used by the filterbank is zero for the last point, so Takehiro's\n * code doesn't bother to compute with it.\n *\n * FFT starts at 576-224-MDCTDELAY (304) = 576-FFTOFFSET\n *\n *
\n */\n\n\n this.lame_encode_mp3_frame = function (gfp, inbuf_l, inbuf_r, mp3buf, mp3bufPos, mp3buf_size) {\n var mp3count;\n var masking_LR = new_array_n([2, 2]);\n /*\n * LR masking &\n * energy\n */\n masking_LR[0][0] = new III_psy_ratio();\n masking_LR[0][1] = new III_psy_ratio();\n masking_LR[1][0] = new III_psy_ratio();\n masking_LR[1][1] = new III_psy_ratio();\n var masking_MS = new_array_n([2, 2]);\n /* MS masking & energy */\n masking_MS[0][0] = new III_psy_ratio();\n masking_MS[0][1] = new III_psy_ratio();\n masking_MS[1][0] = new III_psy_ratio();\n masking_MS[1][1] = new III_psy_ratio();\n //III_psy_ratio masking[][];\n var masking;\n /* pointer to selected maskings */\n var inbuf = [null, null];\n var gfc = gfp.internal_flags;\n\n var tot_ener = new_float_n([2, 4]);\n var ms_ener_ratio = [.5, .5];\n var pe = [[0., 0.], [0., 0.]];\n var pe_MS = [[0., 0.], [0., 0.]];\n\n//float[][] pe_use;\n var pe_use;\n\n var ch, gr;\n\n inbuf[0] = inbuf_l;\n inbuf[1] = inbuf_r;\n\n if (gfc.lame_encode_frame_init == 0) {\n /* first run? */\n lame_encode_frame_init(gfp, inbuf);\n\n }\n\n /********************** padding *****************************/\n /**\n * \n * padding method as described in\n * \"MPEG-Layer3 / Bitstream Syntax and Decoding\"\n * by Martin Sieler, Ralph Sperschneider\n *\n * note: there is no padding for the very first frame\n *\n * Robert Hegemann 2000-06-22\n *
\n */\n gfc.padding = 0;\n if ((gfc.slot_lag -= gfc.frac_SpF) < 0) {\n gfc.slot_lag += gfp.out_samplerate;\n gfc.padding = 1;\n }\n\n /****************************************\n * Stage 1: psychoacoustic model *\n ****************************************/\n\n if (gfc.psymodel != 0) {\n /*\n * psychoacoustic model psy model has a 1 granule (576) delay that\n * we must compensate for (mt 6/99).\n */\n var ret;\n var bufp = [null, null];\n /* address of beginning of left & right granule */\n var bufpPos = 0;\n /* address of beginning of left & right granule */\n var blocktype = new_int(2);\n\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n\n for (ch = 0; ch < gfc.channels_out; ch++) {\n bufp[ch] = inbuf[ch];\n bufpPos = 576 + gr * 576 - Encoder.FFTOFFSET;\n }\n if (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) {\n ret = psy.L3psycho_anal_vbr(gfp, bufp, bufpPos, gr,\n masking_LR, masking_MS, pe[gr], pe_MS[gr],\n tot_ener[gr], blocktype);\n } else {\n ret = psy.L3psycho_anal_ns(gfp, bufp, bufpPos, gr,\n masking_LR, masking_MS, pe[gr], pe_MS[gr],\n tot_ener[gr], blocktype);\n }\n if (ret != 0)\n return -4;\n\n if (gfp.mode == MPEGMode.JOINT_STEREO) {\n ms_ener_ratio[gr] = tot_ener[gr][2] + tot_ener[gr][3];\n if (ms_ener_ratio[gr] > 0)\n ms_ener_ratio[gr] = tot_ener[gr][3] / ms_ener_ratio[gr];\n }\n\n /* block type flags */\n for (ch = 0; ch < gfc.channels_out; ch++) {\n var cod_info = gfc.l3_side.tt[gr][ch];\n cod_info.block_type = blocktype[ch];\n cod_info.mixed_block_flag = 0;\n }\n }\n } else {\n /* no psy model */\n for (gr = 0; gr < gfc.mode_gr; gr++)\n for (ch = 0; ch < gfc.channels_out; ch++) {\n gfc.l3_side.tt[gr][ch].block_type = Encoder.NORM_TYPE;\n gfc.l3_side.tt[gr][ch].mixed_block_flag = 0;\n pe_MS[gr][ch] = pe[gr][ch] = 700;\n }\n }\n\n /* auto-adjust of ATH, useful for low volume */\n adjust_ATH(gfc);\n\n /****************************************\n * Stage 2: MDCT *\n ****************************************/\n\n /* polyphase filtering / mdct */\n newMDCT.mdct_sub48(gfc, inbuf[0], inbuf[1]);\n\n /****************************************\n * Stage 3: MS/LR decision *\n ****************************************/\n\n /* Here will be selected MS or LR coding of the 2 stereo channels */\n gfc.mode_ext = Encoder.MPG_MD_LR_LR;\n\n if (gfp.force_ms) {\n gfc.mode_ext = Encoder.MPG_MD_MS_LR;\n } else if (gfp.mode == MPEGMode.JOINT_STEREO) {\n /*\n * ms_ratio = is scaled, for historical reasons, to look like a\n * ratio of side_channel / total. 0 = signal is 100% mono .5 = L & R\n * uncorrelated\n */\n\n /**\n * \n * [0] and [1] are the results for the two granules in MPEG-1,\n * in MPEG-2 it's only a faked averaging of the same value\n * _prev is the value of the last granule of the previous frame\n * _next is the value of the first granule of the next frame\n *
\n */\n\n var sum_pe_MS = 0.;\n var sum_pe_LR = 0.;\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n sum_pe_MS += pe_MS[gr][ch];\n sum_pe_LR += pe[gr][ch];\n }\n }\n\n /* based on PE: M/S coding would not use much more bits than L/R */\n if (sum_pe_MS <= 1.00 * sum_pe_LR) {\n\n var gi0 = gfc.l3_side.tt[0];\n var gi1 = gfc.l3_side.tt[gfc.mode_gr - 1];\n\n if (gi0[0].block_type == gi0[1].block_type\n && gi1[0].block_type == gi1[1].block_type) {\n\n gfc.mode_ext = Encoder.MPG_MD_MS_LR;\n }\n }\n }\n\n /* bit and noise allocation */\n if (gfc.mode_ext == MPG_MD_MS_LR) {\n masking = masking_MS;\n /* use MS masking */\n pe_use = pe_MS;\n } else {\n masking = masking_LR;\n /* use LR masking */\n pe_use = pe;\n }\n\n /* copy data for MP3 frame analyzer */\n if (gfp.analysis && gfc.pinfo != null) {\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n gfc.pinfo.ms_ratio[gr] = gfc.ms_ratio[gr];\n gfc.pinfo.ms_ener_ratio[gr] = ms_ener_ratio[gr];\n gfc.pinfo.blocktype[gr][ch] = gfc.l3_side.tt[gr][ch].block_type;\n gfc.pinfo.pe[gr][ch] = pe_use[gr][ch];\n System.arraycopy(gfc.l3_side.tt[gr][ch].xr, 0,\n gfc.pinfo.xr[gr][ch], 0, 576);\n /*\n * in psymodel, LR and MS data was stored in pinfo. switch\n * to MS data:\n */\n if (gfc.mode_ext == MPG_MD_MS_LR) {\n gfc.pinfo.ers[gr][ch] = gfc.pinfo.ers[gr][ch + 2];\n System.arraycopy(gfc.pinfo.energy[gr][ch + 2], 0,\n gfc.pinfo.energy[gr][ch], 0,\n gfc.pinfo.energy[gr][ch].length);\n }\n }\n }\n }\n\n /****************************************\n * Stage 4: quantization loop *\n ****************************************/\n\n if (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_abr) {\n\n var i;\n var f;\n\n for (i = 0; i < 18; i++)\n gfc.nsPsy.pefirbuf[i] = gfc.nsPsy.pefirbuf[i + 1];\n\n f = 0.0;\n for (gr = 0; gr < gfc.mode_gr; gr++)\n for (ch = 0; ch < gfc.channels_out; ch++)\n f += pe_use[gr][ch];\n gfc.nsPsy.pefirbuf[18] = f;\n\n f = gfc.nsPsy.pefirbuf[9];\n for (i = 0; i < 9; i++)\n f += (gfc.nsPsy.pefirbuf[i] + gfc.nsPsy.pefirbuf[18 - i])\n * Encoder.fircoef[i];\n\n f = (670 * 5 * gfc.mode_gr * gfc.channels_out) / f;\n for (gr = 0; gr < gfc.mode_gr; gr++) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n pe_use[gr][ch] *= f;\n }\n }\n }\n gfc.iteration_loop.iteration_loop(gfp, pe_use, ms_ener_ratio, masking);\n\n /****************************************\n * Stage 5: bitstream formatting *\n ****************************************/\n\n /* write the frame to the bitstream */\n bs.format_bitstream(gfp);\n\n /* copy mp3 bit buffer into array */\n mp3count = bs.copy_buffer(gfc, mp3buf, mp3bufPos, mp3buf_size, 1);\n\n if (gfp.bWriteVbrTag)\n vbr.addVbrFrame(gfp);\n\n if (gfp.analysis && gfc.pinfo != null) {\n for (ch = 0; ch < gfc.channels_out; ch++) {\n var j;\n for (j = 0; j < FFTOFFSET; j++)\n gfc.pinfo.pcmdata[ch][j] = gfc.pinfo.pcmdata[ch][j\n + gfp.framesize];\n for (j = FFTOFFSET; j < 1600; j++) {\n gfc.pinfo.pcmdata[ch][j] = inbuf[ch][j - FFTOFFSET];\n }\n }\n qupvt.set_frame_pinfo(gfp, masking);\n }\n\n updateStats(gfc);\n\n return mp3count;\n }\n }\n\n\n//package mp3;\n\n function VBRSeekInfo() {\n /**\n * What we have seen so far.\n */\n this.sum = 0;\n /**\n * How many frames we have seen in this chunk.\n */\n this.seen = 0;\n /**\n * How many frames we want to collect into one chunk.\n */\n this.want = 0;\n /**\n * Actual position in our bag.\n */\n this.pos = 0;\n /**\n * Size of our bag.\n */\n this.size = 0;\n /**\n * Pointer to our bag.\n */\n this.bag = null;\n this.nVbrNumFrames = 0;\n this.nBytesWritten = 0;\n /* VBR tag data */\n this.TotalFrameSize = 0;\n }\n\n\n\n function IIISideInfo() {\n this.tt = [[null, null], [null, null]];\n this.main_data_begin = 0;\n this.private_bits = 0;\n this.resvDrain_pre = 0;\n this.resvDrain_post = 0;\n this.scfsi = [new_int(4), new_int(4)];\n\n for (var gr = 0; gr < 2; gr++) {\n for (var ch = 0; ch < 2; ch++) {\n this.tt[gr][ch] = new GrInfo();\n }\n }\n }\n\n\n function III_psy_xmin() {\n this.l = new_float(Encoder.SBMAX_l);\n this.s = new_float_n([Encoder.SBMAX_s, 3]);\n\n var self = this;\n this.assign = function (iii_psy_xmin) {\n System.arraycopy(iii_psy_xmin.l, 0, self.l, 0, Encoder.SBMAX_l);\n for (var i = 0; i < Encoder.SBMAX_s; i++) {\n for (var j = 0; j < 3; j++) {\n self.s[i][j] = iii_psy_xmin.s[i][j];\n }\n }\n }\n }\n\n\n\n//package mp3;\n\n /**\n * Variables used for --nspsytune\n *\n * @author Ken\n *\n */\n function NsPsy() {\n this.last_en_subshort = new_float_n([4, 9]);\n this.lastAttacks = new_int(4);\n this.pefirbuf = new_float(19);\n this.longfact = new_float(Encoder.SBMAX_l);\n this.shortfact = new_float(Encoder.SBMAX_s);\n\n /**\n * short block tuning\n */\n this.attackthre = 0.;\n this.attackthre_s = 0.;\n }\n\n\n\n\n LameInternalFlags.MFSIZE = (3 * 1152 + Encoder.ENCDELAY - Encoder.MDCTDELAY);\n LameInternalFlags.MAX_HEADER_BUF = 256;\n LameInternalFlags.MAX_BITS_PER_CHANNEL = 4095;\n LameInternalFlags.MAX_BITS_PER_GRANULE = 7680;\n LameInternalFlags.BPC = 320;\n\n function LameInternalFlags() {\n var MAX_HEADER_LEN = 40;\n\n\n /********************************************************************\n * internal variables NOT set by calling program, and should not be *\n * modified by the calling program *\n ********************************************************************/\n\n /**\n * Some remarks to the Class_ID field: The Class ID is an Identifier for a\n * pointer to this struct. It is very unlikely that a pointer to\n * lame_global_flags has the same 32 bits in it's structure (large and other\n * special properties, for instance prime).\n *\n * To test that the structure is right and initialized, use: if ( gfc .\n * Class_ID == LAME_ID ) ... Other remark: If you set a flag to 0 for uninit\n * data and 1 for init data, the right test should be \"if (flag == 1)\" and\n * NOT \"if (flag)\". Unintended modification of this element will be\n * otherwise misinterpreted as an init.\n */\n this.Class_ID = 0;\n\n this.lame_encode_frame_init = 0;\n this.iteration_init_init = 0;\n this.fill_buffer_resample_init = 0;\n\n //public float mfbuf[][] = new float[2][MFSIZE];\n this.mfbuf = new_float_n([2, LameInternalFlags.MFSIZE]);\n\n /**\n * granules per frame\n */\n this.mode_gr = 0;\n /**\n * number of channels in the input data stream (PCM or decoded PCM)\n */\n this.channels_in = 0;\n /**\n * number of channels in the output data stream (not used for decoding)\n */\n this.channels_out = 0;\n /**\n * input_samp_rate/output_samp_rate\n */\n //public double resample_ratio;\n this.resample_ratio = 0.;\n\n this.mf_samples_to_encode = 0;\n this.mf_size = 0;\n /**\n * min bitrate index\n */\n this.VBR_min_bitrate = 0;\n /**\n * max bitrate index\n */\n this.VBR_max_bitrate = 0;\n this.bitrate_index = 0;\n this.samplerate_index = 0;\n this.mode_ext = 0;\n\n /* lowpass and highpass filter control */\n /**\n * normalized frequency bounds of passband\n */\n this.lowpass1 = 0.;\n this.lowpass2 = 0.;\n /**\n * normalized frequency bounds of passband\n */\n this.highpass1 = 0.;\n this.highpass2 = 0.;\n\n /**\n * 0 = none 1 = ISO AAC model 2 = allow scalefac_select=1\n */\n this.noise_shaping = 0;\n\n /**\n * 0 = ISO model: amplify all distorted bands
\n * 1 = amplify within 50% of max (on db scale)
\n * 2 = amplify only most distorted band
\n * 3 = method 1 and refine with method 2
\n */\n this.noise_shaping_amp = 0;\n /**\n * 0 = no substep
\n * 1 = use substep shaping at last step(VBR only)
\n * (not implemented yet)
\n * 2 = use substep inside loop
\n * 3 = use substep inside loop and last step
\n */\n this.substep_shaping = 0;\n\n /**\n * 1 = gpsycho. 0 = none\n */\n this.psymodel = 0;\n /**\n * 0 = stop at over=0, all scalefacs amplified or
\n * a scalefac has reached max value
\n * 1 = stop when all scalefacs amplified or a scalefac has reached max value
\n * 2 = stop when all scalefacs amplified\n */\n this.noise_shaping_stop = 0;\n\n /**\n * 0 = no, 1 = yes\n */\n this.subblock_gain = 0;\n /**\n * 0 = no. 1=outside loop 2=inside loop(slow)\n */\n this.use_best_huffman = 0;\n\n /**\n * 0 = stop early after 0 distortion found. 1 = full search\n */\n this.full_outer_loop = 0;\n\n //public IIISideInfo l3_side = new IIISideInfo();\n this.l3_side = new IIISideInfo();\n this.ms_ratio = new_float(2);\n\n /* used for padding */\n /**\n * padding for the current frame?\n */\n this.padding = 0;\n this.frac_SpF = 0;\n this.slot_lag = 0;\n\n /**\n * optional ID3 tags\n */\n //public ID3TagSpec tag_spec;\n this.tag_spec = null;\n this.nMusicCRC = 0;\n\n /* variables used by Quantize */\n //public int OldValue[] = new int[2];\n this.OldValue = new_int(2);\n //public int CurrentStep[] = new int[2];\n this.CurrentStep = new_int(2);\n\n this.masking_lower = 0.;\n //public int bv_scf[] = new int[576];\n this.bv_scf = new_int(576);\n //public int pseudohalf[] = new int[L3Side.SFBMAX];\n this.pseudohalf = new_int(L3Side.SFBMAX);\n\n /**\n * will be set in lame_init_params\n */\n this.sfb21_extra = false;\n\n /* BPC = maximum number of filter convolution windows to precompute */\n //public float[][] inbuf_old = new float[2][];\n this.inbuf_old = new Array(2);\n //public float[][] blackfilt = new float[2 * BPC + 1][];\n this.blackfilt = new Array(2 * LameInternalFlags.BPC + 1);\n //public double itime[] = new double[2];\n this.itime = new_double(2);\n this.sideinfo_len = 0;\n\n /* variables for newmdct.c */\n //public float sb_sample[][][][] = new float[2][2][18][Encoder.SBLIMIT];\n this.sb_sample = new_float_n([2, 2, 18, Encoder.SBLIMIT]);\n this.amp_filter = new_float(32);\n\n /* variables for BitStream */\n\n /**\n * \n * mpeg1: buffer=511 bytes smallest frame: 96-38(sideinfo)=58\n * max number of frames in reservoir: 8\n * mpeg2: buffer=255 bytes. smallest frame: 24-23bytes=1\n * with VBR, if you are encoding all silence, it is possible to\n * have 8kbs/24khz frames with 1byte of data each, which means we need\n * to buffer up to 255 headers!\n *
\n */\n /**\n * also, max_header_buf has to be a power of two\n */\n /**\n * max size of header is 38\n */\n\n function Header() {\n this.write_timing = 0;\n this.ptr = 0;\n //public byte buf[] = new byte[MAX_HEADER_LEN];\n this.buf = new_byte(MAX_HEADER_LEN);\n }\n\n this.header = new Array(LameInternalFlags.MAX_HEADER_BUF);\n\n this.h_ptr = 0;\n this.w_ptr = 0;\n this.ancillary_flag = 0;\n\n /* variables for Reservoir */\n /**\n * in bits\n */\n this.ResvSize = 0;\n /**\n * in bits\n */\n this.ResvMax = 0;\n\n //public ScaleFac scalefac_band = new ScaleFac();\n this.scalefac_band = new ScaleFac();\n\n /* daa from PsyModel */\n /* The static variables \"r\", \"phi_sav\", \"new\", \"old\" and \"oldest\" have */\n /* to be remembered for the unpredictability measure. For \"r\" and */\n /* \"phi_sav\", the first index from the left is the channel select and */\n /* the second index is the \"age\" of the data. */\n this.minval_l = new_float(Encoder.CBANDS);\n this.minval_s = new_float(Encoder.CBANDS);\n this.nb_1 = new_float_n([4, Encoder.CBANDS]);\n this.nb_2 = new_float_n([4, Encoder.CBANDS]);\n this.nb_s1 = new_float_n([4, Encoder.CBANDS]);\n this.nb_s2 = new_float_n([4, Encoder.CBANDS]);\n this.s3_ss = null;\n this.s3_ll = null;\n this.decay = 0.;\n\n //public III_psy_xmin[] thm = new III_psy_xmin[4];\n //public III_psy_xmin[] en = new III_psy_xmin[4];\n this.thm = new Array(4);\n this.en = new Array(4);\n\n /**\n * fft and energy calculation\n */\n this.tot_ener = new_float(4);\n\n /* loudness calculation (for adaptive threshold of hearing) */\n /**\n * loudness^2 approx. per granule and channel\n */\n this.loudness_sq = new_float_n([2, 2]);\n /**\n * account for granule delay of L3psycho_anal\n */\n this.loudness_sq_save = new_float(2);\n\n /**\n * Scale Factor Bands\n */\n this.mld_l = new_float(Encoder.SBMAX_l);\n this.mld_s = new_float(Encoder.SBMAX_s);\n this.bm_l = new_int(Encoder.SBMAX_l);\n this.bo_l = new_int(Encoder.SBMAX_l);\n this.bm_s = new_int(Encoder.SBMAX_s);\n this.bo_s = new_int(Encoder.SBMAX_s);\n this.npart_l = 0;\n this.npart_s = 0;\n\n this.s3ind = new_int_n([Encoder.CBANDS, 2]);\n this.s3ind_s = new_int_n([Encoder.CBANDS, 2]);\n\n this.numlines_s = new_int(Encoder.CBANDS);\n this.numlines_l = new_int(Encoder.CBANDS);\n this.rnumlines_l = new_float(Encoder.CBANDS);\n this.mld_cb_l = new_float(Encoder.CBANDS);\n this.mld_cb_s = new_float(Encoder.CBANDS);\n this.numlines_s_num1 = 0;\n this.numlines_l_num1 = 0;\n\n /* ratios */\n this.pe = new_float(4);\n this.ms_ratio_s_old = 0.;\n this.ms_ratio_l_old = 0.;\n this.ms_ener_ratio_old = 0.;\n\n /**\n * block type\n */\n this.blocktype_old = new_int(2);\n\n /**\n * variables used for --nspsytune\n */\n this.nsPsy = new NsPsy();\n\n /**\n * used for Xing VBR header\n */\n this.VBR_seek_table = new VBRSeekInfo();\n\n /**\n * all ATH related stuff\n */\n //public ATH ATH;\n this.ATH = null;\n\n this.PSY = null;\n\n this.nogap_total = 0;\n this.nogap_current = 0;\n\n /* ReplayGain */\n this.decode_on_the_fly = true;\n this.findReplayGain = true;\n this.findPeakSample = true;\n this.PeakSample = 0.;\n this.RadioGain = 0;\n this.AudiophileGain = 0;\n //public ReplayGain rgdata;\n this.rgdata = null;\n\n /**\n * gain change required for preventing clipping\n */\n this.noclipGainChange = 0;\n /**\n * user-specified scale factor required for preventing clipping\n */\n this.noclipScale = 0.;\n\n /* simple statistics */\n this.bitrate_stereoMode_Hist = new_int_n([16, 4 + 1]);\n /**\n * norm/start/short/stop/mixed(short)/sum\n */\n this.bitrate_blockType_Hist = new_int_n([16, 4 + 1 + 1]);\n\n //public PlottingData pinfo;\n //public MPGLib.mpstr_tag hip;\n this.pinfo = null;\n this.hip = null;\n\n this.in_buffer_nsamples = 0;\n //public float[] in_buffer_0;\n //public float[] in_buffer_1;\n this.in_buffer_0 = null;\n this.in_buffer_1 = null;\n\n //public IIterationLoop iteration_loop;\n this.iteration_loop = null;\n\n for (var i = 0; i < this.en.length; i++) {\n this.en[i] = new III_psy_xmin();\n }\n for (var i = 0; i < this.thm.length; i++) {\n this.thm[i] = new III_psy_xmin();\n }\n for (var i = 0; i < this.header.length; i++) {\n this.header[i] = new Header();\n }\n\n }\n\n\n\n function FFT() {\n\n var window = new_float(Encoder.BLKSIZE);\n var window_s = new_float(Encoder.BLKSIZE_s / 2);\n\n var costab = [\n 9.238795325112867e-01, 3.826834323650898e-01,\n 9.951847266721969e-01, 9.801714032956060e-02,\n 9.996988186962042e-01, 2.454122852291229e-02,\n 9.999811752826011e-01, 6.135884649154475e-03\n ];\n\n function fht(fz, fzPos, n) {\n var tri = 0;\n var k4;\n var fi;\n var gi;\n\n n <<= 1;\n /* to get BLKSIZE, because of 3DNow! ASM routine */\n var fn = fzPos + n;\n k4 = 4;\n do {\n var s1, c1;\n var i, k1, k2, k3, kx;\n kx = k4 >> 1;\n k1 = k4;\n k2 = k4 << 1;\n k3 = k2 + k1;\n k4 = k2 << 1;\n fi = fzPos;\n gi = fi + kx;\n do {\n var f0, f1, f2, f3;\n f1 = fz[fi + 0] - fz[fi + k1];\n f0 = fz[fi + 0] + fz[fi + k1];\n f3 = fz[fi + k2] - fz[fi + k3];\n f2 = fz[fi + k2] + fz[fi + k3];\n fz[fi + k2] = f0 - f2;\n fz[fi + 0] = f0 + f2;\n fz[fi + k3] = f1 - f3;\n fz[fi + k1] = f1 + f3;\n f1 = fz[gi + 0] - fz[gi + k1];\n f0 = fz[gi + 0] + fz[gi + k1];\n f3 = (Util.SQRT2 * fz[gi + k3]);\n f2 = (Util.SQRT2 * fz[gi + k2]);\n fz[gi + k2] = f0 - f2;\n fz[gi + 0] = f0 + f2;\n fz[gi + k3] = f1 - f3;\n fz[gi + k1] = f1 + f3;\n gi += k4;\n fi += k4;\n } while (fi < fn);\n c1 = costab[tri + 0];\n s1 = costab[tri + 1];\n for (i = 1; i < kx; i++) {\n var c2, s2;\n c2 = 1 - (2 * s1) * s1;\n s2 = (2 * s1) * c1;\n fi = fzPos + i;\n gi = fzPos + k1 - i;\n do {\n var a, b, g0, f0, f1, g1, f2, g2, f3, g3;\n b = s2 * fz[fi + k1] - c2 * fz[gi + k1];\n a = c2 * fz[fi + k1] + s2 * fz[gi + k1];\n f1 = fz[fi + 0] - a;\n f0 = fz[fi + 0] + a;\n g1 = fz[gi + 0] - b;\n g0 = fz[gi + 0] + b;\n b = s2 * fz[fi + k3] - c2 * fz[gi + k3];\n a = c2 * fz[fi + k3] + s2 * fz[gi + k3];\n f3 = fz[fi + k2] - a;\n f2 = fz[fi + k2] + a;\n g3 = fz[gi + k2] - b;\n g2 = fz[gi + k2] + b;\n b = s1 * f2 - c1 * g3;\n a = c1 * f2 + s1 * g3;\n fz[fi + k2] = f0 - a;\n fz[fi + 0] = f0 + a;\n fz[gi + k3] = g1 - b;\n fz[gi + k1] = g1 + b;\n b = c1 * g2 - s1 * f3;\n a = s1 * g2 + c1 * f3;\n fz[gi + k2] = g0 - a;\n fz[gi + 0] = g0 + a;\n fz[fi + k3] = f1 - b;\n fz[fi + k1] = f1 + b;\n gi += k4;\n fi += k4;\n } while (fi < fn);\n c2 = c1;\n c1 = c2 * costab[tri + 0] - s1 * costab[tri + 1];\n s1 = c2 * costab[tri + 1] + s1 * costab[tri + 0];\n }\n tri += 2;\n } while (k4 < n);\n }\n\n var rv_tbl = [0x00, 0x80, 0x40,\n 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10,\n 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70,\n 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28,\n 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58,\n 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04,\n 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64,\n 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34,\n 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c,\n 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c,\n 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c,\n 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22,\n 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52,\n 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a,\n 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a,\n 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a,\n 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46,\n 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16,\n 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76,\n 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e,\n 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e,\n 0xde, 0x3e, 0xbe, 0x7e, 0xfe];\n\n this.fft_short = function (gfc, x_real, chn, buffer, bufPos) {\n for (var b = 0; b < 3; b++) {\n var x = Encoder.BLKSIZE_s / 2;\n var k = 0xffff & ((576 / 3) * (b + 1));\n var j = Encoder.BLKSIZE_s / 8 - 1;\n do {\n var f0, f1, f2, f3, w;\n var i = rv_tbl[j << 2] & 0xff;\n\n f0 = window_s[i] * buffer[chn][bufPos + i + k];\n w = window_s[0x7f - i] * buffer[chn][bufPos + i + k + 0x80];\n f1 = f0 - w;\n f0 = f0 + w;\n f2 = window_s[i + 0x40] * buffer[chn][bufPos + i + k + 0x40];\n w = window_s[0x3f - i] * buffer[chn][bufPos + i + k + 0xc0];\n f3 = f2 - w;\n f2 = f2 + w;\n\n x -= 4;\n x_real[b][x + 0] = f0 + f2;\n x_real[b][x + 2] = f0 - f2;\n x_real[b][x + 1] = f1 + f3;\n x_real[b][x + 3] = f1 - f3;\n\n f0 = window_s[i + 0x01] * buffer[chn][bufPos + i + k + 0x01];\n w = window_s[0x7e - i] * buffer[chn][bufPos + i + k + 0x81];\n f1 = f0 - w;\n f0 = f0 + w;\n f2 = window_s[i + 0x41] * buffer[chn][bufPos + i + k + 0x41];\n w = window_s[0x3e - i] * buffer[chn][bufPos + i + k + 0xc1];\n f3 = f2 - w;\n f2 = f2 + w;\n\n x_real[b][x + Encoder.BLKSIZE_s / 2 + 0] = f0 + f2;\n x_real[b][x + Encoder.BLKSIZE_s / 2 + 2] = f0 - f2;\n x_real[b][x + Encoder.BLKSIZE_s / 2 + 1] = f1 + f3;\n x_real[b][x + Encoder.BLKSIZE_s / 2 + 3] = f1 - f3;\n } while (--j >= 0);\n\n fht(x_real[b], x, Encoder.BLKSIZE_s / 2);\n /* BLKSIZE_s/2 because of 3DNow! ASM routine */\n /* BLKSIZE/2 because of 3DNow! ASM routine */\n }\n }\n\n this.fft_long = function (gfc, y, chn, buffer, bufPos) {\n var jj = Encoder.BLKSIZE / 8 - 1;\n var x = Encoder.BLKSIZE / 2;\n\n do {\n var f0, f1, f2, f3, w;\n var i = rv_tbl[jj] & 0xff;\n f0 = window[i] * buffer[chn][bufPos + i];\n w = window[i + 0x200] * buffer[chn][bufPos + i + 0x200];\n f1 = f0 - w;\n f0 = f0 + w;\n f2 = window[i + 0x100] * buffer[chn][bufPos + i + 0x100];\n w = window[i + 0x300] * buffer[chn][bufPos + i + 0x300];\n f3 = f2 - w;\n f2 = f2 + w;\n\n x -= 4;\n y[x + 0] = f0 + f2;\n y[x + 2] = f0 - f2;\n y[x + 1] = f1 + f3;\n y[x + 3] = f1 - f3;\n\n f0 = window[i + 0x001] * buffer[chn][bufPos + i + 0x001];\n w = window[i + 0x201] * buffer[chn][bufPos + i + 0x201];\n f1 = f0 - w;\n f0 = f0 + w;\n f2 = window[i + 0x101] * buffer[chn][bufPos + i + 0x101];\n w = window[i + 0x301] * buffer[chn][bufPos + i + 0x301];\n f3 = f2 - w;\n f2 = f2 + w;\n\n y[x + Encoder.BLKSIZE / 2 + 0] = f0 + f2;\n y[x + Encoder.BLKSIZE / 2 + 2] = f0 - f2;\n y[x + Encoder.BLKSIZE / 2 + 1] = f1 + f3;\n y[x + Encoder.BLKSIZE / 2 + 3] = f1 - f3;\n } while (--jj >= 0);\n\n fht(y, x, Encoder.BLKSIZE / 2);\n /* BLKSIZE/2 because of 3DNow! ASM routine */\n }\n\n this.init_fft = function (gfc) {\n /* The type of window used here will make no real difference, but */\n /*\n * in the interest of merging nspsytune stuff - switch to blackman\n * window\n */\n for (var i = 0; i < Encoder.BLKSIZE; i++)\n /* blackman window */\n window[i] = (0.42 - 0.5 * Math.cos(2 * Math.PI * (i + .5)\n / Encoder.BLKSIZE) + 0.08 * Math.cos(4 * Math.PI * (i + .5)\n / Encoder.BLKSIZE));\n\n for (var i = 0; i < Encoder.BLKSIZE_s / 2; i++)\n window_s[i] = (0.5 * (1.0 - Math.cos(2.0 * Math.PI\n * (i + 0.5) / Encoder.BLKSIZE_s)));\n\n }\n\n }\n\n /*\n * psymodel.c\n *\n * Copyright (c) 1999-2000 Mark Taylor\n * Copyright (c) 2001-2002 Naoki Shibata\n * Copyright (c) 2000-2003 Takehiro Tominaga\n * Copyright (c) 2000-2008 Robert Hegemann\n * Copyright (c) 2000-2005 Gabriel Bouvigne\n * Copyright (c) 2000-2005 Alexander Leidinger\n *\n * This library is free software; you can redistribute it and/or\n * modify it under the terms of the GNU Lesser General Public\n * License as published by the Free Software Foundation; either\n * version 2 of the License, or (at your option) any later version.\n *\n * This library is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n * Library General Public License for more details.\n *\n * You should have received a copy of the GNU Lesser General Public\n * License along with this library; if not, write to the\n * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n * Boston, MA 02111-1307, USA.\n */\n\n /* $Id: PsyModel.java,v 1.27 2011/05/24 20:48:06 kenchis Exp $ */\n\n\n /*\n PSYCHO ACOUSTICS\n\n\n This routine computes the psycho acoustics, delayed by one granule.\n\n Input: buffer of PCM data (1024 samples).\n\n This window should be centered over the 576 sample granule window.\n The routine will compute the psycho acoustics for\n this granule, but return the psycho acoustics computed\n for the *previous* granule. This is because the block\n type of the previous granule can only be determined\n after we have computed the psycho acoustics for the following\n granule.\n\n Output: maskings and energies for each scalefactor band.\n block type, PE, and some correlation measures.\n The PE is used by CBR modes to determine if extra bits\n from the bit reservoir should be used. The correlation\n measures are used to determine mid/side or regular stereo.\n */\n /*\n Notation:\n\n barks: a non-linear frequency scale. Mapping from frequency to\n barks is given by freq2bark()\n\n scalefactor bands: The spectrum (frequencies) are broken into\n SBMAX \"scalefactor bands\". Thes bands\n are determined by the MPEG ISO spec. In\n the noise shaping/quantization code, we allocate\n bits among the partition bands to achieve the\n best possible quality\n\n partition bands: The spectrum is also broken into about\n 64 \"partition bands\". Each partition\n band is about .34 barks wide. There are about 2-5\n partition bands for each scalefactor band.\n\n LAME computes all psycho acoustic information for each partition\n band. Then at the end of the computations, this information\n is mapped to scalefactor bands. The energy in each scalefactor\n band is taken as the sum of the energy in all partition bands\n which overlap the scalefactor band. The maskings can be computed\n in the same way (and thus represent the average masking in that band)\n or by taking the minmum value multiplied by the number of\n partition bands used (which represents a minimum masking in that band).\n */\n /*\n The general outline is as follows:\n\n 1. compute the energy in each partition band\n 2. compute the tonality in each partition band\n 3. compute the strength of each partion band \"masker\"\n 4. compute the masking (via the spreading function applied to each masker)\n 5. Modifications for mid/side masking.\n\n Each partition band is considiered a \"masker\". The strength\n of the i'th masker in band j is given by:\n\n s3(bark(i)-bark(j))*strength(i)\n\n The strength of the masker is a function of the energy and tonality.\n The more tonal, the less masking. LAME uses a simple linear formula\n (controlled by NMT and TMN) which says the strength is given by the\n energy divided by a linear function of the tonality.\n */\n /*\n s3() is the \"spreading function\". It is given by a formula\n determined via listening tests.\n\n The total masking in the j'th partition band is the sum over\n all maskings i. It is thus given by the convolution of\n the strength with s3(), the \"spreading function.\"\n\n masking(j) = sum_over_i s3(i-j)*strength(i) = s3 o strength\n\n where \"o\" = convolution operator. s3 is given by a formula determined\n via listening tests. It is normalized so that s3 o 1 = 1.\n\n Note: instead of a simple convolution, LAME also has the\n option of using \"additive masking\"\n\n The most critical part is step 2, computing the tonality of each\n partition band. LAME has two tonality estimators. The first\n is based on the ISO spec, and measures how predictiable the\n signal is over time. The more predictable, the more tonal.\n The second measure is based on looking at the spectrum of\n a single granule. The more peaky the spectrum, the more\n tonal. By most indications, the latter approach is better.\n\n Finally, in step 5, the maskings for the mid and side\n channel are possibly increased. Under certain circumstances,\n noise in the mid & side channels is assumed to also\n be masked by strong maskers in the L or R channels.\n\n\n Other data computed by the psy-model:\n\n ms_ratio side-channel / mid-channel masking ratio (for previous granule)\n ms_ratio_next side-channel / mid-channel masking ratio for this granule\n\n percep_entropy[2] L and R values (prev granule) of PE - A measure of how\n much pre-echo is in the previous granule\n percep_entropy_MS[2] mid and side channel values (prev granule) of percep_entropy\n energy[4] L,R,M,S energy in each channel, prev granule\n blocktype_d[2] block type to use for previous granule\n */\n//package mp3;\n\n//import java.util.Arrays;\n\n\n function PsyModel() {\n\n var fft = new FFT();\n\n var LOG10 = 2.30258509299404568402;\n\n var rpelev = 2;\n var rpelev2 = 16;\n var rpelev_s = 2;\n var rpelev2_s = 16;\n\n /* size of each partition band, in barks: */\n var DELBARK = .34;\n\n /* tuned for output level (sensitive to energy scale) */\n var VO_SCALE = (1. / (14752 * 14752) / (Encoder.BLKSIZE / 2));\n\n var temporalmask_sustain_sec = 0.01;\n\n var NS_PREECHO_ATT0 = 0.8;\n var NS_PREECHO_ATT1 = 0.6;\n var NS_PREECHO_ATT2 = 0.3;\n\n var NS_MSFIX = 3.5;\n\n var NSATTACKTHRE = 4.4;\n var NSATTACKTHRE_S = 25;\n\n var NSFIRLEN = 21;\n\n /* size of each partition band, in barks: */\n var LN_TO_LOG10 = 0.2302585093;\n\n function NON_LINEAR_SCALE_ENERGY(x) {\n return x;\n }\n\n /**\n * \n * L3psycho_anal. Compute psycho acoustics.\n *\n * Data returned to the calling program must be delayed by one\n * granule.\n *\n * This is done in two places.\n * If we do not need to know the blocktype, the copying\n * can be done here at the top of the program: we copy the data for\n * the last granule (computed during the last call) before it is\n * overwritten with the new data. It looks like this:\n *\n * 0. static psymodel_data\n * 1. calling_program_data = psymodel_data\n * 2. compute psymodel_data\n *\n * For data which needs to know the blocktype, the copying must be\n * done at the end of this loop, and the old values must be saved:\n *\n * 0. static psymodel_data_old\n * 1. compute psymodel_data\n * 2. compute possible block type of this granule\n * 3. compute final block type of previous granule based on #2.\n * 4. calling_program_data = psymodel_data_old\n * 5. psymodel_data_old = psymodel_data\n * psycho_loudness_approx\n * jd - 2001 mar 12\n * in: energy - BLKSIZE/2 elements of frequency magnitudes ^ 2\n * gfp - uses out_samplerate, ATHtype (also needed for ATHformula)\n * returns: loudness^2 approximation, a positive value roughly tuned for a value\n * of 1.0 for signals near clipping.\n * notes: When calibrated, feeding this function binary white noise at sample\n * values +32767 or -32768 should return values that approach 3.\n * ATHformula is used to approximate an equal loudness curve.\n * future: Data indicates that the shape of the equal loudness curve varies\n * with intensity. This function might be improved by using an equal\n * loudness curve shaped for typical playback levels (instead of the\n * ATH, that is shaped for the threshold). A flexible realization might\n * simply bend the existing ATH curve to achieve the desired shape.\n * However, the potential gain may not be enough to justify an effort.\n *
\n */\n function psycho_loudness_approx(energy, gfc) {\n var loudness_power = 0.0;\n /* apply weights to power in freq. bands */\n for (var i = 0; i < Encoder.BLKSIZE / 2; ++i)\n loudness_power += energy[i] * gfc.ATH.eql_w[i];\n loudness_power *= VO_SCALE;\n\n return loudness_power;\n }\n\n function compute_ffts(gfp, fftenergy, fftenergy_s, wsamp_l, wsamp_lPos, wsamp_s, wsamp_sPos, gr_out, chn, buffer, bufPos) {\n var gfc = gfp.internal_flags;\n if (chn < 2) {\n fft.fft_long(gfc, wsamp_l[wsamp_lPos], chn, buffer, bufPos);\n fft.fft_short(gfc, wsamp_s[wsamp_sPos], chn, buffer, bufPos);\n }\n /* FFT data for mid and side channel is derived from L & R */\n else if (chn == 2) {\n for (var j = Encoder.BLKSIZE - 1; j >= 0; --j) {\n var l = wsamp_l[wsamp_lPos + 0][j];\n var r = wsamp_l[wsamp_lPos + 1][j];\n wsamp_l[wsamp_lPos + 0][j] = (l + r) * Util.SQRT2 * 0.5;\n wsamp_l[wsamp_lPos + 1][j] = (l - r) * Util.SQRT2 * 0.5;\n }\n for (var b = 2; b >= 0; --b) {\n for (var j = Encoder.BLKSIZE_s - 1; j >= 0; --j) {\n var l = wsamp_s[wsamp_sPos + 0][b][j];\n var r = wsamp_s[wsamp_sPos + 1][b][j];\n wsamp_s[wsamp_sPos + 0][b][j] = (l + r) * Util.SQRT2 * 0.5;\n wsamp_s[wsamp_sPos + 1][b][j] = (l - r) * Util.SQRT2 * 0.5;\n }\n }\n }\n\n /*********************************************************************\n * compute energies\n *********************************************************************/\n fftenergy[0] = NON_LINEAR_SCALE_ENERGY(wsamp_l[wsamp_lPos + 0][0]);\n fftenergy[0] *= fftenergy[0];\n\n for (var j = Encoder.BLKSIZE / 2 - 1; j >= 0; --j) {\n var re = (wsamp_l[wsamp_lPos + 0])[Encoder.BLKSIZE / 2 - j];\n var im = (wsamp_l[wsamp_lPos + 0])[Encoder.BLKSIZE / 2 + j];\n fftenergy[Encoder.BLKSIZE / 2 - j] = NON_LINEAR_SCALE_ENERGY((re\n * re + im * im) * 0.5);\n }\n for (var b = 2; b >= 0; --b) {\n fftenergy_s[b][0] = (wsamp_s[wsamp_sPos + 0])[b][0];\n fftenergy_s[b][0] *= fftenergy_s[b][0];\n for (var j = Encoder.BLKSIZE_s / 2 - 1; j >= 0; --j) {\n var re = (wsamp_s[wsamp_sPos + 0])[b][Encoder.BLKSIZE_s\n / 2 - j];\n var im = (wsamp_s[wsamp_sPos + 0])[b][Encoder.BLKSIZE_s\n / 2 + j];\n fftenergy_s[b][Encoder.BLKSIZE_s / 2 - j] = NON_LINEAR_SCALE_ENERGY((re\n * re + im * im) * 0.5);\n }\n }\n /* total energy */\n {\n var totalenergy = 0.0;\n for (var j = 11; j < Encoder.HBLKSIZE; j++)\n totalenergy += fftenergy[j];\n\n gfc.tot_ener[chn] = totalenergy;\n }\n\n if (gfp.analysis) {\n for (var j = 0; j < Encoder.HBLKSIZE; j++) {\n gfc.pinfo.energy[gr_out][chn][j] = gfc.pinfo.energy_save[chn][j];\n gfc.pinfo.energy_save[chn][j] = fftenergy[j];\n }\n gfc.pinfo.pe[gr_out][chn] = gfc.pe[chn];\n }\n\n /*********************************************************************\n * compute loudness approximation (used for ATH auto-level adjustment)\n *********************************************************************/\n if (gfp.athaa_loudapprox == 2 && chn < 2) {\n // no loudness for mid/side ch\n gfc.loudness_sq[gr_out][chn] = gfc.loudness_sq_save[chn];\n gfc.loudness_sq_save[chn] = psycho_loudness_approx(fftenergy, gfc);\n }\n }\n\n /* mask_add optimization */\n /* init the limit values used to avoid computing log in mask_add when it is not necessary */\n\n /**\n * \n * For example, with i = 10*log10(m2/m1)/10*16 (= log10(m2/m1)*16)\n *\n * abs(i)>8 is equivalent (as i is an integer) to\n * abs(i)>=9\n * i>=9 || i<=-9\n * equivalent to (as i is the biggest integer smaller than log10(m2/m1)*16\n * or the smallest integer bigger than log10(m2/m1)*16 depending on the sign of log10(m2/m1)*16)\n * log10(m2/m1)>=9/16 || log10(m2/m1)<=-9/16\n * exp10 is strictly increasing thus this is equivalent to\n * m2/m1 >= 10^(9/16) || m2/m1<=10^(-9/16) which are comparisons to constants\n *
\n */\n\n /**\n * as in if(i>8)\n */\n var I1LIMIT = 8;\n /**\n * as in if(i>24) . changed 23\n */\n var I2LIMIT = 23;\n /**\n * as in if(m<15)\n */\n var MLIMIT = 15;\n\n var ma_max_i1;\n var ma_max_i2;\n var ma_max_m;\n\n /**\n * This is the masking table:
\n * According to tonality, values are going from 0dB (TMN) to 9.3dB (NMT).
\n * After additive masking computation, 8dB are added, so final values are\n * going from 8dB to 17.3dB\n *\n * pow(10, -0.0..-0.6)\n */\n var tab = [1.0, 0.79433, 0.63096, 0.63096,\n 0.63096, 0.63096, 0.63096, 0.25119, 0.11749];\n\n function init_mask_add_max_values() {\n ma_max_i1 = Math.pow(10, (I1LIMIT + 1) / 16.0);\n ma_max_i2 = Math.pow(10, (I2LIMIT + 1) / 16.0);\n ma_max_m = Math.pow(10, (MLIMIT) / 10.0);\n }\n\n var table1 = [3.3246 * 3.3246,\n 3.23837 * 3.23837, 3.15437 * 3.15437, 3.00412 * 3.00412,\n 2.86103 * 2.86103, 2.65407 * 2.65407, 2.46209 * 2.46209,\n 2.284 * 2.284, 2.11879 * 2.11879, 1.96552 * 1.96552,\n 1.82335 * 1.82335, 1.69146 * 1.69146, 1.56911 * 1.56911,\n 1.46658 * 1.46658, 1.37074 * 1.37074, 1.31036 * 1.31036,\n 1.25264 * 1.25264, 1.20648 * 1.20648, 1.16203 * 1.16203,\n 1.12765 * 1.12765, 1.09428 * 1.09428, 1.0659 * 1.0659,\n 1.03826 * 1.03826, 1.01895 * 1.01895, 1];\n\n var table2 = [1.33352 * 1.33352,\n 1.35879 * 1.35879, 1.38454 * 1.38454, 1.39497 * 1.39497,\n 1.40548 * 1.40548, 1.3537 * 1.3537, 1.30382 * 1.30382,\n 1.22321 * 1.22321, 1.14758 * 1.14758, 1];\n\n var table3 = [2.35364 * 2.35364,\n 2.29259 * 2.29259, 2.23313 * 2.23313, 2.12675 * 2.12675,\n 2.02545 * 2.02545, 1.87894 * 1.87894, 1.74303 * 1.74303,\n 1.61695 * 1.61695, 1.49999 * 1.49999, 1.39148 * 1.39148,\n 1.29083 * 1.29083, 1.19746 * 1.19746, 1.11084 * 1.11084,\n 1.03826 * 1.03826];\n\n /**\n * addition of simultaneous masking Naoki Shibata 2000/7\n */\n function mask_add(m1, m2, kk, b, gfc, shortblock) {\n var ratio;\n\n if (m2 > m1) {\n if (m2 < (m1 * ma_max_i2))\n ratio = m2 / m1;\n else\n return (m1 + m2);\n } else {\n if (m1 >= (m2 * ma_max_i2))\n return (m1 + m2);\n ratio = m1 / m2;\n }\n\n /* Should always be true, just checking */\n\n m1 += m2;\n //if (((long)(b + 3) & 0xffffffff) <= 3 + 3) {\n if ((b + 3) <= 3 + 3) {\n /* approximately, 1 bark = 3 partitions */\n /* 65% of the cases */\n /* originally 'if(i > 8)' */\n if (ratio >= ma_max_i1) {\n /* 43% of the total */\n return m1;\n }\n\n /* 22% of the total */\n var i = 0 | (Util.FAST_LOG10_X(ratio, 16.0));\n return m1 * table2[i];\n }\n\n /**\n * \n * m<15 equ log10((m1+m2)/gfc.ATH.cb[k])<1.5\n * equ (m1+m2)/gfc.ATH.cb[k]<10^1.5\n * equ (m1+m2)<10^1.5 * gfc.ATH.cb[k]\n *
\n */\n var i = 0 | Util.FAST_LOG10_X(ratio, 16.0);\n if (shortblock != 0) {\n m2 = gfc.ATH.cb_s[kk] * gfc.ATH.adjust;\n } else {\n m2 = gfc.ATH.cb_l[kk] * gfc.ATH.adjust;\n }\n if (m1 < ma_max_m * m2) {\n /* 3% of the total */\n /* Originally if (m > 0) { */\n if (m1 > m2) {\n var f, r;\n\n f = 1.0;\n if (i <= 13)\n f = table3[i];\n\n r = Util.FAST_LOG10_X(m1 / m2, 10.0 / 15.0);\n return m1 * ((table1[i] - f) * r + f);\n }\n\n if (i > 13)\n return m1;\n\n return m1 * table3[i];\n }\n\n /* 10% of total */\n return m1 * table1[i];\n }\n\n var table2_ = [1.33352 * 1.33352,\n 1.35879 * 1.35879, 1.38454 * 1.38454, 1.39497 * 1.39497,\n 1.40548 * 1.40548, 1.3537 * 1.3537, 1.30382 * 1.30382,\n 1.22321 * 1.22321, 1.14758 * 1.14758, 1];\n\n /**\n * addition of simultaneous masking Naoki Shibata 2000/7\n */\n function vbrpsy_mask_add(m1, m2, b) {\n var ratio;\n\n if (m1 < 0) {\n m1 = 0;\n }\n if (m2 < 0) {\n m2 = 0;\n }\n if (m1 <= 0) {\n return m2;\n }\n if (m2 <= 0) {\n return m1;\n }\n if (m2 > m1) {\n ratio = m2 / m1;\n } else {\n ratio = m1 / m2;\n }\n if (-2 <= b && b <= 2) {\n /* approximately, 1 bark = 3 partitions */\n /* originally 'if(i > 8)' */\n if (ratio >= ma_max_i1) {\n return m1 + m2;\n } else {\n var i = 0 | (Util.FAST_LOG10_X(ratio, 16.0));\n return (m1 + m2) * table2_[i];\n }\n }\n if (ratio < ma_max_i2) {\n return m1 + m2;\n }\n if (m1 < m2) {\n m1 = m2;\n }\n return m1;\n }\n\n /**\n * compute interchannel masking effects\n */\n function calc_interchannel_masking(gfp, ratio) {\n var gfc = gfp.internal_flags;\n if (gfc.channels_out > 1) {\n for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {\n var l = gfc.thm[0].l[sb];\n var r = gfc.thm[1].l[sb];\n gfc.thm[0].l[sb] += r * ratio;\n gfc.thm[1].l[sb] += l * ratio;\n }\n for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {\n for (var sblock = 0; sblock < 3; sblock++) {\n var l = gfc.thm[0].s[sb][sblock];\n var r = gfc.thm[1].s[sb][sblock];\n gfc.thm[0].s[sb][sblock] += r * ratio;\n gfc.thm[1].s[sb][sblock] += l * ratio;\n }\n }\n }\n }\n\n /**\n * compute M/S thresholds from Johnston & Ferreira 1992 ICASSP paper\n */\n function msfix1(gfc) {\n for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {\n /* use this fix if L & R masking differs by 2db or less */\n /* if db = 10*log10(x2/x1) < 2 */\n /* if (x2 < 1.58*x1) { */\n if (gfc.thm[0].l[sb] > 1.58 * gfc.thm[1].l[sb]\n || gfc.thm[1].l[sb] > 1.58 * gfc.thm[0].l[sb])\n continue;\n var mld = gfc.mld_l[sb] * gfc.en[3].l[sb];\n var rmid = Math.max(gfc.thm[2].l[sb],\n Math.min(gfc.thm[3].l[sb], mld));\n\n mld = gfc.mld_l[sb] * gfc.en[2].l[sb];\n var rside = Math.max(gfc.thm[3].l[sb],\n Math.min(gfc.thm[2].l[sb], mld));\n gfc.thm[2].l[sb] = rmid;\n gfc.thm[3].l[sb] = rside;\n }\n\n for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {\n for (var sblock = 0; sblock < 3; sblock++) {\n if (gfc.thm[0].s[sb][sblock] > 1.58 * gfc.thm[1].s[sb][sblock]\n || gfc.thm[1].s[sb][sblock] > 1.58 * gfc.thm[0].s[sb][sblock])\n continue;\n var mld = gfc.mld_s[sb] * gfc.en[3].s[sb][sblock];\n var rmid = Math.max(gfc.thm[2].s[sb][sblock],\n Math.min(gfc.thm[3].s[sb][sblock], mld));\n\n mld = gfc.mld_s[sb] * gfc.en[2].s[sb][sblock];\n var rside = Math.max(gfc.thm[3].s[sb][sblock],\n Math.min(gfc.thm[2].s[sb][sblock], mld));\n\n gfc.thm[2].s[sb][sblock] = rmid;\n gfc.thm[3].s[sb][sblock] = rside;\n }\n }\n }\n\n /**\n * Adjust M/S maskings if user set \"msfix\"\n *\n * Naoki Shibata 2000\n */\n function ns_msfix(gfc, msfix, athadjust) {\n var msfix2 = msfix;\n var athlower = Math.pow(10, athadjust);\n\n msfix *= 2.0;\n msfix2 *= 2.0;\n for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {\n var thmLR, thmM, thmS, ath;\n ath = (gfc.ATH.cb_l[gfc.bm_l[sb]]) * athlower;\n thmLR = Math.min(Math.max(gfc.thm[0].l[sb], ath),\n Math.max(gfc.thm[1].l[sb], ath));\n thmM = Math.max(gfc.thm[2].l[sb], ath);\n thmS = Math.max(gfc.thm[3].l[sb], ath);\n if (thmLR * msfix < thmM + thmS) {\n var f = thmLR * msfix2 / (thmM + thmS);\n thmM *= f;\n thmS *= f;\n }\n gfc.thm[2].l[sb] = Math.min(thmM, gfc.thm[2].l[sb]);\n gfc.thm[3].l[sb] = Math.min(thmS, gfc.thm[3].l[sb]);\n }\n\n athlower *= ( Encoder.BLKSIZE_s / Encoder.BLKSIZE);\n for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {\n for (var sblock = 0; sblock < 3; sblock++) {\n var thmLR, thmM, thmS, ath;\n ath = (gfc.ATH.cb_s[gfc.bm_s[sb]]) * athlower;\n thmLR = Math.min(Math.max(gfc.thm[0].s[sb][sblock], ath),\n Math.max(gfc.thm[1].s[sb][sblock], ath));\n thmM = Math.max(gfc.thm[2].s[sb][sblock], ath);\n thmS = Math.max(gfc.thm[3].s[sb][sblock], ath);\n\n if (thmLR * msfix < thmM + thmS) {\n var f = thmLR * msfix / (thmM + thmS);\n thmM *= f;\n thmS *= f;\n }\n gfc.thm[2].s[sb][sblock] = Math.min(gfc.thm[2].s[sb][sblock],\n thmM);\n gfc.thm[3].s[sb][sblock] = Math.min(gfc.thm[3].s[sb][sblock],\n thmS);\n }\n }\n }\n\n /**\n * short block threshold calculation (part 2)\n *\n * partition band bo_s[sfb] is at the transition from scalefactor band sfb\n * to the next one sfb+1; enn and thmm have to be split between them\n */\n function convert_partition2scalefac_s(gfc, eb, thr, chn, sblock) {\n var sb, b;\n var enn = 0.0;\n var thmm = 0.0;\n for (sb = b = 0; sb < Encoder.SBMAX_s; ++b, ++sb) {\n var bo_s_sb = gfc.bo_s[sb];\n var npart_s = gfc.npart_s;\n var b_lim = bo_s_sb < npart_s ? bo_s_sb : npart_s;\n while (b < b_lim) {\n // iff failed, it may indicate some index error elsewhere\n enn += eb[b];\n thmm += thr[b];\n b++;\n }\n gfc.en[chn].s[sb][sblock] = enn;\n gfc.thm[chn].s[sb][sblock] = thmm;\n\n if (b >= npart_s) {\n ++sb;\n break;\n }\n // iff failed, it may indicate some index error elsewhere\n {\n /* at transition sfb . sfb+1 */\n var w_curr = gfc.PSY.bo_s_weight[sb];\n var w_next = 1.0 - w_curr;\n enn = w_curr * eb[b];\n thmm = w_curr * thr[b];\n gfc.en[chn].s[sb][sblock] += enn;\n gfc.thm[chn].s[sb][sblock] += thmm;\n enn = w_next * eb[b];\n thmm = w_next * thr[b];\n }\n }\n /* zero initialize the rest */\n for (; sb < Encoder.SBMAX_s; ++sb) {\n gfc.en[chn].s[sb][sblock] = 0;\n gfc.thm[chn].s[sb][sblock] = 0;\n }\n }\n\n /**\n * longblock threshold calculation (part 2)\n */\n function convert_partition2scalefac_l(gfc, eb, thr, chn) {\n var sb, b;\n var enn = 0.0;\n var thmm = 0.0;\n for (sb = b = 0; sb < Encoder.SBMAX_l; ++b, ++sb) {\n var bo_l_sb = gfc.bo_l[sb];\n var npart_l = gfc.npart_l;\n var b_lim = bo_l_sb < npart_l ? bo_l_sb : npart_l;\n while (b < b_lim) {\n // iff failed, it may indicate some index error elsewhere\n enn += eb[b];\n thmm += thr[b];\n b++;\n }\n gfc.en[chn].l[sb] = enn;\n gfc.thm[chn].l[sb] = thmm;\n\n if (b >= npart_l) {\n ++sb;\n break;\n }\n {\n /* at transition sfb . sfb+1 */\n var w_curr = gfc.PSY.bo_l_weight[sb];\n var w_next = 1.0 - w_curr;\n enn = w_curr * eb[b];\n thmm = w_curr * thr[b];\n gfc.en[chn].l[sb] += enn;\n gfc.thm[chn].l[sb] += thmm;\n enn = w_next * eb[b];\n thmm = w_next * thr[b];\n }\n }\n /* zero initialize the rest */\n for (; sb < Encoder.SBMAX_l; ++sb) {\n gfc.en[chn].l[sb] = 0;\n gfc.thm[chn].l[sb] = 0;\n }\n }\n\n function compute_masking_s(gfp, fftenergy_s, eb, thr, chn, sblock) {\n var gfc = gfp.internal_flags;\n var j, b;\n\n for (b = j = 0; b < gfc.npart_s; ++b) {\n var ebb = 0, m = 0;\n var n = gfc.numlines_s[b];\n for (var i = 0; i < n; ++i, ++j) {\n var el = fftenergy_s[sblock][j];\n ebb += el;\n if (m < el)\n m = el;\n }\n eb[b] = ebb;\n }\n for (j = b = 0; b < gfc.npart_s; b++) {\n var kk = gfc.s3ind_s[b][0];\n var ecb = gfc.s3_ss[j++] * eb[kk];\n ++kk;\n while (kk <= gfc.s3ind_s[b][1]) {\n ecb += gfc.s3_ss[j] * eb[kk];\n ++j;\n ++kk;\n }\n\n { /* limit calculated threshold by previous granule */\n var x = rpelev_s * gfc.nb_s1[chn][b];\n thr[b] = Math.min(ecb, x);\n }\n if (gfc.blocktype_old[chn & 1] == Encoder.SHORT_TYPE) {\n /* limit calculated threshold by even older granule */\n var x = rpelev2_s * gfc.nb_s2[chn][b];\n var y = thr[b];\n thr[b] = Math.min(x, y);\n }\n\n gfc.nb_s2[chn][b] = gfc.nb_s1[chn][b];\n gfc.nb_s1[chn][b] = ecb;\n }\n for (; b <= Encoder.CBANDS; ++b) {\n eb[b] = 0;\n thr[b] = 0;\n }\n }\n\n function block_type_set(gfp, uselongblock, blocktype_d, blocktype) {\n var gfc = gfp.internal_flags;\n\n if (gfp.short_blocks == ShortBlock.short_block_coupled\n /* force both channels to use the same block type */\n /* this is necessary if the frame is to be encoded in ms_stereo. */\n /* But even without ms_stereo, FhG does this */\n && !(uselongblock[0] != 0 && uselongblock[1] != 0))\n uselongblock[0] = uselongblock[1] = 0;\n\n /*\n * update the blocktype of the previous granule, since it depends on\n * what happend in this granule\n */\n for (var chn = 0; chn < gfc.channels_out; chn++) {\n blocktype[chn] = Encoder.NORM_TYPE;\n /* disable short blocks */\n if (gfp.short_blocks == ShortBlock.short_block_dispensed)\n uselongblock[chn] = 1;\n if (gfp.short_blocks == ShortBlock.short_block_forced)\n uselongblock[chn] = 0;\n\n if (uselongblock[chn] != 0) {\n /* no attack : use long blocks */\n if (gfc.blocktype_old[chn] == Encoder.SHORT_TYPE)\n blocktype[chn] = Encoder.STOP_TYPE;\n } else {\n /* attack : use short blocks */\n blocktype[chn] = Encoder.SHORT_TYPE;\n if (gfc.blocktype_old[chn] == Encoder.NORM_TYPE) {\n gfc.blocktype_old[chn] = Encoder.START_TYPE;\n }\n if (gfc.blocktype_old[chn] == Encoder.STOP_TYPE)\n gfc.blocktype_old[chn] = Encoder.SHORT_TYPE;\n }\n\n blocktype_d[chn] = gfc.blocktype_old[chn];\n // value returned to calling program\n gfc.blocktype_old[chn] = blocktype[chn];\n // save for next call to l3psy_anal\n }\n }\n\n function NS_INTERP(x, y, r) {\n /* was pow((x),(r))*pow((y),1-(r)) */\n if (r >= 1.0) {\n /* 99.7% of the time */\n return x;\n }\n if (r <= 0.0)\n return y;\n if (y > 0.0) {\n /* rest of the time */\n return (Math.pow(x / y, r) * y);\n }\n /* never happens */\n return 0.0;\n }\n\n /**\n * these values are tuned only for 44.1kHz...\n */\n var regcoef_s = [11.8, 13.6, 17.2, 32, 46.5,\n 51.3, 57.5, 67.1, 71.5, 84.6, 97.6, 130,\n /* 255.8 */\n ];\n\n function pecalc_s(mr, masking_lower) {\n var pe_s = 1236.28 / 4;\n for (var sb = 0; sb < Encoder.SBMAX_s - 1; sb++) {\n for (var sblock = 0; sblock < 3; sblock++) {\n var thm = mr.thm.s[sb][sblock];\n if (thm > 0.0) {\n var x = thm * masking_lower;\n var en = mr.en.s[sb][sblock];\n if (en > x) {\n if (en > x * 1e10) {\n pe_s += regcoef_s[sb] * (10.0 * LOG10);\n } else {\n pe_s += regcoef_s[sb] * Util.FAST_LOG10(en / x);\n }\n }\n }\n }\n }\n\n return pe_s;\n }\n\n /**\n * these values are tuned only for 44.1kHz...\n */\n var regcoef_l = [6.8, 5.8, 5.8, 6.4, 6.5, 9.9,\n 12.1, 14.4, 15, 18.9, 21.6, 26.9, 34.2, 40.2, 46.8, 56.5,\n 60.7, 73.9, 85.7, 93.4, 126.1,\n /* 241.3 */\n ];\n\n function pecalc_l(mr, masking_lower) {\n var pe_l = 1124.23 / 4;\n for (var sb = 0; sb < Encoder.SBMAX_l - 1; sb++) {\n var thm = mr.thm.l[sb];\n if (thm > 0.0) {\n var x = thm * masking_lower;\n var en = mr.en.l[sb];\n if (en > x) {\n if (en > x * 1e10) {\n pe_l += regcoef_l[sb] * (10.0 * LOG10);\n } else {\n pe_l += regcoef_l[sb] * Util.FAST_LOG10(en / x);\n }\n }\n }\n }\n return pe_l;\n }\n\n function calc_energy(gfc, fftenergy, eb, max, avg) {\n var b, j;\n\n for (b = j = 0; b < gfc.npart_l; ++b) {\n var ebb = 0, m = 0;\n var i;\n for (i = 0; i < gfc.numlines_l[b]; ++i, ++j) {\n var el = fftenergy[j];\n ebb += el;\n if (m < el)\n m = el;\n }\n eb[b] = ebb;\n max[b] = m;\n avg[b] = ebb * gfc.rnumlines_l[b];\n }\n }\n\n function calc_mask_index_l(gfc, max, avg, mask_idx) {\n var last_tab_entry = tab.length - 1;\n var b = 0;\n var a = avg[b] + avg[b + 1];\n if (a > 0.0) {\n var m = max[b];\n if (m < max[b + 1])\n m = max[b + 1];\n a = 20.0 * (m * 2.0 - a)\n / (a * (gfc.numlines_l[b] + gfc.numlines_l[b + 1] - 1));\n var k = 0 | a;\n if (k > last_tab_entry)\n k = last_tab_entry;\n mask_idx[b] = k;\n } else {\n mask_idx[b] = 0;\n }\n\n for (b = 1; b < gfc.npart_l - 1; b++) {\n a = avg[b - 1] + avg[b] + avg[b + 1];\n if (a > 0.0) {\n var m = max[b - 1];\n if (m < max[b])\n m = max[b];\n if (m < max[b + 1])\n m = max[b + 1];\n a = 20.0\n * (m * 3.0 - a)\n / (a * (gfc.numlines_l[b - 1] + gfc.numlines_l[b]\n + gfc.numlines_l[b + 1] - 1));\n var k = 0 | a;\n if (k > last_tab_entry)\n k = last_tab_entry;\n mask_idx[b] = k;\n } else {\n mask_idx[b] = 0;\n }\n }\n\n a = avg[b - 1] + avg[b];\n if (a > 0.0) {\n var m = max[b - 1];\n if (m < max[b])\n m = max[b];\n a = 20.0 * (m * 2.0 - a)\n / (a * (gfc.numlines_l[b - 1] + gfc.numlines_l[b] - 1));\n var k = 0 | a;\n if (k > last_tab_entry)\n k = last_tab_entry;\n mask_idx[b] = k;\n } else {\n mask_idx[b] = 0;\n }\n }\n\n var fircoef = [\n -8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,\n -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,\n -5.52212e-17 * 2, -0.313819 * 2\n ];\n\n this.L3psycho_anal_ns = function (gfp, buffer, bufPos, gr_out, masking_ratio, masking_MS_ratio, percep_entropy, percep_MS_entropy, energy, blocktype_d) {\n /*\n * to get a good cache performance, one has to think about the sequence,\n * in which the variables are used.\n */\n var gfc = gfp.internal_flags;\n\n /* fft and energy calculation */\n var wsamp_L = new_float_n([2, Encoder.BLKSIZE]);\n var wsamp_S = new_float_n([2, 3, Encoder.BLKSIZE_s]);\n\n /* convolution */\n var eb_l = new_float(Encoder.CBANDS + 1);\n var eb_s = new_float(Encoder.CBANDS + 1);\n var thr = new_float(Encoder.CBANDS + 2);\n\n /* block type */\n var blocktype = new_int(2), uselongblock = new_int(2);\n\n /* usual variables like loop indices, etc.. */\n var numchn, chn;\n var b, i, j, k;\n var sb, sblock;\n\n /* variables used for --nspsytune */\n var ns_hpfsmpl = new_float_n([2, 576]);\n var pcfact;\n var mask_idx_l = new_int(Encoder.CBANDS + 2), mask_idx_s = new_int(Encoder.CBANDS + 2);\n\n Arrays.fill(mask_idx_s, 0);\n\n numchn = gfc.channels_out;\n /* chn=2 and 3 = Mid and Side channels */\n if (gfp.mode == MPEGMode.JOINT_STEREO)\n numchn = 4;\n\n if (gfp.VBR == VbrMode.vbr_off)\n pcfact = gfc.ResvMax == 0 ? 0 : ( gfc.ResvSize)\n / gfc.ResvMax * 0.5;\n else if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh\n || gfp.VBR == VbrMode.vbr_mt) {\n pcfact = 0.6;\n } else\n pcfact = 1.0;\n\n /**********************************************************************\n * Apply HPF of fs/4 to the input signal. This is used for attack\n * detection / handling.\n **********************************************************************/\n /* Don't copy the input buffer into a temporary buffer */\n /* unroll the loop 2 times */\n for (chn = 0; chn < gfc.channels_out; chn++) {\n /* apply high pass filter of fs/4 */\n var firbuf = buffer[chn];\n var firbufPos = bufPos + 576 - 350 - NSFIRLEN + 192;\n for (i = 0; i < 576; i++) {\n var sum1, sum2;\n sum1 = firbuf[firbufPos + i + 10];\n sum2 = 0.0;\n for (j = 0; j < ((NSFIRLEN - 1) / 2) - 1; j += 2) {\n sum1 += fircoef[j]\n * (firbuf[firbufPos + i + j] + firbuf[firbufPos + i\n + NSFIRLEN - j]);\n sum2 += fircoef[j + 1]\n * (firbuf[firbufPos + i + j + 1] + firbuf[firbufPos\n + i + NSFIRLEN - j - 1]);\n }\n ns_hpfsmpl[chn][i] = sum1 + sum2;\n }\n masking_ratio[gr_out][chn].en.assign(gfc.en[chn]);\n masking_ratio[gr_out][chn].thm.assign(gfc.thm[chn]);\n if (numchn > 2) {\n /* MS maskings */\n /* percep_MS_entropy [chn-2] = gfc . pe [chn]; */\n masking_MS_ratio[gr_out][chn].en.assign(gfc.en[chn + 2]);\n masking_MS_ratio[gr_out][chn].thm.assign(gfc.thm[chn + 2]);\n }\n }\n\n for (chn = 0; chn < numchn; chn++) {\n var wsamp_l;\n var wsamp_s;\n var en_subshort = new_float(12);\n var en_short = [0, 0, 0, 0];\n var attack_intensity = new_float(12);\n var ns_uselongblock = 1;\n var attackThreshold;\n var max = new_float(Encoder.CBANDS), avg = new_float(Encoder.CBANDS);\n var ns_attacks = [0, 0, 0, 0];\n var fftenergy = new_float(Encoder.HBLKSIZE);\n var fftenergy_s = new_float_n([3, Encoder.HBLKSIZE_s]);\n\n /*\n * rh 20040301: the following loops do access one off the limits so\n * I increase the array dimensions by one and initialize the\n * accessed values to zero\n */\n\n /***************************************************************\n * determine the block type (window type)\n ***************************************************************/\n /* calculate energies of each sub-shortblocks */\n for (i = 0; i < 3; i++) {\n en_subshort[i] = gfc.nsPsy.last_en_subshort[chn][i + 6];\n attack_intensity[i] = en_subshort[i]\n / gfc.nsPsy.last_en_subshort[chn][i + 4];\n en_short[0] += en_subshort[i];\n }\n\n if (chn == 2) {\n for (i = 0; i < 576; i++) {\n var l, r;\n l = ns_hpfsmpl[0][i];\n r = ns_hpfsmpl[1][i];\n ns_hpfsmpl[0][i] = l + r;\n ns_hpfsmpl[1][i] = l - r;\n }\n }\n {\n var pf = ns_hpfsmpl[chn & 1];\n var pfPos = 0;\n for (i = 0; i < 9; i++) {\n var pfe = pfPos + 576 / 9;\n var p = 1.;\n for (; pfPos < pfe; pfPos++)\n if (p < Math.abs(pf[pfPos]))\n p = Math.abs(pf[pfPos]);\n\n gfc.nsPsy.last_en_subshort[chn][i] = en_subshort[i + 3] = p;\n en_short[1 + i / 3] += p;\n if (p > en_subshort[i + 3 - 2]) {\n p = p / en_subshort[i + 3 - 2];\n } else if (en_subshort[i + 3 - 2] > p * 10.0) {\n p = en_subshort[i + 3 - 2] / (p * 10.0);\n } else\n p = 0.0;\n attack_intensity[i + 3] = p;\n }\n }\n\n if (gfp.analysis) {\n var x = attack_intensity[0];\n for (i = 1; i < 12; i++)\n if (x < attack_intensity[i])\n x = attack_intensity[i];\n gfc.pinfo.ers[gr_out][chn] = gfc.pinfo.ers_save[chn];\n gfc.pinfo.ers_save[chn] = x;\n }\n\n /* compare energies between sub-shortblocks */\n attackThreshold = (chn == 3) ? gfc.nsPsy.attackthre_s\n : gfc.nsPsy.attackthre;\n for (i = 0; i < 12; i++)\n if (0 == ns_attacks[i / 3]\n && attack_intensity[i] > attackThreshold)\n ns_attacks[i / 3] = (i % 3) + 1;\n\n /*\n * should have energy change between short blocks, in order to avoid\n * periodic signals\n */\n for (i = 1; i < 4; i++) {\n var ratio;\n if (en_short[i - 1] > en_short[i]) {\n ratio = en_short[i - 1] / en_short[i];\n } else {\n ratio = en_short[i] / en_short[i - 1];\n }\n if (ratio < 1.7) {\n ns_attacks[i] = 0;\n if (i == 1)\n ns_attacks[0] = 0;\n }\n }\n\n if (ns_attacks[0] != 0 && gfc.nsPsy.lastAttacks[chn] != 0)\n ns_attacks[0] = 0;\n\n if (gfc.nsPsy.lastAttacks[chn] == 3\n || (ns_attacks[0] + ns_attacks[1] + ns_attacks[2] + ns_attacks[3]) != 0) {\n ns_uselongblock = 0;\n\n if (ns_attacks[1] != 0 && ns_attacks[0] != 0)\n ns_attacks[1] = 0;\n if (ns_attacks[2] != 0 && ns_attacks[1] != 0)\n ns_attacks[2] = 0;\n if (ns_attacks[3] != 0 && ns_attacks[2] != 0)\n ns_attacks[3] = 0;\n }\n\n if (chn < 2) {\n uselongblock[chn] = ns_uselongblock;\n } else {\n if (ns_uselongblock == 0) {\n uselongblock[0] = uselongblock[1] = 0;\n }\n }\n\n /*\n * there is a one granule delay. Copy maskings computed last call\n * into masking_ratio to return to calling program.\n */\n energy[chn] = gfc.tot_ener[chn];\n\n /*********************************************************************\n * compute FFTs\n *********************************************************************/\n wsamp_s = wsamp_S;\n wsamp_l = wsamp_L;\n compute_ffts(gfp, fftenergy, fftenergy_s, wsamp_l, (chn & 1),\n wsamp_s, (chn & 1), gr_out, chn, buffer, bufPos);\n\n /*********************************************************************\n * Calculate the energy and the tonality of each partition.\n *********************************************************************/\n calc_energy(gfc, fftenergy, eb_l, max, avg);\n calc_mask_index_l(gfc, max, avg, mask_idx_l);\n /* compute masking thresholds for short blocks */\n for (sblock = 0; sblock < 3; sblock++) {\n var enn, thmm;\n compute_masking_s(gfp, fftenergy_s, eb_s, thr, chn, sblock);\n convert_partition2scalefac_s(gfc, eb_s, thr, chn, sblock);\n /**** short block pre-echo control ****/\n for (sb = 0; sb < Encoder.SBMAX_s; sb++) {\n thmm = gfc.thm[chn].s[sb][sblock];\n\n thmm *= NS_PREECHO_ATT0;\n if (ns_attacks[sblock] >= 2 || ns_attacks[sblock + 1] == 1) {\n var idx = (sblock != 0) ? sblock - 1 : 2;\n var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,\n NS_PREECHO_ATT1 * pcfact);\n thmm = Math.min(thmm, p);\n }\n\n if (ns_attacks[sblock] == 1) {\n var idx = (sblock != 0) ? sblock - 1 : 2;\n var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,\n NS_PREECHO_ATT2 * pcfact);\n thmm = Math.min(thmm, p);\n } else if ((sblock != 0 && ns_attacks[sblock - 1] == 3)\n || (sblock == 0 && gfc.nsPsy.lastAttacks[chn] == 3)) {\n var idx = (sblock != 2) ? sblock + 1 : 0;\n var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,\n NS_PREECHO_ATT2 * pcfact);\n thmm = Math.min(thmm, p);\n }\n\n /* pulse like signal detection for fatboy.wav and so on */\n enn = en_subshort[sblock * 3 + 3]\n + en_subshort[sblock * 3 + 4]\n + en_subshort[sblock * 3 + 5];\n if (en_subshort[sblock * 3 + 5] * 6 < enn) {\n thmm *= 0.5;\n if (en_subshort[sblock * 3 + 4] * 6 < enn)\n thmm *= 0.5;\n }\n\n gfc.thm[chn].s[sb][sblock] = thmm;\n }\n }\n gfc.nsPsy.lastAttacks[chn] = ns_attacks[2];\n\n /*********************************************************************\n * convolve the partitioned energy and unpredictability with the\n * spreading function, s3_l[b][k]\n ********************************************************************/\n k = 0;\n {\n for (b = 0; b < gfc.npart_l; b++) {\n /*\n * convolve the partitioned energy with the spreading\n * function\n */\n var kk = gfc.s3ind[b][0];\n var eb2 = eb_l[kk] * tab[mask_idx_l[kk]];\n var ecb = gfc.s3_ll[k++] * eb2;\n while (++kk <= gfc.s3ind[b][1]) {\n eb2 = eb_l[kk] * tab[mask_idx_l[kk]];\n ecb = mask_add(ecb, gfc.s3_ll[k++] * eb2, kk, kk - b,\n gfc, 0);\n }\n ecb *= 0.158489319246111;\n /* pow(10,-0.8) */\n\n /**** long block pre-echo control ****/\n /**\n * \n * dont use long block pre-echo control if previous granule was\n * a short block. This is to avoid the situation:\n * frame0: quiet (very low masking)\n * frame1: surge (triggers short blocks)\n * frame2: regular frame. looks like pre-echo when compared to\n * frame0, but all pre-echo was in frame1.\n *
\n */\n /*\n * chn=0,1 L and R channels\n *\n * chn=2,3 S and M channels.\n */\n\n if (gfc.blocktype_old[chn & 1] == Encoder.SHORT_TYPE)\n thr[b] = ecb;\n else\n thr[b] = NS_INTERP(\n Math.min(ecb, Math.min(rpelev\n * gfc.nb_1[chn][b], rpelev2\n * gfc.nb_2[chn][b])), ecb, pcfact);\n\n gfc.nb_2[chn][b] = gfc.nb_1[chn][b];\n gfc.nb_1[chn][b] = ecb;\n }\n }\n for (; b <= Encoder.CBANDS; ++b) {\n eb_l[b] = 0;\n thr[b] = 0;\n }\n /* compute masking thresholds for long blocks */\n convert_partition2scalefac_l(gfc, eb_l, thr, chn);\n }\n /* end loop over chn */\n\n if (gfp.mode == MPEGMode.STEREO || gfp.mode == MPEGMode.JOINT_STEREO) {\n if (gfp.interChRatio > 0.0) {\n calc_interchannel_masking(gfp, gfp.interChRatio);\n }\n }\n\n if (gfp.mode == MPEGMode.JOINT_STEREO) {\n var msfix;\n msfix1(gfc);\n msfix = gfp.msfix;\n if (Math.abs(msfix) > 0.0)\n ns_msfix(gfc, msfix, gfp.ATHlower * gfc.ATH.adjust);\n }\n\n /***************************************************************\n * determine final block type\n ***************************************************************/\n block_type_set(gfp, uselongblock, blocktype_d, blocktype);\n\n /*********************************************************************\n * compute the value of PE to return ... no delay and advance\n *********************************************************************/\n for (chn = 0; chn < numchn; chn++) {\n var ppe;\n var ppePos = 0;\n var type;\n var mr;\n\n if (chn > 1) {\n ppe = percep_MS_entropy;\n ppePos = -2;\n type = Encoder.NORM_TYPE;\n if (blocktype_d[0] == Encoder.SHORT_TYPE\n || blocktype_d[1] == Encoder.SHORT_TYPE)\n type = Encoder.SHORT_TYPE;\n mr = masking_MS_ratio[gr_out][chn - 2];\n } else {\n ppe = percep_entropy;\n ppePos = 0;\n type = blocktype_d[chn];\n mr = masking_ratio[gr_out][chn];\n }\n\n if (type == Encoder.SHORT_TYPE)\n ppe[ppePos + chn] = pecalc_s(mr, gfc.masking_lower);\n else\n ppe[ppePos + chn] = pecalc_l(mr, gfc.masking_lower);\n\n if (gfp.analysis)\n gfc.pinfo.pe[gr_out][chn] = ppe[ppePos + chn];\n\n }\n return 0;\n }\n\n function vbrpsy_compute_fft_l(gfp, buffer, bufPos, chn, gr_out, fftenergy, wsamp_l, wsamp_lPos) {\n var gfc = gfp.internal_flags;\n if (chn < 2) {\n fft.fft_long(gfc, wsamp_l[wsamp_lPos], chn, buffer, bufPos);\n } else if (chn == 2) {\n /* FFT data for mid and side channel is derived from L & R */\n for (var j = Encoder.BLKSIZE - 1; j >= 0; --j) {\n var l = wsamp_l[wsamp_lPos + 0][j];\n var r = wsamp_l[wsamp_lPos + 1][j];\n wsamp_l[wsamp_lPos + 0][j] = (l + r) * Util.SQRT2 * 0.5;\n wsamp_l[wsamp_lPos + 1][j] = (l - r) * Util.SQRT2 * 0.5;\n }\n }\n\n /*********************************************************************\n * compute energies\n *********************************************************************/\n fftenergy[0] = NON_LINEAR_SCALE_ENERGY(wsamp_l[wsamp_lPos + 0][0]);\n fftenergy[0] *= fftenergy[0];\n\n for (var j = Encoder.BLKSIZE / 2 - 1; j >= 0; --j) {\n var re = wsamp_l[wsamp_lPos + 0][Encoder.BLKSIZE / 2 - j];\n var im = wsamp_l[wsamp_lPos + 0][Encoder.BLKSIZE / 2 + j];\n fftenergy[Encoder.BLKSIZE / 2 - j] = NON_LINEAR_SCALE_ENERGY((re\n * re + im * im) * 0.5);\n }\n /* total energy */\n {\n var totalenergy = 0.0;\n for (var j = 11; j < Encoder.HBLKSIZE; j++)\n totalenergy += fftenergy[j];\n\n gfc.tot_ener[chn] = totalenergy;\n }\n\n if (gfp.analysis) {\n for (var j = 0; j < Encoder.HBLKSIZE; j++) {\n gfc.pinfo.energy[gr_out][chn][j] = gfc.pinfo.energy_save[chn][j];\n gfc.pinfo.energy_save[chn][j] = fftenergy[j];\n }\n gfc.pinfo.pe[gr_out][chn] = gfc.pe[chn];\n }\n }\n\n function vbrpsy_compute_fft_s(gfp, buffer, bufPos, chn, sblock, fftenergy_s, wsamp_s, wsamp_sPos) {\n var gfc = gfp.internal_flags;\n\n if (sblock == 0 && chn < 2) {\n fft.fft_short(gfc, wsamp_s[wsamp_sPos], chn, buffer, bufPos);\n }\n if (chn == 2) {\n /* FFT data for mid and side channel is derived from L & R */\n for (var j = Encoder.BLKSIZE_s - 1; j >= 0; --j) {\n var l = wsamp_s[wsamp_sPos + 0][sblock][j];\n var r = wsamp_s[wsamp_sPos + 1][sblock][j];\n wsamp_s[wsamp_sPos + 0][sblock][j] = (l + r) * Util.SQRT2 * 0.5;\n wsamp_s[wsamp_sPos + 1][sblock][j] = (l - r) * Util.SQRT2 * 0.5;\n }\n }\n\n /*********************************************************************\n * compute energies\n *********************************************************************/\n fftenergy_s[sblock][0] = wsamp_s[wsamp_sPos + 0][sblock][0];\n fftenergy_s[sblock][0] *= fftenergy_s[sblock][0];\n for (var j = Encoder.BLKSIZE_s / 2 - 1; j >= 0; --j) {\n var re = wsamp_s[wsamp_sPos + 0][sblock][Encoder.BLKSIZE_s / 2 - j];\n var im = wsamp_s[wsamp_sPos + 0][sblock][Encoder.BLKSIZE_s / 2 + j];\n fftenergy_s[sblock][Encoder.BLKSIZE_s / 2 - j] = NON_LINEAR_SCALE_ENERGY((re\n * re + im * im) * 0.5);\n }\n }\n\n /**\n * compute loudness approximation (used for ATH auto-level adjustment)\n */\n function vbrpsy_compute_loudness_approximation_l(gfp, gr_out, chn, fftenergy) {\n var gfc = gfp.internal_flags;\n if (gfp.athaa_loudapprox == 2 && chn < 2) {\n // no loudness for mid/side ch\n gfc.loudness_sq[gr_out][chn] = gfc.loudness_sq_save[chn];\n gfc.loudness_sq_save[chn] = psycho_loudness_approx(fftenergy, gfc);\n }\n }\n\n var fircoef_ = [-8.65163e-18 * 2,\n -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,\n -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2,\n 0.0931738 * 2, -5.52212e-17 * 2, -0.313819 * 2];\n\n /**\n * Apply HPF of fs/4 to the input signal. This is used for attack detection\n * / handling.\n */\n function vbrpsy_attack_detection(gfp, buffer, bufPos, gr_out, masking_ratio, masking_MS_ratio, energy, sub_short_factor, ns_attacks, uselongblock) {\n var ns_hpfsmpl = new_float_n([2, 576]);\n var gfc = gfp.internal_flags;\n var n_chn_out = gfc.channels_out;\n /* chn=2 and 3 = Mid and Side channels */\n var n_chn_psy = (gfp.mode == MPEGMode.JOINT_STEREO) ? 4 : n_chn_out;\n /* Don't copy the input buffer into a temporary buffer */\n /* unroll the loop 2 times */\n for (var chn = 0; chn < n_chn_out; chn++) {\n /* apply high pass filter of fs/4 */\n firbuf = buffer[chn];\n var firbufPos = bufPos + 576 - 350 - NSFIRLEN + 192;\n for (var i = 0; i < 576; i++) {\n var sum1, sum2;\n sum1 = firbuf[firbufPos + i + 10];\n sum2 = 0.0;\n for (var j = 0; j < ((NSFIRLEN - 1) / 2) - 1; j += 2) {\n sum1 += fircoef_[j]\n * (firbuf[firbufPos + i + j] + firbuf[firbufPos + i\n + NSFIRLEN - j]);\n sum2 += fircoef_[j + 1]\n * (firbuf[firbufPos + i + j + 1] + firbuf[firbufPos\n + i + NSFIRLEN - j - 1]);\n }\n ns_hpfsmpl[chn][i] = sum1 + sum2;\n }\n masking_ratio[gr_out][chn].en.assign(gfc.en[chn]);\n masking_ratio[gr_out][chn].thm.assign(gfc.thm[chn]);\n if (n_chn_psy > 2) {\n /* MS maskings */\n /* percep_MS_entropy [chn-2] = gfc . pe [chn]; */\n masking_MS_ratio[gr_out][chn].en.assign(gfc.en[chn + 2]);\n masking_MS_ratio[gr_out][chn].thm.assign(gfc.thm[chn + 2]);\n }\n }\n for (var chn = 0; chn < n_chn_psy; chn++) {\n var attack_intensity = new_float(12);\n var en_subshort = new_float(12);\n var en_short = [0, 0, 0, 0];\n var pf = ns_hpfsmpl[chn & 1];\n var pfPos = 0;\n var attackThreshold = (chn == 3) ? gfc.nsPsy.attackthre_s\n : gfc.nsPsy.attackthre;\n var ns_uselongblock = 1;\n\n if (chn == 2) {\n for (var i = 0, j = 576; j > 0; ++i, --j) {\n var l = ns_hpfsmpl[0][i];\n var r = ns_hpfsmpl[1][i];\n ns_hpfsmpl[0][i] = l + r;\n ns_hpfsmpl[1][i] = l - r;\n }\n }\n /***************************************************************\n * determine the block type (window type)\n ***************************************************************/\n /* calculate energies of each sub-shortblocks */\n for (var i = 0; i < 3; i++) {\n en_subshort[i] = gfc.nsPsy.last_en_subshort[chn][i + 6];\n attack_intensity[i] = en_subshort[i]\n / gfc.nsPsy.last_en_subshort[chn][i + 4];\n en_short[0] += en_subshort[i];\n }\n\n for (var i = 0; i < 9; i++) {\n var pfe = pfPos + 576 / 9;\n var p = 1.;\n for (; pfPos < pfe; pfPos++)\n if (p < Math.abs(pf[pfPos]))\n p = Math.abs(pf[pfPos]);\n\n gfc.nsPsy.last_en_subshort[chn][i] = en_subshort[i + 3] = p;\n en_short[1 + i / 3] += p;\n if (p > en_subshort[i + 3 - 2]) {\n p = p / en_subshort[i + 3 - 2];\n } else if (en_subshort[i + 3 - 2] > p * 10.0) {\n p = en_subshort[i + 3 - 2] / (p * 10.0);\n } else {\n p = 0.0;\n }\n attack_intensity[i + 3] = p;\n }\n /* pulse like signal detection for fatboy.wav and so on */\n for (var i = 0; i < 3; ++i) {\n var enn = en_subshort[i * 3 + 3]\n + en_subshort[i * 3 + 4] + en_subshort[i * 3 + 5];\n var factor = 1.;\n if (en_subshort[i * 3 + 5] * 6 < enn) {\n factor *= 0.5;\n if (en_subshort[i * 3 + 4] * 6 < enn) {\n factor *= 0.5;\n }\n }\n sub_short_factor[chn][i] = factor;\n }\n\n if (gfp.analysis) {\n var x = attack_intensity[0];\n for (var i = 1; i < 12; i++) {\n if (x < attack_intensity[i]) {\n x = attack_intensity[i];\n }\n }\n gfc.pinfo.ers[gr_out][chn] = gfc.pinfo.ers_save[chn];\n gfc.pinfo.ers_save[chn] = x;\n }\n\n /* compare energies between sub-shortblocks */\n for (var i = 0; i < 12; i++) {\n if (0 == ns_attacks[chn][i / 3]\n && attack_intensity[i] > attackThreshold) {\n ns_attacks[chn][i / 3] = (i % 3) + 1;\n }\n }\n\n /*\n * should have energy change between short blocks, in order to avoid\n * periodic signals\n */\n /* Good samples to show the effect are Trumpet test songs */\n /*\n * GB: tuned (1) to avoid too many short blocks for test sample\n * TRUMPET\n */\n /*\n * RH: tuned (2) to let enough short blocks through for test sample\n * FSOL and SNAPS\n */\n for (var i = 1; i < 4; i++) {\n var u = en_short[i - 1];\n var v = en_short[i];\n var m = Math.max(u, v);\n if (m < 40000) { /* (2) */\n if (u < 1.7 * v && v < 1.7 * u) { /* (1) */\n if (i == 1 && ns_attacks[chn][0] <= ns_attacks[chn][i]) {\n ns_attacks[chn][0] = 0;\n }\n ns_attacks[chn][i] = 0;\n }\n }\n }\n\n if (ns_attacks[chn][0] <= gfc.nsPsy.lastAttacks[chn]) {\n ns_attacks[chn][0] = 0;\n }\n\n if (gfc.nsPsy.lastAttacks[chn] == 3\n || (ns_attacks[chn][0] + ns_attacks[chn][1]\n + ns_attacks[chn][2] + ns_attacks[chn][3]) != 0) {\n ns_uselongblock = 0;\n\n if (ns_attacks[chn][1] != 0 && ns_attacks[chn][0] != 0) {\n ns_attacks[chn][1] = 0;\n }\n if (ns_attacks[chn][2] != 0 && ns_attacks[chn][1] != 0) {\n ns_attacks[chn][2] = 0;\n }\n if (ns_attacks[chn][3] != 0 && ns_attacks[chn][2] != 0) {\n ns_attacks[chn][3] = 0;\n }\n }\n if (chn < 2) {\n uselongblock[chn] = ns_uselongblock;\n } else {\n if (ns_uselongblock == 0) {\n uselongblock[0] = uselongblock[1] = 0;\n }\n }\n\n /*\n * there is a one granule delay. Copy maskings computed last call\n * into masking_ratio to return to calling program.\n */\n energy[chn] = gfc.tot_ener[chn];\n }\n }\n\n function vbrpsy_skip_masking_s(gfc, chn, sblock) {\n if (sblock == 0) {\n for (var b = 0; b < gfc.npart_s; b++) {\n gfc.nb_s2[chn][b] = gfc.nb_s1[chn][b];\n gfc.nb_s1[chn][b] = 0;\n }\n }\n }\n\n function vbrpsy_skip_masking_l(gfc, chn) {\n for (var b = 0; b < gfc.npart_l; b++) {\n gfc.nb_2[chn][b] = gfc.nb_1[chn][b];\n gfc.nb_1[chn][b] = 0;\n }\n }\n\n function psyvbr_calc_mask_index_s(gfc, max, avg, mask_idx) {\n var last_tab_entry = tab.length - 1;\n var b = 0;\n var a = avg[b] + avg[b + 1];\n if (a > 0.0) {\n var m = max[b];\n if (m < max[b + 1])\n m = max[b + 1];\n a = 20.0 * (m * 2.0 - a)\n / (a * (gfc.numlines_s[b] + gfc.numlines_s[b + 1] - 1));\n var k = 0 | a;\n if (k > last_tab_entry)\n k = last_tab_entry;\n mask_idx[b] = k;\n } else {\n mask_idx[b] = 0;\n }\n\n for (b = 1; b < gfc.npart_s - 1; b++) {\n a = avg[b - 1] + avg[b] + avg[b + 1];\n if (a > 0.0) {\n var m = max[b - 1];\n if (m < max[b])\n m = max[b];\n if (m < max[b + 1])\n m = max[b + 1];\n a = 20.0\n * (m * 3.0 - a)\n / (a * (gfc.numlines_s[b - 1] + gfc.numlines_s[b]\n + gfc.numlines_s[b + 1] - 1));\n var k = 0 | a;\n if (k > last_tab_entry)\n k = last_tab_entry;\n mask_idx[b] = k;\n } else {\n mask_idx[b] = 0;\n }\n }\n\n a = avg[b - 1] + avg[b];\n if (a > 0.0) {\n var m = max[b - 1];\n if (m < max[b])\n m = max[b];\n a = 20.0 * (m * 2.0 - a)\n / (a * (gfc.numlines_s[b - 1] + gfc.numlines_s[b] - 1));\n var k = 0 | a;\n if (k > last_tab_entry)\n k = last_tab_entry;\n mask_idx[b] = k;\n } else {\n mask_idx[b] = 0;\n }\n }\n\n function vbrpsy_compute_masking_s(gfp, fftenergy_s, eb, thr, chn, sblock) {\n var gfc = gfp.internal_flags;\n var max = new float[Encoder.CBANDS], avg = new_float(Encoder.CBANDS);\n var i, j, b;\n var mask_idx_s = new int[Encoder.CBANDS];\n\n for (b = j = 0; b < gfc.npart_s; ++b) {\n var ebb = 0, m = 0;\n var n = gfc.numlines_s[b];\n for (i = 0; i < n; ++i, ++j) {\n var el = fftenergy_s[sblock][j];\n ebb += el;\n if (m < el)\n m = el;\n }\n eb[b] = ebb;\n max[b] = m;\n avg[b] = ebb / n;\n }\n for (; b < Encoder.CBANDS; ++b) {\n max[b] = 0;\n avg[b] = 0;\n }\n psyvbr_calc_mask_index_s(gfc, max, avg, mask_idx_s);\n for (j = b = 0; b < gfc.npart_s; b++) {\n var kk = gfc.s3ind_s[b][0];\n var last = gfc.s3ind_s[b][1];\n var dd, dd_n;\n var x, ecb, avg_mask;\n dd = mask_idx_s[kk];\n dd_n = 1;\n ecb = gfc.s3_ss[j] * eb[kk] * tab[mask_idx_s[kk]];\n ++j;\n ++kk;\n while (kk <= last) {\n dd += mask_idx_s[kk];\n dd_n += 1;\n x = gfc.s3_ss[j] * eb[kk] * tab[mask_idx_s[kk]];\n ecb = vbrpsy_mask_add(ecb, x, kk - b);\n ++j;\n ++kk;\n }\n dd = (1 + 2 * dd) / (2 * dd_n);\n avg_mask = tab[dd] * 0.5;\n ecb *= avg_mask;\n thr[b] = ecb;\n gfc.nb_s2[chn][b] = gfc.nb_s1[chn][b];\n gfc.nb_s1[chn][b] = ecb;\n {\n /*\n * if THR exceeds EB, the quantization routines will take the\n * difference from other bands. in case of strong tonal samples\n * (tonaltest.wav) this leads to heavy distortions. that's why\n * we limit THR here.\n */\n x = max[b];\n x *= gfc.minval_s[b];\n x *= avg_mask;\n if (thr[b] > x) {\n thr[b] = x;\n }\n }\n if (gfc.masking_lower > 1) {\n thr[b] *= gfc.masking_lower;\n }\n if (thr[b] > eb[b]) {\n thr[b] = eb[b];\n }\n if (gfc.masking_lower < 1) {\n thr[b] *= gfc.masking_lower;\n }\n\n }\n for (; b < Encoder.CBANDS; ++b) {\n eb[b] = 0;\n thr[b] = 0;\n }\n }\n\n function vbrpsy_compute_masking_l(gfc, fftenergy, eb_l, thr, chn) {\n var max = new_float(Encoder.CBANDS), avg = new_float(Encoder.CBANDS);\n var mask_idx_l = new_int(Encoder.CBANDS + 2);\n var b;\n\n /*********************************************************************\n * Calculate the energy and the tonality of each partition.\n *********************************************************************/\n calc_energy(gfc, fftenergy, eb_l, max, avg);\n calc_mask_index_l(gfc, max, avg, mask_idx_l);\n\n /*********************************************************************\n * convolve the partitioned energy and unpredictability with the\n * spreading function, s3_l[b][k]\n ********************************************************************/\n var k = 0;\n for (b = 0; b < gfc.npart_l; b++) {\n var x, ecb, avg_mask, t;\n /* convolve the partitioned energy with the spreading function */\n var kk = gfc.s3ind[b][0];\n var last = gfc.s3ind[b][1];\n var dd = 0, dd_n = 0;\n dd = mask_idx_l[kk];\n dd_n += 1;\n ecb = gfc.s3_ll[k] * eb_l[kk] * tab[mask_idx_l[kk]];\n ++k;\n ++kk;\n while (kk <= last) {\n dd += mask_idx_l[kk];\n dd_n += 1;\n x = gfc.s3_ll[k] * eb_l[kk] * tab[mask_idx_l[kk]];\n t = vbrpsy_mask_add(ecb, x, kk - b);\n ecb = t;\n ++k;\n ++kk;\n }\n dd = (1 + 2 * dd) / (2 * dd_n);\n avg_mask = tab[dd] * 0.5;\n ecb *= avg_mask;\n\n /**** long block pre-echo control ****/\n /**\n * \n * dont use long block pre-echo control if previous granule was\n * a short block. This is to avoid the situation:\n * frame0: quiet (very low masking)\n * frame1: surge (triggers short blocks)\n * frame2: regular frame. looks like pre-echo when compared to\n * frame0, but all pre-echo was in frame1.\n *
\n */\n /*\n * chn=0,1 L and R channels chn=2,3 S and M channels.\n */\n if (gfc.blocktype_old[chn & 0x01] == Encoder.SHORT_TYPE) {\n var ecb_limit = rpelev * gfc.nb_1[chn][b];\n if (ecb_limit > 0) {\n thr[b] = Math.min(ecb, ecb_limit);\n } else {\n /**\n * \n * Robert 071209:\n * Because we don't calculate long block psy when we know a granule\n * should be of short blocks, we don't have any clue how the granule\n * before would have looked like as a long block. So we have to guess\n * a little bit for this END_TYPE block.\n * Most of the time we get away with this sloppyness. (fingers crossed :)\n * The speed increase is worth it.\n *
\n */\n thr[b] = Math.min(ecb, eb_l[b] * NS_PREECHO_ATT2);\n }\n } else {\n var ecb_limit_2 = rpelev2 * gfc.nb_2[chn][b];\n var ecb_limit_1 = rpelev * gfc.nb_1[chn][b];\n var ecb_limit;\n if (ecb_limit_2 <= 0) {\n ecb_limit_2 = ecb;\n }\n if (ecb_limit_1 <= 0) {\n ecb_limit_1 = ecb;\n }\n if (gfc.blocktype_old[chn & 0x01] == Encoder.NORM_TYPE) {\n ecb_limit = Math.min(ecb_limit_1, ecb_limit_2);\n } else {\n ecb_limit = ecb_limit_1;\n }\n thr[b] = Math.min(ecb, ecb_limit);\n }\n gfc.nb_2[chn][b] = gfc.nb_1[chn][b];\n gfc.nb_1[chn][b] = ecb;\n {\n /*\n * if THR exceeds EB, the quantization routines will take the\n * difference from other bands. in case of strong tonal samples\n * (tonaltest.wav) this leads to heavy distortions. that's why\n * we limit THR here.\n */\n x = max[b];\n x *= gfc.minval_l[b];\n x *= avg_mask;\n if (thr[b] > x) {\n thr[b] = x;\n }\n }\n if (gfc.masking_lower > 1) {\n thr[b] *= gfc.masking_lower;\n }\n if (thr[b] > eb_l[b]) {\n thr[b] = eb_l[b];\n }\n if (gfc.masking_lower < 1) {\n thr[b] *= gfc.masking_lower;\n }\n }\n for (; b < Encoder.CBANDS; ++b) {\n eb_l[b] = 0;\n thr[b] = 0;\n }\n }\n\n function vbrpsy_compute_block_type(gfp, uselongblock) {\n var gfc = gfp.internal_flags;\n\n if (gfp.short_blocks == ShortBlock.short_block_coupled\n /* force both channels to use the same block type */\n /* this is necessary if the frame is to be encoded in ms_stereo. */\n /* But even without ms_stereo, FhG does this */\n && !(uselongblock[0] != 0 && uselongblock[1] != 0))\n uselongblock[0] = uselongblock[1] = 0;\n\n for (var chn = 0; chn < gfc.channels_out; chn++) {\n /* disable short blocks */\n if (gfp.short_blocks == ShortBlock.short_block_dispensed) {\n uselongblock[chn] = 1;\n }\n if (gfp.short_blocks == ShortBlock.short_block_forced) {\n uselongblock[chn] = 0;\n }\n }\n }\n\n function vbrpsy_apply_block_type(gfp, uselongblock, blocktype_d) {\n var gfc = gfp.internal_flags;\n\n /*\n * update the blocktype of the previous granule, since it depends on\n * what happend in this granule\n */\n for (var chn = 0; chn < gfc.channels_out; chn++) {\n var blocktype = Encoder.NORM_TYPE;\n /* disable short blocks */\n\n if (uselongblock[chn] != 0) {\n /* no attack : use long blocks */\n if (gfc.blocktype_old[chn] == Encoder.SHORT_TYPE)\n blocktype = Encoder.STOP_TYPE;\n } else {\n /* attack : use short blocks */\n blocktype = Encoder.SHORT_TYPE;\n if (gfc.blocktype_old[chn] == Encoder.NORM_TYPE) {\n gfc.blocktype_old[chn] = Encoder.START_TYPE;\n }\n if (gfc.blocktype_old[chn] == Encoder.STOP_TYPE)\n gfc.blocktype_old[chn] = Encoder.SHORT_TYPE;\n }\n\n blocktype_d[chn] = gfc.blocktype_old[chn];\n // value returned to calling program\n gfc.blocktype_old[chn] = blocktype;\n // save for next call to l3psy_anal\n }\n }\n\n /**\n * compute M/S thresholds from Johnston & Ferreira 1992 ICASSP paper\n */\n function vbrpsy_compute_MS_thresholds(eb, thr, cb_mld, ath_cb, athadjust, msfix, n) {\n var msfix2 = msfix * 2;\n var athlower = msfix > 0 ? Math.pow(10, athadjust) : 1;\n var rside, rmid;\n for (var b = 0; b < n; ++b) {\n var ebM = eb[2][b];\n var ebS = eb[3][b];\n var thmL = thr[0][b];\n var thmR = thr[1][b];\n var thmM = thr[2][b];\n var thmS = thr[3][b];\n\n /* use this fix if L & R masking differs by 2db or less */\n if (thmL <= 1.58 * thmR && thmR <= 1.58 * thmL) {\n var mld_m = cb_mld[b] * ebS;\n var mld_s = cb_mld[b] * ebM;\n rmid = Math.max(thmM, Math.min(thmS, mld_m));\n rside = Math.max(thmS, Math.min(thmM, mld_s));\n } else {\n rmid = thmM;\n rside = thmS;\n }\n if (msfix > 0) {\n /***************************************************************/\n /* Adjust M/S maskings if user set \"msfix\" */\n /***************************************************************/\n /* Naoki Shibata 2000 */\n var thmLR, thmMS;\n var ath = ath_cb[b] * athlower;\n thmLR = Math.min(Math.max(thmL, ath), Math.max(thmR, ath));\n thmM = Math.max(rmid, ath);\n thmS = Math.max(rside, ath);\n thmMS = thmM + thmS;\n if (thmMS > 0 && (thmLR * msfix2) < thmMS) {\n var f = thmLR * msfix2 / thmMS;\n thmM *= f;\n thmS *= f;\n }\n rmid = Math.min(thmM, rmid);\n rside = Math.min(thmS, rside);\n }\n if (rmid > ebM) {\n rmid = ebM;\n }\n if (rside > ebS) {\n rside = ebS;\n }\n thr[2][b] = rmid;\n thr[3][b] = rside;\n }\n }\n\n this.L3psycho_anal_vbr = function (gfp, buffer, bufPos, gr_out, masking_ratio, masking_MS_ratio, percep_entropy, percep_MS_entropy, energy, blocktype_d) {\n var gfc = gfp.internal_flags;\n\n /* fft and energy calculation */\n var wsamp_l;\n var wsamp_s;\n var fftenergy = new_float(Encoder.HBLKSIZE);\n var fftenergy_s = new_float_n([3, Encoder.HBLKSIZE_s]);\n var wsamp_L = new_float_n([2, Encoder.BLKSIZE]);\n var wsamp_S = new_float_n([2, 3, Encoder.BLKSIZE_s]);\n var eb = new_float_n([4, Encoder.CBANDS]), thr = new_float_n([4, Encoder.CBANDS]);\n var sub_short_factor = new_float_n([4, 3]);\n var pcfact = 0.6;\n\n /* block type */\n var ns_attacks = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],\n [0, 0, 0, 0]];\n var uselongblock = new_int(2);\n\n /* usual variables like loop indices, etc.. */\n\n /* chn=2 and 3 = Mid and Side channels */\n var n_chn_psy = (gfp.mode == MPEGMode.JOINT_STEREO) ? 4\n : gfc.channels_out;\n\n vbrpsy_attack_detection(gfp, buffer, bufPos, gr_out, masking_ratio,\n masking_MS_ratio, energy, sub_short_factor, ns_attacks,\n uselongblock);\n\n vbrpsy_compute_block_type(gfp, uselongblock);\n\n /* LONG BLOCK CASE */\n {\n for (var chn = 0; chn < n_chn_psy; chn++) {\n var ch01 = chn & 0x01;\n wsamp_l = wsamp_L;\n vbrpsy_compute_fft_l(gfp, buffer, bufPos, chn, gr_out,\n fftenergy, wsamp_l, ch01);\n\n vbrpsy_compute_loudness_approximation_l(gfp, gr_out, chn,\n fftenergy);\n\n if (uselongblock[ch01] != 0) {\n vbrpsy_compute_masking_l(gfc, fftenergy, eb[chn], thr[chn],\n chn);\n } else {\n vbrpsy_skip_masking_l(gfc, chn);\n }\n }\n if ((uselongblock[0] + uselongblock[1]) == 2) {\n /* M/S channel */\n if (gfp.mode == MPEGMode.JOINT_STEREO) {\n vbrpsy_compute_MS_thresholds(eb, thr, gfc.mld_cb_l,\n gfc.ATH.cb_l, gfp.ATHlower * gfc.ATH.adjust,\n gfp.msfix, gfc.npart_l);\n }\n }\n /* TODO: apply adaptive ATH masking here ?? */\n for (var chn = 0; chn < n_chn_psy; chn++) {\n var ch01 = chn & 0x01;\n if (uselongblock[ch01] != 0) {\n convert_partition2scalefac_l(gfc, eb[chn], thr[chn], chn);\n }\n }\n }\n\n /* SHORT BLOCKS CASE */\n {\n for (var sblock = 0; sblock < 3; sblock++) {\n for (var chn = 0; chn < n_chn_psy; ++chn) {\n var ch01 = chn & 0x01;\n\n if (uselongblock[ch01] != 0) {\n vbrpsy_skip_masking_s(gfc, chn, sblock);\n } else {\n /* compute masking thresholds for short blocks */\n wsamp_s = wsamp_S;\n vbrpsy_compute_fft_s(gfp, buffer, bufPos, chn, sblock,\n fftenergy_s, wsamp_s, ch01);\n vbrpsy_compute_masking_s(gfp, fftenergy_s, eb[chn],\n thr[chn], chn, sblock);\n }\n }\n if ((uselongblock[0] + uselongblock[1]) == 0) {\n /* M/S channel */\n if (gfp.mode == MPEGMode.JOINT_STEREO) {\n vbrpsy_compute_MS_thresholds(eb, thr, gfc.mld_cb_s,\n gfc.ATH.cb_s, gfp.ATHlower * gfc.ATH.adjust,\n gfp.msfix, gfc.npart_s);\n }\n /* L/R channel */\n }\n /* TODO: apply adaptive ATH masking here ?? */\n for (var chn = 0; chn < n_chn_psy; ++chn) {\n var ch01 = chn & 0x01;\n if (0 == uselongblock[ch01]) {\n convert_partition2scalefac_s(gfc, eb[chn], thr[chn],\n chn, sblock);\n }\n }\n }\n\n /**** short block pre-echo control ****/\n for (var chn = 0; chn < n_chn_psy; chn++) {\n var ch01 = chn & 0x01;\n\n if (uselongblock[ch01] != 0) {\n continue;\n }\n for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {\n var new_thmm = new_float(3);\n for (var sblock = 0; sblock < 3; sblock++) {\n var thmm = gfc.thm[chn].s[sb][sblock];\n thmm *= NS_PREECHO_ATT0;\n\n if (ns_attacks[chn][sblock] >= 2\n || ns_attacks[chn][sblock + 1] == 1) {\n var idx = (sblock != 0) ? sblock - 1 : 2;\n var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,\n NS_PREECHO_ATT1 * pcfact);\n thmm = Math.min(thmm, p);\n } else if (ns_attacks[chn][sblock] == 1) {\n var idx = (sblock != 0) ? sblock - 1 : 2;\n var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,\n NS_PREECHO_ATT2 * pcfact);\n thmm = Math.min(thmm, p);\n } else if ((sblock != 0 && ns_attacks[chn][sblock - 1] == 3)\n || (sblock == 0 && gfc.nsPsy.lastAttacks[chn] == 3)) {\n var idx = (sblock != 2) ? sblock + 1 : 0;\n var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,\n NS_PREECHO_ATT2 * pcfact);\n thmm = Math.min(thmm, p);\n }\n\n /* pulse like signal detection for fatboy.wav and so on */\n thmm *= sub_short_factor[chn][sblock];\n\n new_thmm[sblock] = thmm;\n }\n for (var sblock = 0; sblock < 3; sblock++) {\n gfc.thm[chn].s[sb][sblock] = new_thmm[sblock];\n }\n }\n }\n }\n for (var chn = 0; chn < n_chn_psy; chn++) {\n gfc.nsPsy.lastAttacks[chn] = ns_attacks[chn][2];\n }\n\n /***************************************************************\n * determine final block type\n ***************************************************************/\n vbrpsy_apply_block_type(gfp, uselongblock, blocktype_d);\n\n /*********************************************************************\n * compute the value of PE to return ... no delay and advance\n *********************************************************************/\n for (var chn = 0; chn < n_chn_psy; chn++) {\n var ppe;\n var ppePos;\n var type;\n var mr;\n\n if (chn > 1) {\n ppe = percep_MS_entropy;\n ppePos = -2;\n type = Encoder.NORM_TYPE;\n if (blocktype_d[0] == Encoder.SHORT_TYPE\n || blocktype_d[1] == Encoder.SHORT_TYPE)\n type = Encoder.SHORT_TYPE;\n mr = masking_MS_ratio[gr_out][chn - 2];\n } else {\n ppe = percep_entropy;\n ppePos = 0;\n type = blocktype_d[chn];\n mr = masking_ratio[gr_out][chn];\n }\n\n if (type == Encoder.SHORT_TYPE) {\n ppe[ppePos + chn] = pecalc_s(mr, gfc.masking_lower);\n } else {\n ppe[ppePos + chn] = pecalc_l(mr, gfc.masking_lower);\n }\n\n if (gfp.analysis) {\n gfc.pinfo.pe[gr_out][chn] = ppe[ppePos + chn];\n }\n }\n return 0;\n }\n\n function s3_func_x(bark, hf_slope) {\n var tempx = bark, tempy;\n\n if (tempx >= 0) {\n tempy = -tempx * 27;\n } else {\n tempy = tempx * hf_slope;\n }\n if (tempy <= -72.0) {\n return 0;\n }\n return Math.exp(tempy * LN_TO_LOG10);\n }\n\n function norm_s3_func_x(hf_slope) {\n var lim_a = 0, lim_b = 0;\n {\n var x = 0, l, h;\n for (x = 0; s3_func_x(x, hf_slope) > 1e-20; x -= 1)\n ;\n l = x;\n h = 0;\n while (Math.abs(h - l) > 1e-12) {\n x = (h + l) / 2;\n if (s3_func_x(x, hf_slope) > 0) {\n h = x;\n } else {\n l = x;\n }\n }\n lim_a = l;\n }\n {\n var x = 0, l, h;\n for (x = 0; s3_func_x(x, hf_slope) > 1e-20; x += 1)\n ;\n l = 0;\n h = x;\n while (Math.abs(h - l) > 1e-12) {\n x = (h + l) / 2;\n if (s3_func_x(x, hf_slope) > 0) {\n l = x;\n } else {\n h = x;\n }\n }\n lim_b = h;\n }\n {\n var sum = 0;\n var m = 1000;\n var i;\n for (i = 0; i <= m; ++i) {\n var x = lim_a + i * (lim_b - lim_a) / m;\n var y = s3_func_x(x, hf_slope);\n sum += y;\n }\n {\n var norm = (m + 1) / (sum * (lim_b - lim_a));\n /* printf( \"norm = %lf\\n\",norm); */\n return norm;\n }\n }\n }\n\n /**\n * The spreading function. Values returned in units of energy\n */\n function s3_func(bark) {\n var tempx, x, tempy, temp;\n tempx = bark;\n if (tempx >= 0)\n tempx *= 3;\n else\n tempx *= 1.5;\n\n if (tempx >= 0.5 && tempx <= 2.5) {\n temp = tempx - 0.5;\n x = 8.0 * (temp * temp - 2.0 * temp);\n } else\n x = 0.0;\n tempx += 0.474;\n tempy = 15.811389 + 7.5 * tempx - 17.5\n * Math.sqrt(1.0 + tempx * tempx);\n\n if (tempy <= -60.0)\n return 0.0;\n\n tempx = Math.exp((x + tempy) * LN_TO_LOG10);\n\n /**\n * \n * Normalization. The spreading function should be normalized so that:\n * +inf\n * /\n * | s3 [ bark ] d(bark) = 1\n * /\n * -inf\n *
\n */\n tempx /= .6609193;\n return tempx;\n }\n\n /**\n * see for example \"Zwicker: Psychoakustik, 1982; ISBN 3-540-11401-7\n */\n function freq2bark(freq) {\n /* input: freq in hz output: barks */\n if (freq < 0)\n freq = 0;\n freq = freq * 0.001;\n return 13.0 * Math.atan(.76 * freq) + 3.5\n * Math.atan(freq * freq / (7.5 * 7.5));\n }\n\n function init_numline(numlines, bo, bm, bval, bval_width, mld, bo_w, sfreq, blksize, scalepos, deltafreq, sbmax) {\n var b_frq = new_float(Encoder.CBANDS + 1);\n var sample_freq_frac = sfreq / (sbmax > 15 ? 2 * 576 : 2 * 192);\n var partition = new_int(Encoder.HBLKSIZE);\n var i;\n sfreq /= blksize;\n var j = 0;\n var ni = 0;\n /* compute numlines, the number of spectral lines in each partition band */\n /* each partition band should be about DELBARK wide. */\n for (i = 0; i < Encoder.CBANDS; i++) {\n var bark1;\n var j2;\n bark1 = freq2bark(sfreq * j);\n\n b_frq[i] = sfreq * j;\n\n for (j2 = j; freq2bark(sfreq * j2) - bark1 < DELBARK\n && j2 <= blksize / 2; j2++)\n ;\n\n numlines[i] = j2 - j;\n ni = i + 1;\n\n while (j < j2) {\n partition[j++] = i;\n }\n if (j > blksize / 2) {\n j = blksize / 2;\n ++i;\n break;\n }\n }\n b_frq[i] = sfreq * j;\n\n for (var sfb = 0; sfb < sbmax; sfb++) {\n var i1, i2, start, end;\n var arg;\n start = scalepos[sfb];\n end = scalepos[sfb + 1];\n\n i1 = 0 | Math.floor(.5 + deltafreq * (start - .5));\n if (i1 < 0)\n i1 = 0;\n i2 = 0 | Math.floor(.5 + deltafreq * (end - .5));\n\n if (i2 > blksize / 2)\n i2 = blksize / 2;\n\n bm[sfb] = (partition[i1] + partition[i2]) / 2;\n bo[sfb] = partition[i2];\n var f_tmp = sample_freq_frac * end;\n /*\n * calculate how much of this band belongs to current scalefactor\n * band\n */\n bo_w[sfb] = (f_tmp - b_frq[bo[sfb]])\n / (b_frq[bo[sfb] + 1] - b_frq[bo[sfb]]);\n if (bo_w[sfb] < 0) {\n bo_w[sfb] = 0;\n } else {\n if (bo_w[sfb] > 1) {\n bo_w[sfb] = 1;\n }\n }\n /* setup stereo demasking thresholds */\n /* formula reverse enginerred from plot in paper */\n arg = freq2bark(sfreq * scalepos[sfb] * deltafreq);\n arg = ( Math.min(arg, 15.5) / 15.5);\n\n mld[sfb] = Math.pow(10.0,\n 1.25 * (1 - Math.cos(Math.PI * arg)) - 2.5);\n }\n\n /* compute bark values of each critical band */\n j = 0;\n for (var k = 0; k < ni; k++) {\n var w = numlines[k];\n var bark1, bark2;\n\n bark1 = freq2bark(sfreq * (j));\n bark2 = freq2bark(sfreq * (j + w - 1));\n bval[k] = .5 * (bark1 + bark2);\n\n bark1 = freq2bark(sfreq * (j - .5));\n bark2 = freq2bark(sfreq * (j + w - .5));\n bval_width[k] = bark2 - bark1;\n j += w;\n }\n\n return ni;\n }\n\n function init_s3_values(s3ind, npart, bval, bval_width, norm, use_old_s3) {\n var s3 = new_float_n([Encoder.CBANDS, Encoder.CBANDS]);\n /*\n * The s3 array is not linear in the bark scale.\n *\n * bval[x] should be used to get the bark value.\n */\n var j;\n var numberOfNoneZero = 0;\n\n /**\n * \n * s[i][j], the value of the spreading function,\n * centered at band j (masker), for band i (maskee)\n *\n * i.e.: sum over j to spread into signal barkval=i\n * NOTE: i and j are used opposite as in the ISO docs\n *
\n */\n if (use_old_s3) {\n for (var i = 0; i < npart; i++) {\n for (j = 0; j < npart; j++) {\n var v = s3_func(bval[i] - bval[j]) * bval_width[j];\n s3[i][j] = v * norm[i];\n }\n }\n } else {\n for (j = 0; j < npart; j++) {\n var hf_slope = 15 + Math.min(21 / bval[j], 12);\n var s3_x_norm = norm_s3_func_x(hf_slope);\n for (var i = 0; i < npart; i++) {\n var v = s3_x_norm\n * s3_func_x(bval[i] - bval[j], hf_slope)\n * bval_width[j];\n s3[i][j] = v * norm[i];\n }\n }\n }\n for (var i = 0; i < npart; i++) {\n for (j = 0; j < npart; j++) {\n if (s3[i][j] > 0.0)\n break;\n }\n s3ind[i][0] = j;\n\n for (j = npart - 1; j > 0; j--) {\n if (s3[i][j] > 0.0)\n break;\n }\n s3ind[i][1] = j;\n numberOfNoneZero += (s3ind[i][1] - s3ind[i][0] + 1);\n }\n\n var p = new_float(numberOfNoneZero);\n var k = 0;\n for (var i = 0; i < npart; i++)\n for (j = s3ind[i][0]; j <= s3ind[i][1]; j++)\n p[k++] = s3[i][j];\n\n return p;\n }\n\n function stereo_demask(f) {\n /* setup stereo demasking thresholds */\n /* formula reverse enginerred from plot in paper */\n var arg = freq2bark(f);\n arg = (Math.min(arg, 15.5) / 15.5);\n\n return Math.pow(10.0,\n 1.25 * (1 - Math.cos(Math.PI * arg)) - 2.5);\n }\n\n /**\n * NOTE: the bitrate reduction from the inter-channel masking effect is low\n * compared to the chance of getting annyoing artefacts. L3psycho_anal_vbr\n * does not use this feature. (Robert 071216)\n */\n this.psymodel_init = function (gfp) {\n var gfc = gfp.internal_flags;\n var i;\n var useOldS3 = true;\n var bvl_a = 13, bvl_b = 24;\n var snr_l_a = 0, snr_l_b = 0;\n var snr_s_a = -8.25, snr_s_b = -4.5;\n var bval = new_float(Encoder.CBANDS);\n var bval_width = new_float(Encoder.CBANDS);\n var norm = new_float(Encoder.CBANDS);\n var sfreq = gfp.out_samplerate;\n\n switch (gfp.experimentalZ) {\n default:\n case 0:\n useOldS3 = true;\n break;\n case 1:\n useOldS3 = (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) ? false\n : true;\n break;\n case 2:\n useOldS3 = false;\n break;\n case 3:\n bvl_a = 8;\n snr_l_a = -1.75;\n snr_l_b = -0.0125;\n snr_s_a = -8.25;\n snr_s_b = -2.25;\n break;\n }\n gfc.ms_ener_ratio_old = .25;\n gfc.blocktype_old[0] = gfc.blocktype_old[1] = Encoder.NORM_TYPE;\n // the vbr header is long blocks\n\n for (i = 0; i < 4; ++i) {\n for (var j = 0; j < Encoder.CBANDS; ++j) {\n gfc.nb_1[i][j] = 1e20;\n gfc.nb_2[i][j] = 1e20;\n gfc.nb_s1[i][j] = gfc.nb_s2[i][j] = 1.0;\n }\n for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {\n gfc.en[i].l[sb] = 1e20;\n gfc.thm[i].l[sb] = 1e20;\n }\n for (var j = 0; j < 3; ++j) {\n for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {\n gfc.en[i].s[sb][j] = 1e20;\n gfc.thm[i].s[sb][j] = 1e20;\n }\n gfc.nsPsy.lastAttacks[i] = 0;\n }\n for (var j = 0; j < 9; j++)\n gfc.nsPsy.last_en_subshort[i][j] = 10.;\n }\n\n /* init. for loudness approx. -jd 2001 mar 27 */\n gfc.loudness_sq_save[0] = gfc.loudness_sq_save[1] = 0.0;\n\n /*************************************************************************\n * now compute the psychoacoustic model specific constants\n ************************************************************************/\n /* compute numlines, bo, bm, bval, bval_width, mld */\n\n gfc.npart_l = init_numline(gfc.numlines_l, gfc.bo_l, gfc.bm_l, bval,\n bval_width, gfc.mld_l, gfc.PSY.bo_l_weight, sfreq,\n Encoder.BLKSIZE, gfc.scalefac_band.l, Encoder.BLKSIZE\n / (2.0 * 576), Encoder.SBMAX_l);\n /* compute the spreading function */\n for (i = 0; i < gfc.npart_l; i++) {\n var snr = snr_l_a;\n if (bval[i] >= bvl_a) {\n snr = snr_l_b * (bval[i] - bvl_a) / (bvl_b - bvl_a) + snr_l_a\n * (bvl_b - bval[i]) / (bvl_b - bvl_a);\n }\n norm[i] = Math.pow(10.0, snr / 10.0);\n if (gfc.numlines_l[i] > 0) {\n gfc.rnumlines_l[i] = 1.0 / gfc.numlines_l[i];\n } else {\n gfc.rnumlines_l[i] = 0;\n }\n }\n gfc.s3_ll = init_s3_values(gfc.s3ind, gfc.npart_l, bval, bval_width,\n norm, useOldS3);\n\n /* compute long block specific values, ATH and MINVAL */\n var j = 0;\n for (i = 0; i < gfc.npart_l; i++) {\n var x;\n\n /* ATH */\n x = Float.MAX_VALUE;\n for (var k = 0; k < gfc.numlines_l[i]; k++, j++) {\n var freq = sfreq * j / (1000.0 * Encoder.BLKSIZE);\n var level;\n /*\n * ATH below 100 Hz constant, not further climbing\n */\n level = this.ATHformula(freq * 1000, gfp) - 20;\n // scale to FFT units; returned value is in dB\n level = Math.pow(10., 0.1 * level);\n // convert from dB . energy\n level *= gfc.numlines_l[i];\n if (x > level)\n x = level;\n }\n gfc.ATH.cb_l[i] = x;\n\n /*\n * MINVAL. For low freq, the strength of the masking is limited by\n * minval this is an ISO MPEG1 thing, dont know if it is really\n * needed\n */\n /*\n * FIXME: it does work to reduce low-freq problems in S53-Wind-Sax\n * and lead-voice samples, but introduces some 3 kbps bit bloat too.\n * TODO: Further refinement of the shape of this hack.\n */\n x = -20 + bval[i] * 20 / 10;\n if (x > 6) {\n x = 100;\n }\n if (x < -15) {\n x = -15;\n }\n x -= 8.;\n gfc.minval_l[i] = (Math.pow(10.0, x / 10.) * gfc.numlines_l[i]);\n }\n\n /************************************************************************\n * do the same things for short blocks\n ************************************************************************/\n gfc.npart_s = init_numline(gfc.numlines_s, gfc.bo_s, gfc.bm_s, bval,\n bval_width, gfc.mld_s, gfc.PSY.bo_s_weight, sfreq,\n Encoder.BLKSIZE_s, gfc.scalefac_band.s, Encoder.BLKSIZE_s\n / (2.0 * 192), Encoder.SBMAX_s);\n\n /* SNR formula. short block is normalized by SNR. is it still right ? */\n j = 0;\n for (i = 0; i < gfc.npart_s; i++) {\n var x;\n var snr = snr_s_a;\n if (bval[i] >= bvl_a) {\n snr = snr_s_b * (bval[i] - bvl_a) / (bvl_b - bvl_a) + snr_s_a\n * (bvl_b - bval[i]) / (bvl_b - bvl_a);\n }\n norm[i] = Math.pow(10.0, snr / 10.0);\n\n /* ATH */\n x = Float.MAX_VALUE;\n for (var k = 0; k < gfc.numlines_s[i]; k++, j++) {\n var freq = sfreq * j / (1000.0 * Encoder.BLKSIZE_s);\n var level;\n /* freq = Min(.1,freq); */\n /*\n * ATH below 100 Hz constant, not\n * further climbing\n */\n level = this.ATHformula(freq * 1000, gfp) - 20;\n // scale to FFT units; returned value is in dB\n level = Math.pow(10., 0.1 * level);\n // convert from dB . energy\n level *= gfc.numlines_s[i];\n if (x > level)\n x = level;\n }\n gfc.ATH.cb_s[i] = x;\n\n /*\n * MINVAL. For low freq, the strength of the masking is limited by\n * minval this is an ISO MPEG1 thing, dont know if it is really\n * needed\n */\n x = (-7.0 + bval[i] * 7.0 / 12.0);\n if (bval[i] > 12) {\n x *= 1 + Math.log(1 + x) * 3.1;\n }\n if (bval[i] < 12) {\n x *= 1 + Math.log(1 - x) * 2.3;\n }\n if (x < -15) {\n x = -15;\n }\n x -= 8;\n gfc.minval_s[i] = Math.pow(10.0, x / 10)\n * gfc.numlines_s[i];\n }\n\n gfc.s3_ss = init_s3_values(gfc.s3ind_s, gfc.npart_s, bval, bval_width,\n norm, useOldS3);\n\n init_mask_add_max_values();\n fft.init_fft(gfc);\n\n /* setup temporal masking */\n gfc.decay = Math.exp(-1.0 * LOG10\n / (temporalmask_sustain_sec * sfreq / 192.0));\n\n {\n var msfix;\n msfix = NS_MSFIX;\n if ((gfp.exp_nspsytune & 2) != 0)\n msfix = 1.0;\n if (Math.abs(gfp.msfix) > 0.0)\n msfix = gfp.msfix;\n gfp.msfix = msfix;\n\n /*\n * spread only from npart_l bands. Normally, we use the spreading\n * function to convolve from npart_l down to npart_l bands\n */\n for (var b = 0; b < gfc.npart_l; b++)\n if (gfc.s3ind[b][1] > gfc.npart_l - 1)\n gfc.s3ind[b][1] = gfc.npart_l - 1;\n }\n\n /*\n * prepare for ATH auto adjustment: we want to decrease the ATH by 12 dB\n * per second\n */\n var frame_duration = (576. * gfc.mode_gr / sfreq);\n gfc.ATH.decay = Math.pow(10., -12. / 10. * frame_duration);\n gfc.ATH.adjust = 0.01;\n /* minimum, for leading low loudness */\n gfc.ATH.adjustLimit = 1.0;\n /* on lead, allow adjust up to maximum */\n\n\n if (gfp.ATHtype != -1) {\n /* compute equal loudness weights (eql_w) */\n var freq;\n var freq_inc = gfp.out_samplerate\n / (Encoder.BLKSIZE);\n var eql_balance = 0.0;\n freq = 0.0;\n for (i = 0; i < Encoder.BLKSIZE / 2; ++i) {\n /* convert ATH dB to relative power (not dB) */\n /* to determine eql_w */\n freq += freq_inc;\n gfc.ATH.eql_w[i] = 1. / Math.pow(10, this.ATHformula(freq, gfp) / 10);\n eql_balance += gfc.ATH.eql_w[i];\n }\n eql_balance = 1.0 / eql_balance;\n for (i = Encoder.BLKSIZE / 2; --i >= 0;) { /* scale weights */\n gfc.ATH.eql_w[i] *= eql_balance;\n }\n }\n {\n for (var b = j = 0; b < gfc.npart_s; ++b) {\n for (i = 0; i < gfc.numlines_s[b]; ++i) {\n ++j;\n }\n }\n for (var b = j = 0; b < gfc.npart_l; ++b) {\n for (i = 0; i < gfc.numlines_l[b]; ++i) {\n ++j;\n }\n }\n }\n j = 0;\n for (i = 0; i < gfc.npart_l; i++) {\n var freq = sfreq * (j + gfc.numlines_l[i] / 2) / (1.0 * Encoder.BLKSIZE);\n gfc.mld_cb_l[i] = stereo_demask(freq);\n j += gfc.numlines_l[i];\n }\n for (; i < Encoder.CBANDS; ++i) {\n gfc.mld_cb_l[i] = 1;\n }\n j = 0;\n for (i = 0; i < gfc.npart_s; i++) {\n var freq = sfreq * (j + gfc.numlines_s[i] / 2) / (1.0 * Encoder.BLKSIZE_s);\n gfc.mld_cb_s[i] = stereo_demask(freq);\n j += gfc.numlines_s[i];\n }\n for (; i < Encoder.CBANDS; ++i) {\n gfc.mld_cb_s[i] = 1;\n }\n return 0;\n }\n\n /**\n * Those ATH formulas are returning their minimum value for input = -1\n */\n function ATHformula_GB(f, value) {\n /**\n * \n * from Painter & Spanias\n * modified by Gabriel Bouvigne to better fit the reality\n * ath = 3.640 * pow(f,-0.8)\n * - 6.800 * exp(-0.6*pow(f-3.4,2.0))\n * + 6.000 * exp(-0.15*pow(f-8.7,2.0))\n * + 0.6* 0.001 * pow(f,4.0);\n *\n *\n * In the past LAME was using the Painter &Spanias formula.\n * But we had some recurrent problems with HF content.\n * We measured real ATH values, and found the older formula\n * to be inaccurate in the higher part. So we made this new\n * formula and this solved most of HF problematic test cases.\n * The tradeoff is that in VBR mode it increases a lot the\n * bitrate.\n *
\n */\n\n /*\n * This curve can be adjusted according to the VBR scale: it adjusts\n * from something close to Painter & Spanias on V9 up to Bouvigne's\n * formula for V0. This way the VBR bitrate is more balanced according\n * to the -V value.\n */\n\n // the following Hack allows to ask for the lowest value\n if (f < -.3)\n f = 3410;\n\n // convert to khz\n f /= 1000;\n f = Math.max(0.1, f);\n var ath = 3.640 * Math.pow(f, -0.8) - 6.800\n * Math.exp(-0.6 * Math.pow(f - 3.4, 2.0)) + 6.000\n * Math.exp(-0.15 * Math.pow(f - 8.7, 2.0))\n + (0.6 + 0.04 * value) * 0.001 * Math.pow(f, 4.0);\n return ath;\n }\n\n this.ATHformula = function (f, gfp) {\n var ath;\n switch (gfp.ATHtype) {\n case 0:\n ath = ATHformula_GB(f, 9);\n break;\n case 1:\n // over sensitive, should probably be removed\n ath = ATHformula_GB(f, -1);\n break;\n case 2:\n ath = ATHformula_GB(f, 0);\n break;\n case 3:\n // modification of GB formula by Roel\n ath = ATHformula_GB(f, 1) + 6;\n break;\n case 4:\n ath = ATHformula_GB(f, gfp.ATHcurve);\n break;\n default:\n ath = ATHformula_GB(f, 0);\n break;\n }\n return ath;\n }\n\n }\n\n\n\n function Lame() {\n var self = this;\n var LAME_MAXALBUMART = (128 * 1024);\n\n Lame.V9 = 410;\n Lame.V8 = 420;\n Lame.V7 = 430;\n Lame.V6 = 440;\n Lame.V5 = 450;\n Lame.V4 = 460;\n Lame.V3 = 470;\n Lame.V2 = 480;\n Lame.V1 = 490;\n Lame.V0 = 500;\n\n /* still there for compatibility */\n\n Lame.R3MIX = 1000;\n Lame.STANDARD = 1001;\n Lame.EXTREME = 1002;\n Lame.INSANE = 1003;\n Lame.STANDARD_FAST = 1004;\n Lame.EXTREME_FAST = 1005;\n Lame.MEDIUM = 1006;\n Lame.MEDIUM_FAST = 1007;\n\n /**\n * maximum size of mp3buffer needed if you encode at most 1152 samples for\n * each call to lame_encode_buffer. see lame_encode_buffer() below\n * (LAME_MAXMP3BUFFER is now obsolete)\n */\n var LAME_MAXMP3BUFFER = (16384 + LAME_MAXALBUMART);\n Lame.LAME_MAXMP3BUFFER = LAME_MAXMP3BUFFER;\n\n var ga;\n var bs;\n var p;\n var qupvt;\n var qu;\n var psy = new PsyModel();\n var vbr;\n var ver;\n var id3;\n var mpglib;\n this.enc = new Encoder();\n\n this.setModules = function (_ga, _bs, _p, _qupvt, _qu, _vbr, _ver, _id3, _mpglib) {\n ga = _ga;\n bs = _bs;\n p = _p;\n qupvt = _qupvt;\n qu = _qu;\n vbr = _vbr;\n ver = _ver;\n id3 = _id3;\n mpglib = _mpglib;\n this.enc.setModules(bs, psy, qupvt, vbr);\n }\n\n /**\n * PSY Model related stuff\n */\n function PSY() {\n /**\n * The dbQ stuff.\n */\n this.mask_adjust = 0.;\n /**\n * The dbQ stuff.\n */\n this.mask_adjust_short = 0.;\n /* at transition from one scalefactor band to next */\n /**\n * Band weight long scalefactor bands.\n */\n this.bo_l_weight = new_float(Encoder.SBMAX_l);\n /**\n * Band weight short scalefactor bands.\n */\n this.bo_s_weight = new_float(Encoder.SBMAX_s);\n }\n\n function LowPassHighPass() {\n this.lowerlimit = 0.;\n }\n\n function BandPass(bitrate, lPass) {\n this.lowpass = lPass;\n }\n\n var LAME_ID = 0xFFF88E3B;\n\n function lame_init_old(gfp) {\n var gfc;\n\n gfp.class_id = LAME_ID;\n\n gfc = gfp.internal_flags = new LameInternalFlags();\n\n /* Global flags. set defaults here for non-zero values */\n /* see lame.h for description */\n /*\n * set integer values to -1 to mean that LAME will compute the best\n * value, UNLESS the calling program as set it (and the value is no\n * longer -1)\n */\n\n gfp.mode = MPEGMode.NOT_SET;\n gfp.original = 1;\n gfp.in_samplerate = 44100;\n gfp.num_channels = 2;\n gfp.num_samples = -1;\n\n gfp.bWriteVbrTag = true;\n gfp.quality = -1;\n gfp.short_blocks = null;\n gfc.subblock_gain = -1;\n\n gfp.lowpassfreq = 0;\n gfp.highpassfreq = 0;\n gfp.lowpasswidth = -1;\n gfp.highpasswidth = -1;\n\n gfp.VBR = VbrMode.vbr_off;\n gfp.VBR_q = 4;\n gfp.ATHcurve = -1;\n gfp.VBR_mean_bitrate_kbps = 128;\n gfp.VBR_min_bitrate_kbps = 0;\n gfp.VBR_max_bitrate_kbps = 0;\n gfp.VBR_hard_min = 0;\n gfc.VBR_min_bitrate = 1;\n /* not 0 ????? */\n gfc.VBR_max_bitrate = 13;\n /* not 14 ????? */\n\n gfp.quant_comp = -1;\n gfp.quant_comp_short = -1;\n\n gfp.msfix = -1;\n\n gfc.resample_ratio = 1;\n\n gfc.OldValue[0] = 180;\n gfc.OldValue[1] = 180;\n gfc.CurrentStep[0] = 4;\n gfc.CurrentStep[1] = 4;\n gfc.masking_lower = 1;\n gfc.nsPsy.attackthre = -1;\n gfc.nsPsy.attackthre_s = -1;\n\n gfp.scale = -1;\n\n gfp.athaa_type = -1;\n gfp.ATHtype = -1;\n /* default = -1 = set in lame_init_params */\n gfp.athaa_loudapprox = -1;\n /* 1 = flat loudness approx. (total energy) */\n /* 2 = equal loudness curve */\n gfp.athaa_sensitivity = 0.0;\n /* no offset */\n gfp.useTemporal = null;\n gfp.interChRatio = -1;\n\n /*\n * The reason for int mf_samples_to_encode = ENCDELAY + POSTDELAY;\n * ENCDELAY = internal encoder delay. And then we have to add\n * POSTDELAY=288 because of the 50% MDCT overlap. A 576 MDCT granule\n * decodes to 1152 samples. To synthesize the 576 samples centered under\n * this granule we need the previous granule for the first 288 samples\n * (no problem), and the next granule for the next 288 samples (not\n * possible if this is last granule). So we need to pad with 288 samples\n * to make sure we can encode the 576 samples we are interested in.\n */\n gfc.mf_samples_to_encode = Encoder.ENCDELAY + Encoder.POSTDELAY;\n gfp.encoder_padding = 0;\n gfc.mf_size = Encoder.ENCDELAY - Encoder.MDCTDELAY;\n /*\n * we pad input with this many 0's\n */\n\n gfp.findReplayGain = false;\n gfp.decode_on_the_fly = false;\n\n gfc.decode_on_the_fly = false;\n gfc.findReplayGain = false;\n gfc.findPeakSample = false;\n\n gfc.RadioGain = 0;\n gfc.AudiophileGain = 0;\n gfc.noclipGainChange = 0;\n gfc.noclipScale = -1.0;\n\n gfp.preset = 0;\n\n gfp.write_id3tag_automatic = true;\n return 0;\n }\n\n this.lame_init = function () {\n var gfp = new LameGlobalFlags();\n\n var ret = lame_init_old(gfp);\n if (ret != 0) {\n return null;\n }\n\n gfp.lame_allocated_gfp = 1;\n return gfp;\n }\n\n function filter_coef(x) {\n if (x > 1.0)\n return 0.0;\n if (x <= 0.0)\n return 1.0;\n\n return Math.cos(Math.PI / 2 * x);\n }\n\n this.nearestBitrateFullIndex = function (bitrate) {\n /* borrowed from DM abr presets */\n\n var full_bitrate_table = [8, 16, 24, 32, 40, 48, 56, 64, 80,\n 96, 112, 128, 160, 192, 224, 256, 320];\n\n var lower_range = 0, lower_range_kbps = 0, upper_range = 0, upper_range_kbps = 0;\n\n /* We assume specified bitrate will be 320kbps */\n upper_range_kbps = full_bitrate_table[16];\n upper_range = 16;\n lower_range_kbps = full_bitrate_table[16];\n lower_range = 16;\n\n /*\n * Determine which significant bitrates the value specified falls\n * between, if loop ends without breaking then we were correct above\n * that the value was 320\n */\n for (var b = 0; b < 16; b++) {\n if ((Math.max(bitrate, full_bitrate_table[b + 1])) != bitrate) {\n upper_range_kbps = full_bitrate_table[b + 1];\n upper_range = b + 1;\n lower_range_kbps = full_bitrate_table[b];\n lower_range = (b);\n break;\n /* We found upper range */\n }\n }\n\n /* Determine which range the value specified is closer to */\n if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps)) {\n return lower_range;\n }\n return upper_range;\n }\n\n function optimum_samplefreq(lowpassfreq, input_samplefreq) {\n /*\n * Rules:\n *\n * - if possible, sfb21 should NOT be used\n */\n var suggested_samplefreq = 44100;\n\n if (input_samplefreq >= 48000)\n suggested_samplefreq = 48000;\n else if (input_samplefreq >= 44100)\n suggested_samplefreq = 44100;\n else if (input_samplefreq >= 32000)\n suggested_samplefreq = 32000;\n else if (input_samplefreq >= 24000)\n suggested_samplefreq = 24000;\n else if (input_samplefreq >= 22050)\n suggested_samplefreq = 22050;\n else if (input_samplefreq >= 16000)\n suggested_samplefreq = 16000;\n else if (input_samplefreq >= 12000)\n suggested_samplefreq = 12000;\n else if (input_samplefreq >= 11025)\n suggested_samplefreq = 11025;\n else if (input_samplefreq >= 8000)\n suggested_samplefreq = 8000;\n\n if (lowpassfreq == -1)\n return suggested_samplefreq;\n\n if (lowpassfreq <= 15960)\n suggested_samplefreq = 44100;\n if (lowpassfreq <= 15250)\n suggested_samplefreq = 32000;\n if (lowpassfreq <= 11220)\n suggested_samplefreq = 24000;\n if (lowpassfreq <= 9970)\n suggested_samplefreq = 22050;\n if (lowpassfreq <= 7230)\n suggested_samplefreq = 16000;\n if (lowpassfreq <= 5420)\n suggested_samplefreq = 12000;\n if (lowpassfreq <= 4510)\n suggested_samplefreq = 11025;\n if (lowpassfreq <= 3970)\n suggested_samplefreq = 8000;\n\n if (input_samplefreq < suggested_samplefreq) {\n /*\n * choose a valid MPEG sample frequency above the input sample\n * frequency to avoid SFB21/12 bitrate bloat rh 061115\n */\n if (input_samplefreq > 44100) {\n return 48000;\n }\n if (input_samplefreq > 32000) {\n return 44100;\n }\n if (input_samplefreq > 24000) {\n return 32000;\n }\n if (input_samplefreq > 22050) {\n return 24000;\n }\n if (input_samplefreq > 16000) {\n return 22050;\n }\n if (input_samplefreq > 12000) {\n return 16000;\n }\n if (input_samplefreq > 11025) {\n return 12000;\n }\n if (input_samplefreq > 8000) {\n return 11025;\n }\n return 8000;\n }\n return suggested_samplefreq;\n }\n\n /**\n * convert samp freq in Hz to index\n */\n function SmpFrqIndex(sample_freq, gpf) {\n switch (sample_freq) {\n case 44100:\n gpf.version = 1;\n return 0;\n case 48000:\n gpf.version = 1;\n return 1;\n case 32000:\n gpf.version = 1;\n return 2;\n case 22050:\n gpf.version = 0;\n return 0;\n case 24000:\n gpf.version = 0;\n return 1;\n case 16000:\n gpf.version = 0;\n return 2;\n case 11025:\n gpf.version = 0;\n return 0;\n case 12000:\n gpf.version = 0;\n return 1;\n case 8000:\n gpf.version = 0;\n return 2;\n default:\n gpf.version = 0;\n return -1;\n }\n }\n\n /**\n * @param bRate\n * legal rates from 8 to 320\n */\n function FindNearestBitrate(bRate, version, samplerate) {\n /* MPEG-1 or MPEG-2 LSF */\n if (samplerate < 16000)\n version = 2;\n\n var bitrate = Tables.bitrate_table[version][1];\n\n for (var i = 2; i <= 14; i++) {\n if (Tables.bitrate_table[version][i] > 0) {\n if (Math.abs(Tables.bitrate_table[version][i] - bRate) < Math\n .abs(bitrate - bRate))\n bitrate = Tables.bitrate_table[version][i];\n }\n }\n return bitrate;\n }\n\n /**\n * @param bRate\n * legal rates from 32 to 448 kbps\n * @param version\n * MPEG-1 or MPEG-2/2.5 LSF\n */\n function BitrateIndex(bRate, version, samplerate) {\n /* convert bitrate in kbps to index */\n if (samplerate < 16000)\n version = 2;\n for (var i = 0; i <= 14; i++) {\n if (Tables.bitrate_table[version][i] > 0) {\n if (Tables.bitrate_table[version][i] == bRate) {\n return i;\n }\n }\n }\n return -1;\n }\n\n function optimum_bandwidth(lh, bitrate) {\n /**\n * \n * Input:\n * bitrate total bitrate in kbps\n *\n * Output:\n * lowerlimit: best lowpass frequency limit for input filter in Hz\n * upperlimit: best highpass frequency limit for input filter in Hz\n *
\n */\n var freq_map = [new BandPass(8, 2000),\n new BandPass(16, 3700), new BandPass(24, 3900),\n new BandPass(32, 5500), new BandPass(40, 7000),\n new BandPass(48, 7500), new BandPass(56, 10000),\n new BandPass(64, 11000), new BandPass(80, 13500),\n new BandPass(96, 15100), new BandPass(112, 15600),\n new BandPass(128, 17000), new BandPass(160, 17500),\n new BandPass(192, 18600), new BandPass(224, 19400),\n new BandPass(256, 19700), new BandPass(320, 20500)];\n\n var table_index = self.nearestBitrateFullIndex(bitrate);\n lh.lowerlimit = freq_map[table_index].lowpass;\n }\n\n function lame_init_params_ppflt(gfp) {\n var gfc = gfp.internal_flags;\n /***************************************************************/\n /* compute info needed for polyphase filter (filter type==0, default) */\n /***************************************************************/\n\n var lowpass_band = 32;\n var highpass_band = -1;\n\n if (gfc.lowpass1 > 0) {\n var minband = 999;\n for (var band = 0; band <= 31; band++) {\n var freq = (band / 31.0);\n /* this band and above will be zeroed: */\n if (freq >= gfc.lowpass2) {\n lowpass_band = Math.min(lowpass_band, band);\n }\n if (gfc.lowpass1 < freq && freq < gfc.lowpass2) {\n minband = Math.min(minband, band);\n }\n }\n\n /*\n * compute the *actual* transition band implemented by the polyphase\n * filter\n */\n if (minband == 999) {\n gfc.lowpass1 = (lowpass_band - .75) / 31.0;\n } else {\n gfc.lowpass1 = (minband - .75) / 31.0;\n }\n gfc.lowpass2 = lowpass_band / 31.0;\n }\n\n /*\n * make sure highpass filter is within 90% of what the effective\n * highpass frequency will be\n */\n if (gfc.highpass2 > 0) {\n if (gfc.highpass2 < .9 * (.75 / 31.0)) {\n gfc.highpass1 = 0;\n gfc.highpass2 = 0;\n System.err.println(\"Warning: highpass filter disabled. \"\n + \"highpass frequency too small\\n\");\n }\n }\n\n if (gfc.highpass2 > 0) {\n var maxband = -1;\n for (var band = 0; band <= 31; band++) {\n var freq = band / 31.0;\n /* this band and below will be zereod */\n if (freq <= gfc.highpass1) {\n highpass_band = Math.max(highpass_band, band);\n }\n if (gfc.highpass1 < freq && freq < gfc.highpass2) {\n maxband = Math.max(maxband, band);\n }\n }\n /*\n * compute the *actual* transition band implemented by the polyphase\n * filter\n */\n gfc.highpass1 = highpass_band / 31.0;\n if (maxband == -1) {\n gfc.highpass2 = (highpass_band + .75) / 31.0;\n } else {\n gfc.highpass2 = (maxband + .75) / 31.0;\n }\n }\n\n for (var band = 0; band < 32; band++) {\n var fc1, fc2;\n var freq = band / 31.0;\n if (gfc.highpass2 > gfc.highpass1) {\n fc1 = filter_coef((gfc.highpass2 - freq)\n / (gfc.highpass2 - gfc.highpass1 + 1e-20));\n } else {\n fc1 = 1.0;\n }\n if (gfc.lowpass2 > gfc.lowpass1) {\n fc2 = filter_coef((freq - gfc.lowpass1)\n / (gfc.lowpass2 - gfc.lowpass1 + 1e-20));\n } else {\n fc2 = 1.0;\n }\n gfc.amp_filter[band] = (fc1 * fc2);\n }\n }\n\n function lame_init_qval(gfp) {\n var gfc = gfp.internal_flags;\n\n switch (gfp.quality) {\n default:\n case 9: /* no psymodel, no noise shaping */\n gfc.psymodel = 0;\n gfc.noise_shaping = 0;\n gfc.noise_shaping_amp = 0;\n gfc.noise_shaping_stop = 0;\n gfc.use_best_huffman = 0;\n gfc.full_outer_loop = 0;\n break;\n\n case 8:\n gfp.quality = 7;\n //$FALL-THROUGH$\n case 7:\n /*\n * use psymodel (for short block and m/s switching), but no noise\n * shapping\n */\n gfc.psymodel = 1;\n gfc.noise_shaping = 0;\n gfc.noise_shaping_amp = 0;\n gfc.noise_shaping_stop = 0;\n gfc.use_best_huffman = 0;\n gfc.full_outer_loop = 0;\n break;\n\n case 6:\n gfc.psymodel = 1;\n if (gfc.noise_shaping == 0)\n gfc.noise_shaping = 1;\n gfc.noise_shaping_amp = 0;\n gfc.noise_shaping_stop = 0;\n if (gfc.subblock_gain == -1)\n gfc.subblock_gain = 1;\n gfc.use_best_huffman = 0;\n gfc.full_outer_loop = 0;\n break;\n\n case 5:\n gfc.psymodel = 1;\n if (gfc.noise_shaping == 0)\n gfc.noise_shaping = 1;\n gfc.noise_shaping_amp = 0;\n gfc.noise_shaping_stop = 0;\n if (gfc.subblock_gain == -1)\n gfc.subblock_gain = 1;\n gfc.use_best_huffman = 0;\n gfc.full_outer_loop = 0;\n break;\n\n case 4:\n gfc.psymodel = 1;\n if (gfc.noise_shaping == 0)\n gfc.noise_shaping = 1;\n gfc.noise_shaping_amp = 0;\n gfc.noise_shaping_stop = 0;\n if (gfc.subblock_gain == -1)\n gfc.subblock_gain = 1;\n gfc.use_best_huffman = 1;\n gfc.full_outer_loop = 0;\n break;\n\n case 3:\n gfc.psymodel = 1;\n if (gfc.noise_shaping == 0)\n gfc.noise_shaping = 1;\n gfc.noise_shaping_amp = 1;\n gfc.noise_shaping_stop = 1;\n if (gfc.subblock_gain == -1)\n gfc.subblock_gain = 1;\n gfc.use_best_huffman = 1;\n gfc.full_outer_loop = 0;\n break;\n\n case 2:\n gfc.psymodel = 1;\n if (gfc.noise_shaping == 0)\n gfc.noise_shaping = 1;\n if (gfc.substep_shaping == 0)\n gfc.substep_shaping = 2;\n gfc.noise_shaping_amp = 1;\n gfc.noise_shaping_stop = 1;\n if (gfc.subblock_gain == -1)\n gfc.subblock_gain = 1;\n gfc.use_best_huffman = 1;\n /* inner loop */\n gfc.full_outer_loop = 0;\n break;\n\n case 1:\n gfc.psymodel = 1;\n if (gfc.noise_shaping == 0)\n gfc.noise_shaping = 1;\n if (gfc.substep_shaping == 0)\n gfc.substep_shaping = 2;\n gfc.noise_shaping_amp = 2;\n gfc.noise_shaping_stop = 1;\n if (gfc.subblock_gain == -1)\n gfc.subblock_gain = 1;\n gfc.use_best_huffman = 1;\n gfc.full_outer_loop = 0;\n break;\n\n case 0:\n gfc.psymodel = 1;\n if (gfc.noise_shaping == 0)\n gfc.noise_shaping = 1;\n if (gfc.substep_shaping == 0)\n gfc.substep_shaping = 2;\n gfc.noise_shaping_amp = 2;\n gfc.noise_shaping_stop = 1;\n if (gfc.subblock_gain == -1)\n gfc.subblock_gain = 1;\n gfc.use_best_huffman = 1;\n /*\n * type 2 disabled because of it slowness, in favor of full outer\n * loop search\n */\n gfc.full_outer_loop = 0;\n /*\n * full outer loop search disabled because of audible distortions it\n * may generate rh 060629\n */\n break;\n }\n\n }\n\n function lame_init_bitstream(gfp) {\n var gfc = gfp.internal_flags;\n gfp.frameNum = 0;\n\n if (gfp.write_id3tag_automatic) {\n id3.id3tag_write_v2(gfp);\n }\n /* initialize histogram data optionally used by frontend */\n\n gfc.bitrate_stereoMode_Hist = new_int_n([16, 4 + 1]);\n gfc.bitrate_blockType_Hist = new_int_n([16, 4 + 1 + 1]);\n\n gfc.PeakSample = 0.0;\n\n /* Write initial VBR Header to bitstream and init VBR data */\n if (gfp.bWriteVbrTag)\n vbr.InitVbrTag(gfp);\n }\n\n /********************************************************************\n * initialize internal params based on data in gf (globalflags struct filled\n * in by calling program)\n *\n * OUTLINE:\n *\n * We first have some complex code to determine bitrate, output samplerate\n * and mode. It is complicated by the fact that we allow the user to set\n * some or all of these parameters, and need to determine best possible\n * values for the rest of them:\n *\n * 1. set some CPU related flags 2. check if we are mono.mono, stereo.mono\n * or stereo.stereo 3. compute bitrate and output samplerate: user may have\n * set compression ratio user may have set a bitrate user may have set a\n * output samplerate 4. set some options which depend on output samplerate\n * 5. compute the actual compression ratio 6. set mode based on compression\n * ratio\n *\n * The remaining code is much simpler - it just sets options based on the\n * mode & compression ratio:\n *\n * set allow_diff_short based on mode select lowpass filter based on\n * compression ratio & mode set the bitrate index, and min/max bitrates for\n * VBR modes disable VBR tag if it is not appropriate initialize the\n * bitstream initialize scalefac_band data set sideinfo_len (based on\n * channels, CRC, out_samplerate) write an id3v2 tag into the bitstream\n * write VBR tag into the bitstream set mpeg1/2 flag estimate the number of\n * frames (based on a lot of data)\n *\n * now we set more flags: nspsytune: see code VBR modes see code CBR/ABR see\n * code\n *\n * Finally, we set the algorithm flags based on the gfp.quality value\n * lame_init_qval(gfp);\n *\n ********************************************************************/\n this.lame_init_params = function (gfp) {\n var gfc = gfp.internal_flags;\n\n gfc.Class_ID = 0;\n if (gfc.ATH == null)\n gfc.ATH = new ATH();\n if (gfc.PSY == null)\n gfc.PSY = new PSY();\n if (gfc.rgdata == null)\n gfc.rgdata = new ReplayGain();\n\n gfc.channels_in = gfp.num_channels;\n if (gfc.channels_in == 1)\n gfp.mode = MPEGMode.MONO;\n gfc.channels_out = (gfp.mode == MPEGMode.MONO) ? 1 : 2;\n gfc.mode_ext = Encoder.MPG_MD_MS_LR;\n if (gfp.mode == MPEGMode.MONO)\n gfp.force_ms = false;\n /*\n * don't allow forced mid/side stereo for mono output\n */\n\n if (gfp.VBR == VbrMode.vbr_off && gfp.VBR_mean_bitrate_kbps != 128\n && gfp.brate == 0)\n gfp.brate = gfp.VBR_mean_bitrate_kbps;\n\n if (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_mtrh\n || gfp.VBR == VbrMode.vbr_mt) {\n /* these modes can handle free format condition */\n } else {\n gfp.free_format = false;\n /* mode can't be mixed with free format */\n }\n\n if (gfp.VBR == VbrMode.vbr_off && gfp.brate == 0) {\n /* no bitrate or compression ratio specified, use 11.025 */\n if (BitStream.EQ(gfp.compression_ratio, 0))\n gfp.compression_ratio = 11.025;\n /*\n * rate to compress a CD down to exactly 128000 bps\n */\n }\n\n /* find bitrate if user specify a compression ratio */\n if (gfp.VBR == VbrMode.vbr_off && gfp.compression_ratio > 0) {\n\n if (gfp.out_samplerate == 0)\n gfp.out_samplerate = map2MP3Frequency((int)(0.97 * gfp.in_samplerate));\n /*\n * round up with a margin of 3 %\n */\n\n /*\n * choose a bitrate for the output samplerate which achieves\n * specified compression ratio\n */\n gfp.brate = 0 | (gfp.out_samplerate * 16 * gfc.channels_out / (1.e3 * gfp.compression_ratio));\n\n /* we need the version for the bitrate table look up */\n gfc.samplerate_index = SmpFrqIndex(gfp.out_samplerate, gfp);\n\n if (!gfp.free_format) /*\n * for non Free Format find the nearest allowed\n * bitrate\n */\n gfp.brate = FindNearestBitrate(gfp.brate, gfp.version,\n gfp.out_samplerate);\n }\n\n if (gfp.out_samplerate != 0) {\n if (gfp.out_samplerate < 16000) {\n gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,\n 8);\n gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,\n 64);\n } else if (gfp.out_samplerate < 32000) {\n gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,\n 8);\n gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,\n 160);\n } else {\n gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,\n 32);\n gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,\n 320);\n }\n }\n\n /****************************************************************/\n /* if a filter has not been enabled, see if we should add one: */\n /****************************************************************/\n if (gfp.lowpassfreq == 0) {\n var lowpass = 16000.;\n\n switch (gfp.VBR) {\n case VbrMode.vbr_off:\n {\n var lh = new LowPassHighPass();\n optimum_bandwidth(lh, gfp.brate);\n lowpass = lh.lowerlimit;\n break;\n }\n case VbrMode.vbr_abr:\n {\n var lh = new LowPassHighPass();\n optimum_bandwidth(lh, gfp.VBR_mean_bitrate_kbps);\n lowpass = lh.lowerlimit;\n break;\n }\n case VbrMode.vbr_rh:\n {\n var x = [19500, 19000, 18600, 18000, 17500, 16000,\n 15600, 14900, 12500, 10000, 3950];\n if (0 <= gfp.VBR_q && gfp.VBR_q <= 9) {\n var a = x[gfp.VBR_q], b = x[gfp.VBR_q + 1], m = gfp.VBR_q_frac;\n lowpass = linear_int(a, b, m);\n } else {\n lowpass = 19500;\n }\n break;\n }\n default:\n {\n var x = [19500, 19000, 18500, 18000, 17500, 16500,\n 15500, 14500, 12500, 9500, 3950];\n if (0 <= gfp.VBR_q && gfp.VBR_q <= 9) {\n var a = x[gfp.VBR_q], b = x[gfp.VBR_q + 1], m = gfp.VBR_q_frac;\n lowpass = linear_int(a, b, m);\n } else {\n lowpass = 19500;\n }\n }\n }\n if (gfp.mode == MPEGMode.MONO\n && (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_abr))\n lowpass *= 1.5;\n\n gfp.lowpassfreq = lowpass | 0;\n }\n\n if (gfp.out_samplerate == 0) {\n if (2 * gfp.lowpassfreq > gfp.in_samplerate) {\n gfp.lowpassfreq = gfp.in_samplerate / 2;\n }\n gfp.out_samplerate = optimum_samplefreq(gfp.lowpassfreq | 0,\n gfp.in_samplerate);\n }\n\n gfp.lowpassfreq = Math.min(20500, gfp.lowpassfreq);\n gfp.lowpassfreq = Math.min(gfp.out_samplerate / 2, gfp.lowpassfreq);\n\n if (gfp.VBR == VbrMode.vbr_off) {\n gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out\n / (1.e3 * gfp.brate);\n }\n if (gfp.VBR == VbrMode.vbr_abr) {\n gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out\n / (1.e3 * gfp.VBR_mean_bitrate_kbps);\n }\n\n /*\n * do not compute ReplayGain values and do not find the peak sample if\n * we can't store them\n */\n if (!gfp.bWriteVbrTag) {\n gfp.findReplayGain = false;\n gfp.decode_on_the_fly = false;\n gfc.findPeakSample = false;\n }\n gfc.findReplayGain = gfp.findReplayGain;\n gfc.decode_on_the_fly = gfp.decode_on_the_fly;\n\n if (gfc.decode_on_the_fly)\n gfc.findPeakSample = true;\n\n if (gfc.findReplayGain) {\n if (ga.InitGainAnalysis(gfc.rgdata, gfp.out_samplerate) == GainAnalysis.INIT_GAIN_ANALYSIS_ERROR) {\n gfp.internal_flags = null;\n return -6;\n }\n }\n\n if (gfc.decode_on_the_fly && !gfp.decode_only) {\n if (gfc.hip != null) {\n mpglib.hip_decode_exit(gfc.hip);\n }\n gfc.hip = mpglib.hip_decode_init();\n }\n\n gfc.mode_gr = gfp.out_samplerate <= 24000 ? 1 : 2;\n /*\n * Number of granules per frame\n */\n gfp.framesize = 576 * gfc.mode_gr;\n gfp.encoder_delay = Encoder.ENCDELAY;\n\n gfc.resample_ratio = gfp.in_samplerate / gfp.out_samplerate;\n\n /**\n * \n * sample freq bitrate compression ratio\n * [kHz] [kbps/channel] for 16 bit input\n * 44.1 56 12.6\n * 44.1 64 11.025\n * 44.1 80 8.82\n * 22.05 24 14.7\n * 22.05 32 11.025\n * 22.05 40 8.82\n * 16 16 16.0\n * 16 24 10.667\n *
\n */\n /**\n * \n * For VBR, take a guess at the compression_ratio.\n * For example:\n *\n * VBR_q compression like\n * - 4.4 320 kbps/44 kHz\n * 0...1 5.5 256 kbps/44 kHz\n * 2 7.3 192 kbps/44 kHz\n * 4 8.8 160 kbps/44 kHz\n * 6 11 128 kbps/44 kHz\n * 9 14.7 96 kbps\n *\n * for lower bitrates, downsample with --resample\n *
\n */\n switch (gfp.VBR) {\n case VbrMode.vbr_mt:\n case VbrMode.vbr_rh:\n case VbrMode.vbr_mtrh:\n {\n /* numbers are a bit strange, but they determine the lowpass value */\n var cmp = [5.7, 6.5, 7.3, 8.2, 10, 11.9, 13, 14,\n 15, 16.5];\n gfp.compression_ratio = cmp[gfp.VBR_q];\n }\n break;\n case VbrMode.vbr_abr:\n gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out\n / (1.e3 * gfp.VBR_mean_bitrate_kbps);\n break;\n default:\n gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out\n / (1.e3 * gfp.brate);\n break;\n }\n\n /*\n * mode = -1 (not set by user) or mode = MONO (because of only 1 input\n * channel). If mode has not been set, then select J-STEREO\n */\n if (gfp.mode == MPEGMode.NOT_SET) {\n gfp.mode = MPEGMode.JOINT_STEREO;\n }\n\n /* apply user driven high pass filter */\n if (gfp.highpassfreq > 0) {\n gfc.highpass1 = 2. * gfp.highpassfreq;\n\n if (gfp.highpasswidth >= 0)\n gfc.highpass2 = 2. * (gfp.highpassfreq + gfp.highpasswidth);\n else\n /* 0% above on default */\n gfc.highpass2 = (1 + 0.00) * 2. * gfp.highpassfreq;\n\n gfc.highpass1 /= gfp.out_samplerate;\n gfc.highpass2 /= gfp.out_samplerate;\n } else {\n gfc.highpass1 = 0;\n gfc.highpass2 = 0;\n }\n /* apply user driven low pass filter */\n if (gfp.lowpassfreq > 0) {\n gfc.lowpass2 = 2. * gfp.lowpassfreq;\n if (gfp.lowpasswidth >= 0) {\n gfc.lowpass1 = 2. * (gfp.lowpassfreq - gfp.lowpasswidth);\n if (gfc.lowpass1 < 0) /* has to be >= 0 */\n gfc.lowpass1 = 0;\n } else { /* 0% below on default */\n gfc.lowpass1 = (1 - 0.00) * 2. * gfp.lowpassfreq;\n }\n gfc.lowpass1 /= gfp.out_samplerate;\n gfc.lowpass2 /= gfp.out_samplerate;\n } else {\n gfc.lowpass1 = 0;\n gfc.lowpass2 = 0;\n }\n\n /**********************************************************************/\n /* compute info needed for polyphase filter (filter type==0, default) */\n /**********************************************************************/\n lame_init_params_ppflt(gfp);\n /*******************************************************\n * samplerate and bitrate index\n *******************************************************/\n gfc.samplerate_index = SmpFrqIndex(gfp.out_samplerate, gfp);\n if (gfc.samplerate_index < 0) {\n gfp.internal_flags = null;\n return -1;\n }\n\n if (gfp.VBR == VbrMode.vbr_off) {\n if (gfp.free_format) {\n gfc.bitrate_index = 0;\n } else {\n gfp.brate = FindNearestBitrate(gfp.brate, gfp.version,\n gfp.out_samplerate);\n gfc.bitrate_index = BitrateIndex(gfp.brate, gfp.version,\n gfp.out_samplerate);\n if (gfc.bitrate_index <= 0) {\n gfp.internal_flags = null;\n return -1;\n }\n }\n } else {\n gfc.bitrate_index = 1;\n }\n\n /* for CBR, we will write an \"info\" tag. */\n\n if (gfp.analysis)\n gfp.bWriteVbrTag = false;\n\n /* some file options not allowed if output is: not specified or stdout */\n if (gfc.pinfo != null)\n gfp.bWriteVbrTag = false;\n /* disable Xing VBR tag */\n\n bs.init_bit_stream_w(gfc);\n\n var j = gfc.samplerate_index + (3 * gfp.version) + 6\n * (gfp.out_samplerate < 16000 ? 1 : 0);\n for (var i = 0; i < Encoder.SBMAX_l + 1; i++)\n gfc.scalefac_band.l[i] = qupvt.sfBandIndex[j].l[i];\n\n for (var i = 0; i < Encoder.PSFB21 + 1; i++) {\n var size = (gfc.scalefac_band.l[22] - gfc.scalefac_band.l[21])\n / Encoder.PSFB21;\n var start = gfc.scalefac_band.l[21] + i * size;\n gfc.scalefac_band.psfb21[i] = start;\n }\n gfc.scalefac_band.psfb21[Encoder.PSFB21] = 576;\n\n for (var i = 0; i < Encoder.SBMAX_s + 1; i++)\n gfc.scalefac_band.s[i] = qupvt.sfBandIndex[j].s[i];\n\n for (var i = 0; i < Encoder.PSFB12 + 1; i++) {\n var size = (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12])\n / Encoder.PSFB12;\n var start = gfc.scalefac_band.s[12] + i * size;\n gfc.scalefac_band.psfb12[i] = start;\n }\n gfc.scalefac_band.psfb12[Encoder.PSFB12] = 192;\n /* determine the mean bitrate for main data */\n if (gfp.version == 1) /* MPEG 1 */\n gfc.sideinfo_len = (gfc.channels_out == 1) ? 4 + 17 : 4 + 32;\n else\n /* MPEG 2 */\n gfc.sideinfo_len = (gfc.channels_out == 1) ? 4 + 9 : 4 + 17;\n\n if (gfp.error_protection)\n gfc.sideinfo_len += 2;\n\n lame_init_bitstream(gfp);\n\n gfc.Class_ID = LAME_ID;\n\n {\n var k;\n\n for (k = 0; k < 19; k++)\n gfc.nsPsy.pefirbuf[k] = 700 * gfc.mode_gr * gfc.channels_out;\n\n if (gfp.ATHtype == -1)\n gfp.ATHtype = 4;\n }\n\n switch (gfp.VBR) {\n\n case VbrMode.vbr_mt:\n gfp.VBR = VbrMode.vbr_mtrh;\n //$FALL-THROUGH$\n case VbrMode.vbr_mtrh:\n {\n if (gfp.useTemporal == null) {\n gfp.useTemporal = false;\n /* off by default for this VBR mode */\n }\n\n p.apply_preset(gfp, 500 - (gfp.VBR_q * 10), 0);\n /**\n * \n * The newer VBR code supports only a limited\n * subset of quality levels:\n * 9-5=5 are the same, uses x^3/4 quantization\n * 4-0=0 are the same 5 plus best huffman divide code\n *
\n */\n if (gfp.quality < 0)\n gfp.quality = LAME_DEFAULT_QUALITY;\n if (gfp.quality < 5)\n gfp.quality = 0;\n if (gfp.quality > 5)\n gfp.quality = 5;\n\n gfc.PSY.mask_adjust = gfp.maskingadjust;\n gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;\n\n /*\n * sfb21 extra only with MPEG-1 at higher sampling rates\n */\n if (gfp.experimentalY)\n gfc.sfb21_extra = false;\n else\n gfc.sfb21_extra = (gfp.out_samplerate > 44000);\n\n gfc.iteration_loop = new VBRNewIterationLoop(qu);\n break;\n\n }\n case VbrMode.vbr_rh:\n {\n\n p.apply_preset(gfp, 500 - (gfp.VBR_q * 10), 0);\n\n gfc.PSY.mask_adjust = gfp.maskingadjust;\n gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;\n\n /*\n * sfb21 extra only with MPEG-1 at higher sampling rates\n */\n if (gfp.experimentalY)\n gfc.sfb21_extra = false;\n else\n gfc.sfb21_extra = (gfp.out_samplerate > 44000);\n\n /*\n * VBR needs at least the output of GPSYCHO, so we have to garantee\n * that by setting a minimum quality level, actually level 6 does\n * it. down to level 6\n */\n if (gfp.quality > 6)\n gfp.quality = 6;\n\n if (gfp.quality < 0)\n gfp.quality = LAME_DEFAULT_QUALITY;\n\n gfc.iteration_loop = new VBROldIterationLoop(qu);\n break;\n }\n\n default: /* cbr/abr */\n {\n var vbrmode;\n\n /*\n * no sfb21 extra with CBR code\n */\n gfc.sfb21_extra = false;\n\n if (gfp.quality < 0)\n gfp.quality = LAME_DEFAULT_QUALITY;\n\n vbrmode = gfp.VBR;\n if (vbrmode == VbrMode.vbr_off)\n gfp.VBR_mean_bitrate_kbps = gfp.brate;\n /* second, set parameters depending on bitrate */\n p.apply_preset(gfp, gfp.VBR_mean_bitrate_kbps, 0);\n gfp.VBR = vbrmode;\n\n gfc.PSY.mask_adjust = gfp.maskingadjust;\n gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;\n\n if (vbrmode == VbrMode.vbr_off) {\n gfc.iteration_loop = new CBRNewIterationLoop(qu);\n } else {\n gfc.iteration_loop = new ABRIterationLoop(qu);\n }\n break;\n }\n }\n /* initialize default values common for all modes */\n\n if (gfp.VBR != VbrMode.vbr_off) { /* choose a min/max bitrate for VBR */\n /* if the user didn't specify VBR_max_bitrate: */\n gfc.VBR_min_bitrate = 1;\n /*\n * default: allow 8 kbps (MPEG-2) or 32 kbps (MPEG-1)\n */\n gfc.VBR_max_bitrate = 14;\n /*\n * default: allow 160 kbps (MPEG-2) or 320 kbps (MPEG-1)\n */\n if (gfp.out_samplerate < 16000)\n gfc.VBR_max_bitrate = 8;\n /* default: allow 64 kbps (MPEG-2.5) */\n if (gfp.VBR_min_bitrate_kbps != 0) {\n gfp.VBR_min_bitrate_kbps = FindNearestBitrate(\n gfp.VBR_min_bitrate_kbps, gfp.version,\n gfp.out_samplerate);\n gfc.VBR_min_bitrate = BitrateIndex(gfp.VBR_min_bitrate_kbps,\n gfp.version, gfp.out_samplerate);\n if (gfc.VBR_min_bitrate < 0)\n return -1;\n }\n if (gfp.VBR_max_bitrate_kbps != 0) {\n gfp.VBR_max_bitrate_kbps = FindNearestBitrate(\n gfp.VBR_max_bitrate_kbps, gfp.version,\n gfp.out_samplerate);\n gfc.VBR_max_bitrate = BitrateIndex(gfp.VBR_max_bitrate_kbps,\n gfp.version, gfp.out_samplerate);\n if (gfc.VBR_max_bitrate < 0)\n return -1;\n }\n gfp.VBR_min_bitrate_kbps = Tables.bitrate_table[gfp.version][gfc.VBR_min_bitrate];\n gfp.VBR_max_bitrate_kbps = Tables.bitrate_table[gfp.version][gfc.VBR_max_bitrate];\n gfp.VBR_mean_bitrate_kbps = Math.min(\n Tables.bitrate_table[gfp.version][gfc.VBR_max_bitrate],\n gfp.VBR_mean_bitrate_kbps);\n gfp.VBR_mean_bitrate_kbps = Math.max(\n Tables.bitrate_table[gfp.version][gfc.VBR_min_bitrate],\n gfp.VBR_mean_bitrate_kbps);\n }\n\n /* just another daily changing developer switch */\n if (gfp.tune) {\n gfc.PSY.mask_adjust += gfp.tune_value_a;\n gfc.PSY.mask_adjust_short += gfp.tune_value_a;\n }\n\n /* initialize internal qval settings */\n lame_init_qval(gfp);\n /*\n * automatic ATH adjustment on\n */\n if (gfp.athaa_type < 0)\n gfc.ATH.useAdjust = 3;\n else\n gfc.ATH.useAdjust = gfp.athaa_type;\n\n /* initialize internal adaptive ATH settings -jd */\n gfc.ATH.aaSensitivityP = Math.pow(10.0, gfp.athaa_sensitivity\n / -10.0);\n\n if (gfp.short_blocks == null) {\n gfp.short_blocks = ShortBlock.short_block_allowed;\n }\n\n /*\n * Note Jan/2003: Many hardware decoders cannot handle short blocks in\n * regular stereo mode unless they are coupled (same type in both\n * channels) it is a rare event (1 frame per min. or so) that LAME would\n * use uncoupled short blocks, so lets turn them off until we decide how\n * to handle this. No other encoders allow uncoupled short blocks, even\n * though it is in the standard.\n */\n /*\n * rh 20040217: coupling makes no sense for mono and dual-mono streams\n */\n if (gfp.short_blocks == ShortBlock.short_block_allowed\n && (gfp.mode == MPEGMode.JOINT_STEREO || gfp.mode == MPEGMode.STEREO)) {\n gfp.short_blocks = ShortBlock.short_block_coupled;\n }\n\n if (gfp.quant_comp < 0)\n gfp.quant_comp = 1;\n if (gfp.quant_comp_short < 0)\n gfp.quant_comp_short = 0;\n\n if (gfp.msfix < 0)\n gfp.msfix = 0;\n\n /* select psychoacoustic model */\n gfp.exp_nspsytune = gfp.exp_nspsytune | 1;\n\n if (gfp.internal_flags.nsPsy.attackthre < 0)\n gfp.internal_flags.nsPsy.attackthre = PsyModel.NSATTACKTHRE;\n if (gfp.internal_flags.nsPsy.attackthre_s < 0)\n gfp.internal_flags.nsPsy.attackthre_s = PsyModel.NSATTACKTHRE_S;\n\n\n if (gfp.scale < 0)\n gfp.scale = 1;\n\n if (gfp.ATHtype < 0)\n gfp.ATHtype = 4;\n\n if (gfp.ATHcurve < 0)\n gfp.ATHcurve = 4;\n\n if (gfp.athaa_loudapprox < 0)\n gfp.athaa_loudapprox = 2;\n\n if (gfp.interChRatio < 0)\n gfp.interChRatio = 0;\n\n if (gfp.useTemporal == null)\n gfp.useTemporal = true;\n /* on by default */\n\n /*\n * padding method as described in\n * \"MPEG-Layer3 / Bitstream Syntax and Decoding\" by Martin Sieler, Ralph\n * Sperschneider\n *\n * note: there is no padding for the very first frame\n *\n * Robert Hegemann 2000-06-22\n */\n gfc.slot_lag = gfc.frac_SpF = 0;\n if (gfp.VBR == VbrMode.vbr_off)\n gfc.slot_lag = gfc.frac_SpF = (((gfp.version + 1) * 72000 * gfp.brate) % gfp.out_samplerate) | 0;\n\n qupvt.iteration_init(gfp);\n psy.psymodel_init(gfp);\n return 0;\n }\n\n function update_inbuffer_size(gfc, nsamples) {\n if (gfc.in_buffer_0 == null || gfc.in_buffer_nsamples < nsamples) {\n gfc.in_buffer_0 = new_float(nsamples);\n gfc.in_buffer_1 = new_float(nsamples);\n gfc.in_buffer_nsamples = nsamples;\n }\n }\n\n this.lame_encode_flush = function (gfp, mp3buffer, mp3bufferPos, mp3buffer_size) {\n var gfc = gfp.internal_flags;\n var buffer = new_short_n([2, 1152]);\n var imp3 = 0, mp3count, mp3buffer_size_remaining;\n\n /*\n * we always add POSTDELAY=288 padding to make sure granule with real\n * data can be complety decoded (because of 50% overlap with next\n * granule\n */\n var end_padding;\n var frames_left;\n var samples_to_encode = gfc.mf_samples_to_encode - Encoder.POSTDELAY;\n var mf_needed = calcNeeded(gfp);\n\n /* Was flush already called? */\n if (gfc.mf_samples_to_encode < 1) {\n return 0;\n }\n mp3count = 0;\n\n if (gfp.in_samplerate != gfp.out_samplerate) {\n /*\n * delay due to resampling; needs to be fixed, if resampling code\n * gets changed\n */\n samples_to_encode += 16. * gfp.out_samplerate / gfp.in_samplerate;\n }\n end_padding = gfp.framesize - (samples_to_encode % gfp.framesize);\n if (end_padding < 576)\n end_padding += gfp.framesize;\n gfp.encoder_padding = end_padding;\n\n frames_left = (samples_to_encode + end_padding) / gfp.framesize;\n\n /*\n * send in a frame of 0 padding until all internal sample buffers are\n * flushed\n */\n while (frames_left > 0 && imp3 >= 0) {\n var bunch = mf_needed - gfc.mf_size;\n var frame_num = gfp.frameNum;\n\n bunch *= gfp.in_samplerate;\n bunch /= gfp.out_samplerate;\n if (bunch > 1152)\n bunch = 1152;\n if (bunch < 1)\n bunch = 1;\n\n mp3buffer_size_remaining = mp3buffer_size - mp3count;\n\n /* if user specifed buffer size = 0, dont check size */\n if (mp3buffer_size == 0)\n mp3buffer_size_remaining = 0;\n\n imp3 = this.lame_encode_buffer(gfp, buffer[0], buffer[1], bunch,\n mp3buffer, mp3bufferPos, mp3buffer_size_remaining);\n\n mp3bufferPos += imp3;\n mp3count += imp3;\n frames_left -= (frame_num != gfp.frameNum) ? 1 : 0;\n }\n /*\n * Set gfc.mf_samples_to_encode to 0, so we may detect and break loops\n * calling it more than once in a row.\n */\n gfc.mf_samples_to_encode = 0;\n\n if (imp3 < 0) {\n /* some type of fatal error */\n return imp3;\n }\n\n mp3buffer_size_remaining = mp3buffer_size - mp3count;\n /* if user specifed buffer size = 0, dont check size */\n if (mp3buffer_size == 0)\n mp3buffer_size_remaining = 0;\n\n /* mp3 related stuff. bit buffer might still contain some mp3 data */\n bs.flush_bitstream(gfp);\n imp3 = bs.copy_buffer(gfc, mp3buffer, mp3bufferPos,\n mp3buffer_size_remaining, 1);\n if (imp3 < 0) {\n /* some type of fatal error */\n return imp3;\n }\n mp3bufferPos += imp3;\n mp3count += imp3;\n mp3buffer_size_remaining = mp3buffer_size - mp3count;\n /* if user specifed buffer size = 0, dont check size */\n if (mp3buffer_size == 0)\n mp3buffer_size_remaining = 0;\n\n if (gfp.write_id3tag_automatic) {\n /* write a id3 tag to the bitstream */\n id3.id3tag_write_v1(gfp);\n\n imp3 = bs.copy_buffer(gfc, mp3buffer, mp3bufferPos,\n mp3buffer_size_remaining, 0);\n\n if (imp3 < 0) {\n return imp3;\n }\n mp3count += imp3;\n }\n return mp3count;\n };\n\n this.lame_encode_buffer = function (gfp, buffer_l, buffer_r, nsamples, mp3buf, mp3bufPos, mp3buf_size) {\n var gfc = gfp.internal_flags;\n var in_buffer = [null, null];\n\n if (gfc.Class_ID != LAME_ID)\n return -3;\n\n if (nsamples == 0)\n return 0;\n\n update_inbuffer_size(gfc, nsamples);\n\n in_buffer[0] = gfc.in_buffer_0;\n in_buffer[1] = gfc.in_buffer_1;\n\n /* make a copy of input buffer, changing type to sample_t */\n for (var i = 0; i < nsamples; i++) {\n in_buffer[0][i] = buffer_l[i];\n if (gfc.channels_in > 1)\n in_buffer[1][i] = buffer_r[i];\n }\n\n return lame_encode_buffer_sample(gfp, in_buffer[0], in_buffer[1],\n nsamples, mp3buf, mp3bufPos, mp3buf_size);\n }\n\n function calcNeeded(gfp) {\n var mf_needed = Encoder.BLKSIZE + gfp.framesize - Encoder.FFTOFFSET;\n /*\n * amount needed for FFT\n */\n mf_needed = Math.max(mf_needed, 512 + gfp.framesize - 32);\n\n return mf_needed;\n }\n\n function lame_encode_buffer_sample(gfp, buffer_l, buffer_r, nsamples, mp3buf, mp3bufPos, mp3buf_size) {\n var gfc = gfp.internal_flags;\n var mp3size = 0, ret, i, ch, mf_needed;\n var mp3out;\n var mfbuf = [null, null];\n var in_buffer = [null, null];\n\n if (gfc.Class_ID != LAME_ID)\n return -3;\n\n if (nsamples == 0)\n return 0;\n\n /* copy out any tags that may have been written into bitstream */\n mp3out = bs.copy_buffer(gfc, mp3buf, mp3bufPos, mp3buf_size, 0);\n if (mp3out < 0)\n return mp3out;\n /* not enough buffer space */\n mp3bufPos += mp3out;\n mp3size += mp3out;\n\n in_buffer[0] = buffer_l;\n in_buffer[1] = buffer_r;\n\n /* Apply user defined re-scaling */\n\n /* user selected scaling of the samples */\n if (BitStream.NEQ(gfp.scale, 0) && BitStream.NEQ(gfp.scale, 1.0)) {\n for (i = 0; i < nsamples; ++i) {\n in_buffer[0][i] *= gfp.scale;\n if (gfc.channels_out == 2)\n in_buffer[1][i] *= gfp.scale;\n }\n }\n\n /* user selected scaling of the channel 0 (left) samples */\n if (BitStream.NEQ(gfp.scale_left, 0)\n && BitStream.NEQ(gfp.scale_left, 1.0)) {\n for (i = 0; i < nsamples; ++i) {\n in_buffer[0][i] *= gfp.scale_left;\n }\n }\n\n /* user selected scaling of the channel 1 (right) samples */\n if (BitStream.NEQ(gfp.scale_right, 0)\n && BitStream.NEQ(gfp.scale_right, 1.0)) {\n for (i = 0; i < nsamples; ++i) {\n in_buffer[1][i] *= gfp.scale_right;\n }\n }\n\n /* Downsample to Mono if 2 channels in and 1 channel out */\n if (gfp.num_channels == 2 && gfc.channels_out == 1) {\n for (i = 0; i < nsamples; ++i) {\n in_buffer[0][i] = 0.5 * ( in_buffer[0][i] + in_buffer[1][i]);\n in_buffer[1][i] = 0.0;\n }\n }\n\n mf_needed = calcNeeded(gfp);\n\n mfbuf[0] = gfc.mfbuf[0];\n mfbuf[1] = gfc.mfbuf[1];\n\n var in_bufferPos = 0;\n while (nsamples > 0) {\n var in_buffer_ptr = [null, null];\n var n_in = 0;\n /* number of input samples processed with fill_buffer */\n var n_out = 0;\n /* number of samples output with fill_buffer */\n /* n_in <> n_out if we are resampling */\n\n in_buffer_ptr[0] = in_buffer[0];\n in_buffer_ptr[1] = in_buffer[1];\n /* copy in new samples into mfbuf, with resampling */\n var inOut = new InOut();\n fill_buffer(gfp, mfbuf, in_buffer_ptr, in_bufferPos, nsamples,\n inOut);\n n_in = inOut.n_in;\n n_out = inOut.n_out;\n\n /* compute ReplayGain of resampled input if requested */\n if (gfc.findReplayGain && !gfc.decode_on_the_fly)\n if (ga.AnalyzeSamples(gfc.rgdata, mfbuf[0], gfc.mf_size,\n mfbuf[1], gfc.mf_size, n_out, gfc.channels_out) == GainAnalysis.GAIN_ANALYSIS_ERROR)\n return -6;\n\n /* update in_buffer counters */\n nsamples -= n_in;\n in_bufferPos += n_in;\n if (gfc.channels_out == 2)\n ;// in_bufferPos += n_in;\n\n /* update mfbuf[] counters */\n gfc.mf_size += n_out;\n\n /*\n * lame_encode_flush may have set gfc.mf_sample_to_encode to 0 so we\n * have to reinitialize it here when that happened.\n */\n if (gfc.mf_samples_to_encode < 1) {\n gfc.mf_samples_to_encode = Encoder.ENCDELAY + Encoder.POSTDELAY;\n }\n gfc.mf_samples_to_encode += n_out;\n\n if (gfc.mf_size >= mf_needed) {\n /* encode the frame. */\n /* mp3buf = pointer to current location in buffer */\n /* mp3buf_size = size of original mp3 output buffer */\n /* = 0 if we should not worry about the */\n /* buffer size because calling program is */\n /* to lazy to compute it */\n /* mp3size = size of data written to buffer so far */\n /* mp3buf_size-mp3size = amount of space avalable */\n\n var buf_size = mp3buf_size - mp3size;\n if (mp3buf_size == 0)\n buf_size = 0;\n\n ret = lame_encode_frame(gfp, mfbuf[0], mfbuf[1], mp3buf,\n mp3bufPos, buf_size);\n\n if (ret < 0)\n return ret;\n mp3bufPos += ret;\n mp3size += ret;\n\n /* shift out old samples */\n gfc.mf_size -= gfp.framesize;\n gfc.mf_samples_to_encode -= gfp.framesize;\n for (ch = 0; ch < gfc.channels_out; ch++)\n for (i = 0; i < gfc.mf_size; i++)\n mfbuf[ch][i] = mfbuf[ch][i + gfp.framesize];\n }\n }\n\n return mp3size;\n }\n\n function lame_encode_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3bufPos, mp3buf_size) {\n var ret = self.enc.lame_encode_mp3_frame(gfp, inbuf_l, inbuf_r, mp3buf,\n mp3bufPos, mp3buf_size);\n gfp.frameNum++;\n return ret;\n }\n\n function InOut() {\n this.n_in = 0;\n this.n_out = 0;\n }\n\n\n function NumUsed() {\n this.num_used = 0;\n }\n\n /**\n * Greatest common divisor.\n *