def seek(self, timestamp): timestamp = min(timestamp, self._duration) if self._duration else timestamp prop = PROPVARIANT() prop.vt = VT_I8 prop.llVal = timestamp_to_wmf(timestamp) pos_com = com.GUID(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) try: self._source_reader.SetCurrentPosition(pos_com, prop) except OSError as err: warnings.warn(str(err)) ole32.PropVariantClear(ctypes.byref(prop))
else: XInputGetBatteryInformation = None # wbemcli ################################################# BSTR = LPCWSTR IWbemContext = c_void_p RPC_C_AUTHN_WINNT = 10 RPC_C_AUTHZ_NONE = 0 RPC_C_AUTHN_LEVEL_CALL = 0x03 RPC_C_IMP_LEVEL_IMPERSONATE = 3 EOAC_NONE = 0 VT_BSTR = 8 CLSID_WbemLocator = com.GUID(0x4590f811, 0x1d3a, 0x11d0, 0x89, 0x1f, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24) IID_IWbemLocator = com.GUID(0xdc12a687, 0x737f, 0x11cf, 0x88, 0x4d, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24) class IWbemClassObject(com.pIUnknown): _methods_ = [ ('GetQualifierSet', com.STDMETHOD()), ('Get', com.STDMETHOD(BSTR, LONG, POINTER(VARIANT), c_void_p, c_void_p)) # ... long, unneeded ] class IEnumWbemClassObject(com.pIUnknown): _methods_ = [('Reset', com.STDMETHOD()),
class PROPERTYKEY(ctypes.Structure): _fields_ = [ ('fmtid', com.GUID), ('pid', DWORD), ] def __repr__(self): return "PROPERTYKEY({}, pid={})".format(self.fmtid, self.pid) REFPROPERTYKEY = PROPERTYKEY PKEY_Device_FriendlyName = PROPERTYKEY( com.GUID(0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0), 14) PKEY_Device_DeviceDesc = PROPERTYKEY( com.GUID(0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0), 2) PKEY_DeviceInterface_FriendlyName = PROPERTYKEY( com.GUID(0x026e516e, 0xb814, 0x414b, 0x83, 0xcd, 0x85, 0x6d, 0x6f, 0xef, 0x48, 0x22), 2) class IPropertyStore(com.pIUnknown): _methods_ = [ ('GetCount', com.STDMETHOD(POINTER(DWORD))), ('GetAt', com.STDMETHOD(DWORD, POINTER(PROPERTYKEY))), ('GetValue', com.STDMETHOD(REFPROPERTYKEY, POINTER(PROPVARIANT))), ('SetValue', com.STDMETHOD()), ('Commit', com.STDMETHOD()),
DIPH_DEVICE = 0 DIPH_BYOFFSET = 1 DIPH_BYID = 2 DIPH_BYUSAGE = 3 DISCL_EXCLUSIVE = 0x00000001 DISCL_NONEXCLUSIVE = 0x00000002 DISCL_FOREGROUND = 0x00000004 DISCL_BACKGROUND = 0x00000008 DISCL_NOWINKEY = 0x00000010 DIPROP_BUFFERSIZE = 1 GUID_XAxis = \ com.GUID(0xA36D02E0,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00) class DIDEVICEINSTANCE(ctypes.Structure): _fields_ = (('dwSize', DWORD), ('guidInstance', com.GUID), ('guidProduct', com.GUID), ('dwDevType', DWORD), ('tszInstanceName', WCHAR * MAX_PATH), ('tszProductName', WCHAR * MAX_PATH), ('guidFFDriver', com.GUID), ('wUsagePage', WORD), ('wUsage', WORD)) LPDIDEVICEINSTANCE = ctypes.POINTER(DIDEVICEINSTANCE) LPDIENUMDEVICESCALLBACK = ctypes.WINFUNCTYPE(BOOL, LPDIDEVICEINSTANCE, LPVOID)
def _load_audio(self, stream=MF_SOURCE_READER_FIRST_AUDIO_STREAM): """ Prepares the audio stream for playback by detecting if it's compressed and attempting to decompress to PCM. Default: Only get the first available audio stream. """ # Will be an audio file. self._audio_stream_index = stream # Get what the native/real media type is (audio only) imfmedia = IMFMediaType() try: self._source_reader.GetNativeMediaType(self._audio_stream_index, 0, ctypes.byref(imfmedia)) except OSError as err: if err.winerror == MF_E_INVALIDSTREAMNUMBER: assert _debug('WMFAudioDecoder: No audio stream found.') return # Get Major media type (Audio, Video, etc) # TODO: Make GUID take no arguments for a null version: guid_audio_type = com.GUID(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) imfmedia.GetGUID(MF_MT_MAJOR_TYPE, ctypes.byref(guid_audio_type)) if guid_audio_type == MFMediaType_Audio: assert _debug('WMFAudioDecoder: Found Audio Stream.') # Deselect any other streams if we don't need them. (Small speedup) if not self.decode_video: self._source_reader.SetStreamSelection( MF_SOURCE_READER_ANY_STREAM, False) # Select first audio stream. self._source_reader.SetStreamSelection( MF_SOURCE_READER_FIRST_AUDIO_STREAM, True) # Check sub media type, AKA what kind of codec guid_compressed = com.GUID(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) imfmedia.GetGUID(MF_MT_SUBTYPE, ctypes.byref(guid_compressed)) if guid_compressed == MFAudioFormat_PCM or guid_compressed == MFAudioFormat_Float: assert _debug('WMFAudioDecoder: Found Uncompressed Audio:', guid_compressed) else: assert _debug('WMFAudioDecoder: Found Compressed Audio:', guid_compressed) # If audio is compressed, attempt to decompress it by forcing source reader to use PCM mf_mediatype = IMFMediaType() MFCreateMediaType(ctypes.byref(mf_mediatype)) mf_mediatype.SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio) mf_mediatype.SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM) try: self._source_reader.SetCurrentMediaType( self._audio_stream_index, None, mf_mediatype) except OSError as err: # Can't decode codec. raise DecodeException(err) from None # Current media type should now be properly decoded at this point. decoded_media_type = IMFMediaType( ) # Maybe reusing older IMFMediaType will work? self._source_reader.GetCurrentMediaType( self._audio_stream_index, ctypes.byref(decoded_media_type)) wfx_length = ctypes.c_uint32() wfx = POINTER(WAVEFORMATEX)() MFCreateWaveFormatExFromMFMediaType(decoded_media_type, ctypes.byref(wfx), ctypes.byref(wfx_length), 0) self._wfx = wfx.contents self.audio_format = AudioFormat( channels=self._wfx.nChannels, sample_size=self._wfx.wBitsPerSample, sample_rate=self._wfx.nSamplesPerSec) else: assert _debug('WMFAudioDecoder: Audio stream not found')
mfreadwrite_lib = ctypes.windll.LoadLibrary(mfreadwrite) mfplat_lib = ctypes.windll.LoadLibrary(mfplat) except OSError: # Doesn't exist? Should stop import of library. raise ImportError('Could not load WMF library.') MF_SOURCE_READERF_ERROR = 0x00000001 MF_SOURCE_READERF_ENDOFSTREAM = 0x00000002 MF_SOURCE_READERF_NEWSTREAM = 0x00000004 MF_SOURCE_READERF_NATIVEMEDIATYPECHANGED = 0x00000010 MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED = 0x00000020 MF_SOURCE_READERF_STREAMTICK = 0x00000100 # Audio attributes MF_LOW_LATENCY = com.GUID(0x9c27891a, 0xed7a, 0x40e1, 0x88, 0xe8, 0xb2, 0x27, 0x27, 0xa0, 0x24, 0xee) # Audio information MF_MT_ALL_SAMPLES_INDEPENDENT = com.GUID(0xc9173739, 0x5e56, 0x461c, 0xb7, 0x13, 0x46, 0xfb, 0x99, 0x5c, 0xb9, 0x5f) MF_MT_FIXED_SIZE_SAMPLES = com.GUID(0xb8ebefaf, 0xb718, 0x4e04, 0xb0, 0xa9, 0x11, 0x67, 0x75, 0xe3, 0x32, 0x1b) MF_MT_SAMPLE_SIZE = com.GUID(0xdad3ab78, 0x1990, 0x408b, 0xbc, 0xe2, 0xeb, 0xa6, 0x73, 0xda, 0xcc, 0x10) MF_MT_COMPRESSED = com.GUID(0x3afd0cee, 0x18f2, 0x4ba5, 0xa1, 0x10, 0x8b, 0xea, 0x50, 0x2e, 0x1f, 0x92) MF_MT_WRAPPED_TYPE = com.GUID(0x4d3f7b23, 0xd02f, 0x4e6c, 0x9b, 0xee, 0xe4, 0xbf, 0x2c, 0x6c, 0x69, 0x5d) MF_MT_AUDIO_NUM_CHANNELS = com.GUID(0x37e48bf5, 0x645e, 0x4c5b, 0x89, 0xde, 0xad, 0xa9, 0xe2, 0x9b, 0x69, 0x6a)
com.STDMETHOD(DWORD, DWORD, ctypes.POINTER(ctypes.c_void_p), LPDWORD, ctypes.POINTER(ctypes.c_void_p), LPDWORD, DWORD)), ('Play', com.STDMETHOD(DWORD, DWORD, DWORD)), ('SetCurrentPosition', com.STDMETHOD(DWORD)), ('SetFormat', com.STDMETHOD(LPWAVEFORMATEX)), ('SetVolume', com.STDMETHOD(LONG)), ('SetPan', com.STDMETHOD(LONG)), ('SetFrequency', com.STDMETHOD(DWORD)), ('Stop', com.STDMETHOD()), ('Unlock', com.STDMETHOD(ctypes.c_void_p, DWORD, ctypes.c_void_p, DWORD)), ('Restore', com.STDMETHOD()), ] IID_IDirectSound3DListener = com.GUID(0x279AFA84, 0x4981, 0x11CE, 0xA5, 0x21, 0x00, 0x20, 0xAF, 0x0B, 0xE5, 0x60) class IDirectSound3DListener(com.pIUnknown): _methods_ = [ ('GetAllParameters', com.STDMETHOD(LPDS3DLISTENER)), ('GetDistanceFactor', com.STDMETHOD(PD3DVALUE)), ('GetDopplerFactor', com.STDMETHOD(PD3DVALUE)), ('GetOrientation', com.STDMETHOD(PD3DVECTOR, PD3DVECTOR)), ('GetPosition', com.STDMETHOD(PD3DVECTOR)), ('GetRolloffFactor', com.STDMETHOD(PD3DVALUE)), ('GetVelocity', com.STDMETHOD(PD3DVECTOR)), ('SetAllParameters', com.STDMETHOD(LPDS3DLISTENER)), ('SetDistanceFactor', com.STDMETHOD(D3DVALUE, DWORD)), ('SetDopplerFactor', com.STDMETHOD(D3DVALUE, DWORD)), ('SetOrientation',