Web Audio API流:为什么dataArray不更改? [英] Web Audio API Stream: why isn't dataArray changing?

查看:86
本文介绍了Web Audio API流:为什么dataArray不更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决.请参阅下面的答案.

EDIT 2: solved. See answer below.

我稍微更改了代码,添加了一个增益节点,移动了一个函数.我还发现,如果我使用麦克风,它将可以正常工作.仍不适用于USB音频输入.任何的想法?这是我当前的代码:

I changed my code a little, added a gain node, moved a function. I also found that IF I use the microphone, it will work. Still doesn't work with usb audio input. Any idea? This is my current code:

window.AudioContext = window.AudioContext || window.webkitAudioContext;

window.onload = function(){

  var audioContext = new AudioContext();
  var analyser = audioContext.createAnalyser();
  var gainNode = audioContext.createGain();

  navigator.mediaDevices.getUserMedia({ audio:true, video:false }).then(function(stream){ //MediaStream

    var source = audioContext.createMediaStreamSource(stream);
    source.connect(analyser);
    analyser.connect(gainNode);
    gainNode.connect(audioContext.destination);

    listen();

  });

  function listen(){

    analyser.fftSize = 256;
    var bufferLength = analyser.frequencyBinCount;
    var dataArray = new Uint8Array(bufferLength);
    var index = 0;

    function write(){
      requestAnimationFrame(listen);
      analyser.getByteTimeDomainData(dataArray);
      $('.monitor').html(JSON.stringify(dataArray) + ' -- ' + (index++));
    }

    write();
  }

}

旧/原始帖子:

我当前的代码是这个,我目前通过USB音频接口连接了一个kewboard:我已经发出信号了,已经在其他程序上尝试过了..

my current code is this, and I currently connected a kewboard via a USB audio interface: I've got signal, already tried with other programs.. So:

window.AudioContext = window.AudioContext || window.webkitAudioContext;

window.onload = function(){

  var audioContext = new AudioContext();
  var analyser = audioContext.createAnalyser();

  navigator.mediaDevices.getUserMedia({ audio:true, video:false }).then(function(stream){ //MediaStream

    var source = audioContext.createMediaStreamSource(stream);
    source.connect(analyser);
    analyser.connect(audioContext.destination);

    analyser.fftSize = 2048;
    var bufferLength = analyser.frequencyBinCount;
    var dataArray = new Uint8Array(bufferLength);

    function listen(){
      requestAnimationFrame(listen);
      analyser.getByteTimeDomainData(dataArray);
      $('.monitor').html(JSON.stringify(dataArray));
    }

    listen();

  });
}

当我弹奏键盘时,dataArray完全没有改变.为什么?我是新手,所以可能我做错了事...

While I'm playing my keyboard, the dataArray doesn't change at all. Why? I'm new to this things so probably I'm doing something wrong...

推荐答案

现在可以使用了.我当前的基本测试代码如下. HTML只能在div.monitor中写东西.目前正在使用Firefox进行测试.我的硬件是键盘>调音台> Behringer UCA222>计算机(USB).弹奏键盘时我得到了数据,现在我很开心.

Ok now it's working. My basic current test code is the following. Html has nothing but a div.monitor to write inside. Currently testing on firefox. My hardware is keyboard > mixer > behringer UCA222 > computer (usb). I get data when playing the keyboard and I'm happy now.

与原始代码有一些区别,但是我认为最重要的是我正在全局保存媒体源(window.audiosource).这里还有有关相关问题的其他帖子,例如:Chrome: onaudioprocess会在一段时间后停止被调用,而此 HTML5在Firefox中5秒后麦克风捕获将停止.

There are several differences from the original code, but I think the most important is that I'm saving the media source globally (window.audiosource). There are other posts here about a related issue, for example this: Chrome: onaudioprocess stops getting called after a while and this HTML5 Microphone capture stops after 5 seconds in Firefox.

window.AudioContext = window.AudioContext || window.webkitAudioContext;

navigator.getUserMedia = (navigator.getUserMedia ||
                          navigator.webkitGetUserMedia ||
                          navigator.mozGetUserMedia ||
                          navigator.msGetUserMedia);

var audioContext = new (window.AudioContext || window.webkitAudioContext)();
var analyser = audioContext.createAnalyser();

if(navigator.getUserMedia){
  navigator.getUserMedia(
    { audio: true }
    ,function(stream){
      window.audiosource = audioContext.createMediaStreamSource(stream);
      audiosource.connect(analyser);
      listen();
    }
    ,function(err){ console.log('The following gUM error occured: ' + err); }
  );
}

function listen(){
  requestAnimationFrame(listen);

  analyser.fftSize = 256;
  var bufferLength = analyser.frequencyBinCount;
  var dataArray = new Uint8Array(bufferLength);
  analyser.getByteTimeDomainData(dataArray);
  $('.monitor').html(JSON.stringify(dataArray));
}

这篇关于Web Audio API流:为什么dataArray不更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆