implementing FftPitchDetector in C#
Asked Answered
V

1

0

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!

Vallo answered 21/2, 2013 at 17:55 Comment(2)
FftPitchDetector.cs from where? Care to provide a link?Oysterman
hg01.codeplex.com/voicerecorder/rev/bb6af2fb777c 2.1 until 2.53Vallo
U
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.

Unapt answered 22/2, 2013 at 8:12 Comment(3)
I have tried to do this: **PitchDetect.DetectPitch(floats, 8820); error : IndexOutOfRangeException pointing this: fftBuffer[p1++] = fftBuffer[p2]; in SmbPitchShift.cs AND tried this : PitchDetect.DetectPitch(floats, 8821); error: IndexOutOfRangeException pointing this : fftBuffer[n * 2] = buffer[n-inFrames] * window(n, frames); in FftPitchDetector.cs How to solve this problem?Vallo
for the FFT, the number of samples you pass in must be a power of two. That is one of the jobs the SampleAggregator is fulfilling - batching things up into 1024 samples at a timeUnapt
I can't get the picture about what you've explained, do you mind to code some examples and explain from the code? It could be better if you modify from the code above. I'm new to audio programming, and I have limited time to do this as my project is needed to pass up very soon. I wish to know more on how to use it instead of how it works on my current situation. I will further understand this after submit my project, because I found this all is so interesting, but still, I'll need to submit my project on time. Sorry for any inconvenience.. Btw, you are so cool! :DVallo

© 2022 - 2024 — McMap. All rights reserved.