Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from threading import Thread
- def PlaySoundWin(fname):
- import winsound
- import sys
- winsound.PlaySound(fname, winsound.SND_FILENAME)
- ##def PlaySoundOSS(fname):
- ## import wave
- ## import ossaudiodev
- ##
- ## s = wave.open(fname,'rb')
- ## (nc,sw,fr,nf,comptype, compname) = s.getparams( )
- ## dsp = ossaudiodev.open('/dev/dsp','w')
- ## try:
- ## from ossaudiodev import AFMT_S16_NE
- ## except ImportError:
- ## if byteorder == "little":
- ## AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
- ## else:
- ## AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
- ## dsp.setparameters(AFMT_S16_NE, nc, fr)
- ## data = s.readframes(nf)
- ## s.close()
- ## dsp.write(data)
- ## dsp.close()
- def PlaySoundAlsa(fname):
- import wave
- import alsaaudio
- f = wave.open(fname, 'rb')
- device = alsaaudio.PCM(card='default')
- device.setchannels(f.getnchannels())
- device.setrate(f.getframerate())
- if f.getsampwidth() == 1:
- device.setformat(alsaaudio.PCM_FORMAT_U8)
- elif f.getsampwidth() == 2:
- device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
- elif f.getsampwidth() == 3:
- device.setformat(alsaaudio.PCM_FORMAT_S24_LE)
- elif f.getsampwidth() == 4:
- device.setformat(alsaaudio.PCM_FORMAT_S32_LE)
- else:
- raise ValueError('Unsupported format')
- device.setperiodsize(320)
- data = f.readframes(320)
- while data:
- device.write(data)
- data = f.readframes(320)
- f.close()
- def PlaySound(fname):
- if sys.platform in ('win32', 'cygwin'):
- PlaySoundWin(fname)
- else:
- PlaySoundAlsa(fname)
- def PlaySoundAsync(fname):
- pt = Thread(target=PlaySound, args=(fname,))
- pt.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement