在C#中实现FftPitchDetector [英] implementing FftPitchDetector in C#

查看:160
本文介绍了在C#中实现FftPitchDetector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将FftPitchDetector.cs添加到我的项目中,但是我不确定如何使用它.

I've added FftPitchDetector.cs into my project, but I'm not sure how to use it.

我的代码:

private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
        {

            if (waveWriter == null) return;

            byte[] buffer = e.Buffer;

            float sample32 = 0;
            int bytesRecorded = e.BytesRecorded;
            float[] floats = new float[buffer.Length];


            waveWriter.Write(buffer, 0, bytesRecorded);

            for (int index = 0; index < e.BytesRecorded; index += 2)
            {

                short sample = (short)((buffer[index + 1] << 8) |
                                        buffer[index + 0]);
                sample32 = sample / 32768f;
                sampleAggregator.Add(sample32);
            }
            floats = bytesToFloats(buffer);

            FftPitchDetector PitchDetect = new FftPitchDetector(sample32);
            **PitchDetect.DetectPitch(XXXXXX, XXXXXXXXXXX);**

           }

        private static float[] bytesToFloats(byte[] bytes)
        {
            float[] floats = new float[bytes.Length / 2];
            for (int i = 0; i < bytes.Length; i += 2)
            {
                floats[i / 2] = bytes[i] | (bytes[i + 1] << 8);
            }

            return floats;
        }

我应该在 PitchDetect.DetectPitch(XXXXXX,XXXXXXXXXXX); ??

如何使用FftPitchDetector.cs获取输入频率?

How can I get the input frequency using FftPitchDetector.cs?

谢谢!

推荐答案

我写了一篇附带的文章,解释了此代码的工作原理,可以访问

I have written an accompanying article, explaining how this code works, which can be accessed here. Basically, you are passing in an array of samples, and a number indicating how many samples are in that array (in case it is not the same as the length of the array). It returns the frequency in Hz. However, remember that this code is simply trying to select a musical note so that it can work out how much to pitch shift by for an auto-tune effect, so it is only looking for values in a certain range, and may not actually return the loudest frequency in the incoming signal.

这篇关于在C#中实现FftPitchDetector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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