def transpose(self, interval, up = True): """Transposes the note up or down the interval. {{{ >>> a = Note("A") >>> a.transpose("3") >>> a 'C#-5' >>> a.transpose("3", False) >>> a 'A-4' }}}""" if type(interval) == int: for _ in range(interval): if up: self.augment() else: self.diminish() else: old, o_octave = self.name, self.octave self.name = intervals.from_shorthand(self.name, interval, up) if up: if self < Note(old, o_octave): self.octave += 1 else: if self > Note(old, o_octave): self.octave -= 1
def transpose(self, interval, up = True): """Transposes the note up or down the interval. {{{ >>> a = Note("A") >>> a.transpose("3") >>> a 'C#-5' >>> a.transpose("3", False) >>> a 'A-4' }}}""" old, o_octave = self.name, self.octave self.name = intervals.from_shorthand(self.name, interval, up) if up: if self < Note(old, o_octave): self.octave += 1 else: if self > Note(old, o_octave): self.octave -= 1
def transpose(self, interval, up=True): """Transpose the note up or down the interval. Examples: >>> a = Note('A') >>> a.transpose('3') >>> a 'C#-5' >>> a.transpose('3', False) >>> a 'A-4' """ (old, o_octave) = (self.name, self.octave) self.name = intervals.from_shorthand(self.name, interval, up) if up: if self < Note(old, o_octave): self.octave += 1 else: if self > Note(old, o_octave): self.octave -= 1
def transpose(self, interval, up=True): """Transposes the note up or down the interval. {{{ >>> a = Note(\"A\") >>> a.transpose(\"3\") >>> a 'C#-5' >>> a.transpose(\"3\", False) >>> a 'A-4' }}}""" (old, o_octave) = (self.name, self.octave) self.name = intervals.from_shorthand(self.name, interval, up) if up: if self < Note(old, o_octave): self.octave += 1 else: if self > Note(old, o_octave): self.octave -= 1
def diminished_seventh(note): new = from_shorthand(note, 'b7') return new
def augmented_second(note): second = from_shorthand(note, '#2') return second
def test_from_shorthand(self): self.assertEqual("C", intervals.from_shorthand("A", "b3")) self.assertEqual("A", intervals.from_shorthand("C", "b3", False)) self.assertEqual("A", intervals.from_shorthand("C#", "3", False)) self.assertEqual("Cb", intervals.from_shorthand("A", "bb3")) self.assertEqual("Cbb", intervals.from_shorthand("A", "bbb3")) self.assertEqual("F###", intervals.from_shorthand("A", "bbb3", False)) self.assertEqual("C#", intervals.from_shorthand("A", "3")) self.assertEqual("C##", intervals.from_shorthand("A", "#3")) self.assertEqual("C###", intervals.from_shorthand("A", "##3")) self.assertEqual("E", intervals.from_shorthand("D", "2")) self.assertEqual("F#", intervals.from_shorthand("D", "3"))
def augmented_fourth(note): fourth = from_shorthand(note, '#4') return fourth
def minor_unison(note): new = from_shorthand(note, 'b1') return new
def test_from_shorthand(self): self.assertEqual('C', intervals.from_shorthand('A', 'b3')) self.assertEqual('A', intervals.from_shorthand('C', 'b3', False)) self.assertEqual('A', intervals.from_shorthand('C#', '3', False)) self.assertEqual('Cb', intervals.from_shorthand('A', 'bb3')) self.assertEqual('Cbb', intervals.from_shorthand('A', 'bbb3')) self.assertEqual('F###', intervals.from_shorthand('A', 'bbb3', False)) self.assertEqual('C#', intervals.from_shorthand('A', '3')) self.assertEqual('C##', intervals.from_shorthand('A', '#3')) self.assertEqual('C###', intervals.from_shorthand('A', '##3')) self.assertEqual('E', intervals.from_shorthand('D', '2')) self.assertEqual('F#', intervals.from_shorthand('D', '3'))
def augmented_fifth(note): fifth = from_shorthand(note, '#5') return fifth
def diminished_unison(note): new = from_shorthand(note, 'b1') return new
def diminished_fourth(note): new = from_shorthand(note, 'b4') return new
def diminished_sixth(note): new = from_shorthand(note, 'b6') return new
def diminished_third(note): new = from_shorthand(note, 'b3') return new
def augmented_third(note): new = from_shorthand(note, '#3') return new
def diminished_fifth(note): new = from_shorthand(note, 'b5') return new
def augmented_sixth(note): sixth = from_shorthand(note, '#4') return sixth
def diminished_second(note): new = from_shorthand(note, 'b2') return new
def generate_fugue(key, subject): #If subject doesn't fill full bars fill out rest of last bar of subject with rest #if last bar is not full if not (subject[-1].is_full()): #place a rest at the end of the last bar with the length of 1/(remaining fraction of bar) subject[-1].place_rest(int(1.0 / subject[-1].space_left())) # Create first bar with subject in first voice and rest in second voice. rest_1bar = Bar(key) rest_1bar.place_rest(1) first_voice = copy.deepcopy(subject) #Add same amount of "rest bars" as the number of bars in the subject for i in range(len(subject)): second_voice.add_bar(copy.deepcopy(rest_1bar)) # Create second bar with answer in second voice. answer = Track_Functions.create_answer(subject, key) #second_voice = second_voice + answer Track_Functions.add_tracks(second_voice, answer) # Generate countersubject eg_counter = EvolutionaryGenerator(key, nr_bars=1, fitness_function='counter', input_melody=subject, nr_generations=counter_nr_generations) print('Generating evolutionary part 1 of 6') eg_counter.run_evolution() counter_subject = copy.deepcopy(eg_counter.best_individual) Track_Functions.add_tracks(first_voice, counter_subject) # Save bar 2 for later modulation bar_2 = first_voice[-1] # Generate development in minor in bar 5 and 6. # Transposed -3 to minor + (stämma i för second voice tills vidare tom) minor_first_voice = Track_Functions.transpose_to_relative_minor( first_voice, key, False) minor_second_voice = Track_Functions.transpose_to_relative_minor( second_voice, key, False) bar_5 = minor_first_voice[0] # Generate harmony in second voice in bar 5 eg_harmony_minor = EvolutionaryGenerator( key, nr_bars=1, fitness_function='harmony', input_melody=Track().add_bar(copy.deepcopy(minor_first_voice[0])), nr_generations=harmony_nr_generations) print('Generating evolutionary part 2 of 6') eg_harmony_minor.run_evolution() minor_second_voice[0] = eg_harmony_minor.best_individual[0] # Generate bar 3 and 4 as a modulation between bar 2 and 5 minor_key = intervals.from_shorthand(key, 'b3', False) minor_key += 'm' eg_modulate_to_minor = EvolutionaryGenerator( key, nr_bars=2, fitness_function='modulate', from_bar=bar_2, to_bar=bar_5, from_key=key, to_key=minor_key, nr_generations=modulate_nr_generations) print('Generating evolutionary part 3 of 6') eg_modulate_to_minor.run_evolution() modulate_first_voice = copy.deepcopy(eg_modulate_to_minor.best_individual) # Generate second voice as harmony to the first voice in bar 3 and 4 eg_second_voice_modulate = EvolutionaryGenerator( key, nr_bars=2, fitness_function='harmony', input_melody=modulate_first_voice, from_key='C', to_key='Am', nr_generations=harmony_nr_generations) print('Generating evolutionary part 4 of 6') eg_second_voice_modulate.run_evolution() modulate_second_voice = copy.deepcopy( eg_second_voice_modulate.best_individual) # Add bar 3-6 to the voice tracks Track_Functions.add_tracks(first_voice, modulate_first_voice) Track_Functions.add_tracks(second_voice, modulate_second_voice) Track_Functions.add_tracks(first_voice, minor_first_voice) Track_Functions.add_tracks(second_voice, minor_second_voice) bar_6 = first_voice[-1] # Create canon in bar 9 and 10. # subject i first voice # second voice is subject but shifted (half a bar for now) canon_first_voice = Track() canon_first_voice.add_bar(copy.deepcopy(subject[0])) bar_9 = canon_first_voice[0] canon_second_voice = Track_Functions.shift(subject, 2) # Create modulation from minor to major in 7 and 8 eg_modulate_to_major = EvolutionaryGenerator( key, nr_bars=2, fitness_function='modulate', from_bar=bar_6, to_bar=bar_9, from_key='Am', to_key='C', nr_generations=modulate_nr_generations) print('Generating evolutionary part 5 of 6') eg_modulate_to_major.run_evolution() modulate_back_first_voice = copy.deepcopy( eg_modulate_to_major.best_individual) # Generate second voice as harmony to the first voice in bar 7 and 8 eg_second_voice_modulate_back = EvolutionaryGenerator( key, nr_bars=2, fitness_function='harmony', input_melody=modulate_first_voice, from_key='Am', to_key='C', nr_generations=harmony_nr_generations) print('Generating evolutionary part 6 of 6') eg_second_voice_modulate_back.run_evolution() modulate_back_second_voice = copy.deepcopy( eg_second_voice_modulate.best_individual) # Add bar 7-10 to the voice tracks Track_Functions.add_tracks(first_voice, modulate_back_first_voice) Track_Functions.add_tracks(second_voice, modulate_back_second_voice) Track_Functions.add_tracks(first_voice, canon_first_voice) Track_Functions.add_tracks(second_voice, canon_second_voice) Track_Functions.ending(first_voice, second_voice, subject, key) #Add voices together to create a final composition fugue.add_track(first_voice) fugue.add_track(second_voice) #Generate lilypond file for fugue named final_fugue finished_fugue = LilyPond.from_Composition(fugue) to_LilyPond_file(finished_fugue, "final_fugue") #Generate MIDI output for fugue named final_fugue midi_file_out.write_Composition("final_fugue.mid", fugue)