如何在javascript中获取频率值? [英] how to get frequency value in javascript?

查看:301
本文介绍了如何在javascript中获取频率值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是意大利学生,我正在使用p5的库创建一个网络吉他调音器。
我想知道是否有办法获得麦克风输入的频率。

I’m an italian student, and I’m using p5’s libraries to create a web guitar tuner. I would like to know if there is a way to get the frequency of the microphone input.

库链接: https://p5js.org/reference/#/libraries/p5.sound

接受使用不同库的不同解决方案

Different solution with different libraries are accepted

谢谢

推荐答案

如果你使用P5,建立这个例子。声音的FFT对象,你可以调用 fft.analyze()来获得一个振荡数组,分成不同的频率区。

Building off of this example, if you use P5.sound's FFT object, you can call fft.analyze() to get an array of amplitudes divided up into different frequency bins.

默认值为1024个bin。这些频段均匀地从0Hz到奈奎斯特频率间隔,这是您的音频采样率的一半。 (默认采样率为44100Hz,因此默认的奈奎斯特频率为22050Hz)。

The default is 1024 bins. These bins are evenly spaced from 0Hz to the Nyquist frequency, which is half your audio sample rate. (The default sample rate is 44100Hz, so the default Nyquist frequency is 22050Hz).

另请注意,每个箱的默认分辨率为22050Hz / 1024箱≈21.53Hz/完事。如果你使用P5.sound,你将获得最高21.53Hz的精度(假设你的计算机设置方式与我的相同)。

Note also that each bin has a default resolution of 22050Hz / 1024 bins ≈ 21.53Hz/bin. You're going to get at best a 21.53Hz precision if you use P5.sound (assuming your computer is set up the same way mine is).

所以,bin 0(或 spectrum [0] )包含频率为~0 - 21.5Hz,bin 1( spectrum [1] )包含频率为~21.5Hz - 43.0Hz等的幅度。

So, bin 0 (or spectrum[0]) contains the amplitude at frequencies ~ 0 - 21.5Hz, bin 1 (spectrum[1]) contains the amplitude at frequencies ~ 21.5Hz - 43.0Hz, et cetera.

这意味着任何给定bin的频率为: freq = binIndex *(nyquist / numberOfBins)

This means the frequency at any given bin is: freq = binIndex * (nyquist / numberOfBins).

如果这对您来说足够好,您可以遍历<$ c $返回的数组c> fft.analyze(),找到峰值的索引,然后使用奈奎斯特与数组长度(箱子)的比率将该索引转换回频率。

If that's good enough for you, you can loop through the array returned by fft.analyze(), find the index of the peak, and then convert that index back to a frequency using the ratio of Nyquist to array length (bins).

function getLoudestFrequency() {
    var nyquist = sampleRate() / 2; // 22050
    var spectrum = fft.analyze(); // array of amplitudes in bins
    var numberOfBins = spectrum.length;
    var maxAmp = 0;
    var largestBin;

    for (var i = 0; i < numberOfBins; i++) {
        var thisAmp = spectrum[i]; // amplitude of current bin
        if (thisAmp > maxAmp) {
            maxAmp = thisAmp;
            largestBin = i;
        }
    }

    var loudestFreq = largestBin * (nyquist / numberOfBins);
    return loudestFreq;
}

假设±21.53Hz对您来说不够准确。那么......你可以用更多的箱子初始化你的FFT对象: new p5.FFT(0.8,[bins])文档说最大垃圾箱数是1024,但我刚试过8192 (对于2.69Hz的分辨率)它似乎对我来说很好。

Let's say ±21.53Hz isn't accurate enough for you. Well... you can initialize your FFT object with more bins: new p5.FFT(0.8,[bins]). The documentation says that the max number of bins is 1024, but I just tried with 8192 (for resolution of 2.69Hz) and it seemed to work fine for me.

这篇关于如何在javascript中获取频率值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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