synchronization - 如何与录音同步播放多个音频
问题描述
我正在开发一个允许同时播放多个音轨(例如鼓、萨克斯管、贝斯、人声)的应用程序,我设法通过在未来与 AudioSourceNode.start(timeInFuture) 进行调度来同步播放。现在我需要在播放其他歌曲时录制用户的麦克风,保存录音,然后与其他曲目同步播放。对于录制我使用 MediaRecorder API,我可以从流开始录制并创建一个新的 AudioSourceNode。
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
console.log('getUserMedia supported.');
navigator.mediaDevices.getUserMedia({ audio: { channelCount: { ideal: 2 }, latency: 0.05, echoCancellation: { ideal: true } } })
.then(stream => {
this.mediaRecorder = new MediaRecorder(stream);
this.mediaRecorder.ondataavailable = (e: any) => {
this.recordedChunks.push(e.data);
}
this.mediaRecorder.onstart = (e: any) => {
this.timestamp = Date.now() - this.timestamp;
}
this.mediaRecorder.onstop = (e: any) => {
console.log("recorder stopped", e);
stream.getTracks().forEach(t => t.stop())
const blob = new Blob(this.recordedChunks); //, { type: 'audio/ogg; codecs=opus' });
this.recordedChunks = [];
console.log(blob);
let fileReader = new FileReader();
fileReader.onloadend = () => {
if (fileReader.result instanceof ArrayBuffer)
this.audioCtx.decodeAudioData(fileReader.result,
(audioBuffer) => {
this.currentTrack.details.push({
name: "Record",
url: "assets/record/record",
gain: 100,
gainNode: null,
source: null,
enabled: true,
audioByte: audioBuffer
})
this.weAreRecord = true;
},
(error) => { console.log("decode error: ", error) }
)
}
fileReader.readAsArrayBuffer(blob);
}
source.start(); //Here i'm starting playback
this.mediaRecorder.start(); //Here i'm starting recording
this.timestamp = Date.now();
console.log(this.timestamp);
})
// Error callback
.catch(function (err) {
console.log('The following getUserMedia error occurred: ' + err);
});
} else {
console.log('getUserMedia not supported on your browser!');
}
问题出现在这里:如果我尝试使用 AudioSourceNode.start(timeInFuture) 播放所有曲目(包括录音),则录音不同步。
for (let i = 0; i < this.currentTrack.details.length; i++) {
this.currentTrack.details[i].source.start(now + delay, timeA);
}
有没有办法在播放前同步所有曲目?谢谢大家
解决方案
推荐阅读
- docker - 使用 Compose 和 Github 操作部署 Docker 容器
- ruby-on-rails - 复杂的时间转换和阶段划分留下错误
- python - 我可以只显示我的 MonkeyLearn 模型中的 tag_name 吗?
- javascript - 如何将类添加到传递的组件
- c# - JwtSecurityToken 过期时间无效 .NET Core 3.1
- anylogic - 使用 createAndStart 的目的
- java - 如何使用 Java 中的当前登录信息访问安全网站
- linux - Armv7 程序集 - 当我按下回车键时,额外的控制台提示“pi@raspberrypi:~ $”?
- asp.net-core - 我们可以使用 devexpress 在 Asp.net 核心视图上添加动态控件吗?
- algorithm - 求 n 条线段与两条平行线上端点的交点数