Skip to content

Commit

Permalink
demo编辑将选择文件、解码Audio统一到Runtime里面
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyuecn committed Jan 10, 2020
1 parent f794eff commit 0aacd15
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 284 deletions.
88 changes: 16 additions & 72 deletions assets/runtime-codes/lib.samplerate.raise.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ Runtime.Ctrls([
,{html:"<hr/>"}
,{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();
});
}
}}
]);


Expand Down Expand Up @@ -165,76 +181,4 @@ function recStop(){



//*****拖拽或者选择文件******
$(".choiceFileBox").remove();
Runtime.Log('<div class="choiceFileBox">\
<div class="dropFile" onclick="$(\'.choiceFile\').click()" style="border: 3px dashed #a2a1a1;background:#eee; padding:30px 0; text-align:center;cursor: pointer;">\
拖拽多个音乐文件到这里 / 点此选择,并转换\
</div>\
<input type="file" class="choiceFile" style="display:none" accept="audio/*" multiple="multiple">\
</div>');
$(".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<src.length;i++){//floatTo16BitPCM
var s=Math.max(-1,Math.min(1,src[i]));
s=s<0?s*0x8000:s*0x7FFF;
pcm[i]=s;
};

Runtime.LogAudio(srcBlob,Math.round(src.length/sampleRate*1000),{set:{sampleRate:sampleRate}},"已解码"+name);

rec=null;
transform([pcm],sampleRate);

call();
},function(e){
Runtime.Log("audio解码失败:"+e.message,1);
});
};


Runtime.Log("结束录音转换格式以最后点击的哪个为准");
45 changes: 10 additions & 35 deletions assets/runtime-codes/lib.transform.mp32other.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ var test=function(mp3Blob){
Runtime.Ctrls([
{name:"开始mp3录音",click:"recStart"}
,{name:"结束录音并转换",click:"recStop"}

,{choiceFile:{
multiple:false
,name:"mp3"
,mime:"audio/mp3"
,process:function(fileName,arrayBuffer,filesCount,fileIdx,endCall){
test(new Blob([arrayBuffer]));
endCall();
}
}}
]);


Expand Down Expand Up @@ -143,38 +153,3 @@ function recStop(){
Runtime.Log("录音失败:"+msg, 1);
});
};




//*****拖拽或者选择文件******
$(".choiceFileBox").remove();
Runtime.Log('<div class="choiceFileBox">\
<div class="dropFile" onclick="$(\'.choiceFile\').click()" style="border: 3px dashed #a2a1a1;background:#eee; padding:30px 0; margin-top:30px; text-align:center;cursor: pointer;">\
拖拽mp3到这里 / 点此选择,并转换\
</div>\
<input type="file" class="choiceFile" style="display:none" accept="audio/mp3">\
</div>');
$(".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);
};
45 changes: 10 additions & 35 deletions assets/runtime-codes/lib.transform.wav2other.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}}
]);


Expand Down Expand Up @@ -162,38 +172,3 @@ function recStop(){
Runtime.Log("录音失败:"+msg, 1);
});
};




//*****拖拽或者选择文件******
$(".choiceFileBox").remove();
Runtime.Log('<div class="choiceFileBox">\
<div class="dropFile" onclick="$(\'.choiceFile\').click()" style="border: 3px dashed #a2a1a1;background:#eee; padding:30px 0; margin-top:30px; text-align:center;cursor: pointer;">\
拖拽wav到这里 / 点此选择,并转换\
</div>\
<input type="file" class="choiceFile" style="display:none" accept="audio/wav">\
</div>');
$(".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);
};
90 changes: 23 additions & 67 deletions assets/runtime-codes/teach.realtime.mix_multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
}}
]);


Expand Down Expand Up @@ -234,51 +250,6 @@ function recStop(){
};


//*****拖拽或者选择文件******
$(".choiceFileBox").remove();
Runtime.Log('<div class="choiceFileBox">\
<div class="dropFile" onclick="$(\'.choiceFile\').click()" style="border: 3px dashed #a2a1a1;background:#eee; padding:30px 0; text-align:center;cursor: pointer;">\
拖拽多个音乐文件到这里 / 点此选择,替换混音BGM\
</div>\
<input type="file" class="choiceFile" style="display:none" accept="audio/*" multiple="multiple">\
</div>');
$(".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();
};



//*****加载和解码素材********
Expand All @@ -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<src.length;i++){//floatTo16BitPCM
var s=Math.max(-1,Math.min(1,src[i]));
s=s<0?s*0x8000:s*0x7FFF;
pcm[i]=s;
};
Runtime.DecodeAudio(name,arr,function(data){
Runtime.LogAudio(data.srcBlob,data.duration,{set:data},"已解码"+name);

Runtime.LogAudio(srcBlob,Math.round(src.length/sampleRate*1000),{set:{sampleRate:sampleRate}},"已解码"+name);
if(bgName){
musicBGs[bgName]={pcm:pcm,sampleRate:sampleRate};
musicBGs[bgName]={pcm:data.data,sampleRate:data.sampleRate};
}else{
musics.push({pcm:pcm,sampleRate:sampleRate});
musics.push({pcm:data.data,sampleRate:data.sampleRate});
};
call();
},function(e){
Runtime.Log("audio解码失败:"+e.message,1);
},function(msg){
Runtime.Log(msg,1);
call();
});
};
var loadAll=function(){
Expand Down
Loading

0 comments on commit 0aacd15

Please sign in to comment.