implementing FftPitchDetector in C#

爷,独闯天下 提交于 2019-12-25 02:39:05

问题


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

My code:

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;
        }

Which parameters I should put inside PitchDetect.DetectPitch(XXXXXX, XXXXXXXXXXX); ??

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

Thank you!


回答1:


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.



来源:https://stackoverflow.com/questions/15009084/implementing-fftpitchdetector-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!