def generate_transition(previous_phrase, next_phrase, nb_note_needed, pattern_index, key):
    
    previous_note = Note(previous_phrase[len(previous_phrase)-1])
    next_note = Note(next_phrase[0])
    selected_notes = get_note_between(previous_note, next_note, previous_phrase)
    temp_selected = get_note_between(previous_note, next_note, next_phrase)
    
    for i in range(len(temp_selected)):
        if temp_selected[i] not in selected_notes:
            selected_notes.append(temp_selected[i])
            
    if (float(len(selected_notes))/abs((nb_note_needed[0]+nb_note_needed[1])))<(float(3)/4):
        temp_from_scale = get_note_between(previous_note, next_note, generate_blues_scale(key))
        for i in range(len(temp_from_scale)):
            if temp_from_scale[i] not in selected_notes:
                selected_notes.append(temp_from_scale[i])
    
    first_bar = Bar()
    last_bar = Bar()
    
    for i in range(1, nb_note_needed[0]):
        time = first_bar.current_beat + 1
        list_compatible = get_compatible_notes(pattern_index, selected_notes, key, time)
        print "liste compatible : " + str(list_compatible) 
        list_length = get_max_length_note(first_bar, nb_note_needed[0]-i)
        best_notes = get_best_notes_transition(list_compatible, previous_note, next_note, 0,  float(i)/nb_note_needed[0])
        print "best_notes : " + str(best_notes)
        chosen_note = best_notes[random.randint(0, len(best_notes)-1)]
        chosen_length = list_length[random.randint(0, len(list_length)-1)]
        first_bar.place_notes(chosen_note.name, chosen_length)
        
    for i in range(1, nb_note_needed[1]):
        time = last_bar.current_beat + 1
        list_compatible = get_compatible_notes(pattern_index, selected_notes, key, time)
        list_length = get_max_length_note(last_bar, nb_note_needed[1]-i)
        best_notes = get_best_notes_transition(list_compatible, previous_note, next_note, 1, float(i)/nb_note_needed[1])
        chosen_note = best_notes[random.randint(0, len(best_notes)-1)]
        chosen_length = list_length[random.randint(0, len(list_length)-1)]
        last_bar.place_notes(chosen_note.name, chosen_length)
    
    if last_bar.length - last_bar.current_beat != 0 : 
        print("ajout de silence")
        space_left = 1.0 / (last_bar.length - last_bar.current_beat)
        last_bar.place_rest(space_left) 
    
    if first_bar.length - first_bar.current_beat != 0 : 
        print("ajout de silence")
        space_left = 1.0 / (first_bar.length - first_bar.current_beat)
        first_bar.place_rest(space_left)         
    
    
        
    return [first_bar, last_bar]
Beispiel #2
0
def drum_beat(beat_probas):
    b = Bar()

    for (bass, snare, hhat) in beat_probas:
        p = random()
        if p < bass:
            b.place_notes(BASS, 8)
        elif p < bass + snare:
            b.place_notes(SNARE, 8)
        elif p < bass + snare + hhat:
            b.place_notes(HIHAT, 8)
        else:
            b.place_rest(8)

    return b
Beispiel #3
0
def drum_beat(beat_probas):
    b = Bar()

    for (bass, snare, hhat) in beat_probas:
        p = random()
        if p < bass:
            b.place_notes(BASS, 8)
        elif p < bass + snare:
            b.place_notes(SNARE, 8)
        elif p < bass + snare + hhat:
            b.place_notes(HIHAT, 8)
        else:
            b.place_rest(8)

    return b
Beispiel #4
0
def bassline(chord, beat_probas):

    chord_tones = chords.from_shorthand(chord)
    octave = 2

    b = Bar()

    for (bass, els, octa) in beat_probas:
        p = random.random()
        if p < bass:
            b.place_notes(Note(chord_tones[0], octave), 8)
        elif p < bass + els:
            b.place_notes(Note(random.choice(chord_tones[1:]), octave), 8)
        elif p < bass + els + octa:
            b.place_notes(Note(chord_tones[0], octave + 1), 8)
        else:
            b.place_rest(8)

    return b
Beispiel #5
0
def bassline(chord, beat_probas):

    chord_tones = chords.from_shorthand(chord)
    octave = 2

    b = Bar()

    for (bass, els, octa) in beat_probas:
        p = random.random()
        if p < bass:
            b.place_notes(Note(chord_tones[0], octave), 8)
        elif p < bass + els:
            b.place_notes(Note(random.choice(chord_tones[1:]), octave), 8)
        elif p < bass + els + octa:
            b.place_notes(Note(chord_tones[0], octave + 1), 8)
        else:
            b.place_rest(8)

    return b
Beispiel #6
0
def generate_end_bars(previews_bar, previews_bar_notes, pattern_index, key, mode):
    # on genere deux mesures de fin
    # previews_bar = format de la bdd
    nb_notes = get_nb_notes(previews_bar)
    first_bar = Bar()
    last_bar = Bar()
    last_index = len(previews_bar_notes)-1
    last_note = previews_bar_notes[last_index]
    
    for i in range(1, nb_notes[0]):
        time = first_bar.current_beat + 1
        list_compatible = get_compatible_notes(pattern_index, previews_bar_notes, key, time)
        list_length = get_max_length_note(first_bar, nb_notes[0]-i)
        best_notes = get_best_notes(list_compatible, last_note)
        chosen_note = best_notes[random.randint(0, len(best_notes)-1)]
        chosen_length = list_length[random.randint(0, len(list_length)-1)]
        first_bar.place_notes(chosen_note.name, chosen_length)
        
    if first_bar.length - first_bar.current_beat != 0 : 
        print("ajout de silence")
        space_left = 1.0 / (first_bar.length - first_bar.current_beat)
        first_bar.place_rest(space_left)
    
        
    for i in range(1, nb_notes[1]):
        time = last_bar.current_beat + 1
        list_compatible = get_compatible_notes(pattern_index, previews_bar_notes, key, time)
        list_length = get_max_length_note(last_bar, nb_notes[1]-i)
        best_notes = get_best_notes(list_compatible, last_note)
        chosen_note = best_notes[random.randint(0, len(best_notes)-1)]
        chosen_length = list_length[random.randint(0, len(list_length)-1)]
        #chosen_note.octave
        
        chord_possible = []
        chord_possible.append(intervals.unison(key, key))
        chord_possible.append(intervals.fourth(key, key))
        chord_possible.append(intervals.fifth(key, key))
        
        intervals_last = []
        
        for note_possible in chord_possible :
            test = []
            test.append(intervals.measure(note_possible, last_note.name))
            test.append(intervals.measure(last_note.name, note_possible))
            intervals_last.append(min(test))
        
        for i,j in enumerate(intervals_last) :
            if j == min(intervals_last) :
                index = i
                break
            
        chosen_chord = chord_possible[index] 
        
        if mode == "mixolydien" :
                            
            chord = chords.triad(chosen_chord, key)
            chord_list = []
            for note in chord :
                note_m = Note(note, chosen_note.octave)
                chord_list.append(note_m)
                last_note = note_m
            last_bar.place_notes(chord_list, chosen_length)
        else :
            chord = chords.triad(chosen_chord, key)
            chord_list = []
            for note in chord :
                note_m = Note(note, chosen_note.octave)
                last_note = note_m
                chord_list.append(note_m)
            last_bar.place_notes(chord_list, chosen_length)
            
    if last_bar.length - last_bar.current_beat != 0 : 
        print("ajout de silence")
        space_left = 1.0 / (last_bar.length - last_bar.current_beat)
        last_bar.place_rest(space_left)     
            
    return [first_bar, last_bar]
        
        
        
    
        
        
       
Beispiel #7
0
def generate_long_right_hand(phrase_list, progression_list, nb_bars, pattern_index, mode, key, chorus):
    t = Track()
    k = 0
    # on parcours la liste de phrases 
    for phrase in phrase_list :
        k += 1
        previews_note = None
        nb_p = 0
        for bar in phrase[1][3] :
            list_bar = generate_bar(previews_note, bar, key, mode, 'I', progression_list, nb_p, pattern_index)
            b = list_bar[0]
            last_note = list_bar[1]
            nb_bars+=1
            t.add_bar(b)
            last_bar = bar
            nb_p+=1

        if phrase[1][1] < 4 :
            # composer le reste TODO en attendant on met du vide
            last_bar_notes = list_bar[2]
        
            next_index = nb_p+4
            if next_index < 9 :
                next_progression = progression_list[0][next_index]
                next_phrase = chorus[1]
                first_bar = next_phrase[1][3][0]
                first_bar_list = generate_bar(last_note, first_bar, key, mode, next_progression, progression_list, nb_p+4, pattern_index)
                first_bar_notes = first_bar_list[2]
              
                nb_notes_to_generate = get_nb_note_needed(last_bar, first_bar)
              
                print "****************** je vais generer la transition ! ****************"
                transition_bar = generate_transition(first_bar_notes, last_bar_notes, nb_notes_to_generate, pattern_index, key)
                print "***************************** Fini ! ******************************"
                                                
                for i in range(len(transition_bar)):
                    t.add_bar(transition_bar[i])
        
        # on ajoute le refrain
        for chorus_bars in chorus[0][2] :
            t.add_bar(chorus_bars)
            
        if k == len(phrase_list) :
            list_end = generate_end_bars(chorus[0][2][1], chorus[3], pattern_index, key, mode)
            for end_bar in list_end :
                t.add_bar(end_bar)
        
        #generer transition vers la phrase suivante
        #ou generer la fin


        for i in range(2): 
            b = Bar(key, (4,4))
            b.place_rest(1)
            t.add_bar(b)
            
        
        
            
    #generer phrase + genererer transition
    # ajouter refrain
    # si ce n'est pas la fin : generer transition entre refrain et prochaine phrase
    # sinon, generer fin
    
    
    return t