Skip to content

Commit

Permalink
修复mp3-engine.js在1.3中因代码精简产生的bug:录制mp3文件达到100MB以上的处理代码被精简掉了,还原代码修复
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyuecn committed Dec 24, 2023
1 parent 09e5fb6 commit 4747184
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 36 deletions.
9 changes: 9 additions & 0 deletions assets/node-codes/-global-.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ global.FormatMs=function(ms){
+("00"+ss).substr(-3);
return t;
};
global.FormatSize=function(num){
var size=+num||0,s="B";
if(size>1023){ s="KB"; size/=1024; }
if(size>1023){ s="MB"; size/=1024; }
if(size>1023){ s="GB"; size/=1024; }
var txt=+size.toFixed(1)+" "+s;
if(size>9)txt=Math.round(size)+" "+s;
return txt;
};
global.Sleep=function(ms){ return new Promise(function(resolve,reject){
setTimeout(resolve,ms);
}) };
Expand Down
91 changes: 84 additions & 7 deletions assets/node-codes/test-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
作者:高坚果
时间:2023-09-25 20:51:06
运行: node test-engine.js [type=mp3] [br=16] [dist] [full][fast][save] [loop]
运行: node test-engine.js [type=mp3] [br=16] [dist] [full][fast][save] [big][loop]
type=mp3|ogg|amr
br=16 只测试这个比特率
dist 使用压缩版js进行测试
full 进行完整测试,会测试时长为1小时的音频编码,测试会很慢
fast 进行快速测试,最长测试1秒的音频编码,测试会很快
big 只进行超长时间编码测试
loop 循环进行测试,不结束
oggFullFast ogg全采样率测试
save 保存一个文件,然后退出,用于检查音频内容
Expand All @@ -22,6 +23,8 @@ if(FastTest) Log("FastTest",3);
var FullTest=global.FullTest||process.argv.indexOf("full")!=-1;
if(FullTest) Log("FullTest",3);

var TestBig=process.argv.indexOf("big")!=-1;
if(TestBig) Log("TestBig",3);
var TestLoop=process.argv.indexOf("loop")!=-1;
if(TestLoop) Log("TestLoop",3);
var TestSave=process.argv.indexOf("save")!=-1;
Expand Down Expand Up @@ -213,6 +216,65 @@ var testExec=function(type,sr,br,srcPcm,dur,fast){ return new Promise(function(r
});
});
})};
var bigTestEngine=function(type,sr,br){ return new Promise(function(resolve,reject){
var dur=24*60*60000;
var Tag="bigTestEngine "+type+" "+sr+" "+br+" "+dur/60000/60+"小时";
Log("------ "+Tag+(TestLoop?" loop:"+LoopCount:"")
+" 已耗时:"+FormatMs(Date.now()-StartTime)+" ------",3);

if(TestSave){
var path=ArrayBufferSaveTempFile("test-engine-big-save-"+sr+"-"+br+"."+type,new Uint8Array(0).buffer);
Log("保存文件到:"+path,2);
}

var t1=Date.now();
var encSize=0,lastDur=0;
var rec=Recorder({
type:type,sampleRate:sr,bitRate:br
,onProcess:function(buffers,powerLevel,duration){
lastDur=duration;
for(var i=buffers.length-2;i>=0;i--){
if(!buffers[i]) break;
buffers[i]=null;//清除缓冲数据
}
}
,takeoffEncodeChunk:function(bytes){
encSize+=bytes.byteLength;
if(TestSave){
fs.writeFileSync(path, Buffer.from(bytes.buffer), { flag:"a" });
}
}
});
rec.envStart({ envName:"nodejs",canProcess:true },sampleRate);

var size=sampleRate/1000*dur;
var idx=0;
while(idx<size){
var pcm=testPcm; idx+=pcm.length; rec.envIn(pcm,0);
pcm=new Int16Array(pcm.length); var vol=Math.random();
for(var i=0;i<pcm.length;i++){
pcm[i]=vol*(0x7fff-Math.random()*0x7fff*2);
}
idx+=pcm.length; rec.envIn(pcm,0);

process.stdout.clearLine();
process.stdout.write("\r>>> "+(idx/size*100).toFixed(2)+"%"
+" "+FormatMs(lastDur)
+" "+FormatSize(encSize)
+" | 预估:"+FormatSize(encSize/idx*size)
+" 耗时:"+FormatMs(Date.now()-t1)
+" 还需:"+FormatMs(~~((Date.now()-t1)/idx*(size-idx))));
};
console.log("");

rec.stop(function(blob){
var msg="envIn编码出:"+encSize+"字节";
msg+=" 耗时:"+FormatMs(Date.now()-t1);
Log("OK "+msg,2);
resolve();
},function(msg){ throw msg });
})};



var LoopCount=0;
Expand All @@ -238,7 +300,12 @@ do{
}
LoopCount++;

if(!TestType || TestType=="mp3"){
while(!TestType || TestType=="mp3"){
if(TestBig){
if(!TestBitRate||TestBitRate<=16)await bigTestEngine("mp3",16000,16);
if(!TestBitRate||TestBitRate>16)await bigTestEngine("mp3",48000,TestBitRate||320);
okMsgs.push("test mp3 big"); break;
};
var mp3BitRate=TestBitRate?[TestBitRate]
:[8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 192, 224, 256, 320];
for(var i=0;i<mp3BitRate.length;i++){
Expand All @@ -251,9 +318,14 @@ do{
await testEngine("mp3",44100,br,1);
await testEngine("mp3",48000,br,fast);
}
okMsgs.push("test mp3");
okMsgs.push("test mp3"); break;
}
if(!TestType || TestType=="ogg"){
while(!TestType || TestType=="ogg"){
if(TestBig){
if(!TestBitRate||TestBitRate<=16)await bigTestEngine("ogg",16000,16);
if(!TestBitRate||TestBitRate>16)await bigTestEngine("ogg",48000,TestBitRate||100);
okMsgs.push("test ogg big"); break;
};
var oggBitRate=TestBitRate?[TestBitRate]
:[16, 30, 40, 50, 60, 70, 80, 90, 100];
if(OggFullFast){
Expand All @@ -277,15 +349,20 @@ do{
await testEngine("ogg",44100,br,1);
await testEngine("ogg",48000,br,fast);
}
okMsgs.push("test ogg");
okMsgs.push("test ogg"); break;
}
if(!TestType || TestType=="amr"){
while(!TestType || TestType=="amr"){
if(TestBig){
if(!TestBitRate||TestBitRate<12.2)await bigTestEngine("amr",16000,4.75);
if(!TestBitRate||TestBitRate>=12.2)await bigTestEngine("amr",48000,TestBitRate||12.2);
okMsgs.push("test amr big"); break;
};
var amrBitRate=TestBitRate?[TestBitRate]
:[4.75, 5.15, 5.9, 6.7, 7.4, 7.95, 10.2, 12.2];
for(var i=0,L=amrBitRate.length-1;i<=L;i++){
await testEngine("amr",8000,amrBitRate[i],i!=0&&i!=L);
}
okMsgs.push("test amr");
okMsgs.push("test amr"); break;
}
}while(TestLoop);

Expand Down
8 changes: 4 additions & 4 deletions assets/npm-home/hash-history.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[
{
"sha1": "4072233e2ee2223cdb63a77617f53b5e30cf4d28",
"time": "2023/12/24 18:12:15"
},
{
"sha1": "7afdb2aa559360be175b3587c80281cf422196ac",
"time": "2023/12/17 18:28:33"
Expand All @@ -14,9 +18,5 @@
{
"sha1": "e23c1caa0e37bb7ffdf8097f91c22400064173be",
"time": "2023/7/1 22:14:28"
},
{
"sha1": "9901f4660f25031c99f73bc33d97238df9b3febb",
"time": "2023/6/29 21:25:33"
}
]
24 changes: 12 additions & 12 deletions assets/ztest_iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@
reclog('<span style="color:red">【Uncaught Error】'+message+'<pre>'+"at:"+lineNo+":"+columnNo+" url:"+url+"\n"+(error&&error.stack||"不能获得错误堆栈")+'</pre></span>');
};

reclog("IFrame测试,location: "+location.href.replace(/./g,function(a){return "&#"+a.charCodeAt(0)+";"})+' ,如果你点击了iframe中的任何链接,需点击一下此按钮(比如清除RecordApp注入的对象):<button onclick="clearPage()">重置本页面环境</button>');
reclog('提示:`RecordApp测试`可以模拟跨域,跨域时未设置iframe相应策略H5录音权限永远是拒绝的(allow="camera;microphone")<button onclick="setAllow(1)">重新打开网页并设置H5策略</button> <button onclick="setAllow()">清除H5策略</button>',2);
reclog("IFrame测试,顶层页面URL: "+location.href.replace(/./g,function(a){return "&#"+a.charCodeAt(0)+";"})+'');
reclog('提示:跨域时未设置iframe相应策略H5录音权限永远是拒绝的(<span class="allowSetTips"></span>)<button onclick="setAllow(1)">重新打开网页并设置H5策略</button> <button onclick="setAllow()">清除H5策略</button>');
</script>

<script>
var clearPage=function(){
window.NativeRecordReceivePCM=null;//干掉RecordApp Native注入到顶层的对象
window.AppJsBridgeRequest=null;//干掉RecordApp示例配置Native注入到顶层的对象
reclog("本页面环境已重置,已尝试清除RecordApp注入到本页面的对象");
};
var viewIframe=function(url,allow){
document.querySelector(".box").innerHTML='<iframe src="'+url+'" '+(allow?'allow="camera;microphone"':'')+' class="iframe" style="width:98%;height:85vh; border:4px solid #0B1"></iframe>';
document.querySelector(".allowSetTips").innerHTML='<span style="color:'+(allow?"#0b1":"#f00")+'">iframe'+(allow?"已设置":"未设置")+' allow="camera;microphone"</span>';
};

var iframeUrl=decodeURIComponent((/[?&#]iframeUrl=((https?(:|%3A)|\/)[^&#]+)/i.exec(location.href)||[])[1]||"");
console.log("iframeUrl: "+iframeUrl);
if(!iframeUrl){
reclog('提供的iframeUrl参数无效',1);
}else{
iframeUrl=(iframeUrl[0]=="/"?"..":"")+iframeUrl;
viewIframe(iframeUrl);
if(iframeUrl[0]=="/"){
if(/gitee/.test(location.href)){
iframeUrl="https://xiangyuecn.github.io/Recorder"+iframeUrl;
}else{
iframeUrl="https://xiangyuecn.gitee.io/recorder"+iframeUrl;
}
}
viewIframe(iframeUrl,true);
}

var setAllow=function(set){
if(set){
iframeUrl=prompt("iframe地址(可以跨域)",iframeUrl)||iframeUrl;
};
clearPage();

viewIframe(iframeUrl,set);
reclog(set?'已设置iframe的allow属性为"camera;microphone"':"已清除iframe的allow属性");
};
</script>

Expand All @@ -69,7 +69,7 @@
//移动端加载控制台组件
var elem=document.createElement("script");
elem.setAttribute("type","text/javascript");
elem.setAttribute("src","https://xiangyuecn.gitee.io/recorder/assets/ztest-vconsole.js");
elem.setAttribute("src","ztest-vconsole.js");
document.body.appendChild(elem);
elem.onload=function(){
new VConsole();
Expand Down
2 changes: 1 addition & 1 deletion assets/工具-代码运行和静态分发Runtime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@
//移动端加载控制台组件
var elem=document.createElement("script");
elem.setAttribute("type","text/javascript");
elem.setAttribute("src","https://xiangyuecn.gitee.io/recorder/assets/ztest-vconsole.js");
elem.setAttribute("src","ztest-vconsole.js");
document.body.appendChild(elem);
elem.onload=function(){
new VConsole();
Expand Down
2 changes: 1 addition & 1 deletion dist/engine/mp3.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/recorder-core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion recorder.mp3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion recorder.wav.min.js

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions src/engine/mp3-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ function Takehiro() {
break;

default:
abort();//fix cc 精简 print
//fix cc 精简 print
break;
}
}
Expand Down Expand Up @@ -2456,7 +2456,7 @@ function BitStream() {

if (gfc.h_ptr == gfc.w_ptr) {
/* yikes! we are out of header buffer space */
abort();//fix cc 精简 print
//fix cc 精简 print
}

}
Expand Down Expand Up @@ -2759,7 +2759,7 @@ function BitStream() {
total_bytes_output.total += bufByteIdx + 1;

if (flushbits < 0) {
abort();//fix cc 精简 print
//fix cc 精简 print
}
return flushbits;
}
Expand Down Expand Up @@ -2828,20 +2828,28 @@ function BitStream() {
* what we think the resvsize is:
*/
if (compute_flushbits(gfp, new TotalBytes()) != gfc.ResvSize) {
abort();//fix cc 精简 print
//fix cc 精简 print
}

/*
* compare main_data_begin for the next frame with what we think the
* resvsize is:
*/
if ((l3_side.main_data_begin * 8) != gfc.ResvSize) {
abort();//fix cc 精简
//fix cc 精简 print
gfc.ResvSize = l3_side.main_data_begin * 8;
}
//;

if (totbit > 1000000000) {
abort();//fix cc 精简
if (totbit > 1000000000) { //不可精简
/*
* to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset
* bit counter
*/
var i;
for (i = 0; i < LameInternalFlags.MAX_HEADER_BUF; ++i)
gfc.header[i].write_timing -= totbit;
totbit = 0;
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/recorder-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var IsNum=function(v){return typeof v=="number"};
var Recorder=function(set){
return new initFn(set);
};
var LM=Recorder.LM="2023-12-17 18:13";
var LM=Recorder.LM="2023-12-24 18:09";
var GitUrl="https://github.com/xiangyuecn/Recorder";
var RecTxt="Recorder";
var getUserMediaTxt="getUserMedia";
Expand Down

0 comments on commit 4747184

Please sign in to comment.