Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

record / playback

By: a guest on Aug 17th, 2011  |  syntax: Python  |  size: 1.23 KB  |  views: 306  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. import alsaaudio, sys
  3. import time
  4.  
  5. channels = 1
  6. sample_size = 1
  7. frame_size = channels * sample_size
  8. frame_rate = 44100
  9. byte_rate = frame_rate * frame_size
  10.  
  11. #******************************************************************
  12. #******************************************************************
  13. #               OUTPUT                                       *
  14. #period size controls internal number of frames per period
  15. period_size = 160
  16.  
  17. out = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NORMAL)
  18.  
  19. out.setchannels(channels)
  20. out.setformat(alsaaudio.PCM_FORMAT_S16_LE)
  21. out.setrate(frame_rate)
  22. out.setperiodsize(period_size)
  23.  
  24. #******************************************************************
  25. #******************************************************************
  26. #                       INPUT                                   *
  27.  
  28. inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL)
  29.  
  30. inp.setchannels(channels)
  31. inp.setrate(frame_rate)
  32. inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)#16 bit little endian
  33. inp.setperiodsize(period_size)
  34.  
  35. #******************************************************************
  36.  
  37. def main():
  38.        
  39.         while True:
  40.                 l, data = inp.read()#l seems to always be 940
  41.                 if l:
  42.                         #time.sleep(.1)
  43.                         out.write(data)
  44.        
  45.                 time.sleep(.001)
  46.         return
  47.        
  48. if __name__ == '__main__':
  49.         main()
clone this paste RAW Paste Data