我正在尝试创建一个实验性应用程序,可以实时传输音频client 1
to client 2
.
因此,在学习了有关同一主题的一些教程和问题之后,我使用了WebRTC and binaryjs。到目前为止,这就是我得到的
1- Client 1
and Client 2
已连接到 BinaryJS 来发送/接收数据块。
2- Client 1
使用WebRTC录制音频并逐步发送到BinaryJS
3- Client 2
接收块并尝试播放它们。
好吧,我在最后一部分遇到了错误。这是我收到的错误消息:
未捕获的范围错误:源太大
在 Float32Array.set(本机)
这是代码:
Client 1
var WSClient;
var AudioStream;
function load(){
var session = {
audio: true,
video: false
};
var recordRTC = null;
navigator.getUserMedia(session, startRecording, onError);
WSClient = new BinaryClient('ws://localhost:9001');
WSClient.on('open',function(){
console.log('client opened')
AudioStream = WSClient.createStream();
})
}
function startRecording(stream){
var context = new AudioContext();
var audio_input = context.createMediaStreamSource(stream);
var buffer_size = 2048;
var recorder = context.createScriptProcessor(buffer_size, 1, 1);
recorder.onaudioprocess = function(e){
console.log('chunk')
var left = e.inputBuffer.getChannelData(0);
AudioStream.write(left);
};
audio_input.connect(recorder);
recorder.connect(context.destination);
}
Client 2
var WSClient;
var audioContext;
var sourceNode;
function load(){
audioContext = new AudioContext();
sourceNode = audioContext.createBufferSource();
sourceNode.connect(audioContext.destination);
sourceNode.start(0);
WSClient = new BinaryClient('ws://localhost:9001');
WSClient.on('open',function(){
console.log('client opened');
});
WSClient.on('stream', function(stream, meta){
// collect stream data
stream.on('data', function(data){
console.log('received chunk')
var integers = new Int16Array(data);
var audioBuffer = audioContext.createBuffer(1, 2048, 4410);
audioBuffer.getChannelData(0).set(integers); //appearently this is where the error occurs
sourceNode.buffer = audioBuffer;
});
});
}
Server
var wav = require('wav');
var binaryjs = require('binaryjs');
var binaryjs_server = binaryjs.BinaryServer;
var server = binaryjs_server({port: 9001});
server.on('connection', function(client){
console.log('server connected');
var file_writter = null;
client.on('stream', function(stream, meta){
console.log('streaming', server.clients)
//send to other clients
for(var id in server.clients){
if(server.clients.hasOwnProperty(id)){
var otherClient = server.clients[id];
if(otherClient != client){
var send = otherClient.createStream(meta);
stream.pipe(send);
}
}
}
});
client.on('close', function(stream){
console.log('client closed')
if(file_writter != null) file_writter.end();
});
});
错误发生在这里:
audioBuffer.getChannelData(0).set(integers);
所以我有两个问题:
是否可以发送我捕获的块client 1
然后将它们复制到client 2
?
我遇到的错误有什么关系?
多谢你们!
@edit 1
由于我从其他问题中获取了代码片段,因此我仍在尝试理解它。我评论了该行client 2
创建一个的代码Int16Array
现在我得到了一个不同的错误(但我不知道哪个版本的代码更正确):
未捕获的 DOMException:无法在“AudioBufferSourceNode”上设置“缓冲区”属性:已设置缓冲区后无法设置缓冲区
可能是因为我每次获得新数据块时都会设置它。