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).
Ofri Raviv Nov 25 '09 at 16:58 2009-11-25 16:58
source share