TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Which library to extract notes from sound?

Forums > Swift

@Arban  

Does anyone have experience with any lib to extract notes from mic in real time? Want to analyse some one playing and compare it to sheet music. Any idea for a good lib or tutorial?

3      

Sounds like @Arban has a noisy problem.

Does anyone have experience with any lib to extract notes from mic in real time?

I hope you didn't ask us to Google this for you?

See (GitHub) -> AudioKit

If AudioKit can't help, then I'm out of ideas! Please return here and let us know how you solved this!

3      

Determining the varying musical pitch of an audio stream has more steps than you probably expected.

Unless you can find a more specific third party library, you can use an audio analysis function called FFT (Fast Fourier Transform) to measure the frequency (pitch) of an audio signal. The AudioKit referenced by @Obelix includes an FFT function. You also can use Apple’s own FFT tool called vDSP.FFT, which is part of the vDSP framework.

An FFT outputs a array that is a histogram of the spectral components of the input audio signal. The array element having the largest magnitude value tells you the fundamental frequency of the audio signal.

After you determine the frequency of the audio signal, you can determine which frequency of the 12-tone musical scale it is closest to.

Each element of an FFT output array is of type DSPDoubleSplitComplex, a tuple representing the real and imaginary parts of that spectral component. To get the magnitude you must sum the squares of the real and imaginary parts. (No need to take the square root because you’re only looking for the bin having the largest magnitude, not actually measuring the magnitude.)

An FFT operates on a fixed number of input samples, which is called the “length” of the FFT. The frequency resolution — i.e., the width of each histogram increment in Hz — is the audio sampling rate divided by the FFT length. Choose the length “L”so that the resolution is less than the pitch difference between the two lowest notes that you will be evaluating.

To create a “real time FFT” that periodically reports the frequency of a real time audio input stream, you need to periodically re-run the FFT function on a sliding selection (“window”) of the L most recent audio samples.

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.