Search code examples
pythonaudiofftfrequencypyaudio

Check for a specific sound (input: microphone)


My problem: I currently have a sound file containing a specific sound I recorded. I want to be able to recognize when that sound is played again for over like 2 seconds. The volume does not matter to me, I want to be able to recognize when that specific note is played. For example the file holds a recording of the note A (la), and if i play the note A on a piano next to the microphone, the raspberry pi will print "correct" or something. I am having trouble recognizing the note, and previous research has suggested finding the frequency / using FFT function but i have been unable to figure it out. Do you recommend any libraries I should use in order to implement this?

Ideally I would be able to identify the pitch of an external sound. As soon as I have the pitch I would be able to check it between a range of frequencies.


Solution

  • You indeed want to use something like FFT, which both numpy and scipy offer. The idea would be that you collect a buffer of your microphone input, apply the FFT on it, then you would try and find if the most powerful frequency is that of the note you're looking for. There exists tables that can tell you what the frequency of each note is.

    You're essentially making a spectrogram.

    If you want an order of operations:

    1. Building frequency scale:
      1. Determine frequency scale using np.fft.fftfreq (N being the same length as your buffer)
    2. Build table of notes
      1. Establish what frequency belongs to what note (use a reference)
      2. Determine a margin of error
    3. Identifying notes (This part is in a loop)

      1. Collect signal in a buffer of select size

      2. Apply FFT

      3. Find highest value in frequency domain

      4. Look for corresponding note within a range of error in lookup table

    Useful functions:

    Numpy FFT

    Numpy FFTFREQ

    Numpy ARGMAX

    Other helpful questions:

    Maintain a streaming microphone input in Python