Пример #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)
Пример #2
0
def run():
    # We can use OpenAL directly to play some sound, there is no
    # necessity to use the audio module. This however is a bit more
    # complex (read: involves writing more code) and less
    # comfortable. For a direct OpenAL example, check the openal.py
    # example file.

    # Create a SoundSink. It is used do the output device handling,
    # source and audio buffer management and anything else necessary
    # to play sounds.
    #
    # You can create multiple sinks for different audio devices or for
    # the same device, so that you can e.g. manage different positions
    # for listening to sounds.
    sink = audio.SoundSink()

    # Create a SoundSource. The SoundSource defines an object within
    # your application world, that can emit sounds. It could be e.g. a
    # spaceship within your game, the general output object within your a
    # music player application, ...
    source = audio.SoundSource()

    # Load a SoundData object from the passed WAV file. A SoundData is
    # nothing more than a raw audio buffer (as PCM data), along with
    # some information about its channels, bit rate and frequency.
    data = audio.load_file(RESOURCES.get_path("hey.wav"))

    # Add the SoundData to the source, so that it will be played, when
    # the source is processed by the SoundSink.
    source.queue(data)

    # Tell the SoundSource that it shall be played on the next
    # processing by the SoundSink. This will cause the SoundSink to
    # buffer all attached SoundData objects of the SoundSource and play
    # them in order.
    source.request = audio.SOURCE_PLAY
    sink.process_source(source)

    # The main loop. In contrast to other exampes, we are checking the
    # status of our previously created source directly. Once it is done
    # playing, we exit from the loop to finish the program execution.
    #
    # The OpenAL sound system uses its own threads to play sounds and
    # music so that we do not not handle it explicitly, if we do not
    # need to perform more complex or synchronised operations. Hence,
    # once a SoundSource was requested to play something and got
    # processed, there is not much more left to do.
    while True:
        state = al.get_source_i(source.ssid, al.AL_SOURCE_STATE)
        if state == al.AL_PLAYING:
            print("playing...")
        else:
            print("done")
            break
        time.sleep(2)
Пример #3
0
 def __del__(self):
     """Deletes the SoundSink and also destroys the associated
     context and closes the bound audio output device."""
     if self.context:
         for ssid, source in self._sources.items():
             querystate = al.get_source_i(ssid, al.AL_SOURCE_STATE)
             if querystate != al.AL_STOPPED:
                 al.source_stop(ssid)
             al.delete_sources([ssid])
             source._ssid = None
         self._sources = {}
         al.delete_buffers(self._buffers.keys())
         self._buffers = {}
         alc.destroy_context(self.context)
         if self._hasopened:
             alc.close_device(self.device)
         self.context = None
         self.device = None
Пример #4
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")