这是我第一次在这边发文,假如等等下面有什么违规的地方,说声抱歉
我这边要做个client端的javascript写的程式 来负责的抓取使用者的mic的data
然后再把抓到的Data做成Pcm Data
而我写的程式如下
let timer;
let chunks = [];
let finalChunks = [];
let count = 0;
let theData = 0;
let audioInput = null;
let microphone_stream;
let gain_node = null;
let script_processor_node;
$('#s').click(function(){
window.AudioContext = window.AudioContext ||
window.webkitAudioContext;
if (!navigator.mediaDevices.getUserMedia){
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
}
if(navigator.mediaDevices.getUserMedia)
{
navigator.getUserMedia
(
{audio:true},
function(stream){
start_microphone(stream);
},
function(e){
alert('Error capturing audio.');
}
);
}
else
{
alert('getUserMedia not supported in this browser.');
}
});
function process_microphone_buffer(event) {
theData = event.inputBuffer.getChannelData(0);
}
function start_microphone(stream){
let context = new AudioContext();
let BUFF_SIZE_RENDERER = 16384;
microphone_stream = context.createMediaStreamSource(stream);
script_processor_node = context.createScriptProcessor(1024,1,1);
script_processor_node.onaudioprocess = process_microphone_buffer;
microphone_stream.connect(script_processor_node);
update();
function update()
{
console.log(theData);
timer = setTimeout(update,1000);
if(theData !== 0){
chunks.push(Array.from(theData));
count++;
}
if(count === 30)
{
clearTimeout(timer);
console.log((chunks));
stream.getTracks()[0].stop();
let i;
let j;
for( i = 0;i<chunks.length;i++)
{
for( j = 0;j<1024;j++)
{
finalChunks.push(chunks[i][j]);
}
}
console.log(finalChunks);
console.log(finalChunks.length);
}
}
}
简单解释
有个button '#s' 是负责开始的,使用者开始就会开始请求使用者的Mic(getUserMedia)
然后做个30秒,把抓到的data放到物件chunks
之后汇出chunks在float32array 转 int16 做成pcm data(在这边就没写了,另一块东西)
不过问题在这
onaudioprocess这个method
负责去call抓buffer里面的data的function
在chrome不支援........
目前试过的浏览器只有火狐ok
想问看看这边的版友
有没有比较好的方式可以让火狐和chrome都可以执行的方案