Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Reading microphone level (Python or command line)

  1. #1
    Join Date
    May 2006
    Beans
    15
    Distro
    Ubuntu 8.04 Hardy Heron

    Question Reading microphone level (Python or command line)

    To use my microphone as a sensor for the Player Robotics Device I need to get the value of the current volume, frequency or whatever as float but can't find a way to do that. Does anyone know either a python module or a command line program a similar output? Has anyone worked with sndpeek (http://www.cs.princeton.edu/sound/software/sndpeek/) and could that help?

  2. #2
    Join Date
    Aug 2006
    Beans
    366

    Re: Reading microphone level (Python or command line)

    volume.py, an adesklet, imports ossaudiodev to do that. The program is fairly straight forward.
    http://adesklets.sourceforge.net/desklets.html
    Linux Counter entry # 99383 (since 1995), Feisty Xbuntu 64 bit
    Folders! We don't need no stinking folders. "I don't have anything on my machine that needs folding" -- Unknown

  3. #3
    Join Date
    May 2006
    Beans
    15
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Reading microphone level (Python or command line)

    Thanks for the answer. I looked into it but as far as I understand it it just displays the current setting of the system volume. I'm looking for a way to get the current volume in the room.

  4. #4
    Join Date
    Aug 2006
    Beans
    366

    Re: Reading microphone level (Python or command line)

    Mine is several year old, but line 213 in mine starts with
    # Get the current system volume
    def get_volume(self) :
    Linux Counter entry # 99383 (since 1995), Feisty Xbuntu 64 bit
    Folders! We don't need no stinking folders. "I don't have anything on my machine that needs folding" -- Unknown

  5. #5
    Join Date
    May 2006
    Beans
    15
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Reading microphone level (Python or command line)

    Jep. But that just gives you, well, the current system volume which is (I tried it) the master volume set via the volume control on the gnome-panel or alsamixer.

  6. #6
    Join Date
    Mar 2007
    Beans
    53

    Re: Reading microphone level (Python or command line)

    Code:
    #!/usr/bin/python
    ## This is an example of a simple sound capture script.
    ##
    ## The script opens an ALSA pcm for sound capture. Set
    ## various attributes of the capture, and reads in a loop,
    ## Then prints the volume.
    ##
    ## To test it out, run it and shout at your microphone:
    
    import alsaaudio, time, audioop
    
    # Open the device in nonblocking capture mode. The last argument could
    # just as well have been zero for blocking mode. Then we could have
    # left out the sleep call in the bottom of the loop
    inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
    
    # Set attributes: Mono, 8000 Hz, 16 bit little endian samples
    inp.setchannels(1)
    inp.setrate(8000)
    inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
    
    # The period size controls the internal number of frames per period.
    # The significance of this parameter is documented in the ALSA api.
    # For our purposes, it is suficcient to know that reads from the device
    # will return this many frames. Each frame being 2 bytes long.
    # This means that the reads below will return either 320 bytes of data
    # or 0 bytes of data. The latter is possible because we are in nonblocking
    # mode.
    inp.setperiodsize(160)
    
    while True:
    	# Read data from device
    	l,data = inp.read()
    	if l:
    		# Return the maximum of the absolute value of all samples in a fragment.
    		print audioop.max(data, 2)
    	time.sleep(.001)
    Enjoy!
    Last edited by CShadowRun; January 9th, 2009 at 10:58 PM.
    I am now known as azelphur.

  7. #7
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: Reading microphone level (Python or command line)

    Dear OP, for both of your data (frequency and volume) you will actually need to interpret concrete sample data you captured from your audio-in device.

    For getting the volume, the result heavily depends on your definition of "volume". The post above uses the most simple one, there are also others which measure the energy of the waveform, etc.

    Getting the frequency is actually not simple at all. There might be libraries who attempt to do this, but don't expect too much!

  8. #8
    Join Date
    Sep 2006
    Beans
    11
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Reading microphone level (Python or command line)

    Quote Originally Posted by CShadowRun View Post
    Code:
    #!/usr/bin/python
    ## This is an example of a simple sound capture script.
    ##
    ## The script opens an ALSA pcm for sound capture. Set
    ## various attributes of the capture, and reads in a loop,
    ## Then prints the volume.
    ##
    ## To test it out, run it and shout at your microphone:
    
    import alsaaudio, time, audioop
    
    # Open the device in nonblocking capture mode. The last argument could
    # just as well have been zero for blocking mode. Then we could have
    # left out the sleep call in the bottom of the loop
    inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
    
    # Set attributes: Mono, 8000 Hz, 16 bit little endian samples
    inp.setchannels(1)
    inp.setrate(8000)
    inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
    
    # The period size controls the internal number of frames per period.
    # The significance of this parameter is documented in the ALSA api.
    # For our purposes, it is suficcient to know that reads from the device
    # will return this many frames. Each frame being 2 bytes long.
    # This means that the reads below will return either 320 bytes of data
    # or 0 bytes of data. The latter is possible because we are in nonblocking
    # mode.
    inp.setperiodsize(160)
    
    while True:
    	# Read data from device
    	l,data = inp.read()
    	if l:
    		# Return the maximum of the absolute value of all samples in a fragment.
    		print audioop.max(data, 2)
    	time.sleep(.001)
    Enjoy!
    Great! This is exactly what I've been looking for too. I was about to hack apart sndpeek and use SWIG to pythonize it, but this is far better.

  9. #9
    Join Date
    May 2009
    Beans
    2

    Re: Reading microphone level (Python or command line)

    very thanks
    but there is a problem i really faced


    Traceback (most recent call last):
    File "C:\Users\TOSHIBA\Desktop\voice.py", line 10, in <module>
    import alsaaudio, time, audioop
    ImportError: No module named alsaaudio

    how can i solve this
    would somebody help me
    thanks any way.

  10. #10
    Join Date
    Mar 2007
    Beans
    53

    Re: Reading microphone level (Python or command line)

    Quote Originally Posted by short_cake View Post
    very thanks
    but there is a problem i really faced


    Traceback (most recent call last):
    File "C:\Users\TOSHIBA\Desktop\voice.py", line 10, in <module>
    import alsaaudio, time, audioop
    ImportError: No module named alsaaudio

    how can i solve this
    would somebody help me
    thanks any way.
    Your on windows, this is a ubuntu support forum! This code will not work on windows.
    Last edited by CShadowRun; May 11th, 2009 at 09:47 PM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •