Exemplo n.º 1
0
    def __init__(self, filename, file):
        self.filename = filename
        self.file = file
        self._stream = None
        self.sample_size = 16

        self._load_source()

        self.audio_format = AudioFormat(channels=self._stream.channels,
                                        sample_size=self.sample_size,
                                        sample_rate=self._stream.frequency)
Exemplo n.º 2
0
def _as_static(data, fs):
    """Get data into the Pyglet audio format."""
    fs = int(fs)
    if data.ndim not in (1, 2):
        raise ValueError('Data must have one or two dimensions')
    n_ch = data.shape[0] if data.ndim == 2 else 1
    audio_format = AudioFormat(channels=n_ch, sample_size=16, sample_rate=fs)
    data = data.T.ravel('C')
    data[data < -1] = -1
    data[data > 1] = 1
    data = (data * (2**15)).astype('int16').tostring()
    return StaticMemorySourceFixed(data, audio_format)
Exemplo n.º 3
0
def stream(context):
    context.connect()
    audio_format = AudioFormat(1, 16, 44100)
    with context:
        stream = context.create_stream(audio_format)
    return stream
Exemplo n.º 4
0
    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)
        guid_audio_type = com.GUID(
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0)  # TODO: Make GUID take no arguments for a null version.

        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 MediaDecodeException(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')
Exemplo n.º 5
0
def audio_format_3d(request):
    return AudioFormat(*request.param)