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 generate_blues_scale(key="C"): """Returns an ordered list of the notes of the blues scale in this key. \ For example: if the key is set to 'C', this function will return \ `['C', 'D#', 'F', 'F#', 'G', 'A#']`. \ This function will raise an !NoteFormatError if the key isn't recognised""" if not (notes.is_valid_note(key)): raise NoteFormatError, "Unrecognised format for key '%s'" % key result = [] fifth_index = notes.fifths.index(key[0]) result.append(intervals.unison(key)) result.append(notes.diminish(intervals.third(key, key))) result.append(intervals.third(key, key)) result.append(intervals.fourth(key, key)) result.append(notes.diminish(intervals.fifth(key, key))) result.append(intervals.fifth(key, key)) result.append(notes.diminish(intervals.seventh(key, key))) # Remove redundant #'s and b's from the result result = map(notes.remove_redundant_accidentals, result) tonic = result.index(notes.remove_redundant_accidentals(key)) result = result[tonic:] + result[:tonic] return result
def get_progression_key(note_int, key): if note_int == 1 : p_key = intervals.unison(key) if note_int == 4 : p_key = intervals.fourth(key, key) if note_int == 5 : p_key = intervals.fifth(key, key) return p_key
def second_voice_ending(second_track, key): first = key fourth = intervals.fourth(key, key) fifth = intervals.fifth(key, key) second_track.add_notes(Note(first, 3), 2) second_track.add_notes(Note(fourth, 3), 2) second_track.add_notes(Note(fifth, 3), 2) second_track.add_notes(Note(fifth, 3), 1)
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 get_note_pattern(pattern, key): if pattern[0] == 1 : note = intervals.unison(key) elif pattern[0] == 2 : note = intervals.second(key, key) elif pattern[0] == 3 : note = intervals.third(key, key) elif pattern[0] == 4 : note = intervals.fourth(key, key) elif pattern[0] == 5 : note = intervals.fifth(key, key) elif pattern[0] == 6 : note = intervals.sixth(key, key) elif pattern[0] == 7 : note = intervals.seventh(key, key) if pattern[3] == "bemol": note = notes.diminish(note) elif pattern[3] == "diese" : note = notes.augment(note) return note
def ending(first_track, second_track, subject, key=r""): if not bool(key): key = 'C' canon_subject = shift(subject, 2) cadence = [chord.I(key), chord.IV(key), chord.V(key), chord.I(key)] notes = [] for note in canon_subject[-1]: if note[-1][0].name not in notes: notes.append(note[-1][0].name) bar = Bar(key=key) # Hitta om det finns ett ackord med de sista noterna i subjektet i, i så fall, komplettera det ackordet if len(notes) <= 2 or len(chord.determine(notes, True)) == 0: chord_notes = [intervals.fourth(notes[0], key)] else: subject_chord = chord.determine(notes, True)[0] chord_notes = chord.from_shorthand(subject_chord) if len(chord_notes) != len(notes): for note in notes: chord_notes.remove(note) else: chord_notes = chord_notes[0] bar.place_notes(chord_notes[random.randint(0, len(chord_notes) - 1)], 2) first_track.add_bar( bar) #Sätt något som passar med andra hälften av subjektet while second_track[ -1].current_beat < 1: #Placing fitting notes in second to last bar #print('test1') duration = 2**random.randint(1, 3) pitch = cadence[0][random.randint(1, len(cadence[0]) - 1)] # If the randomized duration doesn't fit in the bar, make it fit if 1 / duration > 1 - second_track[-1].current_beat: duration = 1 / (1 - second_track[-1].current_beat) # Place the new note in the bar second_track[-1].place_notes(pitch, duration) first_track[-1].place_notes(cadence[0][0], 2) bar = Bar(key=key) bar.place_notes(cadence[1][0], 2) bar.place_notes(cadence[2][0], 2) first_track.add_bar(bar) bar = Bar(key=key) bar.place_notes(cadence[3][0], 1) first_track.add_bar(bar) bar = Bar(key=key) while bar.current_beat < 0.5: #print('test2') # Randomize pitch and duration of each note. duration = 2**random.randint(1, 3) pitch = cadence[1][random.randint(1, len(cadence[1]) - 1)] # If the randomized duration doesn't fit in the bar, make it fit if 1 / duration > 1 - bar.current_beat: duration = 1 / (1 - bar.current_beat) # Place the new note in the bar bar.place_notes(pitch, duration) while bar.current_beat < 1: #print('test3') duration = 2**random.randint(1, 3) pitch = cadence[2][random.randint(1, len(cadence[2]) - 1)] # If the randomized duration doesn't fit in the bar, make it fit if 1 / duration > 1 - bar.current_beat: duration = 1 / (1 - bar.current_beat) # Place the new note in the bar bar.place_notes(pitch, duration) second_track.add_bar(bar) bar = Bar(key=key) bar.place_notes(cadence[3][2], 1) second_track.add_bar(bar)
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)
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]