Author Topic: Detecting the note/key/frequency/pitch  (Read 7840 times)

mmutee

  • Posts: 7
Detecting the note/key/frequency/pitch
« on: 29 Mar '08 - 16:48 »
Hi, I am very new here. Been trolling the forums here for a month or so, gathering info. I love Bass, I tried Fmod for a while but found it to be a bit unstable at times. I program in VB6.

My question (first of many, I am afraid!)

Is it possible to find the musical note of what is being played at the moment? Like if I get the FFT data, will I be able find the frequency? Or is there another way?

I did search the forum but didn't find anything, perhaps I am not using the right keywords.

I am trying to build an application which detects musical key of start and end of audio files, so I can use those files to make mixes.
« Last Edit: 29 Mar '08 - 16:54 by mmutee »

mmutee

  • Posts: 7
Request
« Reply #1 on: 11 Apr '08 - 16:42 »
Is it possible for someone to point me to the right direction?

zydu

  • Posts: 14
Re: Detecting the note/key/frequency/pitch
« Reply #2 on: 11 Apr '08 - 17:39 »
I am struggling with similar problem, there have been several threads about it but none has really helped me.

I would suggest reading following articles :
"Parallel Processing Techniques for Estimating Pitch Periods of Speech in the Time Domain" - Rabines-Gold algorithm is explained here
"Signal Processing for Melody Transcription" by J. McNab

Simplest way wound be using DetectPeakFrequency() method from Visuals class to get frequency which has highest energy, but unfortunately resolution would be fairly low ( on bass guitar I am getting like 4 semitone resolution which is unacceptable) and you have to manage many possible errors like octave errors for example. Also u can use Channel_GetData() method to perform FFT and then maybe implement your own algorithm for estimating pitch.

Assigning note to pitch doesn't seem to be a problem if you actually manage to detect pitch correctly. It is explained in the second article, I mentioned.

good luck

Mike



mmutee

  • Posts: 7
Re: Detecting the note/key/frequency/pitch
« Reply #3 on: 13 Apr '08 - 15:19 »
Thanks for the reply Zydu. I didnt realize that this is a very undeveloped field. I expected a ready made solution or something, looks like, for once, I'll have to do the work myself :)

The function you mentioned, DetectPeakFrequency(), where can I find it? I use Visual Basic 6, and dont see it anywhere.
Does it return the frequency with highest amplitude in an FFT data array?

If I may ask, what do you need pitch detection for? I wanted to build an automatic music mixing program. The practical solution for my problem would be different from yours if you want to do vocal pitch detection or a tuner application.
« Last Edit: 13 Apr '08 - 15:23 by mmutee »

zydu

  • Posts: 14
Re: Detecting the note/key/frequency/pitch
« Reply #4 on: 13 Apr '08 - 16:40 »
The function you mentioned, DetectPeakFrequency(), where can I find it? I use Visual Basic 6, and dont see it anywhere.
Does it return the frequency with highest amplitude in an FFT data array?
Un4seen.Bass.Misc.Visuals.DetectPeakFrequency() is the method I mentioned and yes, it returns frequency with highest energy in FFT data array.

If I may ask, what do you need pitch detection for? I wanted to build an automatic music mixing program. The practical solution for my problem would be different from yours if you want to do vocal pitch detection or a tuner application.
I am doing research on generating drum patterns for given melody. I need pitch for audio transcription.

Regards

Mike

mmutee

  • Posts: 7
Re: Detecting the note/key/frequency/pitch
« Reply #5 on: 13 Apr '08 - 19:32 »
Far more complicated than my problem :)

I guess you would detect changes in notes and look for patterns so you could create a metronome? How would it handle bass guitar playing a funk riff? :) Shortest distance between two notes could give you a part of tempo though, like 1/4th or 1/8th etc.

BTW, what FFT size you find more suited for this stuff?
« Last Edit: 13 Apr '08 - 21:03 by mmutee »

Ian @ un4seen

  • Administrator
  • Posts: 20437
Re: Detecting the note/key/frequency/pitch
« Reply #6 on: 15 Apr '08 - 13:16 »
Simplest way wound be using DetectPeakFrequency() method from Visuals class to get frequency which has highest energy, but unfortunately resolution would be fairly low...

If a sound's frequency falls between 2 bins, its energy will be split between them. So, the accuracy can be improved by taking account of the adjoining bins, eg. the ratio. Here's a simple method that I've used before, which should generally detect a tone to within a 100th of a hertz...

Code: [Select]
float GetPeakFreq(DWORD channel)
{
float fft[2048];
BASS_ChannelGetData(channel, fft, BASS_DATA_FFT4096); // 4096 sample FFT
float peak=0; // could set a threshold level to ignore noise
int peaki=0;
for (int a=2; a<2047; a++)
if (peak<fft[a]) { // found peak
peak=fft[a];
peaki=a;
}
if (!peaki) return 0; // no sound
peak=peaki+0.8721*sin((fft[peaki+1]-fft[peaki-1])/fft[peaki]*0.7632); // tweak the bin
BASS_CHANNELINFO ci;
BASS_ChannelGetInfo(channel, &ci);
return peak*ci.freq/4096; // translate bin to frequency
}

BTW, what FFT size you find more suited for this stuff?

The larger the FFT, the greater the frequency resolution/accuracy will be.

PatriciaMac

  • Posts: 40
Re: Detecting the note/key/frequency/pitch
« Reply #7 on: 17 Jun '13 - 11:54 »
I am actually building a guitar tuner too, and if it works fine when the sound is loud, i am having some serious noise, when i don't play anything, i get "crazy values" ( when it's quiet in the room )

the Hz is only valid for a couple of seconds after the note impulse on the guitar.

Is there a way to have "0" when it's just ambiant noise ?

pat.

Ian @ un4seen

  • Administrator
  • Posts: 20437
Re: Detecting the note/key/frequency/pitch
« Reply #8 on: 17 Jun '13 - 17:09 »
As noted in the code above, the "peak" variable could be initialized with a threshold level (rather than 0), to have anything below that ignored. You could try experimenting with that to find a level that works well for your environment. A more advanced solution could be to get FFT readings of the background noise, and then subtract that from the live sound's FFT readings.

Here's another thread related to pitch detection that you may also find useful...

   www.un4seen.com/forum/?topic=13623

PatriciaMac

  • Posts: 40
Re: Detecting the note/key/frequency/pitch
« Reply #9 on: 17 Jun '13 - 18:11 »
Thanks a lot, the code works great with my voice, but as soon as i use a guitar, it doesn't seem to capture the sound very well, i get wrong results ( on an ipad )

any ideas why ?

Pat.

Ian @ un4seen

  • Administrator
  • Posts: 20437
Re: Detecting the note/key/frequency/pitch
« Reply #10 on: 18 Jun '13 - 15:00 »
If you haven't already done so, please try the code in the linked thread above. That looks for harmonics, which should help to more reliably detect a guitar sound.