Beispiel #1
0
def run():
    if len (sys.argv) < 2:
        print ("Usage: %s wavefile" % os.path.basename(sys.argv[0]))
        print ("    Using an example wav file...")
        wavefp = wave.open(RESOURCES.get("hey.wav"))
    else:
        wavefp = wave.open(sys.argv[1], "rb")

    channels = wavefp.getnchannels()
    bitrate = wavefp.getsampwidth() * 8
    samplerate = wavefp.getframerate()
    wavbuf = wavefp.readframes(wavefp.getnframes())
    formatmap = {
        (1, 8) : al.AL_FORMAT_MONO8,
        (2, 8) : al.AL_FORMAT_STEREO8,
        (1, 16): al.AL_FORMAT_MONO16,
        (2, 16) : al.AL_FORMAT_STEREO16,
    }
    alformat = formatmap[(channels, bitrate)]

    device = alc.open_device()
    context = alc.create_context(device)
    alc.make_context_current(context)

    sources = al.gen_sources(1)

    al.source_f(sources[0], al.AL_PITCH, 1)
    al.source_f(sources[0], al.AL_GAIN, 1)
    al.source_3f(sources[0], al.AL_POSITION, 0, 0, 0)
    al.source_3f(sources[0], al.AL_VELOCITY, 0, 0, 0)

    buffers = al.gen_buffers(1)

    al.buffer_data(buffers[0], alformat, wavbuf, samplerate)
    al.source_queue_buffers(sources[0], buffers)
    al.source_play(sources[0])

    state = al.get_source_i(sources[0], al.AL_SOURCE_STATE)
    while state == al.AL_PLAYING:
        print("playing the file...")
        time.sleep(1)
        state = al.get_source_i(sources[0], al.AL_SOURCE_STATE)
    print("done")

    al.delete_sources(sources)
    al.delete_buffers(buffers)
    alc.destroy_context(context)
    alc.close_device(device)
Beispiel #2
0
    def process_source(self, source):
        """Processes a SoundSource.

        Note: this does NOT activate the SoundSink. If another SoundSink
        is active, chances are good that the source is processed in that
        SoundSink.
        """
        ssid = source._ssid
        if ssid is None:
            ssid = self._create_source(source)

        # TODO: this should be only set on changes.
        # al.source_f(ssid, al.AL_GAIN, source.gain)
        # al.source_f(ssid, al.AL_PITCH, source.pitch)
        # al.source_fv(ssid, al.AL_POSITION, source.position)
        # al.source_fv(ssid, al.AL_VELOCITY, source.velocity)

        self._create_buffers(source)
        querystate = al.get_source_i(ssid, al.AL_SOURCE_STATE)
        if source.request == SOURCE_NONE:
            # if no change is to be made, nothing will be done.
            pass
        elif source.request == SOURCE_REWIND:
            al.source_rewind(ssid)
            source.request = SOURCE_NONE
        elif source.request == SOURCE_PLAY:
            if querystate != al.AL_PLAYING:
                al.source_play(ssid)
            source.request = SOURCE_NONE
        elif source.request == SOURCE_STOP:
            if querystate != al.AL_STOPPED:
                al.source_stop(ssid)
            source.request = SOURCE_NONE
        elif source.request == SOURCE_PAUSE:
            if querystate != al.AL_PAUSED:
                al.source_pause(ssid)
            source.request = SOURCE_NONE
        else:
            raise ValueError("invalid request state on source")