示例#1
0
    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)
示例#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
文件: OpenAL_3D.py 项目: TDHster/PyAL
 def __init__(self):
 #load source player
     self.source = al.ALuint(0)
     al.alGenSources(1, self.source)
 #disable rolloff factor by default
     al.alSourcef(self.source, al.AL_ROLLOFF_FACTOR, 0)
 #disable source relative by default
     al.alSourcei(self.source, al.AL_SOURCE_RELATIVE,0)
 #capture player state buffer
     self.state = al.ALint(0)
 #set internal variable tracking
     self._volume = 1.0
     self._pitch = 1.0
     self._position = [0,0,0]
     self._rolloff = 1.0
     self._loop = False
     self.queue = []
示例#5
0
def main():
    source = al.ALuint()
    device = alc.alcOpenDevice(None)
    if not device:
        error = alc.alcGetError()
        # do something with the error, which is a ctypes value
        return -1
    # Omit error checking
    context = alc.alcCreateContext(device, None)
    alc.alcMakeContextCurrent(context)

    # Do more things
    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)

    al.alDeleteSources(1, source)
    alc.alcDestroyContext(context)
    alc.alcCloseDevice(device)
    return 0
示例#6
0
def get_sources():
    """
    Instantiates and returns a left, center and right audio source.

    Returns:
        A tuple of (LEFT, RIGHT, CENTER) localized sources.
    """
    POS_LEFT, POS_CENTER, POS_RIGHT = [(i, 0, 0) for i in range(-1, 2)]

    # Instantiate the sources
    sources = left, right, center = tuple(al.ALuint(i) for i in range(0, 3))
    for src in sources:
        al.alGenSources(1, src)
        al.alSourcef(src, al.AL_PITCH, 1)  # plays at default pitch.
        al.alSourcef(src, al.AL_GAIN, 1)
        al.alSource3f(src, al.AL_VELOCITY, 0, 0, 0)
        al.alSourcei(src, al.AL_LOOPING, al.AL_FALSE)

    # Assign a position to each source.
    al.alSource3f(left, al.AL_POSITION, *POS_LEFT)
    al.alSource3f(right, al.AL_POSITION, *POS_RIGHT)
    al.alSource3f(center, al.AL_POSITION, *POS_CENTER)

    return left, right, center
示例#7
0
文件: OpenAL_3D.py 项目: TDHster/PyAL
 def _set_volume(self,vol):
     self._volume = vol
     al.alSourcef(self.source, al.AL_GAIN, vol)
示例#8
0
文件: OpenAL_3D.py 项目: TDHster/PyAL
 def _set_pitch(self,pit):
     self._pitch = pit
     al.alSourcef(self.source, al.AL_PITCH, pit)
示例#9
0
文件: OpenAL_3D.py 项目: TDHster/PyAL
 def _set_rolloff(self,value):
     self._rolloff = value
     al.alSourcef(self.source, al.AL_ROLLOFF_FACTOR, value)