Esempio n. 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)
Esempio n. 2
0
 def _create_source(self, source):
     """Creates a new OpenAL source from the the passed SoundSource."""
     ssid = None
     for kssid, ksource in self._sources.items():
         if ksource is None:
             # Got a free source
             ssid = kssid
             break
     if ssid is not None:
         source._ssid = ssid
         self._sources[ssid] = source
     else:
         # No free source
         ssid = al.gen_sources(1)[0]
         self._sources[ssid] = source
         source._ssid = ssid
     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)
     return ssid