def play(self): # 播放文件 f = open(self.file, 'rb') # 打开文件 header = f.read(WAV_HEADER_SIZE) # 读取WAV文件头 (riff, riffsize, wave, fmt, fmtsize, format, nchannels, samplespersecond, datarate, blockalign, bitspersample, data, size) =\ struct.unpack('<4sl4s4slhhllhh4sl', header) # 获取参数值 if riff != 'RIFF' or fmtsize != 16 or fmt != 'fmt ' or data != 'data': # 判断文件格式 raise 'Data Error' wfx = pywintypes.WAVEFORMATEX() # 创建WAVEFORMATEX结构 wfx.wFormatTag = format wfx.nChannels = nchannels wfx.nSamplesPerSec = samplespersecond wfx.nAvgBytesPerSec = datarate wfx.nBlockAlign = blockalign wfx.wBitsPerSample = bitspersample d = directsound.DirectSoundCreate(None, None) # 使用DirectSound播放声音 d.SetCooperativeLevel(None, directsound.DSSCL_PRIORITY) sdesc = directsound.DSBUFFERDESC() sdesc.dwFlags = (directsound.DSBCAPS_STICKYFOCUS | directsound.DSBCAPS_CTRLPOSITIONNOTIFY) sdesc.dwBufferBytes = size sdesc.lpwfxFormat = wfx self.buffer = buffer = d.CreateSoundBuffer(sdesc, None) buffer.Update(0, f.read(size)) buffer.Play(0)
def testCreate(self): """DirectSoundCreate()""" try: d = ds.DirectSoundCreate(None, None) except pythoncom.com_error as exc: if exc.hresult != ds.DSERR_NODRIVER: raise raise TestSkipped(exc)
def play(self): f = open(self.file, 'rb') hdr = f.read(WAV_HEADER_SIZE) wfx, size = self.wav_header_unpack(hdr) d = ds.DirectSoundCreate(None, None) d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY) sdesc = ds.DSBUFFERDESC() sdesc.dwFlags = (ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY) sdesc.dwBufferBytes = size sdesc.lpwfxFormat = wfx self.currentbuffer = buffer = d.CreateSoundBuffer(sdesc, None) buffer.Update(0, f.read(size)) buffer.Play(0)
def add(self): self.file = filedialog.askopenfilename(title='Python DirectSound', filetypes=[('WAV', '*.wav')]) f = open(self.file, 'rb') hdr = f.read(WAV_HEADER_SIZE) wfx, size = self.wav_header_unpack(hdr) d = ds.DirectSoundCreate(None, None) d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY) sdesc = ds.DSBUFFERDESC() sdesc.dwFlags = (ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY) sdesc.dwBufferBytes = size sdesc.lpwfxFormat = wfx self.buffer = buffer = d.CreateSoundBuffer(sdesc, None) buffer.Update(0, f.read(size))
def testPlay(self): """Mesdames et Messieurs, la cour de Devin Dazzle""" # look for the test file in various places candidates = [ os.path.dirname(__file__), os.path.dirname(sys.argv[0]), # relative to 'testall.py' in the win32com test suite. os.path.join( os.path.dirname(sys.argv[0]), "../../win32comext/directsound/test" ), ".", ] for candidate in candidates: fname = os.path.join(candidate, "01-Intro.wav") if os.path.isfile(fname): break else: raise TestSkipped("Can't find test .wav file to play") with open(fname, "rb") as f: hdr = f.read(WAV_HEADER_SIZE) wfx, size = wav_header_unpack(hdr) d = ds.DirectSoundCreate(None, None) d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY) sdesc = ds.DSBUFFERDESC() sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY sdesc.dwBufferBytes = size sdesc.lpwfxFormat = wfx buffer = d.CreateSoundBuffer(sdesc, None) event = win32event.CreateEvent(None, 0, 0, None) notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify) notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event)) buffer.Update(0, f.read(size)) buffer.Play(0) win32event.WaitForSingleObject(event, -1)
def testPlay(self): """Mesdames et Messieurs, la cour de Devin Dazzle""" # relative to 'testall.py' in the win32com test suite. extra = os.path.join( os.path.dirname(sys.argv[0]), "../../win32comext/directsound/test" ) fname = find_test_fixture("01-Intro.wav", extra) with open(fname, "rb") as f: hdr = f.read(WAV_HEADER_SIZE) wfx, size = wav_header_unpack(hdr) try: d = ds.DirectSoundCreate(None, None) except pythoncom.com_error as exc: if exc.hresult != ds.DSERR_NODRIVER: raise raise TestSkipped(exc) d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY) sdesc = ds.DSBUFFERDESC() sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY sdesc.dwBufferBytes = size sdesc.lpwfxFormat = wfx buffer = d.CreateSoundBuffer(sdesc, None) event = win32event.CreateEvent(None, 0, 0, None) notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify) notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event)) buffer.Update(0, f.read(size)) buffer.Play(0) win32event.WaitForSingleObject(event, -1)
def loadwav(wavefname): d = ds.DirectSoundCreate(None, None) d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY) w = wave.open(wavefname, "rb") sdesc = ds.DSBUFFERDESC() sdesc.lpwfxFormat = pywintypes.WAVEFORMATEX() sdesc.lpwfxFormat.wFormatTag = pywintypes.WAVE_FORMAT_PCM sdesc.lpwfxFormat.nChannels = w.getnchannels() sdesc.lpwfxFormat.nSamplesPerSec = w.getframerate() sdesc.lpwfxFormat.nAvgBytesPerSec = w.getnchannels() * w.getsampwidth( ) * w.getframerate() sdesc.lpwfxFormat.nBlockAlign = w.getnchannels() * w.getsampwidth() sdesc.lpwfxFormat.wBitsPerSample = w.getsampwidth() * 8 sdesc.dwBufferBytes = w.getnframes() * w.getnchannels() * w.getsampwidth() sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY buffer = d.CreateSoundBuffer(sdesc, None) buffer.Update(0, w.readframes(w.getnframes())) w.close() return buffer
def testCreate(self): '''DirectSoundCreate()''' d = ds.DirectSoundCreate(None, None)
print("Block align: " + str(blockalign)) wfx.wBitsPerSample = bitspersample print("Bits per sample: " + str(bitspersample)) return wfx, datalength sound_file = sys.argv[1] fname = os.path.join(os.path.dirname(__file__), sound_file) print("You want to play file: " + fname) f = open(fname, 'rb') #read wav header and unpack hdr = f.read(WAV_HEADER_SIZE) wfx, size = wav_header_unpack(hdr) d = ds.DirectSoundCreate(None, None) d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY) sdesc = ds.DSBUFFERDESC() sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY sdesc.dwBufferBytes = size sdesc.lpwfxFormat = wfx buffer = d.CreateSoundBuffer(sdesc, None) event = win32event.CreateEvent(None, 0, 0, None) notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify) notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event)) buffer.Update(0, f.read(size)) buffer.Play(0) win32event.WaitForSingleObject(event, -1)