【问题标题】:Not getting frequency values in webaudio-api没有在 webaudio-api 中获取频率值
【发布时间】:2017-04-22 09:47:26
【问题描述】:

我正在使用来自 github 的这个 spectrogram.js 来绘制频谱图并实时获取频率值。 Github Repo

我已经编写了这个额外的 stopSong 函数:

function stopSong() {
 var analyser = audioContext.createAnalyser();
 var ctx = new AudioContext();
  var osc = ctx.createOscillator();
  osc.connect(ctx.destination);
  osc.start(0);



spectro.stop();
 var freqData= new Uint8Array(analyser.frequencyBinCount);
 analyser.getByteFrequencyData(freqData);
 //var f = Math.round(freqData[1]);
 //  var text = f + ' Hz';
  var idx = 0;
                for (var j=0; j < analyser.frequencyBinCount; j++) {
                    if (freqData[j] > freqData[idx]) {
                        idx = j;
                    }
                }
   var frequency = idx * ctx.sampleRate / analyser.fftSize;
                console.log(frequency);



  //document.getElementById("frec").innerHTML = text;


             }

但每次我运行它时,它都会给出 0 作为输出。谁能告诉我的代码有什么问题。

【问题讨论】:

    标签: web-audio-api frequency-analysis


    【解决方案1】:

    您需要将oscillator 连接到analyser

    oscillator.connect(analyser);

    您也可能想多次致电getByteFrequencyData,可能是requestAnimationFrame,或者类似setTimeout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多