问题描述
我正在尝试保存来自webAudio API的输出以供将来使用,到目前为止,我认为获取PCM数据并将其保存为文件将符合我的期望,我想知道webAudio或mozAudio是否已经支持保存输出流,如果不能从输出流中获取pcm数据
I am trying to save the output from webAudio API for future use , so far i think getting PCM data and saving it as a file will do my expectation , I am wondering if the webAudio or mozAudio already supports saving the output stream if not how can i get the pcm data from the output stream
推荐答案
除了尝试捕获网络音频以某种编程方式进行.这里的假设是您希望通过当前正在浏览的页面上的JavaScript中执行的代码来执行此操作,但这还不是很清楚.
There isn't a good sense of the requirements here outside of attempting to capture web audio in some programmatic way. The presumption here is you want to do this from code executing in JavaScript on the page that's currently being browsed, but that also isn't entirely clear.
正如Incognito指出的那样,您可以在Chrome中通过使用挂在 decodeAudioData()
上的回调来完成此操作.但是,如果您只是试图捕获单个Web流的输出并将其解码为PCM以供您选择的声音工具使用,那么这对于您的使用来说可能会过于复杂.
As Incognito points out, you can do this in Chrome by using a callback hanging off decodeAudioData()
. But, this may be overly complicated for your uses if you're simply trying to capture, for example, the output of a single web stream and decode it into PCM for use in your sound tools of choice.
对于使用当前工具遮盖媒体URL或以其他方式难以解码的情况,您可能会考虑的另一种策略是从您的基础声卡中捕获.如果(且仅)当声卡不能有效地采样流时,这可以使您免费解码,但可能会降低采样率.
Another strategy you might consider, for cases when the media URL is obscured or otherwise difficult to decode using your current tools, is capture from your underlying sound card. This gives you the decoding for free, at the potential expense of a lower sampling rate if (and only if) your sound card isn't able to sample the stream effectively.
我们知道,您已经通过对PCM编码的需求对数字信号进行了数字编码.显然,只有在您具有使用被采样文件的合法权利的情况下,才这样做.
As we know, you're already encoding analog signals digitally anyway via your desire for PCM encoding. Obviously, only do this if you have the legal right to use the files being sampled.
无论您选择哪种路线,都祝您好运.无论是程序化流解剖还是现场采样,您现在都应该有足够的信息来进行.
Regardless of the route you choose, best of luck to you. Be it programmatic stream dissection or spot sampling, you should now have more than enough information to proceed.
根据OP的其他信息,这似乎是必需的解决方案(合并自和此处,使用 NodeJS对 fs
)的实现:
Based on additional information from the OP, this seems like the needed solution (merged from here and here, using NodeJS' implementation of fs
):
var fs = require('fs');
function saveAudio(data, saveLocation) {
var context = new (window.AudioContext || window.webkitAudioContext)();
var source = context.createBufferSource();
if(context.decodeAudioData) {
context.decodeAudioData(data, function(buffer) {
fs.writeFile(saveLocation, buffer, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
}, function(e) {
console.log(e);
});
} else {
var buffer = context.createBuffer(data, false /*mixToMono*/);
fs.writeFile(saveLocation, buffer, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
}
}
(警告:未经测试的代码.如果不起作用,欢迎进行编辑.)
(Warning: untested code. If this doesn't work, edits are welcome.)
这有效地从,从提供的数据
中解码PCM,然后尝试将其保存到目标 saveLocation
.真的很简单.
This effectively spools out decodeAudioData
from the Web Audio API, decodes PCM from the supplied data
, then attempts to save it to the target saveLocation
. Simple enough, really.
这篇关于从webAudio/mozAudio获取原始PCM数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!