User:Ruben/Prototyping/Granular Synthesis

From XPUB & Lens-Based wiki

Granular Synthesis practice: a delay

import sys, struct 
 
c = 0
samples = []
numsamples = 17640 # 0.4 sec
# numsamples = 1764  # 0.040 sec == 40 ms == 0.040 * 44100 => 1764
# numsamples = 176 # 0.004 sec == 4 ms == 0.004 * 44100
# numsamples = 44 # 0.001 sec == 1 ms == 0.001 * 44100
# numsamples = 441 #0.01 sec == 1dec == 0.01 * 44100 = 441
# numsamples = 44 #0.001 sec == 1centi == 0.001 * 44100 = 44.1
# numsamples = int(4.41) #0.0001 sec == 1milli == 0.0001 * 44100 = 4.41
# numsamples = int(1) #0.000001 sec == 1micro == 0.000001 * 44100 = 0.0441
 
loop = 1
prev = None
 
while True:
    sample = sys.stdin.read(2*numsamples)
    if not sample:
        break
 
    samples = struct.unpack("H"*numsamples, sample)
 
    # sys.stderr.write(str(n)+"/n")
    # o = max(35000,n)
    # take your slice and repeat
    for i in range(loop):
        new = list(samples)
        if prev is not None:
            for x in xrange(0,numsamples):
                new[x] = 0.3 * new[x] + 0.7 * prev[x]
                pass
        out = struct.pack("H"*numsamples, *new)
        sys.stdout.write(out)

    # prev = samples
    prev = new