Recognition of key of sound

I have a guitar and I need my computer to find out which note is playing, recognizing the tone. Can this be done in python, also possible with pygame? Being able to do this in pygame would be very helpful.

+17
python audio
Nov 25 '09 at 15:19
source share
5 answers

To recognize the frequency of the audio signal, you must use the FFT ( Fast Fourier Transform ) algorithm. As far as I can tell, PyGame does not have the ability to record audio and does not support FFT conversion.

First, you need to capture raw sampled data from the sound card; this kind of data is called PCM (Pulse Code Modulation). The easiest way to capture audio in Python is through the PyAudio library (Python bindings to PortAudio). GStreamer can also do this, it is probably redundant for your purposes. Capturing 16-bit samples at 48,000 Hz is pretty typical and probably the best normal sound card will give you.

Once you have the original PCM audio data, you can use the fftpack module from the scipy library to run samples using the FFT transform.This will give you the frequency distribution of the analyzed audio signal, i.e. how strong the signal is in certain frequency bands. Then, it is a matter of finding the frequency that has the strongest signal.

You may need additional filtering to avoid harmonic frequencies. I'm not sure.

+18
Nov 25 '09 at 15:27
source share

Once I wrote a utility that does just that - it analyzes what sounds are played.

You can see the code here (or you can download the entire project, integrated with Frets On Fire, a guitar hero, an open source clone to create a real guitar hero). It was tested using guitars, harmonics and whistles :) The code is ugly, but it works :)

I used pymedia for recording and scipy for FFT.

With the exception of the basics that others have already noted, I can give you some tips:

  • If you record from a microphone, there is a lot of noise. You will need to use a lot of trial errors to set thresholds and sound purification methods to make it work. One possible solution is to use an electric guitar and connect its output to the audio input. It worked better for me.
  • In particular, there is a lot of noise around 50 Hz. This is not so bad, but its overtones (see below) are at a frequency of 100 Hz and 150 Hz, and it is close to the guitar G2 and D3 .... As I said, my solution was to switch to an electric guitar .
  • There is a tradeoff between detection speed and accuracy. The more samples you choose, the longer you will need to detect sounds, but you will more accurately determine the exact step. If you really want to make a project out of this, you will probably have to use several time scales.
  • When tones are reproduced, it has overtones . Sometimes, after a few seconds, overtones can be even more powerful than the base tone. If you cannot handle this, your program will think that he heard E2 for a few seconds, and then E3. To overcome this, I used the list of sounds currently being played, and then, while this note or one of its overtones had energy in it, I took on the same note that was played ....
  • It is especially difficult to detect when someone plays the same note 2 (or more) times in a row, because it is difficult to distinguish between this and random fluctuations in sound level. You will see in my code that I had to use a constant that had to be adjusted in accordance with the guitar used (apparently, each guitar has its own character of power fluctuations).
+19
Nov 25 '09 at 16:58
source share

You will need to use an audio library such as the built-in audioop .

Analysis of the specific note that is played is not trivial, but can be done using these APIs.

May also be useful: http://wiki.python.org/moin/PythonInMusic

+1
Nov 25 '09 at 15:24
source share

Very similar questions:

  • Audio Processing - Key Recognition
  • Real-time pitch detection
  • Real-time pitch detection using FFT

Including sound in a sequence of notes is not easy, especially with multiple notes at once. Read Google’s results for “frequency estimation” and “note recognition”.

I have some examples of estimating the frequency of Python , but this is only part of what you need to decide in order to get notes from guitar notes.

+1
Mar 14 '10 at 22:16
source share



All Articles