forked from xiangyuecn/Recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.js
15884 lines (14054 loc) · 526 KB
/
recorder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
/*
录音
*/
(function(window){
//兼容环境
window.RecorderLM="2018-09-19 10:50";
var NOOP=function(){};
var $={
extend:function(a,b){
a||(a={});
b||(b={});
for(var k in b){
a[k]=b[k];
}
return a;
}
};
//end兼容环境
function Recorder(set){
return new RecorderFn(set);
};
Recorder.IsOpen=function(){
var stream=Recorder.Stream;
if(stream){
var tracks=stream.getTracks();
if(tracks.length>0){
return tracks[0].readyState=="live";
};
};
return false;
};
function RecorderFn(set){
this.set=$.extend({
type:"mp3" //输出类型:mp3,wav,wav输出文件尺寸超大不推荐使用,但mp3编码支持会导致js文件超大,如果不需支持mp3可以使js文件大幅减小
,bitRate:16 //比特率 wav:16或8位,MP3:8比特1k/s,16比特2k/s 比较划得来
,sampleRate:16000 //采样率,wav专用;wav格式大小=sampleRate*时间;mp3此项无效,对文件大小无影响,直接使用源采样率
//采样率参考https://www.cnblogs.com/devin87/p/mp3-recorder.html
,bufferSize:8192 //AudioContext缓冲大小,相对于ctx.sampleRate=48000/秒:
//取值256, 512, 1024, 2048, 4096, 8192, or 16384,会影响onProcess调用速度
,onProcess:NOOP //fn(this.buffer,powerLevel,bufferDuration) buffer=[缓冲数据,...],powerLevel:当前缓冲的音量级别0-100,bufferDuration:已缓冲时长
},set);
};
RecorderFn.prototype={
//打开录音资源True(),False(msg),需要调用close。注意:此方法是异步的;一般使用时打开,用完立即关闭;可重复调用,可用来测试是否能录音
open:function(True,False){
True=True||NOOP;
False=False||NOOP;
if(Recorder.IsOpen()){
True();
return;
};
var notSupport="此浏览器不支持录音";
var AC=window.AudioContext;
if(!AC){
AC=window.webkitAudioContext;
};
if(!AC){
False(notSupport);
return;
};
var scope=navigator.mediaDevices||{};
if(!scope.getUserMedia){
scope=navigator;
scope.getUserMedia||(scope.getUserMedia=scope.webkitGetUserMedia||scope.mozGetUserMedia||scope.msGetUserMedia);
};
if(!scope.getUserMedia){
False(notSupport);
return;
};
Recorder.Ctx=Recorder.Ctx||new AC();//不能反复构造,number of hardware contexts reached maximum (6)
var f1=function(stream){
Recorder.Stream=stream;
True();
};
var f2=function(e){
var code=e.code||e.name||"";
console.error(e);
False(/Permission/i.test(code)?"用户拒绝了录音权限":"无法录音:"+code);
};
var pro=scope.getUserMedia({audio:true},f1,f2);
if(pro&&pro.then){
pro.then(f1).catch(f2);
};
}
//关闭释放录音资源
,close:function(call){
call=call||NOOP;
var This=this;
This._stop();
var stream=Recorder.Stream;
if(stream){
var tracks=stream.getTracks();
for(var i=0;i<tracks.length;i++){
tracks[i].stop();
};
};
Recorder.Stream=0;
call();
}
//开始录音,需先调用open;不支持、错误,不会有任何提示,stop时自然能得到错误
,start:function(){
console.log("["+Date.now()+"]Start");
var This=this,set=This.set;
var buffer=This.buffer=[];//数据缓冲
This.recSize=0;//数据大小
This._stop();
This.state=0;
if(!Recorder.IsOpen()){
return;
};
var ctx=Recorder.Ctx;
var media=This.media=ctx.createMediaStreamSource(Recorder.Stream);
var process=This.process=(ctx.createScriptProcessor||ctx.createJavaScriptNode).call(ctx,set.bufferSize,1,1);//单声道,省的数据处理复杂
var onInt;
process.onaudioprocess=function(e){
if(This.state!=1){
return;
};
var o=e.inputBuffer.getChannelData(0);//块是共享的,必须复制出来
var size=o.length;
This.recSize+=size;
var res=new Int16Array(size);
var power=0;
for(var j=0;j<size;j++){//floatTo16BitPCM
//var s=Math.max(-1,Math.min(1,o[j]*8));//PCM 音量直接放大8倍,失真还能接受
var s=Math.max(-1,Math.min(1,o[j]));
s=s<0?s*0x8000:s*0x7FFF;
res[j]=s;
power+=Math.abs(s);
};
buffer.push(res);
power/=size;
var powerLevel=0;
if(power>0){
//https://blog.csdn.net/jody1989/article/details/73480259
powerLevel=Math.round(Math.max(0,(20*Math.log10(power/0x7fff)+34)*100/34));
};
var duration=Math.round(This.recSize/Recorder.Ctx.sampleRate*1000);
clearTimeout(onInt);
onInt=setTimeout(function(){
set.onProcess(buffer,powerLevel,duration);
});
};
media.connect(process);
process.connect(ctx.destination);
This.state=1;
}
,_stop:function(){
var This=this;
if(This.state){
This.state=0;
This.media.disconnect();
This.process.disconnect();
};
}
/*暂停录音*/
,pause:function(_resume){
var This=this;
if(This.state){
This.state=_resume||2;
};
}
/*恢复录音*/
,resume:function(){
this.pause(1);
}
/*
结束录音并返回录音数据blob对象
True(blob,duration) blob:录音数据audio/mp3|wav格式
duration:录音时长,单位毫秒
False(msg)
*/
,stop:function(True,False){
console.log("["+Date.now()+"]Stop");
True=True||NOOP;
False=False||NOOP;
var This=this,set=This.set;
if(!This.state){
False("未开始录音");
return;
};
This._stop();
var size=This.recSize;
if(!size){
False("未采集到录音");
return;
};
//准备数据
var res=new Int16Array(size);
var offset=0;
for (var i=0;i<This.buffer.length;i++) {
var o=This.buffer[i];
res.set(o,offset);
offset+=o.length;
}
var duration=Math.round(size/Recorder.Ctx.sampleRate*1000);
setTimeout(function(){
var t1=Date.now();
This[set.type](res,function(blob){
console.log("["+Date.now()+"]End",blob,duration,"编码耗时:"+(Date.now()-t1));
True(blob,duration);
});
});
}
,wav:function(res,call){
var This=this,set=This.set
,size=This.recSize
,sampleRate=set.sampleRate
,ctxSampleRate=Recorder.Ctx.sampleRate
,bitRate=set.bitRate==8?8:16;
//采样 https://www.cnblogs.com/blqw/p/3782420.html
var compression=Math.round(ctxSampleRate/sampleRate);
if(compression>1){//新采样高于录音采样不处理,省去了插值处理,直接抽样
size=Math.floor(size/compression);
var compRes=new Int16Array(size);
var i=0,j=0;
while(i<size){
compRes[i]=res[j];
j+=compression;
i++;
};
res=compRes;
}else{
sampleRate=ctxSampleRate;
};
//编码数据 https://github.com/mattdiamond/Recorderjs https://www.cnblogs.com/blqw/p/3782420.html https://www.cnblogs.com/xiaoqi/p/6993912.html
var dataLength=size*(bitRate/8);
var buffer=new ArrayBuffer(44+dataLength);
var data=new DataView(buffer);
var offset=0;
var writeString=function(str){
for (var i=0;i<str.length;i++,offset++) {
data.setUint8(offset,str.charCodeAt(i));
};
};
var write16=function(v){
data.setUint16(offset,v,true);
offset+=2;
};
var write32=function(v){
data.setUint32(offset,v,true);
offset+=4;
};
/* RIFF identifier */
writeString('RIFF');
/* RIFF chunk length */
write32(36+dataLength);
/* RIFF type */
writeString('WAVE');
/* format chunk identifier */
writeString('fmt ');
/* format chunk length */
write32(16);
/* sample format (raw) */
write16(1);
/* channel count */
write16(1);
/* sample rate */
write32(sampleRate);
/* byte rate (sample rate * block align) */
write32(sampleRate*(bitRate/8));
/* block align (channel count * bytes per sample) */
write16(bitRate/8);
/* bits per sample */
write16(bitRate);
/* data chunk identifier */
writeString('data');
/* data chunk length */
write32(dataLength);
// 写入采样数据
if(bitRate==8) {
for(var i=0;i<size;i++,offset++) {
var val=res[i];
val=parseInt(255/(65535/(val+32768)));
data.setInt8(offset,val,true);
};
}else{
for (var i=0;i<size;i++,offset+=2){
data.setInt16(offset,res[i],true);
};
};
call(new Blob([data],{type:"audio/wav"}));
}
,mp3:function(res,call){
var This=this,set=This.set,size=res.length;
//https://github.com/wangpengfei15975/recorder.js
//https://github.com/zhuker/lamejs bug:采样率必须和源一致,不然8k时没有声音,有问题fix:https://github.com/zhuker/lamejs/pull/11
var mp3=new lamejs.Mp3Encoder(1,Recorder.Ctx.sampleRate,set.bitRate);
var blockSize=5760;
var data=[];
var idx=0;
var run=function(){
if(idx<size){
var buf=mp3.encodeBuffer(res.subarray(idx,idx+blockSize));
if(buf.length>0){
data.push(buf);
};
idx+=blockSize;
setTimeout(run);//Worker? 复杂了
}else{
var buf=mp3.flush();
if(buf.length>0){
data.push(buf);
};
call(new Blob(data,{type:"audio/mp3"}));
};
};
run();
}
};
window.Recorder=Recorder;
/*
mp3编码依赖lamejs,如果无需mp3支持直接移除此代码
https://github.com/zhuker/lamejs/blob/bfb7f6c6d7877e0fe1ad9e72697a871676119a0e/lame.all.js
*/
function lamejs() {
function new_byte(count) {
return new Int8Array(count);
}
function new_short(count) {
return new Int16Array(count);
}
function new_int(count) {
return new Int32Array(count);
}
function new_float(count) {
return new Float32Array(count);
}
function new_double(count) {
return new Float64Array(count);
}
function new_float_n(args) {
if (args.length == 1) {
return new_float(args[0]);
}
var sz = args[0];
args = args.slice(1);
var A = [];
for (var i = 0; i < sz; i++) {
A.push(new_float_n(args));
}
return A;
}
function new_int_n(args) {
if (args.length == 1) {
return new_int(args[0]);
}
var sz = args[0];
args = args.slice(1);
var A = [];
for (var i = 0; i < sz; i++) {
A.push(new_int_n(args));
}
return A;
}
function new_short_n(args) {
if (args.length == 1) {
return new_short(args[0]);
}
var sz = args[0];
args = args.slice(1);
var A = [];
for (var i = 0; i < sz; i++) {
A.push(new_short_n(args));
}
return A;
}
function new_array_n(args) {
if (args.length == 1) {
return new Array(args[0]);
}
var sz = args[0];
args = args.slice(1);
var A = [];
for (var i = 0; i < sz; i++) {
A.push(new_array_n(args));
}
return A;
}
var Arrays = {};
Arrays.fill = function (a, fromIndex, toIndex, val) {
if (arguments.length == 2) {
for (var i = 0; i < a.length; i++) {
a[i] = arguments[1];
}
} else {
for (var i = fromIndex; i < toIndex; i++) {
a[i] = val;
}
}
};
var System = {};
System.arraycopy = function (src, srcPos, dest, destPos, length) {
var srcEnd = srcPos + length;
while (srcPos < srcEnd)
dest[destPos++] = src[srcPos++];
};
var Util = {};
Util.SQRT2 = 1.41421356237309504880;
Util.FAST_LOG10 = function (x) {
return Math.log10(x);
};
Util.FAST_LOG10_X = function (x, y) {
return Math.log10(x) * y;
};
function ShortBlock(ordinal) {
this.ordinal = ordinal;
}
/**
* LAME may use them, even different block types for L/R.
*/
ShortBlock.short_block_allowed = new ShortBlock(0);
/**
* LAME may use them, but always same block types in L/R.
*/
ShortBlock.short_block_coupled = new ShortBlock(1);
/**
* LAME will not use short blocks, long blocks only.
*/
ShortBlock.short_block_dispensed = new ShortBlock(2);
/**
* LAME will not use long blocks, short blocks only.
*/
ShortBlock.short_block_forced = new ShortBlock(3);
var Float = {};
Float.MAX_VALUE = 3.4028235e+38;
function VbrMode(ordinal) {
this.ordinal = ordinal;
}
VbrMode.vbr_off = new VbrMode(0);
VbrMode.vbr_mt = new VbrMode(1);
VbrMode.vbr_rh = new VbrMode(2);
VbrMode.vbr_abr = new VbrMode(3);
VbrMode.vbr_mtrh = new VbrMode(4);
VbrMode.vbr_default = VbrMode.vbr_mtrh;
var assert = function (x) {
//console.assert(x);
};
var module_exports = {
"System": System,
"VbrMode": VbrMode,
"Float": Float,
"ShortBlock": ShortBlock,
"Util": Util,
"Arrays": Arrays,
"new_array_n": new_array_n,
"new_byte": new_byte,
"new_double": new_double,
"new_float": new_float,
"new_float_n": new_float_n,
"new_int": new_int,
"new_int_n": new_int_n,
"new_short": new_short,
"new_short_n": new_short_n,
"assert": assert
};
//package mp3;
/* MPEG modes */
function MPEGMode(ordinal) {
var _ordinal = ordinal;
this.ordinal = function () {
return _ordinal;
}
}
MPEGMode.STEREO = new MPEGMode(0);
MPEGMode.JOINT_STEREO = new MPEGMode(1);
MPEGMode.DUAL_CHANNEL = new MPEGMode(2);
MPEGMode.MONO = new MPEGMode(3);
MPEGMode.NOT_SET = new MPEGMode(4);
function Version() {
/**
* URL for the LAME website.
*/
var LAME_URL = "http://www.mp3dev.org/";
/**
* Major version number.
*/
var LAME_MAJOR_VERSION = 3;
/**
* Minor version number.
*/
var LAME_MINOR_VERSION = 98;
/**
* Patch level.
*/
var LAME_PATCH_VERSION = 4;
/**
* Major version number.
*/
var PSY_MAJOR_VERSION = 0;
/**
* Minor version number.
*/
var PSY_MINOR_VERSION = 93;
/**
* A string which describes the version of LAME.
*
* @return string which describes the version of LAME
*/
this.getLameVersion = function () {
// primary to write screen reports
return (LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "." + LAME_PATCH_VERSION);
}
/**
* The short version of the LAME version string.
*
* @return short version of the LAME version string
*/
this.getLameShortVersion = function () {
// Adding date and time to version string makes it harder for output
// validation
return (LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "." + LAME_PATCH_VERSION);
}
/**
* The shortest version of the LAME version string.
*
* @return shortest version of the LAME version string
*/
this.getLameVeryShortVersion = function () {
// Adding date and time to version string makes it harder for output
return ("LAME" + LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "r");
}
/**
* String which describes the version of GPSYCHO
*
* @return string which describes the version of GPSYCHO
*/
this.getPsyVersion = function () {
return (PSY_MAJOR_VERSION + "." + PSY_MINOR_VERSION);
}
/**
* String which is a URL for the LAME website.
*
* @return string which is a URL for the LAME website
*/
this.getLameUrl = function () {
return LAME_URL;
}
/**
* Quite useless for a java version, however we are compatible ;-)
*
* @return "32bits"
*/
this.getLameOsBitness = function () {
return "32bits";
}
}
/*
* MP3 huffman table selecting and bit counting
*
* Copyright (c) 1999-2005 Takehiro TOMINAGA
* Copyright (c) 2002-2005 Gabriel Bouvigne
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* $Id: Takehiro.java,v 1.26 2011/05/24 20:48:06 kenchis Exp $ */
//package mp3;
//import java.util.Arrays;
function Takehiro() {
var qupvt = null;
this.qupvt = null;
this.setModules = function (_qupvt) {
this.qupvt = _qupvt;
qupvt = _qupvt;
}
function Bits(b) {
this.bits = 0 | b;
}
var subdv_table = [[0, 0], /* 0 bands */
[0, 0], /* 1 bands */
[0, 0], /* 2 bands */
[0, 0], /* 3 bands */
[0, 0], /* 4 bands */
[0, 1], /* 5 bands */
[1, 1], /* 6 bands */
[1, 1], /* 7 bands */
[1, 2], /* 8 bands */
[2, 2], /* 9 bands */
[2, 3], /* 10 bands */
[2, 3], /* 11 bands */
[3, 4], /* 12 bands */
[3, 4], /* 13 bands */
[3, 4], /* 14 bands */
[4, 5], /* 15 bands */
[4, 5], /* 16 bands */
[4, 6], /* 17 bands */
[5, 6], /* 18 bands */
[5, 6], /* 19 bands */
[5, 7], /* 20 bands */
[6, 7], /* 21 bands */
[6, 7], /* 22 bands */
];
/**
* nonlinear quantization of xr More accurate formula than the ISO formula.
* Takes into account the fact that we are quantizing xr . ix, but we want
* ix^4/3 to be as close as possible to x^4/3. (taking the nearest int would
* mean ix is as close as possible to xr, which is different.)
*
* From Segher Boessenkool <[email protected]> 11/1999
*
* 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro Tominaga.
* If you need the ASM code, check CVS circa Aug 2000.
*
* 01/2004: Optimizations by Gabriel Bouvigne
*/
function quantize_lines_xrpow_01(l, istep, xr, xrPos, ix, ixPos) {
var compareval0 = (1.0 - 0.4054) / istep;
l = l >> 1;
while ((l--) != 0) {
ix[ixPos++] = (compareval0 > xr[xrPos++]) ? 0 : 1;
ix[ixPos++] = (compareval0 > xr[xrPos++]) ? 0 : 1;
}
}
/**
* XRPOW_FTOI is a macro to convert floats to ints.<BR>
* if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]<BR>
* ROUNDFAC= -0.0946<BR>
*
* if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]<BR>
* ROUNDFAC=0.4054<BR>
*
* Note: using floor() or 0| is extremely slow. On machines where the
* TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile to write
* some ASM for XRPOW_FTOI().
*/
function quantize_lines_xrpow(l, istep, xr, xrPos, ix, ixPos) {
l = l >> 1;
var remaining = l % 2;
l = l >> 1;
while (l-- != 0) {
var x0, x1, x2, x3;
var rx0, rx1, rx2, rx3;
x0 = xr[xrPos++] * istep;
x1 = xr[xrPos++] * istep;
rx0 = 0 | x0;
x2 = xr[xrPos++] * istep;
rx1 = 0 | x1;
x3 = xr[xrPos++] * istep;
rx2 = 0 | x2;
x0 += qupvt.adj43[rx0];
rx3 = 0 | x3;
x1 += qupvt.adj43[rx1];
ix[ixPos++] = 0 | x0;
x2 += qupvt.adj43[rx2];
ix[ixPos++] = 0 | x1;
x3 += qupvt.adj43[rx3];
ix[ixPos++] = 0 | x2;
ix[ixPos++] = 0 | x3;
}
if (remaining != 0) {
var x0, x1;
var rx0, rx1;
x0 = xr[xrPos++] * istep;
x1 = xr[xrPos++] * istep;
rx0 = 0 | x0;
rx1 = 0 | x1;
x0 += qupvt.adj43[rx0];
x1 += qupvt.adj43[rx1];
ix[ixPos++] = 0 | x0;
ix[ixPos++] = 0 | x1;
}
}
/**
* Quantization function This function will select which lines to quantize
* and call the proper quantization function
*/
function quantize_xrpow(xp, pi, istep, codInfo, prevNoise) {
/* quantize on xr^(3/4) instead of xr */
var sfb;
var sfbmax;
var j = 0;
var prev_data_use;
var accumulate = 0;
var accumulate01 = 0;
var xpPos = 0;
var iData = pi;
var iDataPos = 0;
var acc_iData = iData;
var acc_iDataPos = 0;
var acc_xp = xp;
var acc_xpPos = 0;
/*
* Reusing previously computed data does not seems to work if global
* gain is changed. Finding why it behaves this way would allow to use a
* cache of previously computed values (let's 10 cached values per sfb)
* that would probably provide a noticeable speedup
*/
prev_data_use = (prevNoise != null && (codInfo.global_gain == prevNoise.global_gain));
if (codInfo.block_type == Encoder.SHORT_TYPE)
sfbmax = 38;
else
sfbmax = 21;
for (sfb = 0; sfb <= sfbmax; sfb++) {
var step = -1;
if (prev_data_use || codInfo.block_type == Encoder.NORM_TYPE) {
step = codInfo.global_gain
- ((codInfo.scalefac[sfb] + (codInfo.preflag != 0 ? qupvt.pretab[sfb]
: 0)) << (codInfo.scalefac_scale + 1))
- codInfo.subblock_gain[codInfo.window[sfb]] * 8;
}
if (prev_data_use && (prevNoise.step[sfb] == step)) {
/*
* do not recompute this part, but compute accumulated lines
*/
if (accumulate != 0) {
quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,
acc_iData, acc_iDataPos);
accumulate = 0;
}
if (accumulate01 != 0) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate01 = 0;
}
} else { /* should compute this part */
var l = codInfo.width[sfb];
if ((j + codInfo.width[sfb]) > codInfo.max_nonzero_coeff) {
/* do not compute upper zero part */
var usefullsize;
usefullsize = codInfo.max_nonzero_coeff - j + 1;
Arrays.fill(pi, codInfo.max_nonzero_coeff, 576, 0);
l = usefullsize;
if (l < 0) {
l = 0;
}
/* no need to compute higher sfb values */
sfb = sfbmax + 1;
}
/* accumulate lines to quantize */
if (0 == accumulate && 0 == accumulate01) {
acc_iData = iData;
acc_iDataPos = iDataPos;
acc_xp = xp;
acc_xpPos = xpPos;
}
if (prevNoise != null && prevNoise.sfb_count1 > 0
&& sfb >= prevNoise.sfb_count1
&& prevNoise.step[sfb] > 0
&& step >= prevNoise.step[sfb]) {
if (accumulate != 0) {
quantize_lines_xrpow(accumulate, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate = 0;
acc_iData = iData;
acc_iDataPos = iDataPos;
acc_xp = xp;
acc_xpPos = xpPos;
}
accumulate01 += l;
} else {
if (accumulate01 != 0) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate01 = 0;
acc_iData = iData;
acc_iDataPos = iDataPos;
acc_xp = xp;
acc_xpPos = xpPos;
}
accumulate += l;
}
if (l <= 0) {
/*
* rh: 20040215 may happen due to "prev_data_use"
* optimization
*/
if (accumulate01 != 0) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate01 = 0;
}
if (accumulate != 0) {
quantize_lines_xrpow(accumulate, istep, acc_xp,
acc_xpPos, acc_iData, acc_iDataPos);
accumulate = 0;
}
break;
/* ends for-loop */
}
}
if (sfb <= sfbmax) {
iDataPos += codInfo.width[sfb];
xpPos += codInfo.width[sfb];
j += codInfo.width[sfb];
}
}
if (accumulate != 0) { /* last data part */
quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,
acc_iData, acc_iDataPos);
accumulate = 0;
}
if (accumulate01 != 0) { /* last data part */
quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_xpPos,
acc_iData, acc_iDataPos);
accumulate01 = 0;
}
}
/**
* ix_max
*/
function ix_max(ix, ixPos, endPos) {
var max1 = 0, max2 = 0;
do {
var x1 = ix[ixPos++];
var x2 = ix[ixPos++];
if (max1 < x1)
max1 = x1;
if (max2 < x2)
max2 = x2;
} while (ixPos < endPos);
if (max1 < max2)
max1 = max2;
return max1;
}
function count_bit_ESC(ix, ixPos, end, t1, t2, s) {
/* ESC-table is used */
var linbits = Tables.ht[t1].xlen * 65536 + Tables.ht[t2].xlen;
var sum = 0, sum2;
do {
var x = ix[ixPos++];
var y = ix[ixPos++];
if (x != 0) {
if (x > 14) {
x = 15;
sum += linbits;
}
x *= 16;
}
if (y != 0) {
if (y > 14) {
y = 15;
sum += linbits;
}
x += y;
}
sum += Tables.largetbl[x];
} while (ixPos < end);
sum2 = sum & 0xffff;
sum >>= 16;
if (sum > sum2) {
sum = sum2;
t1 = t2;
}
s.bits += sum;
return t1;
}
function count_bit_noESC(ix, ixPos, end, s) {
/* No ESC-words */
var sum1 = 0;
var hlen1 = Tables.ht[1].hlen;
do {
var x = ix[ixPos + 0] * 2 + ix[ixPos + 1];
ixPos += 2;
sum1 += hlen1[x];
} while (ixPos < end);
s.bits += sum1;
return 1;
}
function count_bit_noESC_from2(ix, ixPos, end, t1, s) {
/* No ESC-words */
var sum = 0, sum2;
var xlen = Tables.ht[t1].xlen;
var hlen;
if (t1 == 2)
hlen = Tables.table23;
else