from pylibpd import *
from struct import unpack
import array
import pygame
import numpy
BUFFERSIZE = 4096
SAMPLERATE = 44100
BLOCKSIZE = 64
pygame.mixer.init(frequency=SAMPLERATE)
m = PdManager(1, 2, SAMPLERATE, 1)
patch = libpd_open_patch('bloopy.pd', '.')
print("$0: ", patch)
inbuf = array.array('h', range(BLOCKSIZE))
ch = pygame.mixer.Channel(0)
sounds = [pygame.mixer.Sound(numpy.zeros((BUFFERSIZE, 2), numpy.int16)) for s in range(2)]
samples = [pygame.sndarray.samples(s) for s in sounds]
selector = 0
clock = pygame.time.Clock()
while(1):
if not ch.get_queue():
for x in range(BUFFERSIZE):
if x % BLOCKSIZE == 0:
barray = m.process(inbuf)
outbuf = unpack('h'*(len(barray)//2),barray)
samples[selector][x][0] = outbuf[(x % BLOCKSIZE) * 2]
samples[selector][x][1] = outbuf[(x % BLOCKSIZE) * 2 + 1]
ch.queue(sounds[selector])
selector = int(not selector)
clock.tick(40)
libpd_release()