Skip to content

Commit

Permalink
静态分发工具添加runtime-codes
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyuecn committed Oct 22, 2019
1 parent e007b4b commit 2a82858
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 7 deletions.
163 changes: 163 additions & 0 deletions assets/runtime-codes/lib.transform.wav2other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/******************
《扩展类库-wav格式转成其他格式》
作者:高坚果
时间:2019-10-22 13:48:35
文档:
Recorder.Wav2Other(newSet,wavBlob,True,False)
newSet:Recorder的set参数,用来生成新格式
wavBlob:wav二进制数据
True: fn(blob,duration,mockRec) 和Recorder的stop函数参数一致,mockRec为转码时用到的Recorder对象引用
False: fn(errMsg) 和Recorder的stop函数参数一致
******************/

//=====wav转mp3核心函数==========
Recorder.Wav2Other=function(newSet,wavBlob,True,False){
var reader=new FileReader();
reader.onloadend=function(){
//检测wav文件头
var wavView=new Uint8Array(reader.result);
var eq=function(p,s){
for(var i=0;i<s.length;i++){
if(wavView[p+i]!=s.charCodeAt(i)){
return false;
};
};
return true;
};
var pcm;
if(eq(0,"RIFF")&&eq(8,"WAVEfmt ")){
if(wavView[20]==1 && wavView[22]==1){//raw pcm 单声道
var sampleRate=wavView[24]+(wavView[25]<<8)+(wavView[26]<<16)+(wavView[27]<<24);
var bitRate=wavView[34]+(wavView[35]<<8);
console.log("wav info",sampleRate,bitRate);
if(bitRate==16){
pcm=new Int16Array(wavView.slice(44).buffer);
}else if(bitRate==8){
pcm=new Int16Array(wavView.length-44);
//8位转成16位
for(var j=44,d=0;j<wavView.length;j++,d++){
var b=wavView[j];
pcm[d]=(b-128)<<8;
};
};
};
};
if(!pcm){
False&&False("非wav raw格式音频,无法转码");
return;
};

var rec=Recorder(newSet).mock(pcm,sampleRate);
rec.stop(function(blob,duration){
True(blob,duration,rec);
},False);
};
reader.readAsArrayBuffer(wavBlob);
};
//=====END=========================



//转换测试
var test=function(wavBlob){
if(!wavBlob){
Runtime.Log("无数据源,请先录音",1);
return;
};
var set={
type:"mp3"
,sampleRate:48000
,bitRate:96
};

//数据格式一 Blob
Recorder.Wav2Other(set,wavBlob,function(blob,duration,rec){
Runtime.Log("wav src blob 转换成 mp3...",2);
Runtime.LogAudio(blob,duration,rec);
},function(msg){
Runtime.Log(msg,1);
});

//数据格式二 Base64 模拟
var reader=new FileReader();
reader.onloadend=function(){
var base64=(/.+;\s*base64\s*,\s*(.+)$/i.exec(reader.result)||[])[1];

//数据格式二核心代码,以上代码无关紧要
var bstr=atob(base64),n=bstr.length,u8arr=new Uint8Array(n);
while(n--){
u8arr[n]=bstr.charCodeAt(n);
};

Recorder.Wav2Other(set,new Blob([u8arr.buffer]),function(blob,duration,rec){
Runtime.Log("wav as base64 转换成 mp3...",2);
Runtime.LogAudio(blob,duration,rec);
},function(msg){
Runtime.Log(msg,1);
});
};
reader.readAsDataURL(wavBlob);
};






//=====以下代码无关紧要,音频数据源,采集原始音频用的==================
//显示控制按钮
Runtime.Ctrls([
{name:"16位wav录音",click:"recStart16"}
,{name:"8位wav录音",click:"recStart8"}
,{name:"结束录音并转换",click:"recStop"}
]);


//加载录音框架
Runtime.Import([
{url:RootFolder+"/src/recorder-core.js",check:function(){return !window.Recorder}}
,{url:RootFolder+"/src/engine/mp3.js",check:function(){return !Recorder.prototype.mp3}}
,{url:RootFolder+"/src/engine/mp3-engine.js",check:function(){return !Recorder.lamejs}}
,{url:RootFolder+"/src/engine/wav.js",check:function(){return !Recorder.prototype.wav}}
]);

//调用录音
var rec;
function recStart16(){
recStart(16);
};
function recStart8(){
recStart(8);
};
function recStart(bitRate){
rec=Recorder({
type:"wav"
,bitRate:bitRate
,onProcess:function(buffers,powerLevel,bufferDuration,bufferSampleRate){
Runtime.Process.apply(null,arguments);
}
});
var t=setTimeout(function(){
Runtime.Log("无法录音:权限请求被忽略(超时假装手动点击了确认对话框)",1);
},8000);

rec.open(function(){//打开麦克风授权获得相关资源
clearTimeout(t);
rec.start();//开始录音
},function(msg,isUserNotAllow){//用户拒绝未授权或不支持
clearTimeout(t);
Runtime.Log((isUserNotAllow?"UserNotAllow,":"")+"无法录音:"+msg, 1);
});
};
function recStop(){
rec.stop(function(blob,duration){
rec.close();//释放录音资源

Runtime.LogAudio(blob,duration,rec);

test(blob);
},function(msg){
Runtime.Log("录音失败:"+msg, 1);
});
};
33 changes: 26 additions & 7 deletions assets/工具-代码运行和静态分发Runtime.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
var id=LogAudios.length;
LogAudios.push({blob:blob,set:$.extend({},set),duration:duration});

Runtime.Log((msg||"已录制")+":"+set.bitRate+"kbps "+set.sampleRate+"hz "+blob.size+"b ["+set.type+"]"+duration+'ms <button onclick="Runtime.LogAudioDown(\''+id+'\')">下载</button> <button onclick="Runtime.LogAudioPlay(\''+id+'\')">播放</button> <span class="p'+id+'"></span> <span class="LogAudio_'+id+'"></span>');
Runtime.Log((msg||"已录制")+":"+(set.bitRate||"-")+"kbps "+(set.sampleRate||"-")+"hz "+blob.size+"b ["+(set.type||"-")+"]"+duration+'ms <button onclick="Runtime.LogAudioDown(\''+id+'\')">下载</button> <button onclick="Runtime.LogAudioPlay(\''+id+'\')">播放</button> <span class="p'+id+'"></span> <span class="LogAudio_'+id+'"></span>');
}
,LogClear:function(){
$(".mainLog").html("");
Expand Down Expand Up @@ -532,19 +532,38 @@
//显示代码
var viewUrlCode=function(href){
var idf=(/[?#&]idf=([^&#]+)/.exec(href)||[])[1];
var jsname=(/[?#&]jsname=([\w\-\.]+)/.exec(href)||[])[1];
var sha1=(/[?#&]sha1=([^&#]+)/.exec(href)||[])[1];
var shareCode=/[?#&]shareCode=v(\d+)_([^&#]+)/.exec(href);
if(!shareCode){
if(!idf){
//idf="self_base_demo";
Runtime.Log("初始化完成,可以编写代码,或者点击一个Demo来编辑",2);
if(jsname){
var path="runtime-codes/"+jsname+".js";
$.ajax({
url:path
,dataType:"text"
,success:function(data){
setVal(data);
Runtime.Log("已加载"+path+"源码,可运行查看效果",2);
}
,error:function(){
Runtime.Log("加载指定的"+path+"失败",1);
}
});
return;
};
for(var i=0;i<DemoCodeList.length;i++){
if(DemoCodeList[i].idf==idf){
Runtime.UseDemoCode(i);

if(idf){
for(var i=0;i<DemoCodeList.length;i++){
if(DemoCodeList[i].idf==idf){
Runtime.UseDemoCode(i);
return;
};
};
Runtime.Log("未知启动标识:"+idf,1);
return;
};

Runtime.Log("初始化完成,可以编写代码,或者点击一个Demo来编辑",2);
return;
};
var shareVer=shareCode[1];
Expand Down

0 comments on commit 2a82858

Please sign in to comment.