Test if mic is sending any input? (Linux/OSX)

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Monado_III, Nov 24, 2015.

  1. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    637
    352
    Feb 8, 2015
    Canada
    /dev/null
    I'm writing a program that will, when I hook up my electronic drum into my microphone jack, count how many times I've hit a drum and print it to the screen, replacing the previous number. I have hopefully most of the code done, but I need help figuring out a way to test if any input is being sent in from the mic or if it's all blank. A bash script would be preferable but any way would work as long as it returns either 1 or 0 and can be launched from inside the program (using system() etc.). Anyone know of a way of going about this? I can post what I currently have if it's of any interest to you.

    edit: 400th post!
     
    Last edited by Monado_III, Nov 24, 2015
  2. spoonm

    spoonm Can count to 3.

    Member
    174
    84
    May 20, 2015
    Brazil
    Quick google search brought me this: http://stackoverflow.com/questions/13606537/current-level-of-microphone-input
    You can try it. You'll end up with something like this:
    Code:
    arecord -d 1 /tmp/inprec.wav
    sox /tmp/inprec.wav -n stat
    You might have to install sox, and I don't know how to help you if the target system doesn't run alsa.
    To get the boolean return you want from a bash script, you can check of a null mean amplitude. I'm not sure if you want to limit how long a drumbeat can last, so this is certainly not the right approach. Might still help, though. Here's a scrot with what I tested:

    [​IMG]

    You can use the mean amplitude to tell if any input was given during the recording. Compare that to 0, if true yay, else... something's there!

    Note that I don't think ALSA is what's used in OSX and though your default shell is bash, other people could be using fish, zsh, etc.
    And go on, post it, I'm interested. :3
     
    Monado_III likes this.
  3. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    637
    352
    Feb 8, 2015
    Canada
    /dev/null
    thanks, I have no clue how I didn't find that page. And ALSA isn't used in OSX (ALSA=Advanced Linux Sound Architecture)