Advertisement
Guest User

Untitled

a guest
Oct 14th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import sys
  2. from threading import Thread
  3.  
  4.  
  5. def PlaySoundWin(fname):
  6.     import winsound
  7.     import sys
  8.    
  9.     winsound.PlaySound(fname, winsound.SND_FILENAME)
  10.  
  11.  
  12. ##def PlaySoundOSS(fname):
  13. ##    import wave
  14. ##    import ossaudiodev
  15. ##
  16. ##    s = wave.open(fname,'rb')
  17. ##    (nc,sw,fr,nf,comptype, compname) = s.getparams( )
  18. ##    dsp = ossaudiodev.open('/dev/dsp','w')
  19. ##    try:
  20. ##        from ossaudiodev import AFMT_S16_NE
  21. ##    except ImportError:
  22. ##        if byteorder == "little":
  23. ##            AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
  24. ##        else:
  25. ##            AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
  26. ##    dsp.setparameters(AFMT_S16_NE, nc, fr)
  27. ##    data = s.readframes(nf)
  28. ##    s.close()
  29. ##    dsp.write(data)
  30. ##    dsp.close()
  31.  
  32.  
  33. def PlaySoundAlsa(fname):    
  34.     import wave
  35.     import alsaaudio
  36.  
  37.     f = wave.open(fname, 'rb')
  38.     device = alsaaudio.PCM(card='default')
  39.    
  40.     device.setchannels(f.getnchannels())
  41.     device.setrate(f.getframerate())
  42.  
  43.     if f.getsampwidth() == 1:
  44.         device.setformat(alsaaudio.PCM_FORMAT_U8)
  45.     elif f.getsampwidth() == 2:
  46.         device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
  47.     elif f.getsampwidth() == 3:
  48.         device.setformat(alsaaudio.PCM_FORMAT_S24_LE)
  49.     elif f.getsampwidth() == 4:
  50.         device.setformat(alsaaudio.PCM_FORMAT_S32_LE)
  51.     else:
  52.         raise ValueError('Unsupported format')
  53.  
  54.     device.setperiodsize(320)
  55.    
  56.     data = f.readframes(320)
  57.     while data:
  58.         device.write(data)
  59.         data = f.readframes(320)
  60.     f.close()
  61.  
  62.  
  63. def PlaySound(fname):
  64.     if sys.platform in ('win32', 'cygwin'):
  65.         PlaySoundWin(fname)
  66.     else:
  67.         PlaySoundAlsa(fname)
  68.        
  69.  
  70. def PlaySoundAsync(fname):
  71.     pt = Thread(target=PlaySound, args=(fname,))
  72.     pt.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement