notes = []
for t in range(len(g)):
    chord = {}
    vol_orig = g[t].mean()
    while g[t].mean() > 0:
        note_probs = np.dot(notes_start, g[t])
        i = note_probs.argmax()
        best_match = (g[t] * notes_start[i]).argmax()
        vol = float(g[t][best_match]) / notes_start[i][best_match]
        if vol < 1e-2: break  #if int(100*vol)
        if vol * g[t].mean() / vol_orig > 0.2:
            chord[i] = chord.get(i, 0) + vol
        #print i,vol
        g[t] -= (vol * notes_start[i]).astype(int)

    for i in chord:
        if active.get(i) and chord[i] > active[i].volume:
            notes.append(active[i])

        if active.get(i) and chord[i] < active[i].volume:
            active[i].dur += 0.25

        else:
            active[i] = Note(classes[i])
            active[i].time = t
            active[i].volume = 70  #max(int(min(100*chord[i], 100)),10)
    for i in set(active) - set(chord):  # + those new
        notes.append(active[i])
        del active[i]

test_output(notes)
notes = []
for t in range(len(g)):
  chord = {}
  vol_orig = g[t].mean()
  while g[t].mean() > 0:
    note_probs = np.dot(notes_start, g[t])
    i = note_probs.argmax()
    best_match = (g[t]*notes_start[i]).argmax()
    vol = float(g[t][best_match]) / notes_start[i][best_match]
    if vol < 1e-2: break #if int(100*vol)
    if vol * g[t].mean() / vol_orig > 0.2:
      chord[i] = chord.get(i,0) + vol
    #print i,vol
    g[t] -= (vol*notes_start[i]).astype(int)
  
  for i in chord:
    if active.get(i) and chord[i] > active[i].volume:
      notes.append(active[i])
        
    if active.get(i) and chord[i] < active[i].volume:
      active[i].dur += 0.25
    
    else:
      active[i] = Note(classes[i])
      active[i].time = t
      active[i].volume = 70#max(int(min(100*chord[i], 100)),10)
  for i in set(active) - set(chord): # + those new
    notes.append(active[i])
    del active[i]    

test_output(notes)