示例#1
0
文件: OpenAL_3D.py 项目: TDHster/PyAL
    def __init__(self,filename):
        self.name = filename
    #load/set wav file
        if len (sys.argv) < 2:
            print ("Usage: %s wavefile" % os.path.basename(sys.argv[0]))
            print ("    Using an example wav file...")
            dirname = os.path.dirname(os.path.realpath(__file__))
            fname = os.path.join(dirname, filename)
        else:
            fname = sys.argv[1]

        wavefp = wave.open(fname)
        channels = wavefp.getnchannels()
        bitrate = wavefp.getsampwidth() * 8
        samplerate = wavefp.getframerate()
        wavbuf = wavefp.readframes(wavefp.getnframes())
        self.duration = (len(wavbuf) / float(samplerate))/2
        self.length = len(wavbuf)
        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)]

        self.buf = al.ALuint(0)
        al.alGenBuffers(1, self.buf)
    #allocate buffer space to: buffer, format, data, len(data), and samplerate
        al.alBufferData(self.buf, alformat, wavbuf, len(wavbuf), samplerate)
示例#2
0
def run():
    if len(sys.argv) < 2:
        print("Usage: %s wavefile" % os.path.basename(sys.argv[0]))
        print("    Using an example wav file...")
        dirname = os.path.dirname(__file__)
        fname = os.path.join(dirname, "hey.wav")
    else:
        fname = sys.argv[1]

    wavefp = wave.open(fname)
    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.alcOpenDevice(None)
    context = alc.alcCreateContext(device, None)
    alc.alcMakeContextCurrent(context)

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

    al.alSourcef(source, al.AL_PITCH, 1)
    al.alSourcef(source, al.AL_GAIN, 1)
    al.alSource3f(source, al.AL_POSITION, 0, 10, 0)
    al.alSource3f(source, al.AL_VELOCITY, 0, 0, 0)
    al.alSourcei(source, al.AL_LOOPING, 1)

    buf = al.ALuint(0)
    al.alGenBuffers(1, buf)

    al.alBufferData(buf, alformat, wavbuf, len(wavbuf), samplerate)
    al.alSourceQueueBuffers(source, 1, buf)
    al.alSourcePlay(source)

    state = al.ALint(0)
    al.alGetSourcei(source, al.AL_SOURCE_STATE, state)
    z = 10
    while z > -10:
        time.sleep(1)
        al.alSource3f(source, al.AL_POSITION, 0, 0, z)
        print("playing at %r" % ([z, 0, 0]))
        z -= 1
    print("done")

    al.alDeleteSources(1, source)
    al.alDeleteBuffers(1, buf)
    alc.alcDestroyContext(context)
    alc.alcCloseDevice(device)
def run():
    if len (sys.argv) < 2:
        print ("Usage: %s wavefile" % os.path.basename(sys.argv[0]))
        print ("    Using an example wav file...")
        dirname = os.path.dirname(__file__)
        fname = os.path.join(dirname, "hey.wav")
    else:
        fname = sys.argv[1]

    wavefp = wave.open(fname)
    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.alcOpenDevice(None)
    context = alc.alcCreateContext(device, None)
    alc.alcMakeContextCurrent(context)

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

    al.alSourcef(source, al.AL_PITCH, 1)
    al.alSourcef(source, al.AL_GAIN, 1)
    al.alSource3f(source, al.AL_POSITION, 10, 0, 0)
    al.alSource3f(source, al.AL_VELOCITY, 0, 0, 0)
    al.alSourcei(source, al.AL_LOOPING, 1)

    buf = al.ALuint(0)
    al.alGenBuffers(1, buf)

    al.alBufferData(buf, alformat, wavbuf, len(wavbuf), samplerate)
    al.alSourceQueueBuffers(source, 1, buf)
    al.alSourcePlay(source)

    state = al.ALint(0)
    al.alGetSourcei(source, al.AL_SOURCE_STATE, state)
    z = 10
    while z > -10:
        time.sleep(1)
        al.alSource3f(source, al.AL_POSITION, z, 0, 0)
        print("playing at %r" % ([z, 0, 0]))
        z -= 1
    print("done")

    al.alDeleteSources(1, source)
    al.alDeleteBuffers(1, buf)
    alc.alcDestroyContext(context)
    alc.alcCloseDevice(device)
示例#4
0
    def __init__(self):
        self.channels = 1
        self.bitrate = 16
        self.samplerate = 8000
        self.wavbuf = None
        self.alformat = al.AL_FORMAT_MONO16
        self.length = None
        ##        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)]

        self.buf = al.ALuint(0)
        al.alGenBuffers(1, self.buf)
示例#5
0
def haas(path_to_wav):
    """
    Demonstrates the Haas Effect using the given <=16-bit WAV file.
    Plays the given audio three times, perceived from the center,
    left and right, respectively.
    """
    # Load the WAVE file and save some of its properties.
    wav = wave.open(path_to_wav)
    wav_name = path_to_wav.rsplit(os.path.sep).pop()
    channels = wav.getnchannels()
    bitrate = wav.getsampwidth() * 8
    samplerate = wav.getframerate()
    wav_buffer = wav.readframes(wav.getnframes())
    wav_fmt_map = {
        (1, 8): al.AL_FORMAT_MONO8,
        (2, 8): al.AL_FORMAT_STEREO8,
        (1, 16): al.AL_FORMAT_MONO16,
        (2, 16): al.AL_FORMAT_STEREO16,
        # 24-bit ints and floats are not supported by OpenAL out of the box.
    }
    alformat = wav_fmt_map[(channels, bitrate)]

    # Get the playback device / context.
    device = alc.alcOpenDevice(None)  # opens the default device.
    context = alc.alcCreateContext(device, None)  # no attributes needed.
    alc.alcMakeContextCurrent(context)

    # Prepare the playback buffer.
    buffer = al.ALuint(0)
    al.alGenBuffers(1, buffer)
    al.alBufferData(buffer, alformat, wav_buffer, len(wav_buffer), samplerate)

    # Initialize and configure the sources for playback.
    sources = src_left, src_right, src_center = get_sources()
    for src in sources:
        al.alSourceQueueBuffers(src, 1, buffer)

    def play(delay_ms=15, dominant='left'):
        """
        Plays the above wav file from a perceived location.

        Args:
            delay_ms: The delay between the first and second source.
            dominant: The dominant location to play the audio from.
        """
        dominant = dominant.lower()
        directions = {'left', 'right', 'center'}
        GAIN = 1.

        if dominant not in directions or dominant == 'center':
            al.alSourcePlay(src_center)
        else:
            # Determine the order in which to play the sources.
            play_order = [src_left, src_right]  # left-dominant.
            if dominant == 'right':
                play_order.reverse()

            # Adjust the gains for a more biased stereo effect.
            al.alSourcef(play_order[0], al.AL_GAIN, GAIN * 0.5)
            al.alSourcef(play_order[1], al.AL_GAIN, GAIN * 0.4)

            # Apply the precedence effect by playing the sources
            # 'delay_ms' milliseconds apart.
            al.alSourcePlay(play_order[0])
            time.sleep(delay_ms / 1000)
            al.alSourcePlay(play_order[1])

        # Wait until playback is finished, then delete the buffer.
        duration = wav.getnframes() / samplerate
        time.sleep(duration)

    # Play the wav file localized at each of the given positions.
    for position in ['center', 'left', 'right']:
        print(f'Playing {wav_name} perceived from the {position}.')
        play(dominant=position)
        print('Done playing.\n')

    # Clean up.
    for src in sources:
        al.alDeleteSources(1, src)
    al.alDeleteBuffers(1, buffer)
    alc.alcDestroyContext(context)
    alc.alcCloseDevice(device)

    return 0