import wave from array import array import struct universe = [-int(2100000012/7),int(2100000012/7)] life = 44100*120 gene = 100*[0]+sum([z*[y*x] for z in [11,3,17] for y in [-7,5,2] for x in range(11)],[]) death = [[[1],1],0] blood = [[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] body = [ [[-15000,15000],[-10000,10000],[-5000,5000],[-1500,1500],[-650,650],[-375,375],[-242,242]], [[-15000,15000],[-10000,10000],[-5000,5000],[-1500,1500],[-650,650],[-375,375],[-242,242]] ] nesteds = [] def prepare_gene(g): t = [g,len(g)] print("prepared a new gene") print(t) return t def set_gene(g): return prepare_gene(g) def baptize(g,d): n = [ [ [[g,int(0*g[1]/28),0,[g,int(14*g[1]/28),0,d]],2], [[g,int(1*g[1]/28),0,[g,int(15*g[1]/28),0,d]],2], [[g,int(2*g[1]/28),0,[g,int(16*g[1]/28),0,d]],2], [[g,int(3*g[1]/28),0,[g,int(17*g[1]/28),0,d]],2], [[g,int(4*g[1]/28),0,[g,int(18*g[1]/28),0,d]],2], [[g,int(5*g[1]/28),0,[g,int(19*g[1]/28),0,d]],2], [[g,int(6*g[1]/28),0,[g,int(20*g[1]/28),0,d]],2] ], [ [[g,int(7*g[1]/28),0,[g,int(21*g[1]/28),0,d]],2], [[g,int(8*g[1]/28),0,[g,int(22*g[1]/28),0,d]],2], [[g,int(9*g[1]/28),0,[g,int(23*g[1]/28),0,d]],2], [[g,int(10*g[1]/28),0,[g,int(24*g[1]/28),0,d]],2], [[g,int(11*g[1]/28),0,[g,int(25*g[1]/28),0,d]],2], [[g,int(12*g[1]/28),0,[g,int(26*g[1]/28),0,d]],2], [[g,int(13*g[1]/28),0,[g,int(27*g[1]/28),0,d]],2] ] ] return n # gene,nesteds,leftright,which,depth def set_its_gene(g,n,lr,w,d): pg = prepare_gene(g) print("nested", lr, "-", w, "is gonna get twisted at..depth =", d) x = n[lr][w][0] if d == 1: x[0] = pg x[1] = x[1]%pg[1] else: for i in range(d): if i == d-1: x[0] = pg x[1] = x[1]%pg[1] else: x = x[-1] print(n[lr][w][0]) # [[gene, whereofmyself, cntofmodulator, osc-por-next-switch], depth] def gadd(g,dth,n,lr,w): pg = prepare_gene(g) print("gene will b added to the tail of the mul", lr, "-", w, "depth =", n[lr][w][1]) x = n[lr][w] d = x[1] x[1] = x[1]+1 if d == 1: x[0][-1] = pg else: for i in range(d): if i == 0: x = x[0][-1] elif i == d-1: x[0] = pg x[1] = x[1]%pg[1] x[-1] = dth else: x = x[-1] # add to the tail def gcut(dth,n,lr,w,d): print("nested", lr, "-", w, "will get cut at..depth =", d) x = n[lr][w] x[1] = d if d == 1: x[0][-1] = dth else: for i in range(d): if i == 0: x = x[0][-1] elif i == d - 1: x[-1] = self.death else: x = x[-1] # [src, length, [max, abs(min), 2 * width ** 2]] # what is this **2 #[src, length, absmaxmin] #death is [gene,0] # s = [self.gene, int(0*self.gene[1]/28), 0, [self.gene, int(14*self.gene[1]/28), 0, self.death]] # [gene, whereofmyself, cntofmodulator, osc-por-next-switch], depth] # len = 4 # s[-1][0] = self.gene = [src, length, [max, abs(min), 2 * width ** 2]] # s[-1][0][0] = src # s[-1][0][0][s[-1][1]] = i s[-1][1] of src # increase cnt for modu # s[-1] = self.death = [[[1], 1, [1, 1, 2]], 0] # [1,1,2] = [max,min,2 * 1**2] # always 0? cuz % 1 # s[-1][1] = (s[-1][1] + 1) % s[-1][0][1] def poke_synapse(s): s[2] = s[2]+1 # speed controlled by mod if s[2] == abs(s[-1][0][0][s[-1][1]]) or s[-1][0][0][s[-1][1]] == 0: # last one? if len(s[-1]) == 2: dmyv = 1 else: #dummy value increase the position of the deeper one dmyv = poke_synapse(s[-1]) s[2] = 0 # when minus go back n mirror if s[-1][0][0][s[-1][1]] < 0: s[1] = (s[1]-1)%s[0][1] elif s[-1][0][0][s[-1][1]] > 0: s[1] = (s[1]+1)%s[0][1] if s[-1][0][0][s[-1][1]] < 0: return int(s[0][0][s[1]]/abs(s[-1][0][0][s[-1][1]])) else: return int(s[-1][0][0][s[-1][1]]*s[0][0][s[1]]) # choose(tmp,w,bl[lr],bo[lr]) def choose(t,w,bl,bo): chz = [] # when more than max if t > bo[w][1]: d = t-bo[w][1] for i in range(7): if i != w and bo[i][1]-bl[i] > d: chz.append(i) # when less than max else: d = t-bo[w][0] for i in range(7): if i != w and bo[i][0]-bl[i] < d: chz.append(i) if chz != []: lc = len(chz) other = t%lc if d < 0: bo[chz[other]][0] = bo[chz[other]][0]-d bo[w][0] = t else: bo[chz[other]][1] = bo[chz[other]][1]-d bo[w][1] = t bl[w] = t def sing(bl,bo,n,u): for lr in range(2): for w in range(7): mlv = poke_synapse(n[lr][w][0]) tmp = bl[lr][w]+mlv if tmp < u[0] or tmp > u[1]: continue elif tmp < bo[lr][w][0] or tmp > bo[lr][w][1]: choose(tmp,w,bl[lr],bo[lr]) continue else: bl[lr][w] = tmp return [sum(bl[0]),sum(bl[1])] def l2h(lst): h_thang = array('h') l_thang = len(lst) for gas in range(l_thang): h_thang.append(lst[gas]) return h_thang def finout(l): for i in range(500): l[2*i] = int((i*l[2*i])/500) l[2 *i+1] = int((i*l[2*i+1])/500) l[-1- 2*i] = int((i*l[-1-2*i])/500) l[-2- 2*i] = int((i*l[-2-2*i])/500) def normalize(lst): mom = 2147483547 mx = max(lst) mn = min(lst) l_len = len(lst) print('gonna normalize mx=', mx, 'mn=', mn, 'length=', int(l_len/2)) mx = mx-mn if mx == 0 or mx == 65534 or mx == 65535: print('no need 2 normalize') else: for i in range(l_len): lst[i] = lst[i]-mn ml = 2147352578/mx for i in range(l_len): lst[i] = int((ml*lst[i])/32767) mx = max(lst) mn = min(lst) h = int((mx-mn)/2) if h != 0: for i in range(l_len): lst[i] = int(lst[i]-h) mx = max(lst) mn = min(lst) print('normalized mx=', mx, 'mn=', mn) gene = set_gene(gene) nesteds = baptize(gene,death) song = [] for i in range(life): # cheeze = i % 1111111 # ^ will give a chance to create a pattern of length=1111111 cheeze = i # got to automate n funcnize these list buildings if cheeze == 44100 * 10: set_its_gene(300*[0]+list(range(44))+list(range(45,-77,-5))+list(range(-73,0,-7)),nesteds,1,1,1) elif cheeze == 44100 * 20: gadd(17*[-4]+4*[7]+130*[0]+7*[6,4,2,-1,-3,-5,-11],death,nesteds,0,1) elif cheeze == 44100 * 35: set_gene(3*[13]+11*[7]+33*[0]+4*[-5]+15*[-11]) nesteds = baptize(gene,death) elif cheeze == 44100 * 50: gadd(sum([y*[y+x] for y in [3,11,7] for x in list(range(2,99,4))+list(range(-101,33,10))],[]),death,nesteds,0,3) elif cheeze == 44100 * 65: set_its_gene(5*[-3]+17*[1]+20*[0]+21*[-13]+33*[11],nesteds,0,2,2) elif cheeze == 44100 * 80: set_gene(31*[1]+11*[5]+55*[0]+33*[-3]+11*[-7]) nesteds = baptize(gene,death) elif cheeze == 44100 * 95: gcut(death,nesteds,0,5,1) breath = sing(blood,body,nesteds,universe) song.append(breath[0]) song.append(breath[1]) if i % 44100 == 0 and i != 0: print(int(i/44100), song[-2], song[-1]) print(body) #normalize(song) finout(song) thang = l2h(song) wav = wave.open("soundthang.wav", "w") wav.setnchannels(2) wav.setsampwidth(2) wav.setframerate(44100) #wav.writeframes(thang.tostring()) wav.writeframes(thang.tobytes()) wav.close() print('done')