import wave from array import array import struct import sys # dir = 0 will be use for both vf calcing y former frame copying life = 44100 * 270 # gotta check whether its too short or not f_names = [ "sari.wav", "gama.wav", "pada.wav", "nisa.wav" ] # poetry cannot be next to each other # [which, fixed-value] poetry = [[-1], [0,57], [int(44100 * 3)]] noiz = [1,3] acid = [[[0,2]], [[1,2,3]], [[0,2]]] ratios = [[1,4],[1,2],[9,16],[5,8],[3,4],[13,16],[8,9],[4,5],[2,3],[8,13]] len_r = len(ratios) howoften = 7 #mpttrn = [0, 57575, 111111] mpttrn = [1, 1, 7] # 0 = organic 1 = pulsy pulse = 51715 prepoe = 1331 layers = 3 def numden(v): if v % howoften == 0: cz = (v * layers + pulse) % len_r return ratios[cz] else: return [1,1] def normalize(l): mom = sys.maxint mx = max(l) mn = min(l) l_len = len(l) print 'gonna normalize mx=', mx, 'mn=', mn, 'length=', l_len for i in range(l_len): l[i] = l[i] - mn mx = mx - mn if mx == 0 or mx == 65535: print 'no need 2 normalize' elif mx > 65535: mln = mom / mx mld = mom / 65535 # ttt = mx / 65535 for i in range(l_len): l[i] = mln * l[i] / mld # l[i] = l[i] / (ttt + 1) else: ml = 65535000 / mx for i in range(l_len): l[i] = (ml * l[i]) / 1000 mx = max(l) mn = min(l) h = (65535 - mx) / 2 if h != 0: for i in range(l_len): l[i] = l[i] + h mx = max(l) mn = min(l) print 'normalized mx=', mx, 'mn=', mn def rsrc(fn): w = wave.open(fn, "r") b = w.readframes(w.getnframes()) s = list(struct.unpack('%dh' % (len(b) / w.getsampwidth()), b)) w.close() b = [] normalize(s) return [s, len(s) / 2] def l2h(thang): h_thang = array('h') l_thang = len(thang) for gas in range(l_thang): h_thang.append(thang[gas] - 32768) return h_thang def loopy(t, s): tf = t[2][0] + t[2][2] * (t[2][1] * t[2][3]) / t[2][4] oore = (tf / s[t[0]][1]) % 2 tf = tf % s[t[0]][1] if oore == 1: tf = s[t[0]][1] - 1 - tf return tf # [src_num, vol, [start@src, cnt, dir(1 = fw , -1 = bw, 0 = stay), num, den], [max_vol, b__free@cnt_x, what_kinda_untouchableness]] # its not f but cnt that determines the real frame def position(s, drs, nds, f, srclen): # return (s + f * (drs[0] * nds[0][0] * nds[1][1] - drs[1] * nds[1][0] * nds[0][1]) / (nds[0][1] * nds[1][1])) % srclen return (s + f * drs[0] * nds[0][0] / nds[0][1] - f * drs[1] * nds[1][0] / nds[1][1]) % srclen # t[2][0] = position(t[2][0], [t[2][2], t[2][2]], [[nm, dn], [t[2][3], t[2][4]]], t[2][1], s[t[0]][1]) # [src_num, vol, [start@src, cnt, dir(1 = fw , -1 = bw, 0 = stay), num, den], [max_vol, b__free@cnt_x, what_kinda_untouchableness]] def mix(f, trks, s, e, p): l = p[0] * p[1][0] r = p[0] * p[1][1] for t in trks: # vf = (t[2][0] + t[2][1] * ((t[2][2] * f) / t[2][3])) % s[t[0]][1] vf = loopy(t, s) l = l + t[1] * s[t[0]][0][2 * vf] r = r + t[1] * s[t[0]][0][2 * vf + 1] e.append(l) e.append(r) # [src_num, vol, [start@src, cnt, dir(1 = fw , -1 = bw, 0 = stay), num, den], [max_vol, b__free@cnt_x, what_kinda_untouchableness]] # what_kinda_untouchableness?(0 = free, 1 = fixed, 2 = vol_only, 3 = diryvol, 4 = numdenyvol) # change_src(f, tracks, src, pool, tracks[0][0]) #def change_src(f, t, s, p, du): def change_src(f, t, s, p, a): # print f, "changin src" mm = sys.maxint al = len(a) # gotta do the pool thang 1st before twisting p[1][0] = p[1][0] * p[0] p[1][1] = p[1][1] * p[0] vf = loopy(t, s) p[1][0] = p[1][0] + t[1] * s[t[0]][0][2 * vf] p[1][1] = p[1][1] + t[1] * s[t[0]][0][2 * vf + 1] p[0] = p[0] + t[1] if p[0] > 0: p[1][0] = p[1][0] / p[0] p[1][1] = p[1][1] / p[0] # src_num # t[0] = (du + 1 + (f + p[1][0]) % (sl - 1)) % sl t[0] = a[(f + p[1][0]) % al] # vol t[1] = 0 # start@src t[2][0] = (mm - f - p[1][1]) % s[t[0]][1] # cnt t[2][1] = 0 # dir(1 = fw , -1 = bw, 0 = stay), t[2][2] = -1 + (mm - t[2][0] - f) % 3 if t[2][2] == 0: t[2][2] = 1 # den # t[2][4] = 8 + (mm - f) % 9 # num # t[2][3] = t[2][4] / 8 + (f + t[2][2] * t[2][1]) % (2 * t[2][4] - t[2][4] / 8) # t[2][3] = t[2][4] / 4 + (f + t[2][2] * t[2][1]) % (2 * t[2][4] - t[2][4] / 4) # cz = (mm - f) % len_r # t[2][3] = ratios[cz][0] # t[2][4] = ratios[cz][1] rr = numden(mm - f) t[2][3] = rr[0] t[2][4] = rr[1] # max_vol t[3][0] = 30 + (mm - f - 2 * t[2][1]) % 71 # b__free@cnt_x # actually worth nothang cuz type is zero t[3][1] = 33333 + (mm - f - t[2][1]) % 77777 # what_kinda_untouchableness t[3][2] = 0 # print "src changed", f, "=", t # print "pooled = ", p[0] def twist(f, t, s): c = f % 2 # if c == 0 and t[2][1] != 0: if c == 0: # print f, "twistin dir" t[2][0] = position(t[2][0], [t[2][2], -1 * t[2][2]], [[t[2][3], t[2][4]], [t[2][3], t[2][4]]], t[2][1], s[t[0]][1]) t[2][2] = -1 * t[2][2] else: # print f, "twistin numden" nm = t[2][3] dn = t[2][4] rr = numden(f) t[2][3] = rr[0] t[2][4] = rr[1] # cz = f % len_r # t[2][3] = ratios[cz][0] # t[2][4] = ratios[cz][1] # t[2][4] = 8 + f % 9 # t[2][3] = t[2][4] / 4 + (f + nm + dn) % (int(1.5 * t[2][4]) - t[2][4] / 4) ## t[2][3] = t[2][4] / 8 + (f + nm + dn) % (2 * t[2][4] - t[2][4] / 8) # t[2][3] = t[2][4] / 4 + (f + nm + dn) % (2 * t[2][4] - t[2][4] / 4) t[2][0] = position(t[2][0], [t[2][2], t[2][2]], [[nm, dn], [t[2][3], t[2][4]]], t[2][1], s[t[0]][1]) # print "twisted track = ", t # [src_num, vol, [start@src, cnt, dir(1 = fw , -1 = bw, 0 = stay), num, den], [max_vol, b__free@cnt_x, what_kinda_untouchableness]] def giventake(f, t, l): e1 = 1 + f % (l - 1) e2 = 1 + (e1 + t[e1][1]) % (l - 1) if t[e1][1] > 0 and e2 != e1 and t[e2][1] < t[e2][3][0]: t[e1][1] = t[e1][1] - 1 t[e2][1] = t[e2][1] + 1 def breath(p, f, t, s): if f % 44100 == 0: print '[', f / 44100, ']' # for j in t: # print j[1], # print "->", p t_len = len(t) for i in range(1, t_len): if p[0] != 0 and t[i][1] < t[i][3][0]: t[i][1] = t[i][1] + 1 p[0] = p[0] - 1 # this one es not exchange it only brings back from the pool to each track # arb_thang(pool, f, episode, tracks, src, core[x]) # [@f, tracknum, srcnum, vol, strt, dir, num, den, timer, type] # [src_num, vol, [start@src, cnt, dir(1 = fw , -1 = bw, 0 = stay), num, den], [max_vol, b__free@cnt_x, what_kinda_untouchableness]] def arb_thang(p, f, e, t, s, c): lyr = len(t) # do the vol 1st p[1][0] = p[1][0] * p[0] p[1][1] = p[1][1] * p[0] if c[1] == 0: mrorls = c[3] - t[c[1]][1] t[c[1]][1] = c[3] if mrorls <= 0: vf = loopy(t[c[1]], s) p[1][0] = p[1][0] + abs(mrorls) * s[t[c[1]][0]][0][2 * vf] p[1][1] = p[1][1] + abs(mrorls) * s[t[c[1]][0]][0][2 * vf + 1] p[0] = p[0] + abs(mrorls) elif p[0] < mrorls: d = mrorls - p[0] p[0] = 0 for l in range(1, lyr): vf = loopy(t[l], s) if t[l][1] < d: d = d - t[l][1] p[1][0] = p[1][0] + t[l][1] * s[t[l][0]][0][2 * vf] p[1][1] = p[1][1] + t[l][1] * s[t[l][0]][0][2 * vf + 1] else: t[l][1] = t[l][1] - d p[1][0] = p[1][0] + d * s[t[l][0]][0][2 * vf] p[1][1] = p[1][1] + d * s[t[l][0]][0][2 * vf + 1] break else: p[0] = p[0] - mrorls else: vf = loopy(t[c[1]], s) p[1][0] = p[1][0] + t[c[1]][1] * s[t[c[1]][0]][0][2 * vf] p[1][1] = p[1][1] + t[c[1]][1] * s[t[c[1]][0]][0][2 * vf + 1] p[0] = p[0] + t[c[1]][1] t[c[1]][1] = 0 if p[0] > 0: p[1][0] = p[1][0] / p[0] p[1][1] = p[1][1] / p[0] # src_num t[c[1]][0] = c[2] # start@src t[c[1]][2][0] = c[4] # cnt t[c[1]][2][1] = 0 # dir(1 = fw , -1 = bw, 0 = stay), t[c[1]][2][2] = c[5] # num t[c[1]][2][3] = c[6] # den t[c[1]][2][4] = c[7] # max_vol t[c[1]][3][0] = c[3] # b__free@cnt_x # actually worth nothang cuz type is zero t[c[1]][3][1] = c[8] # what_kinda_untouchableness t[c[1]][3][2] = c[9] print "arb thang", t[c[1]] # shoot(bullet, core, pulse, src, life) #[src_num, vol, [start@src, cnt, dir(1 = fw , -1 = bw, 0 = stay), num, den], [max_vol, b__free@cnt_x, what_kinda_untouchableness]] def shoot(b, c, p, s, lf, lyrs, sh): # there should b non pulse version too pero l8er if sh[0] == 1: cycl = (((sys.maxint - s[b[0]][1]) % sh[1]) + (s[b[0]][1] / p) + 1) * p else: # sh[0] == 0 version should b redesigned cycl = sh[1] / (1 + (sys.maxint - s[b[0]][1]) % 7) # cycl = (((sys.maxint - s[b[0]][1]) % 30) + (s[b[0]][1] / p) + 1) * p f = lf - cycl while f > 0: # think over whichtrack=2ndperm l8er c.append([f, 1 + f % (lyrs - 1) , b[0], b[1], 0, 1, 1, 1, s[b[0]][1], 2]) f = f - cycl # [@f, tracknum, srcnum, vol, strt, dir, num, den, timer, type] # make_patterns(lyr, src, pulse, noiz, mpttrn) def make_patterns(l, s, p, nz, mp): # def make_patterns(lyr, src, pulse, noiz): # currently noiz only cuz twist can bring in randoms pttrn = [] mm = sys.maxint sl = len(s) nl = len(nz) # sm = 0 how_many = 1 + (mm / (l + 1) - p) % 5 for w in range(how_many): # src_no = nz[(mm / (w + 1) - w * l * p) % nl] src_no = nz[((mm - l - p) / (w + 1)) % nl] vol = 10 + s[src_no][0][(mm - l * (p - sl) - 5 * w) % s[src_no][1]] % 91 strt = (mm - l * (p + sl) - vol * w) % s[src_no][1] if mp[0] == 1: cnt_length = p * (mp[1] + (mm / (w + 1) - strt) % (mp[2] + 1)) else: cnt_length = mp[1] + (mm - strt) % mp[2] # cnt_length = p * (1 + (mm / (w + 1) - strt) % 5) # cnt_length = 22222 + (mm - strt) % 111111 dir = -1 + (src_no + vol + strt + cnt_length) % 3 if dir == 0: dir = 1 rr = numden(mm / vol - strt * w) num = rr[0] den = rr[1] # den = 8 + (mm / vol - strt * w) % 9 # num = den / 8 + (mm / (w + 1) - cnt_length) % (2 * den - den / 8) # num = den / 4 + (mm / (w + 1) - cnt_length) % (int(1.5 * den) - den / 4) type = 2 + (mm - (den + w) * num) % 3 if type > 3: type = 0 pttrn.append([src_no, vol, strt, cnt_length, dir, num, den, type]) print pttrn return pttrn # [src_no, vol, strt, cnt_length, dir, num, den, type] # [[[pattern]..[pattern]], sum] # what_kinda_untouchableness?(0 = free, 1 = fixed, 2 = vol_only, 3 = twisty) def play_patterns(pttrn, c, l, s, p, tl): pt_len = len(pttrn) w = 0 f = 0 while f < l: tn = 1 + (f + 21 * w + pttrn[w][3]) % (tl - 1) c.append([f, tn, pttrn[w][0], pttrn[w][1], pttrn[w][2], pttrn[w][4], pttrn[w][5], pttrn[w][6], pttrn[w][3], pttrn[w][7]]) f = f + pttrn[w][3] w = (w + 1) % pt_len # [src_no, vol, strt, cnt_length, dir, num, den, type] # [@f, tracknum, srcnum, vol, strt, dir, num, den, timer, type] src = [] for fn in f_names: src.append(rsrc(fn)) s_len = len(src) for i in range(s_len): print 'src no.', i, '=', src[i][1] p_len = len(poetry) p_sum = 0 for poem in poetry: if len(poem) == 1: if poem[0] > 0: p_sum = p_sum + poem[0] else: p_sum = p_sum + src[poem[0]][1] for poem in poetry: if len(poem) == 1 and poem[0] == -1: poem[0] = life - p_sum p_sum = 0 for i in range(p_len): if len(poetry[i]) == 1: p_sum = p_sum + poetry[i][0] else: p_sum = p_sum + src[poetry[i][0]][1] acid[i].append(p_sum) print "the structure es" print poetry print acid core = [] strt = 0 # es the fade in fade out for cr in range(p_len): if len(poetry[cr]) == 1: strt = strt + poetry[cr][0] else: if cr > 0: pp = strt - prepoe vry = prepoe / poetry[cr][1] dlta = poetry[cr][1] / prepoe for tf in range(pp, strt): if vry != 0: if (tf - pp) % vry == 0: core.append([tf, 0, poetry[cr][0], (tf - pp) / vry, 0, 0, 1, 1, src[poetry[cr][0]][1], 1]) else: core.append([tf, 0, poetry[cr][0], dlta * (tf - pp), 0, 0, 1, 1, src[poetry[cr][0]][1], 1]) core.append([strt, 0, poetry[cr][0], poetry[cr][1], 0, 1, 1, 1, src[poetry[cr][0]][1], 1]) nd = strt + src[poetry[cr][0]][1] if cr < p_len - 1: pp = nd + prepoe vry = prepoe / poetry[cr][1] dlta = poetry[cr][1] / prepoe for tf in range(nd, pp): if vry != 0: if (tf - nd) % vry == 0: core.append([tf, 0, poetry[cr][0], poetry[cr][1] - (tf - nd) / vry, 0, 0, 1, 1, src[poetry[cr][0]][1], 1]) else: core.append([tf, 0, poetry[cr][0], poetry[cr][1] - dlta * (tf - nd + 1), 0, 0, 1, 1, src[poetry[cr][0]][1], 1]) core.append([pp, 0, poetry[cr][0], 0, 0, 0, 1, 1, src[poetry[cr][0]][1], 1]) strt = strt + src[poetry[cr][0]][1] # beccareful when the track es 0 # [@f, tracknum, srcnum, vol, strt, dir, num, den, timer, type] ev = 100 / layers tracks = [] for t in range(layers): if t == 0: tracks.append([0, 100 - ev * (layers - 1), [0, 0, 0, 1, 1], [100 - ev * (layers - 1), 0, 0]]) else: tracks.append([0, ev, [0, 0, 0, 1, 1], [ev, 0, 0]]) print "track init done" #[src_num, vol, [start@src, cnt, dir(1 = fw , -1 = bw, 0 = stay), num, den], [max_vol, b__free@cnt_x, what_kinda_untouchableness]] #[at_which_frame(can b the same as oters), max_vol, whichtrack, whichsrc, #startonsrc, dir(1 = fw, -1 = bw, 0 = staythere), num, den, #how_far_is_it_untouchable?(not frame but count), what_kinda_untouchableness?] for lyr in range(layers): play_patterns(make_patterns(lyr, src, pulse, noiz, mpttrn), core, life, src, pulse, layers) print "layer", lyr, "done" print "sorting core" core.sort() c_len = len(core) print "core sorted length =", c_len #[at_which_frame(can b the same as oters), whichtrack, whichsrc, max_vol, #startonsrc, dir(1 = fw, -1 = bw, 0 = staythere), num, den, #how_far_is_it_untouchable?(not frame but count), #what_kinda_untouchableness?(0 = free, 1 = fixed, 2 = vol_only, 3 = twisty) #2-4 twists are weaker than core episode = [] pool = [0, [0, 0]] # [poolvol, [average, average]] # pool = [0, [32768, 32768]] # how do i init this thang? x = 0 wa = 0 for f in range(life): if acid[wa][1] == f: wa = wa + 1 if x < c_len: while core[x][0] == f: arb_thang(pool, f, episode, tracks, src, core[x]) x = x + 1 if x == c_len: break for t in tracks: # change gotta b done before cnt get updated if f > 0 and pool[0] < 5 and (t[3][2] + t[3][1] + episode[2 * (f - 1)] + episode[2 * (f - 1) + 1]) % 11111 == 3 and t[3][2] == 0: # change_src(f, t, src, pool, tracks[0][0]) change_src(f, t, src, pool, acid[wa][0]) t[2][1] = t[2][1] + 1 if t[2][1] > t[3][1]: t[3][2] = 0 if f > 0 and (episode[2 * (f - 1)] + episode[2 * (f - 1) + 1] + t[0] + t[1] + t[2][1]) % 3210 == 7 and t[3][2] == 3: twist(f, t, src) breath(pool, f, tracks, src) giventake(f, tracks, layers) mix(f, tracks, src, episode, pool) print "pool =", pool[0] normalize(episode) for i in range(10000): episode[2 * i] = (i * episode[2 * i] + (10000 - i) * 32768) / 10000 episode[2 * i + 1] = (i * episode[2 * i + 1] + (10000 - i) * 32768) / 10000 episode[2 * life - 1 - 2 * i] = ((10000 - i) * episode[2 * life - 1 - 2 * i] + i * 32768) / 10000 episode[2 * life - 2 - 2 * i] = ((10000 - i) * episode[2 * life - 2 - 2 * i] + i * 32768) / 10000 e_thang = l2h(episode) a_thang = wave.open("episode.wav", "w") a_thang.setnchannels(2) a_thang.setsampwidth(2) a_thang.setframerate(44100) a_thang.writeframes(e_thang.tostring()) a_thang.close() print 'done'