2016-06-09 1 views
0

질문 : 웹 오디오 API를 사용하고 있습니다. 라디오 스트림과 같이 논스톱 오디오 스트림을 버퍼해야합니다. 알림을 받으면 지난 3 초 오디오 데이터를 가져와 서버로 보내야합니다. 어떻게 할 수 있습니까? nodejs는 버퍼가 내장되어 있지만 순환 버퍼가 아닌 것처럼 보입니다. 논스톱 스트림을 작성하면 오버 플로우 된 것처럼 보입니다.웹 오디오 스트림의 마지막 3s 데이터를 내보내는 방법

내 질문에 대한 답변 : 주변 오디오 기반 웹 인증 방법을 구현 중입니다. 간단히 말해서, 두 개의 오디오 신호 (클라이언트에서 하나, 앵커 장치에서 하나, 서버와 항상 동기화 됨)를 비교해야합니다. 유사하면 인증 요청이 서버에 의해 승인됩니다. 오디오 녹음은 웹 오디오 API를 사용하여 클라이언트와 앵커 장치 모두에서 구현됩니다.

주변 오디오를 스트리밍하려면 앵커 장치에서 버퍼를 관리해야합니다. 앵커 장치는 항상 실행 중이므로 스트림이 종료되지 않습니다.

답변

0

ScriptProcessorNode를 사용하여 스트림에서 오디오를 캡처 할 수 있습니다. 이것은 더 이상 사용되지 않지만 현재 브라우저는 새로운 AudioWorker를 실제로 구현하지 않습니다.

var N = 1024; 
var time = 3; // Desired time of capture; 
var frame_holder = []; 
var time_per_frame = N/context.sampleRate; 
var num_frames = Math.ceil(time/time_per_frame); // Minimum number to meet time 
var script = context.createScriptProcessor(N,1,1); 
script.connect(context.destination); 

script.onaudioprocess = function(e) { 
    var input = e.inputBuffer.getChannelData(0); 
    var output = e.outputBuffer.getChannelData(0); 
    var copy = new Float32Array(input.length); 
    for (var n=0; n<input.length; n++) { 
    output[n] = 0.0; // Null this as I guess you are capturing microphone 
    copy[n] = input[n]; 
    } 
    // Now we need to see if we have more than 3s worth of frames 
    if (frame_holder.length > num_frames) { 
    frame_holder = frame_holder.slice(frame_holder.length-num_frames); 
    } 
    // Add in the current frame 
    var temp = frame_holder.slice(1); // Cut off first frame; 
    frame_holder = temp.concat([copy]); // Add the latest frame 
} 

그런 다음 실제 전송을 위해, 당신은 단지 문자열 함께 프레임 복사해야합니다. 물론 하나의 긴 어레이를 유지하는 것보다 쉽습니다.