예제 #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...")
        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)
예제 #2
0
 def rec(self):
     if self.captureDev != None:
         s_available = al.ALint(0)
         #capture audio
         alc.alcGetIntegerv(self.captureDev, alc.ALC_CAPTURE_SAMPLES, 1,
                            s_available)
         #transfer captured audio data into tmp buffer, add to main buffer
         box = ' ' * (s_available.value * 2)
         alc.alcCaptureSamples(self.captureDev, box, s_available)
         self.samplescaptured += box
예제 #3
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 = []