From 0aacd15c4d6935bb398e1f724a9f1c46e2b6bb93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E5=9D=9A=E6=9E=9C?= <753610399@qq.com> Date: Fri, 10 Jan 2020 16:40:25 +0800 Subject: [PATCH] =?UTF-8?q?demo=E7=BC=96=E8=BE=91=E5=B0=86=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E6=96=87=E4=BB=B6=E3=80=81=E8=A7=A3=E7=A0=81Audio?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=88=B0Runtime=E9=87=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/runtime-codes/lib.samplerate.raise.js | 88 +++------------ .../runtime-codes/lib.transform.mp32other.js | 45 ++------ .../runtime-codes/lib.transform.wav2other.js | 45 ++------ .../teach.realtime.mix_multiple.js | 90 ++++------------ assets/runtime-codes/teach.sonic.transform.js | 89 +++------------- ...0\201\345\210\206\345\217\221Runtime.html" | 100 +++++++++++++++++- 6 files changed, 173 insertions(+), 284 deletions(-) diff --git a/assets/runtime-codes/lib.samplerate.raise.js b/assets/runtime-codes/lib.samplerate.raise.js index 6cb7097..f4686d2 100644 --- a/assets/runtime-codes/lib.samplerate.raise.js +++ b/assets/runtime-codes/lib.samplerate.raise.js @@ -121,6 +121,22 @@ Runtime.Ctrls([ ,{html:"
"} ,{name:"8k转16k",click:"k8k16"} ,{name:"16k转44.1k",click:"k16k441"} + + ,{choiceFile:{ + process:function(fileName,arrayBuffer,filesCount,fileIdx,endCall){ + Runtime.DecodeAudio(fileName,arrayBuffer,function(data){ + Runtime.LogAudio(data.srcBlob,data.duration,{set:data},"已解码"+fileName); + + rec=null; + transform([data.data],data.sampleRate); + + endCall(); + },function(msg){ + Runtime.Log(msg,1); + endCall(); + }); + } + }} ]); @@ -165,76 +181,4 @@ function recStop(){ -//*****拖拽或者选择文件****** -$(".choiceFileBox").remove(); -Runtime.Log('
\ -
\ - 拖拽多个音乐文件到这里 / 点此选择,并转换\ -
\ - \ -
'); -$(".dropFile").bind("dragover",function(e){ - e.preventDefault(); -}).bind("drop",function(e){ - e.preventDefault(); - - readChoiceFile(e.originalEvent.dataTransfer.files); -}); -$(".choiceFile").bind("change",function(e){ - readChoiceFile(e.target.files); -}); -function readChoiceFile(files){ - if(!files.length){ - return; - }; - - Runtime.Log("发现"+files.length+"个文件,开始转换..."); - - var idx=-1; - var run=function(){ - idx++; - if(idx>=files.length){ - return; - }; - - var file = files[idx]; - var reader = new FileReader(); - reader.onload = function(e){ - decodeAudio(file.name,e.target.result,run); - } - reader.readAsArrayBuffer(file); - }; - run(); -}; -var decodeAudio=function(name,arr,call){ - if(!Recorder.Support()){//强制激活Recorder.Ctx 不支持大概率也不支持解码 - Runtime.Log("浏览器不支持音频解码",1); - return; - }; - var srcBlob=new Blob([arr],{type:"audio/"+(/[^.]+$/.exec(name)||[])[0]}); - var ctx=Recorder.Ctx; - ctx.decodeAudioData(arr,function(raw){ - var src=raw.getChannelData(0); - var sampleRate=raw.sampleRate; - console.log(name,raw,srcBlob); - - var pcm=new Int16Array(src.length); - for(var i=0;i\ -
\ - 拖拽mp3到这里 / 点此选择,并转换\ -
\ - \ -'); -$(".dropFile").bind("dragover",function(e){ - e.preventDefault(); -}).bind("drop",function(e){ - e.preventDefault(); - - readChoiceFile(e.originalEvent.dataTransfer.files); -}); -$(".choiceFile").bind("change",function(e){ - readChoiceFile(e.target.files); -}); -function readChoiceFile(files){ - if(!files.length){ - return; - }; - - var file = files[0]; - Runtime.Log("发现"+files.length+"个文件,只转换第1个:"+file.name+",开始转换..."); - var reader = new FileReader(); - reader.onload = function(e){ - test(new Blob([e.target.result])); - } - reader.readAsArrayBuffer(file); -}; \ No newline at end of file diff --git a/assets/runtime-codes/lib.transform.wav2other.js b/assets/runtime-codes/lib.transform.wav2other.js index c60274c..555d636 100644 --- a/assets/runtime-codes/lib.transform.wav2other.js +++ b/assets/runtime-codes/lib.transform.wav2other.js @@ -112,6 +112,16 @@ Runtime.Ctrls([ {name:"16位wav录音",click:"recStart16"} ,{name:"8位wav录音",click:"recStart8"} ,{name:"结束录音并转换",click:"recStop"} + + ,{choiceFile:{ + multiple:false + ,name:"wav" + ,mime:"audio/wav" + ,process:function(fileName,arrayBuffer,filesCount,fileIdx,endCall){ + test(new Blob([arrayBuffer])); + endCall(); + } + }} ]); @@ -162,38 +172,3 @@ function recStop(){ Runtime.Log("录音失败:"+msg, 1); }); }; - - - - -//*****拖拽或者选择文件****** -$(".choiceFileBox").remove(); -Runtime.Log('
\ -
\ - 拖拽wav到这里 / 点此选择,并转换\ -
\ - \ -
'); -$(".dropFile").bind("dragover",function(e){ - e.preventDefault(); -}).bind("drop",function(e){ - e.preventDefault(); - - readChoiceFile(e.originalEvent.dataTransfer.files); -}); -$(".choiceFile").bind("change",function(e){ - readChoiceFile(e.target.files); -}); -function readChoiceFile(files){ - if(!files.length){ - return; - }; - - var file = files[0]; - Runtime.Log("发现"+files.length+"个文件,只转换第1个:"+file.name+",开始转换..."); - var reader = new FileReader(); - reader.onload = function(e){ - test(new Blob([e.target.result])); - } - reader.readAsArrayBuffer(file); -}; \ No newline at end of file diff --git a/assets/runtime-codes/teach.realtime.mix_multiple.js b/assets/runtime-codes/teach.realtime.mix_multiple.js index dfe505f..103b29a 100644 --- a/assets/runtime-codes/teach.realtime.mix_multiple.js +++ b/assets/runtime-codes/teach.realtime.mix_multiple.js @@ -173,6 +173,22 @@ Runtime.Ctrls([ ,{name:"爆笑音效",click:"bgmSet('xiao');Date.now",cls:"mixMinBtn mixMinBtnOff mixBtn-xiao"} ,{name:"晕倒音效",click:"bgmSet('yun');Date.now",cls:"mixMinBtn mixMinBtnOff mixBtn-yun"} ,{name:"转场音效",click:"bgmSet('scene');Date.now",cls:"mixMinBtn mixMinBtnOff mixBtn-scene"} + + ,{choiceFile:{ + title:"替换混音BGM" + ,process:function(fileName,arrayBuffer,filesCount,fileIdx,endCall){ + if(fileIdx==0){ + loadWait=0; + musics=[]; + }; + decodeAudio(fileName,arrayBuffer,function(){ + if(fileIdx+1>=filesCount){ + Runtime.Log("素材替换完毕,可以开始录音了",2); + }; + endCall(); + }); + } + }} ]); @@ -234,51 +250,6 @@ function recStop(){ }; -//*****拖拽或者选择文件****** -$(".choiceFileBox").remove(); -Runtime.Log('
\ -
\ - 拖拽多个音乐文件到这里 / 点此选择,替换混音BGM\ -
\ - \ -
'); -$(".dropFile").bind("dragover",function(e){ - e.preventDefault(); -}).bind("drop",function(e){ - e.preventDefault(); - - readChoiceFile(e.originalEvent.dataTransfer.files); -}); -$(".choiceFile").bind("change",function(e){ - readChoiceFile(e.target.files); -}); -function readChoiceFile(files){ - if(!files.length){ - return; - }; - - Runtime.Log("发现"+files.length+"个文件,开始替换素材..."); - loadWait=0; - musics=[]; - - var idx=-1; - var run=function(){ - idx++; - if(idx>=files.length){ - Runtime.Log("素材替换完毕,可以开始录音了",2); - return; - }; - - var file = files[idx]; - var reader = new FileReader(); - reader.onload = function(e){ - decodeAudio(file.name,e.target.result,run); - } - reader.readAsArrayBuffer(file); - }; - run(); -}; - //*****加载和解码素材******** @@ -301,33 +272,18 @@ var load=function(name,bgName,call){ xhr.send(); }; var decodeAudio=function(name,arr,call,bgName){ - if(!Recorder.Support()){//强制激活Recorder.Ctx 不支持大概率也不支持解码 - Runtime.Log("浏览器不支持音频解码",1); - return; - }; - var srcBlob=new Blob([arr],{type:"audio/"+(/[^.]+$/.exec(name)||[])[0]}); - var ctx=Recorder.Ctx; - ctx.decodeAudioData(arr,function(raw){ - var src=raw.getChannelData(0); - var sampleRate=raw.sampleRate; - console.log(name,raw,srcBlob); - - var pcm=new Int16Array(src.length); - for(var i=0;i'} ,{name:"重新转换",click:"recTransform"} ,{name:"重置设置",click:"resetCtrl"} + + ,{choiceFile:{ + process:function(fileName,arrayBuffer,filesCount,fileIdx,endCall){ + Runtime.DecodeAudio(fileName,arrayBuffer,function(data){ + Runtime.LogAudio(data.srcBlob,data.duration,{set:data},"已解码"+fileName); + + rec=null; + transform([data.data],data.sampleRate); + + endCall(); + },function(msg){ + Runtime.Log(msg,1); + endCall(); + }); + } + }} ]); $(".sonicCtrlRange").bind("change",function(e){ @@ -177,76 +193,3 @@ function recStop(){ Runtime.Log("录音失败:"+msg, 1); },true); }; - - - -//*****拖拽或者选择文件****** -$(".choiceFileBox").remove(); -Runtime.Log('
\ -
\ - 拖拽多个音乐文件到这里 / 点此选择,并转换\ -
\ - \ -
'); -$(".dropFile").bind("dragover",function(e){ - e.preventDefault(); -}).bind("drop",function(e){ - e.preventDefault(); - - readChoiceFile(e.originalEvent.dataTransfer.files); -}); -$(".choiceFile").bind("change",function(e){ - readChoiceFile(e.target.files); -}); -function readChoiceFile(files){ - if(!files.length){ - return; - }; - - Runtime.Log("发现"+files.length+"个文件,开始转换..."); - - var idx=-1; - var run=function(){ - idx++; - if(idx>=files.length){ - return; - }; - - var file = files[idx]; - var reader = new FileReader(); - reader.onload = function(e){ - decodeAudio(file.name,e.target.result,run); - } - reader.readAsArrayBuffer(file); - }; - run(); -}; -var decodeAudio=function(name,arr,call){ - if(!Recorder.Support()){//强制激活Recorder.Ctx 不支持大概率也不支持解码 - Runtime.Log("浏览器不支持音频解码",1); - return; - }; - var srcBlob=new Blob([arr],{type:"audio/"+(/[^.]+$/.exec(name)||[])[0]}); - var ctx=Recorder.Ctx; - ctx.decodeAudioData(arr,function(raw){ - var src=raw.getChannelData(0); - var sampleRate=raw.sampleRate; - console.log(name,raw,srcBlob); - - var pcm=new Int16Array(src.length); - for(var i=0;i'+o.name+''); }; @@ -213,6 +216,42 @@ ProcessWaveView.input(buffers[buffers.length-1],powerLevel,bufferSampleRate); } + ,DecodeAudio:function(fileName,arrayBuffer,True,False){ + True=True||NOOP; + False=False||NOOP; + if(!Recorder.Support()){//强制激活Recorder.Ctx 不支持大概率也不支持解码 + False("浏览器不支持音频解码"); + return; + }; + var type=(/[^.]+$/.exec(fileName)||[])[0]||""; + var srcBlob=new Blob([arrayBuffer],{type:type&&("audio/"+type)||""}); + + var ctx=Recorder.Ctx; + ctx.decodeAudioData(arrayBuffer,function(raw){ + var src=raw.getChannelData(0); + var sampleRate=raw.sampleRate; + console.log(fileName,raw,srcBlob); + + var pcm=new Int16Array(src.length); + for(var i=0;i\ +
\ + 拖拽'+(set.multiple?"多":"一")+'个'+set.name+'文件到这里 / 点此选择,并'+set.title+'\ +
\ + \ +'); + $(".RuntimeChoiceDropFile").bind("dragover",function(e){ + e.preventDefault(); + }).bind("drop",function(e){ + e.preventDefault(); + + readChoiceFile(e.originalEvent.dataTransfer.files); + }); + $(".RuntimeChoiceFile").bind("change",function(e){ + readChoiceFile(e.target.files); + }); + function readChoiceFile(files){ + if(!files.length){ + return; + }; + + Runtime.Log("发现"+files.length+"个文件,开始"+set.title+"..."); + + var idx=-1; + var run=function(){ + idx++; + if(idx>=files.length){ + return; + }; + + var file = files[idx]; + var reader = new FileReader(); + reader.onload = function(e){ + set.process(file.name,e.target.result,files.length,idx,run); + } + reader.readAsArrayBuffer(file); + }; + run(); + }; +}; + + })(); @@ -819,8 +913,10 @@ Runtime.Import(jsList) 导入js列表,只能调用一次,jsList:[{url:"",check:fn()},...] check返回false跳过这条的导入 - Runtime.Ctrls(ctrls) 在控制区显示按钮,只能调用一次,ctrls:[{html:"html代码"},{name:"按钮名称",click:"函数名称",cls:"class name"},...] - + Runtime.Ctrls(ctrls) 在控制区显示按钮,只能调用一次,ctrls:[{name:"按钮名称",click:"函数名称",cls:"class name"},{html:"html代码"},{choiceFile:{multiple:true,name:"音乐",title:"转换",mime:"audio/*",process:fn(fileName,arrayBuffer,filesCount,fileIdx,endCall)}},...] + + Runtime.DecodeAudio(fileName,arrayBuffer,True,False) 解码一个音频文件,True({sampleRate:采样率,duration:音频时长,srcBlob:Blob对象,type:从文件名中提取的音频类型,data:[Int16,...]pcm数据}),False(errMsg) + Runtime.Log(msgHtml,color) 在日志区显示日志html,color:0默认,1红色,2绿色,其他指定颜色 Runtime.LogAudio(blob,duration,rec,msgHtml) 在日志区显示一个音频的日志信息,rec只要对象内有set属性就行