Beispiel #1
0
    def __init__(self, device_name=None):
        super(OpenALDriver, self).__init__()

        # TODO devices must be enumerated on Windows, otherwise 1.0 context is
        # returned.

        self._device = alc.alcOpenDevice(device_name)
        if not self._device:
            raise Exception("No OpenAL device.")

        alcontext = alc.alcCreateContext(self._device, None)
        alc.alcMakeContextCurrent(alcontext)

        self.have_1_1 = self.have_version(1, 1) and False

        self._lock = threading.Lock()

        # Start worker thread
        self.worker = OpenALWorker()
        self.worker.start()
Beispiel #2
0
    def __init__(self, device_name=None):
        super(OpenALDriver, self).__init__()

        # TODO devices must be enumerated on Windows, otherwise 1.0 context is
        # returned.

        self._device = alc.alcOpenDevice(device_name)
        if not self._device:
            raise Exception('No OpenAL device.')

        alcontext = alc.alcCreateContext(self._device, None)
        alc.alcMakeContextCurrent(alcontext)

        self.have_1_1 = self.have_version(1, 1) and False

        self._lock = threading.Lock()

        # Start worker thread
        self.worker = OpenALWorker()
        self.worker.start()
Beispiel #3
0
            al.alBufferData(buffer, self.buffer_format, self.sample_buffer,
                            sample_buffer_size.value, self.sample_rate)

        # Advance buffer pointer
        self.packet_data = ctypes.c_uint8.from_address(
            ctypes.addressof(self.packet_data) + len)
        self.packet_size -= len

        return True


if __name__ == '__main__':
    import sys

    # openal
    device = alc.alcOpenDevice(None)
    if not device:
        raise Exception('No OpenAL device.')

    alcontext = alc.alcCreateContext(device, None)
    alc.alcMakeContextCurrent(alcontext)

    source = al.ALuint()
    al.alGenSources(1, source)

    pool = BufferPool()

    # avcodec
    init()
    container = open(sys.argv[1])
    stream = get_audio_stream(container)
Beispiel #4
0
class AVCodecDecoder:
    def __init__(self, container, stream):
        self.codec = get_codec(container, stream)
        if self.codec.contents.channels == 1:
            self.buffer_format = al.AL_FORMAT_MONO16
        elif self.codec.contents.channels == 2:
            self.buffer_format = al.AL_FORMAT_STEREO16
        else:
            raise Exception('Invalid number of channels')
        self.sample_rate = self.codec.contents.sample_rate

        self.container = container
        self.stream = stream
        self.packet = avformat.AVPacket()
        self.sample_buffer = \
            (ctypes.c_int16 * (avcodec.AVCODEC_MAX_AUDIO_FRAME_SIZE / 2))()

        self.read_packet()

    def read_packet(self):
        while True:
            if self.packet.data:
                self.packet.data = None
                self.packet.size = 0

            if avformat.av_read_packet(self.container, self.packet) < 0:
                break

            if self.packet.stream_index == self.stream:
                break

        self.packet_data = self.packet.data
        self.packet_size = self.packet.size

    def fill_buffer(self, buffer):
        if self.packet_size <= 0:
            self.read_packet()

        if self.packet_size == 0:  # EOS
            return False

        sample_buffer_size = ctypes.c_int()
        len = avcodec.avcodec_decode_audio(self.codec, self.sample_buffer,
                                           sample_buffer_size,
                                           self.packet_data, self.packet_size)
        if len < 0:
            # Error, skip frame
            raise Exception('frame error TODO')

        if sample_buffer_size.value > 0:
            al.alBufferData(buffer, self.buffer_format, self.sample_buffer,
                            sample_buffer_size.value, self.sample_rate)

        # Advance buffer pointer
        self.packet_data = ctypes.c_uint8.from_address(
            ctypes.addressof(self.packet_data) + len)
        self.packet_size -= len

        return True

    import sys

    # openal
    device = alc.alcOpenDevice(None)
    if not device:
        raise Exception('No OpenAL device.')

    alcontext = alc.alcCreateContext(device, None)
    alc.alcMakeContextCurrent(alcontext)

    source = al.ALuint()
    al.alGenSources(1, source)

    pool = BufferPool()

    # avcodec
    init()
    container = open(sys.argv[1])
    stream = get_audio_stream(container)

    decoder = AVCodecDecoder(container, stream)

    while True:
        buffer = pool.get_buffer()
        if not decoder.fill_buffer(buffer):
            break
        al.alSourceQueueBuffers(source, 1, buffer)

    al.alSourcePlay(source)

    while True:
        value = al.ALint()
        al.alGetSourcei(source, al.AL_SOURCE_STATE, value)
        if value.value == al.AL_STOPPED:
            break

        time.sleep(1)
Beispiel #5
0
            al.alBufferData(buffer, self.buffer_format, 
                            self.sample_buffer, sample_buffer_size.value,
                            self.sample_rate)

        # Advance buffer pointer
        self.packet_data = ctypes.c_uint8.from_address(
                                ctypes.addressof(self.packet_data) + len)
        self.packet_size -= len

        return True
        
if __name__ == '__main__':
    import sys

    # openal
    device = alc.alcOpenDevice(None)
    if not device:
        raise Exception('No OpenAL device.')

    alcontext = alc.alcCreateContext(device, None)
    alc.alcMakeContextCurrent(alcontext)

    source = al.ALuint()
    al.alGenSources(1, source)

    pool = BufferPool()

    # avcodec
    init()
    container = open(sys.argv[1])
    stream = get_audio_stream(container)