Javascript 使用web音频api分析麦克风输入(将MediaStreamSource转换为BufferSource)
我正在尝试使用Web音频Api获取每分钟的节拍(BPM),就像在以下链接(或)中一样,但是从音频流(麦克风)获取。 不幸的是,我没有让它运行。 有人知道我如何将麦克风MediaStreamSource转换为BufferSource并像第一个链接的网站一样继续吗? 以下是我到目前为止的代码:Javascript 使用web音频api分析麦克风输入(将MediaStreamSource转换为BufferSource),javascript,audio,web,web-audio-api,Javascript,Audio,Web,Web Audio Api,我正在尝试使用Web音频Api获取每分钟的节拍(BPM),就像在以下链接(或)中一样,但是从音频流(麦克风)获取。 不幸的是,我没有让它运行。 有人知道我如何将麦克风MediaStreamSource转换为BufferSource并像第一个链接的网站一样继续吗? 以下是我到目前为止的代码: navigator.mediaDevices.getUserMedia({ audio: true, video: false }) .then(function(stream) { /* use t
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(function(stream) {
/* use the stream */
var OfflineContext = window.OfflineAudioContext || window.webkitOfflineAudioContext;
var source = OfflineContext.createMediaStreamSource(stream);
source.connect(OfflineContext);
var offlineContext = new OfflineContext(2, 30 * 44100, 44100);
offlineContext.decodeAudioData(stream, function(buffer) {
// Create buffer source
var source = offlineContext.createBufferSource();
source.buffer = buffer;
// Beats, or kicks, generally occur around the 100 to 150 hz range.
// Below this is often the bassline. So let's focus just on that.
// First a lowpass to remove most of the song.
var lowpass = offlineContext.createBiquadFilter();
lowpass.type = "lowpass";
lowpass.frequency.value = 150;
lowpass.Q.value = 1;
// Run the output of the source through the low pass.
source.connect(lowpass);
// Now a highpass to remove the bassline.
var highpass = offlineContext.createBiquadFilter();
highpass.type = "highpass";
highpass.frequency.value = 100;
highpass.Q.value = 1;
// Run the output of the lowpass through the highpass.
lowpass.connect(highpass);
// Run the output of the highpass through our offline context.
highpass.connect(offlineContext.destination);
// Start the source, and render the output into the offline conext.
source.start(0);
offlineContext.startRendering();
});
})
.catch(function(err) {
/* handle the error */
alert("Error");
});
谢谢大家! 那些文章很棒。您当前的方法存在一些问题:
此外,您不能在脱机上下文中创建MediaStreamAudioSourceNode。它必须是一个音频上下文。哦,是的。我知道有一个我忘记了