def common_note(phrase1, phrase2, key, mode):
    phrase1_list = []
    percentage = 0
    number_notes = 0
    key1 = key
    key2 = key
    
    if mode != "none" :     
        if phrase1[0]==0 :
            key1 = key
        elif phrase1[0]==4 :
            key1 = intervals.fourth(key, key)
        elif phrase1[0]==5:
            key1 = intervals.fifth(key, key)
            
        if phrase2[0]==0 :
            key2 = key
        elif phrase2[0]==4 :
            key2 = intervals.fourth(key, key)
        elif phrase2[0]==5:
            key2 = intervals.fifth(key, key)

    for bars in  phrase1[1][3] :
        for n in bars :
            notes = get_note(n, key1)
            phrase1_list.append(notes)
        
    for bars in phrase2[1][3] :
        for n in bars :
            number_notes+=1
            if get_note(n, key2) in phrase1_list:
                percentage+=1
    
    return (percentage/number_notes)*100
def gap_last_first_note(phrase1, phrase2, key, mode): 
    
    key1 = key
    key2 = key
    if mode != "none" : 
        if phrase1[0]==0 :
            key1 = key
        elif phrase1[0]==4 :
            key1 = intervals.fourth(key, key)
        elif phrase1[0]==5:
            key1 = intervals.fifth(key, key)
            
        if phrase2[0]==0 :
            key2 = key
        elif phrase2[0]==4 :
            key2 = intervals.fourth(key, key)
        elif phrase2[0]==5:
            key2 = intervals.fifth(key, key)
          
    last_note_1 = phrase1[1][3][phrase2[1][1]-1][-1:][0]
    last_note_2 = phrase2[1][3][phrase2[1][1]-1][-1:][0]
    last_note_1 = get_note(last_note_1, key1)
    last_note_2 = get_note(last_note_2, key2)
    return intervals.measure(last_note_1, last_note_2)
def generate_bar(previews_note, bar, key, mode, progression, progression_list, nb_p, pattern_index):
    if mode == 'mixolydien' :
        if progression == 'IV' :
            key = intervals.fourth(key, key)
        elif progression == 'V' :
            key = intervals.fifth(key, key)
        
    b = Bar(key, (4, 4))
    position_note = 0
    already_used=[]
    list_note = []
    
    for bar_note in bar :
        if position_note not in already_used :
            is_chord = chord_length(bar_note, bar, position_note)
            if is_chord[2] :
                note_list = []
                # c est un accord
                for p_note in bar[is_chord[0]:is_chord[1]+1] :
                    note_str = get_note(p_note, key)
                    note = Note(note_str, p_note[5])
                    if previews_note is not None:
                        if p_note[4]=='+':
                            if int(note) < previews_note :
                                note.octave_up()
                        elif p_note[4]=='-':
                            if int(note) > previews_note :
                                note.octave_down()
                    current_progression = progression_list[0][nb_p]
                    note = change_note_if_needed(bar_note[2], note, pattern_index, current_progression, bar_note, key)     
                    previews_note = int(note)
                    
                    #appeler best_note la aussi
                    note_list.append(note)
                    list_note.append(note)
                    
                for n in range(is_chord[0], is_chord[1]+1):
                    already_used.append(n)
                
                b.place_notes(note_list, bar_note[1])

                        
            else :  
                note_str = get_note(bar_note, key)
                note = Note(note_str, bar_note[5])
                
                if previews_note is not None:
                    if bar_note[4]=='+':
                        if int(note) < previews_note :
                            note.octave_up()

                    elif bar_note[4]=='-':
                        if int(note) > previews_note :
                            note.octave_down()
                
                # la un faut appeler le truc pour modifier la note
                current_progression = progression_list[0][nb_p]
                note = change_note_if_needed(bar_note[2], note, pattern_index, current_progression, bar_note, key)
                            
                previews_note = int(note)
                list_note.append(note)
                b.place_notes(note, bar_note[1])
                already_used.append(position_note)
        position_note+=1
     
    return (b, previews_note, list_note)