Advertisement
jsbsan

escuchador

Feb 12th, 2014
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. #!/usr/bin/python
  2. ## This is an example of a simple sound capture script.
  3. ##
  4. ## The script opens an ALSA pcm for sound capture. Set
  5. ## various attributes of the capture, and reads in a loop,
  6. ## Then prints the volume.
  7. ##
  8. ## To test it out, run it and shout at your microphone:
  9.  
  10. import alsaaudio, time, audioop
  11.  
  12. # Open the device in nonblocking capture mode. The last argument could
  13. # just as well have been zero for blocking mode. Then we could have
  14. # left out the sleep call in the bottom of the loop
  15. inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
  16.  
  17. # Set attributes: Mono, 8000 Hz, 16 bit little endian samples
  18. inp.setchannels(1)
  19. inp.setrate(8000)
  20. inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
  21.  
  22. # The period size controls the internal number of frames per period.
  23. # The significance of this parameter is documented in the ALSA api.
  24. # For our purposes, it is suficcient to know that reads from the device
  25. # will return this many frames. Each frame being 2 bytes long.
  26. # This means that the reads below will return either 320 bytes of data
  27. # or 0 bytes of data. The latter is possible because we are in nonblocking
  28. # mode.
  29. # importante!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  30. # necesario instalar
  31. # sudo apt-get install python-alsaaudio
  32. inp.setperiodsize(160)
  33. lista=[1]
  34. controlsalida= True
  35. contador=0
  36. for i in range(1,100):
  37.     contador=contador+1
  38.     # Read data from device
  39.     l,data = inp.read()
  40.     if l:
  41.         # Return the maximum of the absolute value of all samples in a fragment.
  42.         #print audioop.max(data, 2)
  43.         lista.append(audioop.max(data, 2))
  44.         time.sleep(.01)
  45.         if contador==100:
  46.             controlsalida=False
  47.             break
  48.     else:
  49.         time.sleep(.01)
  50.  
  51. suma=0
  52. media=0
  53. contador=0
  54. for i in lista:
  55.     contador=contador+1
  56.     suma+=i
  57. media=suma/contador
  58.  
  59. txt=str(media)
  60. f=open("/tmp/DatosSonido.txt","w")
  61. f.write(txt)
  62. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement